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

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

Issue 183743021: Implement MediaCaptureDevices. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/audio_stream_indicator.h" 16 #include "chrome/browser/media/audio_stream_indicator.h"
17 #include "chrome/browser/media/desktop_streams_registry.h" 17 #include "chrome/browser/media/desktop_streams_registry.h"
18 #include "chrome/browser/media/media_stream_capture_indicator.h" 18 #include "chrome/browser/media/media_stream_capture_indicator.h"
19 #include "chrome/browser/media/media_stream_infobar_delegate.h" 19 #include "chrome/browser/media/media_stream_infobar_delegate.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ui/screen_capture_notification_ui.h" 21 #include "chrome/browser/ui/screen_capture_notification_ui.h"
22 #include "chrome/browser/ui/simple_message_box.h" 22 #include "chrome/browser/ui/simple_message_box.h"
23 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" 23 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
24 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h" 25 #include "chrome/common/pref_names.h"
26 #include "components/user_prefs/pref_registry_syncable.h" 26 #include "components/user_prefs/pref_registry_syncable.h"
27 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/desktop_media_id.h" 28 #include "content/public/browser/desktop_media_id.h"
29 #include "content/public/browser/media_devices_monitor.h" 29 #include "content/public/browser/media_capture_devices.h"
30 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_source.h" 31 #include "content/public/browser/notification_source.h"
32 #include "content/public/browser/notification_types.h" 32 #include "content/public/browser/notification_types.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 "ui/base/l10n/l10n_util.h" 39 #include "ui/base/l10n/l10n_util.h"
40 40
41 #if defined(OS_CHROMEOS) 41 #if defined(OS_CHROMEOS)
42 #include "ash/shell.h" 42 #include "ash/shell.h"
43 #endif // defined(OS_CHROMEOS) 43 #endif // defined(OS_CHROMEOS)
44 44
45 using content::BrowserThread; 45 using content::BrowserThread;
46 using content::MediaCaptureDevices;
46 using content::MediaStreamDevices; 47 using content::MediaStreamDevices;
47 48
48 namespace { 49 namespace {
49 50
50 // Finds a device in |devices| that has |device_id|, or NULL if not found. 51 // Finds a device in |devices| that has |device_id|, or NULL if not found.
51 const content::MediaStreamDevice* FindDeviceWithId( 52 const content::MediaStreamDevice* FindDeviceWithId(
52 const content::MediaStreamDevices& devices, 53 const content::MediaStreamDevices& devices,
53 const std::string& device_id) { 54 const std::string& device_id) {
54 content::MediaStreamDevices::const_iterator iter = devices.begin(); 55 content::MediaStreamDevices::const_iterator iter = devices.begin();
55 for (; iter != devices.end(); ++iter) { 56 for (; iter != devices.end(); ++iter) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 callback(callback) { 188 callback(callback) {
188 } 189 }
189 190
190 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {} 191 MediaCaptureDevicesDispatcher::PendingAccessRequest::~PendingAccessRequest() {}
191 192
192 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() { 193 MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
193 return Singleton<MediaCaptureDevicesDispatcher>::get(); 194 return Singleton<MediaCaptureDevicesDispatcher>::get();
194 } 195 }
195 196
196 MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() 197 MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher()
197 : devices_enumerated_(false), 198 : is_device_enumeration_disabled_(false),
198 is_device_enumeration_disabled_(false),
199 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()), 199 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()),
200 audio_stream_indicator_(new AudioStreamIndicator()) { 200 audio_stream_indicator_(new AudioStreamIndicator()) {
201 // MediaCaptureDevicesDispatcher is a singleton. It should be created on 201 // MediaCaptureDevicesDispatcher is a singleton. It should be created on
202 // UI thread. Otherwise, it will not receive 202 // UI thread. Otherwise, it will not receive
203 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in 203 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in
204 // possible use after free. 204 // possible use after free.
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
206 notifications_registrar_.Add( 206 notifications_registrar_.Add(
207 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 207 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
208 content::NotificationService::AllSources()); 208 content::NotificationService::AllSources());
(...skipping 20 matching lines...) Expand all
229 } 229 }
230 230
231 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { 231 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
233 observers_.RemoveObserver(observer); 233 observers_.RemoveObserver(observer);
234 } 234 }
235 235
236 const MediaStreamDevices& 236 const MediaStreamDevices&
237 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { 237 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() {
238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
239 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { 239 if (is_device_enumeration_disabled_)
240 content::EnsureMonitorCaptureDevices(); 240 return test_audio_devices_;
241 devices_enumerated_ = true; 241
242 } 242 return MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices();
243 return audio_devices_;
244 } 243 }
245 244
246 const MediaStreamDevices& 245 const MediaStreamDevices&
247 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { 246 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { 248 if (is_device_enumeration_disabled_)
250 content::EnsureMonitorCaptureDevices(); 249 return test_video_devices_;
251 devices_enumerated_ = true; 250
252 } 251 return MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices();
253 return video_devices_;
254 } 252 }
255 253
256 void MediaCaptureDevicesDispatcher::Observe( 254 void MediaCaptureDevicesDispatcher::Observe(
257 int type, 255 int type,
258 const content::NotificationSource& source, 256 const content::NotificationSource& source,
259 const content::NotificationDetails& details) { 257 const content::NotificationDetails& details) {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
261 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { 259 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
262 content::WebContents* web_contents = 260 content::WebContents* web_contents =
263 content::Source<content::WebContents>(source).ptr(); 261 content::Source<content::WebContents>(source).ptr();
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 return audio_stream_indicator_; 668 return audio_stream_indicator_;
671 } 669 }
672 670
673 DesktopStreamsRegistry* 671 DesktopStreamsRegistry*
674 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() { 672 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() {
675 if (!desktop_streams_registry_) 673 if (!desktop_streams_registry_)
676 desktop_streams_registry_.reset(new DesktopStreamsRegistry()); 674 desktop_streams_registry_.reset(new DesktopStreamsRegistry());
677 return desktop_streams_registry_.get(); 675 return desktop_streams_registry_.get();
678 } 676 }
679 677
680 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged( 678 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged() {
681 const content::MediaStreamDevices& devices) {
682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 679 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
683 BrowserThread::PostTask( 680 BrowserThread::PostTask(
684 BrowserThread::UI, FROM_HERE, 681 BrowserThread::UI, FROM_HERE,
685 base::Bind(&MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread, 682 base::Bind(
686 base::Unretained(this), devices)); 683 &MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread,
684 base::Unretained(this)));
687 } 685 }
688 686
689 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged( 687 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged() {
690 const content::MediaStreamDevices& devices) {
691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 688 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
692 BrowserThread::PostTask( 689 BrowserThread::PostTask(
693 BrowserThread::UI, FROM_HERE, 690 BrowserThread::UI, FROM_HERE,
694 base::Bind(&MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread, 691 base::Bind(
695 base::Unretained(this), devices)); 692 &MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread,
693 base::Unretained(this)));
696 } 694 }
697 695
698 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( 696 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged(
699 int render_process_id, 697 int render_process_id,
700 int render_view_id, 698 int render_view_id,
701 int page_request_id, 699 int page_request_id,
702 const GURL& security_origin, 700 const GURL& security_origin,
703 const content::MediaStreamDevice& device, 701 const content::MediaStreamDevice& device,
704 content::MediaRequestState state) { 702 content::MediaRequestState state) {
705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
(...skipping 17 matching lines...) Expand all
723 int render_process_id, 721 int render_process_id,
724 int render_frame_id) { 722 int render_frame_id) {
725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 723 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
726 BrowserThread::PostTask( 724 BrowserThread::PostTask(
727 BrowserThread::UI, FROM_HERE, 725 BrowserThread::UI, FROM_HERE,
728 base::Bind( 726 base::Bind(
729 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, 727 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread,
730 base::Unretained(this), render_process_id, render_frame_id)); 728 base::Unretained(this), render_process_id, render_frame_id));
731 } 729 }
732 730
733 void MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread( 731 void MediaCaptureDevicesDispatcher::NotifyAudioDevicesChangedOnUIThread() {
734 const content::MediaStreamDevices& devices) {
735 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
736 devices_enumerated_ = true;
737 audio_devices_ = devices;
738 FOR_EACH_OBSERVER(Observer, observers_, 732 FOR_EACH_OBSERVER(Observer, observers_,
739 OnUpdateAudioDevices(audio_devices_)); 733 OnUpdateAudioDevices(GetAudioCaptureDevices()));
jiayl 2014/03/05 22:48:27 The result can be cached to avoid calling it once
michaelbai 2014/03/06 05:24:52 Done.
740 } 734 }
741 735
742 void MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread( 736 void MediaCaptureDevicesDispatcher::NotifyVideoDevicesChangedOnUIThread() {
743 const content::MediaStreamDevices& devices) {
744 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
745 devices_enumerated_ = true;
746 video_devices_ = devices;
747 FOR_EACH_OBSERVER(Observer, observers_, 737 FOR_EACH_OBSERVER(Observer, observers_,
748 OnUpdateVideoDevices(video_devices_)); 738 OnUpdateVideoDevices(GetVideoCaptureDevices()));
jiayl 2014/03/05 22:48:27 dito.
michaelbai 2014/03/06 05:24:52 Done.
749 } 739 }
750 740
751 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( 741 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread(
752 int render_process_id, 742 int render_process_id,
753 int render_view_id, 743 int render_view_id,
754 int page_request_id, 744 int page_request_id,
755 const GURL& security_origin, 745 const GURL& security_origin,
756 const content::MediaStreamDevice& device, 746 const content::MediaStreamDevice& device,
757 content::MediaRequestState state) { 747 content::MediaRequestState state) {
758 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced 748 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 int render_frame_id) { 812 int render_frame_id) {
823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 813 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
824 FOR_EACH_OBSERVER(Observer, observers_, 814 FOR_EACH_OBSERVER(Observer, observers_,
825 OnCreatingAudioStream(render_process_id, render_frame_id)); 815 OnCreatingAudioStream(render_process_id, render_frame_id));
826 } 816 }
827 817
828 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() { 818 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() {
829 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 819 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
830 return desktop_capture_sessions_.size() > 0; 820 return desktop_capture_sessions_.size() > 0;
831 } 821 }
822
823
824 void MediaCaptureDevicesDispatcher::SetTestAudioCaptureDevices(
825 const MediaStreamDevices& devices) {
826 test_audio_devices_ = devices;
827 }
828
829 void MediaCaptureDevicesDispatcher::SetTestVideoCaptureDevices(
830 const MediaStreamDevices& devices) {
831 test_video_devices_ = devices;
832 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698