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/ui/webui/gpu_internals_ui.h" | 5 #include "chrome/browser/ui/webui/tracing_ui.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <string> | 7 #include <string> |
9 #include <utility> | |
10 #include <vector> | |
11 | 8 |
12 #include "base/command_line.h" | 9 #include "base/command_line.h" |
13 #include "base/file_util.h" | 10 #include "base/file_util.h" |
14 #include "base/memory/singleton.h" | |
15 #include "base/message_loop.h" | |
16 #include "base/path_service.h" | |
17 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
18 #include "base/stringprintf.h" | |
19 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
20 #include "base/string_piece.h" | |
21 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
22 #include "base/values.h" | |
23 #include "chrome/browser/browser_process.h" | |
24 #include "chrome/browser/io_thread.h" | |
25 #include "chrome/browser/net/chrome_net_log.h" | |
26 #include "chrome/browser/net/connection_tester.h" | |
27 #include "chrome/browser/net/passive_log_collector.h" | |
28 #include "chrome/browser/net/url_fixer_upper.h" | |
29 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/browser/ui/shell_dialogs.h" | 15 #include "chrome/browser/ui/shell_dialogs.h" |
31 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
32 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
33 #include "chrome/common/chrome_paths.h" | |
34 #include "chrome/common/chrome_version_info.h" | 17 #include "chrome/common/chrome_version_info.h" |
35 #include "chrome/common/jstemplate_builder.h" | |
36 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
37 #include "content/browser/browser_thread.h" | |
38 #include "content/browser/gpu/gpu_data_manager.h" | 19 #include "content/browser/gpu/gpu_data_manager.h" |
39 #include "content/browser/gpu/gpu_process_host.h" | |
40 #include "content/browser/renderer_host/render_view_host.h" | 20 #include "content/browser/renderer_host/render_view_host.h" |
41 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
42 #include "content/browser/tab_contents/tab_contents_view.h" | 22 #include "content/browser/tab_contents/tab_contents_view.h" |
43 #include "content/browser/trace_controller.h" | 23 #include "content/browser/trace_controller.h" |
44 #include "grit/browser_resources.h" | 24 #include "grit/browser_resources.h" |
45 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
46 #include "net/base/escape.h" | |
47 #include "net/url_request/url_request_context_getter.h" | |
48 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
49 #include "ui/base/resource/resource_bundle.h" | |
50 | 27 |
51 namespace { | 28 namespace { |
52 | 29 |
53 ChromeWebUIDataSource* CreateGpuHTMLSource() { | 30 ChromeWebUIDataSource* CreateTracingHTMLSource() { |
54 ChromeWebUIDataSource* source = | 31 ChromeWebUIDataSource* source = |
55 new ChromeWebUIDataSource(chrome::kChromeUIGpuInternalsHost); | 32 new ChromeWebUIDataSource(chrome::kChromeUITracingHost); |
56 | 33 |
57 source->set_json_path("strings.js"); | 34 source->set_json_path("strings.js"); |
58 source->add_resource_path("gpu_internals.js", IDR_GPU_INTERNALS_JS); | 35 source->add_resource_path("tracing.js", IDR_TRACING_JS); |
59 source->set_default_resource(IDR_GPU_INTERNALS_HTML); | 36 source->set_default_resource(IDR_TRACING_HTML); |
| 37 source->AddLocalizedString("tracingTitle", IDS_TRACING_TITLE); |
60 return source; | 38 return source; |
61 } | 39 } |
62 | 40 |
63 // This class receives javascript messages from the renderer. | 41 // This class receives javascript messages from the renderer. |
64 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 42 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
65 // this class's methods are expected to run on the UI thread. | 43 // this class's methods are expected to run on the UI thread. |
66 class GpuMessageHandler | 44 class TracingMessageHandler |
67 : public WebUIMessageHandler, | 45 : public WebUIMessageHandler, |
68 public SelectFileDialog::Listener, | 46 public SelectFileDialog::Listener, |
69 public base::SupportsWeakPtr<GpuMessageHandler>, | 47 public base::SupportsWeakPtr<TracingMessageHandler>, |
70 public TraceSubscriber { | 48 public TraceSubscriber { |
71 public: | 49 public: |
72 GpuMessageHandler(); | 50 TracingMessageHandler(); |
73 virtual ~GpuMessageHandler(); | 51 virtual ~TracingMessageHandler(); |
74 | 52 |
75 // WebUIMessageHandler implementation. | 53 // WebUIMessageHandler implementation. |
76 virtual WebUIMessageHandler* Attach(WebUI* web_ui) OVERRIDE; | 54 virtual WebUIMessageHandler* Attach(WebUI* web_ui); |
77 virtual void RegisterMessages() OVERRIDE; | 55 virtual void RegisterMessages(); |
78 | 56 |
79 // Messages | 57 // SelectFileDialog::Listener implementation |
| 58 virtual void FileSelected(const FilePath& path, int index, void* params); |
| 59 virtual void FileSelectionCanceled(void* params); |
| 60 |
| 61 // TraceSubscriber implementation. |
| 62 virtual void OnEndTracingComplete(); |
| 63 virtual void OnTraceDataCollected(const std::string& json_events); |
| 64 virtual void OnTraceBufferPercentFullReply(float percent_full); |
| 65 |
| 66 // Messages. |
| 67 void OnBrowserBridgeInitialized(const ListValue* list); |
| 68 void OnCallAsync(const ListValue* list); |
80 void OnBeginTracing(const ListValue* list); | 69 void OnBeginTracing(const ListValue* list); |
81 void OnEndTracingAsync(const ListValue* list); | 70 void OnEndTracingAsync(const ListValue* list); |
82 void OnBrowserBridgeInitialized(const ListValue* list); | |
83 void OnCallAsync(const ListValue* list); | |
84 void OnBeginRequestBufferPercentFull(const ListValue* list); | 71 void OnBeginRequestBufferPercentFull(const ListValue* list); |
85 void OnLoadTraceFile(const ListValue* list); | 72 void OnLoadTraceFile(const ListValue* list); |
86 void OnSaveTraceFile(const ListValue* list); | 73 void OnSaveTraceFile(const ListValue* list); |
87 | 74 |
88 // Submessages dispatched from OnCallAsync | 75 // Submessages dispatched from OnCallAsync. |
89 Value* OnRequestClientInfo(const ListValue* list); | 76 Value* OnRequestClientInfo(const ListValue* list); |
90 Value* OnRequestLogMessages(const ListValue* list); | 77 Value* OnRequestLogMessages(const ListValue* list); |
91 | 78 |
92 // SelectFileDialog::Listener implementation | 79 // Callbacks. |
93 virtual void FileSelected(const FilePath& path, | 80 void OnGpuInfoUpdate(); |
94 int index, | |
95 void* params) OVERRIDE; | |
96 virtual void FileSelectionCanceled(void* params) OVERRIDE; | |
97 | 81 |
98 // Callbacks. | 82 // Callbacks. |
99 void OnGpuInfoUpdate(); | |
100 void LoadTraceFileComplete(std::string* file_contents); | 83 void LoadTraceFileComplete(std::string* file_contents); |
101 void SaveTraceFileComplete(); | 84 void SaveTraceFileComplete(); |
102 | 85 |
103 // TraceSubscriber implementation. | |
104 virtual void OnEndTracingComplete() OVERRIDE; | |
105 virtual void OnTraceDataCollected(const std::string& json_events) OVERRIDE; | |
106 virtual void OnTraceBufferPercentFullReply(float percent_full) OVERRIDE; | |
107 | |
108 // Executes the javascript function |function_name| in the renderer, passing | 86 // Executes the javascript function |function_name| in the renderer, passing |
109 // it the argument |value|. | 87 // it the argument |value|. |
110 void CallJavascriptFunction(const std::wstring& function_name, | 88 void CallJavascriptFunction(const std::wstring& function_name, |
111 const Value* value); | 89 const Value* value); |
112 | 90 |
113 private: | 91 private: |
114 DISALLOW_COPY_AND_ASSIGN(GpuMessageHandler); | 92 // The file dialog to select a file for loading or saving traces. |
| 93 scoped_refptr<SelectFileDialog> select_trace_file_dialog_; |
| 94 |
| 95 // The type of the file dialog as the same one is used for loading or saving |
| 96 // traces. |
| 97 SelectFileDialog::Type select_trace_file_dialog_type_; |
| 98 |
| 99 // The trace data that is to be written to the file on saving. |
| 100 scoped_ptr<std::string> trace_data_to_save_; |
| 101 |
| 102 // True while tracing is active. |
| 103 bool trace_enabled_; |
115 | 104 |
116 // Cache the Singleton for efficiency. | 105 // Cache the Singleton for efficiency. |
117 GpuDataManager* gpu_data_manager_; | 106 GpuDataManager* gpu_data_manager_; |
118 | 107 |
| 108 // Callback called when the GPU info is updated. |
119 Callback0::Type* gpu_info_update_callback_; | 109 Callback0::Type* gpu_info_update_callback_; |
120 | 110 |
121 scoped_refptr<SelectFileDialog> select_trace_file_dialog_; | 111 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
122 SelectFileDialog::Type select_trace_file_dialog_type_; | |
123 scoped_ptr<std::string> trace_data_to_save_; | |
124 | |
125 bool trace_enabled_; | |
126 }; | 112 }; |
127 | 113 |
| 114 // A proxy passed to the Read and Write tasks used when loading or saving trace |
| 115 // data. |
128 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { | 116 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
129 public: | 117 public: |
130 explicit TaskProxy(const base::WeakPtr<GpuMessageHandler>& handler) | 118 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) |
131 : handler_(handler) {} | 119 : handler_(handler) {} |
132 void LoadTraceFileCompleteProxy(std::string* file_contents) { | 120 void LoadTraceFileCompleteProxy(std::string* file_contents) { |
133 if (handler_) | 121 if (handler_) |
134 handler_->LoadTraceFileComplete(file_contents); | 122 handler_->LoadTraceFileComplete(file_contents); |
135 delete file_contents; | 123 delete file_contents; |
136 } | 124 } |
137 | 125 |
138 void SaveTraceFileCompleteProxy() { | 126 void SaveTraceFileCompleteProxy() { |
139 if (handler_) | 127 if (handler_) |
140 handler_->SaveTraceFileComplete(); | 128 handler_->SaveTraceFileComplete(); |
141 } | 129 } |
142 | 130 |
143 private: | 131 private: |
144 base::WeakPtr<GpuMessageHandler> handler_; | |
145 friend class base::RefCountedThreadSafe<TaskProxy>; | 132 friend class base::RefCountedThreadSafe<TaskProxy>; |
| 133 |
| 134 // The message handler to call callbacks on. |
| 135 base::WeakPtr<TracingMessageHandler> handler_; |
| 136 |
146 DISALLOW_COPY_AND_ASSIGN(TaskProxy); | 137 DISALLOW_COPY_AND_ASSIGN(TaskProxy); |
147 }; | 138 }; |
148 | 139 |
149 //////////////////////////////////////////////////////////////////////////////// | 140 //////////////////////////////////////////////////////////////////////////////// |
150 // | 141 // |
151 // GpuMessageHandler | 142 // TracingMessageHandler |
152 // | 143 // |
153 //////////////////////////////////////////////////////////////////////////////// | 144 //////////////////////////////////////////////////////////////////////////////// |
154 | 145 |
155 GpuMessageHandler::GpuMessageHandler() | 146 TracingMessageHandler::TracingMessageHandler() |
156 : gpu_info_update_callback_(NULL), | 147 : select_trace_file_dialog_type_(SelectFileDialog::SELECT_NONE), |
157 select_trace_file_dialog_type_(SelectFileDialog::SELECT_NONE), | 148 trace_enabled_(false), |
158 trace_enabled_(false) { | 149 gpu_info_update_callback_(NULL) { |
159 gpu_data_manager_ = GpuDataManager::GetInstance(); | 150 gpu_data_manager_ = GpuDataManager::GetInstance(); |
160 DCHECK(gpu_data_manager_); | 151 DCHECK(gpu_data_manager_); |
161 } | 152 } |
162 | 153 |
163 GpuMessageHandler::~GpuMessageHandler() { | 154 TracingMessageHandler::~TracingMessageHandler() { |
164 if (gpu_info_update_callback_) { | 155 if (gpu_info_update_callback_) { |
165 gpu_data_manager_->RemoveGpuInfoUpdateCallback(gpu_info_update_callback_); | 156 gpu_data_manager_->RemoveGpuInfoUpdateCallback(gpu_info_update_callback_); |
166 delete gpu_info_update_callback_; | 157 delete gpu_info_update_callback_; |
167 } | 158 } |
168 | 159 |
169 if (select_trace_file_dialog_) | 160 if (select_trace_file_dialog_) |
170 select_trace_file_dialog_->ListenerDestroyed(); | 161 select_trace_file_dialog_->ListenerDestroyed(); |
171 | 162 |
172 // If we are the current subscriber, this will result in ending tracing. | 163 // If we are the current subscriber, this will result in ending tracing. |
173 TraceController::GetInstance()->CancelSubscriber(this); | 164 TraceController::GetInstance()->CancelSubscriber(this); |
174 } | 165 } |
175 | 166 |
176 WebUIMessageHandler* GpuMessageHandler::Attach(WebUI* web_ui) { | 167 WebUIMessageHandler* TracingMessageHandler::Attach(WebUI* web_ui) { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | 169 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); |
179 return result; | 170 return result; |
180 } | 171 } |
181 | 172 |
182 /* BrowserBridge.callAsync prepends a requestID to these messages. */ | 173 void TracingMessageHandler::RegisterMessages() { |
183 void GpuMessageHandler::RegisterMessages() { | |
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
185 | 175 |
186 web_ui_->RegisterMessageCallback( | 176 web_ui_->RegisterMessageCallback( |
| 177 "browserBridgeInitialized", |
| 178 NewCallback(this, &TracingMessageHandler::OnBrowserBridgeInitialized)); |
| 179 web_ui_->RegisterMessageCallback( |
| 180 "callAsync", |
| 181 NewCallback(this, &TracingMessageHandler::OnCallAsync)); |
| 182 web_ui_->RegisterMessageCallback( |
187 "beginTracing", | 183 "beginTracing", |
188 NewCallback(this, &GpuMessageHandler::OnBeginTracing)); | 184 NewCallback(this, &TracingMessageHandler::OnBeginTracing)); |
189 web_ui_->RegisterMessageCallback( | 185 web_ui_->RegisterMessageCallback( |
190 "endTracingAsync", | 186 "endTracingAsync", |
191 NewCallback(this, &GpuMessageHandler::OnEndTracingAsync)); | 187 NewCallback(this, &TracingMessageHandler::OnEndTracingAsync)); |
192 web_ui_->RegisterMessageCallback( | |
193 "browserBridgeInitialized", | |
194 NewCallback(this, &GpuMessageHandler::OnBrowserBridgeInitialized)); | |
195 web_ui_->RegisterMessageCallback( | |
196 "callAsync", | |
197 NewCallback(this, &GpuMessageHandler::OnCallAsync)); | |
198 web_ui_->RegisterMessageCallback( | 188 web_ui_->RegisterMessageCallback( |
199 "beginRequestBufferPercentFull", | 189 "beginRequestBufferPercentFull", |
200 NewCallback(this, &GpuMessageHandler::OnBeginRequestBufferPercentFull)); | 190 NewCallback(this, |
| 191 &TracingMessageHandler::OnBeginRequestBufferPercentFull)); |
201 web_ui_->RegisterMessageCallback( | 192 web_ui_->RegisterMessageCallback( |
202 "loadTraceFile", | 193 "loadTraceFile", |
203 NewCallback(this, &GpuMessageHandler::OnLoadTraceFile)); | 194 NewCallback(this, &TracingMessageHandler::OnLoadTraceFile)); |
204 web_ui_->RegisterMessageCallback( | 195 web_ui_->RegisterMessageCallback( |
205 "saveTraceFile", | 196 "saveTraceFile", |
206 NewCallback(this, &GpuMessageHandler::OnSaveTraceFile)); | 197 NewCallback(this, &TracingMessageHandler::OnSaveTraceFile)); |
207 } | 198 } |
208 | 199 |
209 void GpuMessageHandler::OnCallAsync(const ListValue* args) { | 200 void TracingMessageHandler::OnCallAsync(const ListValue* args) { |
210 DCHECK_GE(args->GetSize(), static_cast<size_t>(2)); | 201 DCHECK_GE(args->GetSize(), static_cast<size_t>(2)); |
211 // unpack args into requestId, submessage and submessageArgs | 202 // unpack args into requestId, submessage and submessageArgs |
212 bool ok; | 203 bool ok; |
213 Value* requestId; | 204 Value* requestId; |
214 ok = args->Get(0, &requestId); | 205 ok = args->Get(0, &requestId); |
215 DCHECK(ok); | 206 DCHECK(ok); |
216 | 207 |
217 std::string submessage; | 208 std::string submessage; |
218 ok = args->GetString(1, &submessage); | 209 ok = args->GetString(1, &submessage); |
219 DCHECK(ok); | 210 DCHECK(ok); |
(...skipping 26 matching lines...) Expand all Loading... |
246 web_ui_->CallJavascriptFunction("browserBridge.onCallAsyncReply", | 237 web_ui_->CallJavascriptFunction("browserBridge.onCallAsyncReply", |
247 *requestId, | 238 *requestId, |
248 *ret); | 239 *ret); |
249 delete ret; | 240 delete ret; |
250 } else { | 241 } else { |
251 web_ui_->CallJavascriptFunction("browserBridge.onCallAsyncReply", | 242 web_ui_->CallJavascriptFunction("browserBridge.onCallAsyncReply", |
252 *requestId); | 243 *requestId); |
253 } | 244 } |
254 } | 245 } |
255 | 246 |
256 void GpuMessageHandler::OnBeginRequestBufferPercentFull(const ListValue* list) { | 247 void TracingMessageHandler::OnBrowserBridgeInitialized(const ListValue* args) { |
| 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 249 |
| 250 DCHECK(!gpu_info_update_callback_); |
| 251 |
| 252 // Watch for changes in GPUInfo |
| 253 gpu_info_update_callback_ = |
| 254 NewCallback(this, &TracingMessageHandler::OnGpuInfoUpdate); |
| 255 gpu_data_manager_->AddGpuInfoUpdateCallback(gpu_info_update_callback_); |
| 256 |
| 257 // Tell GpuDataManager it should have full GpuInfo. If the |
| 258 // Gpu process has not run yet, this will trigger its launch. |
| 259 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded(); |
| 260 |
| 261 // Run callback immediately in case the info is ready and no update in the |
| 262 // future. |
| 263 OnGpuInfoUpdate(); |
| 264 } |
| 265 |
| 266 Value* TracingMessageHandler::OnRequestClientInfo(const ListValue* list) { |
| 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 268 |
| 269 DictionaryValue* dict = new DictionaryValue(); |
| 270 |
| 271 chrome::VersionInfo version_info; |
| 272 |
| 273 if (!version_info.is_valid()) { |
| 274 DLOG(ERROR) << "Unable to create chrome::VersionInfo"; |
| 275 } else { |
| 276 // We have everything we need to send the right values. |
| 277 dict->SetString("version", version_info.Version()); |
| 278 dict->SetString("cl", version_info.LastChange()); |
| 279 dict->SetString("version_mod", |
| 280 chrome::VersionInfo::GetVersionStringModifier()); |
| 281 dict->SetString("official", |
| 282 l10n_util::GetStringUTF16( |
| 283 version_info.IsOfficialBuild() ? |
| 284 IDS_ABOUT_VERSION_OFFICIAL : |
| 285 IDS_ABOUT_VERSION_UNOFFICIAL)); |
| 286 |
| 287 dict->SetString("command_line", |
| 288 CommandLine::ForCurrentProcess()->GetCommandLineString()); |
| 289 } |
| 290 |
| 291 dict->SetString("blacklist_version", |
| 292 GpuDataManager::GetInstance()->GetBlacklistVersion()); |
| 293 |
| 294 return dict; |
| 295 } |
| 296 |
| 297 Value* TracingMessageHandler::OnRequestLogMessages(const ListValue*) { |
| 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 299 |
| 300 return gpu_data_manager_->log_messages().DeepCopy(); |
| 301 } |
| 302 |
| 303 void TracingMessageHandler::OnBeginRequestBufferPercentFull( |
| 304 const ListValue* list) { |
257 TraceController::GetInstance()->GetTraceBufferPercentFullAsync(this); | 305 TraceController::GetInstance()->GetTraceBufferPercentFullAsync(this); |
258 } | 306 } |
259 | 307 |
| 308 void TracingMessageHandler::OnGpuInfoUpdate() { |
| 309 // Get GPU Info. |
| 310 scoped_ptr<base::DictionaryValue> gpu_info_val( |
| 311 gpu_data_manager_->GpuInfoAsDictionaryValue()); |
| 312 |
| 313 // Add in blacklisting features |
| 314 Value* feature_status = gpu_data_manager_->GetFeatureStatus(); |
| 315 if (feature_status) |
| 316 gpu_info_val->Set("featureStatus", feature_status); |
| 317 |
| 318 // Send GPU Info to javascript. |
| 319 web_ui_->CallJavascriptFunction("browserBridge.onGpuInfoUpdate", |
| 320 *(gpu_info_val.get())); |
| 321 } |
| 322 |
| 323 // A task used for asynchronously reading a file to a string. Calls the |
| 324 // TaskProxy callback when reading is complete. |
260 class ReadTraceFileTask : public Task { | 325 class ReadTraceFileTask : public Task { |
261 public: | 326 public: |
262 ReadTraceFileTask(TaskProxy* proxy, const FilePath& path) | 327 ReadTraceFileTask(TaskProxy* proxy, const FilePath& path) |
263 : proxy_(proxy) | 328 : proxy_(proxy), |
264 , path_(path) {} | 329 path_(path) {} |
265 | 330 |
266 virtual void Run() { | 331 virtual void Run() { |
267 std::string* file_contents = new std::string(); | 332 std::string* file_contents = new std::string(); |
268 if (!file_util::ReadFileToString(path_, file_contents)) { | 333 if (!file_util::ReadFileToString(path_, file_contents)) { |
269 delete file_contents; | 334 delete file_contents; |
270 return; | 335 return; |
271 } | 336 } |
272 BrowserThread::PostTask( | 337 BrowserThread::PostTask( |
273 BrowserThread::UI, FROM_HERE, | 338 BrowserThread::UI, FROM_HERE, |
274 NewRunnableMethod(proxy_.get(), | 339 NewRunnableMethod(proxy_.get(), |
275 &TaskProxy::LoadTraceFileCompleteProxy, | 340 &TaskProxy::LoadTraceFileCompleteProxy, |
276 file_contents)); | 341 file_contents)); |
277 } | 342 } |
278 | 343 |
279 private: | 344 private: |
| 345 // The proxy that defines the completion callback. |
280 scoped_refptr<TaskProxy> proxy_; | 346 scoped_refptr<TaskProxy> proxy_; |
281 | 347 |
282 // Path of the file to open. | 348 // Path of the file to open. |
283 const FilePath path_; | 349 const FilePath path_; |
284 }; | 350 }; |
285 | 351 |
| 352 // A task used for asynchronously writing a file from a string. Calls the |
| 353 // TaskProxy callback when writing is complete. |
286 class WriteTraceFileTask : public Task { | 354 class WriteTraceFileTask : public Task { |
287 public: | 355 public: |
288 WriteTraceFileTask(TaskProxy* proxy, | 356 WriteTraceFileTask(TaskProxy* proxy, |
289 const FilePath& path, | 357 const FilePath& path, |
290 std::string* contents) | 358 std::string* contents) |
291 : proxy_(proxy) | 359 : proxy_(proxy) |
292 , path_(path) | 360 , path_(path) |
293 , contents_(contents) {} | 361 , contents_(contents) {} |
294 | 362 |
295 virtual void Run() { | 363 virtual void Run() { |
296 if (!file_util::WriteFile(path_, contents_->c_str(), contents_->size())) | 364 if (!file_util::WriteFile(path_, contents_->c_str(), contents_->size())) |
297 return; | 365 return; |
298 BrowserThread::PostTask( | 366 BrowserThread::PostTask( |
299 BrowserThread::UI, FROM_HERE, | 367 BrowserThread::UI, FROM_HERE, |
300 NewRunnableMethod(proxy_.get(), | 368 NewRunnableMethod(proxy_.get(), |
301 &TaskProxy::SaveTraceFileCompleteProxy)); | 369 &TaskProxy::SaveTraceFileCompleteProxy)); |
302 } | 370 } |
303 | 371 |
304 private: | 372 private: |
| 373 // The proxy that defines the completion callback. |
305 scoped_refptr<TaskProxy> proxy_; | 374 scoped_refptr<TaskProxy> proxy_; |
306 | 375 |
307 // Path of the file to save. | 376 // Path of the file to save. |
308 const FilePath path_; | 377 const FilePath path_; |
309 | 378 |
310 // What to save | 379 // What to save |
311 scoped_ptr<std::string> contents_; | 380 scoped_ptr<std::string> contents_; |
312 }; | 381 }; |
313 | 382 |
314 void GpuMessageHandler::FileSelected( | 383 void TracingMessageHandler::FileSelected( |
315 const FilePath& path, int index, void* params) { | 384 const FilePath& path, int index, void* params) { |
316 if (select_trace_file_dialog_type_ == SelectFileDialog::SELECT_OPEN_FILE) | 385 if (select_trace_file_dialog_type_ == SelectFileDialog::SELECT_OPEN_FILE) |
317 BrowserThread::PostTask( | 386 BrowserThread::PostTask( |
318 BrowserThread::FILE, FROM_HERE, | 387 BrowserThread::FILE, FROM_HERE, |
319 new ReadTraceFileTask(new TaskProxy(AsWeakPtr()), path)); | 388 new ReadTraceFileTask(new TaskProxy(AsWeakPtr()), path)); |
320 else | 389 else |
321 BrowserThread::PostTask( | 390 BrowserThread::PostTask( |
322 BrowserThread::FILE, FROM_HERE, | 391 BrowserThread::FILE, FROM_HERE, |
323 new WriteTraceFileTask(new TaskProxy(AsWeakPtr()), path, | 392 new WriteTraceFileTask(new TaskProxy(AsWeakPtr()), path, |
324 trace_data_to_save_.release())); | 393 trace_data_to_save_.release())); |
325 select_trace_file_dialog_.release(); | 394 select_trace_file_dialog_.release(); |
326 } | 395 } |
327 | 396 |
328 void GpuMessageHandler::FileSelectionCanceled(void* params) { | 397 void TracingMessageHandler::FileSelectionCanceled(void* params) { |
329 select_trace_file_dialog_.release(); | 398 select_trace_file_dialog_.release(); |
330 if (select_trace_file_dialog_type_ == SelectFileDialog::SELECT_OPEN_FILE) | 399 if (select_trace_file_dialog_type_ == SelectFileDialog::SELECT_OPEN_FILE) { |
331 web_ui_->CallJavascriptFunction("tracingController.onLoadTraceFileCanceled")
; | 400 web_ui_->CallJavascriptFunction( |
332 else | 401 "tracingController.onLoadTraceFileCanceled"); |
333 web_ui_->CallJavascriptFunction("tracingController.onSaveTraceFileCanceled")
; | 402 } else { |
| 403 web_ui_->CallJavascriptFunction( |
| 404 "tracingController.onSaveTraceFileCanceled"); |
| 405 } |
334 } | 406 } |
335 | 407 |
336 void GpuMessageHandler::OnLoadTraceFile(const ListValue* list) { | 408 void TracingMessageHandler::OnLoadTraceFile(const ListValue* list) { |
337 // Only allow a single dialog at a time. | 409 // Only allow a single dialog at a time. |
338 if (select_trace_file_dialog_.get()) | 410 if (select_trace_file_dialog_.get()) |
339 return; | 411 return; |
340 select_trace_file_dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; | 412 select_trace_file_dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; |
341 select_trace_file_dialog_ = SelectFileDialog::Create(this); | 413 select_trace_file_dialog_ = SelectFileDialog::Create(this); |
342 select_trace_file_dialog_->SelectFile( | 414 select_trace_file_dialog_->SelectFile( |
343 SelectFileDialog::SELECT_OPEN_FILE, | 415 SelectFileDialog::SELECT_OPEN_FILE, |
344 string16(), | 416 string16(), |
345 FilePath(), | 417 FilePath(), |
346 NULL, 0, FILE_PATH_LITERAL(""), web_ui_->tab_contents(), | 418 NULL, 0, FILE_PATH_LITERAL(""), web_ui_->tab_contents(), |
347 web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); | 419 web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); |
348 } | 420 } |
349 | 421 |
350 void GpuMessageHandler::LoadTraceFileComplete(std::string* file_contents) { | 422 void TracingMessageHandler::LoadTraceFileComplete(std::string* file_contents) { |
351 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
352 std::wstring javascript; | 424 std::wstring javascript; |
353 javascript += L"tracingController.onLoadTraceFileComplete("; | 425 javascript += L"tracingController.onLoadTraceFileComplete("; |
354 javascript += UTF8ToWide(*file_contents); | 426 javascript += UTF8ToWide(*file_contents); |
355 javascript += L");"; | 427 javascript += L");"; |
356 | 428 |
357 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), | 429 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), |
358 WideToUTF16Hack(javascript)); | 430 WideToUTF16Hack(javascript)); |
359 } | 431 } |
360 | 432 |
361 void GpuMessageHandler::OnSaveTraceFile(const ListValue* list) { | 433 void TracingMessageHandler::OnSaveTraceFile(const ListValue* list) { |
362 // Only allow a single dialog at a time. | 434 // Only allow a single dialog at a time. |
363 if (select_trace_file_dialog_.get()) | 435 if (select_trace_file_dialog_.get()) |
364 return; | 436 return; |
365 | 437 |
366 DCHECK(list->GetSize() == 1); | 438 DCHECK(list->GetSize() == 1); |
367 | 439 |
368 std::string* trace_data = new std::string(); | 440 std::string* trace_data = new std::string(); |
369 bool ok = list->GetString(0, trace_data); | 441 bool ok = list->GetString(0, trace_data); |
370 DCHECK(ok); | 442 DCHECK(ok); |
371 trace_data_to_save_.reset(trace_data); | 443 trace_data_to_save_.reset(trace_data); |
372 | 444 |
373 select_trace_file_dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; | 445 select_trace_file_dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; |
374 select_trace_file_dialog_ = SelectFileDialog::Create(this); | 446 select_trace_file_dialog_ = SelectFileDialog::Create(this); |
375 select_trace_file_dialog_->SelectFile( | 447 select_trace_file_dialog_->SelectFile( |
376 SelectFileDialog::SELECT_SAVEAS_FILE, | 448 SelectFileDialog::SELECT_SAVEAS_FILE, |
377 string16(), | 449 string16(), |
378 FilePath(), | 450 FilePath(), |
379 NULL, 0, FILE_PATH_LITERAL(""), web_ui_->tab_contents(), | 451 NULL, 0, FILE_PATH_LITERAL(""), web_ui_->tab_contents(), |
380 web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); | 452 web_ui_->tab_contents()->view()->GetTopLevelNativeWindow(), NULL); |
381 } | 453 } |
382 | 454 |
383 void GpuMessageHandler::SaveTraceFileComplete() { | 455 void TracingMessageHandler::SaveTraceFileComplete() { |
384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
385 std::wstring javascript; | 457 std::wstring javascript; |
386 web_ui_->CallJavascriptFunction("tracingController.onSaveTraceFileComplete"); | 458 web_ui_->CallJavascriptFunction("tracingController.onSaveTraceFileComplete"); |
387 } | 459 } |
388 | 460 |
389 void GpuMessageHandler::OnBrowserBridgeInitialized(const ListValue* args) { | 461 void TracingMessageHandler::OnBeginTracing(const ListValue* args) { |
390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
391 | |
392 DCHECK(!gpu_info_update_callback_); | |
393 | |
394 // Watch for changes in GPUInfo | |
395 gpu_info_update_callback_ = | |
396 NewCallback(this, &GpuMessageHandler::OnGpuInfoUpdate); | |
397 gpu_data_manager_->AddGpuInfoUpdateCallback(gpu_info_update_callback_); | |
398 | |
399 // Tell GpuDataManager it should have full GpuInfo. If the | |
400 // Gpu process has not run yet, this will trigger its launch. | |
401 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded(); | |
402 | |
403 // Run callback immediately in case the info is ready and no update in the | |
404 // future. | |
405 OnGpuInfoUpdate(); | |
406 } | |
407 | |
408 Value* GpuMessageHandler::OnRequestClientInfo(const ListValue* list) { | |
409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
410 | |
411 DictionaryValue* dict = new DictionaryValue(); | |
412 | |
413 chrome::VersionInfo version_info; | |
414 | |
415 if (!version_info.is_valid()) { | |
416 DLOG(ERROR) << "Unable to create chrome::VersionInfo"; | |
417 } else { | |
418 // We have everything we need to send the right values. | |
419 dict->SetString("version", version_info.Version()); | |
420 dict->SetString("cl", version_info.LastChange()); | |
421 dict->SetString("version_mod", | |
422 chrome::VersionInfo::GetVersionStringModifier()); | |
423 dict->SetString("official", | |
424 l10n_util::GetStringUTF16( | |
425 version_info.IsOfficialBuild() ? | |
426 IDS_ABOUT_VERSION_OFFICIAL : | |
427 IDS_ABOUT_VERSION_UNOFFICIAL)); | |
428 | |
429 dict->SetString("command_line", | |
430 CommandLine::ForCurrentProcess()->GetCommandLineString()); | |
431 } | |
432 | |
433 dict->SetString("blacklist_version", | |
434 GpuDataManager::GetInstance()->GetBlacklistVersion()); | |
435 | |
436 return dict; | |
437 } | |
438 | |
439 DictionaryValue* NewDescriptionValuePair(const std::string& desc, | |
440 const std::string& value) { | |
441 DictionaryValue* dict = new DictionaryValue(); | |
442 dict->SetString("description", desc); | |
443 dict->SetString("value", value); | |
444 return dict; | |
445 } | |
446 | |
447 DictionaryValue* NewDescriptionValuePair(const std::string& desc, | |
448 Value* value) { | |
449 DictionaryValue* dict = new DictionaryValue(); | |
450 dict->SetString("description", desc); | |
451 dict->Set("value", value); | |
452 return dict; | |
453 } | |
454 | |
455 #if defined(OS_WIN) | |
456 // Output DxDiagNode tree as nested array of {description,value} pairs | |
457 ListValue* DxDiagNodeToList(const DxDiagNode& node) { | |
458 ListValue* list = new ListValue(); | |
459 for (std::map<std::string, std::string>::const_iterator it = | |
460 node.values.begin(); | |
461 it != node.values.end(); | |
462 ++it) { | |
463 list->Append(NewDescriptionValuePair(it->first, it->second)); | |
464 } | |
465 | |
466 for (std::map<std::string, DxDiagNode>::const_iterator it = | |
467 node.children.begin(); | |
468 it != node.children.end(); | |
469 ++it) { | |
470 ListValue* sublist = DxDiagNodeToList(it->second); | |
471 list->Append(NewDescriptionValuePair(it->first, sublist)); | |
472 } | |
473 return list; | |
474 } | |
475 | |
476 #endif // OS_WIN | |
477 | |
478 DictionaryValue* GpuInfoToDict(const GPUInfo& gpu_info) { | |
479 ListValue* basic_info = new ListValue(); | |
480 basic_info->Append(NewDescriptionValuePair("Initialization time", | |
481 base::Int64ToString(gpu_info.initialization_time.InMilliseconds()))); | |
482 basic_info->Append(NewDescriptionValuePair("Vendor Id", | |
483 base::StringPrintf("0x%04x", gpu_info.vendor_id))); | |
484 basic_info->Append(NewDescriptionValuePair("Device Id", | |
485 base::StringPrintf("0x%04x", gpu_info.device_id))); | |
486 basic_info->Append(NewDescriptionValuePair("Driver vendor", | |
487 gpu_info.driver_vendor)); | |
488 basic_info->Append(NewDescriptionValuePair("Driver version", | |
489 gpu_info.driver_version)); | |
490 basic_info->Append(NewDescriptionValuePair("Driver date", | |
491 gpu_info.driver_date)); | |
492 basic_info->Append(NewDescriptionValuePair("Pixel shader version", | |
493 gpu_info.pixel_shader_version)); | |
494 basic_info->Append(NewDescriptionValuePair("Vertex shader version", | |
495 gpu_info.vertex_shader_version)); | |
496 basic_info->Append(NewDescriptionValuePair("GL version", | |
497 gpu_info.gl_version)); | |
498 basic_info->Append(NewDescriptionValuePair("GL_VENDOR", | |
499 gpu_info.gl_vendor)); | |
500 basic_info->Append(NewDescriptionValuePair("GL_RENDERER", | |
501 gpu_info.gl_renderer)); | |
502 basic_info->Append(NewDescriptionValuePair("GL_VERSION", | |
503 gpu_info.gl_version_string)); | |
504 basic_info->Append(NewDescriptionValuePair("GL_EXTENSIONS", | |
505 gpu_info.gl_extensions)); | |
506 | |
507 DictionaryValue* info = new DictionaryValue(); | |
508 info->Set("basic_info", basic_info); | |
509 | |
510 #if defined(OS_WIN) | |
511 Value* dx_info; | |
512 if (gpu_info.dx_diagnostics.children.size()) | |
513 dx_info = DxDiagNodeToList(gpu_info.dx_diagnostics); | |
514 else | |
515 dx_info = Value::CreateNullValue(); | |
516 info->Set("diagnostics", dx_info); | |
517 #endif | |
518 | |
519 return info; | |
520 } | |
521 | |
522 Value* GpuMessageHandler::OnRequestLogMessages(const ListValue*) { | |
523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
524 | |
525 return gpu_data_manager_->log_messages().DeepCopy(); | |
526 } | |
527 | |
528 void GpuMessageHandler::OnGpuInfoUpdate() { | |
529 const GPUInfo& gpu_info = gpu_data_manager_->gpu_info(); | |
530 | |
531 // Get GPU Info. | |
532 DictionaryValue* gpu_info_val = GpuInfoToDict(gpu_info); | |
533 | |
534 // Add in blacklisting features | |
535 Value* feature_status = gpu_data_manager_->GetFeatureStatus(); | |
536 if (feature_status) | |
537 gpu_info_val->Set("featureStatus", feature_status); | |
538 | |
539 // Send GPU Info to javascript. | |
540 web_ui_->CallJavascriptFunction("browserBridge.onGpuInfoUpdate", | |
541 *gpu_info_val); | |
542 | |
543 delete gpu_info_val; | |
544 } | |
545 | |
546 void GpuMessageHandler::OnBeginTracing(const ListValue* args) { | |
547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
548 trace_enabled_ = true; | 463 trace_enabled_ = true; |
549 // TODO(jbates) This may fail, but that's OK for current use cases. | 464 // TODO(jbates) This may fail, but that's OK for current use cases. |
550 // Ex: Multiple about:gpu traces can not trace simultaneously. | 465 // Ex: Multiple about:gpu traces can not trace simultaneously. |
551 // TODO(nduca) send feedback to javascript about whether or not BeginTracing | 466 // TODO(nduca) send feedback to javascript about whether or not BeginTracing |
552 // was successful. | 467 // was successful. |
553 TraceController::GetInstance()->BeginTracing(this); | 468 TraceController::GetInstance()->BeginTracing(this); |
554 } | 469 } |
555 | 470 |
556 void GpuMessageHandler::OnEndTracingAsync(const ListValue* list) { | 471 void TracingMessageHandler::OnEndTracingAsync(const ListValue* list) { |
557 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
558 | 473 |
559 // TODO(nduca): fix javascript code to make sure trace_enabled_ is always true | 474 // TODO(nduca): fix javascript code to make sure trace_enabled_ is always true |
560 // here. triggered a false condition by just clicking stop | 475 // here. triggered a false condition by just clicking stop |
561 // trace a few times when it was going slow, and maybe switching | 476 // trace a few times when it was going slow, and maybe switching |
562 // between tabs. | 477 // between tabs. |
563 if (trace_enabled_ && | 478 if (trace_enabled_ && |
564 !TraceController::GetInstance()->EndTracingAsync(this)) { | 479 !TraceController::GetInstance()->EndTracingAsync(this)) { |
565 // Set to false now, since it turns out we never were the trace subscriber. | 480 // Set to false now, since it turns out we never were the trace subscriber. |
566 OnEndTracingComplete(); | 481 OnEndTracingComplete(); |
567 } | 482 } |
568 } | 483 } |
569 | 484 |
570 void GpuMessageHandler::OnEndTracingComplete() { | 485 void TracingMessageHandler::OnEndTracingComplete() { |
571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
572 trace_enabled_ = false; | 487 trace_enabled_ = false; |
573 web_ui_->CallJavascriptFunction("tracingController.onEndTracingComplete"); | 488 web_ui_->CallJavascriptFunction("tracingController.onEndTracingComplete"); |
574 } | 489 } |
575 | 490 |
576 void GpuMessageHandler::OnTraceDataCollected(const std::string& json_events) { | 491 void TracingMessageHandler::OnTraceDataCollected( |
| 492 const std::string& json_events) { |
577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 493 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
578 std::wstring javascript; | 494 std::wstring javascript; |
579 javascript += L"tracingController.onTraceDataCollected("; | 495 javascript += L"tracingController.onTraceDataCollected("; |
580 javascript += UTF8ToWide(json_events); | 496 javascript += UTF8ToWide(json_events); |
581 javascript += L");"; | 497 javascript += L");"; |
582 | 498 |
583 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), | 499 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), |
584 WideToUTF16Hack(javascript)); | 500 WideToUTF16Hack(javascript)); |
585 } | 501 } |
586 | 502 |
587 void GpuMessageHandler::OnTraceBufferPercentFullReply(float percent_full) { | 503 void TracingMessageHandler::OnTraceBufferPercentFullReply(float percent_full) { |
588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 504 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
589 web_ui_->CallJavascriptFunction( | 505 web_ui_->CallJavascriptFunction( |
590 "tracingController.onRequestBufferPercentFullComplete", | 506 "tracingController.onRequestBufferPercentFullComplete", |
591 *scoped_ptr<Value>(Value::CreateDoubleValue(percent_full))); | 507 *scoped_ptr<Value>(Value::CreateDoubleValue(percent_full))); |
592 } | 508 } |
593 | 509 |
594 } // namespace | 510 } // namespace |
595 | 511 |
596 | 512 |
597 //////////////////////////////////////////////////////////////////////////////// | 513 //////////////////////////////////////////////////////////////////////////////// |
598 // | 514 // |
599 // GpuInternalsUI | 515 // TracingUI |
600 // | 516 // |
601 //////////////////////////////////////////////////////////////////////////////// | 517 //////////////////////////////////////////////////////////////////////////////// |
602 | 518 |
603 GpuInternalsUI::GpuInternalsUI(TabContents* contents) : ChromeWebUI(contents) { | 519 TracingUI::TracingUI(TabContents* contents) : ChromeWebUI(contents) { |
604 AddMessageHandler((new GpuMessageHandler())->Attach(this)); | 520 AddMessageHandler((new TracingMessageHandler())->Attach(this)); |
605 | 521 |
606 // Set up the chrome://gpu-internals/ source. | 522 // Set up the chrome://tracing/ source. |
607 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 523 Profile::FromBrowserContext(contents->browser_context())-> |
608 profile->GetChromeURLDataManager()->AddDataSource(CreateGpuHTMLSource()); | 524 GetChromeURLDataManager()->AddDataSource(CreateTracingHTMLSource()); |
609 } | 525 } |
OLD | NEW |