OLD | NEW |
---|---|
(Empty) | |
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 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | |
6 | |
7 #include "content/browser/renderer_host/render_view_host_delegate.h" | |
8 #include "content/browser/renderer_host/render_view_host_impl.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "media/video/capture/fake_video_capture_device.h" | |
11 | |
12 namespace content { | |
13 | |
14 class MediaStreamUIProxy::Core { | |
15 public: | |
16 explicit Core(const base::WeakPtr<MediaStreamUIProxy>& proxy); | |
17 ~Core(); | |
18 | |
19 void RequestAccess(const MediaStreamRequest& request); | |
20 void OnStarted(); | |
21 | |
22 void SetRenderViewHostDelegateForTests(RenderViewHostDelegate* delegate); | |
23 | |
24 private: | |
25 void ProcessAccessRequestResponse(const MediaStreamDevices& devices, | |
26 scoped_ptr<MediaStreamUI> stream_ui); | |
27 void ProcessStopRequestFromUI(); | |
28 | |
29 base::WeakPtr<MediaStreamUIProxy> proxy_; | |
30 scoped_ptr<MediaStreamUI> ui_; | |
31 | |
32 RenderViewHostDelegate* render_delegate_; | |
33 | |
34 // WeakPtr<> is used to RequestMediaAccessPermission() because there is no way | |
35 // cancel media requests. | |
36 base::WeakPtrFactory<Core> weak_factory_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(Core); | |
39 }; | |
40 | |
41 MediaStreamUIProxy::Core::Core(const base::WeakPtr<MediaStreamUIProxy>& proxy) | |
42 : proxy_(proxy), | |
43 render_delegate_(NULL), | |
44 weak_factory_(this) { | |
45 } | |
46 | |
47 MediaStreamUIProxy::Core::~Core() { | |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
49 } | |
50 | |
51 void MediaStreamUIProxy::Core::RequestAccess( | |
52 const MediaStreamRequest& request) { | |
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
54 | |
55 if (!render_delegate_) { | |
56 RenderViewHostImpl* host = RenderViewHostImpl::FromID( | |
57 request.render_process_id, request.render_view_id); | |
58 | |
59 // Tab may have gone away. | |
60 if (!host || !host->GetDelegate()) { | |
61 ProcessAccessRequestResponse( | |
62 MediaStreamDevices(), scoped_ptr<MediaStreamUI>()); | |
63 return; | |
64 } | |
65 | |
66 render_delegate_ = host->GetDelegate(); | |
miu
2013/06/06 20:03:01
In this "set once lazily" scheme, will render_dele
Sergey Ulanov
2013/06/06 21:40:03
This method should only be called only once per in
| |
67 } | |
68 | |
69 render_delegate_->RequestMediaAccessPermission( | |
70 request, base::Bind(&Core::ProcessAccessRequestResponse, | |
71 weak_factory_.GetWeakPtr())); | |
72 } | |
73 | |
74 void MediaStreamUIProxy::Core::OnStarted() { | |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
76 if (ui_) { | |
77 ui_->OnStarted(base::Bind(&Core::ProcessStopRequestFromUI, | |
78 base::Unretained(this))); | |
79 } | |
80 } | |
81 | |
82 void MediaStreamUIProxy::Core::SetRenderViewHostDelegateForTests( | |
83 RenderViewHostDelegate* delegate) { | |
84 render_delegate_ = delegate; | |
85 } | |
86 | |
87 void MediaStreamUIProxy::Core::ProcessAccessRequestResponse( | |
88 const MediaStreamDevices& devices, | |
89 scoped_ptr<MediaStreamUI> stream_ui) { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
91 | |
92 ui_ = stream_ui.Pass(); | |
93 BrowserThread::PostTask( | |
94 BrowserThread::IO, FROM_HERE, | |
95 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, | |
96 proxy_, devices)); | |
97 } | |
98 | |
99 void MediaStreamUIProxy::Core::ProcessStopRequestFromUI() { | |
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
101 | |
102 BrowserThread::PostTask( | |
103 BrowserThread::IO, FROM_HERE, | |
104 base::Bind(&MediaStreamUIProxy::ProcessStopRequestFromUI, proxy_)); | |
105 } | |
106 | |
107 MediaStreamUIProxy::MediaStreamUIProxy() | |
108 : weak_factory_(this) { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
110 core_.reset(new Core(weak_factory_.GetWeakPtr())); | |
111 } | |
112 | |
113 MediaStreamUIProxy::~MediaStreamUIProxy() { | |
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
115 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, core_.release()); | |
116 } | |
117 | |
118 void MediaStreamUIProxy::SetRenderViewHostDelegateForTests( | |
miu
2013/06/06 20:03:01
Consider using injection (i.e., argument to the co
Sergey Ulanov
2013/06/06 21:40:03
Done.
| |
119 RenderViewHostDelegate* delegate) { | |
120 core_->SetRenderViewHostDelegateForTests(delegate); | |
121 } | |
122 | |
123 void MediaStreamUIProxy::RequestAccess( | |
124 const MediaStreamRequest& request, | |
125 const ResponseCallback& response_callback) { | |
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
127 | |
128 response_callback_ = response_callback; | |
129 BrowserThread::PostTask( | |
130 BrowserThread::UI, FROM_HERE, | |
131 base::Bind(&Core::RequestAccess, base::Unretained(core_.get()), request)); | |
132 } | |
133 | |
134 void MediaStreamUIProxy::OnStarted(const base::Closure& stop_callback) { | |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
136 | |
137 stop_callback_ = stop_callback; | |
138 BrowserThread::PostTask( | |
139 BrowserThread::UI, FROM_HERE, | |
140 base::Bind(&Core::OnStarted, base::Unretained(core_.get()))); | |
141 } | |
142 | |
143 void MediaStreamUIProxy::ProcessAccessRequestResponse( | |
144 const MediaStreamDevices& devices) { | |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
146 DCHECK(!response_callback_.is_null()); | |
147 | |
148 ResponseCallback cb = response_callback_; | |
149 response_callback_.Reset(); | |
150 cb.Run(devices); | |
151 } | |
152 | |
153 void MediaStreamUIProxy::ProcessStopRequestFromUI() { | |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
155 DCHECK(!stop_callback_.is_null()); | |
156 | |
157 base::Closure cb = stop_callback_; | |
158 stop_callback_.Reset(); | |
159 cb.Run(); | |
160 } | |
161 | |
162 FakeMediaStreamUIProxy::FakeMediaStreamUIProxy() {} | |
163 | |
164 FakeMediaStreamUIProxy::~FakeMediaStreamUIProxy() {} | |
165 | |
166 void FakeMediaStreamUIProxy::SetAvailableDevices( | |
167 const MediaStreamDevices& devices) { | |
168 devices_ = devices; | |
169 } | |
170 | |
171 void FakeMediaStreamUIProxy::RequestAccess( | |
172 const MediaStreamRequest& request, | |
173 const ResponseCallback& response_callback) { | |
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
175 | |
176 response_callback_ = response_callback; | |
177 | |
178 MediaStreamDevices devices_to_use; | |
179 bool accepted_audio = false; | |
180 bool accepted_video = false; | |
181 // Use the first capture device of the same media type in the list for the | |
182 // fake UI. | |
183 for (MediaStreamDevices::const_iterator it = devices_.begin(); | |
184 it != devices_.end(); ++it) { | |
185 if (!accepted_audio && | |
186 IsAudioMediaType(request.audio_type) && | |
187 IsAudioMediaType(it->type)) { | |
188 devices_to_use.push_back(*it); | |
189 accepted_audio = true; | |
190 } else if (!accepted_video && | |
191 IsVideoMediaType(request.video_type) && | |
192 IsVideoMediaType(it->type)) { | |
193 devices_to_use.push_back(*it); | |
194 accepted_video = true; | |
195 } | |
196 } | |
197 | |
198 BrowserThread::PostTask( | |
199 BrowserThread::IO, FROM_HERE, | |
200 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, | |
201 weak_factory_.GetWeakPtr(), devices_to_use)); | |
202 } | |
203 | |
204 void FakeMediaStreamUIProxy::OnStarted(const base::Closure& stop_callback) { | |
205 } | |
206 | |
207 } // namespace content | |
OLD | NEW |