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