Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 10 | #include "base/sha1.h" |
| 11 | #include "base/strings/string_number_conversions.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 14 | #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" |
| 15 | #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry_factory.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "chrome/browser/media/audio_stream_indicator.h" |
| 17 | #include "chrome/browser/media/media_stream_capture_indicator.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 18 | #include "chrome/browser/media/media_stream_infobar_delegate.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 19 | #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 20 | #include "chrome/browser/profiles/profile.h" |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 21 | #include "chrome/browser/ui/screen_capture_notification_ui.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | #include "chrome/browser/ui/simple_message_box.h" |
| 23 | #include "chrome/common/chrome_switches.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 24 | #include "chrome/common/extensions/extension.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 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/media_devices_monitor.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 29 | #include "content/public/browser/notification_service.h" |
| 30 | #include "content/public/browser/notification_source.h" |
| 31 | #include "content/public/browser/notification_types.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 32 | #include "content/public/browser/web_contents.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 33 | #include "content/public/common/media_stream_request.h" |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 34 | #include "extensions/common/constants.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | #include "grit/generated_resources.h" |
| 36 | #include "ui/base/l10n/l10n_util.h" |
| 37 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 38 | #if defined(USE_CRAS) |
| 39 | #include "media/audio/cras/audio_manager_cras.h" |
| 40 | #endif |
| 41 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | using content::BrowserThread; |
| 43 | using content::MediaStreamDevices; |
| 44 | |
| 45 | namespace { |
| 46 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 47 | // Finds a device in |devices| that has |device_id|, or NULL if not found. |
| 48 | const content::MediaStreamDevice* FindDeviceWithId( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 49 | const content::MediaStreamDevices& devices, |
| 50 | const std::string& device_id) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | content::MediaStreamDevices::const_iterator iter = devices.begin(); |
| 52 | for (; iter != devices.end(); ++iter) { |
| 53 | if (iter->id == device_id) { |
| 54 | return &(*iter); |
| 55 | } |
| 56 | } |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 57 | return NULL; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // This is a short-term solution to allow testing of the the Screen Capture API |
| 61 | // with Google Hangouts in M27. |
| 62 | // TODO(sergeyu): Remove this whitelist as soon as possible. |
| 63 | bool IsOriginWhitelistedForScreenCapture(const GURL& origin) { |
| 64 | #if defined(OFFICIAL_BUILD) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 65 | if (// Google Hangouts. |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 66 | (origin.SchemeIs("https") && |
| 67 | EndsWith(origin.spec(), ".talkgadget.google.com/", true)) || |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 68 | origin.spec() == "https://plus.google.com/" || |
| 69 | origin.spec() == "chrome-extension://pkedcjkdefgpdelpbcmbmeomcjbeemfm/" || |
| 70 | origin.spec() == "chrome-extension://fmfcbgogabcbclcofgocippekhfcmgfj/" || |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 71 | origin.spec() == "chrome-extension://hfaagokkkhdbgiakmmlclaapfelnkoah/" || |
| 72 | origin.spec() == "chrome-extension://gfdkimpbcpahaombhbimeihdjnejgicl/") { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | // Check against hashed origins. |
| 76 | const std::string origin_hash = base::SHA1HashString(origin.spec()); |
| 77 | DCHECK_EQ(origin_hash.length(), base::kSHA1Length); |
| 78 | const std::string hexencoded_origin_hash = |
| 79 | base::HexEncode(origin_hash.data(), origin_hash.length()); |
| 80 | return |
| 81 | hexencoded_origin_hash == "3C2705BC432E7C51CA8553FDC5BEE873FF2468EE" || |
| 82 | hexencoded_origin_hash == "50F02B8A668CAB274527D58356F07C2143080FCC"; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | #else |
| 84 | return false; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 90 | MediaCaptureDevicesDispatcher::PendingAccessRequest::PendingAccessRequest( |
| 91 | const content::MediaStreamRequest& request, |
| 92 | const content::MediaResponseCallback& callback) |
| 93 | : request(request), |
| 94 | callback(callback) { |
| 95 | } |
| 96 | |
| 97 | MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {} |
| 98 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 99 | MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() { |
| 100 | return Singleton<MediaCaptureDevicesDispatcher>::get(); |
| 101 | } |
| 102 | |
| 103 | MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() |
| 104 | : devices_enumerated_(false), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 105 | is_device_enumeration_disabled_(false), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | media_stream_capture_indicator_(new MediaStreamCaptureIndicator()), |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 107 | audio_stream_indicator_(new AudioStreamIndicator()) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 108 | // MediaCaptureDevicesDispatcher is a singleton. It should be created on |
| 109 | // UI thread. Otherwise, it will not receive |
| 110 | // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in |
| 111 | // possible use after free. |
| 112 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 113 | notifications_registrar_.Add( |
| 114 | this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 115 | content::NotificationService::AllSources()); |
| 116 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 117 | |
| 118 | MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() {} |
| 119 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 120 | void MediaCaptureDevicesDispatcher::RegisterProfilePrefs( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 121 | user_prefs::PrefRegistrySyncable* registry) { |
| 122 | registry->RegisterStringPref( |
| 123 | prefs::kDefaultAudioCaptureDevice, |
| 124 | std::string(), |
| 125 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 126 | registry->RegisterStringPref( |
| 127 | prefs::kDefaultVideoCaptureDevice, |
| 128 | std::string(), |
| 129 | user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void MediaCaptureDevicesDispatcher::AddObserver(Observer* observer) { |
| 133 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 134 | if (!observers_.HasObserver(observer)) |
| 135 | observers_.AddObserver(observer); |
| 136 | } |
| 137 | |
| 138 | void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { |
| 139 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 140 | observers_.RemoveObserver(observer); |
| 141 | } |
| 142 | |
| 143 | const MediaStreamDevices& |
| 144 | MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { |
| 145 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 146 | if (!is_device_enumeration_disabled_ && !devices_enumerated_) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 147 | content::EnsureMonitorCaptureDevices(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 148 | devices_enumerated_ = true; |
| 149 | } |
| 150 | return audio_devices_; |
| 151 | } |
| 152 | |
| 153 | const MediaStreamDevices& |
| 154 | MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { |
| 155 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 156 | if (!is_device_enumeration_disabled_ && !devices_enumerated_) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 157 | content::EnsureMonitorCaptureDevices(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 158 | devices_enumerated_ = true; |
| 159 | } |
| 160 | return video_devices_; |
| 161 | } |
| 162 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 163 | void MediaCaptureDevicesDispatcher::Observe( |
| 164 | int type, |
| 165 | const content::NotificationSource& source, |
| 166 | const content::NotificationDetails& details) { |
| 167 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 168 | if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
| 169 | content::WebContents* web_contents = |
| 170 | content::Source<content::WebContents>(source).ptr(); |
| 171 | pending_requests_.erase(web_contents); |
| 172 | } |
| 173 | } |
| 174 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 175 | void MediaCaptureDevicesDispatcher::ProcessMediaAccessRequest( |
| 176 | content::WebContents* web_contents, |
| 177 | const content::MediaStreamRequest& request, |
| 178 | const content::MediaResponseCallback& callback, |
| 179 | const extensions::Extension* extension) { |
| 180 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 181 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 182 | if (request.video_type == content::MEDIA_SCREEN_VIDEO_CAPTURE || |
| 183 | request.audio_type == content::MEDIA_SYSTEM_AUDIO_CAPTURE) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 184 | ProcessScreenCaptureAccessRequest( |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 185 | web_contents, request, callback, extension); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 186 | } else if (extension) { |
| 187 | // For extensions access is approved based on extension permissions. |
| 188 | ProcessMediaAccessRequestFromExtension( |
| 189 | web_contents, request, callback, extension); |
| 190 | } else { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 191 | ProcessRegularMediaAccessRequest(web_contents, request, callback); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void MediaCaptureDevicesDispatcher::ProcessScreenCaptureAccessRequest( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 196 | content::WebContents* web_contents, |
| 197 | const content::MediaStreamRequest& request, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 198 | const content::MediaResponseCallback& callback, |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 199 | const extensions::Extension* extension) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 200 | const bool component_extension = |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 201 | extension && extension->location() == extensions::Manifest::COMPONENT; |
| 202 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 203 | content::MediaStreamDevices devices; |
| 204 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 205 | const bool screen_capture_enabled = |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 206 | CommandLine::ForCurrentProcess()->HasSwitch( |
| 207 | switches::kEnableUserMediaScreenCapturing) || |
| 208 | IsOriginWhitelistedForScreenCapture(request.security_origin); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 209 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 210 | const bool origin_is_secure = |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 211 | request.security_origin.SchemeIsSecure() || |
| 212 | request.security_origin.SchemeIs(extensions::kExtensionScheme) || |
| 213 | CommandLine::ForCurrentProcess()->HasSwitch( |
| 214 | switches::kAllowHttpScreenCapture); |
| 215 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 216 | const bool screen_video_capture_requested = |
| 217 | request.video_type == content::MEDIA_SCREEN_VIDEO_CAPTURE; |
| 218 | |
| 219 | const bool system_audio_capture_requested = |
| 220 | request.audio_type == content::MEDIA_SYSTEM_AUDIO_CAPTURE; |
| 221 | |
| 222 | #if defined(USE_CRAS) |
| 223 | const bool system_audio_capture_supported = true; |
| 224 | #else |
| 225 | const bool system_audio_capture_supported = false; |
| 226 | #endif |
| 227 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 228 | // Approve request only when the following conditions are met: |
| 229 | // 1. Screen capturing is enabled via command line switch or white-listed for |
| 230 | // the given origin. |
| 231 | // 2. Request comes from a page with a secure origin or from an extension. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 232 | // 3. Video capture is requested for screen video. |
| 233 | // 4. Audio capture is either not requested, or requested for system audio. |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 234 | if (screen_capture_enabled && origin_is_secure && |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 235 | screen_video_capture_requested && |
| 236 | (!system_audio_capture_requested || system_audio_capture_supported)) { |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 237 | // For component extensions, bypass message box. |
| 238 | bool user_approved = false; |
| 239 | if (!component_extension) { |
| 240 | string16 application_name = UTF8ToUTF16(request.security_origin.spec()); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 241 | string16 confirmation_text = l10n_util::GetStringFUTF16( |
| 242 | request.audio_type == content::MEDIA_NO_SERVICE ? |
| 243 | IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TEXT : |
| 244 | IDS_MEDIA_SCREEN_AND_AUDIO_CAPTURE_CONFIRMATION_TEXT, |
| 245 | application_name); |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 246 | chrome::MessageBoxResult result = chrome::ShowMessageBox( |
| 247 | NULL, |
| 248 | l10n_util::GetStringFUTF16( |
| 249 | IDS_MEDIA_SCREEN_CAPTURE_CONFIRMATION_TITLE, application_name), |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 250 | confirmation_text, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 251 | chrome::MESSAGE_BOX_TYPE_QUESTION); |
| 252 | user_approved = (result == chrome::MESSAGE_BOX_RESULT_YES); |
| 253 | } |
| 254 | |
| 255 | if (user_approved || component_extension) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 256 | devices.push_back(content::MediaStreamDevice( |
| 257 | content::MEDIA_SCREEN_VIDEO_CAPTURE, std::string(), "Screen")); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 258 | if (system_audio_capture_requested) { |
| 259 | #if defined(USE_CRAS) |
| 260 | // Use the special loopback device ID for system audio capture. |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 261 | devices.push_back(content::MediaStreamDevice( |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 262 | content::MEDIA_SYSTEM_AUDIO_CAPTURE, |
| 263 | media::AudioManagerCras::kLoopbackDeviceId, |
| 264 | "System Audio")); |
| 265 | #endif |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 266 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 270 | scoped_ptr<content::MediaStreamUI> ui; |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 271 | // Unless we're being invoked from a component extension, register to display |
| 272 | // the notification for stream capture. |
| 273 | if (!devices.empty() && !component_extension) { |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 274 | // Use extension name as title for extensions and origin for drive-by web. |
| 275 | std::string title; |
| 276 | if (extension) { |
| 277 | title = extension->name(); |
| 278 | } else { |
| 279 | title = web_contents->GetURL().GetOrigin().spec(); |
| 280 | } |
| 281 | |
| 282 | ui = ScreenCaptureNotificationUI::Create(l10n_util::GetStringFUTF16( |
| 283 | IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_TEXT, UTF8ToUTF16(title))); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 284 | } |
| 285 | callback.Run(devices, ui.Pass()); |
| 286 | } |
| 287 | |
| 288 | void MediaCaptureDevicesDispatcher::ProcessMediaAccessRequestFromExtension( |
| 289 | content::WebContents* web_contents, |
| 290 | const content::MediaStreamRequest& request, |
| 291 | const content::MediaResponseCallback& callback, |
| 292 | const extensions::Extension* extension) { |
| 293 | content::MediaStreamDevices devices; |
| 294 | Profile* profile = |
| 295 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 296 | |
| 297 | #if defined(OS_ANDROID) |
| 298 | // Tab capture is not supported on Android. |
| 299 | bool tab_capture_allowed = false; |
| 300 | #else |
| 301 | extensions::TabCaptureRegistry* tab_capture_registry = |
| 302 | extensions::TabCaptureRegistryFactory::GetForProfile(profile); |
| 303 | bool tab_capture_allowed = |
| 304 | tab_capture_registry->VerifyRequest(request.render_process_id, |
| 305 | request.render_view_id); |
| 306 | #endif |
| 307 | |
| 308 | if (request.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE && |
| 309 | tab_capture_allowed && |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 310 | extension->HasAPIPermission(extensions::APIPermission::kTabCapture)) { |
| 311 | devices.push_back(content::MediaStreamDevice( |
| 312 | content::MEDIA_TAB_AUDIO_CAPTURE, std::string(), std::string())); |
| 313 | } else if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE && |
| 314 | extension->HasAPIPermission( |
| 315 | extensions::APIPermission::kAudioCapture)) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 316 | GetDefaultDevicesForProfile(profile, true, false, &devices); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (request.video_type == content::MEDIA_TAB_VIDEO_CAPTURE && |
| 320 | tab_capture_allowed && |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 321 | extension->HasAPIPermission(extensions::APIPermission::kTabCapture)) { |
| 322 | devices.push_back(content::MediaStreamDevice( |
| 323 | content::MEDIA_TAB_VIDEO_CAPTURE, std::string(), std::string())); |
| 324 | } else if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE && |
| 325 | extension->HasAPIPermission(extensions::APIPermission::kVideoCapture)) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 326 | GetDefaultDevicesForProfile(profile, false, true, &devices); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | scoped_ptr<content::MediaStreamUI> ui; |
| 330 | if (!devices.empty()) { |
| 331 | ui = media_stream_capture_indicator_->RegisterMediaStream( |
| 332 | web_contents, devices); |
| 333 | } |
| 334 | callback.Run(devices, ui.Pass()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 337 | void MediaCaptureDevicesDispatcher::ProcessRegularMediaAccessRequest( |
| 338 | content::WebContents* web_contents, |
| 339 | const content::MediaStreamRequest& request, |
| 340 | const content::MediaResponseCallback& callback) { |
| 341 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 342 | |
| 343 | RequestsQueue& queue = pending_requests_[web_contents]; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 344 | queue.push_back(PendingAccessRequest(request, callback)); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 345 | |
| 346 | // If this is the only request then show the infobar. |
| 347 | if (queue.size() == 1) |
| 348 | ProcessQueuedAccessRequest(web_contents); |
| 349 | } |
| 350 | |
| 351 | void MediaCaptureDevicesDispatcher::ProcessQueuedAccessRequest( |
| 352 | content::WebContents* web_contents) { |
| 353 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 354 | |
| 355 | std::map<content::WebContents*, RequestsQueue>::iterator it = |
| 356 | pending_requests_.find(web_contents); |
| 357 | |
| 358 | if (it == pending_requests_.end() || it->second.empty()) { |
| 359 | // Don't do anything if the tab was was closed. |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | DCHECK(!it->second.empty()); |
| 364 | |
| 365 | MediaStreamInfoBarDelegate::Create( |
| 366 | web_contents, it->second.front().request, |
| 367 | base::Bind(&MediaCaptureDevicesDispatcher::OnAccessRequestResponse, |
| 368 | base::Unretained(this), web_contents)); |
| 369 | } |
| 370 | |
| 371 | void MediaCaptureDevicesDispatcher::OnAccessRequestResponse( |
| 372 | content::WebContents* web_contents, |
| 373 | const content::MediaStreamDevices& devices, |
| 374 | scoped_ptr<content::MediaStreamUI> ui) { |
| 375 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 376 | |
| 377 | std::map<content::WebContents*, RequestsQueue>::iterator it = |
| 378 | pending_requests_.find(web_contents); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 379 | if (it == pending_requests_.end()) { |
| 380 | // WebContents has been destroyed. Don't need to do anything. |
| 381 | return; |
| 382 | } |
| 383 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 384 | RequestsQueue& queue(it->second); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 385 | if (queue.empty()) |
| 386 | return; |
| 387 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 388 | content::MediaResponseCallback callback = queue.front().callback; |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 389 | queue.pop_front(); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 390 | |
| 391 | if (!queue.empty()) { |
| 392 | // Post a task to process next queued request. It has to be done |
| 393 | // asynchronously to make sure that calling infobar is not destroyed until |
| 394 | // after this function returns. |
| 395 | BrowserThread::PostTask( |
| 396 | BrowserThread::UI, FROM_HERE, |
| 397 | base::Bind(&MediaCaptureDevicesDispatcher::ProcessQueuedAccessRequest, |
| 398 | base::Unretained(this), web_contents)); |
| 399 | } |
| 400 | |
| 401 | callback.Run(devices, ui.Pass()); |
| 402 | } |
| 403 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 404 | void MediaCaptureDevicesDispatcher::GetDefaultDevicesForProfile( |
| 405 | Profile* profile, |
| 406 | bool audio, |
| 407 | bool video, |
| 408 | content::MediaStreamDevices* devices) { |
| 409 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 410 | DCHECK(audio || video); |
| 411 | |
| 412 | PrefService* prefs = profile->GetPrefs(); |
| 413 | std::string default_device; |
| 414 | if (audio) { |
| 415 | default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 416 | const content::MediaStreamDevice* device = |
| 417 | GetRequestedAudioDevice(default_device); |
| 418 | if (!device) |
| 419 | device = GetFirstAvailableAudioDevice(); |
| 420 | if (device) |
| 421 | devices->push_back(*device); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (video) { |
| 425 | default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 426 | const content::MediaStreamDevice* device = |
| 427 | GetRequestedVideoDevice(default_device); |
| 428 | if (!device) |
| 429 | device = GetFirstAvailableVideoDevice(); |
| 430 | if (device) |
| 431 | devices->push_back(*device); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 435 | const content::MediaStreamDevice* |
| 436 | MediaCaptureDevicesDispatcher::GetRequestedAudioDevice( |
| 437 | const std::string& requested_audio_device_id) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 438 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 439 | const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); |
| 440 | const content::MediaStreamDevice* const device = |
| 441 | FindDeviceWithId(audio_devices, requested_audio_device_id); |
| 442 | return device; |
| 443 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 444 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 445 | const content::MediaStreamDevice* |
| 446 | MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() { |
| 447 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 448 | const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); |
| 449 | if (audio_devices.empty()) |
| 450 | return NULL; |
| 451 | return &(*audio_devices.begin()); |
| 452 | } |
| 453 | |
| 454 | const content::MediaStreamDevice* |
| 455 | MediaCaptureDevicesDispatcher::GetRequestedVideoDevice( |
| 456 | const std::string& requested_video_device_id) { |
| 457 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 458 | const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); |
| 459 | const content::MediaStreamDevice* const device = |
| 460 | FindDeviceWithId(video_devices, requested_video_device_id); |
| 461 | return device; |
| 462 | } |
| 463 | |
| 464 | const content::MediaStreamDevice* |
| 465 | MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() { |
| 466 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 467 | const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); |
| 468 | if (video_devices.empty()) |
| 469 | return NULL; |
| 470 | return &(*video_devices.begin()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 473 | void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() { |
| 474 | is_device_enumeration_disabled_ = true; |
| 475 | } |
| 476 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 477 | scoped_refptr<MediaStreamCaptureIndicator> |
| 478 | MediaCaptureDevicesDispatcher::GetMediaStreamCaptureIndicator() { |
| 479 | return media_stream_capture_indicator_; |
| 480 | } |
| 481 | |
| 482 | scoped_refptr<AudioStreamIndicator> |
| 483 | MediaCaptureDevicesDispatcher::GetAudioStreamIndicator() { |
| 484 | return audio_stream_indicator_; |
| 485 | } |
| 486 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 487 | void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged( |
| 488 | const content::MediaStreamDevices& devices) { |
| 489 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 490 | BrowserThread::PostTask( |
| 491 | BrowserThread::UI, FROM_HERE, |
| 492 | base::Bind(&MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread, |
| 493 | base::Unretained(this), devices)); |
| 494 | } |
| 495 | |
| 496 | void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged( |
| 497 | const content::MediaStreamDevices& devices) { |
| 498 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 499 | BrowserThread::PostTask( |
| 500 | BrowserThread::UI, FROM_HERE, |
| 501 | base::Bind(&MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread, |
| 502 | base::Unretained(this), devices)); |
| 503 | } |
| 504 | |
| 505 | void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( |
| 506 | int render_process_id, |
| 507 | int render_view_id, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 508 | int page_request_id, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 509 | const content::MediaStreamDevice& device, |
| 510 | content::MediaRequestState state) { |
| 511 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 512 | BrowserThread::PostTask( |
| 513 | BrowserThread::UI, FROM_HERE, |
| 514 | base::Bind( |
| 515 | &MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 516 | base::Unretained(this), render_process_id, render_view_id, |
| 517 | page_request_id, device, state)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 518 | |
| 519 | } |
| 520 | |
| 521 | void MediaCaptureDevicesDispatcher::OnAudioStreamPlayingChanged( |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 522 | int render_process_id, int render_view_id, int stream_id, |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 523 | bool is_playing, float power_dbfs, bool clipped) { |
| 524 | audio_stream_indicator_->UpdateWebContentsStatus( |
| 525 | render_process_id, render_view_id, stream_id, |
| 526 | is_playing, power_dbfs, clipped); |
| 527 | } |
| 528 | |
| 529 | void MediaCaptureDevicesDispatcher::OnCreatingAudioStream( |
| 530 | int render_process_id, |
| 531 | int render_view_id) { |
| 532 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 533 | BrowserThread::PostTask( |
| 534 | BrowserThread::UI, FROM_HERE, |
| 535 | base::Bind( |
| 536 | &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, |
| 537 | base::Unretained(this), render_process_id, render_view_id)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread( |
| 541 | const content::MediaStreamDevices& devices) { |
| 542 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 543 | devices_enumerated_ = true; |
| 544 | audio_devices_ = devices; |
| 545 | FOR_EACH_OBSERVER(Observer, observers_, |
| 546 | OnUpdateAudioDevices(audio_devices_)); |
| 547 | } |
| 548 | |
| 549 | void MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread( |
| 550 | const content::MediaStreamDevices& devices){ |
| 551 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 552 | devices_enumerated_ = true; |
| 553 | video_devices_ = devices; |
| 554 | FOR_EACH_OBSERVER(Observer, observers_, |
| 555 | OnUpdateVideoDevices(video_devices_)); |
| 556 | } |
| 557 | |
| 558 | void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( |
| 559 | int render_process_id, |
| 560 | int render_view_id, |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 561 | int page_request_id, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 562 | const content::MediaStreamDevice& device, |
| 563 | content::MediaRequestState state) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 564 | // Cancel the request. |
| 565 | if (state == content::MEDIA_REQUEST_STATE_CLOSING) { |
| 566 | bool found = false; |
| 567 | for (RequestsQueues::iterator rqs_it = pending_requests_.begin(); |
| 568 | rqs_it != pending_requests_.end(); ++rqs_it) { |
| 569 | RequestsQueue& queue = rqs_it->second; |
| 570 | for (RequestsQueue::iterator it = queue.begin(); |
| 571 | it != queue.end(); ++it) { |
| 572 | if (it->request.render_process_id == render_process_id && |
| 573 | it->request.render_view_id == render_view_id && |
| 574 | it->request.page_request_id == page_request_id) { |
| 575 | queue.erase(it); |
| 576 | found = true; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | if (found) |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 585 | FOR_EACH_OBSERVER(Observer, observers_, |
| 586 | OnRequestUpdate(render_process_id, |
| 587 | render_view_id, |
| 588 | device, |
| 589 | state)); |
| 590 | } |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 591 | |
| 592 | void MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread( |
| 593 | int render_process_id, |
| 594 | int render_view_id) { |
| 595 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 596 | FOR_EACH_OBSERVER(Observer, observers_, |
| 597 | OnCreatingAudioStream(render_process_id, render_view_id)); |
| 598 | } |