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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h"
7 #include "content/browser/browser_main_loop.h"
8 #include "content/browser/child_process_security_policy_impl.h"
9 #include "content/common/media/media_stream_messages.h"
10 #include "content/common/media/media_stream_options.h"
11 #include "content/public/browser/render_process_host.h"
16 MediaStreamDispatcherHost::MediaStreamDispatcherHost(
17 int render_process_id
,
18 const ResourceContext::SaltCallback
& salt_callback
,
19 MediaStreamManager
* media_stream_manager
)
20 : BrowserMessageFilter(MediaStreamMsgStart
),
21 render_process_id_(render_process_id
),
22 salt_callback_(salt_callback
),
23 media_stream_manager_(media_stream_manager
) {
26 void MediaStreamDispatcherHost::StreamGenerated(
29 const std::string
& label
,
30 const StreamDeviceInfoArray
& audio_devices
,
31 const StreamDeviceInfoArray
& video_devices
) {
32 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
33 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated("
34 << ", {label = " << label
<< "})";
36 Send(new MediaStreamMsg_StreamGenerated(
37 render_view_id
, page_request_id
, label
, audio_devices
,
41 void MediaStreamDispatcherHost::StreamGenerationFailed(
44 content::MediaStreamRequestResult result
) {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
46 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerationFailed("
47 << ", {page_request_id = " << page_request_id
<< "}"
48 << ", { result= " << result
<< "})";
51 Send(new MediaStreamMsg_StreamGenerationFailed(render_view_id
,
56 void MediaStreamDispatcherHost::DeviceStopped(int render_view_id
,
57 const std::string
& label
,
58 const StreamDeviceInfo
& device
) {
59 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
60 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped("
61 << "{label = " << label
<< "}, "
62 << "{type = " << device
.device
.type
<< "}, "
63 << "{device_id = " << device
.device
.id
<< "})";
65 Send(new MediaStreamMsg_DeviceStopped(render_view_id
, label
, device
));
68 void MediaStreamDispatcherHost::DevicesEnumerated(
71 const std::string
& label
,
72 const StreamDeviceInfoArray
& devices
) {
73 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
74 DVLOG(1) << "MediaStreamDispatcherHost::DevicesEnumerated("
75 << ", {page_request_id = " << page_request_id
<< "})";
77 Send(new MediaStreamMsg_DevicesEnumerated(render_view_id
, page_request_id
,
81 void MediaStreamDispatcherHost::DeviceOpened(
84 const std::string
& label
,
85 const StreamDeviceInfo
& video_device
) {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
87 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened("
88 << ", {page_request_id = " << page_request_id
<< "})";
90 Send(new MediaStreamMsg_DeviceOpened(
91 render_view_id
, page_request_id
, label
, video_device
));
94 bool MediaStreamDispatcherHost::OnMessageReceived(
95 const IPC::Message
& message
, bool* message_was_ok
) {
97 IPC_BEGIN_MESSAGE_MAP_EX(MediaStreamDispatcherHost
, message
, *message_was_ok
)
98 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream
, OnGenerateStream
)
99 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream
,
100 OnCancelGenerateStream
)
101 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice
,
103 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices
,
105 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices
,
106 OnCancelEnumerateDevices
)
107 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice
,
109 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice
,
111 IPC_MESSAGE_UNHANDLED(handled
= false)
112 IPC_END_MESSAGE_MAP_EX()
116 void MediaStreamDispatcherHost::OnChannelClosing() {
117 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing";
119 // Since the IPC channel is gone, close all requesting/requested streams.
120 media_stream_manager_
->CancelAllRequests(render_process_id_
);
123 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() {
126 void MediaStreamDispatcherHost::OnGenerateStream(
129 const StreamOptions
& components
,
130 const GURL
& security_origin
,
132 DVLOG(1) << "MediaStreamDispatcherHost::OnGenerateStream("
133 << render_view_id
<< ", "
134 << page_request_id
<< ", ["
135 << " audio:" << components
.audio_requested
136 << " video:" << components
.video_requested
138 << security_origin
.spec()
139 << ", " << user_gesture
<< ")";
141 if (!IsURLAllowed(security_origin
))
144 media_stream_manager_
->GenerateStream(
145 this, render_process_id_
, render_view_id
, salt_callback_
,
146 page_request_id
, components
, security_origin
, user_gesture
);
149 void MediaStreamDispatcherHost::OnCancelGenerateStream(int render_view_id
,
150 int page_request_id
) {
151 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelGenerateStream("
152 << render_view_id
<< ", "
153 << page_request_id
<< ")";
154 media_stream_manager_
->CancelRequest(render_process_id_
, render_view_id
,
158 void MediaStreamDispatcherHost::OnStopStreamDevice(
160 const std::string
& device_id
) {
161 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice("
162 << render_view_id
<< ", "
164 media_stream_manager_
->StopStreamDevice(render_process_id_
, render_view_id
,
168 void MediaStreamDispatcherHost::OnEnumerateDevices(
171 MediaStreamType type
,
172 const GURL
& security_origin
) {
173 DVLOG(1) << "MediaStreamDispatcherHost::OnEnumerateDevices("
174 << render_view_id
<< ", "
175 << page_request_id
<< ", "
177 << security_origin
.spec() << ")";
179 if (!IsURLAllowed(security_origin
))
182 media_stream_manager_
->EnumerateDevices(
183 this, render_process_id_
, render_view_id
, salt_callback_
,
184 page_request_id
, type
, security_origin
);
187 void MediaStreamDispatcherHost::OnCancelEnumerateDevices(
189 int page_request_id
) {
190 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelEnumerateDevices("
191 << render_view_id
<< ", "
192 << page_request_id
<< ")";
193 media_stream_manager_
->CancelRequest(render_process_id_
, render_view_id
,
197 void MediaStreamDispatcherHost::OnOpenDevice(
200 const std::string
& device_id
,
201 MediaStreamType type
,
202 const GURL
& security_origin
) {
203 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice("
204 << render_view_id
<< ", "
205 << page_request_id
<< ", device_id: "
206 << device_id
.c_str() << ", type: "
208 << security_origin
.spec() << ")";
210 if (!IsURLAllowed(security_origin
))
213 media_stream_manager_
->OpenDevice(
214 this, render_process_id_
, render_view_id
, salt_callback_
,
215 page_request_id
, device_id
, type
, security_origin
);
218 void MediaStreamDispatcherHost::OnCloseDevice(
220 const std::string
& label
) {
221 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice("
222 << render_view_id
<< ", "
225 media_stream_manager_
->CancelRequest(label
);
228 bool MediaStreamDispatcherHost::IsURLAllowed(const GURL
& url
) {
229 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
230 render_process_id_
, url
)) {
231 LOG(ERROR
) << "MSDH: Renderer requested a URL it's not allowed to use.";
238 } // namespace content