| 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/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 Loading... |
| 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 Loading... |
| 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 GenericDoneCallback& callback) { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 124 DCHECK(!callback.is_null()); |
| 125 |
| 126 start_callback_ = callback; |
| 127 if (logging_state_ != CLOSED) { |
| 128 FireGenericDoneCallback(&start_callback_, false, "A log is already open"); |
| 129 return; |
| 130 } |
| 131 logging_state_ = STARTING; |
| 132 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| 133 &WebRtcLoggingHandlerHost::StartLoggingIfAllowed, this)); |
| 134 } |
| 135 |
| 136 void WebRtcLoggingHandlerHost::StopLogging( |
| 137 const GenericDoneCallback& callback) { |
| 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 139 DCHECK(!callback.is_null()); |
| 140 |
| 141 stop_callback_ = callback; |
| 142 if (logging_state_ != STARTED) { |
| 143 FireGenericDoneCallback(&stop_callback_, false, "Logging not started"); |
| 144 return; |
| 145 } |
| 146 logging_state_ = STOPPING; |
| 147 Send(new WebRtcLoggingMsg_StopLogging()); |
| 148 } |
| 149 |
| 150 void WebRtcLoggingHandlerHost::UploadLog(const UploadDoneCallback& callback) { |
| 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 152 DCHECK(!callback.is_null()); |
| 153 |
| 154 if (logging_state_ != STOPPED) { |
| 155 if (!callback.is_null()) { |
| 156 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 157 base::Bind(callback, false, "", kLogNotStoppedOrNoLogOpen)); |
| 158 } |
| 159 return; |
| 160 } |
| 161 upload_callback_ = callback; |
| 162 TriggerUploadLog(); |
| 163 } |
| 164 |
| 165 void WebRtcLoggingHandlerHost::UploadLogDone() { |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 167 logging_state_ = CLOSED; |
| 168 } |
| 169 |
| 170 void WebRtcLoggingHandlerHost::DiscardLog(const GenericDoneCallback& callback) { |
| 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 172 DCHECK(!callback.is_null()); |
| 173 |
| 174 GenericDoneCallback discard_callback = callback; |
| 175 if (logging_state_ != STOPPED) { |
| 176 FireGenericDoneCallback(&discard_callback, false, |
| 177 kLogNotStoppedOrNoLogOpen); |
| 178 return; |
| 179 } |
| 180 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload(); |
| 181 shared_memory_.reset(NULL); |
| 182 logging_state_ = CLOSED; |
| 183 FireGenericDoneCallback(&discard_callback, true, ""); |
| 184 } |
| 185 |
| 96 void WebRtcLoggingHandlerHost::OnChannelClosing() { | 186 void WebRtcLoggingHandlerHost::OnChannelClosing() { |
| 97 UploadLog(); | 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 188 if (logging_state_ == STARTED || logging_state_ == STOPPED) { |
| 189 if (upload_log_on_render_close_) { |
| 190 TriggerUploadLog(); |
| 191 } else { |
| 192 g_browser_process->webrtc_log_uploader()->LoggingStoppedDontUpload(); |
| 193 } |
| 194 } |
| 98 content::BrowserMessageFilter::OnChannelClosing(); | 195 content::BrowserMessageFilter::OnChannelClosing(); |
| 99 } | 196 } |
| 100 | 197 |
| 101 void WebRtcLoggingHandlerHost::OnDestruct() const { | 198 void WebRtcLoggingHandlerHost::OnDestruct() const { |
| 102 BrowserThread::DeleteOnIOThread::Destruct(this); | 199 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 103 } | 200 } |
| 104 | 201 |
| 105 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message, | 202 bool WebRtcLoggingHandlerHost::OnMessageReceived(const IPC::Message& message, |
| 106 bool* message_was_ok) { | 203 bool* message_was_ok) { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 108 bool handled = true; | 205 bool handled = true; |
| 109 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok) | 206 IPC_BEGIN_MESSAGE_MAP_EX(WebRtcLoggingHandlerHost, message, *message_was_ok) |
| 110 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_OpenLog, OnOpenLog) | 207 IPC_MESSAGE_HANDLER(WebRtcLoggingMsg_LoggingStopped, |
| 208 OnLoggingStoppedInRenderer) |
| 111 IPC_MESSAGE_UNHANDLED(handled = false) | 209 IPC_MESSAGE_UNHANDLED(handled = false) |
| 112 IPC_END_MESSAGE_MAP_EX() | 210 IPC_END_MESSAGE_MAP_EX() |
| 113 | 211 |
| 114 return handled; | 212 return handled; |
| 115 } | 213 } |
| 116 | 214 |
| 117 void WebRtcLoggingHandlerHost::OnOpenLog(const std::string& app_session_id, | 215 void WebRtcLoggingHandlerHost::OnLoggingStoppedInRenderer() { |
| 118 const std::string& app_url) { | |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 120 app_session_id_ = app_session_id; | 217 logging_state_ = STOPPED; |
| 121 app_url_ = app_url; | 218 FireGenericDoneCallback(&stop_callback_, true, ""); |
| 122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 123 &WebRtcLoggingHandlerHost::OpenLogIfAllowed, this)); | |
| 124 } | 219 } |
| 125 | 220 |
| 126 void WebRtcLoggingHandlerHost::OpenLogIfAllowed() { | 221 void WebRtcLoggingHandlerHost::StartLoggingIfAllowed() { |
| 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 128 | 223 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging()) { |
| 129 // If the user permits metrics reporting / crash uploading with the checkbox | 224 logging_state_ = CLOSED; |
| 130 // in the prefs, we allow uploading automatically. We disable uploading | 225 FireGenericDoneCallback( |
| 131 // completely for non-official builds. Allowing can be forced with a flag. | 226 &start_callback_, false, "Cannot start, maybe the maximum number of " |
| 132 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | 227 "simultaneuos logs has been reached."); |
| 133 if (!command_line->HasSwitch(switches::kEnableMetricsReportingForTesting)) { | 228 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 } | 229 } |
| 151 | |
| 152 if (!g_browser_process->webrtc_log_uploader()->ApplyForStartLogging()) | |
| 153 return; | |
| 154 | |
| 155 system_request_context_ = g_browser_process->system_request_context(); | 230 system_request_context_ = g_browser_process->system_request_context(); |
| 156 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 231 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 157 &WebRtcLoggingHandlerHost::DoOpenLog, this)); | 232 &WebRtcLoggingHandlerHost::DoStartLogging, this)); |
| 158 } | 233 } |
| 159 | 234 |
| 160 void WebRtcLoggingHandlerHost::DoOpenLog() { | 235 void WebRtcLoggingHandlerHost::DoStartLogging() { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 162 DCHECK(!shared_memory_); | 237 DCHECK(!shared_memory_); |
| 163 | 238 |
| 164 shared_memory_.reset(new base::SharedMemory()); | 239 shared_memory_.reset(new base::SharedMemory()); |
| 165 | 240 |
| 166 if (!shared_memory_->CreateAndMapAnonymous(kWebRtcLogSize)) { | 241 if (!shared_memory_->CreateAndMapAnonymous(kWebRtcLogSize)) { |
| 167 DLOG(ERROR) << "Failed to create shared memory."; | 242 const std::string error_message = "Failed to create shared memory"; |
| 168 Send(new WebRtcLoggingMsg_OpenLogFailed()); | 243 DLOG(ERROR) << error_message; |
| 244 logging_state_ = CLOSED; |
| 245 FireGenericDoneCallback(&start_callback_, false, error_message); |
| 169 return; | 246 return; |
| 170 } | 247 } |
| 171 | 248 |
| 172 if (!shared_memory_->ShareToProcess(PeerHandle(), | 249 if (!shared_memory_->ShareToProcess(PeerHandle(), |
| 173 &foreign_memory_handle_)) { | 250 &foreign_memory_handle_)) { |
| 174 Send(new WebRtcLoggingMsg_OpenLogFailed()); | 251 const std::string error_message = |
| 252 "Failed to share memory to render process"; |
| 253 DLOG(ERROR) << error_message; |
| 254 logging_state_ = CLOSED; |
| 255 FireGenericDoneCallback(&start_callback_, false, error_message); |
| 175 return; | 256 return; |
| 176 } | 257 } |
| 177 | 258 |
| 178 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | 259 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( |
| 179 &WebRtcLoggingHandlerHost::LogMachineInfo, this)); | 260 &WebRtcLoggingHandlerHost::LogMachineInfo, this)); |
| 180 } | 261 } |
| 181 | 262 |
| 182 void WebRtcLoggingHandlerHost::LogMachineInfo() { | 263 void WebRtcLoggingHandlerHost::LogMachineInfo() { |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 184 PartialCircularBuffer pcb(shared_memory_->memory(), | 265 PartialCircularBuffer pcb(shared_memory_->memory(), |
| 185 kWebRtcLogSize, | 266 kWebRtcLogSize, |
| 186 kWebRtcLogSize / 2, | 267 kWebRtcLogSize / 2, |
| 187 false); | 268 false); |
| 188 | 269 |
| 189 // App session ID | 270 // Meta data |
| 190 std::string info = "App session ID: " + app_session_id_ + '\n'; | 271 std::string info; |
| 191 pcb.Write(info.c_str(), info.length()); | 272 std::map<std::string, std::string>::iterator it = meta_data_.begin(); |
| 273 for (; it != meta_data_.end(); ++it) { |
| 274 info = it->first + ": " + it->second + '\n'; |
| 275 pcb.Write(info.c_str(), info.length()); |
| 276 } |
| 192 | 277 |
| 193 // OS | 278 // OS |
| 194 info = base::SysInfo::OperatingSystemName() + " " + | 279 info = base::SysInfo::OperatingSystemName() + " " + |
| 195 base::SysInfo::OperatingSystemVersion() + " " + | 280 base::SysInfo::OperatingSystemVersion() + " " + |
| 196 base::SysInfo::OperatingSystemArchitecture() + '\n'; | 281 base::SysInfo::OperatingSystemArchitecture() + '\n'; |
| 197 pcb.Write(info.c_str(), info.length()); | 282 pcb.Write(info.c_str(), info.length()); |
| 198 #if defined(OS_LINUX) | 283 #if defined(OS_LINUX) |
| 199 info = "Linux distribution: " + base::GetLinuxDistro() + '\n'; | 284 info = "Linux distribution: " + base::GetLinuxDistro() + '\n'; |
| 200 pcb.Write(info.c_str(), info.length()); | 285 pcb.Write(info.c_str(), info.length()); |
| 201 #endif | 286 #endif |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 " network interfaces:" + '\n'; | 325 " network interfaces:" + '\n'; |
| 241 pcb.Write(info.c_str(), info.length()); | 326 pcb.Write(info.c_str(), info.length()); |
| 242 for (net::NetworkInterfaceList::iterator it = network_list.begin(); | 327 for (net::NetworkInterfaceList::iterator it = network_list.begin(); |
| 243 it != network_list.end(); ++it) { | 328 it != network_list.end(); ++it) { |
| 244 info = "Name: " + it->name + | 329 info = "Name: " + it->name + |
| 245 ", Address: " + IPAddressToSensitiveString(it->address) + '\n'; | 330 ", Address: " + IPAddressToSensitiveString(it->address) + '\n'; |
| 246 pcb.Write(info.c_str(), info.length()); | 331 pcb.Write(info.c_str(), info.length()); |
| 247 } | 332 } |
| 248 | 333 |
| 249 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( | 334 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 250 &WebRtcLoggingHandlerHost::NotifyLogOpened, this)); | 335 &WebRtcLoggingHandlerHost::NotifyLoggingStarted, this)); |
| 251 } | 336 } |
| 252 | 337 |
| 253 void WebRtcLoggingHandlerHost::NotifyLogOpened() { | 338 void WebRtcLoggingHandlerHost::NotifyLoggingStarted() { |
| 254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 255 Send(new WebRtcLoggingMsg_LogOpened(foreign_memory_handle_, kWebRtcLogSize)); | 340 Send(new WebRtcLoggingMsg_StartLogging(foreign_memory_handle_, |
| 341 kWebRtcLogSize)); |
| 342 logging_state_ = STARTED; |
| 343 FireGenericDoneCallback(&start_callback_, true, ""); |
| 256 } | 344 } |
| 257 | 345 |
| 258 void WebRtcLoggingHandlerHost::UploadLog() { | 346 void WebRtcLoggingHandlerHost::TriggerUploadLog() { |
| 259 if (!shared_memory_) | 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 260 return; | 348 DCHECK(logging_state_ == STOPPED); |
| 349 |
| 350 logging_state_ = UPLOADING; |
| 351 WebRtcLogUploadDoneData upload_done_data; |
| 352 upload_done_data.callback = upload_callback_; |
| 353 upload_done_data.host = this; |
| 354 upload_callback_.Reset(); |
| 261 | 355 |
| 262 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | 356 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( |
| 263 &WebRtcLogUploader::UploadLog, | 357 &WebRtcLogUploader::LoggingStoppedDoUpload, |
| 264 base::Unretained(g_browser_process->webrtc_log_uploader()), | 358 base::Unretained(g_browser_process->webrtc_log_uploader()), |
| 265 system_request_context_, | 359 system_request_context_, |
| 266 Passed(&shared_memory_), | 360 Passed(&shared_memory_), |
| 267 kWebRtcLogSize, | 361 kWebRtcLogSize, |
| 268 app_session_id_, | 362 meta_data_, |
| 269 app_url_)); | 363 upload_done_data)); |
| 364 |
| 365 meta_data_.clear(); |
| 270 } | 366 } |
| 367 |
| 368 void WebRtcLoggingHandlerHost::FireGenericDoneCallback( |
| 369 GenericDoneCallback* callback, bool success, |
| 370 const std::string& error_message) { |
| 371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 372 DCHECK(!(*callback).is_null()); |
| 373 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, |
| 374 base::Bind(*callback, success, |
| 375 error_message)); |
| 376 (*callback).Reset(); |
| 377 } |
| OLD | NEW |