OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_pr ivate_api.h" | 5 #include "chrome/browser/extensions/api/webrtc_logging_private/webrtc_logging_pr ivate_api.h" |
6 | 6 |
7 #include "base/command_line.h" | |
7 #include "base/hash.h" | 8 #include "base/hash.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "base/supports_user_data.h" | 11 #include "base/supports_user_data.h" |
12 #include "chrome/browser/download/download_prefs.h" | |
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 13 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
12 #include "chrome/browser/extensions/extension_tab_util.h" | 14 #include "chrome/browser/extensions/extension_tab_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 #include "chrome/common/chrome_switches.h" | |
14 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/site_instance.h" | 19 #include "content/public/browser/site_instance.h" |
17 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
18 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 21 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
19 #include "extensions/browser/process_manager.h" | 22 #include "extensions/browser/process_manager.h" |
20 #include "extensions/common/error_utils.h" | 23 #include "extensions/common/error_utils.h" |
21 | 24 |
22 namespace extensions { | 25 namespace extensions { |
23 | 26 |
24 using api::webrtc_logging_private::MetaDataEntry; | 27 using api::webrtc_logging_private::MetaDataEntry; |
25 using api::webrtc_logging_private::RequestInfo; | 28 using api::webrtc_logging_private::RequestInfo; |
26 using content::BrowserThread; | 29 using content::BrowserThread; |
27 | 30 |
28 namespace Discard = api::webrtc_logging_private::Discard; | 31 namespace Discard = api::webrtc_logging_private::Discard; |
29 namespace SetMetaData = api::webrtc_logging_private::SetMetaData; | 32 namespace SetMetaData = api::webrtc_logging_private::SetMetaData; |
30 namespace SetUploadOnRenderClose = | 33 namespace SetUploadOnRenderClose = |
31 api::webrtc_logging_private::SetUploadOnRenderClose; | 34 api::webrtc_logging_private::SetUploadOnRenderClose; |
32 namespace Start = api::webrtc_logging_private::Start; | 35 namespace Start = api::webrtc_logging_private::Start; |
33 namespace StartRtpDump = api::webrtc_logging_private::StartRtpDump; | 36 namespace StartRtpDump = api::webrtc_logging_private::StartRtpDump; |
34 namespace Stop = api::webrtc_logging_private::Stop; | 37 namespace Stop = api::webrtc_logging_private::Stop; |
35 namespace StopRtpDump = api::webrtc_logging_private::StopRtpDump; | 38 namespace StopRtpDump = api::webrtc_logging_private::StopRtpDump; |
36 namespace Store = api::webrtc_logging_private::Store; | 39 namespace Store = api::webrtc_logging_private::Store; |
37 namespace Upload = api::webrtc_logging_private::Upload; | 40 namespace Upload = api::webrtc_logging_private::Upload; |
38 namespace UploadStored = api::webrtc_logging_private::UploadStored; | 41 namespace UploadStored = api::webrtc_logging_private::UploadStored; |
42 namespace StartAecDump = api::webrtc_logging_private::StartAecDump; | |
43 namespace StopAecDump = api::webrtc_logging_private::StopAecDump; | |
39 | 44 |
40 namespace { | 45 namespace { |
41 std::string HashIdWithOrigin(const std::string& security_origin, | 46 std::string HashIdWithOrigin(const std::string& security_origin, |
42 const std::string& log_id) { | 47 const std::string& log_id) { |
43 return base::UintToString(base::Hash(security_origin + log_id)); | 48 return base::UintToString(base::Hash(security_origin + log_id)); |
44 } | 49 } |
50 | |
51 // These global variables must be accessed on the UI thread. | |
52 uint64 current_aec_dump_id = 0; | |
Henrik Grunell
2015/12/18 12:34:21
uint64_t
Henrik Grunell
2015/12/18 12:34:21
Use g_ prefix for these two.
Guido Urdaneta
2015/12/18 18:38:57
Done, but moved to webrtc logging handler
Guido Urdaneta
2015/12/18 18:38:57
Not needed anymore
| |
53 bool is_dump_in_progress = false; | |
54 | |
55 // Returns a base prefix path to be used for AEC dumps. | |
56 base::FilePath GetAecDumpPrefixPath(Profile* profile, uint64 dump_id) { | |
57 static const char kAecDumpFilePrefix[] = "aecdump."; | |
58 DownloadPrefs download_prefs(profile); | |
59 base::FilePath dump_dir = download_prefs.GetDefaultDownloadDirectory(); | |
60 return dump_dir.Append(kAecDumpFilePrefix + base::Int64ToString(dump_id)); | |
61 } | |
62 | |
63 // Starts an AEC dump and save the files in the default download directory for | |
64 // |profile|, provided no dump is in progress. Returns a UTF8 string with the | |
65 // prefix path for the AEC dump files, or an empty string there is already a | |
66 // dump in progress. | |
67 std::string DoStartAecDump(Profile* profile) { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
69 if (is_dump_in_progress) | |
70 return std::string(); | |
71 | |
72 ++current_aec_dump_id; | |
73 is_dump_in_progress = true; | |
74 base::FilePath prefix_path = | |
75 GetAecDumpPrefixPath(profile, current_aec_dump_id); | |
Henrik Grunell
2015/12/18 12:34:21
current_aec_dump_id++ and remove increment above.
Henrik Grunell
2015/12/18 12:34:21
I assume the reason for using a incrementing count
Guido Urdaneta
2015/12/18 18:38:57
Done (with preincrement) in new version.
| |
76 for (content::RenderProcessHost::iterator i( | |
77 content::RenderProcessHost::AllHostsIterator()); | |
78 !i.IsAtEnd(); i.Advance()) { | |
79 i.GetCurrentValue()->EnableAudioDebugRecordings(prefix_path); | |
80 } | |
81 | |
82 return prefix_path.AsUTF8Unsafe(); | |
83 } | |
84 | |
85 // If an AEC dump is ongoing, stop the AEC dump if |dump_id| is its ID. | |
86 // |callback| is invoked with the prefix path of the dump identified with | |
87 // |dump_id|, and true/false if an AEC dump was actually stopped/not stopped. | |
88 void DoStopAecDump(Profile* profile, | |
89 uint64 dump_id, | |
90 base::Callback<void(const std::string&, bool)> callback) { | |
91 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
92 DCHECK_LE(dump_id, current_aec_dump_id); | |
93 std::string prefix_path = | |
94 GetAecDumpPrefixPath(profile, dump_id).AsUTF8Unsafe(); | |
95 | |
96 // Prevent an old posted StopAecDump() call to stop a newer dump. | |
97 // This could happen in a sequence like: | |
98 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. | |
99 // Stop(); // Manually stop dump 1 before 10 seconds; | |
100 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | |
101 if (dump_id < current_aec_dump_id || !is_dump_in_progress) { | |
102 callback.Run(prefix_path, false); | |
Henrik Grunell
2015/12/18 12:34:21
Should we really run the callback if the dump was
Guido Urdaneta
2015/12/18 18:38:57
Since it is the caller who will remove/process fil
| |
103 return; | |
104 } | |
105 | |
106 for (content::RenderProcessHost::iterator i( | |
107 content::RenderProcessHost::AllHostsIterator()); | |
108 !i.IsAtEnd(); i.Advance()) { | |
109 i.GetCurrentValue()->DisableAudioDebugRecordings(); | |
110 } | |
111 | |
112 is_dump_in_progress = false; | |
113 callback.Run(prefix_path, true); | |
114 } | |
45 } // namespace | 115 } // namespace |
46 | 116 |
47 content::RenderProcessHost* WebrtcLoggingPrivateFunction::RphFromRequest( | 117 content::RenderProcessHost* WebrtcLoggingPrivateFunction::RphFromRequest( |
48 const RequestInfo& request, const std::string& security_origin) { | 118 const RequestInfo& request, const std::string& security_origin) { |
49 // If |guest_process_id| is defined, directly use this id to find the | 119 // If |guest_process_id| is defined, directly use this id to find the |
50 // corresponding RenderProcessHost. | 120 // corresponding RenderProcessHost. |
51 if (request.guest_process_id.get()) | 121 if (request.guest_process_id.get()) |
52 return content::RenderProcessHost::FromID(*request.guest_process_id.get()); | 122 return content::RenderProcessHost::FromID(*request.guest_process_id.get()); |
53 | 123 |
54 // Otherwise, use the |tab_id|. If there's no |tab_id| and no | 124 // Otherwise, use the |tab_id|. If there's no |tab_id| and no |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 if (success) { | 187 if (success) { |
118 api::webrtc_logging_private::UploadResult result; | 188 api::webrtc_logging_private::UploadResult result; |
119 result.report_id = report_id; | 189 result.report_id = report_id; |
120 SetResult(result.ToValue().release()); | 190 SetResult(result.ToValue().release()); |
121 } else { | 191 } else { |
122 SetError(error_message); | 192 SetError(error_message); |
123 } | 193 } |
124 SendResponse(success); | 194 SendResponse(success); |
125 } | 195 } |
126 | 196 |
197 void WebrtcLoggingPrivateFunctionWithAecDumpCallback::FireErrorCallback( | |
198 const std::string& error_message) { | |
199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
200 SetError(error_message); | |
201 SendResponse(false); | |
202 } | |
203 | |
204 void WebrtcLoggingPrivateFunctionWithAecDumpCallback::FireSuccessCallback( | |
205 bool is_manual_stop, | |
206 const std::string& prefix_path, | |
207 bool did_stop) { | |
208 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
209 api::webrtc_logging_private::AecDumpInfo result; | |
210 result.is_manual_stop = is_manual_stop; | |
211 result.prefix_path = prefix_path; | |
212 result.did_stop = did_stop; | |
213 SetResult(result.ToValue().release()); | |
214 SendResponse(true); | |
215 } | |
216 | |
127 bool WebrtcLoggingPrivateSetMetaDataFunction::RunAsync() { | 217 bool WebrtcLoggingPrivateSetMetaDataFunction::RunAsync() { |
128 scoped_ptr<SetMetaData::Params> params(SetMetaData::Params::Create(*args_)); | 218 scoped_ptr<SetMetaData::Params> params(SetMetaData::Params::Create(*args_)); |
129 EXTENSION_FUNCTION_VALIDATE(params.get()); | 219 EXTENSION_FUNCTION_VALIDATE(params.get()); |
130 | 220 |
131 WebRtcLoggingHandlerHost::GenericDoneCallback callback; | 221 WebRtcLoggingHandlerHost::GenericDoneCallback callback; |
132 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host = | 222 scoped_refptr<WebRtcLoggingHandlerHost> webrtc_logging_handler_host = |
133 PrepareTask(params->request, params->security_origin, &callback); | 223 PrepareTask(params->request, params->security_origin, &callback); |
134 if (!webrtc_logging_handler_host.get()) | 224 if (!webrtc_logging_handler_host.get()) |
135 return false; | 225 return false; |
136 | 226 |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 | 431 |
342 BrowserThread::PostTask(BrowserThread::IO, | 432 BrowserThread::PostTask(BrowserThread::IO, |
343 FROM_HERE, | 433 FROM_HERE, |
344 base::Bind(&WebRtcLoggingHandlerHost::StopRtpDump, | 434 base::Bind(&WebRtcLoggingHandlerHost::StopRtpDump, |
345 webrtc_logging_handler_host, | 435 webrtc_logging_handler_host, |
346 type, | 436 type, |
347 callback)); | 437 callback)); |
348 return true; | 438 return true; |
349 } | 439 } |
350 | 440 |
441 bool WebrtcLoggingPrivateStartAecDumpFunction::RunAsync() { | |
442 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
443 switches::kEnableAecDumpsFromExtension)) { | |
444 FireErrorCallback("Not supported"); | |
445 return false; | |
446 } | |
447 | |
448 scoped_ptr<StartAecDump::Params> params(StartAecDump::Params::Create(*args_)); | |
449 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
450 if (params->seconds < 0) { | |
451 FireErrorCallback("seconds must be greater than or equal to 0"); | |
452 return true; | |
453 } | |
454 | |
455 std::string prefix_path = DoStartAecDump(GetProfile()); | |
456 if (prefix_path.empty()) { | |
457 FireErrorCallback("AEC Dump already in progress"); | |
458 return true; | |
459 } | |
460 | |
461 if (params->seconds == 0) { | |
462 FireSuccessCallback(false, prefix_path, false); | |
463 return true; | |
464 } | |
465 | |
466 BrowserThread::PostDelayedTask( | |
Henrik Grunell
2015/12/18 12:34:21
Maybe use a Timer? It wraps a delayed task and can
Guido Urdaneta
2015/12/18 18:38:57
Using PostDelayedTask looked more straightforward
| |
467 BrowserThread::UI, FROM_HERE, | |
468 base::Bind( | |
469 &DoStopAecDump, GetProfile(), current_aec_dump_id, | |
470 base::Bind( | |
471 &WebrtcLoggingPrivateStartAecDumpFunction::FireSuccessCallback, | |
472 this, false)), | |
473 base::TimeDelta::FromSeconds(params->seconds)); | |
474 | |
475 return true; | |
476 } | |
477 | |
478 bool WebrtcLoggingPrivateStopAecDumpFunction::RunAsync() { | |
479 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | |
480 switches::kEnableAecDumpsFromExtension)) { | |
481 FireErrorCallback("Not supported"); | |
482 return false; | |
483 } | |
484 | |
485 DoStopAecDump( | |
486 GetProfile(), current_aec_dump_id, | |
487 base::Bind(&WebrtcLoggingPrivateStopAecDumpFunction::FireSuccessCallback, | |
488 this, true)); | |
489 return true; | |
490 } | |
491 | |
351 } // namespace extensions | 492 } // namespace extensions |
OLD | NEW |