| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/tracing/tracing_ui.h" | 5 #include "content/browser/tracing/tracing_ui.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | |
| 15 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 16 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
| 17 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 18 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 23 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
| 24 #include "base/values.h" | 23 #include "base/values.h" |
| 25 #include "content/browser/tracing/grit/tracing_resources.h" | 24 #include "content/browser/tracing/grit/tracing_resources.h" |
| 26 #include "content/browser/tracing/trace_uploader.h" | |
| 27 #include "content/browser/tracing/tracing_controller_impl.h" | 25 #include "content/browser/tracing/tracing_controller_impl.h" |
| 28 #include "content/public/browser/browser_context.h" | 26 #include "content/public/browser/browser_context.h" |
| 29 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/content_browser_client.h" |
| 29 #include "content/public/browser/trace_uploader.h" |
| 30 #include "content/public/browser/tracing_controller.h" | 30 #include "content/public/browser/tracing_controller.h" |
| 31 #include "content/public/browser/tracing_delegate.h" |
| 31 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/browser/web_ui.h" | 33 #include "content/public/browser/web_ui.h" |
| 33 #include "content/public/browser/web_ui_data_source.h" | 34 #include "content/public/browser/web_ui_data_source.h" |
| 34 #include "content/public/common/content_client.h" | 35 #include "content/public/common/content_client.h" |
| 35 #include "content/public/common/content_switches.h" | |
| 36 #include "content/public/common/url_constants.h" | 36 #include "content/public/common/url_constants.h" |
| 37 | 37 |
| 38 namespace content { | 38 namespace content { |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 const char kUploadURL[] = "https://clients2.google.com/cr/staging_report"; | |
| 42 | |
| 43 void OnGotCategories(const WebUIDataSource::GotDataCallback& callback, | 41 void OnGotCategories(const WebUIDataSource::GotDataCallback& callback, |
| 44 const std::set<std::string>& categorySet) { | 42 const std::set<std::string>& categorySet) { |
| 45 scoped_ptr<base::ListValue> category_list(new base::ListValue()); | 43 scoped_ptr<base::ListValue> category_list(new base::ListValue()); |
| 46 for (std::set<std::string>::const_iterator it = categorySet.begin(); | 44 for (std::set<std::string>::const_iterator it = categorySet.begin(); |
| 47 it != categorySet.end(); it++) { | 45 it != categorySet.end(); it++) { |
| 48 category_list->AppendString(*it); | 46 category_list->AppendString(*it); |
| 49 } | 47 } |
| 50 | 48 |
| 51 base::RefCountedString* res = new base::RefCountedString(); | 49 base::RefCountedString* res = new base::RefCountedString(); |
| 52 base::JSONWriter::Write(category_list.get(), &res->data()); | 50 base::JSONWriter::Write(category_list.get(), &res->data()); |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 | 272 |
| 275 | 273 |
| 276 //////////////////////////////////////////////////////////////////////////////// | 274 //////////////////////////////////////////////////////////////////////////////// |
| 277 // | 275 // |
| 278 // TracingUI | 276 // TracingUI |
| 279 // | 277 // |
| 280 //////////////////////////////////////////////////////////////////////////////// | 278 //////////////////////////////////////////////////////////////////////////////// |
| 281 | 279 |
| 282 TracingUI::TracingUI(WebUI* web_ui) | 280 TracingUI::TracingUI(WebUI* web_ui) |
| 283 : WebUIController(web_ui), | 281 : WebUIController(web_ui), |
| 282 delegate_(GetContentClient()->browser()->GetTracingDelegate()), |
| 284 weak_factory_(this) { | 283 weak_factory_(this) { |
| 285 web_ui->RegisterMessageCallback( | 284 web_ui->RegisterMessageCallback( |
| 286 "doUpload", | 285 "doUpload", |
| 287 base::Bind(&TracingUI::DoUpload, base::Unretained(this))); | 286 base::Bind(&TracingUI::DoUpload, base::Unretained(this))); |
| 288 | 287 |
| 289 // Set up the chrome://tracing/ source. | 288 // Set up the chrome://tracing/ source. |
| 290 BrowserContext* browser_context = | 289 BrowserContext* browser_context = |
| 291 web_ui->GetWebContents()->GetBrowserContext(); | 290 web_ui->GetWebContents()->GetBrowserContext(); |
| 292 | 291 |
| 293 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); | 292 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); |
| 294 source->SetJsonPath("strings.js"); | 293 source->SetJsonPath("strings.js"); |
| 295 source->SetDefaultResource(IDR_TRACING_HTML); | 294 source->SetDefaultResource(IDR_TRACING_HTML); |
| 296 source->AddResourcePath("tracing.js", IDR_TRACING_JS); | 295 source->AddResourcePath("tracing.js", IDR_TRACING_JS); |
| 297 source->SetRequestFilter(base::Bind(OnTracingRequest)); | 296 source->SetRequestFilter(base::Bind(OnTracingRequest)); |
| 298 WebUIDataSource::Add(browser_context, source); | 297 WebUIDataSource::Add(browser_context, source); |
| 299 TracingControllerImpl::GetInstance()->RegisterTracingUI(this); | 298 TracingControllerImpl::GetInstance()->RegisterTracingUI(this); |
| 300 } | 299 } |
| 301 | 300 |
| 302 TracingUI::~TracingUI() { | 301 TracingUI::~TracingUI() { |
| 303 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); | 302 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); |
| 304 } | 303 } |
| 305 | 304 |
| 306 void TracingUI::OnMonitoringStateChanged(bool is_monitoring) { | 305 void TracingUI::OnMonitoringStateChanged(bool is_monitoring) { |
| 307 web_ui()->CallJavascriptFunction( | 306 web_ui()->CallJavascriptFunction( |
| 308 "onMonitoringStateChanged", base::FundamentalValue(is_monitoring)); | 307 "onMonitoringStateChanged", base::FundamentalValue(is_monitoring)); |
| 309 } | 308 } |
| 310 | 309 |
| 311 void TracingUI::DoUpload(const base::ListValue* args) { | 310 void TracingUI::DoUpload(const base::ListValue* args) { |
| 312 const base::CommandLine& command_line = | |
| 313 *base::CommandLine::ForCurrentProcess(); | |
| 314 std::string upload_url = kUploadURL; | |
| 315 if (command_line.HasSwitch(switches::kTraceUploadURL)) { | |
| 316 upload_url = | |
| 317 command_line.GetSwitchValueASCII(switches::kTraceUploadURL); | |
| 318 } | |
| 319 if (!GURL(upload_url).is_valid()) { | |
| 320 upload_url.clear(); | |
| 321 } | |
| 322 | |
| 323 if (upload_url.empty()) { | |
| 324 web_ui()->CallJavascriptFunction("onUploadError", | |
| 325 base::StringValue("Upload URL empty or invalid")); | |
| 326 return; | |
| 327 } | |
| 328 | |
| 329 std::string file_contents; | 311 std::string file_contents; |
| 330 if (!args || args->empty() || !args->GetString(0, &file_contents)) { | 312 if (!args || args->empty() || !args->GetString(0, &file_contents)) { |
| 331 web_ui()->CallJavascriptFunction("onUploadError", | 313 web_ui()->CallJavascriptFunction("onUploadError", |
| 332 base::StringValue("Missing data")); | 314 base::StringValue("Missing data")); |
| 333 return; | 315 return; |
| 334 } | 316 } |
| 335 | 317 |
| 318 if (!delegate_) { |
| 319 web_ui()->CallJavascriptFunction("onUploadError", |
| 320 base::StringValue("Not implemented")); |
| 321 return; |
| 322 } |
| 323 |
| 336 TraceUploader::UploadProgressCallback progress_callback = | 324 TraceUploader::UploadProgressCallback progress_callback = |
| 337 base::Bind(&TracingUI::OnTraceUploadProgress, | 325 base::Bind(&TracingUI::OnTraceUploadProgress, |
| 338 weak_factory_.GetWeakPtr()); | 326 weak_factory_.GetWeakPtr()); |
| 339 TraceUploader::UploadDoneCallback done_callback = | 327 TraceUploader::UploadDoneCallback done_callback = |
| 340 base::Bind(&TracingUI::OnTraceUploadComplete, | 328 base::Bind(&TracingUI::OnTraceUploadComplete, |
| 341 weak_factory_.GetWeakPtr()); | 329 weak_factory_.GetWeakPtr()); |
| 342 | 330 |
| 343 #if defined(OS_WIN) | 331 TraceUploader* uploader = delegate_->GetTraceUploader( |
| 344 const char product[] = "Chrome"; | 332 web_ui()->GetWebContents()->GetBrowserContext()->GetRequestContext()); |
| 345 #elif defined(OS_MACOSX) | |
| 346 const char product[] = "Chrome_Mac"; | |
| 347 #elif defined(OS_LINUX) | |
| 348 const char product[] = "Chrome_Linux"; | |
| 349 #elif defined(OS_ANDROID) | |
| 350 const char product[] = "Chrome_Android"; | |
| 351 #elif defined(OS_CHROMEOS) | |
| 352 const char product[] = "Chrome_ChromeOS"; | |
| 353 #else | |
| 354 #error Platform not supported. | |
| 355 #endif | |
| 356 | 333 |
| 357 // GetProduct() returns a string like "Chrome/aa.bb.cc.dd", split out | 334 uploader->DoUpload(file_contents, progress_callback, done_callback); |
| 358 // the part before the "/". | |
| 359 std::vector<std::string> product_components; | |
| 360 base::SplitString(content::GetContentClient()->GetProduct(), '/', | |
| 361 &product_components); | |
| 362 DCHECK_EQ(2U, product_components.size()); | |
| 363 std::string version; | |
| 364 if (product_components.size() == 2U) { | |
| 365 version = product_components[1]; | |
| 366 } else { | |
| 367 version = "unknown"; | |
| 368 } | |
| 369 | |
| 370 BrowserContext* browser_context = | |
| 371 web_ui()->GetWebContents()->GetBrowserContext(); | |
| 372 TraceUploader* uploader = new TraceUploader( | |
| 373 product, version, upload_url, browser_context->GetRequestContext()); | |
| 374 | |
| 375 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( | |
| 376 &TraceUploader::DoUpload, | |
| 377 base::Unretained(uploader), | |
| 378 file_contents, | |
| 379 progress_callback, | |
| 380 done_callback)); | |
| 381 // TODO(mmandlis): Add support for stopping the upload in progress. | 335 // TODO(mmandlis): Add support for stopping the upload in progress. |
| 382 } | 336 } |
| 383 | 337 |
| 384 void TracingUI::OnTraceUploadProgress(int64 current, int64 total) { | 338 void TracingUI::OnTraceUploadProgress(int64 current, int64 total) { |
| 385 DCHECK(current <= total); | 339 DCHECK(current <= total); |
| 386 int percent = (current / total) * 100; | 340 int percent = (current / total) * 100; |
| 387 web_ui()->CallJavascriptFunction( | 341 web_ui()->CallJavascriptFunction( |
| 388 "onUploadProgress", | 342 "onUploadProgress", |
| 389 base::FundamentalValue(percent), | 343 base::FundamentalValue(percent), |
| 390 base::StringValue(base::StringPrintf("%" PRId64, current)), | 344 base::StringValue(base::StringPrintf("%" PRId64, current)), |
| 391 base::StringValue(base::StringPrintf("%" PRId64, total))); | 345 base::StringValue(base::StringPrintf("%" PRId64, total))); |
| 392 } | 346 } |
| 393 | 347 |
| 394 void TracingUI::OnTraceUploadComplete(bool success, | 348 void TracingUI::OnTraceUploadComplete(bool success, |
| 395 const std::string& report_id, | 349 const std::string& report_id, |
| 396 const std::string& error_message) { | 350 const std::string& error_message) { |
| 397 if (success) { | 351 if (success) { |
| 398 web_ui()->CallJavascriptFunction("onUploadComplete", | 352 web_ui()->CallJavascriptFunction("onUploadComplete", |
| 399 base::StringValue(report_id)); | 353 base::StringValue(report_id)); |
| 400 } else { | 354 } else { |
| 401 web_ui()->CallJavascriptFunction("onUploadError", | 355 web_ui()->CallJavascriptFunction("onUploadError", |
| 402 base::StringValue(error_message)); | 356 base::StringValue(error_message)); |
| 403 } | 357 } |
| 404 } | 358 } |
| 405 | 359 |
| 406 } // namespace content | 360 } // namespace content |
| OLD | NEW |