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
{
40 const char kCapturingSameTab
[] = "Cannot capture a tab with an active stream.";
41 const char kFindingTabError
[] = "Error finding tab to capture.";
42 const char kNoAudioOrVideo
[] = "Capture failed. No audio or video requested.";
43 const char kGrantError
[] =
44 "Extension has not been invoked for the current page (see activeTab "
45 "permission). Chrome pages cannot be captured.";
47 // Keys/values for media stream constraints.
48 const char kMediaStreamSource
[] = "chromeMediaSource";
49 const char kMediaStreamSourceId
[] = "chromeMediaSourceId";
50 const char kMediaStreamSourceTab
[] = "tab";
52 // Whitelisted extensions that do not check for a browser action grant because
53 // they provide API's.
54 const char* const kWhitelist
[] = {
55 "enhhojjnijigcajfphajepfemndkmdlo", // Dev
56 "pkedcjkdefgpdelpbcmbmeomcjbeemfm", // Trusted Tester
57 "fmfcbgogabcbclcofgocippekhfcmgfj", // Staging
58 "hfaagokkkhdbgiakmmlclaapfelnkoah", // Canary
59 "F155646B5D1CA545F7E1E4E20D573DFDD44C2540", // Trusted Tester (public)
60 "16CA7A47AAE4BE49B1E75A6B960C3875E945B264" // Release
65 bool TabCaptureCaptureFunction::RunSync() {
66 scoped_ptr
<api::tab_capture::Capture::Params
> params
=
67 TabCapture::Capture::Params::Create(*args_
);
68 EXTENSION_FUNCTION_VALIDATE(params
.get());
70 // Figure out the active WebContents and retrieve the needed ids.
71 Browser
* target_browser
= chrome::FindAnyBrowser(
72 GetProfile(), include_incognito(), chrome::GetActiveDesktop());
73 if (!target_browser
) {
74 error_
= kFindingTabError
;
78 content::WebContents
* target_contents
=
79 target_browser
->tab_strip_model()->GetActiveWebContents();
80 if (!target_contents
) {
81 error_
= kFindingTabError
;
85 const std::string
& extension_id
= extension()->id();
87 // Make sure either we have been granted permission to capture through an
88 // extension icon click or our extension is whitelisted.
89 if (!extension()->permissions_data()->HasAPIPermissionForTab(
90 SessionTabHelper::IdForTab(target_contents
),
91 APIPermission::kTabCaptureForTab
) &&
92 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
93 switches::kWhitelistedExtensionID
) != extension_id
&&
94 !SimpleFeature::IsIdInArray(
95 extension_id
, kWhitelist
, arraysize(kWhitelist
))) {
100 // Create a constraints vector. We will modify all the constraints in this
101 // vector to append our chrome specific constraints.
102 std::vector
<MediaStreamConstraint
*> constraints
;
103 bool has_audio
= params
->options
.audio
.get() && *params
->options
.audio
.get();
104 bool has_video
= params
->options
.video
.get() && *params
->options
.video
.get();
106 if (!has_audio
&& !has_video
) {
107 error_
= kNoAudioOrVideo
;
112 if (!params
->options
.audio_constraints
.get())
113 params
->options
.audio_constraints
.reset(new MediaStreamConstraint
);
115 constraints
.push_back(params
->options
.audio_constraints
.get());
118 if (!params
->options
.video_constraints
.get())
119 params
->options
.video_constraints
.reset(new MediaStreamConstraint
);
121 constraints
.push_back(params
->options
.video_constraints
.get());
124 // Device id we use for Tab Capture.
125 content::RenderFrameHost
* const main_frame
= target_contents
->GetMainFrame();
126 // TODO(miu): We should instead use a "randomly generated device ID" scheme,
127 // like that employed by the desktop capture API. http://crbug.com/163100
128 const std::string device_id
= base::StringPrintf(
129 "web-contents-media-stream://%i:%i",
130 main_frame
->GetProcess()->GetID(),
131 main_frame
->GetRoutingID());
133 // Append chrome specific tab constraints.
134 for (std::vector
<MediaStreamConstraint
*>::iterator it
= constraints
.begin();
135 it
!= constraints
.end(); ++it
) {
136 base::DictionaryValue
* constraint
= &(*it
)->mandatory
.additional_properties
;
137 constraint
->SetString(kMediaStreamSource
, kMediaStreamSourceTab
);
138 constraint
->SetString(kMediaStreamSourceId
, device_id
);
141 TabCaptureRegistry
* registry
= TabCaptureRegistry::Get(GetProfile());
142 if (!registry
->AddRequest(target_contents
, extension_id
)) {
143 error_
= kCapturingSameTab
;
147 // Copy the result from our modified input parameters. This will be
148 // intercepted by custom bindings which will build and send the special
149 // WebRTC user media request.
150 base::DictionaryValue
* result
= new base::DictionaryValue();
151 result
->MergeDictionary(params
->options
.ToValue().get());
157 bool TabCaptureGetCapturedTabsFunction::RunSync() {
158 TabCaptureRegistry
* registry
= TabCaptureRegistry::Get(GetProfile());
159 base::ListValue
* const list
= new base::ListValue();
161 registry
->GetCapturedTabs(extension()->id(), list
);
166 } // namespace extensions