1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Implements the Chrome Extensions Tab Capture API.
7 #include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h"
13 #include "base/command_line.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/values.h"
16 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h"
17 #include "chrome/browser/extensions/extension_renderer_state.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/sessions/session_tab_helper.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "content/public/browser/render_frame_host.h"
24 #include "content/public/browser/render_process_host.h"
25 #include "extensions/common/features/feature.h"
26 #include "extensions/common/features/feature_provider.h"
27 #include "extensions/common/features/simple_feature.h"
28 #include "extensions/common/permissions/permissions_data.h"
29 #include "extensions/common/switches.h"
31 using extensions::api::tab_capture::MediaStreamConstraint
;
33 namespace TabCapture
= extensions::api::tab_capture
;
34 namespace GetCapturedTabs
= TabCapture::GetCapturedTabs
;
36 namespace extensions
{
39 const char kCapturingSameTab
[] = "Cannot capture a tab with an active stream.";
40 const char kFindingTabError
[] = "Error finding tab to capture.";
41 const char kNoAudioOrVideo
[] = "Capture failed. No audio or video requested.";
42 const char kGrantError
[] =
43 "Extension has not been invoked for the current page (see activeTab "
44 "permission). Chrome pages cannot be captured.";
46 // Keys/values for media stream constraints.
47 const char kMediaStreamSource
[] = "chromeMediaSource";
48 const char kMediaStreamSourceId
[] = "chromeMediaSourceId";
49 const char kMediaStreamSourceTab
[] = "tab";
51 // Tab Capture-specific video constraint to enable automatic resolution/rate
52 // throttling mode in the capture pipeline.
53 const char kEnableAutoThrottlingKey
[] = "enableAutoThrottling";
57 // Whitelisted extensions that do not check for a browser action grant because
58 // they provide API's. If there are additional extension ids that need
59 // whitelisting and are *not* the Chromecast extension, add them to a new
62 // This list is also used by CastConfigDelegateChromeos to find official Cast
64 const char* const kChromecastExtensionIds
[] = {
65 "enhhojjnijigcajfphajepfemndkmdlo", // Dev
66 "pkedcjkdefgpdelpbcmbmeomcjbeemfm", // Dogfood
67 "fmfcbgogabcbclcofgocippekhfcmgfj", // Staging
68 "hfaagokkkhdbgiakmmlclaapfelnkoah", // Canary
69 "dliochdbjfkdbacpmhlcpmleaejidimm", // Google Cast Beta
70 "boadgeojelhgndaghljhdicfkmllpafd", // Google Cast Stable
71 "hlgmmjhlnlapooncikdpiiokdjcdpjme", // Test cast extension
74 bool TabCaptureCaptureFunction::RunSync() {
75 scoped_ptr
<api::tab_capture::Capture::Params
> params
=
76 TabCapture::Capture::Params::Create(*args_
);
77 EXTENSION_FUNCTION_VALIDATE(params
.get());
79 // Figure out the active WebContents and retrieve the needed ids.
80 Browser
* target_browser
= chrome::FindAnyBrowser(
81 GetProfile(), include_incognito(), chrome::GetActiveDesktop());
82 if (!target_browser
) {
83 error_
= kFindingTabError
;
87 content::WebContents
* target_contents
=
88 target_browser
->tab_strip_model()->GetActiveWebContents();
89 if (!target_contents
) {
90 error_
= kFindingTabError
;
94 const std::string
& extension_id
= extension()->id();
96 // Make sure either we have been granted permission to capture through an
97 // extension icon click or our extension is whitelisted.
98 if (!extension()->permissions_data()->HasAPIPermissionForTab(
99 SessionTabHelper::IdForTab(target_contents
),
100 APIPermission::kTabCaptureForTab
) &&
101 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
102 switches::kWhitelistedExtensionID
) != extension_id
&&
103 !SimpleFeature::IsIdInArray(extension_id
, kChromecastExtensionIds
,
104 arraysize(kChromecastExtensionIds
))) {
105 error_
= kGrantError
;
109 // Create a constraints vector. We will modify all the constraints in this
110 // vector to append our chrome specific constraints.
111 std::vector
<MediaStreamConstraint
*> constraints
;
112 bool has_audio
= params
->options
.audio
.get() && *params
->options
.audio
.get();
113 bool has_video
= params
->options
.video
.get() && *params
->options
.video
.get();
115 if (!has_audio
&& !has_video
) {
116 error_
= kNoAudioOrVideo
;
121 if (!params
->options
.audio_constraints
.get())
122 params
->options
.audio_constraints
.reset(new MediaStreamConstraint
);
124 constraints
.push_back(params
->options
.audio_constraints
.get());
127 bool enable_auto_throttling
= false;
129 if (params
->options
.video_constraints
.get()) {
130 // Check for the Tab Capture-specific video constraint for enabling
131 // automatic resolution/rate throttling mode in the capture pipeline. See
132 // implementation comments for content::WebContentsVideoCaptureDevice.
133 base::DictionaryValue
& props
=
134 params
->options
.video_constraints
->mandatory
.additional_properties
;
135 if (!props
.GetBooleanWithoutPathExpansion(
136 kEnableAutoThrottlingKey
, &enable_auto_throttling
)) {
137 enable_auto_throttling
= false;
139 // Remove the key from the properties to avoid an "unrecognized
140 // constraint" error in the renderer.
141 props
.RemoveWithoutPathExpansion(kEnableAutoThrottlingKey
, nullptr);
143 params
->options
.video_constraints
.reset(new MediaStreamConstraint
);
146 constraints
.push_back(params
->options
.video_constraints
.get());
149 // Device id we use for Tab Capture.
150 content::RenderFrameHost
* const main_frame
= target_contents
->GetMainFrame();
151 // TODO(miu): We should instead use a "randomly generated device ID" scheme,
152 // like that employed by the desktop capture API. http://crbug.com/163100
153 const std::string device_id
= base::StringPrintf(
154 "web-contents-media-stream://%i:%i%s",
155 main_frame
->GetProcess()->GetID(),
156 main_frame
->GetRoutingID(),
157 enable_auto_throttling
? "?throttling=auto" : "");
159 // Append chrome specific tab constraints.
160 for (std::vector
<MediaStreamConstraint
*>::iterator it
= constraints
.begin();
161 it
!= constraints
.end(); ++it
) {
162 base::DictionaryValue
* constraint
= &(*it
)->mandatory
.additional_properties
;
163 constraint
->SetString(kMediaStreamSource
, kMediaStreamSourceTab
);
164 constraint
->SetString(kMediaStreamSourceId
, device_id
);
167 TabCaptureRegistry
* registry
= TabCaptureRegistry::Get(GetProfile());
168 if (!registry
->AddRequest(target_contents
, extension_id
)) {
169 error_
= kCapturingSameTab
;
173 // Copy the result from our modified input parameters. This will be
174 // intercepted by custom bindings which will build and send the special
175 // WebRTC user media request.
176 base::DictionaryValue
* result
= new base::DictionaryValue();
177 result
->MergeDictionary(params
->options
.ToValue().get());
183 bool TabCaptureGetCapturedTabsFunction::RunSync() {
184 TabCaptureRegistry
* registry
= TabCaptureRegistry::Get(GetProfile());
185 base::ListValue
* const list
= new base::ListValue();
187 registry
->GetCapturedTabs(extension()->id(), list
);
192 } // namespace extensions