OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "chrome/browser/media/audio_debug_recordings_handler.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/command_line.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/prefs/pref_service.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/sys_info.h" | |
16 #include "base/time/time.h" | |
17 #include "chrome/browser/bad_message.h" | |
18 #include "chrome/browser/browser_process.h" | |
19 #include "chrome/browser/media/webrtc_log_list.h" | |
20 #include "chrome/browser/profiles/profile.h" | |
21 #include "chrome/common/channel_info.h" | |
22 #include "chrome/common/chrome_switches.h" | |
23 #include "content/public/browser/browser_thread.h" | |
24 #include "content/public/browser/content_browser_client.h" | |
25 #include "content/public/browser/render_process_host.h" | |
26 #include "net/base/net_util.h" | |
27 #include "net/url_request/url_request_context_getter.h" | |
28 | |
29 using content::BrowserThread; | |
30 | |
31 // Keys used to attach handler to the RenderProcessHost | |
32 const char AudioDebugRecordingsHandler::kAudioDebugRecordingsHandlerKey[] = | |
33 "kAudioDebugRecordingsHandlerKey"; | |
34 | |
35 namespace { | |
36 | |
37 // Returns a path name to be used as prefix for audio debug recordings files. | |
38 base::FilePath GetAudioDebugRecordingsPrefixPath( | |
39 const base::FilePath& directory, | |
40 uint64_t audio_debug_recordings_id) { | |
41 static const char kAudioDebugRecordingsFilePrefix[] = "AudioDebugRecordings."; | |
42 return directory.AppendASCII(kAudioDebugRecordingsFilePrefix + | |
43 base::Int64ToString(audio_debug_recordings_id)); | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 AudioDebugRecordingsHandler::AudioDebugRecordingsHandler(Profile* profile) | |
49 : profile_(profile), | |
50 is_audio_debug_recordings_in_progress_(false), | |
51 current_audio_debug_recordings_id_(0) { | |
52 DCHECK(profile_); | |
53 } | |
54 | |
55 AudioDebugRecordingsHandler::~AudioDebugRecordingsHandler() { | |
56 // TODO: Can we check that we dont hold any references to any Javascript | |
Henrik Grunell
2016/02/23 15:29:10
Did this todo come from WebRtcLoggingHandlerHost?
terelius-chromium
2016/03/02 10:01:09
No, this was based on my interpretation of a comme
Henrik Grunell
2016/03/10 21:33:34
OK. All todos should have a reference. Change to T
| |
57 // callbacks? | |
58 } | |
59 | |
60 void AudioDebugRecordingsHandler::StartAudioDebugRecordings( | |
61 content::RenderProcessHost* host, | |
62 base::TimeDelta delay, | |
63 const TimeLimitedRecordingCallback& callback, | |
64 const TimeLimitedRecordingErrorCallback& error_callback) { | |
65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
66 | |
67 BrowserThread::PostTaskAndReplyWithResult( | |
68 BrowserThread::FILE, FROM_HERE, | |
69 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists, | |
70 this), | |
71 base::Bind(&AudioDebugRecordingsHandler::DoStartAudioDebugRecordings, | |
72 this, host, delay, callback, error_callback)); | |
73 } | |
74 | |
75 void AudioDebugRecordingsHandler::StopAudioDebugRecordings( | |
76 content::RenderProcessHost* host, | |
77 const TimeLimitedRecordingCallback& callback, | |
78 const TimeLimitedRecordingErrorCallback& error_callback) { | |
79 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
80 BrowserThread::PostTaskAndReplyWithResult( | |
81 BrowserThread::FILE, FROM_HERE, | |
82 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists, | |
83 this), | |
84 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this, | |
85 host, true /* manual stop */, | |
86 current_audio_debug_recordings_id_, callback, error_callback)); | |
87 } | |
88 | |
89 base::FilePath AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists() { | |
90 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
91 base::FilePath log_dir_path = | |
92 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath()); | |
93 base::File::Error error; | |
94 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) { | |
95 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error; | |
96 return base::FilePath(); | |
97 } | |
98 return log_dir_path; | |
99 } | |
100 | |
101 void AudioDebugRecordingsHandler::DoStartAudioDebugRecordings( | |
102 content::RenderProcessHost* host, | |
103 base::TimeDelta delay, | |
104 const TimeLimitedRecordingCallback& callback, | |
105 const TimeLimitedRecordingErrorCallback& error_callback, | |
106 const base::FilePath& log_directory) { | |
107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
108 | |
109 if (is_audio_debug_recordings_in_progress_) { | |
110 error_callback.Run("Audio debug recordings already in progress"); | |
111 return; | |
112 } | |
113 | |
114 is_audio_debug_recordings_in_progress_ = true; | |
115 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( | |
116 log_directory, ++current_audio_debug_recordings_id_); | |
117 host->EnableAudioDebugRecordings(prefix_path); | |
118 | |
119 if (delay.is_zero()) { | |
120 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
121 false /* not manually stopped */); | |
122 return; | |
123 } | |
124 | |
125 BrowserThread::PostDelayedTask( | |
126 BrowserThread::UI, FROM_HERE, | |
127 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this, | |
128 host, false /* no manual stop */, | |
129 current_audio_debug_recordings_id_, callback, error_callback, | |
130 prefix_path), | |
131 delay); | |
132 } | |
133 | |
134 void AudioDebugRecordingsHandler::DoStopAudioDebugRecordings( | |
135 content::RenderProcessHost* host, | |
136 bool is_manual_stop, | |
137 uint64_t audio_debug_recordings_id, | |
138 const TimeLimitedRecordingCallback& callback, | |
139 const TimeLimitedRecordingErrorCallback& error_callback, | |
140 const base::FilePath& log_directory) { | |
141 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
142 DCHECK_LE(audio_debug_recordings_id, current_audio_debug_recordings_id_); | |
143 | |
144 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath( | |
145 log_directory, audio_debug_recordings_id); | |
146 // Prevent an old posted StopAudioDebugRecordings() call to stop a newer dump. | |
147 // This could happen in a sequence like: | |
148 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds. | |
149 // Stop(); // Manually stop dump 1 before 10 seconds; | |
150 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2. | |
151 if (audio_debug_recordings_id < current_audio_debug_recordings_id_) { | |
152 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */, | |
153 is_manual_stop); | |
154 return; | |
155 } | |
156 | |
157 if (!is_audio_debug_recordings_in_progress_) { | |
158 error_callback.Run("No audio debug recording in progress"); | |
159 return; | |
160 } | |
161 | |
162 host->DisableAudioDebugRecordings(); | |
163 is_audio_debug_recordings_in_progress_ = false; | |
164 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop); | |
165 } | |
OLD | NEW |