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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 <set>
#include <string>
#include <vector>
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h"
#include "chrome/browser/extensions/extension_renderer_state.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.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"
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.";
// Keys/values for media stream constraints.
const char kMediaStreamSource[] = "chromeMediaSource";
const char kMediaStreamSourceId[] = "chromeMediaSourceId";
const char kMediaStreamSourceTab[] = "tab";
// Tab Capture-specific video constraint to enable automatic resolution/rate
// throttling mode in the capture pipeline.
const char kEnableAutoThrottlingKey[] = "enableAutoThrottling";
} // namespace
// Whitelisted extensions that do not check for a browser action grant because
// they provide API's. If there are additional extension ids that need
// whitelisting and are *not* the Chromecast extension, add them to a new
// kWhitelist array.
const char* const kChromecastExtensionIds[] = {
"enhhojjnijigcajfphajepfemndkmdlo", // Dev
"pkedcjkdefgpdelpbcmbmeomcjbeemfm", // Dogfood
"fmfcbgogabcbclcofgocippekhfcmgfj", // Staging
"hfaagokkkhdbgiakmmlclaapfelnkoah", // Canary
"dliochdbjfkdbacpmhlcpmleaejidimm", // Google Cast Beta
"boadgeojelhgndaghljhdicfkmllpafd", // Google Cast Stable
};
bool TabCaptureCaptureFunction::RunSync() {
scoped_ptr<api::tab_capture::Capture::Params> params =
TabCapture::Capture::Params::Create(*args_);
EXTENSION_FUNCTION_VALIDATE(params.get());
// Figure out the active WebContents and retrieve the needed ids.
Browser* target_browser = chrome::FindAnyBrowser(
GetProfile(), include_incognito(), chrome::GetActiveDesktop());
if (!target_browser) {
error_ = kFindingTabError;
return false;
}
content::WebContents* target_contents =
target_browser->tab_strip_model()->GetActiveWebContents();
if (!target_contents) {
error_ = kFindingTabError;
return false;
}
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 whitelisted.
if (!extension()->permissions_data()->HasAPIPermissionForTab(
SessionTabHelper::IdForTab(target_contents),
APIPermission::kTabCaptureForTab) &&
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kWhitelistedExtensionID) != extension_id &&
!SimpleFeature::IsIdInArray(extension_id, kChromecastExtensionIds,
arraysize(kChromecastExtensionIds))) {
error_ = kGrantError;
return false;
}
// Create a constraints vector. We will modify all the constraints in this
// vector to append our chrome specific constraints.
std::vector<MediaStreamConstraint*> constraints;
bool has_audio = params->options.audio.get() && *params->options.audio.get();
bool has_video = params->options.video.get() && *params->options.video.get();
if (!has_audio && !has_video) {
error_ = kNoAudioOrVideo;
return false;
}
if (has_audio) {
if (!params->options.audio_constraints.get())
params->options.audio_constraints.reset(new MediaStreamConstraint);
constraints.push_back(params->options.audio_constraints.get());
}
bool enable_auto_throttling = false;
if (has_video) {
if (params->options.video_constraints.get()) {
// Check for the Tab Capture-specific video constraint for enabling
// automatic resolution/rate throttling mode in the capture pipeline. See
// implementation comments for content::WebContentsVideoCaptureDevice.
base::DictionaryValue& props =
params->options.video_constraints->mandatory.additional_properties;
if (!props.GetBooleanWithoutPathExpansion(
kEnableAutoThrottlingKey, &enable_auto_throttling)) {
enable_auto_throttling = false;
}
// Remove the key from the properties to avoid an "unrecognized
// constraint" error in the renderer.
props.RemoveWithoutPathExpansion(kEnableAutoThrottlingKey, nullptr);
} else {
params->options.video_constraints.reset(new MediaStreamConstraint);
}
constraints.push_back(params->options.video_constraints.get());
}
// Device id we use for Tab Capture.
content::RenderFrameHost* const main_frame = target_contents->GetMainFrame();
// TODO(miu): We should instead use a "randomly generated device ID" scheme,
// like that employed by the desktop capture API. http://crbug.com/163100
const std::string device_id = base::StringPrintf(
"web-contents-media-stream://%i:%i%s",
main_frame->GetProcess()->GetID(),
main_frame->GetRoutingID(),
enable_auto_throttling ? "?throttling=auto" : "");
// Append chrome specific tab constraints.
for (std::vector<MediaStreamConstraint*>::iterator it = constraints.begin();
it != constraints.end(); ++it) {
base::DictionaryValue* constraint = &(*it)->mandatory.additional_properties;
constraint->SetString(kMediaStreamSource, kMediaStreamSourceTab);
constraint->SetString(kMediaStreamSourceId, device_id);
}
TabCaptureRegistry* registry = TabCaptureRegistry::Get(GetProfile());
if (!registry->AddRequest(target_contents, extension_id)) {
error_ = kCapturingSameTab;
return false;
}
// Copy the result from our modified input parameters. This will be
// intercepted by custom bindings which will build and send the special
// WebRTC user media request.
base::DictionaryValue* result = new base::DictionaryValue();
result->MergeDictionary(params->options.ToValue().get());
SetResult(result);
return true;
}
bool TabCaptureGetCapturedTabsFunction::RunSync() {
TabCaptureRegistry* registry = TabCaptureRegistry::Get(GetProfile());
base::ListValue* const list = new base::ListValue();
if (registry)
registry->GetCapturedTabs(extension()->id(), list);
SetResult(list);
return true;
}
} // namespace extensions
|