Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: chrome/browser/media/audio_debug_recordings_handler.cc

Issue 1650133002: Start and stop RTC event logs from private extension API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unused includes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/strings/string_number_conversions.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/media/webrtc_log_list.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_process_host.h"
20
21 using content::BrowserThread;
22
23 // Keys used to attach handler to the RenderProcessHost
24 const char AudioDebugRecordingsHandler::kAudioDebugRecordingsHandlerKey[] =
25 "kAudioDebugRecordingsHandlerKey";
26
27 namespace {
28
29 // Returns a path name to be used as prefix for audio debug recordings files.
30 base::FilePath GetAudioDebugRecordingsPrefixPath(
31 const base::FilePath& directory,
32 uint64_t audio_debug_recordings_id) {
33 static const char kAudioDebugRecordingsFilePrefix[] = "AudioDebugRecordings.";
34 return directory.AppendASCII(kAudioDebugRecordingsFilePrefix +
35 base::Int64ToString(audio_debug_recordings_id));
36 }
37
38 } // namespace
39
40 AudioDebugRecordingsHandler::AudioDebugRecordingsHandler(Profile* profile)
41 : profile_(profile),
42 is_audio_debug_recordings_in_progress_(false),
43 current_audio_debug_recordings_id_(0) {
44 DCHECK(profile_);
45 }
46
47 AudioDebugRecordingsHandler::~AudioDebugRecordingsHandler() {
48 // TODO: Can we check that we dont hold any references to any Javascript
Henrik Grunell 2016/03/10 21:33:34 Nit: "dont" -> "don't"
terelius-chromium 2016/03/24 16:21:22 Removed.
49 // callbacks?
50 }
51
52 void AudioDebugRecordingsHandler::StartAudioDebugRecordings(
53 content::RenderProcessHost* host,
54 base::TimeDelta delay,
55 const RecordingDoneCallback& callback,
56 const RecordingErrorCallback& error_callback) {
57 DCHECK(thread_checker_.CalledOnValidThread());
58
59 BrowserThread::PostTaskAndReplyWithResult(
60 BrowserThread::FILE, FROM_HERE,
61 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists,
62 this),
63 base::Bind(&AudioDebugRecordingsHandler::DoStartAudioDebugRecordings,
64 this, host, delay, callback, error_callback));
65 }
66
67 void AudioDebugRecordingsHandler::StopAudioDebugRecordings(
68 content::RenderProcessHost* host,
69 const RecordingDoneCallback& callback,
70 const RecordingErrorCallback& error_callback) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 BrowserThread::PostTaskAndReplyWithResult(
73 BrowserThread::FILE, FROM_HERE,
74 base::Bind(&AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists,
75 this),
76 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this,
77 host, true /* manual stop */,
78 current_audio_debug_recordings_id_, callback, error_callback));
79 }
80
81 base::FilePath AudioDebugRecordingsHandler::GetLogDirectoryAndEnsureExists() {
82 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
83 base::FilePath log_dir_path =
84 WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile_->GetPath());
85 base::File::Error error;
86 if (!base::CreateDirectoryAndGetError(log_dir_path, &error)) {
87 DLOG(ERROR) << "Could not create WebRTC log directory, error: " << error;
88 return base::FilePath();
89 }
90 return log_dir_path;
91 }
92
93 void AudioDebugRecordingsHandler::DoStartAudioDebugRecordings(
94 content::RenderProcessHost* host,
95 base::TimeDelta delay,
96 const RecordingDoneCallback& callback,
97 const RecordingErrorCallback& error_callback,
98 const base::FilePath& log_directory) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100
101 if (is_audio_debug_recordings_in_progress_) {
102 error_callback.Run("Audio debug recordings already in progress");
103 return;
104 }
105
106 is_audio_debug_recordings_in_progress_ = true;
107 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath(
108 log_directory, ++current_audio_debug_recordings_id_);
109 host->EnableAudioDebugRecordings(prefix_path);
110
111 if (delay.is_zero()) {
112 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */,
113 false /* not manually stopped */);
114 return;
115 }
116
117 BrowserThread::PostDelayedTask(
118 BrowserThread::UI, FROM_HERE,
119 base::Bind(&AudioDebugRecordingsHandler::DoStopAudioDebugRecordings, this,
120 host, false /* no manual stop */,
121 current_audio_debug_recordings_id_, callback, error_callback,
122 prefix_path),
123 delay);
124 }
125
126 void AudioDebugRecordingsHandler::DoStopAudioDebugRecordings(
127 content::RenderProcessHost* host,
128 bool is_manual_stop,
129 uint64_t audio_debug_recordings_id,
130 const RecordingDoneCallback& callback,
131 const RecordingErrorCallback& error_callback,
132 const base::FilePath& log_directory) {
133 DCHECK(thread_checker_.CalledOnValidThread());
134 DCHECK_LE(audio_debug_recordings_id, current_audio_debug_recordings_id_);
135
136 base::FilePath prefix_path = GetAudioDebugRecordingsPrefixPath(
137 log_directory, audio_debug_recordings_id);
138 // Prevent an old posted StopAudioDebugRecordings() call to stop a newer dump.
139 // This could happen in a sequence like:
140 // Start(10); //Start dump 1. Post Stop() to run after 10 seconds.
141 // Stop(); // Manually stop dump 1 before 10 seconds;
142 // Start(20); // Start dump 2. Posted Stop() for 1 should not stop dump 2.
143 if (audio_debug_recordings_id < current_audio_debug_recordings_id_) {
144 callback.Run(prefix_path.AsUTF8Unsafe(), false /* not stopped */,
145 is_manual_stop);
146 return;
147 }
148
149 if (!is_audio_debug_recordings_in_progress_) {
150 error_callback.Run("No audio debug recording in progress");
151 return;
152 }
153
154 host->DisableAudioDebugRecordings();
155 is_audio_debug_recordings_in_progress_ = false;
156 callback.Run(prefix_path.AsUTF8Unsafe(), true /* stopped */, is_manual_stop);
157 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698