| 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> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "content/browser/bad_message.h" | |
| 12 #include "content/browser/browser_main_loop.h" | 9 #include "content/browser/browser_main_loop.h" |
| 13 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | |
| 14 #include "content/common/media/media_stream_messages.h" | 10 #include "content/common/media/media_stream_messages.h" |
| 15 #include "content/common/media/media_stream_options.h" | 11 #include "content/common/media/media_stream_options.h" |
| 16 #include "content/public/browser/render_process_host.h" | 12 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 18 #include "url/gurl.h" | 14 #include "url/origin.h" |
| 19 | 15 |
| 20 namespace content { | 16 namespace content { |
| 21 | 17 |
| 22 MediaStreamDispatcherHost::MediaStreamDispatcherHost( | 18 MediaStreamDispatcherHost::MediaStreamDispatcherHost( |
| 23 int render_process_id, | 19 int render_process_id, |
| 24 const std::string& salt, | 20 const std::string& salt, |
| 25 MediaStreamManager* media_stream_manager, | 21 MediaStreamManager* media_stream_manager) |
| 26 bool use_fake_ui) | |
| 27 : BrowserMessageFilter(MediaStreamMsgStart), | 22 : BrowserMessageFilter(MediaStreamMsgStart), |
| 28 render_process_id_(render_process_id), | 23 render_process_id_(render_process_id), |
| 29 salt_(salt), | 24 salt_(salt), |
| 30 media_stream_manager_(media_stream_manager), | 25 media_stream_manager_(media_stream_manager) {} |
| 31 use_fake_ui_(use_fake_ui), | |
| 32 weak_factory_(this) {} | |
| 33 | 26 |
| 34 void MediaStreamDispatcherHost::StreamGenerated( | 27 void MediaStreamDispatcherHost::StreamGenerated( |
| 35 int render_frame_id, | 28 int render_frame_id, |
| 36 int page_request_id, | 29 int page_request_id, |
| 37 const std::string& label, | 30 const std::string& label, |
| 38 const StreamDeviceInfoArray& audio_devices, | 31 const StreamDeviceInfoArray& audio_devices, |
| 39 const StreamDeviceInfoArray& video_devices) { | 32 const StreamDeviceInfoArray& video_devices) { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 33 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 41 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated(" | 34 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated(" |
| 42 << ", {label = " << label << "})"; | 35 << ", {label = " << label << "})"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 64 const StreamDeviceInfo& device) { | 57 const StreamDeviceInfo& device) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 66 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped(" | 59 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped(" |
| 67 << "{label = " << label << "}, " | 60 << "{label = " << label << "}, " |
| 68 << "{type = " << device.device.type << "}, " | 61 << "{type = " << device.device.type << "}, " |
| 69 << "{device_id = " << device.device.id << "})"; | 62 << "{device_id = " << device.device.id << "})"; |
| 70 | 63 |
| 71 Send(new MediaStreamMsg_DeviceStopped(render_frame_id, label, device)); | 64 Send(new MediaStreamMsg_DeviceStopped(render_frame_id, label, device)); |
| 72 } | 65 } |
| 73 | 66 |
| 74 void MediaStreamDispatcherHost::DevicesEnumerated( | |
| 75 int render_frame_id, | |
| 76 int page_request_id, | |
| 77 const std::string& label, | |
| 78 const StreamDeviceInfoArray& devices) { | |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 80 DVLOG(1) << "MediaStreamDispatcherHost::DevicesEnumerated(" | |
| 81 << ", {page_request_id = " << page_request_id << "})"; | |
| 82 | |
| 83 Send(new MediaStreamMsg_DevicesEnumerated(render_frame_id, page_request_id, | |
| 84 devices)); | |
| 85 } | |
| 86 | |
| 87 void MediaStreamDispatcherHost::DeviceOpened( | 67 void MediaStreamDispatcherHost::DeviceOpened( |
| 88 int render_frame_id, | 68 int render_frame_id, |
| 89 int page_request_id, | 69 int page_request_id, |
| 90 const std::string& label, | 70 const std::string& label, |
| 91 const StreamDeviceInfo& video_device) { | 71 const StreamDeviceInfo& video_device) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened(" | 73 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened(" |
| 94 << ", {page_request_id = " << page_request_id << "})"; | 74 << ", {page_request_id = " << page_request_id << "})"; |
| 95 | 75 |
| 96 Send(new MediaStreamMsg_DeviceOpened( | 76 Send(new MediaStreamMsg_DeviceOpened( |
| 97 render_frame_id, page_request_id, label, video_device)); | 77 render_frame_id, page_request_id, label, video_device)); |
| 98 } | 78 } |
| 99 | 79 |
| 100 void MediaStreamDispatcherHost::DevicesChanged(MediaStreamType type) { | |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 102 DVLOG(1) << "MediaStreamDispatcherHost::DevicesChanged(" | |
| 103 << "{type = " << type << "})"; | |
| 104 for (const auto& subscriber : device_change_subscribers_) { | |
| 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 } | |
| 114 } | |
| 115 | |
| 116 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message& message) { | 80 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message& message) { |
| 117 bool handled = true; | 81 bool handled = true; |
| 118 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost, message) | 82 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost, message) |
| 119 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream) | 83 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream) |
| 120 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream, | 84 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream, |
| 121 OnCancelGenerateStream) | 85 OnCancelGenerateStream) |
| 122 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice, | 86 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice, |
| 123 OnStopStreamDevice) | 87 OnStopStreamDevice) |
| 124 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices, | |
| 125 OnEnumerateDevices) | |
| 126 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices, | |
| 127 OnCancelEnumerateDevices) | |
| 128 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice, | 88 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice, |
| 129 OnOpenDevice) | 89 OnOpenDevice) |
| 130 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice, | 90 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice, |
| 131 OnCloseDevice) | 91 OnCloseDevice) |
| 132 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_SubscribeToDeviceChangeNotifications, | |
| 133 OnSubscribeToDeviceChangeNotifications) | |
| 134 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelDeviceChangeNotifications, | |
| 135 OnCancelDeviceChangeNotifications) | |
| 136 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_SetCapturingLinkSecured, | 92 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_SetCapturingLinkSecured, |
| 137 OnSetCapturingLinkSecured) | 93 OnSetCapturingLinkSecured) |
| 138 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
| 139 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
| 140 return handled; | 96 return handled; |
| 141 } | 97 } |
| 142 | 98 |
| 143 void MediaStreamDispatcherHost::OnChannelClosing() { | 99 void MediaStreamDispatcherHost::OnChannelClosing() { |
| 144 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing"; | 100 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing"; |
| 145 | 101 |
| 146 // Since the IPC sender is gone, close all requesting/requested streams. | 102 // Since the IPC sender is gone, close all requesting/requested streams. |
| 147 media_stream_manager_->CancelAllRequests(render_process_id_); | 103 media_stream_manager_->CancelAllRequests(render_process_id_); |
| 148 if (!device_change_subscribers_.empty()) | |
| 149 media_stream_manager_->CancelDeviceChangeNotifications(this); | |
| 150 } | 104 } |
| 151 | 105 |
| 152 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() { | 106 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() { |
| 153 } | 107 } |
| 154 | 108 |
| 155 void MediaStreamDispatcherHost::OnGenerateStream( | 109 void MediaStreamDispatcherHost::OnGenerateStream( |
| 156 int render_frame_id, | 110 int render_frame_id, |
| 157 int page_request_id, | 111 int page_request_id, |
| 158 const StreamControls& controls, | 112 const StreamControls& controls, |
| 159 const url::Origin& security_origin, | 113 const url::Origin& security_origin, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 184 void MediaStreamDispatcherHost::OnStopStreamDevice( | 138 void MediaStreamDispatcherHost::OnStopStreamDevice( |
| 185 int render_frame_id, | 139 int render_frame_id, |
| 186 const std::string& device_id) { | 140 const std::string& device_id) { |
| 187 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice(" | 141 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice(" |
| 188 << render_frame_id << ", " | 142 << render_frame_id << ", " |
| 189 << device_id << ")"; | 143 << device_id << ")"; |
| 190 media_stream_manager_->StopStreamDevice(render_process_id_, render_frame_id, | 144 media_stream_manager_->StopStreamDevice(render_process_id_, render_frame_id, |
| 191 device_id); | 145 device_id); |
| 192 } | 146 } |
| 193 | 147 |
| 194 void MediaStreamDispatcherHost::OnEnumerateDevices( | |
| 195 int render_frame_id, | |
| 196 int page_request_id, | |
| 197 MediaStreamType type, | |
| 198 const url::Origin& security_origin) { | |
| 199 DVLOG(1) << "MediaStreamDispatcherHost::OnEnumerateDevices(" | |
| 200 << render_frame_id << ", " << page_request_id << ", " << type << ", " | |
| 201 << security_origin << ")"; | |
| 202 | |
| 203 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, security_origin)) | |
| 204 return; | |
| 205 | |
| 206 media_stream_manager_->EnumerateDevices( | |
| 207 this, render_process_id_, render_frame_id, salt_, page_request_id, type, | |
| 208 security_origin); | |
| 209 } | |
| 210 | |
| 211 void MediaStreamDispatcherHost::OnCancelEnumerateDevices( | |
| 212 int render_frame_id, | |
| 213 int page_request_id) { | |
| 214 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelEnumerateDevices(" | |
| 215 << render_frame_id << ", " | |
| 216 << page_request_id << ")"; | |
| 217 media_stream_manager_->CancelRequest(render_process_id_, render_frame_id, | |
| 218 page_request_id); | |
| 219 } | |
| 220 | |
| 221 void MediaStreamDispatcherHost::OnOpenDevice( | 148 void MediaStreamDispatcherHost::OnOpenDevice( |
| 222 int render_frame_id, | 149 int render_frame_id, |
| 223 int page_request_id, | 150 int page_request_id, |
| 224 const std::string& device_id, | 151 const std::string& device_id, |
| 225 MediaStreamType type, | 152 MediaStreamType type, |
| 226 const url::Origin& security_origin) { | 153 const url::Origin& security_origin) { |
| 227 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice(" << render_frame_id | 154 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice(" << render_frame_id |
| 228 << ", " << page_request_id << ", device_id: " << device_id.c_str() | 155 << ", " << page_request_id << ", device_id: " << device_id.c_str() |
| 229 << ", type: " << type << ", " << security_origin << ")"; | 156 << ", type: " << type << ", " << security_origin << ")"; |
| 230 | 157 |
| 231 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, security_origin)) | 158 if (!MediaStreamManager::IsOriginAllowed(render_process_id_, security_origin)) |
| 232 return; | 159 return; |
| 233 | 160 |
| 234 media_stream_manager_->OpenDevice(this, render_process_id_, render_frame_id, | 161 media_stream_manager_->OpenDevice(this, render_process_id_, render_frame_id, |
| 235 salt_, page_request_id, device_id, type, | 162 salt_, page_request_id, device_id, type, |
| 236 security_origin); | 163 security_origin); |
| 237 } | 164 } |
| 238 | 165 |
| 239 void MediaStreamDispatcherHost::OnCloseDevice( | 166 void MediaStreamDispatcherHost::OnCloseDevice( |
| 240 int render_frame_id, | 167 int render_frame_id, |
| 241 const std::string& label) { | 168 const std::string& label) { |
| 242 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice(" | 169 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice(" |
| 243 << render_frame_id << ", " | 170 << render_frame_id << ", " |
| 244 << label << ")"; | 171 << label << ")"; |
| 245 | 172 |
| 246 media_stream_manager_->CancelRequest(label); | 173 media_stream_manager_->CancelRequest(label); |
| 247 } | 174 } |
| 248 | 175 |
| 249 void MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications( | |
| 250 int render_frame_id, | |
| 251 const url::Origin& security_origin) { | |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 253 DVLOG(1) | |
| 254 << "MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications(" | |
| 255 << render_frame_id << ", " << security_origin << ")"; | |
| 256 DCHECK( | |
| 257 std::find_if( | |
| 258 device_change_subscribers_.begin(), device_change_subscribers_.end(), | |
| 259 [render_frame_id](const DeviceChangeSubscriberInfo& subscriber_info) { | |
| 260 return subscriber_info.render_frame_id == render_frame_id; | |
| 261 }) == device_change_subscribers_.end()); | |
| 262 | |
| 263 if (device_change_subscribers_.empty()) | |
| 264 media_stream_manager_->SubscribeToDeviceChangeNotifications(this); | |
| 265 | |
| 266 device_change_subscribers_.push_back({render_frame_id, security_origin}); | |
| 267 } | |
| 268 | |
| 269 void MediaStreamDispatcherHost::OnCancelDeviceChangeNotifications( | |
| 270 int render_frame_id) { | |
| 271 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 272 DVLOG(1) << "MediaStreamDispatcherHost::CancelDeviceChangeNotifications(" | |
| 273 << render_frame_id << ")"; | |
| 274 auto it = std::find_if( | |
| 275 device_change_subscribers_.begin(), device_change_subscribers_.end(), | |
| 276 [render_frame_id](const DeviceChangeSubscriberInfo& subscriber_info) { | |
| 277 return subscriber_info.render_frame_id == render_frame_id; | |
| 278 }); | |
| 279 if (it == device_change_subscribers_.end()) { | |
| 280 bad_message::ReceivedBadMessage(this, bad_message::MSDH_INVALID_FRAME_ID); | |
| 281 return; | |
| 282 } | |
| 283 device_change_subscribers_.erase(it); | |
| 284 if (device_change_subscribers_.empty()) | |
| 285 media_stream_manager_->CancelDeviceChangeNotifications(this); | |
| 286 } | |
| 287 | |
| 288 std::unique_ptr<MediaStreamUIProxy> | |
| 289 MediaStreamDispatcherHost::CreateMediaStreamUIProxy() { | |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 291 if (use_fake_ui_ || | |
| 292 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 293 switches::kUseFakeUIForMediaStream)) { | |
| 294 return base::MakeUnique<FakeMediaStreamUIProxy>(); | |
| 295 } | |
| 296 | |
| 297 return MediaStreamUIProxy::Create(); | |
| 298 } | |
| 299 | |
| 300 void MediaStreamDispatcherHost::HandleCheckAccessResponse( | |
| 301 std::unique_ptr<MediaStreamUIProxy> ui_proxy, | |
| 302 int render_frame_id, | |
| 303 bool have_access) { | |
| 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 305 if (have_access) | |
| 306 Send(new MediaStreamMsg_DevicesChanged(render_frame_id)); | |
| 307 } | |
| 308 | |
| 309 void MediaStreamDispatcherHost::OnSetCapturingLinkSecured(int session_id, | 176 void MediaStreamDispatcherHost::OnSetCapturingLinkSecured(int session_id, |
| 310 MediaStreamType type, | 177 MediaStreamType type, |
| 311 bool is_secure) { | 178 bool is_secure) { |
| 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 179 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 313 | 180 |
| 314 media_stream_manager_->SetCapturingLinkSecured(render_process_id_, session_id, | 181 media_stream_manager_->SetCapturingLinkSecured(render_process_id_, session_id, |
| 315 type, is_secure); | 182 type, is_secure); |
| 316 } | 183 } |
| 317 | 184 |
| 318 } // namespace content | 185 } // namespace content |
| OLD | NEW |