Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_dispatcher_host.h" | 5 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
|
palmer
2016/04/26 23:25:02
Nit: I think you need a blank line after this.
Guido Urdaneta
2016/05/01 11:08:39
Done.
| |
| 8 #include "base/command_line.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 7 #include "content/browser/browser_main_loop.h" | 10 #include "content/browser/browser_main_loop.h" |
| 8 #include "content/browser/child_process_security_policy_impl.h" | 11 #include "content/browser/child_process_security_policy_impl.h" |
| 12 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | |
| 9 #include "content/common/media/media_stream_messages.h" | 13 #include "content/common/media/media_stream_messages.h" |
| 10 #include "content/common/media/media_stream_options.h" | 14 #include "content/common/media/media_stream_options.h" |
| 11 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/common/content_switches.h" | |
| 12 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 13 | 18 |
| 14 namespace content { | 19 namespace content { |
| 15 | 20 |
| 16 MediaStreamDispatcherHost::MediaStreamDispatcherHost( | 21 MediaStreamDispatcherHost::MediaStreamDispatcherHost( |
| 17 int render_process_id, | 22 int render_process_id, |
| 18 const ResourceContext::SaltCallback& salt_callback, | 23 const ResourceContext::SaltCallback& salt_callback, |
| 19 MediaStreamManager* media_stream_manager) | 24 MediaStreamManager* media_stream_manager, |
| 25 bool use_fake_ui) | |
| 20 : BrowserMessageFilter(MediaStreamMsgStart), | 26 : BrowserMessageFilter(MediaStreamMsgStart), |
| 21 render_process_id_(render_process_id), | 27 render_process_id_(render_process_id), |
| 22 salt_callback_(salt_callback), | 28 salt_callback_(salt_callback), |
| 23 media_stream_manager_(media_stream_manager) { | 29 media_stream_manager_(media_stream_manager), |
| 24 } | 30 use_fake_ui_(use_fake_ui), |
| 31 weak_factory_(this) {} | |
| 25 | 32 |
| 26 void MediaStreamDispatcherHost::StreamGenerated( | 33 void MediaStreamDispatcherHost::StreamGenerated( |
| 27 int render_frame_id, | 34 int render_frame_id, |
| 28 int page_request_id, | 35 int page_request_id, |
| 29 const std::string& label, | 36 const std::string& label, |
| 30 const StreamDeviceInfoArray& audio_devices, | 37 const StreamDeviceInfoArray& audio_devices, |
| 31 const StreamDeviceInfoArray& video_devices) { | 38 const StreamDeviceInfoArray& video_devices) { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 33 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated(" | 40 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated(" |
| 34 << ", {label = " << label << "})"; | 41 << ", {label = " << label << "})"; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 << ", {page_request_id = " << page_request_id << "})"; | 94 << ", {page_request_id = " << page_request_id << "})"; |
| 88 | 95 |
| 89 Send(new MediaStreamMsg_DeviceOpened( | 96 Send(new MediaStreamMsg_DeviceOpened( |
| 90 render_frame_id, page_request_id, label, video_device)); | 97 render_frame_id, page_request_id, label, video_device)); |
| 91 } | 98 } |
| 92 | 99 |
| 93 void MediaStreamDispatcherHost::DevicesChanged(MediaStreamType type) { | 100 void MediaStreamDispatcherHost::DevicesChanged(MediaStreamType type) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 95 DVLOG(1) << "MediaStreamDispatcherHost::DevicesChanged(" | 102 DVLOG(1) << "MediaStreamDispatcherHost::DevicesChanged(" |
| 96 << "{type = " << type << "})"; | 103 << "{type = " << type << "})"; |
| 97 // TODO(guidou): check permissions and forward notifications to renderer. | 104 for (const auto& subscriber : device_change_subscribers_) { |
| 98 // crbug.com/388648 | 105 std::unique_ptr<MediaStreamUIProxy> ui_proxy = CreateMediaStreamUIProxy(); |
| 106 ui_proxy->CheckAccess( | |
| 107 subscriber.security_origin, type, render_process_id_, | |
| 108 subscriber.render_frame_id, | |
| 109 base::Bind(&MediaStreamDispatcherHost::HandleCheckAccessResponse, | |
| 110 weak_factory_.GetWeakPtr(), | |
| 111 base::Passed(std::move(ui_proxy)), | |
| 112 subscriber.render_frame_id)); | |
| 113 } | |
| 99 } | 114 } |
| 100 | 115 |
| 101 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 116 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| 102 bool handled = true; | 117 bool handled = true; |
| 103 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost, message) | 118 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost, message) |
| 104 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream) | 119 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream) |
| 105 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream, | 120 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream, |
| 106 OnCancelGenerateStream) | 121 OnCancelGenerateStream) |
| 107 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice, | 122 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice, |
| 108 OnStopStreamDevice) | 123 OnStopStreamDevice) |
| 109 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices, | 124 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices, |
| 110 OnEnumerateDevices) | 125 OnEnumerateDevices) |
| 111 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices, | 126 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices, |
| 112 OnCancelEnumerateDevices) | 127 OnCancelEnumerateDevices) |
| 113 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice, | 128 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice, |
| 114 OnOpenDevice) | 129 OnOpenDevice) |
| 115 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice, | 130 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice, |
| 116 OnCloseDevice) | 131 OnCloseDevice) |
| 132 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_SubscribeToDeviceChangeNotifications, | |
| 133 OnSubscribeToDeviceChangeNotifications) | |
| 134 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelDeviceChangeNotifications, | |
| 135 OnCancelDeviceChangeNotifications) | |
| 117 IPC_MESSAGE_UNHANDLED(handled = false) | 136 IPC_MESSAGE_UNHANDLED(handled = false) |
| 118 IPC_END_MESSAGE_MAP() | 137 IPC_END_MESSAGE_MAP() |
| 119 return handled; | 138 return handled; |
| 120 } | 139 } |
| 121 | 140 |
| 122 void MediaStreamDispatcherHost::OnChannelClosing() { | 141 void MediaStreamDispatcherHost::OnChannelClosing() { |
| 123 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing"; | 142 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing"; |
| 124 | 143 |
| 125 // Since the IPC sender is gone, close all requesting/requested streams. | 144 // Since the IPC sender is gone, close all requesting/requested streams. |
| 126 media_stream_manager_->CancelAllRequests(render_process_id_); | 145 media_stream_manager_->CancelAllRequests(render_process_id_); |
| 146 if (!device_change_subscribers_.empty()) | |
| 147 media_stream_manager_->CancelDeviceChangeNotifications(this); | |
| 127 } | 148 } |
| 128 | 149 |
| 129 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() { | 150 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() { |
| 130 } | 151 } |
| 131 | 152 |
| 132 void MediaStreamDispatcherHost::OnGenerateStream(int render_frame_id, | 153 void MediaStreamDispatcherHost::OnGenerateStream(int render_frame_id, |
| 133 int page_request_id, | 154 int page_request_id, |
| 134 const StreamControls& controls, | 155 const StreamControls& controls, |
| 135 const GURL& security_origin, | 156 const GURL& security_origin, |
| 136 bool user_gesture) { | 157 bool user_gesture) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 void MediaStreamDispatcherHost::OnCloseDevice( | 241 void MediaStreamDispatcherHost::OnCloseDevice( |
| 221 int render_frame_id, | 242 int render_frame_id, |
| 222 const std::string& label) { | 243 const std::string& label) { |
| 223 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice(" | 244 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice(" |
| 224 << render_frame_id << ", " | 245 << render_frame_id << ", " |
| 225 << label << ")"; | 246 << label << ")"; |
| 226 | 247 |
| 227 media_stream_manager_->CancelRequest(label); | 248 media_stream_manager_->CancelRequest(label); |
| 228 } | 249 } |
| 229 | 250 |
| 251 void MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications( | |
| 252 int render_frame_id, | |
| 253 const GURL& security_origin) { | |
| 254 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 255 DVLOG(1) | |
| 256 << "MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications(" | |
| 257 << render_frame_id << ", " << security_origin.spec() << ")"; | |
| 258 DCHECK( | |
| 259 std::find_if( | |
| 260 device_change_subscribers_.begin(), device_change_subscribers_.end(), | |
| 261 [render_frame_id](const DeviceChangeSubscriberInfo& subscriber_info) { | |
| 262 return subscriber_info.render_frame_id == render_frame_id; | |
| 263 }) == device_change_subscribers_.end()); | |
| 264 | |
| 265 if (device_change_subscribers_.empty()) | |
| 266 media_stream_manager_->SubscribeToDeviceChangeNotifications(this); | |
| 267 | |
| 268 device_change_subscribers_.push_back({render_frame_id, security_origin}); | |
| 269 } | |
| 270 | |
| 271 void MediaStreamDispatcherHost::OnCancelDeviceChangeNotifications( | |
| 272 int render_frame_id) { | |
| 273 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 274 DVLOG(1) << "MediaStreamDispatcherHost::CancelDeviceChangeNotifications(" | |
| 275 << render_frame_id << ")"; | |
| 276 auto it = std::find_if( | |
| 277 device_change_subscribers_.begin(), device_change_subscribers_.end(), | |
| 278 [render_frame_id](const DeviceChangeSubscriberInfo& subscriber_info) { | |
| 279 return subscriber_info.render_frame_id == render_frame_id; | |
| 280 }); | |
| 281 DCHECK(it != device_change_subscribers_.end()); | |
| 282 device_change_subscribers_.erase(it); | |
|
palmer
2016/04/26 23:25:02
Wrap this in
if (it != device_change_subscriber
Guido Urdaneta
2016/05/01 11:08:39
Done. Also in two other places that use the same i
| |
| 283 if (device_change_subscribers_.empty()) | |
| 284 media_stream_manager_->CancelDeviceChangeNotifications(this); | |
| 285 } | |
| 286 | |
| 230 bool MediaStreamDispatcherHost::IsURLAllowed(const GURL& url) { | 287 bool MediaStreamDispatcherHost::IsURLAllowed(const GURL& url) { |
| 231 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | 288 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( |
| 232 render_process_id_, url)) { | 289 render_process_id_, url)) { |
| 233 LOG(ERROR) << "MSDH: Renderer requested a URL it's not allowed to use."; | 290 LOG(ERROR) << "MSDH: Renderer requested a URL it's not allowed to use."; |
| 234 return false; | 291 return false; |
| 235 } | 292 } |
| 236 | 293 |
| 237 return true; | 294 return true; |
| 238 } | 295 } |
| 239 | 296 |
| 297 std::unique_ptr<MediaStreamUIProxy> | |
| 298 MediaStreamDispatcherHost::CreateMediaStreamUIProxy() { | |
| 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 300 if (use_fake_ui_ || | |
| 301 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 302 switches::kUseFakeUIForMediaStream)) { | |
| 303 return base::WrapUnique(new FakeMediaStreamUIProxy()); | |
| 304 } | |
| 305 | |
| 306 return MediaStreamUIProxy::Create(); | |
| 307 } | |
| 308 | |
| 309 void MediaStreamDispatcherHost::HandleCheckAccessResponse( | |
| 310 std::unique_ptr<MediaStreamUIProxy> ui_proxy, | |
| 311 int render_frame_id, | |
| 312 bool have_access) { | |
| 313 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 314 if (have_access) | |
| 315 Send(new MediaStreamMsg_DevicesChanged(render_frame_id)); | |
| 316 } | |
| 317 | |
| 240 } // namespace content | 318 } // namespace content |
| OLD | NEW |