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