| 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 #ifndef CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | 5 #ifndef CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ |
| 6 #define CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | 6 #define CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 14 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 16 #include "content/public/browser/media_observer.h" | 17 #include "content/public/browser/media_observer.h" |
| 17 #include "content/public/browser/notification_observer.h" | |
| 18 #include "content/public/browser/notification_registrar.h" | |
| 19 #include "content/public/browser/web_contents_delegate.h" | 18 #include "content/public/browser/web_contents_delegate.h" |
| 20 #include "content/public/common/media_stream_request.h" | 19 #include "content/public/common/media_stream_request.h" |
| 21 | 20 |
| 22 class DesktopStreamsRegistry; | 21 class DesktopStreamsRegistry; |
| 22 class MediaAccessHandler; |
| 23 class MediaStreamCaptureIndicator; | 23 class MediaStreamCaptureIndicator; |
| 24 class Profile; | 24 class Profile; |
| 25 | 25 |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 class Extension; | 27 class Extension; |
| 28 } | 28 } |
| 29 | 29 |
| 30 namespace user_prefs { | 30 namespace user_prefs { |
| 31 class PrefRegistrySyncable; | 31 class PrefRegistrySyncable; |
| 32 } | 32 } |
| 33 | 33 |
| 34 // This singleton is used to receive updates about media events from the content | 34 // This singleton is used to receive updates about media events from the content |
| 35 // layer. | 35 // layer. |
| 36 class MediaCaptureDevicesDispatcher : public content::MediaObserver, | 36 class MediaCaptureDevicesDispatcher : public content::MediaObserver { |
| 37 public content::NotificationObserver { | |
| 38 public: | 37 public: |
| 39 class Observer { | 38 class Observer { |
| 40 public: | 39 public: |
| 41 // Handle an information update consisting of a up-to-date audio capture | 40 // 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. | 41 // device lists. This happens when a microphone is plugged in or unplugged. |
| 43 virtual void OnUpdateAudioDevices( | 42 virtual void OnUpdateAudioDevices( |
| 44 const content::MediaStreamDevices& devices) {} | 43 const content::MediaStreamDevices& devices) {} |
| 45 | 44 |
| 46 // Handle an information update consisting of a up-to-date video capture | 45 // 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. | 46 // device lists. This happens when a camera is plugged in or unplugged. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 60 int render_frame_id) {} | 59 int render_frame_id) {} |
| 61 | 60 |
| 62 virtual ~Observer() {} | 61 virtual ~Observer() {} |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 static MediaCaptureDevicesDispatcher* GetInstance(); | 64 static MediaCaptureDevicesDispatcher* GetInstance(); |
| 66 | 65 |
| 67 // Registers the preferences related to Media Stream default devices. | 66 // Registers the preferences related to Media Stream default devices. |
| 68 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 67 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 69 | 68 |
| 69 // Returns true if the security origin is associated with casting. |
| 70 static bool IsOriginForCasting(const GURL& origin); |
| 71 |
| 70 // Methods for observers. Called on UI thread. | 72 // Methods for observers. Called on UI thread. |
| 71 // Observers should add themselves on construction and remove themselves | 73 // Observers should add themselves on construction and remove themselves |
| 72 // on destruction. | 74 // on destruction. |
| 73 void AddObserver(Observer* observer); | 75 void AddObserver(Observer* observer); |
| 74 void RemoveObserver(Observer* observer); | 76 void RemoveObserver(Observer* observer); |
| 75 const content::MediaStreamDevices& GetAudioCaptureDevices(); | 77 const content::MediaStreamDevices& GetAudioCaptureDevices(); |
| 76 const content::MediaStreamDevices& GetVideoCaptureDevices(); | 78 const content::MediaStreamDevices& GetVideoCaptureDevices(); |
| 77 | 79 |
| 78 // Method called from WebCapturerDelegate implementations to process access | 80 // Method called from WebCapturerDelegate implementations to process access |
| 79 // requests. |extension| is set to NULL if request was made from a drive-by | 81 // requests. |extension| is set to NULL if request was made from a drive-by |
| 80 // page. | 82 // page. |
| 81 void ProcessMediaAccessRequest( | 83 void ProcessMediaAccessRequest( |
| 82 content::WebContents* web_contents, | 84 content::WebContents* web_contents, |
| 83 const content::MediaStreamRequest& request, | 85 const content::MediaStreamRequest& request, |
| 84 const content::MediaResponseCallback& callback, | 86 const content::MediaResponseCallback& callback, |
| 85 const extensions::Extension* extension); | 87 const extensions::Extension* extension); |
| 86 | 88 |
| 87 // Method called from WebCapturerDelegate implementations to check media | 89 // Method called from WebCapturerDelegate implementations to check media |
| 88 // access permission. Note that this does not query the user. | 90 // access permission. Note that this does not query the user. |
| 89 bool CheckMediaAccessPermission(content::WebContents* web_contents, | 91 bool CheckMediaAccessPermission(content::WebContents* web_contents, |
| 90 const GURL& security_origin, | 92 const GURL& security_origin, |
| 91 content::MediaStreamType type); | 93 content::MediaStreamType type); |
| 92 | 94 |
| 93 // Same as above but for an |extension|, which may not be NULL. | 95 // Same as above but for an |extension|, which may not be NULL. |
| 94 #if defined(ENABLE_EXTENSIONS) | |
| 95 bool CheckMediaAccessPermission(content::WebContents* web_contents, | 96 bool CheckMediaAccessPermission(content::WebContents* web_contents, |
| 96 const GURL& security_origin, | 97 const GURL& security_origin, |
| 97 content::MediaStreamType type, | 98 content::MediaStreamType type, |
| 98 const extensions::Extension* extension); | 99 const extensions::Extension* extension); |
| 99 #endif | |
| 100 | 100 |
| 101 // Helper to get the default devices which can be used by the media request. | 101 // Helper to get the default devices which can be used by the media request. |
| 102 // Uses the first available devices if the default devices are not available. | 102 // Uses the first available devices if the default devices are not available. |
| 103 // If the return list is empty, it means there is no available device on the | 103 // If the return list is empty, it means there is no available device on the |
| 104 // OS. | 104 // OS. |
| 105 // Called on the UI thread. | 105 // Called on the UI thread. |
| 106 void GetDefaultDevicesForProfile(Profile* profile, | 106 void GetDefaultDevicesForProfile(Profile* profile, |
| 107 bool audio, | 107 bool audio, |
| 108 bool video, | 108 bool video, |
| 109 content::MediaStreamDevices* devices); | 109 content::MediaStreamDevices* devices); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 bool IsDesktopCaptureInProgress(); | 144 bool IsDesktopCaptureInProgress(); |
| 145 | 145 |
| 146 // Only for testing. | 146 // Only for testing. |
| 147 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices); | 147 void SetTestAudioCaptureDevices(const content::MediaStreamDevices& devices); |
| 148 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices); | 148 void SetTestVideoCaptureDevices(const content::MediaStreamDevices& devices); |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 friend struct DefaultSingletonTraits<MediaCaptureDevicesDispatcher>; | 151 friend struct DefaultSingletonTraits<MediaCaptureDevicesDispatcher>; |
| 152 | 152 |
| 153 struct PendingAccessRequest { | |
| 154 PendingAccessRequest(const content::MediaStreamRequest& request, | |
| 155 const content::MediaResponseCallback& callback); | |
| 156 ~PendingAccessRequest(); | |
| 157 | |
| 158 // TODO(gbillock): make the MediaStreamDevicesController owned by | |
| 159 // this object when we're using bubbles. | |
| 160 content::MediaStreamRequest request; | |
| 161 content::MediaResponseCallback callback; | |
| 162 }; | |
| 163 typedef std::deque<PendingAccessRequest> RequestsQueue; | |
| 164 typedef std::map<content::WebContents*, RequestsQueue> RequestsQueues; | |
| 165 | |
| 166 MediaCaptureDevicesDispatcher(); | 153 MediaCaptureDevicesDispatcher(); |
| 167 ~MediaCaptureDevicesDispatcher() override; | 154 ~MediaCaptureDevicesDispatcher() override; |
| 168 | 155 |
| 169 // content::NotificationObserver implementation. | |
| 170 void Observe(int type, | |
| 171 const content::NotificationSource& source, | |
| 172 const content::NotificationDetails& details) override; | |
| 173 | |
| 174 // Helpers for ProcessMediaAccessRequest(). | |
| 175 void ProcessDesktopCaptureAccessRequest( | |
| 176 content::WebContents* web_contents, | |
| 177 const content::MediaStreamRequest& request, | |
| 178 const content::MediaResponseCallback& callback, | |
| 179 const extensions::Extension* extension); | |
| 180 void ProcessScreenCaptureAccessRequest( | |
| 181 content::WebContents* web_contents, | |
| 182 const content::MediaStreamRequest& request, | |
| 183 const content::MediaResponseCallback& callback, | |
| 184 const extensions::Extension* extension); | |
| 185 void ProcessTabCaptureAccessRequest( | |
| 186 content::WebContents* web_contents, | |
| 187 const content::MediaStreamRequest& request, | |
| 188 const content::MediaResponseCallback& callback, | |
| 189 const extensions::Extension* extension); | |
| 190 #if defined(ENABLE_EXTENSIONS) | |
| 191 void ProcessMediaAccessRequestFromPlatformAppOrExtension( | |
| 192 content::WebContents* web_contents, | |
| 193 const content::MediaStreamRequest& request, | |
| 194 const content::MediaResponseCallback& callback, | |
| 195 const extensions::Extension* extension); | |
| 196 #endif | |
| 197 void ProcessRegularMediaAccessRequest( | |
| 198 content::WebContents* web_contents, | |
| 199 const content::MediaStreamRequest& request, | |
| 200 const content::MediaResponseCallback& callback); | |
| 201 void ProcessQueuedAccessRequest(content::WebContents* web_contents); | |
| 202 void OnAccessRequestResponse(content::WebContents* web_contents, | |
| 203 const content::MediaStreamDevices& devices, | |
| 204 content::MediaStreamRequestResult result, | |
| 205 scoped_ptr<content::MediaStreamUI> ui); | |
| 206 | |
| 207 // Called by the MediaObserver() functions, executed on UI thread. | 156 // Called by the MediaObserver() functions, executed on UI thread. |
| 208 void NotifyAudioDevicesChangedOnUIThread(); | 157 void NotifyAudioDevicesChangedOnUIThread(); |
| 209 void NotifyVideoDevicesChangedOnUIThread(); | 158 void NotifyVideoDevicesChangedOnUIThread(); |
| 210 void UpdateMediaRequestStateOnUIThread( | 159 void UpdateMediaRequestStateOnUIThread( |
| 211 int render_process_id, | 160 int render_process_id, |
| 212 int render_frame_id, | 161 int render_frame_id, |
| 213 int page_request_id, | 162 int page_request_id, |
| 214 const GURL& security_origin, | 163 const GURL& security_origin, |
| 215 content::MediaStreamType stream_type, | 164 content::MediaStreamType stream_type, |
| 216 content::MediaRequestState state); | 165 content::MediaRequestState state); |
| 217 void OnCreatingAudioStreamOnUIThread(int render_process_id, | 166 void OnCreatingAudioStreamOnUIThread(int render_process_id, |
| 218 int render_frame_id); | 167 int render_frame_id); |
| 219 | 168 |
| 220 // Only for testing, a list of cached audio capture devices. | 169 // Only for testing, a list of cached audio capture devices. |
| 221 content::MediaStreamDevices test_audio_devices_; | 170 content::MediaStreamDevices test_audio_devices_; |
| 222 | 171 |
| 223 // Only for testing, a list of cached video capture devices. | 172 // Only for testing, a list of cached video capture devices. |
| 224 content::MediaStreamDevices test_video_devices_; | 173 content::MediaStreamDevices test_video_devices_; |
| 225 | 174 |
| 226 // A list of observers for the device update notifications. | 175 // A list of observers for the device update notifications. |
| 227 ObserverList<Observer> observers_; | 176 ObserverList<Observer> observers_; |
| 228 | 177 |
| 229 // Flag used by unittests to disable device enumeration. | 178 // Flag used by unittests to disable device enumeration. |
| 230 bool is_device_enumeration_disabled_; | 179 bool is_device_enumeration_disabled_; |
| 231 | 180 |
| 232 RequestsQueues pending_requests_; | |
| 233 | |
| 234 scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_; | 181 scoped_refptr<MediaStreamCaptureIndicator> media_stream_capture_indicator_; |
| 235 | 182 |
| 236 scoped_ptr<DesktopStreamsRegistry> desktop_streams_registry_; | 183 scoped_ptr<DesktopStreamsRegistry> desktop_streams_registry_; |
| 237 | 184 |
| 238 content::NotificationRegistrar notifications_registrar_; | 185 // Handlers for processing media access requests. |
| 239 | 186 ScopedVector<MediaAccessHandler> media_access_handlers_; |
| 240 // Tracks MEDIA_DESKTOP_VIDEO_CAPTURE sessions which reach the | |
| 241 // MEDIA_REQUEST_STATE_DONE state. Sessions are remove when | |
| 242 // MEDIA_REQUEST_STATE_CLOSING is encountered. | |
| 243 struct DesktopCaptureSession { | |
| 244 int render_process_id; | |
| 245 int render_frame_id; | |
| 246 int page_request_id; | |
| 247 }; | |
| 248 typedef std::list<DesktopCaptureSession> DesktopCaptureSessions; | |
| 249 DesktopCaptureSessions desktop_capture_sessions_; | |
| 250 | 187 |
| 251 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher); | 188 DISALLOW_COPY_AND_ASSIGN(MediaCaptureDevicesDispatcher); |
| 252 }; | 189 }; |
| 253 | 190 |
| 254 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ | 191 #endif // CHROME_BROWSER_MEDIA_MEDIA_CAPTURE_DEVICES_DISPATCHER_H_ |
| OLD | NEW |