1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Implements the Chrome Extensions Tab Capture API.
#include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h"
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/media/webrtc/capture_policy_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"
#include "extensions/common/features/simple_feature.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/switches.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
using content::DesktopMediaID;
using content::WebContentsMediaCaptureId;
using extensions::api::tab_capture::MediaStreamConstraint;
namespace TabCapture = extensions::api::tab_capture;
namespace GetCapturedTabs = TabCapture::GetCapturedTabs;
namespace extensions {
namespace {
const char kCapturingSameTab[] = "Cannot capture a tab with an active stream.";
const char kFindingTabError[] = "Error finding tab to capture.";
const char kNoAudioOrVideo[] = "Capture failed. No audio or video requested.";
const char kGrantError[] =
"Extension has not been invoked for the current page (see activeTab "
"permission). Chrome pages cannot be captured.";
const char kInvalidOriginError[] = "Caller tab.url is not a valid URL.";
const char kInvalidTabIdError[] = "Invalid tab specified.";
const char kTabUrlNotSecure[] =
"URL scheme for the specified tab is not secure.";
// Keys/values passed to renderer-side JS bindings.
const char kMediaStreamSource[] = "chromeMediaSource";
const char kMediaStreamSourceId[] = "chromeMediaSourceId";
const char kMediaStreamSourceTab[] = "tab";
bool OptionsSpecifyAudioOrVideo(const TabCapture::CaptureOptions& options) {
return (options.audio && *options.audio) || (options.video && *options.video);
}
DesktopMediaID BuildDesktopMediaID(content::WebContents* target_contents,
TabCapture::CaptureOptions* options) {
content::RenderFrameHost* const target_frame =
target_contents->GetPrimaryMainFrame();
DesktopMediaID source(
DesktopMediaID::TYPE_WEB_CONTENTS, DesktopMediaID::kNullId,
WebContentsMediaCaptureId(target_frame->GetProcess()->GetDeprecatedID(),
target_frame->GetRoutingID()));
return source;
}
// Add Chrome-specific source identifiers to the MediaStreamConstraints objects
// in |options| to provide references to the |target_contents| to be captured.
void AddMediaStreamSourceConstraints(content::WebContents* target_contents,
TabCapture::CaptureOptions* options,
const std::string& device_id) {
DCHECK(options);
DCHECK(target_contents);
MediaStreamConstraint* constraints_to_modify[2] = {nullptr, nullptr};
if (options->audio && *options->audio) {
if (!options->audio_constraints)
options->audio_constraints.emplace();
constraints_to_modify[0] = &*options->audio_constraints;
}
if (options->video && *options->video) {
if (!options->video_constraints)
options->video_constraints.emplace();
constraints_to_modify[1] = &*options->video_constraints;
}
// Append chrome specific tab constraints.
for (MediaStreamConstraint* msc : constraints_to_modify) {
if (!msc)
continue;
base::Value::Dict* constraint = &msc->mandatory.additional_properties;
constraint->Set(kMediaStreamSource, kMediaStreamSourceTab);
constraint->Set(kMediaStreamSourceId, device_id);
}
}
// Find the last-active browser that matches a profile this ExtensionFunction
// can access. We can't use FindLastActiveWithProfile() because we may want to
// include incognito profile browsers.
Browser* GetLastActiveBrowser(const Profile* profile,
const bool match_incognito_profile) {
Browser* target_browser = nullptr;
for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
Profile* browser_profile = browser->profile();
if (browser_profile == profile ||
(match_incognito_profile &&
browser_profile->GetOriginalProfile() == profile)) {
target_browser = browser;
break;
}
}
return target_browser;
}
// Get the id of the allowlisted extension.
std::string GetAllowlistedExtensionID() {
return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kAllowlistedExtensionID);
}
} // namespace
ExtensionFunction::ResponseAction TabCaptureCaptureFunction::Run() {
std::optional<api::tab_capture::Capture::Params> params =
TabCapture::Capture::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
Profile* profile = Profile::FromBrowserContext(browser_context());
const bool match_incognito_profile = include_incognito_information();
Browser* target_browser =
GetLastActiveBrowser(profile, match_incognito_profile);
if (!target_browser)
return RespondNow(Error(kFindingTabError));
content::WebContents* target_contents =
target_browser->tab_strip_model()->GetActiveWebContents();
if (!target_contents)
return RespondNow(Error(kFindingTabError));
content::WebContents* const extension_web_contents = GetSenderWebContents();
EXTENSION_FUNCTION_VALIDATE(extension_web_contents);
const GURL& extension_origin =
extension_web_contents->GetLastCommittedURL().DeprecatedGetOriginAsURL();
AllowedScreenCaptureLevel capture_level =
capture_policy::GetAllowedCaptureLevel(
extension_web_contents->GetLastCommittedURL()
.DeprecatedGetOriginAsURL(),
extension_web_contents);
DesktopMediaList::WebContentsFilter includable_web_contents_filter =
capture_policy::GetIncludableWebContentsFilter(extension_origin,
capture_level);
if (!includable_web_contents_filter.Run(target_contents)) {
return RespondNow(Error(kGrantError));
}
const std::string& extension_id = extension()->id();
// Make sure either we have been granted permission to capture through an
// extension icon click or our extension is allowlisted.
if (!extension()->permissions_data()->HasAPIPermissionForTab(
sessions::SessionTabHelper::IdForTab(target_contents).id(),
mojom::APIPermissionID::kTabCaptureForTab) &&
(GetAllowlistedExtensionID() != extension_id)) {
return RespondNow(Error(kGrantError));
}
if (!OptionsSpecifyAudioOrVideo(params->options))
return RespondNow(Error(kNoAudioOrVideo));
DesktopMediaID source =
BuildDesktopMediaID(target_contents, ¶ms->options);
TabCaptureRegistry* registry = TabCaptureRegistry::Get(browser_context());
content::RenderFrameHost* main_frame =
extension_web_contents->GetPrimaryMainFrame();
int caller_process_id = main_frame->GetProcess()->GetDeprecatedID();
int frame_id = main_frame->GetRoutingID();
std::string device_id = registry->AddRequest(
target_contents, extension_id, false, extension()->url(), source,
caller_process_id, frame_id);
if (device_id.empty()) {
return RespondNow(Error(kCapturingSameTab));
}
AddMediaStreamSourceConstraints(target_contents, ¶ms->options, device_id);
// At this point, everything is set up in the browser process. It's now up to
// the custom JS bindings in the extension's render process to request a
// MediaStream using navigator.webkitGetUserMedia(). The result dictionary,
// passed to SetResult() here, contains the extra "hidden options" that will
// allow the Chrome platform implementation for getUserMedia() to start the
// virtual audio/video capture devices and set up all the data flows. The
// custom JS bindings can be found here:
// chrome/renderer/resources/extensions/tab_capture_custom_bindings.js
return RespondNow(WithArguments(params->options.ToValue()));
}
ExtensionFunction::ResponseAction TabCaptureGetCapturedTabsFunction::Run() {
TabCaptureRegistry* registry = TabCaptureRegistry::Get(browser_context());
base::Value::List list;
if (registry)
registry->GetCapturedTabs(extension()->id(), &list);
return RespondNow(WithArguments(std::move(list)));
}
ExtensionFunction::ResponseAction TabCaptureGetMediaStreamIdFunction::Run() {
std::optional<api::tab_capture::GetMediaStreamId::Params> params =
TabCapture::GetMediaStreamId::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
content::WebContents* target_contents = nullptr;
if (params->options && params->options->target_tab_id) {
if (!ExtensionTabUtil::GetTabById(*(params->options->target_tab_id),
browser_context(), true,
&target_contents)) {
return RespondNow(Error(kInvalidTabIdError));
}
} else {
Profile* profile = Profile::FromBrowserContext(browser_context());
const bool match_incognito_profile = include_incognito_information();
Browser* target_browser =
GetLastActiveBrowser(profile, match_incognito_profile);
if (!target_browser)
return RespondNow(Error(kFindingTabError));
target_contents = target_browser->tab_strip_model()->GetActiveWebContents();
}
if (!target_contents)
return RespondNow(Error(kFindingTabError));
const std::string& extension_id = extension()->id();
// Make sure either we have been granted permission to capture through an
// extension icon click or our extension is allowlisted.
if (!extension()->permissions_data()->HasAPIPermissionForTab(
sessions::SessionTabHelper::IdForTab(target_contents).id(),
mojom::APIPermissionID::kTabCaptureForTab) &&
(GetAllowlistedExtensionID() != extension_id)) {
return RespondNow(Error(kGrantError));
}
GURL origin;
int caller_process_id = -1;
std::optional<int> restrict_to_render_frame_id;
bool should_restrict_to_render_frame = extension()->manifest_version() < 3;
if (params->options && params->options->consumer_tab_id) {
content::WebContents* consumer_contents = nullptr;
if (!ExtensionTabUtil::GetTabById(*(params->options->consumer_tab_id),
browser_context(), true,
&consumer_contents)) {
return RespondNow(Error(kInvalidTabIdError));
}
// TODO(crbug.com/40805196): Use url::Origin directly here and
// throughout this stack.
origin =
consumer_contents->GetLastCommittedURL().DeprecatedGetOriginAsURL();
if (!origin.is_valid()) {
return RespondNow(Error(kInvalidOriginError));
}
if (!network::IsUrlPotentiallyTrustworthy(origin)) {
return RespondNow(Error(kTabUrlNotSecure));
}
content::RenderFrameHost* main_frame =
consumer_contents->GetPrimaryMainFrame();
caller_process_id = main_frame->GetProcess()->GetDeprecatedID();
restrict_to_render_frame_id = main_frame->GetRoutingID();
} else if (should_restrict_to_render_frame) {
content::WebContents* sender_contents = GetSenderWebContents();
if (!sender_contents) {
return RespondNow(Error(
"`tabCapture.getMediaStreamId()` must be called from a frame in "
"manifest version 2."));
}
// TODO(crbug.com/40805196): Use url::Origin directly here and
// throughout this stack.
origin = extension()->url();
content::RenderFrameHost* main_frame =
sender_contents->GetPrimaryMainFrame();
caller_process_id = main_frame->GetProcess()->GetDeprecatedID();
restrict_to_render_frame_id = main_frame->GetRoutingID();
} else {
// TODO(crbug.com/40805196): Use url::Origin directly here and
// throughout this stack.
origin = extension()->url();
caller_process_id = source_process_id();
}
CHECK_NE(-1, caller_process_id);
CHECK(restrict_to_render_frame_id.has_value() ||
!should_restrict_to_render_frame);
DesktopMediaID source = BuildDesktopMediaID(target_contents, nullptr);
TabCaptureRegistry* registry = TabCaptureRegistry::Get(browser_context());
std::string device_id =
registry->AddRequest(target_contents, extension_id, false, origin, source,
caller_process_id, restrict_to_render_frame_id);
if (device_id.empty()) {
return RespondNow(Error(kCapturingSameTab));
}
return RespondNow(WithArguments(device_id));
}
} // namespace extensions
|