Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / tab_capture / tab_capture_api.cc
blob57605300a2b609207d9a78f950a03facd1e15ed9
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"
9 #include <set>
10 #include <string>
11 #include <vector>
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 {
37 namespace {
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";
55 } // namespace
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
60 // kWhitelist array.
62 // This list is also used by CastConfigDelegateChromeos to find official Cast
63 // extensions.
64 const char* const kChromecastExtensionIds[] = {
65 "enhhojjnijigcajfphajepfemndkmdlo", // Dev
66 "fmfcbgogabcbclcofgocippekhfcmgfj", // Staging
67 "hfaagokkkhdbgiakmmlclaapfelnkoah", // Canary
68 "dliochdbjfkdbacpmhlcpmleaejidimm", // Google Cast Beta
69 "boadgeojelhgndaghljhdicfkmllpafd", // Google Cast Stable
70 "hlgmmjhlnlapooncikdpiiokdjcdpjme", // Test cast extension
73 const char* const kMediaRouterExtensionIds[] = {
74 "fjhoaacokmgbjemoflkofnenfaiekifl", // Stable
75 "ekpaaapppgpmolpcldedioblbkmijaca", // Beta
78 bool TabCaptureCaptureFunction::RunSync() {
79 scoped_ptr<api::tab_capture::Capture::Params> params =
80 TabCapture::Capture::Params::Create(*args_);
81 EXTENSION_FUNCTION_VALIDATE(params.get());
83 // Figure out the active WebContents and retrieve the needed ids.
84 Browser* target_browser = chrome::FindAnyBrowser(
85 GetProfile(), include_incognito(), chrome::GetActiveDesktop());
86 if (!target_browser) {
87 error_ = kFindingTabError;
88 return false;
91 content::WebContents* target_contents =
92 target_browser->tab_strip_model()->GetActiveWebContents();
93 if (!target_contents) {
94 error_ = kFindingTabError;
95 return false;
98 const std::string& extension_id = extension()->id();
100 // Make sure either we have been granted permission to capture through an
101 // extension icon click or our extension is whitelisted.
102 if (!extension()->permissions_data()->HasAPIPermissionForTab(
103 SessionTabHelper::IdForTab(target_contents),
104 APIPermission::kTabCaptureForTab) &&
105 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
106 switches::kWhitelistedExtensionID) != extension_id &&
107 !SimpleFeature::IsIdInArray(extension_id, kChromecastExtensionIds,
108 arraysize(kChromecastExtensionIds)) &&
109 !SimpleFeature::IsIdInArray(extension_id, kMediaRouterExtensionIds,
110 arraysize(kMediaRouterExtensionIds))) {
111 error_ = kGrantError;
112 return false;
115 // Create a constraints vector. We will modify all the constraints in this
116 // vector to append our chrome specific constraints.
117 std::vector<MediaStreamConstraint*> constraints;
118 bool has_audio = params->options.audio.get() && *params->options.audio.get();
119 bool has_video = params->options.video.get() && *params->options.video.get();
121 if (!has_audio && !has_video) {
122 error_ = kNoAudioOrVideo;
123 return false;
126 if (has_audio) {
127 if (!params->options.audio_constraints.get())
128 params->options.audio_constraints.reset(new MediaStreamConstraint);
130 constraints.push_back(params->options.audio_constraints.get());
133 bool enable_auto_throttling = false;
134 if (has_video) {
135 if (params->options.video_constraints.get()) {
136 // Check for the Tab Capture-specific video constraint for enabling
137 // automatic resolution/rate throttling mode in the capture pipeline. See
138 // implementation comments for content::WebContentsVideoCaptureDevice.
139 base::DictionaryValue& props =
140 params->options.video_constraints->mandatory.additional_properties;
141 if (!props.GetBooleanWithoutPathExpansion(
142 kEnableAutoThrottlingKey, &enable_auto_throttling)) {
143 enable_auto_throttling = false;
145 // Remove the key from the properties to avoid an "unrecognized
146 // constraint" error in the renderer.
147 props.RemoveWithoutPathExpansion(kEnableAutoThrottlingKey, nullptr);
148 } else {
149 params->options.video_constraints.reset(new MediaStreamConstraint);
152 constraints.push_back(params->options.video_constraints.get());
155 // Device id we use for Tab Capture.
156 content::RenderFrameHost* const main_frame = target_contents->GetMainFrame();
157 // TODO(miu): We should instead use a "randomly generated device ID" scheme,
158 // like that employed by the desktop capture API. http://crbug.com/163100
159 const std::string device_id = base::StringPrintf(
160 "web-contents-media-stream://%i:%i%s",
161 main_frame->GetProcess()->GetID(),
162 main_frame->GetRoutingID(),
163 enable_auto_throttling ? "?throttling=auto" : "");
165 // Append chrome specific tab constraints.
166 for (std::vector<MediaStreamConstraint*>::iterator it = constraints.begin();
167 it != constraints.end(); ++it) {
168 base::DictionaryValue* constraint = &(*it)->mandatory.additional_properties;
169 constraint->SetString(kMediaStreamSource, kMediaStreamSourceTab);
170 constraint->SetString(kMediaStreamSourceId, device_id);
173 TabCaptureRegistry* registry = TabCaptureRegistry::Get(GetProfile());
174 if (!registry->AddRequest(target_contents, extension_id)) {
175 error_ = kCapturingSameTab;
176 return false;
179 // Copy the result from our modified input parameters. This will be
180 // intercepted by custom bindings which will build and send the special
181 // WebRTC user media request.
182 base::DictionaryValue* result = new base::DictionaryValue();
183 result->MergeDictionary(params->options.ToValue().get());
185 SetResult(result);
186 return true;
189 bool TabCaptureGetCapturedTabsFunction::RunSync() {
190 TabCaptureRegistry* registry = TabCaptureRegistry::Get(GetProfile());
191 base::ListValue* const list = new base::ListValue();
192 if (registry)
193 registry->GetCapturedTabs(extension()->id(), list);
194 SetResult(list);
195 return true;
198 } // namespace extensions