Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/browser/media/desktop_capture_access_handler.cc

Issue 1095393004: Refactor: Make MediaCaptureDevicesDispatcher have pluggable handlers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2015 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.
4
5 #include "chrome/browser/media/desktop_capture_access_handler.h"
6
7 #include "base/command_line.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/media/desktop_streams_registry.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/browser/ui/screen_capture_notification_ui.h"
15 #include "chrome/browser/ui/simple_message_box.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/common/media_stream_request.h"
22 #include "extensions/common/constants.h"
23 #include "media/audio/audio_manager_base.h"
24 #include "net/base/net_util.h"
25 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
26 #include "ui/base/l10n/l10n_util.h"
27
28 #if defined(ENABLE_EXTENSIONS)
29 #include "extensions/browser/app_window/app_window.h"
30 #include "extensions/browser/app_window/app_window_registry.h"
31 #include "extensions/common/extension.h"
32 #endif
33
34 using content::BrowserThread;
35
36 namespace {
37 #if defined(ENABLE_EXTENSIONS)
38 bool IsExtensionWhitelistedForScreenCapture(
39 const extensions::Extension* extension) {
40 #if defined(OS_CHROMEOS)
41 std::string hash = base::SHA1HashString(extension->id());
42 std::string hex_hash = base::HexEncode(hash.c_str(), hash.length());
43
44 // crbug.com/446688
45 return hex_hash == "4F25792AF1AA7483936DE29C07806F203C7170A0" ||
46 hex_hash == "BD8781D757D830FC2E85470A1B6E8A718B7EE0D9" ||
47 hex_hash == "4AC2B6C63C6480D150DFDA13E4A5956EB1D0DDBB" ||
48 hex_hash == "81986D4F846CEDDDB962643FA501D1780DD441BB";
49 #else
50 return false;
51 #endif // defined(OS_CHROMEOS)
52 }
53 #endif // defined(ENABLE_EXTENSIONS)
54
55 bool IsBuiltInExtension(const GURL& origin) {
56 return
57 // Feedback Extension.
58 origin.spec() == "chrome-extension://gfdkimpbcpahaombhbimeihdjnejgicl/";
59 }
60
61 // Returns true of the security origin is associated with casting.
62 bool IsOriginForCasting(const GURL& origin) {
63 // Whitelisted tab casting extensions.
64 return
65 // Dev
66 origin.spec() == "chrome-extension://enhhojjnijigcajfphajepfemndkmdlo/" ||
67 // Canary
68 origin.spec() == "chrome-extension://hfaagokkkhdbgiakmmlclaapfelnkoah/" ||
69 // Beta (internal)
70 origin.spec() == "chrome-extension://fmfcbgogabcbclcofgocippekhfcmgfj/" ||
71 // Google Cast Beta
72 origin.spec() == "chrome-extension://dliochdbjfkdbacpmhlcpmleaejidimm/" ||
73 // Google Cast Stable
74 origin.spec() == "chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/" ||
75 // http://crbug.com/457908
76 origin.spec() == "chrome-extension://ekpaaapppgpmolpcldedioblbkmijaca/" ||
77 origin.spec() == "chrome-extension://fjhoaacokmgbjemoflkofnenfaiekifl/";
78 }
79
80 // Helper to get title of the calling application shown in the screen capture
81 // notification.
82 base::string16 GetApplicationTitle(content::WebContents* web_contents,
83 const extensions::Extension* extension) {
84 // Use extension name as title for extensions and host/origin for drive-by
85 // web.
86 std::string title;
87 #if defined(ENABLE_EXTENSIONS)
88 if (extension) {
89 title = extension->name();
90 return base::UTF8ToUTF16(title);
91 }
92 #endif
93 GURL url = web_contents->GetURL();
94 title = url.SchemeIsSecure() ? net::GetHostAndOptionalPort(url)
95 : url.GetOrigin().spec();
96 return base::UTF8ToUTF16(title);
97 }
98
99 // Helper to get list of media stream devices for desktop capture in |devices|.
100 // Registers to display notification if |display_notification| is true.
101 // Returns an instance of MediaStreamUI to be passed to content layer.
102 scoped_ptr<content::MediaStreamUI> GetDevicesForDesktopCapture(
103 content::MediaStreamDevices* devices,
104 content::DesktopMediaID media_id,
105 bool capture_audio,
106 bool display_notification,
107 const base::string16& application_title,
108 const base::string16& registered_extension_name) {
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
110 scoped_ptr<content::MediaStreamUI> ui;
111
112 // Add selected desktop source to the list.
113 devices->push_back(content::MediaStreamDevice(
114 content::MEDIA_DESKTOP_VIDEO_CAPTURE, media_id.ToString(), "Screen"));
115 if (capture_audio) {
116 // Use the special loopback device ID for system audio capture.
117 devices->push_back(content::MediaStreamDevice(
118 content::MEDIA_DESKTOP_AUDIO_CAPTURE,
119 media::AudioManagerBase::kLoopbackInputDeviceId, "System Audio"));
120 }
121
122 // If required, register to display the notification for stream capture.
123 if (display_notification) {
124 if (application_title == registered_extension_name) {
125 ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16(
126 IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT, application_title));
127 } else {
128 ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16(
129 IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT_DELEGATED,
130 registered_extension_name, application_title));
131 }
132 }
133
134 return ui.Pass();
135 }
136
137 #if !defined(OS_ANDROID)
138 // Find browser or app window from a given |web_contents|.
139 gfx::NativeWindow FindParentWindowForWebContents(
140 content::WebContents* web_contents) {
141 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
142 if (browser && browser->window())
143 return browser->window()->GetNativeWindow();
144
145 const extensions::AppWindowRegistry::AppWindowList& window_list =
146 extensions::AppWindowRegistry::Get(web_contents->GetBrowserContext())
147 ->app_windows();
148 for (extensions::AppWindowRegistry::AppWindowList::const_iterator iter =
149 window_list.begin();
150 iter != window_list.end(); ++iter) {
151 if ((*iter)->web_contents() == web_contents)
152 return (*iter)->GetNativeWindow();
153 }
154
155 return NULL;
156 }
157 #endif
158
159 } // namespace
160
161 DesktopCaptureAccessHandler::DesktopCaptureAccessHandler(
162 DesktopStreamsRegistry* registry)
163 : desktop_streams_registry_(registry) {
164 }
165
166 DesktopCaptureAccessHandler::~DesktopCaptureAccessHandler() {
167 }
168
169 void DesktopCaptureAccessHandler::ProcessScreenCaptureAccessRequest(
170 content::WebContents* web_contents,
171 const content::MediaStreamRequest& request,
172 const content::MediaResponseCallback& callback,
173 const extensions::Extension* extension) {
174 content::MediaStreamDevices devices;
175 scoped_ptr<content::MediaStreamUI> ui;
176
177 DCHECK_EQ(request.video_type, content::MEDIA_DESKTOP_VIDEO_CAPTURE);
178
179 bool loopback_audio_supported = false;
180 #if defined(USE_CRAS) || defined(OS_WIN)
181 // Currently loopback audio capture is supported only on Windows and ChromeOS.
182 loopback_audio_supported = true;
183 #endif
184
185 bool component_extension = false;
186 #if defined(ENABLE_EXTENSIONS)
187 component_extension =
188 extension && extension->location() == extensions::Manifest::COMPONENT;
189 #endif
190
191 bool screen_capture_enabled =
192 base::CommandLine::ForCurrentProcess()->HasSwitch(
193 switches::kEnableUserMediaScreenCapturing);
194 #if defined(ENABLE_EXTENSIONS)
195 screen_capture_enabled |= IsOriginForCasting(request.security_origin) ||
196 IsExtensionWhitelistedForScreenCapture(extension) ||
197 IsBuiltInExtension(request.security_origin);
198 #endif
199
200 const bool origin_is_secure =
201 request.security_origin.SchemeIsSecure() ||
202 request.security_origin.SchemeIs(extensions::kExtensionScheme) ||
203 base::CommandLine::ForCurrentProcess()->HasSwitch(
204 switches::kAllowHttpScreenCapture);
205
206 // If basic conditions (screen capturing is enabled and origin is secure)
207 // aren't fulfilled, we'll use "invalid state" as result. Otherwise, we set
208 // it after checking permission.
209 // TODO(grunell): It would be good to change this result for something else,
210 // probably a new one.
211 content::MediaStreamRequestResult result =
212 content::MEDIA_DEVICE_INVALID_STATE;
213
214 // Approve request only when the following conditions are met:
215 // 1. Screen capturing is enabled via command line switch or white-listed for
216 // the given origin.
217 // 2. Request comes from a page with a secure origin or from an extension.
218 if (screen_capture_enabled && origin_is_secure) {
219 // Get title of the calling application prior to showing the message box.
220 // chrome::ShowMessageBox() starts a nested message loop which may allow
221 // |web_contents| to be destroyed on the UI thread before the message box
222 // is closed. See http://crbug.com/326690.
223 base::string16 application_title =
224 GetApplicationTitle(web_contents, extension);
225 #if !defined(OS_ANDROID)
226 gfx::NativeWindow parent_window =
227 FindParentWindowForWebContents(web_contents);
228 #else
229 gfx::NativeWindow parent_window = NULL;
230 #endif
231 web_contents = NULL;
232
233 bool whitelisted_extension = false;
234 #if defined(ENABLE_EXTENSIONS)
235 whitelisted_extension = IsExtensionWhitelistedForScreenCapture(extension);
236 #endif
237
238 // For whitelisted or component extensions, bypass message box.
239 bool user_approved = false;
240 if (!whitelisted_extension && !component_extension) {
241 base::string16 application_name =
242 base::UTF8ToUTF16(request.security_origin.spec());
243 #if defined(ENABLE_EXTENSIONS)
244 if (extension)
245 application_name = base::UTF8ToUTF16(extension->name());
246 #endif
247 base::string16 confirmation_text = l10n_util::GetStringFUTF16(
248 request.audio_type == content::MEDIA_NO_SERVICE
249 ? IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TEXT
250 : IDS_MEDIA_SCREEN_AND_AUDIO_CAPTURE_CONFIRMATION_TEXT,
251 application_name);
252 chrome::MessageBoxResult result = chrome::ShowMessageBox(
253 parent_window,
254 l10n_util::GetStringFUTF16(
255 IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TITLE, application_name),
256 confirmation_text, chrome::MESSAGE_BOX_TYPE_QUESTION);
257 user_approved = (result == chrome::MESSAGE_BOX_RESULT_YES);
258 }
259
260 if (user_approved || component_extension || whitelisted_extension) {
261 content::DesktopMediaID screen_id;
262 #if defined(OS_CHROMEOS)
263 screen_id = content::DesktopMediaID::RegisterAuraWindow(
264 ash::Shell::GetInstance()->GetPrimaryRootWindow());
265 #else // defined(OS_CHROMEOS)
266 screen_id = content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN,
267 webrtc::kFullDesktopScreenId);
268 #endif // !defined(OS_CHROMEOS)
269
270 bool capture_audio =
271 (request.audio_type == content::MEDIA_DESKTOP_AUDIO_CAPTURE &&
272 loopback_audio_supported);
273
274 // Unless we're being invoked from a component extension, register to
275 // display the notification for stream capture.
276 bool display_notification = !component_extension;
277
278 ui = GetDevicesForDesktopCapture(&devices, screen_id, capture_audio,
279 display_notification, application_title,
280 application_title);
281 DCHECK(!devices.empty());
282 }
283
284 // The only case when devices can be empty is if the user has denied
285 // permission.
286 result = devices.empty() ? content::MEDIA_DEVICE_PERMISSION_DENIED
287 : content::MEDIA_DEVICE_OK;
288 }
289
290 callback.Run(devices, result, ui.Pass());
291 }
292
293 void DesktopCaptureAccessHandler::HandleRequest(
294 content::WebContents* web_contents,
295 const content::MediaStreamRequest& request,
296 const content::MediaResponseCallback& callback,
297 const extensions::Extension* extension) {
298 content::MediaStreamDevices devices;
299 scoped_ptr<content::MediaStreamUI> ui;
300
301 if (request.video_type != content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
302 callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
303 return;
304 }
305
306 // If the device id wasn't specified then this is a screen capture request
307 // (i.e. chooseDesktopMedia() API wasn't used to generate device id).
308 if (request.requested_video_device_id.empty()) {
309 ProcessScreenCaptureAccessRequest(web_contents, request, callback,
310 extension);
311 return;
312 }
313
314 // The extension name that the stream is registered with.
315 std::string original_extension_name;
316 // Resolve DesktopMediaID for the specified device id.
317 content::DesktopMediaID media_id;
318 // TODO(miu): Replace "main RenderFrame" IDs with the request's actual
319 // RenderFrame IDs once the desktop capture extension API implementation is
320 // fixed. http://crbug.com/304341
321 content::WebContents* const web_contents_for_stream =
322 content::WebContents::FromRenderFrameHost(
323 content::RenderFrameHost::FromID(request.render_process_id,
324 request.render_frame_id));
325 content::RenderFrameHost* const main_frame =
326 web_contents_for_stream ? web_contents_for_stream->GetMainFrame() : NULL;
327 if (main_frame && desktop_streams_registry_) {
328 media_id = desktop_streams_registry_->RequestMediaForStreamId(
329 request.requested_video_device_id, main_frame->GetProcess()->GetID(),
330 main_frame->GetRoutingID(), request.security_origin,
331 &original_extension_name);
332 }
333
334 // Received invalid device id.
335 if (media_id.type == content::DesktopMediaID::TYPE_NONE) {
336 callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
337 return;
338 }
339
340 bool loopback_audio_supported = false;
341 #if defined(USE_CRAS) || defined(OS_WIN)
342 // Currently loopback audio capture is supported only on Windows and ChromeOS.
343 loopback_audio_supported = true;
344 #endif
345
346 // Audio is only supported for screen capture streams.
347 bool capture_audio =
348 (media_id.type == content::DesktopMediaID::TYPE_SCREEN &&
349 request.audio_type == content::MEDIA_DESKTOP_AUDIO_CAPTURE &&
350 loopback_audio_supported);
351
352 ui = GetDevicesForDesktopCapture(&devices, media_id, capture_audio, true,
353 GetApplicationTitle(web_contents, extension),
354 base::UTF8ToUTF16(original_extension_name));
355
356 callback.Run(devices, content::MEDIA_DEVICE_OK, ui.Pass());
357 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698