93d462b8bbe66567759879b3ea2e852421d03587
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / media_capture_devices_dispatcher.cc
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.
4
5 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
6
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"
44
45 #if defined(OS_CHROMEOS)
46 #include "ash/shell.h"
47 #endif  // defined(OS_CHROMEOS)
48
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)
55
56 using content::BrowserThread;
57 using content::MediaCaptureDevices;
58 using content::MediaStreamDevices;
59
60 namespace {
61
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) {
69       return &(*iter);
70     }
71   }
72   return NULL;
73 };
74
75 // This is a short-term solution to grant camera and/or microphone access to
76 // extensions:
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";
90 }
91
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/") {
107     return true;
108   }
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());
115   return
116       hexencoded_origin_hash == "3C2705BC432E7C51CA8553FDC5BEE873FF2468EE";
117 #else
118   return false;
119 #endif
120 }
121
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/") {
131     return true;
132   }
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());
139   return
140       hexencoded_origin_hash == "3C2705BC432E7C51CA8553FDC5BEE873FF2468EE";
141 #else
142   return false;
143 #endif
144 }
145 #endif
146
147 // Helper to get title of the calling application shown in the screen capture
148 // notification.
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
152   // web.
153   std::string title;
154   if (extension) {
155     title = extension->name();
156   } else {
157     GURL url = web_contents->GetURL();
158     title = url.SchemeIsSecure() ? net::GetHostAndOptionalPort(url)
159                                  : url.GetOrigin().spec();
160   }
161   return base::UTF8ToUTF16(title);
162 }
163
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,
170     bool capture_audio,
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;
176
177   // Add selected desktop source to the list.
178   devices.push_back(content::MediaStreamDevice(
179       content::MEDIA_DESKTOP_VIDEO_CAPTURE, media_id.ToString(), "Screen"));
180   if (capture_audio) {
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"));
185   }
186
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,
192           application_title));
193     } else {
194       ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16(
195           IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT_DELEGATED,
196           registered_extension_name,
197           application_title));
198     }
199   }
200
201   return ui.Pass();
202 }
203
204 #if defined(AUDIO_STREAM_MONITORING)
205
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));
213   if (!web_contents)
214     return NULL;
215   return AudioStreamMonitor::FromWebContents(web_contents);
216 }
217
218 void StartAudioStreamMonitoringOnUIThread(
219     int render_process_id,
220     int render_frame_id,
221     int stream_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);
227 }
228
229 void StopAudioStreamMonitoringOnUIThread(
230     int render_process_id,
231     int render_frame_id,
232     int stream_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);
237 }
238
239 #endif  // defined(AUDIO_STREAM_MONITORING)
240
241 }  // namespace
242
243 MediaCaptureDevicesDispatcher::PendingAccessRequest::PendingAccessRequest(
244     const content::MediaStreamRequest& request,
245     const content::MediaResponseCallback& callback)
246     : request(request),
247       callback(callback) {
248 }
249
250 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {}
251
252 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
253   return Singleton<MediaCaptureDevicesDispatcher>::get();
254 }
255
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());
267
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);
276   }
277 #endif
278 }
279
280 MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {}
281
282 void MediaCaptureDevicesDispatcher::RegisterProfilePrefs(
283     user_prefs::PrefRegistrySyncable* registry) {
284   registry->RegisterStringPref(
285       prefs::kDefaultAudioCaptureDevice,
286       std::string(),
287       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
288   registry->RegisterStringPref(
289       prefs::kDefaultVideoCaptureDevice,
290       std::string(),
291       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
292 }
293
294 void MediaCaptureDevicesDispatcher::AddObserver(Observer* observer) {
295   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
296   if (!observers_.HasObserver(observer))
297     observers_.AddObserver(observer);
298 }
299
300 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) {
301   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
302   observers_.RemoveObserver(observer);
303 }
304
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_;
310
311   return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
312 }
313
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_;
319
320   return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
321 }
322
323 void MediaCaptureDevicesDispatcher::Observe(
324     int type,
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);
332   }
333 }
334
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));
341
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);
355   } else {
356     ProcessRegularMediaAccessRequest(web_contents, request, callback);
357   }
358 }
359
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;
367
368   if (request.video_type != content::MEDIA_DESKTOP_VIDEO_CAPTURE) {
369     callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
370     return;
371   }
372
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);
378     return;
379   }
380
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);
389
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());
393     return;
394   }
395
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;
400 #endif
401
402   // Audio is only supported for screen capture streams.
403   bool capture_audio =
404       (media_id.type == content::DesktopMediaID::TYPE_SCREEN &&
405        request.audio_type == content::MEDIA_LOOPBACK_AUDIO_CAPTURE &&
406        loopback_audio_supported);
407
408   ui = GetDevicesForDesktopCapture(
409       devices, media_id, capture_audio, true,
410       GetApplicationTitle(web_contents, extension),
411       base::UTF8ToUTF16(original_extension_name));
412
413   callback.Run(devices, content::MEDIA_DEVICE_OK, ui.Pass());
414 }
415
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;
423
424   DCHECK_EQ(request.video_type, content::MEDIA_DESKTOP_VIDEO_CAPTURE);
425
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;
430 #endif
431
432   const bool component_extension =
433       extension && extension->location() == extensions::Manifest::COMPONENT;
434
435   const bool screen_capture_enabled =
436       CommandLine::ForCurrentProcess()->HasSwitch(
437           switches::kEnableUserMediaScreenCapturing) ||
438       IsOriginWhitelistedForScreenCapture(request.security_origin);
439
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);
445
446   // Approve request only when the following conditions are met:
447   //  1. Screen capturing is enabled via command line switch or white-listed for
448   //     the given origin.
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);
457     web_contents = NULL;
458
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,
468           application_name);
469       chrome::MessageBoxResult result = chrome::ShowMessageBox(
470           NULL,
471           l10n_util::GetStringFUTF16(
472               IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TITLE, application_name),
473           confirmation_text,
474           chrome::MESSAGE_BOX_TYPE_QUESTION);
475       user_approved = (result == chrome::MESSAGE_BOX_RESULT_YES);
476     }
477
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)
484       screen_id =
485           content::DesktopMediaID(content::DesktopMediaID::TYPE_SCREEN,
486                                   webrtc::kFullDesktopScreenId);
487 #endif  // !defined(OS_CHROMEOS)
488
489       bool capture_audio =
490           (request.audio_type == content::MEDIA_LOOPBACK_AUDIO_CAPTURE &&
491            loopback_audio_supported);
492
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;
496
497       ui = GetDevicesForDesktopCapture(devices, screen_id, capture_audio,
498                                        display_notification, application_title,
499                                        application_title);
500     }
501   }
502
503   callback.Run(
504     devices,
505     devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
506                       content::MEDIA_DEVICE_OK,
507     ui.Pass());
508 }
509
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;
517
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)
522   Profile* profile =
523       Profile::FromBrowserContext(web_contents->GetBrowserContext());
524   extensions::TabCaptureRegistry* tab_capture_registry =
525       extensions::TabCaptureRegistry::Get(profile);
526   if (!tab_capture_registry) {
527     NOTREACHED();
528     callback.Run(devices, content::MEDIA_DEVICE_INVALID_STATE, ui.Pass());
529     return;
530   }
531   bool tab_capture_allowed =
532       tab_capture_registry->VerifyRequest(request.render_process_id,
533                                           request.render_view_id);
534
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()));
540   }
541
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()));
547   }
548
549   if (!devices.empty()) {
550     ui = media_stream_capture_indicator_->RegisterMediaStream(
551         web_contents, devices);
552   }
553   callback.Run(
554     devices,
555     devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
556                       content::MEDIA_DEVICE_OK,
557     ui.Pass());
558 #endif  // !defined(OS_ANDROID)
559 }
560
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;
568   Profile* profile =
569       Profile::FromBrowserContext(web_contents->GetBrowserContext());
570
571   if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE &&
572       extension->HasAPIPermission(extensions::APIPermission::kAudioCapture)) {
573     GetDefaultDevicesForProfile(profile, true, false, &devices);
574   }
575
576   if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE &&
577       extension->HasAPIPermission(extensions::APIPermission::kVideoCapture)) {
578     GetDefaultDevicesForProfile(profile, false, true, &devices);
579   }
580
581   scoped_ptr<content::MediaStreamUI> ui;
582   if (!devices.empty()) {
583     ui = media_stream_capture_indicator_->RegisterMediaStream(
584         web_contents, devices);
585   }
586   callback.Run(
587     devices,
588     devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE :
589                       content::MEDIA_DEVICE_OK,
590     ui.Pass());
591 }
592
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));
598
599   RequestsQueue& queue = pending_requests_[web_contents];
600   queue.push_back(PendingAccessRequest(request, callback));
601
602   // If this is the only request then show the infobar.
603   if (queue.size() == 1)
604     ProcessQueuedAccessRequest(web_contents);
605 }
606
607 void MediaCaptureDevicesDispatcher::ProcessQueuedAccessRequest(
608     content::WebContents* web_contents) {
609   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
610
611   std::map<content::WebContents*, RequestsQueue>::iterator it =
612       pending_requests_.find(web_contents);
613
614   if (it == pending_requests_.end() || it->second.empty()) {
615     // Don't do anything if the tab was closed.
616     return;
617   }
618
619   DCHECK(!it->second.empty());
620
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())
628       return;
629     PermissionBubbleManager::FromWebContents(web_contents)->
630         AddRequest(controller.release());
631     return;
632   }
633
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));
640 }
641
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));
648
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.
653     return;
654   }
655
656   RequestsQueue& queue(it->second);
657   if (queue.empty())
658     return;
659
660   content::MediaResponseCallback callback = queue.front().callback;
661   queue.pop_front();
662
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));
671   }
672
673   callback.Run(devices, result, ui.Pass());
674 }
675
676 void MediaCaptureDevicesDispatcher::GetDefaultDevicesForProfile(
677     Profile* profile,
678     bool audio,
679     bool video,
680     content::MediaStreamDevices* devices) {
681   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
682   DCHECK(audio || video);
683
684   PrefService* prefs = profile->GetPrefs();
685   std::string default_device;
686   if (audio) {
687     default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice);
688     const content::MediaStreamDevice* device =
689         GetRequestedAudioDevice(default_device);
690     if (!device)
691       device = GetFirstAvailableAudioDevice();
692     if (device)
693       devices->push_back(*device);
694   }
695
696   if (video) {
697     default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice);
698     const content::MediaStreamDevice* device =
699         GetRequestedVideoDevice(default_device);
700     if (!device)
701       device = GetFirstAvailableVideoDevice();
702     if (device)
703       devices->push_back(*device);
704   }
705 }
706
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);
714   return device;
715 }
716
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())
722     return NULL;
723   return &(*audio_devices.begin());
724 }
725
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);
733   return device;
734 }
735
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())
741     return NULL;
742   return &(*video_devices.begin());
743 }
744
745 void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() {
746   is_device_enumeration_disabled_ = true;
747 }
748
749 scoped_refptr<MediaStreamCaptureIndicator>
750 MediaCaptureDevicesDispatcher::GetMediaStreamCaptureIndicator() {
751   return media_stream_capture_indicator_;
752 }
753
754 DesktopStreamsRegistry*
755 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() {
756   if (!desktop_streams_registry_)
757     desktop_streams_registry_.reset(new DesktopStreamsRegistry());
758   return desktop_streams_registry_.get();
759 }
760
761 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
762   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
763   BrowserThread::PostTask(
764       BrowserThread::UI, FROM_HERE,
765       base::Bind(
766           &MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread,
767           base::Unretained(this)));
768 }
769
770 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
771   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
772   BrowserThread::PostTask(
773       BrowserThread::UI, FROM_HERE,
774       base::Bind(
775           &MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread,
776           base::Unretained(this)));
777 }
778
779 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
780     int render_process_id,
781     int render_view_id,
782     int page_request_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,
789       base::Bind(
790           &MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread,
791           base::Unretained(this), render_process_id, render_view_id,
792           page_request_id, security_origin, device, state));
793 }
794
795 void MediaCaptureDevicesDispatcher::OnAudioStreamPlaying(
796     int render_process_id,
797     int render_frame_id,
798     int stream_id,
799     const ReadPowerAndClipCallback& read_power_callback) {
800   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
801 #if defined(AUDIO_STREAM_MONITORING)
802   BrowserThread::PostTask(
803       BrowserThread::UI,
804       FROM_HERE,
805       base::Bind(&StartAudioStreamMonitoringOnUIThread,
806                  render_process_id,
807                  render_frame_id,
808                  stream_id,
809                  read_power_callback));
810 #endif
811 }
812
813 void MediaCaptureDevicesDispatcher::OnAudioStreamStopped(
814     int render_process_id,
815     int render_frame_id,
816     int stream_id) {
817   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
818 #if defined(AUDIO_STREAM_MONITORING)
819   BrowserThread::PostTask(
820       BrowserThread::UI,
821       FROM_HERE,
822       base::Bind(&StopAudioStreamMonitoringOnUIThread,
823                  render_process_id,
824                  render_frame_id,
825                  stream_id));
826 #endif
827 }
828
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,
835       base::Bind(
836           &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread,
837           base::Unretained(this), render_process_id, render_frame_id));
838 }
839
840 void MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() {
841   MediaStreamDevices devices = GetAudioCaptureDevices();
842   FOR_EACH_OBSERVER(Observer, observers_,
843                     OnUpdateAudioDevices(devices));
844 }
845
846 void MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() {
847   MediaStreamDevices devices = GetVideoCaptureDevices();
848   FOR_EACH_OBSERVER(Observer, observers_,
849                     OnUpdateVideoDevices(devices));
850 }
851
852 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread(
853     int render_process_id,
854     int render_view_id,
855     int page_request_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,
865                                         page_request_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();
871            ++it) {
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);
876           break;
877         }
878       }
879     }
880   }
881
882   // Cancel the request.
883   if (state == content::MEDIA_REQUEST_STATE_CLOSING) {
884     bool found = false;
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) {
893           queue.erase(it);
894           found = true;
895           break;
896         }
897       }
898       if (found)
899         break;
900     }
901   }
902
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);
910     }
911   }
912 #endif
913
914   FOR_EACH_OBSERVER(Observer, observers_,
915                     OnRequestUpdate(render_process_id,
916                                     render_view_id,
917                                     device,
918                                     state));
919 }
920
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));
931   if (web_contents) {
932     // Note: Calling CreateForWebContents() multiple times is valid (see usage
933     // info for content::WebContentsUserData).
934     AudioStreamMonitor::CreateForWebContents(web_contents);
935   }
936 #endif
937 }
938
939 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() {
940   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
941   return desktop_capture_sessions_.size() > 0;
942 }
943
944
945 void MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices(
946     const MediaStreamDevices& devices) {
947   test_audio_devices_ = devices;
948 }
949
950 void MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices(
951     const MediaStreamDevices& devices) {
952   test_video_devices_ = devices;
953 }