| 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/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 Loading... |
| 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 : media_stream_capture_indicator_(new MediaStreamCaptureIndicator()), |
| 198 is_device_enumeration_disabled_(false), | |
| 199 media_stream_capture_indicator_(new MediaStreamCaptureIndicator()), | |
| 200 audio_stream_indicator_(new AudioStreamIndicator()) { | 199 audio_stream_indicator_(new AudioStreamIndicator()) { |
| 201 // MediaCaptureDevicesDispatcher is a singleton. It should be created on | 200 // MediaCaptureDevicesDispatcher is a singleton. It should be created on |
| 202 // UI thread. Otherwise, it will not receive | 201 // UI thread. Otherwise, it will not receive |
| 203 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in | 202 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in |
| 204 // possible use after free. | 203 // possible use after free. |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 206 notifications_registrar_.Add( | 205 notifications_registrar_.Add( |
| 207 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 206 this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 208 content::NotificationService::AllSources()); | 207 content::NotificationService::AllSources()); |
| 209 } | 208 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 227 if (!observers_.HasObserver(observer)) | 226 if (!observers_.HasObserver(observer)) |
| 228 observers_.AddObserver(observer); | 227 observers_.AddObserver(observer); |
| 229 } | 228 } |
| 230 | 229 |
| 231 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { | 230 void MediaCaptureDevicesDispatcher::RemoveObserver(Observer* observer) { |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 233 observers_.RemoveObserver(observer); | 232 observers_.RemoveObserver(observer); |
| 234 } | 233 } |
| 235 | 234 |
| 236 const MediaStreamDevices& | |
| 237 MediaCaptureDevicesDispatcher::GetAudioCaptureDevices() { | |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 239 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { | |
| 240 content::EnsureMonitorCaptureDevices(); | |
| 241 devices_enumerated_ = true; | |
| 242 } | |
| 243 return audio_devices_; | |
| 244 } | |
| 245 | |
| 246 const MediaStreamDevices& | |
| 247 MediaCaptureDevicesDispatcher::GetVideoCaptureDevices() { | |
| 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 249 if (!is_device_enumeration_disabled_ && !devices_enumerated_) { | |
| 250 content::EnsureMonitorCaptureDevices(); | |
| 251 devices_enumerated_ = true; | |
| 252 } | |
| 253 return video_devices_; | |
| 254 } | |
| 255 | |
| 256 void MediaCaptureDevicesDispatcher::Observe( | 235 void MediaCaptureDevicesDispatcher::Observe( |
| 257 int type, | 236 int type, |
| 258 const content::NotificationSource& source, | 237 const content::NotificationSource& source, |
| 259 const content::NotificationDetails& details) { | 238 const content::NotificationDetails& details) { |
| 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 261 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | 240 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { |
| 262 content::WebContents* web_contents = | 241 content::WebContents* web_contents = |
| 263 content::Source<content::WebContents>(source).ptr(); | 242 content::Source<content::WebContents>(source).ptr(); |
| 264 pending_requests_.erase(web_contents); | 243 pending_requests_.erase(web_contents); |
| 265 } | 244 } |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 device = GetFirstAvailableVideoDevice(); | 594 device = GetFirstAvailableVideoDevice(); |
| 616 if (device) | 595 if (device) |
| 617 devices->push_back(*device); | 596 devices->push_back(*device); |
| 618 } | 597 } |
| 619 } | 598 } |
| 620 | 599 |
| 621 const content::MediaStreamDevice* | 600 const content::MediaStreamDevice* |
| 622 MediaCaptureDevicesDispatcher::GetRequestedAudioDevice( | 601 MediaCaptureDevicesDispatcher::GetRequestedAudioDevice( |
| 623 const std::string& requested_audio_device_id) { | 602 const std::string& requested_audio_device_id) { |
| 624 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 603 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 625 const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); | 604 const content::MediaStreamDevices& audio_devices = |
| 605 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); |
| 626 const content::MediaStreamDevice* const device = | 606 const content::MediaStreamDevice* const device = |
| 627 FindDeviceWithId(audio_devices, requested_audio_device_id); | 607 FindDeviceWithId(audio_devices, requested_audio_device_id); |
| 628 return device; | 608 return device; |
| 629 } | 609 } |
| 630 | 610 |
| 631 const content::MediaStreamDevice* | 611 const content::MediaStreamDevice* |
| 632 MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() { | 612 MediaCaptureDevicesDispatcher::GetFirstAvailableAudioDevice() { |
| 633 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 613 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 634 const content::MediaStreamDevices& audio_devices = GetAudioCaptureDevices(); | 614 const content::MediaStreamDevices& audio_devices = |
| 615 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); |
| 635 if (audio_devices.empty()) | 616 if (audio_devices.empty()) |
| 636 return NULL; | 617 return NULL; |
| 637 return &(*audio_devices.begin()); | 618 return &(*audio_devices.begin()); |
| 638 } | 619 } |
| 639 | 620 |
| 640 const content::MediaStreamDevice* | 621 const content::MediaStreamDevice* |
| 641 MediaCaptureDevicesDispatcher::GetRequestedVideoDevice( | 622 MediaCaptureDevicesDispatcher::GetRequestedVideoDevice( |
| 642 const std::string& requested_video_device_id) { | 623 const std::string& requested_video_device_id) { |
| 643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 624 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 644 const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); | 625 const content::MediaStreamDevices& video_devices = |
| 626 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); |
| 645 const content::MediaStreamDevice* const device = | 627 const content::MediaStreamDevice* const device = |
| 646 FindDeviceWithId(video_devices, requested_video_device_id); | 628 FindDeviceWithId(video_devices, requested_video_device_id); |
| 647 return device; | 629 return device; |
| 648 } | 630 } |
| 649 | 631 |
| 650 const content::MediaStreamDevice* | 632 const content::MediaStreamDevice* |
| 651 MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() { | 633 MediaCaptureDevicesDispatcher::GetFirstAvailableVideoDevice() { |
| 652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 653 const content::MediaStreamDevices& video_devices = GetVideoCaptureDevices(); | 635 const content::MediaStreamDevices& video_devices = |
| 636 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); |
| 654 if (video_devices.empty()) | 637 if (video_devices.empty()) |
| 655 return NULL; | 638 return NULL; |
| 656 return &(*video_devices.begin()); | 639 return &(*video_devices.begin()); |
| 657 } | 640 } |
| 658 | 641 |
| 659 void MediaCaptureDevicesDispatcher::DisableDeviceEnumerationForTesting() { | |
| 660 is_device_enumeration_disabled_ = true; | |
| 661 } | |
| 662 | |
| 663 scoped_refptr<MediaStreamCaptureIndicator> | 642 scoped_refptr<MediaStreamCaptureIndicator> |
| 664 MediaCaptureDevicesDispatcher::GetMediaStreamCaptureIndicator() { | 643 MediaCaptureDevicesDispatcher::GetMediaStreamCaptureIndicator() { |
| 665 return media_stream_capture_indicator_; | 644 return media_stream_capture_indicator_; |
| 666 } | 645 } |
| 667 | 646 |
| 668 scoped_refptr<AudioStreamIndicator> | 647 scoped_refptr<AudioStreamIndicator> |
| 669 MediaCaptureDevicesDispatcher::GetAudioStreamIndicator() { | 648 MediaCaptureDevicesDispatcher::GetAudioStreamIndicator() { |
| 670 return audio_stream_indicator_; | 649 return audio_stream_indicator_; |
| 671 } | 650 } |
| 672 | 651 |
| 673 DesktopStreamsRegistry* | 652 DesktopStreamsRegistry* |
| 674 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() { | 653 MediaCaptureDevicesDispatcher::GetDesktopStreamsRegistry() { |
| 675 if (!desktop_streams_registry_) | 654 if (!desktop_streams_registry_) |
| 676 desktop_streams_registry_.reset(new DesktopStreamsRegistry()); | 655 desktop_streams_registry_.reset(new DesktopStreamsRegistry()); |
| 677 return desktop_streams_registry_.get(); | 656 return desktop_streams_registry_.get(); |
| 678 } | 657 } |
| 679 | 658 |
| 680 void MediaCaptureDevicesDispatcher::OnAudioCaptureDevicesChanged( | |
| 681 const content::MediaStreamDevices& devices) { | |
| 682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 683 BrowserThread::PostTask( | |
| 684 BrowserThread::UI, FROM_HERE, | |
| 685 base::Bind(&MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread, | |
| 686 base::Unretained(this), devices)); | |
| 687 } | |
| 688 | |
| 689 void MediaCaptureDevicesDispatcher::OnVideoCaptureDevicesChanged( | |
| 690 const content::MediaStreamDevices& devices) { | |
| 691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 692 BrowserThread::PostTask( | |
| 693 BrowserThread::UI, FROM_HERE, | |
| 694 base::Bind(&MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread, | |
| 695 base::Unretained(this), devices)); | |
| 696 } | |
| 697 | |
| 698 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( | 659 void MediaCaptureDevicesDispatcher::OnMediaRequestStateChanged( |
| 699 int render_process_id, | 660 int render_process_id, |
| 700 int render_view_id, | 661 int render_view_id, |
| 701 int page_request_id, | 662 int page_request_id, |
| 702 const GURL& security_origin, | 663 const GURL& security_origin, |
| 703 const content::MediaStreamDevice& device, | 664 const content::MediaStreamDevice& device, |
| 704 content::MediaRequestState state) { | 665 content::MediaRequestState state) { |
| 705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 666 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 706 BrowserThread::PostTask( | 667 BrowserThread::PostTask( |
| 707 BrowserThread::UI, FROM_HERE, | 668 BrowserThread::UI, FROM_HERE, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 723 int render_process_id, | 684 int render_process_id, |
| 724 int render_frame_id) { | 685 int render_frame_id) { |
| 725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 686 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 726 BrowserThread::PostTask( | 687 BrowserThread::PostTask( |
| 727 BrowserThread::UI, FROM_HERE, | 688 BrowserThread::UI, FROM_HERE, |
| 728 base::Bind( | 689 base::Bind( |
| 729 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, | 690 &MediaCaptureDevicesDispatcher::OnCreatingAudioStreamOnUIThread, |
| 730 base::Unretained(this), render_process_id, render_frame_id)); | 691 base::Unretained(this), render_process_id, render_frame_id)); |
| 731 } | 692 } |
| 732 | 693 |
| 733 void MediaCaptureDevicesDispatcher::UpdateAudioDevicesOnUIThread( | |
| 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_, | |
| 739 OnUpdateAudioDevices(audio_devices_)); | |
| 740 } | |
| 741 | |
| 742 void MediaCaptureDevicesDispatcher::UpdateVideoDevicesOnUIThread( | |
| 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_, | |
| 748 OnUpdateVideoDevices(video_devices_)); | |
| 749 } | |
| 750 | |
| 751 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( | 694 void MediaCaptureDevicesDispatcher::UpdateMediaRequestStateOnUIThread( |
| 752 int render_process_id, | 695 int render_process_id, |
| 753 int render_view_id, | 696 int render_view_id, |
| 754 int page_request_id, | 697 int page_request_id, |
| 755 const GURL& security_origin, | 698 const GURL& security_origin, |
| 756 const content::MediaStreamDevice& device, | 699 const content::MediaStreamDevice& device, |
| 757 content::MediaRequestState state) { | 700 content::MediaRequestState state) { |
| 758 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced | 701 // Track desktop capture sessions. Tracking is necessary to avoid unbalanced |
| 759 // session counts since not all requests will reach MEDIA_REQUEST_STATE_DONE, | 702 // session counts since not all requests will reach MEDIA_REQUEST_STATE_DONE, |
| 760 // but they will all reach MEDIA_REQUEST_STATE_CLOSING. | 703 // but they will all reach MEDIA_REQUEST_STATE_CLOSING. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 int render_frame_id) { | 765 int render_frame_id) { |
| 823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 766 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 824 FOR_EACH_OBSERVER(Observer, observers_, | 767 FOR_EACH_OBSERVER(Observer, observers_, |
| 825 OnCreatingAudioStream(render_process_id, render_frame_id)); | 768 OnCreatingAudioStream(render_process_id, render_frame_id)); |
| 826 } | 769 } |
| 827 | 770 |
| 828 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() { | 771 bool MediaCaptureDevicesDispatcher::IsDesktopCaptureInProgress() { |
| 829 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 772 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 830 return desktop_capture_sessions_.size() > 0; | 773 return desktop_capture_sessions_.size() > 0; |
| 831 } | 774 } |
| OLD | NEW |