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 ResourceContext
* resource_context
)
21 : BrowserMessageFilter(MediaStreamMsgStart
),
22 render_process_id_(render_process_id
),
23 salt_callback_(salt_callback
),
24 media_stream_manager_(media_stream_manager
),
25 resource_context_(resource_context
) {
28 void MediaStreamDispatcherHost::StreamGenerated(
31 const std::string
& label
,
32 const StreamDeviceInfoArray
& audio_devices
,
33 const StreamDeviceInfoArray
& video_devices
) {
34 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
35 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerated("
36 << ", {label = " << label
<< "})";
38 Send(new MediaStreamMsg_StreamGenerated(
39 render_view_id
, page_request_id
, label
, audio_devices
,
43 void MediaStreamDispatcherHost::StreamGenerationFailed(
46 content::MediaStreamRequestResult result
) {
47 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
48 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerationFailed("
49 << ", {page_request_id = " << page_request_id
<< "}"
50 << ", { result= " << result
<< "})";
53 Send(new MediaStreamMsg_StreamGenerationFailed(render_view_id
,
58 void MediaStreamDispatcherHost::DeviceStopped(int render_view_id
,
59 const std::string
& label
,
60 const StreamDeviceInfo
& device
) {
61 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
62 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped("
63 << "{label = " << label
<< "}, "
64 << "{type = " << device
.device
.type
<< "}, "
65 << "{device_id = " << device
.device
.id
<< "})";
67 Send(new MediaStreamMsg_DeviceStopped(render_view_id
, label
, device
));
70 void MediaStreamDispatcherHost::DevicesEnumerated(
73 const std::string
& label
,
74 const StreamDeviceInfoArray
& devices
) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
76 DVLOG(1) << "MediaStreamDispatcherHost::DevicesEnumerated("
77 << ", {page_request_id = " << page_request_id
<< "})";
79 Send(new MediaStreamMsg_DevicesEnumerated(render_view_id
, page_request_id
,
83 void MediaStreamDispatcherHost::DeviceOpened(
86 const std::string
& label
,
87 const StreamDeviceInfo
& video_device
) {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
89 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened("
90 << ", {page_request_id = " << page_request_id
<< "})";
92 Send(new MediaStreamMsg_DeviceOpened(
93 render_view_id
, page_request_id
, label
, video_device
));
96 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message
& message
) {
98 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost
, message
)
99 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream
, OnGenerateStream
)
100 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream
,
101 OnCancelGenerateStream
)
102 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice
,
104 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices
,
106 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices
,
107 OnCancelEnumerateDevices
)
108 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice
,
110 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice
,
112 IPC_MESSAGE_UNHANDLED(handled
= false)
113 IPC_END_MESSAGE_MAP()
117 void MediaStreamDispatcherHost::OnChannelClosing() {
118 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing";
120 // Since the IPC sender is gone, close all requesting/requested streams.
121 media_stream_manager_
->CancelAllRequests(render_process_id_
);
124 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() {
127 void MediaStreamDispatcherHost::OnGenerateStream(
130 const StreamOptions
& components
,
131 const GURL
& security_origin
,
133 DVLOG(1) << "MediaStreamDispatcherHost::OnGenerateStream("
134 << render_view_id
<< ", "
135 << page_request_id
<< ", ["
136 << " audio:" << components
.audio_requested
137 << " video:" << components
.video_requested
139 << security_origin
.spec()
140 << ", " << user_gesture
<< ")";
142 if (!IsURLAllowed(security_origin
))
145 media_stream_manager_
->GenerateStream(
146 this, render_process_id_
, render_view_id
, salt_callback_
,
147 page_request_id
, components
, security_origin
, user_gesture
);
150 void MediaStreamDispatcherHost::OnCancelGenerateStream(int render_view_id
,
151 int page_request_id
) {
152 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelGenerateStream("
153 << render_view_id
<< ", "
154 << page_request_id
<< ")";
155 media_stream_manager_
->CancelRequest(render_process_id_
, render_view_id
,
159 void MediaStreamDispatcherHost::OnStopStreamDevice(
161 const std::string
& device_id
) {
162 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice("
163 << render_view_id
<< ", "
165 media_stream_manager_
->StopStreamDevice(render_process_id_
, render_view_id
,
169 void MediaStreamDispatcherHost::OnEnumerateDevices(
172 MediaStreamType type
,
173 const GURL
& security_origin
,
174 bool hide_labels_if_no_access
) {
175 DVLOG(1) << "MediaStreamDispatcherHost::OnEnumerateDevices("
176 << render_view_id
<< ", "
177 << page_request_id
<< ", "
179 << security_origin
.spec() << ")";
181 if (!IsURLAllowed(security_origin
))
184 DCHECK(type
== MEDIA_DEVICE_AUDIO_CAPTURE
||
185 type
== MEDIA_DEVICE_VIDEO_CAPTURE
||
186 type
== MEDIA_DEVICE_AUDIO_OUTPUT
);
187 bool have_permission
= true;
188 if (hide_labels_if_no_access
) {
189 bool audio_type
= type
== MEDIA_DEVICE_AUDIO_CAPTURE
||
190 type
== MEDIA_DEVICE_AUDIO_OUTPUT
;
191 have_permission
= audio_type
?
192 resource_context_
->AllowMicAccess(security_origin
) :
193 resource_context_
->AllowCameraAccess(security_origin
);
196 media_stream_manager_
->EnumerateDevices(
197 this, render_process_id_
, render_view_id
, salt_callback_
,
198 page_request_id
, type
, security_origin
, have_permission
);
201 void MediaStreamDispatcherHost::OnCancelEnumerateDevices(
203 int page_request_id
) {
204 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelEnumerateDevices("
205 << render_view_id
<< ", "
206 << page_request_id
<< ")";
207 media_stream_manager_
->CancelRequest(render_process_id_
, render_view_id
,
211 void MediaStreamDispatcherHost::OnOpenDevice(
214 const std::string
& device_id
,
215 MediaStreamType type
,
216 const GURL
& security_origin
) {
217 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice("
218 << render_view_id
<< ", "
219 << page_request_id
<< ", device_id: "
220 << device_id
.c_str() << ", type: "
222 << security_origin
.spec() << ")";
224 if (!IsURLAllowed(security_origin
))
227 media_stream_manager_
->OpenDevice(
228 this, render_process_id_
, render_view_id
, salt_callback_
,
229 page_request_id
, device_id
, type
, security_origin
);
232 void MediaStreamDispatcherHost::OnCloseDevice(
234 const std::string
& label
) {
235 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice("
236 << render_view_id
<< ", "
239 media_stream_manager_
->CancelRequest(label
);
242 bool MediaStreamDispatcherHost::IsURLAllowed(const GURL
& url
) {
243 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
244 render_process_id_
, url
)) {
245 LOG(ERROR
) << "MSDH: Renderer requested a URL it's not allowed to use.";
252 } // namespace content