1 // Copyright 2013 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 CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_UI_PROXY_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_UI_PROXY_H_ 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "content/public/browser/browser_thread.h" 13 #include "content/public/common/media_stream_request.h" 14 15 namespace content { 16 17 class RenderFrameHostDelegate; 18 19 // MediaStreamUIProxy proxies calls to media stream UI between IO thread and UI 20 // thread. One instance of this class is create per MediaStream object. It must 21 // be created, used and destroyed on IO thread. 22 class CONTENT_EXPORT MediaStreamUIProxy { 23 public: 24 typedef base::Callback< 25 void (const MediaStreamDevices& devices, 26 content::MediaStreamRequestResult result)> 27 ResponseCallback; 28 29 typedef base::Callback<void(gfx::NativeViewId window_id)> WindowIdCallback; 30 31 static scoped_ptr<MediaStreamUIProxy> Create(); 32 static scoped_ptr<MediaStreamUIProxy> CreateForTests( 33 RenderFrameHostDelegate* render_delegate); 34 35 virtual ~MediaStreamUIProxy(); 36 37 // Requests access for the MediaStream by calling 38 // WebContentsDelegate::RequestMediaAccessPermission(). The specified 39 // |response_callback| is called when the WebContentsDelegate approves or 40 // denies request. 41 virtual void RequestAccess(const MediaStreamRequest& request, 42 const ResponseCallback& response_callback); 43 44 // Checks if we have permission to access the microphone or camera. Note that 45 // this does not query the user, it checks any stored settings such as policy 46 // or extension permissions. |type| must be MEDIA_DEVICE_AUDIO_CAPTURE 47 // or MEDIA_DEVICE_VIDEO_CAPTURE. 48 virtual void CheckAccess(const GURL& security_origin, 49 MediaStreamType type, 50 int render_process_id, 51 int render_frame_id, 52 const base::Callback<void(bool)>& callback); 53 54 // Notifies the UI that the MediaStream has been started. Must be called after 55 // access has been approved using RequestAccess(). |stop_callback| is be 56 // called on the IO thread after the user has requests the stream to be 57 // stopped. |window_id_callback| is called on the IO thread with the platform- 58 // dependent window ID of the UI. 59 virtual void OnStarted(const base::Closure& stop_callback, 60 const WindowIdCallback& window_id_callback); 61 62 void SetRenderFrameHostDelegateForTests(RenderFrameHostDelegate* delegate); 63 64 protected: 65 explicit MediaStreamUIProxy(RenderFrameHostDelegate* test_render_delegate); 66 67 private: 68 class Core; 69 friend class Core; 70 friend class FakeMediaStreamUIProxy; 71 72 void ProcessAccessRequestResponse( 73 const MediaStreamDevices& devices, 74 content::MediaStreamRequestResult result); 75 void ProcessStopRequestFromUI(); 76 void OnWindowId(const WindowIdCallback& window_id_callback, 77 gfx::NativeViewId* window_id); 78 void OnCheckedAccess(const base::Callback<void(bool)>& callback, 79 bool have_access); 80 81 scoped_ptr<Core, content::BrowserThread::DeleteOnUIThread> core_; 82 ResponseCallback response_callback_; 83 base::Closure stop_callback_; 84 85 base::WeakPtrFactory<MediaStreamUIProxy> weak_factory_; 86 87 DISALLOW_COPY_AND_ASSIGN(MediaStreamUIProxy); 88 }; 89 90 class CONTENT_EXPORT FakeMediaStreamUIProxy : public MediaStreamUIProxy { 91 public: 92 explicit FakeMediaStreamUIProxy(); 93 virtual ~FakeMediaStreamUIProxy(); 94 95 void SetAvailableDevices(const MediaStreamDevices& devices); 96 void SetMicAccess(bool access); 97 void SetCameraAccess(bool access); 98 99 // MediaStreamUIProxy overrides. 100 virtual void RequestAccess( 101 const MediaStreamRequest& request, 102 const ResponseCallback& response_callback) OVERRIDE; 103 virtual void CheckAccess(const GURL& security_origin, 104 MediaStreamType type, 105 int render_process_id, 106 int render_frame_id, 107 const base::Callback<void(bool)>& callback) OVERRIDE; 108 virtual void OnStarted(const base::Closure& stop_callback, 109 const WindowIdCallback& window_id_callback) OVERRIDE; 110 111 private: 112 // This is used for RequestAccess(). 113 MediaStreamDevices devices_; 114 115 // These are used for CheckAccess(). 116 bool mic_access_; 117 bool camera_access_; 118 119 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamUIProxy); 120 }; 121 122 } // namespace content 123 124 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_UI_PROXY_H_ 125