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/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/callback_old.h" | |
12 #include "base/command_line.h" | 11 #include "base/command_line.h" |
13 #include "base/file_util.h" | 12 #include "base/file_util.h" |
14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
15 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
16 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/ui/shell_dialogs.h" | 17 #include "chrome/browser/ui/shell_dialogs.h" |
19 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 18 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
20 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
21 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
41 return source; | 40 return source; |
42 } | 41 } |
43 | 42 |
44 // This class receives javascript messages from the renderer. | 43 // This class receives javascript messages from the renderer. |
45 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 44 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
46 // this class's methods are expected to run on the UI thread. | 45 // this class's methods are expected to run on the UI thread. |
47 class TracingMessageHandler | 46 class TracingMessageHandler |
48 : public WebUIMessageHandler, | 47 : public WebUIMessageHandler, |
49 public SelectFileDialog::Listener, | 48 public SelectFileDialog::Listener, |
50 public base::SupportsWeakPtr<TracingMessageHandler>, | 49 public base::SupportsWeakPtr<TracingMessageHandler>, |
51 public TraceSubscriber { | 50 public TraceSubscriber, |
51 public GpuDataManager::Observer { | |
52 public: | 52 public: |
53 TracingMessageHandler(); | 53 TracingMessageHandler(); |
54 virtual ~TracingMessageHandler(); | 54 virtual ~TracingMessageHandler(); |
55 | 55 |
56 // WebUIMessageHandler implementation. | 56 // WebUIMessageHandler implementation. |
57 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | 57 virtual WebUIMessageHandler* Attach(WebUI* web_ui); |
58 virtual void RegisterMessages(); | 58 virtual void RegisterMessages(); |
59 | 59 |
60 // SelectFileDialog::Listener implementation | 60 // SelectFileDialog::Listener implementation |
61 virtual void FileSelected(const FilePath& path, int index, void* params); | 61 virtual void FileSelected(const FilePath& path, int index, void* params); |
62 virtual void FileSelectionCanceled(void* params); | 62 virtual void FileSelectionCanceled(void* params); |
63 | 63 |
64 // TraceSubscriber implementation. | 64 // TraceSubscriber implementation. |
65 virtual void OnEndTracingComplete(); | 65 virtual void OnEndTracingComplete(); |
66 virtual void OnTraceDataCollected(const std::string& trace_fragment); | 66 virtual void OnTraceDataCollected(const std::string& trace_fragment); |
67 virtual void OnTraceBufferPercentFullReply(float percent_full); | 67 virtual void OnTraceBufferPercentFullReply(float percent_full); |
68 | 68 |
69 // GpuDataManager::Observer implementation. | |
70 virtual void OnGpuInfoUpdate() OVERRIDE; | |
71 | |
69 // Messages. | 72 // Messages. |
70 void OnTracingControllerInitialized(const ListValue* list); | 73 void OnTracingControllerInitialized(const ListValue* list); |
71 void OnBeginTracing(const ListValue* list); | 74 void OnBeginTracing(const ListValue* list); |
72 void OnEndTracingAsync(const ListValue* list); | 75 void OnEndTracingAsync(const ListValue* list); |
73 void OnBeginRequestBufferPercentFull(const ListValue* list); | 76 void OnBeginRequestBufferPercentFull(const ListValue* list); |
74 void OnLoadTraceFile(const ListValue* list); | 77 void OnLoadTraceFile(const ListValue* list); |
75 void OnSaveTraceFile(const ListValue* list); | 78 void OnSaveTraceFile(const ListValue* list); |
76 | 79 |
77 // Callbacks. | 80 // Callbacks. |
78 void OnGpuInfoUpdate(); | |
79 | |
80 // Callbacks. | |
81 void LoadTraceFileComplete(std::string* file_contents); | 81 void LoadTraceFileComplete(std::string* file_contents); |
82 void SaveTraceFileComplete(); | 82 void SaveTraceFileComplete(); |
83 | 83 |
84 private: | 84 private: |
85 // The file dialog to select a file for loading or saving traces. | 85 // The file dialog to select a file for loading or saving traces. |
86 scoped_refptr<SelectFileDialog> select_trace_file_dialog_; | 86 scoped_refptr<SelectFileDialog> select_trace_file_dialog_; |
87 | 87 |
88 // The type of the file dialog as the same one is used for loading or saving | 88 // The type of the file dialog as the same one is used for loading or saving |
89 // traces. | 89 // traces. |
90 SelectFileDialog::Type select_trace_file_dialog_type_; | 90 SelectFileDialog::Type select_trace_file_dialog_type_; |
91 | 91 |
92 // The trace data that is to be written to the file on saving. | 92 // The trace data that is to be written to the file on saving. |
93 scoped_ptr<std::string> trace_data_to_save_; | 93 scoped_ptr<std::string> trace_data_to_save_; |
94 | 94 |
95 // True while tracing is active. | 95 // True while tracing is active. |
96 bool trace_enabled_; | 96 bool trace_enabled_; |
97 | 97 |
98 // Cache the Singleton for efficiency. | 98 // Cache the Singleton for efficiency. |
99 GpuDataManager* gpu_data_manager_; | 99 GpuDataManager* gpu_data_manager_; |
100 | 100 |
101 // Callback called when the GPU info is updated. | |
102 Callback0::Type* gpu_info_update_callback_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); | 101 DISALLOW_COPY_AND_ASSIGN(TracingMessageHandler); |
105 }; | 102 }; |
106 | 103 |
107 // A proxy passed to the Read and Write tasks used when loading or saving trace | 104 // A proxy passed to the Read and Write tasks used when loading or saving trace |
108 // data. | 105 // data. |
109 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { | 106 class TaskProxy : public base::RefCountedThreadSafe<TaskProxy> { |
110 public: | 107 public: |
111 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) | 108 explicit TaskProxy(const base::WeakPtr<TracingMessageHandler>& handler) |
112 : handler_(handler) {} | 109 : handler_(handler) {} |
113 void LoadTraceFileCompleteProxy(std::string* file_contents) { | 110 void LoadTraceFileCompleteProxy(std::string* file_contents) { |
(...skipping 17 matching lines...) Expand all Loading... | |
131 }; | 128 }; |
132 | 129 |
133 //////////////////////////////////////////////////////////////////////////////// | 130 //////////////////////////////////////////////////////////////////////////////// |
134 // | 131 // |
135 // TracingMessageHandler | 132 // TracingMessageHandler |
136 // | 133 // |
137 //////////////////////////////////////////////////////////////////////////////// | 134 //////////////////////////////////////////////////////////////////////////////// |
138 | 135 |
139 TracingMessageHandler::TracingMessageHandler() | 136 TracingMessageHandler::TracingMessageHandler() |
140 : select_trace_file_dialog_type_(SelectFileDialog::SELECT_NONE), | 137 : select_trace_file_dialog_type_(SelectFileDialog::SELECT_NONE), |
141 trace_enabled_(false), | 138 trace_enabled_(false) { |
142 gpu_info_update_callback_(NULL) { | |
143 gpu_data_manager_ = GpuDataManager::GetInstance(); | 139 gpu_data_manager_ = GpuDataManager::GetInstance(); |
144 DCHECK(gpu_data_manager_); | 140 DCHECK(gpu_data_manager_); |
145 } | 141 } |
146 | 142 |
147 TracingMessageHandler::~TracingMessageHandler() { | 143 TracingMessageHandler::~TracingMessageHandler() { |
148 if (gpu_info_update_callback_) { | 144 gpu_data_manager_->RemoveObserver(this); |
149 gpu_data_manager_->RemoveGpuInfoUpdateCallback(gpu_info_update_callback_); | |
150 delete gpu_info_update_callback_; | |
151 } | |
152 | 145 |
153 if (select_trace_file_dialog_) | 146 if (select_trace_file_dialog_) |
154 select_trace_file_dialog_->ListenerDestroyed(); | 147 select_trace_file_dialog_->ListenerDestroyed(); |
155 | 148 |
156 // If we are the current subscriber, this will result in ending tracing. | 149 // If we are the current subscriber, this will result in ending tracing. |
157 TraceController::GetInstance()->CancelSubscriber(this); | 150 TraceController::GetInstance()->CancelSubscriber(this); |
158 } | 151 } |
159 | 152 |
160 WebUIMessageHandler* TracingMessageHandler::Attach(WebUI* web_ui) { | 153 WebUIMessageHandler* TracingMessageHandler::Attach(WebUI* web_ui) { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 21 matching lines...) Expand all Loading... | |
183 base::Unretained(this))); | 176 base::Unretained(this))); |
184 web_ui_->RegisterMessageCallback("saveTraceFile", | 177 web_ui_->RegisterMessageCallback("saveTraceFile", |
185 base::Bind(&TracingMessageHandler::OnSaveTraceFile, | 178 base::Bind(&TracingMessageHandler::OnSaveTraceFile, |
186 base::Unretained(this))); | 179 base::Unretained(this))); |
187 } | 180 } |
188 | 181 |
189 void TracingMessageHandler::OnTracingControllerInitialized( | 182 void TracingMessageHandler::OnTracingControllerInitialized( |
190 const ListValue* args) { | 183 const ListValue* args) { |
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
192 | 185 |
193 DCHECK(!gpu_info_update_callback_); | |
194 | |
195 // Watch for changes in GPUInfo | 186 // Watch for changes in GPUInfo |
196 gpu_info_update_callback_ = | 187 gpu_data_manager_->AddObserver(this); |
Zhenyao Mo
2011/10/26 00:03:12
Since we always call RemoveObserver() in destructo
csilv
2011/10/26 00:16:14
This is actually a better approach to do it here.
| |
197 NewCallback(this, &TracingMessageHandler::OnGpuInfoUpdate); | |
198 gpu_data_manager_->AddGpuInfoUpdateCallback(gpu_info_update_callback_); | |
199 | 188 |
200 // Tell GpuDataManager it should have full GpuInfo. If the | 189 // Tell GpuDataManager it should have full GpuInfo. If the |
201 // Gpu process has not run yet, this will trigger its launch. | 190 // Gpu process has not run yet, this will trigger its launch. |
202 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded(); | 191 gpu_data_manager_->RequestCompleteGpuInfoIfNeeded(); |
203 | 192 |
204 // Run callback immediately in case the info is ready and no update in the | 193 // Run callback immediately in case the info is ready and no update in the |
205 // future. | 194 // future. |
206 OnGpuInfoUpdate(); | 195 OnGpuInfoUpdate(); |
207 | 196 |
208 // Send the client info to the tracingController | 197 // Send the client info to the tracingController |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 // | 440 // |
452 //////////////////////////////////////////////////////////////////////////////// | 441 //////////////////////////////////////////////////////////////////////////////// |
453 | 442 |
454 TracingUI::TracingUI(TabContents* contents) : ChromeWebUI(contents) { | 443 TracingUI::TracingUI(TabContents* contents) : ChromeWebUI(contents) { |
455 AddMessageHandler((new TracingMessageHandler())->Attach(this)); | 444 AddMessageHandler((new TracingMessageHandler())->Attach(this)); |
456 | 445 |
457 // Set up the chrome://tracing/ source. | 446 // Set up the chrome://tracing/ source. |
458 Profile::FromBrowserContext(contents->browser_context())-> | 447 Profile::FromBrowserContext(contents->browser_context())-> |
459 GetChromeURLDataManager()->AddDataSource(CreateTracingHTMLSource()); | 448 GetChromeURLDataManager()->AddDataSource(CreateTracingHTMLSource()); |
460 } | 449 } |
OLD | NEW |