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 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/sha1.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h"
16 #include "chrome/browser/media/desktop_streams_registry.h"
17 #include "chrome/browser/media/media_stream_capture_indicator.h"
18 #include "chrome/browser/media/media_stream_infobar_delegate.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/screen_capture_notification_ui.h"
21 #include "chrome/browser/ui/simple_message_box.h"
22 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/pref_names.h"
26 #include "components/user_prefs/pref_registry_syncable.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/desktop_media_id.h"
29 #include "content/public/browser/media_capture_devices.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_source.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/render_frame_host.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/media_stream_request.h"
36 #include "extensions/common/constants.h"
37 #include "extensions/common/extension.h"
38 #include "grit/generated_resources.h"
39 #include "media/audio/audio_manager_base.h"
40 #include "media/base/media_switches.h"
41 #include "net/base/net_util.h"
42 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
43 #include "ui/base/l10n/l10n_util.h"
45 #if defined(OS_CHROMEOS)
46 #include "ash/shell.h"
47 #endif // defined(OS_CHROMEOS)
49 // Only do audio stream monitoring for platforms that use it for the tab media
50 // indicator UI or the OOM killer.
51 #if !defined(OS_ANDROID) && !defined(OS_IOS)
52 #define AUDIO_STREAM_MONITORING
53 #include "chrome/browser/media/audio_stream_monitor.h"
54 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
56 using content::BrowserThread;
57 using content::MediaCaptureDevices;
58 using content::MediaStreamDevices;
62 // Finds a device in |devices| that has |device_id|, or NULL if not found.
63 const content::MediaStreamDevice* FindDeviceWithId(
64 const content::MediaStreamDevices& devices,
65 const std::string& device_id) {
66 content::MediaStreamDevices::const_iterator iter = devices.begin();
67 for (; iter != devices.end(); ++iter) {
68 if (iter->id == device_id) {
75 // This is a short-term solution to grant camera and/or microphone access to
77 // 1. Virtual keyboard extension.
78 // 2. Google Voice Search Hotword extension.
79 // 3. Flutter gesture recognition extension.
80 // 4. TODO(smus): Airbender experiment 1.
81 // 5. TODO(smus): Airbender experiment 2.
82 // Once http://crbug.com/292856 is fixed, remove this whitelist.
83 bool IsMediaRequestWhitelistedForExtension(
84 const extensions::Extension* extension) {
85 return extension->id() == "mppnpdlheglhdfmldimlhpnegondlapf" ||
86 extension->id() == "bepbmhgboaologfdajaanbcjmnhjmhfn" ||
87 extension->id() == "jokbpnebhdcladagohdnfgjcpejggllo" ||
88 extension->id() == "clffjmdilanldobdnedchkdbofoimcgb" ||
89 extension->id() == "nnckehldicaciogcbchegobnafnjkcne";
92 // This is a short-term solution to allow testing of the the Screen Capture API
93 // with Google Hangouts in M27.
94 // TODO(sergeyu): Remove this whitelist as soon as possible.
95 bool IsOriginWhitelistedForScreenCapture(const GURL& origin) {
96 #if defined(OFFICIAL_BUILD)
97 if (// Google Hangouts.
98 (origin.SchemeIs("https") &&
99 EndsWith(origin.spec(), ".talkgadget.google.com/", true)) ||
100 origin.spec() == "https://talkgadget.google.com/" ||
101 origin.spec() == "https://plus.google.com/" ||
102 origin.spec() == "chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/" ||
103 origin.spec() == "chrome-extension://fmfcbgogabcbclcofgocippekhfcmgfj/" ||
104 origin.spec() == "chrome-extension://hfaagokkkhdbgiakmmlclaapfelnkoah/" ||
105 origin.spec() == "chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/" ||
106 origin.spec() == "chrome-extension://gfdkimpbcpahaombhbimeihdjnejgicl/") {
109 // Check against hashed origins.
110 // TODO(hshi): remove this when trusted tester becomes public.
111 const std::string origin_hash = base::SHA1HashString(origin.spec());
112 DCHECK_EQ(origin_hash.length(), base::kSHA1Length);
113 const std::string hexencoded_origin_hash =
114 base::HexEncode(origin_hash.data(), origin_hash.length());
116 hexencoded_origin_hash == "3C2705BC432E7C51CA8553FDC5BEE873FF2468EE";
122 #if defined(OS_CHROMEOS)
123 // Returns true of the security origin is associated with casting.
124 bool IsOriginForCasting(const GURL& origin) {
125 #if defined(OFFICIAL_BUILD)
126 // Whitelisted tab casting extensions.
127 if (origin.spec() == "chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/" ||
128 origin.spec() == "chrome-extension://fmfcbgogabcbclcofgocippekhfcmgfj/" ||
129 origin.spec() == "chrome-extension://hfaagokkkhdbgiakmmlclaapfelnkoah/" ||
130 origin.spec() == "chrome-extension://boadgeojelhgndaghljhdicfkmllpafd/") {
133 // Check against hashed origins.
134 // TODO(hshi): remove this when trusted tester becomes public.
135 const std::string origin_hash = base::SHA1HashString(origin.spec());
136 DCHECK_EQ(origin_hash.length(), base::kSHA1Length);
137 const std::string hexencoded_origin_hash =
138 base::HexEncode(origin_hash.data(), origin_hash.length());
140 hexencoded_origin_hash == "3C2705BC432E7C51CA8553FDC5BEE873FF2468EE";
147 // Helper to get title of the calling application shown in the screen capture
149 base::string16 GetApplicationTitle(content::WebContents* web_contents,
150 const extensions::Extension* extension) {
151 // Use extension name as title for extensions and host/origin for drive-by
155 title = extension->name();
157 GURL url = web_contents->GetURL();
158 title = url.SchemeIsSecure() ? net::GetHostAndOptionalPort(url)
159 : url.GetOrigin().spec();
161 return base::UTF8ToUTF16(title);
164 // Helper to get list of media stream devices for desktop capture in |devices|.
165 // Registers to display notification if |display_notification| is true.
166 // Returns an instance of MediaStreamUI to be passed to content layer.
167 scoped_ptr<content::MediaStreamUI> GetDevicesForDesktopCapture(
168 content::MediaStreamDevices& devices,
169 content::DesktopMediaID media_id,
171 bool display_notification,
172 const base::string16& application_title,
173 const base::string16& registered_extension_name) {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
175 scoped_ptr<content::MediaStreamUI> ui;
177 // Add selected desktop source to the list.
178 devices.push_back(content::MediaStreamDevice(
179 content::MEDIA_DESKTOP_VIDEO_CAPTURE, media_id.ToString(), "Screen"));
181 // Use the special loopback device ID for system audio capture.
182 devices.push_back(content::MediaStreamDevice(
183 content::MEDIA_LOOPBACK_AUDIO_CAPTURE,
184 media::AudioManagerBase::kLoopbackInputDeviceId, "System Audio"));
187 // If required, register to display the notification for stream capture.
188 if (display_notification) {
189 if (application_title == registered_extension_name) {
190 ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16(
191 IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT,
194 ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16(
195 IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT_DELEGATED,
196 registered_extension_name,
204 #if defined(AUDIO_STREAM_MONITORING)
206 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(
207 int render_process_id,
208 int render_frame_id) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
210 content::WebContents* const web_contents =
211 content::WebContents::FromRenderFrameHost(
212 content::RenderFrameHost::FromID(render_process_id, render_frame_id));
215 return AudioStreamMonitor::FromWebContents(web_contents);
218 void StartAudioStreamMonitoringOnUIThread(
219 int render_process_id,
222 const AudioStreamMonitor::ReadPowerAndClipCallback& read_power_callback) {
223 AudioStreamMonitor* const audio_stream_monitor =
224 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
225 if (audio_stream_monitor)
226 audio_stream_monitor->StartMonitoringStream(stream_id, read_power_callback);
229 void StopAudioStreamMonitoringOnUIThread(
230 int render_process_id,
233 AudioStreamMonitor* const audio_stream_monitor =
234 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
235 if (audio_stream_monitor)
236 audio_stream_monitor->StopMonitoringStream(stream_id);
239 #endif // defined(AUDIO_STREAM_MONITORING)
243 MediaCaptureDevicesDispatcher::PendingAccessRequest::PendingAccessRequest(
244 const content::MediaStreamRequest& request,
245 const content::MediaResponseCallback& callback)
250 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {}
252 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
253 return Singleton<MediaCaptureDevicesDispatcher>::get();
256 MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher()
257 : is_device_enumeration_disabled_(false),
258 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()) {
259 // MediaCaptureDevicesDispatcher is a singleton. It should be created on
260 // UI thread. Otherwise, it will not receive
261 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in
262 // possible use after free.
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
264 notifications_registrar_.Add(
265 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
266 content::NotificationService::AllSources());
268 // AVFoundation is used for video/audio device monitoring and video capture in
269 // Mac. Experimentally, connect it in Canary and Unkown (developer builds).
270 #if defined(OS_MACOSX)
271 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
272 if (channel == chrome::VersionInfo::CHANNEL_CANARY ||
273 channel == chrome::VersionInfo::CHANNEL_UNKNOWN) {
274 CommandLine::ForCurrentProcess()->AppendSwitch(
275 switches::kEnableAVFoundation);
280 MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {}
282 void MediaCaptureDevicesDispatcher::RegisterProfilePrefs(
283 user_prefs::PrefRegistrySyncable* registry) {
284 registry->RegisterStringPref(
285 prefs::kDefaultAudioCaptureDevice,
287 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
288 registry->RegisterStringPref(
289 prefs::kDefaultVideoCaptureDevice,
291 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
294 void MediaCaptureDevicesDispatcher::AddObserver(Observer* observer) {
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
296 if (!observers_.HasObserver(observer))
297 observers_.AddObserver(observer);
300 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) {
301 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
302 observers_.RemoveObserver(observer);
305 const MediaStreamDevices&
306 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
308 if (is_device_enumeration_disabled_ || !test_audio_devices_.empty())
309 return test_audio_devices_;
311 return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
314 const MediaStreamDevices&
315 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
317 if (is_device_enumeration_disabled_ || !test_video_devices_.empty())
318 return test_video_devices_;
320 return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
323 void MediaCaptureDevicesDispatcher::Observe(
325 const content::NotificationSource& source,
326 const content::NotificationDetails& details) {
327 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
328 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
329 content::WebContents* web_contents =
330 content::Source<content::WebContents>(source).ptr();
331 pending_requests_.erase(web_contents);
335 void MediaCaptureDevicesDispatcher::ProcessMediaAccessRequest(
336 content::WebContents* web_contents,
337 const content::MediaStreamRequest& request,
338 const content::MediaResponseCallback& callback,
339 const extensions::Extension* extension) {
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
342 if (request.video_type == content::MEDIA_DESKTOP_VIDEO_CAPTURE ||
343 request.audio_type == content::MEDIA_LOOPBACK_AUDIO_CAPTURE) {
344 ProcessDesktopCaptureAccessRequest(
345 web_contents, request, callback, extension);
346 } else if (request.video_type == content::MEDIA_TAB_VIDEO_CAPTURE ||
347 request.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE) {
348 ProcessTabCaptureAccessRequest(
349 web_contents, request, callback, extension);
350 } else if (extension && (extension->is_platform_app() ||
351 IsMediaRequestWhitelistedForExtension(extension))) {
352 // For extensions access is approved based on extension permissions.
353 ProcessMediaAccessRequestFromPlatformAppOrExtension(
354 web_contents, request, callback, extension);
356 ProcessRegularMediaAccessRequest(web_contents, request, callback);
360 void MediaCaptureDevicesDispatcher::ProcessDesktopCaptureAccessRequest(
361 content::WebContents* web_contents,
362 const content::MediaStreamRequest& request,
363 const content::MediaResponseCallback& callback,
364 const extensions::Extension* extension) {
365 content::MediaStreamDevices devices;
366 scoped_ptr<content::MediaStreamUI> ui;
368 if (request.video_type != content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
369 callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
373 // If the device id wasn't specified then this is a screen capture request
374 // (i.e. chooseDesktopMedia() API wasn't used to generate device id).
375 if (request.requested_video_device_id.empty()) {
376 ProcessScreenCaptureAccessRequest(
377 web_contents, request, callback, extension);
381 // The extension name that the stream is registered with.
382 std::string original_extension_name;
383 // Resolve DesktopMediaID for the specified device id.
384 content::DesktopMediaID media_id =
385 GetDesktopStreamsRegistry()->RequestMediaForStreamId(
386 request.requested_video_device_id, request.render_process_id,
387 request.render_view_id, request.security_origin,
388 &original_extension_name);
390 // Received invalid device id.
391 if (media_id.type == content::DesktopMediaID::TYPE_NONE) {
392 callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
396 bool loopback_audio_supported = false;
397 #if defined(USE_CRAS) || defined(OS_WIN)
398 // Currently loopback audio capture is supported only on Windows and ChromeOS.
399 loopback_audio_supported = true;
402 // Audio is only supported for screen capture streams.
404 (media_id.type == content::DesktopMediaID::TYPE_SCREEN &&
405 request.audio_type == content::MEDIA_LOOPBACK_AUDIO_CAPTURE &&
406 loopback_audio_supported);
408 ui = GetDevicesForDesktopCapture(
409 devices, media_id, capture_audio, true,
410 GetApplicationTitle(web_contents, extension),
411 base::UTF8ToUTF16(original_extension_name));
413 callback.Run(devices, content::MEDIA_DEVICE_OK, ui.Pass());
416 void MediaCaptureDevicesDispatcher::ProcessScreenCaptureAccessRequest(
417 content::WebContents* web_contents,
418 const content::MediaStreamRequest& request,
419 const content::MediaResponseCallback& callback,
420 const extensions::Extension* extension) {
421 content::MediaStreamDevices devices;
422 scoped_ptr<content::MediaStreamUI> ui;
424 DCHECK_EQ(request.video_type, content::MEDIA_DESKTOP_VIDEO_CAPTURE);
426 bool loopback_audio_supported = false;
427 #if defined(USE_CRAS) || defined(OS_WIN)
428 // Currently loopback audio capture is supported only on Windows and ChromeOS.
429 loopback_audio_supported = true;
432 const bool component_extension =
433 extension && extension->location() == extensions::Manifest::COMPONENT;
435 const bool screen_capture_enabled =
436 CommandLine::ForCurrentProcess()->HasSwitch(
437 switches::kEnableUserMediaScreenCapturing) ||
438 IsOriginWhitelistedForScreenCapture(request.security_origin);
440 const bool origin_is_secure =
441 request.security_origin.SchemeIsSecure() ||
442 request.security_origin.SchemeIs(extensions::kExtensionScheme) ||
443 CommandLine::ForCurrentProcess()->HasSwitch(
444 switches::kAllowHttpScreenCapture);
446 // Approve request only when the following conditions are met:
447 // 1. Screen capturing is enabled via command line switch or white-listed for
449 // 2. Request comes from a page with a secure origin or from an extension.
450 if (screen_capture_enabled && origin_is_secure) {
451 // Get title of the calling application prior to showing the message box.
452 // chrome::ShowMessageBox() starts a nested message loop which may allow
453 // |web_contents| to be destroyed on the UI thread before the message box
454 // is closed. See http://crbug.com/326690.
455 base::string16 application_title =
456 GetApplicationTitle(web_contents, extension);
459 // For component extensions, bypass message box.
460 bool user_approved = false;
461 if (!component_extension) {
462 base::string16 application_name = base::UTF8ToUTF16(
463 extension ? extension->name() : request.security_origin.spec());
464 base::string16 confirmation_text = l10n_util::GetStringFUTF16(
465 request.audio_type == content::MEDIA_NO_SERVICE ?
466 IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TEXT :
467 IDS_MEDIA_SCREEN_AND_AUDIO_CAPTURE_CONFIRMATION_TEXT,
469 chrome::MessageBoxResult result = chrome::ShowMessageBox(
471 l10n_util::GetStringFUTF16(
472 IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TITLE, application_name),
474 chrome::MESSAGE_BOX_TYPE_QUESTION);
475 user_approved = (result == chrome::MESSAGE_BOX_RESULT_YES);
478 if (user_approved || component_extension) {
479 content::DesktopMediaID screen_id;
480 #if defined(OS_CHROMEOS)
481 screen_id = content::DesktopMediaID::RegisterAuraWindow(
482 ash::Shell::GetInstance()->GetPrimaryRootWindow());
483 #else // defined(OS_CHROMEOS)
485 content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN,
486 webrtc::kFullDesktopScreenId);
487 #endif // !defined(OS_CHROMEOS)
490 (request.audio_type == content::MEDIA_LOOPBACK_AUDIO_CAPTURE &&
491 loopback_audio_supported);
493 // Unless we're being invoked from a component extension, register to
494 // display the notification for stream capture.
495 bool display_notification = !component_extension;
497 ui = GetDevicesForDesktopCapture(devices, screen_id, capture_audio,
498 display_notification, application_title,
505 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
506 content::MEDIA_DEVICE_OK,
510 void MediaCaptureDevicesDispatcher::ProcessTabCaptureAccessRequest(
511 content::WebContents* web_contents,
512 const content::MediaStreamRequest& request,
513 const content::MediaResponseCallback& callback,
514 const extensions::Extension* extension) {
515 content::MediaStreamDevices devices;
516 scoped_ptr<content::MediaStreamUI> ui;
518 #if defined(OS_ANDROID)
519 // Tab capture is not supported on Android.
520 callback.Run(devices, content::MEDIA_DEVICE_TAB_CAPTURE_FAILURE, ui.Pass());
521 #else // defined(OS_ANDROID)
523 Profile::FromBrowserContext(web_contents->GetBrowserContext());
524 extensions::TabCaptureRegistry* tab_capture_registry =
525 extensions::TabCaptureRegistry::Get(profile);
526 if (!tab_capture_registry) {
528 callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
531 bool tab_capture_allowed =
532 tab_capture_registry->VerifyRequest(request.render_process_id,
533 request.render_view_id);
535 if (request.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE &&
536 tab_capture_allowed &&
537 extension->HasAPIPermission(extensions::APIPermission::kTabCapture)) {
538 devices.push_back(content::MediaStreamDevice(
539 content::MEDIA_TAB_AUDIO_CAPTURE, std::string(), std::string()));
542 if (request.video_type == content::MEDIA_TAB_VIDEO_CAPTURE &&
543 tab_capture_allowed &&
544 extension->HasAPIPermission(extensions::APIPermission::kTabCapture)) {
545 devices.push_back(content::MediaStreamDevice(
546 content::MEDIA_TAB_VIDEO_CAPTURE, std::string(), std::string()));
549 if (!devices.empty()) {
550 ui = media_stream_capture_indicator_->RegisterMediaStream(
551 web_contents, devices);
555 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
556 content::MEDIA_DEVICE_OK,
558 #endif // !defined(OS_ANDROID)
561 void MediaCaptureDevicesDispatcher::
562 ProcessMediaAccessRequestFromPlatformAppOrExtension(
563 content::WebContents* web_contents,
564 const content::MediaStreamRequest& request,
565 const content::MediaResponseCallback& callback,
566 const extensions::Extension* extension) {
567 content::MediaStreamDevices devices;
569 Profile::FromBrowserContext(web_contents->GetBrowserContext());
571 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE &&
572 extension->HasAPIPermission(extensions::APIPermission::kAudioCapture)) {
573 GetDefaultDevicesForProfile(profile, true, false, &devices);
576 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE &&
577 extension->HasAPIPermission(extensions::APIPermission::kVideoCapture)) {
578 GetDefaultDevicesForProfile(profile, false, true, &devices);
581 scoped_ptr<content::MediaStreamUI> ui;
582 if (!devices.empty()) {
583 ui = media_stream_capture_indicator_->RegisterMediaStream(
584 web_contents, devices);
588 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
589 content::MEDIA_DEVICE_OK,
593 void MediaCaptureDevicesDispatcher::ProcessRegularMediaAccessRequest(
594 content::WebContents* web_contents,
595 const content::MediaStreamRequest& request,
596 const content::MediaResponseCallback& callback) {
597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
599 RequestsQueue& queue = pending_requests_[web_contents];
600 queue.push_back(PendingAccessRequest(request, callback));
602 // If this is the only request then show the infobar.
603 if (queue.size() == 1)
604 ProcessQueuedAccessRequest(web_contents);
607 void MediaCaptureDevicesDispatcher::ProcessQueuedAccessRequest(
608 content::WebContents* web_contents) {
609 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
611 std::map<content::WebContents*, RequestsQueue>::iterator it =
612 pending_requests_.find(web_contents);
614 if (it == pending_requests_.end() || it->second.empty()) {
615 // Don't do anything if the tab was closed.
619 DCHECK(!it->second.empty());
621 if (PermissionBubbleManager::Enabled()) {
622 scoped_ptr<MediaStreamDevicesController> controller(
623 new MediaStreamDevicesController(web_contents,
624 it->second.front().request,
625 base::Bind(&MediaCaptureDevicesDispatcher::OnAccessRequestResponse,
626 base::Unretained(this), web_contents)));
627 if (controller->DismissInfoBarAndTakeActionOnSettings())
629 PermissionBubbleManager::FromWebContents(web_contents)->
630 AddRequest(controller.release());
634 // TODO(gbillock): delete this block and the MediaStreamInfoBarDelegate
635 // when we've transitioned to bubbles. (crbug/337458)
636 MediaStreamInfoBarDelegate::Create(
637 web_contents, it->second.front().request,
638 base::Bind(&MediaCaptureDevicesDispatcher::OnAccessRequestResponse,
639 base::Unretained(this), web_contents));
642 void MediaCaptureDevicesDispatcher::OnAccessRequestResponse(
643 content::WebContents* web_contents,
644 const content::MediaStreamDevices& devices,
645 content::MediaStreamRequestResult result,
646 scoped_ptr<content::MediaStreamUI> ui) {
647 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
649 std::map<content::WebContents*, RequestsQueue>::iterator it =
650 pending_requests_.find(web_contents);
651 if (it == pending_requests_.end()) {
652 // WebContents has been destroyed. Don't need to do anything.
656 RequestsQueue& queue(it->second);
660 content::MediaResponseCallback callback = queue.front().callback;
663 if (!queue.empty()) {
664 // Post a task to process next queued request. It has to be done
665 // asynchronously to make sure that calling infobar is not destroyed until
666 // after this function returns.
667 BrowserThread::PostTask(
668 BrowserThread::UI, FROM_HERE,
669 base::Bind(&MediaCaptureDevicesDispatcher::ProcessQueuedAccessRequest,
670 base::Unretained(this), web_contents));
673 callback.Run(devices, result, ui.Pass());
676 void MediaCaptureDevicesDispatcher::GetDefaultDevicesForProfile(
680 content::MediaStreamDevices* devices) {
681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
682 DCHECK(audio || video);
684 PrefService* prefs = profile->GetPrefs();
685 std::string default_device;
687 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
688 const content::MediaStreamDevice* device =
689 GetRequestedAudioDevice(default_device);
691 device = GetFirstAvailableAudioDevice();
693 devices->push_back(*device);
697 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
698 const content::MediaStreamDevice* device =
699 GetRequestedVideoDevice(default_device);
701 device = GetFirstAvailableVideoDevice();
703 devices->push_back(*device);
707 const content::MediaStreamDevice*
708 MediaCaptureDevicesDispatcher::GetRequestedAudioDevice(
709 const std::string& requested_audio_device_id) {
710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
711 const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
712 const content::MediaStreamDevice* const device =
713 FindDeviceWithId(audio_devices, requested_audio_device_id);
717 const content::MediaStreamDevice*
718 MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() {
719 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
720 const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices();
721 if (audio_devices.empty())
723 return &(*audio_devices.begin());
726 const content::MediaStreamDevice*
727 MediaCaptureDevicesDispatcher::GetRequestedVideoDevice(
728 const std::string& requested_video_device_id) {
729 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
730 const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
731 const content::MediaStreamDevice* const device =
732 FindDeviceWithId(video_devices, requested_video_device_id);
736 const content::MediaStreamDevice*
737 MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() {
738 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
739 const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices();
740 if (video_devices.empty())
742 return &(*video_devices.begin());
745 void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() {
746 is_device_enumeration_disabled_ = true;
749 scoped_refptr<MediaStreamCaptureIndicator>
750 MediaCaptureDevicesDispatcher::GetMediaStreamCaptureIndicator() {
751 return media_stream_capture_indicator_;
754 DesktopStreamsRegistry*
755 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() {
756 if (!desktop_streams_registry_)
757 desktop_streams_registry_.reset(new DesktopStreamsRegistry());
758 return desktop_streams_registry_.get();
761 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
762 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
763 BrowserThread::PostTask(
764 BrowserThread::UI, FROM_HERE,
766 &MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread,
767 base::Unretained(this)));
770 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
771 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
772 BrowserThread::PostTask(
773 BrowserThread::UI, FROM_HERE,
775 &MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread,
776 base::Unretained(this)));
779 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
780 int render_process_id,
783 const GURL& security_origin,
784 const content::MediaStreamDevice& device,
785 content::MediaRequestState state) {
786 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
787 BrowserThread::PostTask(
788 BrowserThread::UI, FROM_HERE,
790 &MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread,
791 base::Unretained(this), render_process_id, render_view_id,
792 page_request_id, security_origin, device, state));
795 void MediaCaptureDevicesDispatcher::OnAudioStreamPlaying(
796 int render_process_id,
799 const ReadPowerAndClipCallback& read_power_callback) {
800 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
801 #if defined(AUDIO_STREAM_MONITORING)
802 BrowserThread::PostTask(
805 base::Bind(&StartAudioStreamMonitoringOnUIThread,
809 read_power_callback));
813 void MediaCaptureDevicesDispatcher::OnAudioStreamStopped(
814 int render_process_id,
817 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
818 #if defined(AUDIO_STREAM_MONITORING)
819 BrowserThread::PostTask(
822 base::Bind(&StopAudioStreamMonitoringOnUIThread,
829 void MediaCaptureDevicesDispatcher::OnCreatingAudioStream(
830 int render_process_id,
831 int render_frame_id) {
832 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
833 BrowserThread::PostTask(
834 BrowserThread::UI, FROM_HERE,
836 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread,
837 base::Unretained(this), render_process_id, render_frame_id));
840 void MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() {
841 MediaStreamDevices devices = GetAudioCaptureDevices();
842 FOR_EACH_OBSERVER(Observer, observers_,
843 OnUpdateAudioDevices(devices));
846 void MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() {
847 MediaStreamDevices devices = GetVideoCaptureDevices();
848 FOR_EACH_OBSERVER(Observer, observers_,
849 OnUpdateVideoDevices(devices));
852 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread(
853 int render_process_id,
856 const GURL& security_origin,
857 const content::MediaStreamDevice& device,
858 content::MediaRequestState state) {
859 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced
860 // session counts since not all requests will reach MEDIA_REQUEST_STATE_DONE,
861 // but they will all reach MEDIA_REQUEST_STATE_CLOSING.
862 if (device.type == content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
863 if (state == content::MEDIA_REQUEST_STATE_DONE) {
864 DesktopCaptureSession session = { render_process_id, render_view_id,
866 desktop_capture_sessions_.push_back(session);
867 } else if (state == content::MEDIA_REQUEST_STATE_CLOSING) {
868 for (DesktopCaptureSessions::iterator it =
869 desktop_capture_sessions_.begin();
870 it != desktop_capture_sessions_.end();
872 if (it->render_process_id == render_process_id &&
873 it->render_view_id == render_view_id &&
874 it->page_request_id == page_request_id) {
875 desktop_capture_sessions_.erase(it);
882 // Cancel the request.
883 if (state == content::MEDIA_REQUEST_STATE_CLOSING) {
885 for (RequestsQueues::iterator rqs_it = pending_requests_.begin();
886 rqs_it != pending_requests_.end(); ++rqs_it) {
887 RequestsQueue& queue = rqs_it->second;
888 for (RequestsQueue::iterator it = queue.begin();
889 it != queue.end(); ++it) {
890 if (it->request.render_process_id == render_process_id &&
891 it->request.render_view_id == render_view_id &&
892 it->request.page_request_id == page_request_id) {
903 #if defined(OS_CHROMEOS)
904 if (IsOriginForCasting(security_origin) && IsVideoMediaType(device.type)) {
905 // Notify ash that casting state has changed.
906 if (state == content::MEDIA_REQUEST_STATE_DONE) {
907 ash::Shell::GetInstance()->OnCastingSessionStartedOrStopped(true);
908 } else if (state == content::MEDIA_REQUEST_STATE_CLOSING) {
909 ash::Shell::GetInstance()->OnCastingSessionStartedOrStopped(false);
914 FOR_EACH_OBSERVER(Observer, observers_,
915 OnRequestUpdate(render_process_id,
921 void MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread(
922 int render_process_id,
923 int render_frame_id) {
924 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
925 FOR_EACH_OBSERVER(Observer, observers_,
926 OnCreatingAudioStream(render_process_id, render_frame_id));
927 #if defined(AUDIO_STREAM_MONITORING)
928 content::WebContents* const web_contents =
929 content::WebContents::FromRenderFrameHost(
930 content::RenderFrameHost::FromID(render_process_id, render_frame_id));
932 // Note: Calling CreateForWebContents() multiple times is valid (see usage
933 // info for content::WebContentsUserData).
934 AudioStreamMonitor::CreateForWebContents(web_contents);
939 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() {
940 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
941 return desktop_capture_sessions_.size() > 0;
945 void MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices(
946 const MediaStreamDevices& devices) {
947 test_audio_devices_ = devices;
950 void MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices(
951 const MediaStreamDevices& devices) {
952 test_video_devices_ = devices;