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/common/media_stream_request.h" 13 14 namespace content { 15 16 class RenderViewHostDelegate; 17 18 // MediaStreamUIProxy proxies calls to media stream UI between IO thread and UI 19 // thread. One instance of this class is create per MediaStream object. It must 20 // be create, used and destroyed on IO thread. 21 class CONTENT_EXPORT MediaStreamUIProxy { 22 public: 23 typedef base::Callback< 24 void (const MediaStreamDevices& devices, 25 content::MediaStreamRequestResult result)> 26 ResponseCallback; 27 28 typedef base::Callback<void(gfx::NativeViewId window_id)> WindowIdCallback; 29 30 static scoped_ptr<MediaStreamUIProxy> Create(); 31 static scoped_ptr<MediaStreamUIProxy> CreateForTests( 32 RenderViewHostDelegate* render_delegate); 33 34 virtual ~MediaStreamUIProxy(); 35 36 // Requests access for the MediaStream by calling 37 // WebContentsDelegate::RequestMediaAccessPermission(). The specified 38 // |response_callback| is called when the WebContentsDelegate approves or 39 // denies request. 40 virtual void RequestAccess(const MediaStreamRequest& request, 41 const ResponseCallback& response_callback); 42 43 // Notifies the UI that the MediaStream has been started. Must be called after 44 // access has been approved using RequestAccess(). |stop_callback| is be 45 // called on the IO thread after the user has requests the stream to be 46 // stopped. |window_id_callback| is called on the IO thread with the platform- 47 // dependent window ID of the UI. 48 virtual void OnStarted(const base::Closure& stop_callback, 49 const WindowIdCallback& window_id_callback); 50 51 void SetRenderViewHostDelegateForTests(RenderViewHostDelegate* delegate); 52 53 protected: 54 MediaStreamUIProxy(RenderViewHostDelegate* test_render_delegate); 55 56 private: 57 class Core; 58 friend class Core; 59 friend class FakeMediaStreamUIProxy; 60 61 void ProcessAccessRequestResponse( 62 const MediaStreamDevices& devices, 63 content::MediaStreamRequestResult result); 64 void ProcessStopRequestFromUI(); 65 void OnWindowId(const WindowIdCallback& window_id_callback, 66 gfx::NativeViewId* window_id); 67 68 scoped_ptr<Core> core_; 69 ResponseCallback response_callback_; 70 base::Closure stop_callback_; 71 72 base::WeakPtrFactory<MediaStreamUIProxy> weak_factory_; 73 74 DISALLOW_COPY_AND_ASSIGN(MediaStreamUIProxy); 75 }; 76 77 class CONTENT_EXPORT FakeMediaStreamUIProxy : public MediaStreamUIProxy { 78 public: 79 explicit FakeMediaStreamUIProxy(); 80 virtual ~FakeMediaStreamUIProxy(); 81 82 void SetAvailableDevices(const MediaStreamDevices& devices); 83 84 // MediaStreamUIProxy overrides. 85 virtual void RequestAccess( 86 const MediaStreamRequest& request, 87 const ResponseCallback& response_callback) OVERRIDE; 88 virtual void OnStarted(const base::Closure& stop_callback, 89 const WindowIdCallback& window_id_callback) OVERRIDE; 90 91 private: 92 MediaStreamDevices devices_; 93 94 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamUIProxy); 95 }; 96 97 } // namespace content 98 99 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_STREAM_UI_PROXY_H_ 100