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 "chrome/browser/ui/webui/tracing_ui.h" | 5 #include "chrome/browser/ui/webui/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/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "chrome/browser/gpu_blacklist.h" | |
19 #include "chrome/browser/gpu_util.h" | |
20 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/chrome_select_file_policy.h" | 19 #include "chrome/browser/ui/chrome_select_file_policy.h" |
22 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 20 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
23 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 21 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
24 #include "chrome/common/chrome_version_info.h" | 22 #include "chrome/common/chrome_version_info.h" |
25 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
26 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/gpu_data_manager.h" | 25 #include "content/public/browser/gpu_data_manager.h" |
28 #include "content/public/browser/gpu_data_manager_observer.h" | 26 #include "content/public/browser/gpu_data_manager_observer.h" |
29 #include "content/public/browser/render_view_host.h" | 27 #include "content/public/browser/render_view_host.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return source; | 61 return source; |
64 } | 62 } |
65 | 63 |
66 // This class receives javascript messages from the renderer. | 64 // This class receives javascript messages from the renderer. |
67 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 65 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
68 // this class's methods are expected to run on the UI thread. | 66 // this class's methods are expected to run on the UI thread. |
69 class TracingMessageHandler | 67 class TracingMessageHandler |
70 : public WebUIMessageHandler, | 68 : public WebUIMessageHandler, |
71 public ui::SelectFileDialog::Listener, | 69 public ui::SelectFileDialog::Listener, |
72 public base::SupportsWeakPtr<TracingMessageHandler>, | 70 public base::SupportsWeakPtr<TracingMessageHandler>, |
73 public content::TraceSubscriber, | 71 public content::TraceSubscriber { |
74 public content::GpuDataManagerObserver { | |
75 public: | 72 public: |
76 TracingMessageHandler(); | 73 TracingMessageHandler(); |
77 virtual ~TracingMessageHandler(); | 74 virtual ~TracingMessageHandler(); |
78 | 75 |
79 // WebUIMessageHandler implementation. | 76 // WebUIMessageHandler implementation. |
80 virtual void RegisterMessages(); | 77 virtual void RegisterMessages(); |
81 | 78 |
82 // SelectFileDialog::Listener implementation | 79 // SelectFileDialog::Listener implementation |
83 virtual void FileSelected(const FilePath& path, int index, void* params); | 80 virtual void FileSelected(const FilePath& path, int index, void* params); |
84 virtual void FileSelectionCanceled(void* params); | 81 virtual void FileSelectionCanceled(void* params); |
85 | 82 |
86 // TraceSubscriber implementation. | 83 // TraceSubscriber implementation. |
87 virtual void OnEndTracingComplete(); | 84 virtual void OnEndTracingComplete(); |
88 virtual void OnTraceDataCollected( | 85 virtual void OnTraceDataCollected( |
89 const scoped_refptr<base::RefCountedString>& trace_fragment); | 86 const scoped_refptr<base::RefCountedString>& trace_fragment); |
90 virtual void OnTraceBufferPercentFullReply(float percent_full); | 87 virtual void OnTraceBufferPercentFullReply(float percent_full); |
91 | 88 |
92 // GpuDataManagerObserver implementation. | |
93 virtual void OnGpuInfoUpdate() OVERRIDE; | |
94 virtual void OnVideoMemoryUsageStatsUpdate( | |
95 const content::GPUVideoMemoryUsageStats& video_memory) OVERRIDE {} | |
96 | |
97 // Messages. | 89 // Messages. |
98 void OnTracingControllerInitialized(const ListValue* list); | 90 void OnTracingControllerInitialized(const ListValue* list); |
99 void OnBeginTracing(const ListValue* list); | 91 void OnBeginTracing(const ListValue* list); |
100 void OnEndTracingAsync(const ListValue* list); | 92 void OnEndTracingAsync(const ListValue* list); |
101 void OnBeginRequestBufferPercentFull(const ListValue* list); | 93 void OnBeginRequestBufferPercentFull(const ListValue* list); |
102 void OnLoadTraceFile(const ListValue* list); | 94 void OnLoadTraceFile(const ListValue* list); |
103 void OnSaveTraceFile(const ListValue* list); | 95 void OnSaveTraceFile(const ListValue* list); |
104 | 96 |
105 // Callbacks. | 97 // Callbacks. |
106 void LoadTraceFileComplete(string16* file_contents); | 98 void LoadTraceFileComplete(string16* file_contents); |
107 void SaveTraceFileComplete(); | 99 void SaveTraceFileComplete(); |
108 | 100 |
109 private: | 101 private: |
110 // The file dialog to select a file for loading or saving traces. | 102 // The file dialog to select a file for loading or saving traces. |
111 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; | 103 scoped_refptr<ui::SelectFileDialog> select_trace_file_dialog_; |
112 | 104 |
113 // The type of the file dialog as the same one is used for loading or saving | 105 // The type of the file dialog as the same one is used for loading or saving |
114 // traces. | 106 // traces. |
115 ui::SelectFileDialog::Type select_trace_file_dialog_type_; | 107 ui::SelectFileDialog::Type select_trace_file_dialog_type_; |
116 | 108 |
117 // The trace data that is to be written to the file on saving. | 109 // The trace data that is to be written to the file on saving. |
118 scoped_ptr<std::string> trace_data_to_save_; | 110 scoped_ptr<std::string> trace_data_to_save_; |
119 | 111 |
120 // True while tracing is active. | 112 // True while tracing is active. |
121 bool trace_enabled_; | 113 bool trace_enabled_; |
122 | 114 |
123 // True while system tracing is active. | 115 // True while system tracing is active. |
124 bool system_trace_in_progress_; | 116 bool system_trace_in_progress_; |
125 | 117 |
126 // True if observing the GpuDataManager (re-attaching as observer would | |
127 // DCHECK). | |
128 bool observing_; | |
129 | |
130 void OnEndSystemTracingAck( | 118 void OnEndSystemTracingAck( |
131 const scoped_refptr<base::RefCountedString>& events_str_ptr); | 119 const scoped_refptr<base::RefCountedString>& events_str_ptr); |
132 | 120 |
133 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); | 121 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
134 }; | 122 }; |
135 | 123 |
136 // A proxy passed to the Read and Write tasks used when loading or saving trace | 124 // A proxy passed to the Read and Write tasks used when loading or saving trace |
137 // data. | 125 // data. |
138 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { | 126 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
139 public: | 127 public: |
(...skipping 22 matching lines...) Expand all Loading... |
162 | 150 |
163 //////////////////////////////////////////////////////////////////////////////// | 151 //////////////////////////////////////////////////////////////////////////////// |
164 // | 152 // |
165 // TracingMessageHandler | 153 // TracingMessageHandler |
166 // | 154 // |
167 //////////////////////////////////////////////////////////////////////////////// | 155 //////////////////////////////////////////////////////////////////////////////// |
168 | 156 |
169 TracingMessageHandler::TracingMessageHandler() | 157 TracingMessageHandler::TracingMessageHandler() |
170 : select_trace_file_dialog_type_(ui::SelectFileDialog::SELECT_NONE), | 158 : select_trace_file_dialog_type_(ui::SelectFileDialog::SELECT_NONE), |
171 trace_enabled_(false), | 159 trace_enabled_(false), |
172 system_trace_in_progress_(false), | 160 system_trace_in_progress_(false) { |
173 observing_(false) { | |
174 } | 161 } |
175 | 162 |
176 TracingMessageHandler::~TracingMessageHandler() { | 163 TracingMessageHandler::~TracingMessageHandler() { |
177 GpuDataManager::GetInstance()->RemoveObserver(this); | |
178 | |
179 if (select_trace_file_dialog_) | 164 if (select_trace_file_dialog_) |
180 select_trace_file_dialog_->ListenerDestroyed(); | 165 select_trace_file_dialog_->ListenerDestroyed(); |
181 | 166 |
182 // If we are the current subscriber, this will result in ending tracing. | 167 // If we are the current subscriber, this will result in ending tracing. |
183 TraceController::GetInstance()->CancelSubscriber(this); | 168 TraceController::GetInstance()->CancelSubscriber(this); |
184 | 169 |
185 // Shutdown any system tracing too. | 170 // Shutdown any system tracing too. |
186 if (system_trace_in_progress_) { | 171 if (system_trace_in_progress_) { |
187 #if defined(OS_CHROMEOS) | 172 #if defined(OS_CHROMEOS) |
188 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | 173 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> |
(...skipping 23 matching lines...) Expand all Loading... |
212 base::Unretained(this))); | 197 base::Unretained(this))); |
213 web_ui()->RegisterMessageCallback("saveTraceFile", | 198 web_ui()->RegisterMessageCallback("saveTraceFile", |
214 base::Bind(&TracingMessageHandler::OnSaveTraceFile, | 199 base::Bind(&TracingMessageHandler::OnSaveTraceFile, |
215 base::Unretained(this))); | 200 base::Unretained(this))); |
216 } | 201 } |
217 | 202 |
218 void TracingMessageHandler::OnTracingControllerInitialized( | 203 void TracingMessageHandler::OnTracingControllerInitialized( |
219 const ListValue* args) { | 204 const ListValue* args) { |
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
221 | 206 |
222 // Watch for changes in GPUInfo | |
223 if (!observing_) | |
224 GpuDataManager::GetInstance()->AddObserver(this); | |
225 observing_ = true; | |
226 | |
227 // Tell GpuDataManager it should have full GpuInfo. If the | |
228 // Gpu process has not run yet, this will trigger its launch. | |
229 GpuDataManager::GetInstance()->RequestCompleteGpuInfoIfNeeded(); | |
230 | |
231 // Run callback immediately in case the info is ready and no update in the | |
232 // future. | |
233 OnGpuInfoUpdate(); | |
234 | |
235 // Send the client info to the tracingController | 207 // Send the client info to the tracingController |
236 { | 208 { |
237 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); | 209 scoped_ptr<DictionaryValue> dict(new DictionaryValue()); |
238 chrome::VersionInfo version_info; | 210 chrome::VersionInfo version_info; |
239 | 211 |
240 if (!version_info.is_valid()) { | 212 if (!version_info.is_valid()) { |
241 DLOG(ERROR) << "Unable to create chrome::VersionInfo"; | 213 DLOG(ERROR) << "Unable to create chrome::VersionInfo"; |
242 } else { | 214 } else { |
243 // We have everything we need to send the right values. | 215 // We have everything we need to send the right values. |
244 dict->SetString("version", version_info.Version()); | 216 dict->SetString("version", version_info.Version()); |
245 dict->SetString("cl", version_info.LastChange()); | 217 dict->SetString("cl", version_info.LastChange()); |
246 dict->SetString("version_mod", | 218 dict->SetString("version_mod", |
247 chrome::VersionInfo::GetVersionStringModifier()); | 219 chrome::VersionInfo::GetVersionStringModifier()); |
248 dict->SetString("official", | 220 dict->SetString("official", |
249 l10n_util::GetStringUTF16( | 221 l10n_util::GetStringUTF16( |
250 version_info.IsOfficialBuild() ? | 222 version_info.IsOfficialBuild() ? |
251 IDS_ABOUT_VERSION_OFFICIAL : | 223 IDS_ABOUT_VERSION_OFFICIAL : |
252 IDS_ABOUT_VERSION_UNOFFICIAL)); | 224 IDS_ABOUT_VERSION_UNOFFICIAL)); |
253 | 225 |
254 dict->SetString("command_line", | 226 dict->SetString("command_line", |
255 CommandLine::ForCurrentProcess()->GetCommandLineString()); | 227 CommandLine::ForCurrentProcess()->GetCommandLineString()); |
256 } | 228 } |
257 | 229 |
258 dict->SetString("blacklist_version", | 230 dict->SetString("blacklist_version", |
259 GpuBlacklist::GetInstance()->GetVersion()); | 231 GpuDataManager::GetInstance()->GetBlacklistVersion()); |
260 web_ui()->CallJavascriptFunction("tracingController.onClientInfoUpdate", | 232 web_ui()->CallJavascriptFunction("tracingController.onClientInfoUpdate", |
261 *dict); | 233 *dict); |
262 } | 234 } |
263 } | 235 } |
264 | 236 |
265 void TracingMessageHandler::OnBeginRequestBufferPercentFull( | 237 void TracingMessageHandler::OnBeginRequestBufferPercentFull( |
266 const ListValue* list) { | 238 const ListValue* list) { |
267 TraceController::GetInstance()->GetTraceBufferPercentFullAsync(this); | 239 TraceController::GetInstance()->GetTraceBufferPercentFullAsync(this); |
268 } | 240 } |
269 | 241 |
270 void TracingMessageHandler::OnGpuInfoUpdate() { | |
271 // Get GPU Info. | |
272 scoped_ptr<base::DictionaryValue> gpu_info_val( | |
273 gpu_util::GpuInfoAsDictionaryValue()); | |
274 | |
275 // Add in blacklisting features | |
276 Value* feature_status = gpu_util::GetFeatureStatus(); | |
277 if (feature_status) | |
278 gpu_info_val->Set("featureStatus", feature_status); | |
279 | |
280 // Send GPU Info to javascript. | |
281 web_ui()->CallJavascriptFunction("tracingController.onGpuInfoUpdate", | |
282 *(gpu_info_val.get())); | |
283 } | |
284 | |
285 // A callback used for asynchronously reading a file to a string. Calls the | 242 // A callback used for asynchronously reading a file to a string. Calls the |
286 // TaskProxy callback when reading is complete. | 243 // TaskProxy callback when reading is complete. |
287 void ReadTraceFileCallback(TaskProxy* proxy, const FilePath& path) { | 244 void ReadTraceFileCallback(TaskProxy* proxy, const FilePath& path) { |
288 std::string file_contents; | 245 std::string file_contents; |
289 if (!file_util::ReadFileToString(path, &file_contents)) | 246 if (!file_util::ReadFileToString(path, &file_contents)) |
290 return; | 247 return; |
291 | 248 |
292 // We need to escape the file contents, because it will go into a javascript | 249 // We need to escape the file contents, because it will go into a javascript |
293 // quoted string in TracingMessageHandler::LoadTraceFileComplete. We need to | 250 // quoted string in TracingMessageHandler::LoadTraceFileComplete. We need to |
294 // escape control characters (to have well-formed javascript statements), as | 251 // escape control characters (to have well-formed javascript statements), as |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 // | 505 // |
549 //////////////////////////////////////////////////////////////////////////////// | 506 //////////////////////////////////////////////////////////////////////////////// |
550 | 507 |
551 TracingUI::TracingUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 508 TracingUI::TracingUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
552 web_ui->AddMessageHandler(new TracingMessageHandler()); | 509 web_ui->AddMessageHandler(new TracingMessageHandler()); |
553 | 510 |
554 // Set up the chrome://tracing/ source. | 511 // Set up the chrome://tracing/ source. |
555 Profile* profile = Profile::FromWebUI(web_ui); | 512 Profile* profile = Profile::FromWebUI(web_ui); |
556 ChromeURLDataManager::AddDataSource(profile, CreateTracingHTMLSource()); | 513 ChromeURLDataManager::AddDataSource(profile, CreateTracingHTMLSource()); |
557 } | 514 } |
OLD | NEW |