| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // Messages. | 82 // Messages. |
| 83 void OnTracingControllerInitialized(const base::ListValue* list); | 83 void OnTracingControllerInitialized(const base::ListValue* list); |
| 84 void OnBeginTracing(const base::ListValue* list); | 84 void OnBeginTracing(const base::ListValue* list); |
| 85 void OnEndTracingAsync(const base::ListValue* list); | 85 void OnEndTracingAsync(const base::ListValue* list); |
| 86 void OnBeginRequestBufferPercentFull(const base::ListValue* list); | 86 void OnBeginRequestBufferPercentFull(const base::ListValue* list); |
| 87 void OnLoadTraceFile(const base::ListValue* list); | 87 void OnLoadTraceFile(const base::ListValue* list); |
| 88 void OnSaveTraceFile(const base::ListValue* list); | 88 void OnSaveTraceFile(const base::ListValue* list); |
| 89 void OnGetKnownCategories(const base::ListValue* list); | 89 void OnGetKnownCategories(const base::ListValue* list); |
| 90 | 90 |
| 91 // Callbacks. | 91 // Callbacks. |
| 92 void LoadTraceFileComplete(string16* file_contents); | 92 void LoadTraceFileComplete(string16* file_contents, |
| 93 const base::FilePath &path); |
| 93 void SaveTraceFileComplete(); | 94 void SaveTraceFileComplete(); |
| 94 | 95 |
| 95 private: | 96 private: |
| 96 // 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. |
| 97 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; | 98 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; |
| 98 | 99 |
| 99 // 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 |
| 100 // traces. | 101 // traces. |
| 101 ui::SelectFileDialog::Type select_trace_file_dialog_type_; | 102 ui::SelectFileDialog::Type select_trace_file_dialog_type_; |
| 102 | 103 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 | 115 |
| 115 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); | 116 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
| 116 }; | 117 }; |
| 117 | 118 |
| 118 // 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 |
| 119 // data. | 120 // data. |
| 120 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { | 121 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
| 121 public: | 122 public: |
| 122 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) | 123 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) |
| 123 : handler_(handler) {} | 124 : handler_(handler) {} |
| 124 void LoadTraceFileCompleteProxy(string16* file_contents) { | 125 void LoadTraceFileCompleteProxy(string16* file_contents, |
| 126 const base::FilePath& path) { |
| 125 if (handler_) | 127 if (handler_) |
| 126 handler_->LoadTraceFileComplete(file_contents); | 128 handler_->LoadTraceFileComplete(file_contents, path); |
| 127 delete file_contents; | 129 delete file_contents; |
| 128 } | 130 } |
| 129 | 131 |
| 130 void SaveTraceFileCompleteProxy() { | 132 void SaveTraceFileCompleteProxy() { |
| 131 if (handler_) | 133 if (handler_) |
| 132 handler_->SaveTraceFileComplete(); | 134 handler_->SaveTraceFileComplete(); |
| 133 } | 135 } |
| 134 | 136 |
| 135 private: | 137 private: |
| 136 friend class base::RefCountedThreadSafe<TaskProxy>; | 138 friend class base::RefCountedThreadSafe<TaskProxy>; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 escaped_contents.push_back(c); | 253 escaped_contents.push_back(c); |
| 252 } | 254 } |
| 253 file_contents.clear(); | 255 file_contents.clear(); |
| 254 | 256 |
| 255 scoped_ptr<string16> contents16(new string16); | 257 scoped_ptr<string16> contents16(new string16); |
| 256 UTF8ToUTF16(escaped_contents).swap(*contents16); | 258 UTF8ToUTF16(escaped_contents).swap(*contents16); |
| 257 | 259 |
| 258 BrowserThread::PostTask( | 260 BrowserThread::PostTask( |
| 259 BrowserThread::UI, FROM_HERE, | 261 BrowserThread::UI, FROM_HERE, |
| 260 base::Bind(&TaskProxy::LoadTraceFileCompleteProxy, proxy, | 262 base::Bind(&TaskProxy::LoadTraceFileCompleteProxy, proxy, |
| 261 contents16.release())); | 263 contents16.release(), |
| 264 path)); |
| 262 } | 265 } |
| 263 | 266 |
| 264 // 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 |
| 265 // TaskProxy callback when writing is complete. | 268 // TaskProxy callback when writing is complete. |
| 266 void WriteTraceFileCallback(TaskProxy* proxy, | 269 void WriteTraceFileCallback(TaskProxy* proxy, |
| 267 const base::FilePath& path, | 270 const base::FilePath& path, |
| 268 std::string* contents) { | 271 std::string* contents) { |
| 269 if (!file_util::WriteFile(path, contents->c_str(), contents->size())) | 272 if (!file_util::WriteFile(path, contents->c_str(), contents->size())) |
| 270 return; | 273 return; |
| 271 | 274 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 ui::SelectFileDialog::SELECT_OPEN_FILE, | 321 ui::SelectFileDialog::SELECT_OPEN_FILE, |
| 319 string16(), | 322 string16(), |
| 320 base::FilePath(), | 323 base::FilePath(), |
| 321 NULL, | 324 NULL, |
| 322 0, | 325 0, |
| 323 base::FilePath::StringType(), | 326 base::FilePath::StringType(), |
| 324 web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), | 327 web_ui()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), |
| 325 NULL); | 328 NULL); |
| 326 } | 329 } |
| 327 | 330 |
| 328 void TracingMessageHandler::LoadTraceFileComplete(string16* contents) { | 331 void TracingMessageHandler::LoadTraceFileComplete(string16* contents, |
| 332 const base::FilePath& path) { |
| 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 330 | 334 |
| 331 // We need to pass contents to tracingController.onLoadTraceFileComplete, but | 335 // We need to pass contents to tracingController.onLoadTraceFileComplete, but |
| 332 // 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 |
| 333 // 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. |
| 334 // |contents| has already been escaped in ReadTraceFileCallback. | 338 // |contents| has already been escaped in ReadTraceFileCallback. |
| 335 // 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 |
| 336 // overhead. | 340 // overhead. |
| 337 const size_t kMaxSize = IPC::Channel::kMaximumMessageSize / 2 - 128; | 341 const size_t kMaxSize = IPC::Channel::kMaximumMessageSize / 2 - 128; |
| 338 string16 first_prefix = UTF8ToUTF16("window.traceData = '"); | 342 string16 first_prefix = UTF8ToUTF16("window.traceData = '"); |
| 339 string16 prefix = UTF8ToUTF16("window.traceData += '"); | 343 string16 prefix = UTF8ToUTF16("window.traceData += '"); |
| 340 string16 suffix = UTF8ToUTF16("';"); | 344 string16 suffix = UTF8ToUTF16("';"); |
| 341 | 345 |
| 342 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); | 346 RenderViewHost* rvh = web_ui()->GetWebContents()->GetRenderViewHost(); |
| 343 for (size_t i = 0; i < contents->size(); i += kMaxSize) { | 347 for (size_t i = 0; i < contents->size(); i += kMaxSize) { |
| 344 string16 javascript = i == 0 ? first_prefix : prefix; | 348 string16 javascript = i == 0 ? first_prefix : prefix; |
| 345 javascript += contents->substr(i, kMaxSize) + suffix; | 349 javascript += contents->substr(i, kMaxSize) + suffix; |
| 346 rvh->ExecuteJavascriptInWebFrame(string16(), javascript); | 350 rvh->ExecuteJavascriptInWebFrame(string16(), javascript); |
| 347 } | 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. |
| 348 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( | 355 rvh->ExecuteJavascriptInWebFrame(string16(), UTF8ToUTF16( |
| 349 "tracingController.onLoadTraceFileComplete(window.traceData);" | 356 "tracingController.onLoadTraceFileComplete(window.traceData," + |
| 357 base::GetDoubleQuotedJson(path.value()) + ");" + |
| 350 "delete window.traceData;")); | 358 "delete window.traceData;")); |
| 351 } | 359 } |
| 352 | 360 |
| 353 void TracingMessageHandler::OnSaveTraceFile(const base::ListValue* list) { | 361 void TracingMessageHandler::OnSaveTraceFile(const base::ListValue* list) { |
| 354 // Only allow a single dialog at a time. | 362 // Only allow a single dialog at a time. |
| 355 if (select_trace_file_dialog_) | 363 if (select_trace_file_dialog_) |
| 356 return; | 364 return; |
| 357 | 365 |
| 358 DCHECK(list->GetSize() == 1); | 366 DCHECK(list->GetSize() == 1); |
| 359 | 367 |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { | 552 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
| 545 web_ui->AddMessageHandler(new TracingMessageHandler()); | 553 web_ui->AddMessageHandler(new TracingMessageHandler()); |
| 546 | 554 |
| 547 // Set up the chrome://tracing/ source. | 555 // Set up the chrome://tracing/ source. |
| 548 BrowserContext* browser_context = | 556 BrowserContext* browser_context = |
| 549 web_ui->GetWebContents()->GetBrowserContext(); | 557 web_ui->GetWebContents()->GetBrowserContext(); |
| 550 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); | 558 WebUIDataSource::Add(browser_context, CreateTracingHTMLSource()); |
| 551 } | 559 } |
| 552 | 560 |
| 553 } // namespace content | 561 } // namespace content |
| OLD | NEW |