OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | 5 #include "chrome/browser/media/media_capture_devices_dispatcher.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
11 #include "base/sha1.h" | 11 #include "base/sha1.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" | 15 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" |
16 #include "chrome/browser/media/desktop_streams_registry.h" | 16 #include "chrome/browser/media/desktop_streams_registry.h" |
17 #include "chrome/browser/media/media_stream_capture_indicator.h" | 17 #include "chrome/browser/media/media_stream_capture_indicator.h" |
18 #include "chrome/browser/media/media_stream_infobar_delegate.h" | 18 #include "chrome/browser/media/media_stream_infobar_delegate.h" |
19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/screen_capture_notification_ui.h" | 20 #include "chrome/browser/ui/screen_capture_notification_ui.h" |
21 #include "chrome/browser/ui/simple_message_box.h" | 21 #include "chrome/browser/ui/simple_message_box.h" |
22 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 22 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
23 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
24 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
25 #include "components/user_prefs/pref_registry_syncable.h" | 25 #include "components/user_prefs/pref_registry_syncable.h" |
26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/desktop_media_id.h" | 27 #include "content/public/browser/desktop_media_id.h" |
28 #include "content/public/browser/media_devices_monitor.h" | 28 #include "content/public/browser/media_capture_devices.h" |
29 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
30 #include "content/public/browser/notification_source.h" | 30 #include "content/public/browser/notification_source.h" |
31 #include "content/public/browser/notification_types.h" | 31 #include "content/public/browser/notification_types.h" |
32 #include "content/public/browser/render_frame_host.h" | 32 #include "content/public/browser/render_frame_host.h" |
33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
34 #include "content/public/common/media_stream_request.h" | 34 #include "content/public/common/media_stream_request.h" |
35 #include "extensions/common/constants.h" | 35 #include "extensions/common/constants.h" |
36 #include "extensions/common/extension.h" | 36 #include "extensions/common/extension.h" |
37 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
38 #include "media/audio/audio_manager_base.h" | 38 #include "media/audio/audio_manager_base.h" |
39 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h" | 39 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h" |
40 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
41 | 41 |
42 #if defined(OS_CHROMEOS) | 42 #if defined(OS_CHROMEOS) |
43 #include "ash/shell.h" | 43 #include "ash/shell.h" |
44 #endif // defined(OS_CHROMEOS) | 44 #endif // defined(OS_CHROMEOS) |
45 | 45 |
46 // Only do audio stream monitoring for platforms that use it for the tab media | 46 // Only do audio stream monitoring for platforms that use it for the tab media |
47 // indicator UI or the OOM killer. | 47 // indicator UI or the OOM killer. |
48 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 48 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
49 #define AUDIO_STREAM_MONITORING | 49 #define AUDIO_STREAM_MONITORING |
50 #include "chrome/browser/media/audio_stream_monitor.h" | 50 #include "chrome/browser/media/audio_stream_monitor.h" |
51 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) | 51 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
52 | 52 |
53 using content::BrowserThread; | 53 using content::BrowserThread; |
| 54 using content::MediaCaptureDevices; |
54 using content::MediaStreamDevices; | 55 using content::MediaStreamDevices; |
55 | 56 |
56 namespace { | 57 namespace { |
57 | 58 |
58 // Finds a device in |devices| that has |device_id|, or NULL if not found. | 59 // Finds a device in |devices| that has |device_id|, or NULL if not found. |
59 const content::MediaStreamDevice* FindDeviceWithId( | 60 const content::MediaStreamDevice* FindDeviceWithId( |
60 const content::MediaStreamDevices& devices, | 61 const content::MediaStreamDevices& devices, |
61 const std::string& device_id) { | 62 const std::string& device_id) { |
62 content::MediaStreamDevices::const_iterator iter = devices.begin(); | 63 content::MediaStreamDevices::const_iterator iter = devices.begin(); |
63 for (; iter != devices.end(); ++iter) { | 64 for (; iter != devices.end(); ++iter) { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 callback(callback) { | 241 callback(callback) { |
241 } | 242 } |
242 | 243 |
243 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {} | 244 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {} |
244 | 245 |
245 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() { | 246 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() { |
246 return Singleton<MediaCaptureDevicesDispatcher>::get(); | 247 return Singleton<MediaCaptureDevicesDispatcher>::get(); |
247 } | 248 } |
248 | 249 |
249 MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() | 250 MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() |
250 : devices_enumerated_(false), | 251 : is_device_enumeration_disabled_(false), |
251 is_device_enumeration_disabled_(false), | |
252 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()) { | 252 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()) { |
253 // MediaCaptureDevicesDispatcher is a singleton. It should be created on | 253 // MediaCaptureDevicesDispatcher is a singleton. It should be created on |
254 // UI thread. Otherwise, it will not receive | 254 // UI thread. Otherwise, it will not receive |
255 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in | 255 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in |
256 // possible use after free. | 256 // possible use after free. |
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
258 notifications_registrar_.Add( | 258 notifications_registrar_.Add( |
259 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 259 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
260 content::NotificationService::AllSources()); | 260 content::NotificationService::AllSources()); |
261 } | 261 } |
(...skipping 19 matching lines...) Expand all Loading... |
281 } | 281 } |
282 | 282 |
283 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { | 283 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { |
284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
285 observers_.RemoveObserver(observer); | 285 observers_.RemoveObserver(observer); |
286 } | 286 } |
287 | 287 |
288 const MediaStreamDevices& | 288 const MediaStreamDevices& |
289 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { | 289 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { |
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
291 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { | 291 if (is_device_enumeration_disabled_ || !test_audio_devices_.empty()) |
292 content::EnsureMonitorCaptureDevices(); | 292 return test_audio_devices_; |
293 devices_enumerated_ = true; | 293 |
294 } | 294 return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); |
295 return audio_devices_; | |
296 } | 295 } |
297 | 296 |
298 const MediaStreamDevices& | 297 const MediaStreamDevices& |
299 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { | 298 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { |
300 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
301 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { | 300 if (is_device_enumeration_disabled_ || !test_video_devices_.empty()) |
302 content::EnsureMonitorCaptureDevices(); | 301 return test_video_devices_; |
303 devices_enumerated_ = true; | 302 |
304 } | 303 return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); |
305 return video_devices_; | |
306 } | 304 } |
307 | 305 |
308 void MediaCaptureDevicesDispatcher::Observe( | 306 void MediaCaptureDevicesDispatcher::Observe( |
309 int type, | 307 int type, |
310 const content::NotificationSource& source, | 308 const content::NotificationSource& source, |
311 const content::NotificationDetails& details) { | 309 const content::NotificationDetails& details) { |
312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
313 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 311 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
314 content::WebContents* web_contents = | 312 content::WebContents* web_contents = |
315 content::Source<content::WebContents>(source).ptr(); | 313 content::Source<content::WebContents>(source).ptr(); |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 return media_stream_capture_indicator_; | 721 return media_stream_capture_indicator_; |
724 } | 722 } |
725 | 723 |
726 DesktopStreamsRegistry* | 724 DesktopStreamsRegistry* |
727 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() { | 725 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() { |
728 if (!desktop_streams_registry_) | 726 if (!desktop_streams_registry_) |
729 desktop_streams_registry_.reset(new DesktopStreamsRegistry()); | 727 desktop_streams_registry_.reset(new DesktopStreamsRegistry()); |
730 return desktop_streams_registry_.get(); | 728 return desktop_streams_registry_.get(); |
731 } | 729 } |
732 | 730 |
733 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged( | 731 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() { |
734 const content::MediaStreamDevices& devices) { | |
735 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 732 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
736 BrowserThread::PostTask( | 733 BrowserThread::PostTask( |
737 BrowserThread::UI, FROM_HERE, | 734 BrowserThread::UI, FROM_HERE, |
738 base::Bind(&MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread, | 735 base::Bind( |
739 base::Unretained(this), devices)); | 736 &MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread, |
| 737 base::Unretained(this))); |
740 } | 738 } |
741 | 739 |
742 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged( | 740 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() { |
743 const content::MediaStreamDevices& devices) { | |
744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 741 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
745 BrowserThread::PostTask( | 742 BrowserThread::PostTask( |
746 BrowserThread::UI, FROM_HERE, | 743 BrowserThread::UI, FROM_HERE, |
747 base::Bind(&MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread, | 744 base::Bind( |
748 base::Unretained(this), devices)); | 745 &MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread, |
| 746 base::Unretained(this))); |
749 } | 747 } |
750 | 748 |
751 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( | 749 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( |
752 int render_process_id, | 750 int render_process_id, |
753 int render_view_id, | 751 int render_view_id, |
754 int page_request_id, | 752 int page_request_id, |
755 const GURL& security_origin, | 753 const GURL& security_origin, |
756 const content::MediaStreamDevice& device, | 754 const content::MediaStreamDevice& device, |
757 content::MediaRequestState state) { | 755 content::MediaRequestState state) { |
758 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 int render_process_id, | 800 int render_process_id, |
803 int render_frame_id) { | 801 int render_frame_id) { |
804 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 802 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
805 BrowserThread::PostTask( | 803 BrowserThread::PostTask( |
806 BrowserThread::UI, FROM_HERE, | 804 BrowserThread::UI, FROM_HERE, |
807 base::Bind( | 805 base::Bind( |
808 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, | 806 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, |
809 base::Unretained(this), render_process_id, render_frame_id)); | 807 base::Unretained(this), render_process_id, render_frame_id)); |
810 } | 808 } |
811 | 809 |
812 void MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread( | 810 void MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() { |
813 const content::MediaStreamDevices& devices) { | 811 MediaStreamDevices devices = GetAudioCaptureDevices(); |
814 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
815 devices_enumerated_ = true; | |
816 audio_devices_ = devices; | |
817 FOR_EACH_OBSERVER(Observer, observers_, | 812 FOR_EACH_OBSERVER(Observer, observers_, |
818 OnUpdateAudioDevices(audio_devices_)); | 813 OnUpdateAudioDevices(devices)); |
819 } | 814 } |
820 | 815 |
821 void MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread( | 816 void MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() { |
822 const content::MediaStreamDevices& devices) { | 817 MediaStreamDevices devices = GetVideoCaptureDevices(); |
823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
824 devices_enumerated_ = true; | |
825 video_devices_ = devices; | |
826 FOR_EACH_OBSERVER(Observer, observers_, | 818 FOR_EACH_OBSERVER(Observer, observers_, |
827 OnUpdateVideoDevices(video_devices_)); | 819 OnUpdateVideoDevices(devices)); |
828 } | 820 } |
829 | 821 |
830 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( | 822 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( |
831 int render_process_id, | 823 int render_process_id, |
832 int render_view_id, | 824 int render_view_id, |
833 int page_request_id, | 825 int page_request_id, |
834 const GURL& security_origin, | 826 const GURL& security_origin, |
835 const content::MediaStreamDevice& device, | 827 const content::MediaStreamDevice& device, |
836 content::MediaRequestState state) { | 828 content::MediaRequestState state) { |
837 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced | 829 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 // info for content::WebContentsUserData). | 903 // info for content::WebContentsUserData). |
912 AudioStreamMonitor::CreateForWebContents(web_contents); | 904 AudioStreamMonitor::CreateForWebContents(web_contents); |
913 } | 905 } |
914 #endif | 906 #endif |
915 } | 907 } |
916 | 908 |
917 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() { | 909 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() { |
918 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
919 return desktop_capture_sessions_.size() > 0; | 911 return desktop_capture_sessions_.size() > 0; |
920 } | 912 } |
| 913 |
| 914 |
| 915 void MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices( |
| 916 const MediaStreamDevices& devices) { |
| 917 test_audio_devices_ = devices; |
| 918 } |
| 919 |
| 920 void MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices( |
| 921 const MediaStreamDevices& devices) { |
| 922 test_video_devices_ = devices; |
| 923 } |
OLD | NEW |