| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 virtual void RegisterMessages() OVERRIDE; | 66 virtual void RegisterMessages() OVERRIDE; |
| 67 | 67 |
| 68 // SelectFileDialog::Listener implementation | 68 // SelectFileDialog::Listener implementation |
| 69 virtual void FileSelected(const base::FilePath& path, | 69 virtual void FileSelected(const base::FilePath& path, |
| 70 int index, | 70 int index, |
| 71 void* params) OVERRIDE; | 71 void* params) OVERRIDE; |
| 72 virtual void FileSelectionCanceled(void* params) OVERRIDE; | 72 virtual void FileSelectionCanceled(void* params) OVERRIDE; |
| 73 | 73 |
| 74 // TraceSubscriber implementation. | 74 // TraceSubscriber implementation. |
| 75 virtual void OnEndTracingComplete() OVERRIDE; | 75 virtual void OnEndTracingComplete() OVERRIDE; |
| 76 virtual void OnShowContinuousSamplingTracingComplete() OVERRIDE; |
| 76 virtual void OnTraceDataCollected( | 77 virtual void OnTraceDataCollected( |
| 77 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE; | 78 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE; |
| 79 virtual void OnContinuousSamplingTraceDataCollected( |
| 80 const scoped_refptr<base::RefCountedString>& trace_fragment) OVERRIDE; |
| 78 virtual void OnTraceBufferPercentFullReply(float percent_full) OVERRIDE; | 81 virtual void OnTraceBufferPercentFullReply(float percent_full) OVERRIDE; |
| 79 virtual void OnKnownCategoriesCollected( | 82 virtual void OnKnownCategoriesCollected( |
| 80 const std::set<std::string>& known_categories) OVERRIDE; | 83 const std::set<std::string>& known_categories) OVERRIDE; |
| 81 | 84 |
| 82 // Messages. | 85 // Messages. |
| 83 void OnTracingControllerInitialized(const base::ListValue* list); | 86 void OnTracingControllerInitialized(const base::ListValue* list); |
| 84 void OnBeginTracing(const base::ListValue* list); | 87 void OnBeginTracing(const base::ListValue* list); |
| 85 void OnEndTracingAsync(const base::ListValue* list); | 88 void OnEndTracingAsync(const base::ListValue* list); |
| 89 void OnBeginContinuousSamplingTracing(const base::ListValue* list); |
| 90 void OnEndContinuousSamplingTracing(const base::ListValue* list); |
| 91 void OnShowContinuousSamplingTracingAsync(const base::ListValue* list); |
| 86 void OnBeginRequestBufferPercentFull(const base::ListValue* list); | 92 void OnBeginRequestBufferPercentFull(const base::ListValue* list); |
| 87 void OnLoadTraceFile(const base::ListValue* list); | 93 void OnLoadTraceFile(const base::ListValue* list); |
| 88 void OnSaveTraceFile(const base::ListValue* list); | 94 void OnSaveTraceFile(const base::ListValue* list); |
| 89 void OnGetKnownCategories(const base::ListValue* list); | 95 void OnGetKnownCategories(const base::ListValue* list); |
| 90 | 96 |
| 91 // Callbacks. | 97 // Callbacks. |
| 92 void LoadTraceFileComplete(string16* file_contents, | 98 void LoadTraceFileComplete(string16* file_contents, |
| 93 const base::FilePath &path); | 99 const base::FilePath &path); |
| 94 void SaveTraceFileComplete(); | 100 void SaveTraceFileComplete(); |
| 95 | 101 |
| 96 private: | 102 private: |
| 97 // The file dialog to select a file for loading or saving traces. | 103 // The file dialog to select a file for loading or saving traces. |
| 98 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; | 104 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; |
| 99 | 105 |
| 100 // The type of the file dialog as the same one is used for loading or saving | 106 // The type of the file dialog as the same one is used for loading or saving |
| 101 // traces. | 107 // traces. |
| 102 ui::SelectFileDialog::Type select_trace_file_dialog_type_; | 108 ui::SelectFileDialog::Type select_trace_file_dialog_type_; |
| 103 | 109 |
| 104 // The trace data that is to be written to the file on saving. | 110 // The trace data that is to be written to the file on saving. |
| 105 scoped_ptr<std::string> trace_data_to_save_; | 111 scoped_ptr<std::string> trace_data_to_save_; |
| 106 | 112 |
| 107 // True while tracing is active. | 113 // True while tracing is active. |
| 108 bool trace_enabled_; | 114 bool trace_enabled_; |
| 109 | 115 |
| 116 // True while continuous sampling is active. |
| 117 bool continuous_sampling_trace_enabled_; |
| 118 |
| 110 // True while system tracing is active. | 119 // True while system tracing is active. |
| 111 bool system_trace_in_progress_; | 120 bool system_trace_in_progress_; |
| 112 | 121 |
| 113 void OnEndSystemTracingAck( | 122 void OnEndSystemTracingAck( |
| 114 const scoped_refptr<base::RefCountedString>& events_str_ptr); | 123 const scoped_refptr<base::RefCountedString>& events_str_ptr); |
| 115 | 124 |
| 116 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); | 125 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
| 117 }; | 126 }; |
| 118 | 127 |
| 119 // A proxy passed to the Read and Write tasks used when loading or saving trace | 128 // A proxy passed to the Read and Write tasks used when loading or saving trace |
| (...skipping 26 matching lines...) Expand all Loading... |
| 146 | 155 |
| 147 //////////////////////////////////////////////////////////////////////////////// | 156 //////////////////////////////////////////////////////////////////////////////// |
| 148 // | 157 // |
| 149 // TracingMessageHandler | 158 // TracingMessageHandler |
| 150 // | 159 // |
| 151 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
| 152 | 161 |
| 153 TracingMessageHandler::TracingMessageHandler() | 162 TracingMessageHandler::TracingMessageHandler() |
| 154 : select_trace_file_dialog_type_(ui::SelectFileDialog::SELECT_NONE), | 163 : select_trace_file_dialog_type_(ui::SelectFileDialog::SELECT_NONE), |
| 155 trace_enabled_(false), | 164 trace_enabled_(false), |
| 165 continuous_sampling_trace_enabled_(false), |
| 156 system_trace_in_progress_(false) { | 166 system_trace_in_progress_(false) { |
| 157 } | 167 } |
| 158 | 168 |
| 159 TracingMessageHandler::~TracingMessageHandler() { | 169 TracingMessageHandler::~TracingMessageHandler() { |
| 160 if (select_trace_file_dialog_.get()) | 170 if (select_trace_file_dialog_.get()) |
| 161 select_trace_file_dialog_->ListenerDestroyed(); | 171 select_trace_file_dialog_->ListenerDestroyed(); |
| 162 | 172 |
| 163 // If we are the current subscriber, this will result in ending tracing. | 173 // If we are the current subscriber, this will result in ending tracing. |
| 164 TraceController::GetInstance()->CancelSubscriber(this); | 174 TraceController::GetInstance()->CancelSubscriber(this); |
| 165 | 175 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 178 | 188 |
| 179 web_ui()->RegisterMessageCallback("tracingControllerInitialized", | 189 web_ui()->RegisterMessageCallback("tracingControllerInitialized", |
| 180 base::Bind(&TracingMessageHandler::OnTracingControllerInitialized, | 190 base::Bind(&TracingMessageHandler::OnTracingControllerInitialized, |
| 181 base::Unretained(this))); | 191 base::Unretained(this))); |
| 182 web_ui()->RegisterMessageCallback("beginTracing", | 192 web_ui()->RegisterMessageCallback("beginTracing", |
| 183 base::Bind(&TracingMessageHandler::OnBeginTracing, | 193 base::Bind(&TracingMessageHandler::OnBeginTracing, |
| 184 base::Unretained(this))); | 194 base::Unretained(this))); |
| 185 web_ui()->RegisterMessageCallback("endTracingAsync", | 195 web_ui()->RegisterMessageCallback("endTracingAsync", |
| 186 base::Bind(&TracingMessageHandler::OnEndTracingAsync, | 196 base::Bind(&TracingMessageHandler::OnEndTracingAsync, |
| 187 base::Unretained(this))); | 197 base::Unretained(this))); |
| 198 web_ui()->RegisterMessageCallback("beginContinuousSamplingTracing", |
| 199 base::Bind(&TracingMessageHandler::OnBeginContinuousSamplingTracing, |
| 200 base::Unretained(this))); |
| 201 web_ui()->RegisterMessageCallback("endContinuousSamplingTracing", |
| 202 base::Bind(&TracingMessageHandler::OnEndContinuousSamplingTracing, |
| 203 base::Unretained(this))); |
| 204 web_ui()->RegisterMessageCallback("showContinuousSamplingTracingAsync", |
| 205 base::Bind(&TracingMessageHandler::OnShowContinuousSamplingTracingAsync, |
| 206 base::Unretained(this))); |
| 188 web_ui()->RegisterMessageCallback("beginRequestBufferPercentFull", | 207 web_ui()->RegisterMessageCallback("beginRequestBufferPercentFull", |
| 189 base::Bind(&TracingMessageHandler::OnBeginRequestBufferPercentFull, | 208 base::Bind(&TracingMessageHandler::OnBeginRequestBufferPercentFull, |
| 190 base::Unretained(this))); | 209 base::Unretained(this))); |
| 191 web_ui()->RegisterMessageCallback("loadTraceFile", | 210 web_ui()->RegisterMessageCallback("loadTraceFile", |
| 192 base::Bind(&TracingMessageHandler::OnLoadTraceFile, | 211 base::Bind(&TracingMessageHandler::OnLoadTraceFile, |
| 193 base::Unretained(this))); | 212 base::Unretained(this))); |
| 194 web_ui()->RegisterMessageCallback("saveTraceFile", | 213 web_ui()->RegisterMessageCallback("saveTraceFile", |
| 195 base::Bind(&TracingMessageHandler::OnSaveTraceFile, | 214 base::Bind(&TracingMessageHandler::OnSaveTraceFile, |
| 196 base::Unretained(this))); | 215 base::Unretained(this))); |
| 197 web_ui()->RegisterMessageCallback("getKnownCategories", | 216 web_ui()->RegisterMessageCallback("getKnownCategories", |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 return; | 492 return; |
| 474 #endif | 493 #endif |
| 475 } | 494 } |
| 476 | 495 |
| 477 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); | 496 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
| 478 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( | 497 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( |
| 479 "tracingController.onEndTracingComplete(window.traceData);" | 498 "tracingController.onEndTracingComplete(window.traceData);" |
| 480 "delete window.traceData;")); | 499 "delete window.traceData;")); |
| 481 } | 500 } |
| 482 | 501 |
| 502 void TracingMessageHandler::OnBeginContinuousSamplingTracing( |
| 503 const base::ListValue* args) { |
| 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 505 DCHECK_EQ(args->GetSize(), (size_t) 0); |
| 506 |
| 507 continuous_sampling_trace_enabled_ = true; |
| 508 TraceController::GetInstance()->BeginContinuousSamplingTracing(this); |
| 509 } |
| 510 |
| 511 void TracingMessageHandler::OnEndContinuousSamplingTracing( |
| 512 const base::ListValue* args) { |
| 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 514 DCHECK_EQ(args->GetSize(), (size_t) 0); |
| 515 |
| 516 continuous_sampling_trace_enabled_ = false; |
| 517 TraceController::GetInstance()->EndContinuousSamplingTracing(this); |
| 518 } |
| 519 |
| 520 void TracingMessageHandler::OnShowContinuousSamplingTracingAsync( |
| 521 const base::ListValue* list) { |
| 522 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 523 |
| 524 // When this function yields, we expect to get some number of |
| 525 // OnContinuousSamplingTraceDataCollected callbacks, which will append data |
| 526 // to window.continuousSamplingTraceData. To set up for this, set |
| 527 // window.continuousSamplingTraceData to the empty string. |
| 528 web_ui()->GetWebContents()->GetRenderViewHost()-> |
| 529 ExecuteJavascriptInWebFrame(string16(), |
| 530 UTF8ToUTF16("window.continuousSamplingTraceData = '';")); |
| 531 |
| 532 if (!TraceController::GetInstance()->ShowContinuousSamplingTracingAsync( |
| 533 this)) { |
| 534 OnShowContinuousSamplingTracingComplete(); |
| 535 } |
| 536 } |
| 537 |
| 538 void TracingMessageHandler::OnShowContinuousSamplingTracingComplete() { |
| 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 540 |
| 541 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
| 542 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( |
| 543 "tracingController.onShowContinuousSamplingTracingComplete" |
| 544 "(window.continuousSamplingTraceData);" |
| 545 "delete window.continuousSamplingTraceData;")); |
| 546 } |
| 547 |
| 483 void TracingMessageHandler::OnEndSystemTracingAck( | 548 void TracingMessageHandler::OnEndSystemTracingAck( |
| 484 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 549 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 485 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 486 | 551 |
| 487 web_ui()->CallJavascriptFunction( | 552 web_ui()->CallJavascriptFunction( |
| 488 "tracingController.onSystemTraceDataCollected", | 553 "tracingController.onSystemTraceDataCollected", |
| 489 *scoped_ptr<base::Value>(new base::StringValue(events_str_ptr->data()))); | 554 *scoped_ptr<base::Value>(new base::StringValue(events_str_ptr->data()))); |
| 490 DCHECK(!system_trace_in_progress_); | 555 DCHECK(!system_trace_in_progress_); |
| 491 | 556 |
| 492 OnEndTracingComplete(); | 557 OnEndTracingComplete(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 503 | 568 |
| 504 // Intentionally append a , to the traceData. This technically causes all | 569 // Intentionally append a , to the traceData. This technically causes all |
| 505 // traceData that we pass back to JS to end with a comma, but that is actually | 570 // traceData that we pass back to JS to end with a comma, but that is actually |
| 506 // something the JS side strips away anyway | 571 // something the JS side strips away anyway |
| 507 javascript.append(",\";"); | 572 javascript.append(",\";"); |
| 508 | 573 |
| 509 web_ui()->GetWebContents()->GetRenderViewHost()-> | 574 web_ui()->GetWebContents()->GetRenderViewHost()-> |
| 510 ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(javascript)); | 575 ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(javascript)); |
| 511 } | 576 } |
| 512 | 577 |
| 578 void TracingMessageHandler::OnContinuousSamplingTraceDataCollected( |
| 579 const scoped_refptr<base::RefCountedString>& trace_fragment) { |
| 580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 581 |
| 582 std::string javascript; |
| 583 javascript.reserve(trace_fragment->size() * 2); |
| 584 javascript.append("window.continuousSamplingTraceData += \""); |
| 585 base::JsonDoubleQuote(trace_fragment->data(), false, &javascript); |
| 586 |
| 587 // Intentionally append a , to the traceData. This technically causes all |
| 588 // traceData that we pass back to JS to end with a comma, but that is actually |
| 589 // something the JS side strips away anyway |
| 590 javascript.append(",\";"); |
| 591 |
| 592 web_ui()->GetWebContents()->GetRenderViewHost()-> |
| 593 ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16(javascript)); |
| 594 } |
| 595 |
| 513 void TracingMessageHandler::OnTraceBufferPercentFullReply(float percent_full) { | 596 void TracingMessageHandler::OnTraceBufferPercentFullReply(float percent_full) { |
| 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 515 web_ui()->CallJavascriptFunction( | 598 web_ui()->CallJavascriptFunction( |
| 516 "tracingController.onRequestBufferPercentFullComplete", | 599 "tracingController.onRequestBufferPercentFullComplete", |
| 517 *scoped_ptr<base::Value>(new base::FundamentalValue(percent_full))); | 600 *scoped_ptr<base::Value>(new base::FundamentalValue(percent_full))); |
| 518 } | 601 } |
| 519 | 602 |
| 520 void TracingMessageHandler::OnGetKnownCategories(const base::ListValue* list) { | 603 void TracingMessageHandler::OnGetKnownCategories(const base::ListValue* list) { |
| 521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 522 if (!TraceController::GetInstance()->GetKnownCategoryGroupsAsync(this)) { | 605 if (!TraceController::GetInstance()->GetKnownCategoryGroupsAsync(this)) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 552 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { | 635 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
| 553 web_ui->AddMessageHandler(new TracingMessageHandler()); | 636 web_ui->AddMessageHandler(new TracingMessageHandler()); |
| 554 | 637 |
| 555 // Set up the chrome://tracing/ source. | 638 // Set up the chrome://tracing/ source. |
| 556 BrowserContext* browser_context = | 639 BrowserContext* browser_context = |
| 557 web_ui->GetWebContents()->GetBrowserContext(); | 640 web_ui->GetWebContents()->GetBrowserContext(); |
| 558 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); | 641 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); |
| 559 } | 642 } |
| 560 | 643 |
| 561 } // namespace content | 644 } // namespace content |
| OLD | NEW |