Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / renderer_host / media / media_stream_dispatcher_host.cc
blob929cf198fb4b287c20301396d48d48e3288527ec
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"
12 #include "url/gurl.h"
14 namespace content {
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(
29 int render_frame_id,
30 int page_request_id,
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_frame_id, page_request_id, label, audio_devices, video_devices));
42 void MediaStreamDispatcherHost::StreamGenerationFailed(
43 int render_frame_id,
44 int page_request_id,
45 content::MediaStreamRequestResult result) {
46 DCHECK_CURRENTLY_ON(BrowserThread::IO);
47 DVLOG(1) << "MediaStreamDispatcherHost::StreamGenerationFailed("
48 << ", {page_request_id = " << page_request_id << "}"
49 << ", { result= " << result << "})";
52 Send(new MediaStreamMsg_StreamGenerationFailed(render_frame_id,
53 page_request_id,
54 result));
57 void MediaStreamDispatcherHost::DeviceStopped(int render_frame_id,
58 const std::string& label,
59 const StreamDeviceInfo& device) {
60 DCHECK_CURRENTLY_ON(BrowserThread::IO);
61 DVLOG(1) << "MediaStreamDispatcherHost::DeviceStopped("
62 << "{label = " << label << "}, "
63 << "{type = " << device.device.type << "}, "
64 << "{device_id = " << device.device.id << "})";
66 Send(new MediaStreamMsg_DeviceStopped(render_frame_id, label, device));
69 void MediaStreamDispatcherHost::DevicesEnumerated(
70 int render_frame_id,
71 int page_request_id,
72 const std::string& label,
73 const StreamDeviceInfoArray& devices) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO);
75 DVLOG(1) << "MediaStreamDispatcherHost::DevicesEnumerated("
76 << ", {page_request_id = " << page_request_id << "})";
78 Send(new MediaStreamMsg_DevicesEnumerated(render_frame_id, page_request_id,
79 devices));
82 void MediaStreamDispatcherHost::DeviceOpened(
83 int render_frame_id,
84 int page_request_id,
85 const std::string& label,
86 const StreamDeviceInfo& video_device) {
87 DCHECK_CURRENTLY_ON(BrowserThread::IO);
88 DVLOG(1) << "MediaStreamDispatcherHost::DeviceOpened("
89 << ", {page_request_id = " << page_request_id << "})";
91 Send(new MediaStreamMsg_DeviceOpened(
92 render_frame_id, page_request_id, label, video_device));
95 bool MediaStreamDispatcherHost::OnMessageReceived(const IPC::Message& message) {
96 bool handled = true;
97 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcherHost, message)
98 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_GenerateStream, OnGenerateStream)
99 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelGenerateStream,
100 OnCancelGenerateStream)
101 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_StopStreamDevice,
102 OnStopStreamDevice)
103 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_EnumerateDevices,
104 OnEnumerateDevices)
105 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CancelEnumerateDevices,
106 OnCancelEnumerateDevices)
107 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_OpenDevice,
108 OnOpenDevice)
109 IPC_MESSAGE_HANDLER(MediaStreamHostMsg_CloseDevice,
110 OnCloseDevice)
111 IPC_MESSAGE_UNHANDLED(handled = false)
112 IPC_END_MESSAGE_MAP()
113 return handled;
116 void MediaStreamDispatcherHost::OnChannelClosing() {
117 DVLOG(1) << "MediaStreamDispatcherHost::OnChannelClosing";
119 // Since the IPC sender is gone, close all requesting/requested streams.
120 media_stream_manager_->CancelAllRequests(render_process_id_);
123 MediaStreamDispatcherHost::~MediaStreamDispatcherHost() {
126 void MediaStreamDispatcherHost::OnGenerateStream(
127 int render_frame_id,
128 int page_request_id,
129 const StreamOptions& components,
130 const GURL& security_origin,
131 bool user_gesture) {
132 DVLOG(1) << "MediaStreamDispatcherHost::OnGenerateStream("
133 << render_frame_id << ", "
134 << page_request_id << ", ["
135 << " audio:" << components.audio_requested
136 << " video:" << components.video_requested
137 << " ], "
138 << security_origin.spec()
139 << ", " << user_gesture << ")";
141 if (!IsURLAllowed(security_origin))
142 return;
144 media_stream_manager_->GenerateStream(
145 this, render_process_id_, render_frame_id, salt_callback_,
146 page_request_id, components, security_origin, user_gesture);
149 void MediaStreamDispatcherHost::OnCancelGenerateStream(int render_frame_id,
150 int page_request_id) {
151 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelGenerateStream("
152 << render_frame_id << ", "
153 << page_request_id << ")";
154 media_stream_manager_->CancelRequest(render_process_id_, render_frame_id,
155 page_request_id);
158 void MediaStreamDispatcherHost::OnStopStreamDevice(
159 int render_frame_id,
160 const std::string& device_id) {
161 DVLOG(1) << "MediaStreamDispatcherHost::OnStopStreamDevice("
162 << render_frame_id << ", "
163 << device_id << ")";
164 media_stream_manager_->StopStreamDevice(render_process_id_, render_frame_id,
165 device_id);
168 void MediaStreamDispatcherHost::OnEnumerateDevices(
169 int render_frame_id,
170 int page_request_id,
171 MediaStreamType type,
172 const GURL& security_origin) {
173 DVLOG(1) << "MediaStreamDispatcherHost::OnEnumerateDevices("
174 << render_frame_id << ", "
175 << page_request_id << ", "
176 << type << ", "
177 << security_origin.spec() << ")";
179 if (!IsURLAllowed(security_origin))
180 return;
182 DCHECK(type == MEDIA_DEVICE_AUDIO_CAPTURE ||
183 type == MEDIA_DEVICE_VIDEO_CAPTURE ||
184 type == MEDIA_DEVICE_AUDIO_OUTPUT);
185 bool have_permission =
186 type == MEDIA_DEVICE_AUDIO_CAPTURE || type == MEDIA_DEVICE_AUDIO_OUTPUT ?
187 resource_context_->AllowMicAccess(security_origin) :
188 resource_context_->AllowCameraAccess(security_origin);
190 media_stream_manager_->EnumerateDevices(
191 this, render_process_id_, render_frame_id, salt_callback_,
192 page_request_id, type, security_origin, have_permission);
195 void MediaStreamDispatcherHost::OnCancelEnumerateDevices(
196 int render_frame_id,
197 int page_request_id) {
198 DVLOG(1) << "MediaStreamDispatcherHost::OnCancelEnumerateDevices("
199 << render_frame_id << ", "
200 << page_request_id << ")";
201 media_stream_manager_->CancelRequest(render_process_id_, render_frame_id,
202 page_request_id);
205 void MediaStreamDispatcherHost::OnOpenDevice(
206 int render_frame_id,
207 int page_request_id,
208 const std::string& device_id,
209 MediaStreamType type,
210 const GURL& security_origin) {
211 DVLOG(1) << "MediaStreamDispatcherHost::OnOpenDevice("
212 << render_frame_id << ", "
213 << page_request_id << ", device_id: "
214 << device_id.c_str() << ", type: "
215 << type << ", "
216 << security_origin.spec() << ")";
218 if (!IsURLAllowed(security_origin))
219 return;
221 media_stream_manager_->OpenDevice(
222 this, render_process_id_, render_frame_id, salt_callback_,
223 page_request_id, device_id, type, security_origin);
226 void MediaStreamDispatcherHost::OnCloseDevice(
227 int render_frame_id,
228 const std::string& label) {
229 DVLOG(1) << "MediaStreamDispatcherHost::OnCloseDevice("
230 << render_frame_id << ", "
231 << label << ")";
233 media_stream_manager_->CancelRequest(label);
236 bool MediaStreamDispatcherHost::IsURLAllowed(const GURL& url) {
237 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL(
238 render_process_id_, url)) {
239 LOG(ERROR) << "MSDH: Renderer requested a URL it's not allowed to use.";
240 return false;
243 return true;
246 } // namespace content