OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/webui/gpu_internals_ui.h" | 5 #include "chrome/browser/webui/gpu_internals_ui.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/singleton.h" | 16 #include "base/singleton.h" |
17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
18 #include "base/string_piece.h" | 18 #include "base/string_piece.h" |
19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "base/debug/trace_event.h" |
21 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
22 #include "chrome/browser/browser_thread.h" | 23 #include "chrome/browser/browser_thread.h" |
23 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 24 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
24 #include "chrome/browser/gpu_process_host.h" | 25 #include "chrome/browser/gpu_process_host.h" |
25 #include "chrome/browser/gpu_process_host_ui_shim.h" | 26 #include "chrome/browser/gpu_process_host_ui_shim.h" |
26 #include "chrome/browser/io_thread.h" | 27 #include "chrome/browser/io_thread.h" |
27 #include "chrome/browser/net/chrome_net_log.h" | 28 #include "chrome/browser/net/chrome_net_log.h" |
28 #include "chrome/browser/net/connection_tester.h" | 29 #include "chrome/browser/net/connection_tester.h" |
29 #include "chrome/browser/net/passive_log_collector.h" | 30 #include "chrome/browser/net/passive_log_collector.h" |
30 #include "chrome/browser/net/url_fixer_upper.h" | 31 #include "chrome/browser/net/url_fixer_upper.h" |
31 #include "chrome/browser/platform_util.h" | 32 #include "chrome/browser/platform_util.h" |
32 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
33 #include "chrome/browser/tab_contents/tab_contents.h" | 34 #include "chrome/browser/tab_contents/tab_contents.h" |
| 35 #include "chrome/browser/renderer_host/render_process_host.h" |
| 36 #include "chrome/browser/renderer_host/render_view_host.h" |
34 #include "chrome/common/chrome_paths.h" | 37 #include "chrome/common/chrome_paths.h" |
35 #include "chrome/common/chrome_version_info.h" | 38 #include "chrome/common/chrome_version_info.h" |
36 #include "chrome/common/jstemplate_builder.h" | 39 #include "chrome/common/jstemplate_builder.h" |
37 #include "chrome/common/net/url_request_context_getter.h" | 40 #include "chrome/common/net/url_request_context_getter.h" |
38 #include "chrome/common/url_constants.h" | 41 #include "chrome/common/url_constants.h" |
39 #include "grit/browser_resources.h" | 42 #include "grit/browser_resources.h" |
40 #include "grit/generated_resources.h" | 43 #include "grit/generated_resources.h" |
41 #include "net/base/escape.h" | 44 #include "net/base/escape.h" |
42 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
43 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
(...skipping 25 matching lines...) Expand all Loading... |
69 public: | 72 public: |
70 GpuMessageHandler(); | 73 GpuMessageHandler(); |
71 virtual ~GpuMessageHandler(); | 74 virtual ~GpuMessageHandler(); |
72 | 75 |
73 // WebUIMessageHandler implementation. | 76 // WebUIMessageHandler implementation. |
74 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | 77 virtual WebUIMessageHandler* Attach(WebUI* web_ui); |
75 virtual void RegisterMessages(); | 78 virtual void RegisterMessages(); |
76 | 79 |
77 // Mesages | 80 // Mesages |
78 void OnCallAsync(const ListValue* list); | 81 void OnCallAsync(const ListValue* list); |
| 82 void OnBeginTracing(const ListValue* list); |
| 83 void OnBeginToEndTracing(const ListValue* list); |
79 | 84 |
80 // Submessages dispatched from OnCallAsync | 85 // Submessages dispatched from OnCallAsync |
81 Value* OnRequestGpuInfo(const ListValue* list); | 86 Value* OnRequestGpuInfo(const ListValue* list); |
82 Value* OnRequestClientInfo(const ListValue* list); | 87 Value* OnRequestClientInfo(const ListValue* list); |
83 Value* OnRequestLogMessages(const ListValue* list); | 88 Value* OnRequestLogMessages(const ListValue* list); |
84 | 89 |
| 90 // Callbacks from GpuTrace or ChildProcessHosts |
| 91 void OnTraceDataCollected(const std::string& json_events); |
| 92 |
85 // Executes the javascript function |function_name| in the renderer, passing | 93 // Executes the javascript function |function_name| in the renderer, passing |
86 // it the argument |value|. | 94 // it the argument |value|. |
87 void CallJavascriptFunction(const std::wstring& function_name, | 95 void CallJavascriptFunction(const std::wstring& function_name, |
88 const Value* value); | 96 const Value* value); |
89 | 97 |
90 private: | 98 private: |
91 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler); | 99 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler); |
| 100 |
| 101 void OnEndTracingComplete(); |
| 102 friend class TaskProxy; |
| 103 |
| 104 bool traceEnabled_; |
| 105 int traceNumEndAcksPending_; |
| 106 }; |
| 107 |
| 108 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
| 109 public: |
| 110 TaskProxy(const base::WeakPtr<GpuMessageHandler>& handler) |
| 111 : handler_(handler) {} |
| 112 void OnEndTracingCompleteProxy() { |
| 113 if(handler_) { |
| 114 handler_->OnEndTracingComplete(); |
| 115 } |
| 116 } |
| 117 private: |
| 118 base::WeakPtr<GpuMessageHandler> handler_; |
| 119 friend class base::RefCountedThreadSafe<TaskProxy>; |
| 120 DISALLOW_COPY_AND_ASSIGN(TaskProxy); |
92 }; | 121 }; |
93 | 122 |
94 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
95 // | 124 // |
96 // GpuHTMLSource | 125 // GpuHTMLSource |
97 // | 126 // |
98 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
99 | 128 |
100 GpuHTMLSource::GpuHTMLSource() | 129 GpuHTMLSource::GpuHTMLSource() |
101 : DataSource(chrome::kChromeUIGpuInternalsHost, MessageLoop::current()) { | 130 : DataSource(chrome::kChromeUIGpuInternalsHost, MessageLoop::current()) { |
(...skipping 25 matching lines...) Expand all Loading... |
127 std::string GpuHTMLSource::GetMimeType(const std::string&) const { | 156 std::string GpuHTMLSource::GetMimeType(const std::string&) const { |
128 return "text/html"; | 157 return "text/html"; |
129 } | 158 } |
130 | 159 |
131 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
132 // | 161 // |
133 // GpuMessageHandler | 162 // GpuMessageHandler |
134 // | 163 // |
135 //////////////////////////////////////////////////////////////////////////////// | 164 //////////////////////////////////////////////////////////////////////////////// |
136 | 165 |
137 GpuMessageHandler::GpuMessageHandler() { | 166 GpuMessageHandler::GpuMessageHandler() |
| 167 : traceEnabled_(false) |
| 168 , traceNumEndAcksPending_(0) { |
| 169 |
138 } | 170 } |
139 | 171 |
140 GpuMessageHandler::~GpuMessageHandler() {} | 172 GpuMessageHandler::~GpuMessageHandler() { |
| 173 if (traceEnabled_) { |
| 174 OnBeginToEndTracing(NULL); |
| 175 } |
| 176 } |
141 | 177 |
142 WebUIMessageHandler* GpuMessageHandler::Attach(WebUI* web_ui) { | 178 WebUIMessageHandler* GpuMessageHandler::Attach(WebUI* web_ui) { |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
144 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | 180 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); |
145 return result; | 181 return result; |
146 } | 182 } |
147 | 183 |
148 /* BrowserBridge.callAsync prepends a requestID to these messages. */ | 184 /* BrowserBridge.callAsync prepends a requestID to these messages. */ |
149 void GpuMessageHandler::RegisterMessages() { | 185 void GpuMessageHandler::RegisterMessages() { |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 } else { | 380 } else { |
345 return NULL; | 381 return NULL; |
346 } | 382 } |
347 } | 383 } |
348 | 384 |
349 Value* GpuMessageHandler::OnRequestLogMessages(const ListValue*) { | 385 Value* GpuMessageHandler::OnRequestLogMessages(const ListValue*) { |
350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
351 return GpuProcessHostUIShim::GetInstance()->logMessages(); | 387 return GpuProcessHostUIShim::GetInstance()->logMessages(); |
352 } | 388 } |
353 | 389 |
| 390 void GpuMessageHandler::OnBeginTracing(const ListValue* args) { |
| 391 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 392 |
| 393 traceEnabled_ = true; |
| 394 base::debug::TraceLog::GetInstance()->SetOutputCallback(NewCallback(this, |
| 395 &GpuMessageHandler::OnTraceDataCollected)); |
| 396 base::debug::TraceLog::GetInstance()->SetEnabled(true); |
| 397 GpuProcessHostUIShim::GetInstance()->SetTraceEnabled(true); |
| 398 RenderProcessHost::SetTraceEnabled(true); |
| 399 } |
| 400 |
| 401 void GpuMessageHandler::OnBeginToEndTracing(const ListValue* list) { |
| 402 DCHECK(traceEnabled_ && !traceNumEndAcksPending_); |
| 403 |
| 404 // To end GPU tracing, we have to end tracing on every child process |
| 405 // and get their ack that we've recieved that data. We do this by |
| 406 // tracking the number of ack's we're expecting. When this number |
| 407 // goes to zero, we know we're done. The TRACE_ENDED ack arrives as a |
| 408 // JSON object in the OnTraceDataCollectedAsJSON. |
| 409 traceNumEndAcksPending_ = 1; |
| 410 GpuProcessHostUIShim::GetInstance()->SetTraceEnabled(false); |
| 411 |
| 412 traceNumEndAcksPending_ += |
| 413 RenderProcessHost::SetTraceEnabled(false); |
| 414 } |
| 415 |
| 416 void GpuMessageHandler::OnEndTracingComplete() { |
| 417 // TODO(nduca): disable renderer processes, too |
| 418 base::debug::TraceLog::GetInstance()->SetEnabled(false); |
| 419 base::debug::TraceLog::GetInstance()->SetOutputCallback(NULL); |
| 420 traceEnabled_ = false; |
| 421 web_ui_->CallJavascriptFunction(L"tracingControl.onEndTracingComplete"); |
| 422 } |
| 423 |
| 424 void GpuMessageHandler::OnTraceDataCollected(const std::string& json_events) { |
| 425 DCHECK(traceEnabled_); |
| 426 const char* json_complete = "['TRACE_ENDED']"; |
| 427 if(json_events == json_complete) { |
| 428 DCHECK(traceNumEndAcksPending_); |
| 429 traceNumEndAcksPending_ -= 1; |
| 430 if(traceNumEndAcksPending_ == 0) { |
| 431 TaskProxy* task = new TaskProxy(AsWeakPtr()); |
| 432 task->AddRef(); |
| 433 |
| 434 BrowserThread::PostTask( |
| 435 BrowserThread::FILE, FROM_HERE, |
| 436 NewRunnableMethod( |
| 437 task, &TaskProxy::OnEndTracingCompleteProxy)); |
| 438 } |
| 439 } else { |
| 440 |
| 441 std::wstring javascript; |
| 442 javascript += L"tracingControl.onTraceDataCollected("; |
| 443 javascript += UTF8ToWide(json_events); |
| 444 javascript += L");"; |
| 445 |
| 446 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), |
| 447 WideToUTF16Hack(javascript)); |
| 448 } |
| 449 } |
| 450 |
| 451 |
| 452 |
354 } // namespace | 453 } // namespace |
355 | 454 |
356 | 455 |
357 | 456 |
358 //////////////////////////////////////////////////////////////////////////////// | 457 //////////////////////////////////////////////////////////////////////////////// |
359 // | 458 // |
360 // GpuInternalsUI | 459 // GpuInternalsUI |
361 // | 460 // |
362 //////////////////////////////////////////////////////////////////////////////// | 461 //////////////////////////////////////////////////////////////////////////////// |
363 | 462 |
364 GpuInternalsUI::GpuInternalsUI(TabContents* contents) : WebUI(contents) { | 463 GpuInternalsUI::GpuInternalsUI(TabContents* contents) : WebUI(contents) { |
365 AddMessageHandler((new GpuMessageHandler())->Attach(this)); | 464 AddMessageHandler((new GpuMessageHandler())->Attach(this)); |
366 | 465 |
367 GpuHTMLSource* html_source = new GpuHTMLSource(); | 466 GpuHTMLSource* html_source = new GpuHTMLSource(); |
368 | 467 |
369 // Set up the chrome://gpu/ source. | 468 // Set up the chrome://gpu/ source. |
370 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); | 469 contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source); |
371 } | 470 } |
372 | 471 |
OLD | NEW |