Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / webrtc_logging_private / webrtc_logging_private_api.cc
blob04b908f586f0104bfff92a09657039aa792beabb
1 // Copyright 2013 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 "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_private_api.h"
7 #include "base/hash.h"
8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/supports_user_data.h"
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
12 #include "chrome/browser/extensions/extension_tab_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/common/error_utils.h"
22 namespace extensions {
24 using api::webrtc_logging_private::MetaDataEntry;
25 using api::webrtc_logging_private::RequestInfo;
26 using content::BrowserThread;
28 namespace Discard = api::webrtc_logging_private::Discard;
29 namespace SetMetaData = api::webrtc_logging_private::SetMetaData;
30 namespace SetUploadOnRenderClose =
31 api::webrtc_logging_private::SetUploadOnRenderClose;
32 namespace Start = api::webrtc_logging_private::Start;
33 namespace StartRtpDump = api::webrtc_logging_private::StartRtpDump;
34 namespace Stop = api::webrtc_logging_private::Stop;
35 namespace StopRtpDump = api::webrtc_logging_private::StopRtpDump;
36 namespace Store = api::webrtc_logging_private::Store;
37 namespace Upload = api::webrtc_logging_private::Upload;
38 namespace UploadStored = api::webrtc_logging_private::UploadStored;
40 namespace {
41 std::string HashIdWithOrigin(const std::string& security_origin,
42 const std::string& log_id) {
43 return base::UintToString(base::Hash(security_origin + log_id));
45 } // namespace
47 content::RenderProcessHost* WebrtcLoggingPrivateFunction::RphFromRequest(
48 const RequestInfo& request, const std::string& security_origin) {
49 // If |guest_process_id| is defined, directly use this id to find the
50 // corresponding RenderProcessHost.
51 if (request.guest_process_id.get())
52 return content::RenderProcessHost::FromID(*request.guest_process_id.get());
54 // Otherwise, use the |tab_id|. If there's no |tab_id| and no
55 // |guest_process_id|, we can't look up the RenderProcessHost.
56 if (!request.tab_id.get())
57 return NULL;
59 int tab_id = *request.tab_id.get();
60 content::WebContents* contents = NULL;
61 if (!ExtensionTabUtil::GetTabById(
62 tab_id, GetProfile(), true, NULL, NULL, &contents, NULL)) {
63 error_ = extensions::ErrorUtils::FormatErrorMessage(
64 extensions::tabs_constants::kTabNotFoundError,
65 base::IntToString(tab_id));
66 return NULL;
68 if (!contents) {
69 error_ = extensions::ErrorUtils::FormatErrorMessage(
70 "Web contents for tab not found",
71 base::IntToString(tab_id));
72 return NULL;
74 if (contents->GetURL().GetOrigin().spec() != security_origin) {
75 error_ = extensions::ErrorUtils::FormatErrorMessage(
76 "Invalid security origin",
77 base::IntToString(tab_id));
78 return NULL;
80 return contents->GetRenderProcessHost();
83 scoped_refptr<WebRtcLoggingHandlerHost>
84 WebrtcLoggingPrivateFunction::LoggingHandlerFromRequest(
85 const api::webrtc_logging_private::RequestInfo& request,
86 const std::string& security_origin) {
87 content::RenderProcessHost* host = RphFromRequest(request, security_origin);
88 if (!host)
89 return nullptr;
91 return base::UserDataAdapter<WebRtcLoggingHandlerHost>::Get(host, host);
94 scoped_refptr<WebRtcLoggingHandlerHost>
95 WebrtcLoggingPrivateFunctionWithGenericCallback::PrepareTask(
96 const RequestInfo& request, const std::string& security_origin,
97 WebRtcLoggingHandlerHost::GenericDoneCallback* callback) {
98 *callback = base::Bind(
99 &WebrtcLoggingPrivateFunctionWithGenericCallback::FireCallback, this);
100 return LoggingHandlerFromRequest(request, security_origin);
103 void WebrtcLoggingPrivateFunctionWithGenericCallback::FireCallback(
104 bool success, const std::string& error_message) {
105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
106 if (!success)
107 SetError(error_message);
108 SendResponse(success);
111 void WebrtcLoggingPrivateFunctionWithUploadCallback::FireCallback(
112 bool success, const std::string& report_id,
113 const std::string& error_message) {
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
115 if (success) {
116 api::webrtc_logging_private::UploadResult result;
117 result.report_id = report_id;
118 SetResult(result.ToValue().release());
119 } else {
120 SetError(error_message);
122 SendResponse(success);
125 bool WebrtcLoggingPrivateSetMetaDataFunction::RunAsync() {
126 scoped_ptr<SetMetaData::Params> params(SetMetaData::Params::Create(*args_));
127 EXTENSION_FUNCTION_VALIDATE(params.get());
129 WebRtcLoggingHandlerHost::GenericDoneCallback callback;
130 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host =
131 PrepareTask(params->request, params->security_origin, &callback);
132 if (!webrtc_logging_handler_host.get())
133 return false;
135 scoped_ptr<MetaDataMap> meta_data(new MetaDataMap());
136 for (const linked_ptr<MetaDataEntry>& entry : params->meta_data)
137 (*meta_data.get())[entry->key] = entry->value;
139 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
140 &WebRtcLoggingHandlerHost::SetMetaData, webrtc_logging_handler_host,
141 base::Passed(&meta_data), callback));
143 return true;
146 bool WebrtcLoggingPrivateStartFunction::RunAsync() {
147 scoped_ptr<Start::Params> params(Start::Params::Create(*args_));
148 EXTENSION_FUNCTION_VALIDATE(params.get());
150 WebRtcLoggingHandlerHost::GenericDoneCallback callback;
151 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host =
152 PrepareTask(params->request, params->security_origin, &callback);
153 if (!webrtc_logging_handler_host.get())
154 return false;
156 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
157 &WebRtcLoggingHandlerHost::StartLogging, webrtc_logging_handler_host,
158 callback));
160 return true;
163 bool WebrtcLoggingPrivateSetUploadOnRenderCloseFunction::RunAsync() {
164 scoped_ptr<SetUploadOnRenderClose::Params> params(
165 SetUploadOnRenderClose::Params::Create(*args_));
166 EXTENSION_FUNCTION_VALIDATE(params.get());
168 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host(
169 LoggingHandlerFromRequest(params->request, params->security_origin));
170 if (!webrtc_logging_handler_host.get())
171 return false;
173 webrtc_logging_handler_host->set_upload_log_on_render_close(
174 params->should_upload);
176 return true;
179 bool WebrtcLoggingPrivateStopFunction::RunAsync() {
180 scoped_ptr<Stop::Params> params(Stop::Params::Create(*args_));
181 EXTENSION_FUNCTION_VALIDATE(params.get());
183 WebRtcLoggingHandlerHost::GenericDoneCallback callback;
184 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host =
185 PrepareTask(params->request, params->security_origin, &callback);
186 if (!webrtc_logging_handler_host.get())
187 return false;
189 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
190 &WebRtcLoggingHandlerHost::StopLogging, webrtc_logging_handler_host,
191 callback));
193 return true;
196 bool WebrtcLoggingPrivateStoreFunction::RunAsync() {
197 scoped_ptr<Store::Params> params(Store::Params::Create(*args_));
198 EXTENSION_FUNCTION_VALIDATE(params.get());
200 WebRtcLoggingHandlerHost::GenericDoneCallback callback;
201 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host =
202 PrepareTask(params->request, params->security_origin, &callback);
203 if (!webrtc_logging_handler_host.get())
204 return false;
206 const std::string local_log_id(HashIdWithOrigin(params->security_origin,
207 params->log_id));
209 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
210 &WebRtcLoggingHandlerHost::StoreLog,
211 webrtc_logging_handler_host, local_log_id, callback));
213 return true;
216 bool WebrtcLoggingPrivateUploadStoredFunction::RunAsync() {
217 scoped_ptr<UploadStored::Params> params(UploadStored::Params::Create(*args_));
218 EXTENSION_FUNCTION_VALIDATE(params.get());
220 scoped_refptr<WebRtcLoggingHandlerHost> logging_handler(
221 LoggingHandlerFromRequest(params->request, params->security_origin));
222 if (!logging_handler.get())
223 return false;
225 WebRtcLoggingHandlerHost::UploadDoneCallback callback = base::Bind(
226 &WebrtcLoggingPrivateUploadStoredFunction::FireCallback, this);
228 const std::string local_log_id(HashIdWithOrigin(params->security_origin,
229 params->log_id));
231 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
232 &WebRtcLoggingHandlerHost::UploadStoredLog, logging_handler, local_log_id,
233 callback));
235 return true;
238 bool WebrtcLoggingPrivateUploadFunction::RunAsync() {
239 scoped_ptr<Upload::Params> params(Upload::Params::Create(*args_));
240 EXTENSION_FUNCTION_VALIDATE(params.get());
242 scoped_refptr<WebRtcLoggingHandlerHost> logging_handler(
243 LoggingHandlerFromRequest(params->request, params->security_origin));
244 if (!logging_handler.get())
245 return false;
247 WebRtcLoggingHandlerHost::UploadDoneCallback callback = base::Bind(
248 &WebrtcLoggingPrivateUploadFunction::FireCallback, this);
250 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
251 &WebRtcLoggingHandlerHost::UploadLog, logging_handler, callback));
253 return true;
256 bool WebrtcLoggingPrivateDiscardFunction::RunAsync() {
257 scoped_ptr<Discard::Params> params(Discard::Params::Create(*args_));
258 EXTENSION_FUNCTION_VALIDATE(params.get());
260 WebRtcLoggingHandlerHost::GenericDoneCallback callback;
261 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host =
262 PrepareTask(params->request, params->security_origin, &callback);
263 if (!webrtc_logging_handler_host.get())
264 return false;
266 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
267 &WebRtcLoggingHandlerHost::DiscardLog, webrtc_logging_handler_host,
268 callback));
270 return true;
273 bool WebrtcLoggingPrivateStartRtpDumpFunction::RunAsync() {
274 scoped_ptr<StartRtpDump::Params> params(StartRtpDump::Params::Create(*args_));
275 EXTENSION_FUNCTION_VALIDATE(params.get());
277 if (!params->incoming && !params->outgoing) {
278 FireCallback(false, "Either incoming or outgoing must be true.");
279 return true;
282 RtpDumpType type =
283 (params->incoming && params->outgoing)
284 ? RTP_DUMP_BOTH
285 : (params->incoming ? RTP_DUMP_INCOMING : RTP_DUMP_OUTGOING);
287 content::RenderProcessHost* host =
288 RphFromRequest(params->request, params->security_origin);
289 if (!host)
290 return false;
292 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host(
293 base::UserDataAdapter<WebRtcLoggingHandlerHost>::Get(host, host));
295 WebRtcLoggingHandlerHost::GenericDoneCallback callback = base::Bind(
296 &WebrtcLoggingPrivateStartRtpDumpFunction::FireCallback, this);
298 // This call cannot fail.
299 content::RenderProcessHost::WebRtcStopRtpDumpCallback stop_callback =
300 host->StartRtpDump(params->incoming,
301 params->outgoing,
302 base::Bind(&WebRtcLoggingHandlerHost::OnRtpPacket,
303 webrtc_logging_handler_host));
305 BrowserThread::PostTask(BrowserThread::IO,
306 FROM_HERE,
307 base::Bind(&WebRtcLoggingHandlerHost::StartRtpDump,
308 webrtc_logging_handler_host,
309 type,
310 callback,
311 stop_callback));
312 return true;
315 bool WebrtcLoggingPrivateStopRtpDumpFunction::RunAsync() {
316 scoped_ptr<StopRtpDump::Params> params(StopRtpDump::Params::Create(*args_));
317 EXTENSION_FUNCTION_VALIDATE(params.get());
319 if (!params->incoming && !params->outgoing) {
320 FireCallback(false, "Either incoming or outgoing must be true.");
321 return true;
324 RtpDumpType type =
325 (params->incoming && params->outgoing)
326 ? RTP_DUMP_BOTH
327 : (params->incoming ? RTP_DUMP_INCOMING : RTP_DUMP_OUTGOING);
329 content::RenderProcessHost* host =
330 RphFromRequest(params->request, params->security_origin);
331 if (!host)
332 return false;
334 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host(
335 base::UserDataAdapter<WebRtcLoggingHandlerHost>::Get(host, host));
337 WebRtcLoggingHandlerHost::GenericDoneCallback callback = base::Bind(
338 &WebrtcLoggingPrivateStopRtpDumpFunction::FireCallback, this);
340 BrowserThread::PostTask(BrowserThread::IO,
341 FROM_HERE,
342 base::Bind(&WebRtcLoggingHandlerHost::StopRtpDump,
343 webrtc_logging_handler_host,
344 type,
345 callback));
346 return true;
349 } // namespace extensions