Chromium Code Reviews| 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" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/json/string_escape.h" | |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/content_browser_client.h" | 22 #include "content/public/browser/content_browser_client.h" |
| 22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/trace_controller.h" | 24 #include "content/public/browser/trace_controller.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 // Messages. | 82 // Messages. |
| 82 void OnTracingControllerInitialized(const base::ListValue* list); | 83 void OnTracingControllerInitialized(const base::ListValue* list); |
| 83 void OnBeginTracing(const base::ListValue* list); | 84 void OnBeginTracing(const base::ListValue* list); |
| 84 void OnEndTracingAsync(const base::ListValue* list); | 85 void OnEndTracingAsync(const base::ListValue* list); |
| 85 void OnBeginRequestBufferPercentFull(const base::ListValue* list); | 86 void OnBeginRequestBufferPercentFull(const base::ListValue* list); |
| 86 void OnLoadTraceFile(const base::ListValue* list); | 87 void OnLoadTraceFile(const base::ListValue* list); |
| 87 void OnSaveTraceFile(const base::ListValue* list); | 88 void OnSaveTraceFile(const base::ListValue* list); |
| 88 void OnGetKnownCategories(const base::ListValue* list); | 89 void OnGetKnownCategories(const base::ListValue* list); |
| 89 | 90 |
| 90 // Callbacks. | 91 // Callbacks. |
| 91 void LoadTraceFileComplete(string16* file_contents); | 92 void LoadTraceFileComplete(string16* file_contents, |
| 93 const base::FilePath &path); | |
| 92 void SaveTraceFileComplete(); | 94 void SaveTraceFileComplete(); |
| 93 | 95 |
| 94 private: | 96 private: |
| 95 // The file dialog to select a file for loading or saving traces. | 97 // The file dialog to select a file for loading or saving traces. |
| 96 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; | 98 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; |
| 97 | 99 |
| 98 // The type of the file dialog as the same one is used for loading or saving | 100 // The type of the file dialog as the same one is used for loading or saving |
| 99 // traces. | 101 // traces. |
| 100 ui::SelectFileDialog::Type select_trace_file_dialog_type_; | 102 ui::SelectFileDialog::Type select_trace_file_dialog_type_; |
| 101 | 103 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 113 | 115 |
| 114 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); | 116 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
| 115 }; | 117 }; |
| 116 | 118 |
| 117 // A proxy passed to the Read and Write tasks used when loading or saving trace | 119 // A proxy passed to the Read and Write tasks used when loading or saving trace |
| 118 // data. | 120 // data. |
| 119 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { | 121 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
| 120 public: | 122 public: |
| 121 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) | 123 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) |
| 122 : handler_(handler) {} | 124 : handler_(handler) {} |
| 123 void LoadTraceFileCompleteProxy(string16* file_contents) { | 125 void LoadTraceFileCompleteProxy(string16* file_contents, |
| 126 const base::FilePath& path) { | |
| 124 if (handler_) | 127 if (handler_) |
| 125 handler_->LoadTraceFileComplete(file_contents); | 128 handler_->LoadTraceFileComplete(file_contents, path); |
| 126 delete file_contents; | 129 delete file_contents; |
| 127 } | 130 } |
| 128 | 131 |
| 129 void SaveTraceFileCompleteProxy() { | 132 void SaveTraceFileCompleteProxy() { |
| 130 if (handler_) | 133 if (handler_) |
| 131 handler_->SaveTraceFileComplete(); | 134 handler_->SaveTraceFileComplete(); |
| 132 } | 135 } |
| 133 | 136 |
| 134 private: | 137 private: |
| 135 friend class base::RefCountedThreadSafe<TaskProxy>; | 138 friend class base::RefCountedThreadSafe<TaskProxy>; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 escaped_contents.push_back(c); | 253 escaped_contents.push_back(c); |
| 251 } | 254 } |
| 252 file_contents.clear(); | 255 file_contents.clear(); |
| 253 | 256 |
| 254 scoped_ptr<string16> contents16(new string16); | 257 scoped_ptr<string16> contents16(new string16); |
| 255 UTF8ToUTF16(escaped_contents).swap(*contents16); | 258 UTF8ToUTF16(escaped_contents).swap(*contents16); |
| 256 | 259 |
| 257 BrowserThread::PostTask( | 260 BrowserThread::PostTask( |
| 258 BrowserThread::UI, FROM_HERE, | 261 BrowserThread::UI, FROM_HERE, |
| 259 base::Bind(&TaskProxy::LoadTraceFileCompleteProxy, proxy, | 262 base::Bind(&TaskProxy::LoadTraceFileCompleteProxy, proxy, |
| 260 contents16.release())); | 263 contents16.release(), |
| 264 path)); | |
| 261 } | 265 } |
| 262 | 266 |
| 263 // A callback used for asynchronously writing a file from a string. Calls the | 267 // A callback used for asynchronously writing a file from a string. Calls the |
| 264 // TaskProxy callback when writing is complete. | 268 // TaskProxy callback when writing is complete. |
| 265 void WriteTraceFileCallback(TaskProxy* proxy, | 269 void WriteTraceFileCallback(TaskProxy* proxy, |
| 266 const base::FilePath& path, | 270 const base::FilePath& path, |
| 267 std::string* contents) { | 271 std::string* contents) { |
| 268 if (!file_util::WriteFile(path, contents->c_str(), contents->size())) | 272 if (!file_util::WriteFile(path, contents->c_str(), contents->size())) |
| 269 return; | 273 return; |
| 270 | 274 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 ui::SelectFileDialog::SELECT_OPEN_FILE, | 321 ui::SelectFileDialog::SELECT_OPEN_FILE, |
| 318 string16(), | 322 string16(), |
| 319 base::FilePath(), | 323 base::FilePath(), |
| 320 NULL, | 324 NULL, |
| 321 0, | 325 0, |
| 322 base::FilePath::StringType(), | 326 base::FilePath::StringType(), |
| 323 web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), | 327 web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), |
| 324 NULL); | 328 NULL); |
| 325 } | 329 } |
| 326 | 330 |
| 327 void TracingMessageHandler::LoadTraceFileComplete(string16* contents) { | 331 void TracingMessageHandler::LoadTraceFileComplete(string16* contents, |
| 332 const base::FilePath& path) { | |
| 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 329 | 334 |
| 330 // We need to pass contents to tracingController.onLoadTraceFileComplete, but | 335 // We need to pass contents to tracingController.onLoadTraceFileComplete, but |
| 331 // that may be arbitrarily big, and IPCs messages are limited in size. So we | 336 // that may be arbitrarily big, and IPCs messages are limited in size. So we |
| 332 // need to cut it into pieces and rebuild the string in Javascript. | 337 // need to cut it into pieces and rebuild the string in Javascript. |
| 333 // |contents| has already been escaped in ReadTraceFileCallback. | 338 // |contents| has already been escaped in ReadTraceFileCallback. |
| 334 // IPC::Channel::kMaximumMessageSize is in bytes, and we need to account for | 339 // IPC::Channel::kMaximumMessageSize is in bytes, and we need to account for |
| 335 // overhead. | 340 // overhead. |
| 336 const size_t kMaxSize = IPC::Channel::kMaximumMessageSize / 2 - 128; | 341 const size_t kMaxSize = IPC::Channel::kMaximumMessageSize / 2 - 128; |
| 337 string16 first_prefix = UTF8ToUTF16("window.traceData = '"); | 342 string16 first_prefix = UTF8ToUTF16("window.traceData = '"); |
| 338 string16 prefix = UTF8ToUTF16("window.traceData += '"); | 343 string16 prefix = UTF8ToUTF16("window.traceData += '"); |
| 339 string16 suffix = UTF8ToUTF16("';"); | 344 string16 suffix = UTF8ToUTF16("';"); |
| 340 | 345 |
| 341 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); | 346 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
| 342 for (size_t i = 0; i < contents->size(); i += kMaxSize) { | 347 for (size_t i = 0; i < contents->size(); i += kMaxSize) { |
| 343 string16 javascript = i == 0 ? first_prefix : prefix; | 348 string16 javascript = i == 0 ? first_prefix : prefix; |
| 344 javascript += contents->substr(i, kMaxSize) + suffix; | 349 javascript += contents->substr(i, kMaxSize) + suffix; |
| 345 rvh->ExecuteJavascriptInWebFrame(string16(), javascript); | 350 rvh->ExecuteJavascriptInWebFrame(string16(), javascript); |
| 346 } | 351 } |
| 352 | |
| 353 // The CallJavascriptFunction is not used because we need to pass | |
| 354 // the first param |window.traceData| through as an un-quoted string. | |
| 347 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( | 355 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( |
| 348 "tracingController.onLoadTraceFileComplete(window.traceData);" | 356 "tracingController.onLoadTraceFileComplete(window.traceData," + |
| 357 base::GetDoubleQuotedJson(path.value()) + ");" + | |
|
nduca
2013/05/17 23:15:12
do you have to call path.reserve(path.lenght*2) li
| |
| 349 "delete window.traceData;")); | 358 "delete window.traceData;")); |
| 350 } | 359 } |
| 351 | 360 |
| 352 void TracingMessageHandler::OnSaveTraceFile(const base::ListValue* list) { | 361 void TracingMessageHandler::OnSaveTraceFile(const base::ListValue* list) { |
| 353 // Only allow a single dialog at a time. | 362 // Only allow a single dialog at a time. |
| 354 if (select_trace_file_dialog_) | 363 if (select_trace_file_dialog_) |
| 355 return; | 364 return; |
| 356 | 365 |
| 357 DCHECK(list->GetSize() == 1); | 366 DCHECK(list->GetSize() == 1); |
| 358 | 367 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { | 553 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
| 545 web_ui->AddMessageHandler(new TracingMessageHandler()); | 554 web_ui->AddMessageHandler(new TracingMessageHandler()); |
| 546 | 555 |
| 547 // Set up the chrome://tracing/ source. | 556 // Set up the chrome://tracing/ source. |
| 548 BrowserContext* browser_context = | 557 BrowserContext* browser_context = |
| 549 web_ui->GetWebContents()->GetBrowserContext(); | 558 web_ui->GetWebContents()->GetBrowserContext(); |
| 550 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); | 559 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); |
| 551 } | 560 } |
| 552 | 561 |
| 553 } // namespace content | 562 } // namespace content |
| OLD | NEW |