| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 5 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "content/browser/frame_host/frame_tree_node.h" |
| 8 #include "content/browser/frame_host/render_frame_host_delegate.h" | 9 #include "content/browser/frame_host/render_frame_host_delegate.h" |
| 9 #include "content/browser/frame_host/render_frame_host_impl.h" | 10 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
| 12 #include "media/video/capture/fake_video_capture_device.h" | 13 #include "media/video/capture/fake_video_capture_device.h" |
| 13 | 14 |
| 14 namespace content { | 15 namespace content { |
| 15 | 16 |
| 17 void SetAndCheckAncestorFlag(MediaStreamRequest* request) { |
| 18 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 19 RenderFrameHostImpl* rfh = |
| 20 RenderFrameHostImpl::FromID(request->render_process_id, |
| 21 request->render_frame_id); |
| 22 |
| 23 if (rfh == NULL) { |
| 24 // RenderFrame destroyed before the request is handled? |
| 25 return; |
| 26 } |
| 27 FrameTreeNode* node = rfh->frame_tree_node(); |
| 28 |
| 29 while (node->parent() != NULL) { |
| 30 if (!node->HasSameOrigin(*node->parent())) { |
| 31 request->all_ancestors_have_same_origin = false; |
| 32 return; |
| 33 } |
| 34 node = node->parent(); |
| 35 } |
| 36 request->all_ancestors_have_same_origin = true; |
| 37 } |
| 38 |
| 16 class MediaStreamUIProxy::Core { | 39 class MediaStreamUIProxy::Core { |
| 17 public: | 40 public: |
| 18 explicit Core(const base::WeakPtr<MediaStreamUIProxy>& proxy, | 41 explicit Core(const base::WeakPtr<MediaStreamUIProxy>& proxy, |
| 19 RenderFrameHostDelegate* test_render_delegate); | 42 RenderFrameHostDelegate* test_render_delegate); |
| 20 ~Core(); | 43 ~Core(); |
| 21 | 44 |
| 22 void RequestAccess(const MediaStreamRequest& request); | 45 void RequestAccess(scoped_ptr<MediaStreamRequest> request); |
| 23 bool CheckAccess(const GURL& security_origin, | 46 bool CheckAccess(const GURL& security_origin, |
| 24 MediaStreamType type, | 47 MediaStreamType type, |
| 25 int process_id, | 48 int process_id, |
| 26 int frame_id); | 49 int frame_id); |
| 27 void OnStarted(gfx::NativeViewId* window_id); | 50 void OnStarted(gfx::NativeViewId* window_id); |
| 28 | 51 |
| 29 private: | 52 private: |
| 30 void ProcessAccessRequestResponse(const MediaStreamDevices& devices, | 53 void ProcessAccessRequestResponse(const MediaStreamDevices& devices, |
| 31 content::MediaStreamRequestResult result, | 54 content::MediaStreamRequestResult result, |
| 32 scoped_ptr<MediaStreamUI> stream_ui); | 55 scoped_ptr<MediaStreamUI> stream_ui); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 51 : proxy_(proxy), | 74 : proxy_(proxy), |
| 52 test_render_delegate_(test_render_delegate), | 75 test_render_delegate_(test_render_delegate), |
| 53 weak_factory_(this) { | 76 weak_factory_(this) { |
| 54 } | 77 } |
| 55 | 78 |
| 56 MediaStreamUIProxy::Core::~Core() { | 79 MediaStreamUIProxy::Core::~Core() { |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 58 } | 81 } |
| 59 | 82 |
| 60 void MediaStreamUIProxy::Core::RequestAccess( | 83 void MediaStreamUIProxy::Core::RequestAccess( |
| 61 const MediaStreamRequest& request) { | 84 scoped_ptr<MediaStreamRequest> request) { |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 85 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 63 | 86 |
| 64 RenderFrameHostDelegate* render_delegate = GetRenderFrameHostDelegate( | 87 RenderFrameHostDelegate* render_delegate = GetRenderFrameHostDelegate( |
| 65 request.render_process_id, request.render_frame_id); | 88 request->render_process_id, request->render_frame_id); |
| 66 | 89 |
| 67 // Tab may have gone away, or has no delegate from which to request access. | 90 // Tab may have gone away, or has no delegate from which to request access. |
| 68 if (!render_delegate) { | 91 if (!render_delegate) { |
| 69 ProcessAccessRequestResponse(MediaStreamDevices(), | 92 ProcessAccessRequestResponse(MediaStreamDevices(), |
| 70 MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN, | 93 MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN, |
| 71 scoped_ptr<MediaStreamUI>()); | 94 scoped_ptr<MediaStreamUI>()); |
| 72 return; | 95 return; |
| 73 } | 96 } |
| 97 SetAndCheckAncestorFlag(request.get()); |
| 74 | 98 |
| 75 render_delegate->RequestMediaAccessPermission( | 99 render_delegate->RequestMediaAccessPermission( |
| 76 request, base::Bind(&Core::ProcessAccessRequestResponse, | 100 *request, base::Bind(&Core::ProcessAccessRequestResponse, |
| 77 weak_factory_.GetWeakPtr())); | 101 weak_factory_.GetWeakPtr())); |
| 78 } | 102 } |
| 79 | 103 |
| 80 bool MediaStreamUIProxy::Core::CheckAccess(const GURL& security_origin, | 104 bool MediaStreamUIProxy::Core::CheckAccess(const GURL& security_origin, |
| 81 MediaStreamType type, | 105 MediaStreamType type, |
| 82 int render_process_id, | 106 int render_process_id, |
| 83 int render_frame_id) { | 107 int render_frame_id) { |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 85 | 109 |
| 86 RenderFrameHostDelegate* render_delegate = | 110 RenderFrameHostDelegate* render_delegate = |
| 87 GetRenderFrameHostDelegate(render_process_id, render_frame_id); | 111 GetRenderFrameHostDelegate(render_process_id, render_frame_id); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 : weak_factory_(this) { | 171 : weak_factory_(this) { |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 149 core_.reset(new Core(weak_factory_.GetWeakPtr(), test_render_delegate)); | 173 core_.reset(new Core(weak_factory_.GetWeakPtr(), test_render_delegate)); |
| 150 } | 174 } |
| 151 | 175 |
| 152 MediaStreamUIProxy::~MediaStreamUIProxy() { | 176 MediaStreamUIProxy::~MediaStreamUIProxy() { |
| 153 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 177 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 154 } | 178 } |
| 155 | 179 |
| 156 void MediaStreamUIProxy::RequestAccess( | 180 void MediaStreamUIProxy::RequestAccess( |
| 157 const MediaStreamRequest& request, | 181 scoped_ptr<MediaStreamRequest> request, |
| 158 const ResponseCallback& response_callback) { | 182 const ResponseCallback& response_callback) { |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 160 | 184 |
| 161 response_callback_ = response_callback; | 185 response_callback_ = response_callback; |
| 162 BrowserThread::PostTask( | 186 BrowserThread::PostTask( |
| 163 BrowserThread::UI, FROM_HERE, | 187 BrowserThread::UI, FROM_HERE, |
| 164 base::Bind(&Core::RequestAccess, base::Unretained(core_.get()), request)); | 188 base::Bind(&Core::RequestAccess, base::Unretained(core_.get()), |
| 189 base::Passed(&request))); |
| 165 } | 190 } |
| 166 | 191 |
| 167 void MediaStreamUIProxy::CheckAccess( | 192 void MediaStreamUIProxy::CheckAccess( |
| 168 const GURL& security_origin, | 193 const GURL& security_origin, |
| 169 MediaStreamType type, | 194 MediaStreamType type, |
| 170 int render_process_id, | 195 int render_process_id, |
| 171 int render_frame_id, | 196 int render_frame_id, |
| 172 const base::Callback<void(bool)>& callback) { | 197 const base::Callback<void(bool)>& callback) { |
| 173 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 198 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 174 | 199 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 280 |
| 256 void FakeMediaStreamUIProxy::SetMicAccess(bool access) { | 281 void FakeMediaStreamUIProxy::SetMicAccess(bool access) { |
| 257 mic_access_ = access; | 282 mic_access_ = access; |
| 258 } | 283 } |
| 259 | 284 |
| 260 void FakeMediaStreamUIProxy::SetCameraAccess(bool access) { | 285 void FakeMediaStreamUIProxy::SetCameraAccess(bool access) { |
| 261 camera_access_ = access; | 286 camera_access_ = access; |
| 262 } | 287 } |
| 263 | 288 |
| 264 void FakeMediaStreamUIProxy::RequestAccess( | 289 void FakeMediaStreamUIProxy::RequestAccess( |
| 265 const MediaStreamRequest& request, | 290 scoped_ptr<MediaStreamRequest> request, |
| 266 const ResponseCallback& response_callback) { | 291 const ResponseCallback& response_callback) { |
| 267 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 268 | 293 |
| 269 response_callback_ = response_callback; | 294 response_callback_ = response_callback; |
| 270 | 295 |
| 271 if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 296 if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 272 switches::kUseFakeUIForMediaStream) == "deny") { | 297 switches::kUseFakeUIForMediaStream) == "deny") { |
| 273 // Immediately deny the request. | 298 // Immediately deny the request. |
| 274 BrowserThread::PostTask( | 299 BrowserThread::PostTask( |
| 275 BrowserThread::IO, FROM_HERE, | 300 BrowserThread::IO, FROM_HERE, |
| 276 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, | 301 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, |
| 277 weak_factory_.GetWeakPtr(), | 302 weak_factory_.GetWeakPtr(), |
| 278 MediaStreamDevices(), | 303 MediaStreamDevices(), |
| 279 MEDIA_DEVICE_PERMISSION_DENIED)); | 304 MEDIA_DEVICE_PERMISSION_DENIED)); |
| 280 return; | 305 return; |
| 281 } | 306 } |
| 282 | 307 |
| 283 MediaStreamDevices devices_to_use; | 308 MediaStreamDevices devices_to_use; |
| 284 bool accepted_audio = false; | 309 bool accepted_audio = false; |
| 285 bool accepted_video = false; | 310 bool accepted_video = false; |
| 286 | 311 |
| 287 // Use the first capture device of the same media type in the list for the | 312 // Use the first capture device of the same media type in the list for the |
| 288 // fake UI. | 313 // fake UI. |
| 289 for (MediaStreamDevices::const_iterator it = devices_.begin(); | 314 for (MediaStreamDevices::const_iterator it = devices_.begin(); |
| 290 it != devices_.end(); ++it) { | 315 it != devices_.end(); ++it) { |
| 291 if (!accepted_audio && | 316 if (!accepted_audio && |
| 292 IsAudioInputMediaType(request.audio_type) && | 317 IsAudioInputMediaType(request->audio_type) && |
| 293 IsAudioInputMediaType(it->type) && | 318 IsAudioInputMediaType(it->type) && |
| 294 (request.requested_audio_device_id.empty() || | 319 (request->requested_audio_device_id.empty() || |
| 295 request.requested_audio_device_id == it->id)) { | 320 request->requested_audio_device_id == it->id)) { |
| 296 devices_to_use.push_back(*it); | 321 devices_to_use.push_back(*it); |
| 297 accepted_audio = true; | 322 accepted_audio = true; |
| 298 } else if (!accepted_video && | 323 } else if (!accepted_video && |
| 299 IsVideoMediaType(request.video_type) && | 324 IsVideoMediaType(request->video_type) && |
| 300 IsVideoMediaType(it->type) && | 325 IsVideoMediaType(it->type) && |
| 301 (request.requested_video_device_id.empty() || | 326 (request->requested_video_device_id.empty() || |
| 302 request.requested_video_device_id == it->id)) { | 327 request->requested_video_device_id == it->id)) { |
| 303 devices_to_use.push_back(*it); | 328 devices_to_use.push_back(*it); |
| 304 accepted_video = true; | 329 accepted_video = true; |
| 305 } | 330 } |
| 306 } | 331 } |
| 307 | 332 |
| 308 // Fail the request if a device doesn't exist for the requested type. | 333 // Fail the request if a device doesn't exist for the requested type. |
| 309 if ((request.audio_type != MEDIA_NO_SERVICE && !accepted_audio) || | 334 if ((request->audio_type != MEDIA_NO_SERVICE && !accepted_audio) || |
| 310 (request.video_type != MEDIA_NO_SERVICE && !accepted_video)) { | 335 (request->video_type != MEDIA_NO_SERVICE && !accepted_video)) { |
| 311 devices_to_use.clear(); | 336 devices_to_use.clear(); |
| 312 } | 337 } |
| 313 | 338 |
| 314 BrowserThread::PostTask( | 339 BrowserThread::PostTask( |
| 315 BrowserThread::IO, FROM_HERE, | 340 BrowserThread::IO, FROM_HERE, |
| 316 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, | 341 base::Bind(&MediaStreamUIProxy::ProcessAccessRequestResponse, |
| 317 weak_factory_.GetWeakPtr(), | 342 weak_factory_.GetWeakPtr(), |
| 318 devices_to_use, | 343 devices_to_use, |
| 319 devices_to_use.empty() ? | 344 devices_to_use.empty() ? |
| 320 MEDIA_DEVICE_NO_HARDWARE : | 345 MEDIA_DEVICE_NO_HARDWARE : |
| (...skipping 25 matching lines...) Expand all Loading... |
| 346 callback, | 371 callback, |
| 347 have_access)); | 372 have_access)); |
| 348 return; | 373 return; |
| 349 } | 374 } |
| 350 | 375 |
| 351 void FakeMediaStreamUIProxy::OnStarted( | 376 void FakeMediaStreamUIProxy::OnStarted( |
| 352 const base::Closure& stop_callback, | 377 const base::Closure& stop_callback, |
| 353 const WindowIdCallback& window_id_callback) {} | 378 const WindowIdCallback& window_id_callback) {} |
| 354 | 379 |
| 355 } // namespace content | 380 } // namespace content |
| OLD | NEW |