| OLD | NEW |
| (Empty) |
| 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 #ifndef CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include <list> | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 | |
| 13 #include "base/callback.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/scoped_vector.h" | |
| 16 #include "base/memory/singleton.h" | |
| 17 #include "base/observer_list.h" | |
| 18 #include "content/public/browser/media_observer.h" | |
| 19 #include "content/public/browser/web_contents_delegate.h" | |
| 20 #include "content/public/common/media_stream_request.h" | |
| 21 | |
| 22 class DesktopStreamsRegistry; | |
| 23 class MediaAccessHandler; | |
| 24 class MediaStreamCaptureIndicator; | |
| 25 class Profile; | |
| 26 | |
| 27 namespace extensions { | |
| 28 class Extension; | |
| 29 } | |
| 30 | |
| 31 namespace user_prefs { | |
| 32 class PrefRegistrySyncable; | |
| 33 } | |
| 34 | |
| 35 // This singleton is used to receive updates about media events from the content | |
| 36 // layer. | |
| 37 class MediaCaptureDevicesDispatcher : public content::MediaObserver { | |
| 38 public: | |
| 39 class Observer { | |
| 40 public: | |
| 41 // Handle an information update consisting of a up-to-date audio capture | |
| 42 // device lists. This happens when a microphone is plugged in or unplugged. | |
| 43 virtual void OnUpdateAudioDevices( | |
| 44 const content::MediaStreamDevices& devices) {} | |
| 45 | |
| 46 // Handle an information update consisting of a up-to-date video capture | |
| 47 // device lists. This happens when a camera is plugged in or unplugged. | |
| 48 virtual void OnUpdateVideoDevices( | |
| 49 const content::MediaStreamDevices& devices) {} | |
| 50 | |
| 51 // Handle an information update related to a media stream request. | |
| 52 virtual void OnRequestUpdate( | |
| 53 int render_process_id, | |
| 54 int render_frame_id, | |
| 55 content::MediaStreamType stream_type, | |
| 56 const content::MediaRequestState state) {} | |
| 57 | |
| 58 // Handle an information update that a new stream is being created. | |
| 59 virtual void OnCreatingAudioStream(int render_process_id, | |
| 60 int render_frame_id) {} | |
| 61 | |
| 62 virtual ~Observer() {} | |
| 63 }; | |
| 64 | |
| 65 static MediaCaptureDevicesDispatcher* GetInstance(); | |
| 66 | |
| 67 // Registers the preferences related to Media Stream default devices. | |
| 68 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 69 | |
| 70 // Returns true if the security origin is associated with casting. | |
| 71 static bool IsOriginForCasting(const GURL& origin); | |
| 72 | |
| 73 // Methods for observers. Called on UI thread. | |
| 74 // Observers should add themselves on construction and remove themselves | |
| 75 // on destruction. | |
| 76 void AddObserver(Observer* observer); | |
| 77 void RemoveObserver(Observer* observer); | |
| 78 const content::MediaStreamDevices& GetAudioCaptureDevices(); | |
| 79 const content::MediaStreamDevices& GetVideoCaptureDevices(); | |
| 80 | |
| 81 // Method called from WebCapturerDelegate implementations to process access | |
| 82 // requests. |extension| is set to NULL if request was made from a drive-by | |
| 83 // page. | |
| 84 void ProcessMediaAccessRequest( | |
| 85 content::WebContents* web_contents, | |
| 86 const content::MediaStreamRequest& request, | |
| 87 const content::MediaResponseCallback& callback, | |
| 88 const extensions::Extension* extension); | |
| 89 | |
| 90 // Method called from WebCapturerDelegate implementations to check media | |
| 91 // access permission. Note that this does not query the user. | |
| 92 bool CheckMediaAccessPermission(content::WebContents* web_contents, | |
| 93 const GURL& security_origin, | |
| 94 content::MediaStreamType type); | |
| 95 | |
| 96 // Same as above but for an |extension|, which may not be NULL. | |
| 97 bool CheckMediaAccessPermission(content::WebContents* web_contents, | |
| 98 const GURL& security_origin, | |
| 99 content::MediaStreamType type, | |
| 100 const extensions::Extension* extension); | |
| 101 | |
| 102 // Helper to get the default devices which can be used by the media request. | |
| 103 // Uses the first available devices if the default devices are not available. | |
| 104 // If the return list is empty, it means there is no available device on the | |
| 105 // OS. | |
| 106 // Called on the UI thread. | |
| 107 void GetDefaultDevicesForProfile(Profile* profile, | |
| 108 bool audio, | |
| 109 bool video, | |
| 110 content::MediaStreamDevices* devices); | |
| 111 | |
| 112 // Helpers for picking particular requested devices, identified by raw id. | |
| 113 // If the device requested is not available it will return NULL. | |
| 114 const content::MediaStreamDevice* | |
| 115 GetRequestedAudioDevice(const std::string& requested_audio_device_id); | |
| 116 const content::MediaStreamDevice* | |
| 117 GetRequestedVideoDevice(const std::string& requested_video_device_id); | |
| 118 | |
| 119 // Returns the first available audio or video device, or NULL if no devices | |
| 120 // are available. | |
| 121 const content::MediaStreamDevice* GetFirstAvailableAudioDevice(); | |
| 122 const content::MediaStreamDevice* GetFirstAvailableVideoDevice(); | |
| 123 | |
| 124 // Unittests that do not require actual device enumeration should call this | |
| 125 // API on the singleton. It is safe to call this multiple times on the | |
| 126 // signleton. | |
| 127 void DisableDeviceEnumerationForTesting(); | |
| 128 | |
| 129 // Overridden from content::MediaObserver: | |
| 130 void OnAudioCaptureDevicesChanged() override; | |
| 131 void OnVideoCaptureDevicesChanged() override; | |
| 132 void OnMediaRequestStateChanged(int render_process_id, | |
| 133 int render_frame_id, | |
| 134 int page_request_id, | |
| 135 const GURL& security_origin, | |
| 136 content::MediaStreamType stream_type, | |
| 137 content::MediaRequestState state) override; | |
| 138 void OnCreatingAudioStream(int render_process_id, | |
| 139 int render_frame_id) override; | |
| 140 void OnSetCapturingLinkSecured(int render_process_id, | |
| 141 int render_frame_id, | |
| 142 int page_request_id, | |
| 143 content::MediaStreamType stream_type, | |
| 144 bool is_secure) override; | |
| 145 | |
| 146 scoped_refptr<MediaStreamCaptureIndicator> GetMediaStreamCaptureIndicator(); | |
| 147 | |
| 148 DesktopStreamsRegistry* GetDesktopStreamsRegistry(); | |
| 149 | |
| 150 // Return true if there is any ongoing insecured capturing. The capturing is | |
| 151 // deemed secure if all connected video sinks are reported secure and the | |
| 152 // extension is trusted. | |
| 153 bool IsInsecureCapturingInProgress(int render_process_id, | |
| 154 int render_frame_id); | |
| 155 | |
| 156 // Only for testing. | |
| 157 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices); | |
| 158 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices); | |
| 159 | |
| 160 private: | |
| 161 friend struct base::DefaultSingletonTraits<MediaCaptureDevicesDispatcher>; | |
| 162 | |
| 163 MediaCaptureDevicesDispatcher(); | |
| 164 ~MediaCaptureDevicesDispatcher() override; | |
| 165 | |
| 166 // Called by the MediaObserver() functions, executed on UI thread. | |
| 167 void NotifyAudioDevicesChangedOnUIThread(); | |
| 168 void NotifyVideoDevicesChangedOnUIThread(); | |
| 169 void UpdateMediaRequestStateOnUIThread( | |
| 170 int render_process_id, | |
| 171 int render_frame_id, | |
| 172 int page_request_id, | |
| 173 const GURL& security_origin, | |
| 174 content::MediaStreamType stream_type, | |
| 175 content::MediaRequestState state); | |
| 176 void OnCreatingAudioStreamOnUIThread(int render_process_id, | |
| 177 int render_frame_id); | |
| 178 void UpdateCapturingLinkSecured(int render_process_id, | |
| 179 int render_frame_id, | |
| 180 int page_request_id, | |
| 181 content::MediaStreamType stream_type, | |
| 182 bool is_secure); | |
| 183 | |
| 184 // Only for testing, a list of cached audio capture devices. | |
| 185 content::MediaStreamDevices test_audio_devices_; | |
| 186 | |
| 187 // Only for testing, a list of cached video capture devices. | |
| 188 content::MediaStreamDevices test_video_devices_; | |
| 189 | |
| 190 // A list of observers for the device update notifications. | |
| 191 base::ObserverList<Observer> observers_; | |
| 192 | |
| 193 // Flag used by unittests to disable device enumeration. | |
| 194 bool is_device_enumeration_disabled_; | |
| 195 | |
| 196 scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_; | |
| 197 | |
| 198 std::unique_ptr<DesktopStreamsRegistry> desktop_streams_registry_; | |
| 199 | |
| 200 // Handlers for processing media access requests. | |
| 201 ScopedVector<MediaAccessHandler> media_access_handlers_; | |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher); | |
| 204 }; | |
| 205 | |
| 206 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | |
| OLD | NEW |