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

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

Issue 23691066: Hook up WebRTC logging extension API to the underlying functionality. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: First patch ready for review. Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
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/media/webrtc_logging_handler_host.h" 5 #include "chrome/browser/media/webrtc_logging_handler_host.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 43
44 44
45 #if defined(OS_ANDROID) 45 #if defined(OS_ANDROID)
46 const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB 46 const size_t kWebRtcLogSize = 1 * 1024 * 1024; // 1 MB
47 #else 47 #else
48 const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB 48 const size_t kWebRtcLogSize = 6 * 1024 * 1024; // 6 MB
49 #endif 49 #endif
50 50
51 namespace { 51 namespace {
52 52
53 const char kLogNotStoppedOrNoLogOpen[] =
54 "Logging not stopped or no log open.";
55
53 // For privacy reasons when logging IP addresses. The returned "sensitive 56 // For privacy reasons when logging IP addresses. The returned "sensitive
54 // string" is for release builds a string with the end stripped away. Last 57 // string" is for release builds a string with the end stripped away. Last
55 // octet for IPv4 and last 80 bits (5 groups) for IPv6. String will be 58 // octet for IPv4 and last 80 bits (5 groups) for IPv6. String will be
56 // "1.2.3.x" and "1.2.3::" respectively. For debug builds, the string is 59 // "1.2.3.x" and "1.2.3::" respectively. For debug builds, the string is
57 // not stripped. 60 // not stripped.
58 std::string IPAddressToSensitiveString(const net::IPAddressNumber& address) { 61 std::string IPAddressToSensitiveString(const net::IPAddressNumber& address) {
59 #if defined(NDEBUG) 62 #if defined(NDEBUG)
60 std::string sensitive_address; 63 std::string sensitive_address;
61 switch (net::GetAddressFamily(address)) { 64 switch (net::GetAddressFamily(address)) {
62 case net::ADDRESS_FAMILY_IPV4: { 65 case net::ADDRESS_FAMILY_IPV4: {
(...skipping 19 matching lines...) Expand all
82 } 85 }
83 } 86 }
84 return sensitive_address; 87 return sensitive_address;
85 #else 88 #else
86 return net::IPAddressToString(address); 89 return net::IPAddressToString(address);
87 #endif 90 #endif
88 } 91 }
89 92
90 } // namespace 93 } // namespace
91 94
92 WebRtcLoggingHandlerHost::WebRtcLoggingHandlerHost() {} 95 WebRtcLoggingHandlerHost::WebRtcLoggingHandlerHost()
96 : logging_state_(CLOSED),
97 upload_log_on_render_close_(false) {
98 }
93 99
94 WebRtcLoggingHandlerHost::~WebRtcLoggingHandlerHost() {} 100 WebRtcLoggingHandlerHost::~WebRtcLoggingHandlerHost() {}
95 101
102 void WebRtcLoggingHandlerHost::SetMetaData(
103 const std::map<std::string, std::string>& meta_data,
104 const GenericDoneCallback& callback) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
106 DCHECK(!callback.is_null());
107
108 bool success = false;
109 std::string error_message;
110 if (logging_state_ == CLOSED) {
111 meta_data_ = meta_data;
112 success = true;
113 } else {
114 error_message = "Meta data must be set before starting";
115 }
116 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
117 base::Bind(callback, success,
118 error_message));
119 }
120
121 void WebRtcLoggingHandlerHost::StartLogging(
122 const std::string& app_url,
123 const GenericDoneCallback& callback) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
125 DCHECK(!callback.is_null());
126
127 start_callback_ = callback;
128 if (logging_state_ != CLOSED) {
129 FireGenericDoneCallback(&start_callback_, false, "A log is already open");
130 return;
131 }
132 logging_state_ = STARTING;
133 app_url_ = app_url;
134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
135 &WebRtcLoggingHandlerHost::StartLoggingIfAllowed, this));
136 }
137
138 void WebRtcLoggingHandlerHost::StopLogging(
139 const GenericDoneCallback& callback) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 DCHECK(!callback.is_null());
142
143 stop_callback_ = callback;
144 if (logging_state_ != STARTED) {
145 FireGenericDoneCallback(&stop_callback_, false, "Logging not started");
146 return;
147 }
148 logging_state_ = STOPPING;
149 Send(new WebRtcLoggingMsg_StopLogging());
150 }
151
152 void WebRtcLoggingHandlerHost::UploadLog(const UploadDoneCallback& callback) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
154 DCHECK(!callback.is_null());
155
156 if (logging_state_ != STOPPED) {
157 if (!callback.is_null()) {
158 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
159 base::Bind(callback, false, "", kLogNotStoppedOrNoLogOpen));
160 }
161 return;
162 }
163 upload_callback_ = callback;
164 TriggerUploadLog();
165 }
166
167 void WebRtcLoggingHandlerHost::UploadLogDone() {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
169 logging_state_ = CLOSED;
170 }
171
172 void WebRtcLoggingHandlerHost::DiscardLog(const GenericDoneCallback& callback) {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 DCHECK(!callback.is_null());
175
176 GenericDoneCallback discard_callback = callback;
177 if (logging_state_ != STOPPED) {
178 FireGenericDoneCallback(&discard_callback, false,
179 kLogNotStoppedOrNoLogOpen);
180 return;
181 }
182 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload();
183 shared_memory_.reset(NULL);
184 logging_state_ = CLOSED;
185 FireGenericDoneCallback(&discard_callback, true, "");
186 }
187
96 void WebRtcLoggingHandlerHost::OnChannelClosing() { 188 void WebRtcLoggingHandlerHost::OnChannelClosing() {
97 UploadLog(); 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
190 if (logging_state_ == STARTED || logging_state_ == STOPPED) {
191 if (upload_log_on_render_close_) {
192 TriggerUploadLog();
193 } else {
194 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload();
195 }
196 }
98 content::BrowserMessageFilter::OnChannelClosing(); 197 content::BrowserMessageFilter::OnChannelClosing();
99 } 198 }
100 199
101 void WebRtcLoggingHandlerHost::OnDestruct() const { 200 void WebRtcLoggingHandlerHost::OnDestruct() const {
102 BrowserThread::DeleteOnIOThread::Destruct(this); 201 BrowserThread::DeleteOnIOThread::Destruct(this);
103 } 202 }
104 203
105 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message, 204 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message,
106 bool* message_was_ok) { 205 bool* message_was_ok) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
108 bool handled = true; 207 bool handled = true;
109 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok) 208 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok)
110 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_OpenLog, OnOpenLog) 209 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_LoggingStopped,
210 OnLoggingStoppedInRenderer)
111 IPC_MESSAGE_UNHANDLED(handled = false) 211 IPC_MESSAGE_UNHANDLED(handled = false)
112 IPC_END_MESSAGE_MAP_EX() 212 IPC_END_MESSAGE_MAP_EX()
113 213
114 return handled; 214 return handled;
115 } 215 }
116 216
117 void WebRtcLoggingHandlerHost::OnOpenLog(const std::string& app_session_id, 217 void WebRtcLoggingHandlerHost::OnLoggingStoppedInRenderer() {
118 const std::string& app_url) {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
120 app_session_id_ = app_session_id; 219 logging_state_ = STOPPED;
121 app_url_ = app_url; 220 FireGenericDoneCallback(&stop_callback_, true, "");
122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
123 &WebRtcLoggingHandlerHost::OpenLogIfAllowed, this));
124 } 221 }
125 222
126 void WebRtcLoggingHandlerHost::OpenLogIfAllowed() { 223 void WebRtcLoggingHandlerHost::StartLoggingIfAllowed() {
127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 225 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging()) {
129 // If the user permits metrics reporting / crash uploading with the checkbox 226 logging_state_ = CLOSED;
130 // in the prefs, we allow uploading automatically. We disable uploading 227 FireGenericDoneCallback(
131 // completely for non-official builds. Allowing can be forced with a flag. 228 &start_callback_, false, "Cannot start, maybe the maximum number of "
132 const CommandLine* command_line = CommandLine::ForCurrentProcess(); 229 "simultaneuos logs has been reached.");
133 if (!command_line->HasSwitch(switches::kEnableMetricsReportingForTesting)) { 230 return;
134 bool enabled = false;
135 #if defined(GOOGLE_CHROME_BUILD)
136 #if defined(OS_CHROMEOS)
137 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
138 &enabled);
139 #elif defined(OS_ANDROID)
140 // Android has its own settings for metrics / crash uploading.
141 enabled = g_browser_process->local_state()->GetBoolean(
142 prefs::kCrashReportingEnabled);
143 #else
144 enabled = g_browser_process->local_state()->GetBoolean(
145 prefs::kMetricsReportingEnabled);
146 #endif // #if defined(OS_CHROMEOS)
147 #endif // defined(GOOGLE_CHROME_BUILD)
148 if (!enabled)
149 return;
150 } 231 }
151
152 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging())
153 return;
154
155 system_request_context_ = g_browser_process->system_request_context(); 232 system_request_context_ = g_browser_process->system_request_context();
156 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 233 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
157 &WebRtcLoggingHandlerHost::DoOpenLog, this)); 234 &WebRtcLoggingHandlerHost::DoStartLogging, this));
158 } 235 }
159 236
160 void WebRtcLoggingHandlerHost::DoOpenLog() { 237 void WebRtcLoggingHandlerHost::DoStartLogging() {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
162 DCHECK(!shared_memory_); 239 DCHECK(!shared_memory_);
163 240
164 shared_memory_.reset(new base::SharedMemory()); 241 shared_memory_.reset(new base::SharedMemory());
165 242
166 if (!shared_memory_->CreateAndMapAnonymous(kWebRtcLogSize)) { 243 if (!shared_memory_->CreateAndMapAnonymous(kWebRtcLogSize)) {
167 DLOG(ERROR) << "Failed to create shared memory."; 244 const std::string error_message = "Failed to create shared memory";
168 Send(new WebRtcLoggingMsg_OpenLogFailed()); 245 DLOG(ERROR) << error_message;
246 logging_state_ = CLOSED;
247 FireGenericDoneCallback(&start_callback_, false, error_message);
169 return; 248 return;
170 } 249 }
171 250
172 if (!shared_memory_->ShareToProcess(PeerHandle(), 251 if (!shared_memory_->ShareToProcess(PeerHandle(),
173 &foreign_memory_handle_)) { 252 &foreign_memory_handle_)) {
174 Send(new WebRtcLoggingMsg_OpenLogFailed()); 253 const std::string error_message =
254 "Failed to share memory to render process";
255 DLOG(ERROR) << error_message;
256 logging_state_ = CLOSED;
257 FireGenericDoneCallback(&start_callback_, false, error_message);
175 return; 258 return;
176 } 259 }
177 260
178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 261 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
179 &WebRtcLoggingHandlerHost::LogMachineInfo, this)); 262 &WebRtcLoggingHandlerHost::LogMachineInfo, this));
180 } 263 }
181 264
182 void WebRtcLoggingHandlerHost::LogMachineInfo() { 265 void WebRtcLoggingHandlerHost::LogMachineInfo() {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
184 PartialCircularBuffer pcb(shared_memory_->memory(), 267 PartialCircularBuffer pcb(shared_memory_->memory(),
185 kWebRtcLogSize, 268 kWebRtcLogSize,
186 kWebRtcLogSize / 2, 269 kWebRtcLogSize / 2,
187 false); 270 false);
188 271
189 // App session ID 272 // Meta data
190 std::string info = "App session ID: " + app_session_id_ + '\n'; 273 std::string info;
191 pcb.Write(info.c_str(), info.length()); 274 std::map<std::string, std::string>::iterator it = meta_data_.begin();
275 for (; it != meta_data_.end(); ++it) {
276 info = it->first + ": " + it->second + '\n';
277 pcb.Write(info.c_str(), info.length());
278 }
192 279
193 // OS 280 // OS
194 info = base::SysInfo::OperatingSystemName() + " " + 281 info = base::SysInfo::OperatingSystemName() + " " +
195 base::SysInfo::OperatingSystemVersion() + " " + 282 base::SysInfo::OperatingSystemVersion() + " " +
196 base::SysInfo::OperatingSystemArchitecture() + '\n'; 283 base::SysInfo::OperatingSystemArchitecture() + '\n';
197 pcb.Write(info.c_str(), info.length()); 284 pcb.Write(info.c_str(), info.length());
198 #if defined(OS_LINUX) 285 #if defined(OS_LINUX)
199 info = "Linux distribution: " + base::GetLinuxDistro() + '\n'; 286 info = "Linux distribution: " + base::GetLinuxDistro() + '\n';
200 pcb.Write(info.c_str(), info.length()); 287 pcb.Write(info.c_str(), info.length());
201 #endif 288 #endif
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 " network interfaces:" + '\n'; 327 " network interfaces:" + '\n';
241 pcb.Write(info.c_str(), info.length()); 328 pcb.Write(info.c_str(), info.length());
242 for (net::NetworkInterfaceList::iterator it = network_list.begin(); 329 for (net::NetworkInterfaceList::iterator it = network_list.begin();
243 it != network_list.end(); ++it) { 330 it != network_list.end(); ++it) {
244 info = "Name: " + it->name + 331 info = "Name: " + it->name +
245 ", Address: " + IPAddressToSensitiveString(it->address) + '\n'; 332 ", Address: " + IPAddressToSensitiveString(it->address) + '\n';
246 pcb.Write(info.c_str(), info.length()); 333 pcb.Write(info.c_str(), info.length());
247 } 334 }
248 335
249 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 336 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
250 &WebRtcLoggingHandlerHost::NotifyLogOpened, this)); 337 &WebRtcLoggingHandlerHost::NotifyLoggingStarted, this));
251 } 338 }
252 339
253 void WebRtcLoggingHandlerHost::NotifyLogOpened() { 340 void WebRtcLoggingHandlerHost::NotifyLoggingStarted() {
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
255 Send(new WebRtcLoggingMsg_LogOpened(foreign_memory_handle_, kWebRtcLogSize)); 342 Send(new WebRtcLoggingMsg_StartLogging(foreign_memory_handle_,
343 kWebRtcLogSize));
344 logging_state_ = STARTED;
345 FireGenericDoneCallback(&start_callback_, true, "");
256 } 346 }
257 347
258 void WebRtcLoggingHandlerHost::UploadLog() { 348 void WebRtcLoggingHandlerHost::TriggerUploadLog() {
259 if (!shared_memory_) 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
Jói 2013/09/27 14:39:53 Might want to DCHECK on logging_state_ also to hel
Henrik Grunell 2013/10/02 12:47:18 Done.
260 return; 350
351 logging_state_ = UPLOADING;
352 WebRtcLogUploadDoneData upload_done_data;
353 upload_done_data.callback = upload_callback_;
354 upload_done_data.host = this;
355 upload_callback_.Reset();
261 356
262 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 357 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
263 &WebRtcLogUploader::UploadLog, 358 &WebRtcLogUploader::LoggingStoppedDoUpload,
264 base::Unretained(g_browser_process->webrtc_log_uploader()), 359 base::Unretained(g_browser_process->webrtc_log_uploader()),
265 system_request_context_, 360 system_request_context_,
266 Passed(&shared_memory_), 361 Passed(&shared_memory_),
267 kWebRtcLogSize, 362 kWebRtcLogSize,
268 app_session_id_, 363 meta_data_,
269 app_url_)); 364 app_url_,
365 upload_done_data));
366
367 meta_data_.clear();
368 app_url_.clear();
270 } 369 }
370
371 void WebRtcLoggingHandlerHost::FireGenericDoneCallback(
372 GenericDoneCallback* callback, bool success,
373 const std::string& error_message) {
374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
375 DCHECK(!(*callback).is_null());
376 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
377 base::Bind(*callback, success,
378 error_message));
379 (*callback).Reset();
380 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698