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/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 for (std::set<std::string>::const_iterator it = categorySet.begin(); | 34 for (std::set<std::string>::const_iterator it = categorySet.begin(); |
35 it != categorySet.end(); it++) { | 35 it != categorySet.end(); it++) { |
36 category_list->AppendString(*it); | 36 category_list->AppendString(*it); |
37 } | 37 } |
38 | 38 |
39 base::RefCountedString* res = new base::RefCountedString(); | 39 base::RefCountedString* res = new base::RefCountedString(); |
40 base::JSONWriter::Write(category_list.get(), &res->data()); | 40 base::JSONWriter::Write(category_list.get(), &res->data()); |
41 callback.Run(res); | 41 callback.Run(res); |
42 } | 42 } |
43 | 43 |
44 bool GetTracingOptions(const std::string& data64, | 44 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
45 std::string* category_filter_string, | 45 |
46 int* tracing_options) | 46 bool OnBeginRecording(const std::string& data64, |
47 { | 47 const WebUIDataSource::GotDataCallback& callback) { |
48 std::string data; | 48 std::string data; |
49 if (!base::Base64Decode(data64, &data)) { | 49 if (!base::Base64Decode(data64, &data)) { |
50 LOG(ERROR) << "Options were not base64 encoded."; | 50 LOG(ERROR) << "Options were not base64 encoded."; |
51 return false; | 51 return false; |
52 } | 52 } |
53 | 53 |
54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); | 54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); |
55 if (!optionsRaw) { | 55 if (!optionsRaw) { |
56 LOG(ERROR) << "Options were not valid JSON"; | 56 LOG(ERROR) << "Options were not valid JSON"; |
57 return false; | 57 return false; |
58 } | 58 } |
59 base::DictionaryValue* options; | 59 base::DictionaryValue* options; |
60 if (!optionsRaw->GetAsDictionary(&options)) { | 60 if (!optionsRaw->GetAsDictionary(&options)) { |
61 LOG(ERROR) << "Options must be dict"; | 61 LOG(ERROR) << "Options must be dict"; |
62 return false; | 62 return false; |
63 } | 63 } |
64 | 64 |
| 65 std::string category_filter_string; |
65 bool use_system_tracing; | 66 bool use_system_tracing; |
66 bool use_continuous_tracing; | 67 bool use_continuous_tracing; |
67 bool use_sampling; | 68 bool use_sampling; |
68 | 69 |
69 bool options_ok = true; | 70 bool options_ok = true; |
70 options_ok &= options->GetString("categoryFilter", category_filter_string); | 71 options_ok &= options->GetString("categoryFilter", &category_filter_string); |
71 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); | 72 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); |
72 options_ok &= options->GetBoolean("useContinuousTracing", | 73 options_ok &= options->GetBoolean("useContinuousTracing", |
73 &use_continuous_tracing); | 74 &use_continuous_tracing); |
74 options_ok &= options->GetBoolean("useSampling", &use_sampling); | 75 options_ok &= options->GetBoolean("useSampling", &use_sampling); |
75 if (!options_ok) { | 76 if (!options_ok) { |
76 LOG(ERROR) << "Malformed options"; | 77 LOG(ERROR) << "Malformed options"; |
77 return false; | 78 return false; |
78 } | 79 } |
79 | 80 |
80 *tracing_options = 0; | 81 int tracing_options = 0; |
81 if (use_system_tracing) | 82 if (use_system_tracing) |
82 *tracing_options |= TracingController::ENABLE_SYSTRACE; | 83 tracing_options |= TracingController::ENABLE_SYSTRACE; |
83 if (use_sampling) | 84 if (use_sampling) |
84 *tracing_options |= TracingController::ENABLE_SAMPLING; | 85 tracing_options |= TracingController::ENABLE_SAMPLING; |
85 if (use_continuous_tracing) | 86 if (use_continuous_tracing) |
86 *tracing_options |= TracingController::RECORD_CONTINUOUSLY; | 87 tracing_options |= TracingController::RECORD_CONTINUOUSLY; |
87 return true; | |
88 } | |
89 | |
90 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); | |
91 | |
92 bool OnBeginRecording(const std::string& data64, | |
93 const WebUIDataSource::GotDataCallback& callback) { | |
94 std::string category_filter_string; | |
95 int tracing_options = 0; | |
96 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | |
97 return false; | |
98 | 88 |
99 return TracingController::GetInstance()->EnableRecording( | 89 return TracingController::GetInstance()->EnableRecording( |
100 category_filter_string, | 90 category_filter_string, |
101 static_cast<TracingController::Options>(tracing_options), | 91 static_cast<TracingController::Options>(tracing_options), |
102 base::Bind(&OnRecordingEnabledAck, callback)); | 92 base::Bind(&OnRecordingEnabledAck, callback)); |
103 } | 93 } |
104 | 94 |
105 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | 95 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { |
106 base::RefCountedString* res = new base::RefCountedString(); | 96 base::RefCountedString* res = new base::RefCountedString(); |
107 callback.Run(res); | 97 callback.Run(res); |
(...skipping 15 matching lines...) Expand all Loading... |
123 } | 113 } |
124 | 114 |
125 void BeginReadingRecordingResult( | 115 void BeginReadingRecordingResult( |
126 const WebUIDataSource::GotDataCallback& callback, | 116 const WebUIDataSource::GotDataCallback& callback, |
127 const base::FilePath& path) { | 117 const base::FilePath& path) { |
128 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
129 BrowserThread::FILE, FROM_HERE, | 119 BrowserThread::FILE, FROM_HERE, |
130 base::Bind(ReadRecordingResult, callback, path)); | 120 base::Bind(ReadRecordingResult, callback, path)); |
131 } | 121 } |
132 | 122 |
133 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 123 bool OnBeginRequest(const std::string& path, |
134 | |
135 bool OnEnableMonitoring(const std::string& data64, | |
136 const WebUIDataSource::GotDataCallback& callback) { | |
137 std::string category_filter_string; | |
138 int tracing_options = 0; | |
139 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | |
140 return false; | |
141 | |
142 return TracingController::GetInstance()->EnableMonitoring( | |
143 category_filter_string, | |
144 static_cast<TracingController::Options>(tracing_options), | |
145 base::Bind(OnMonitoringEnabledAck, callback)); | |
146 } | |
147 | |
148 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | |
149 base::RefCountedString* res = new base::RefCountedString(); | |
150 callback.Run(res); | |
151 } | |
152 | |
153 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { | |
154 base::RefCountedString* res = new base::RefCountedString(); | |
155 callback.Run(res); | |
156 } | |
157 | |
158 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback, | |
159 const base::FilePath& path) { | |
160 std::string tmp; | |
161 if (!base::ReadFileToString(path, &tmp)) | |
162 LOG(ERROR) << "Failed to read file " << path.value(); | |
163 base::DeleteFile(path, false); | |
164 callback.Run(base::RefCountedString::TakeString(&tmp)); | |
165 } | |
166 | |
167 void OnMonitoringSnapshotCaptured( | |
168 const WebUIDataSource::GotDataCallback& callback, | |
169 const base::FilePath& path) { | |
170 BrowserThread::PostTask( | |
171 BrowserThread::FILE, FROM_HERE, | |
172 base::Bind(ReadMonitoringSnapshot, callback, path)); | |
173 } | |
174 | |
175 bool OnTracingRequest(const std::string& path, | |
176 const WebUIDataSource::GotDataCallback& callback) { | 124 const WebUIDataSource::GotDataCallback& callback) { |
177 if (path == "json/categories") { | 125 if (path == "json/categories") { |
178 TracingController::GetInstance()->GetCategories( | 126 TracingController::GetInstance()->GetCategories( |
179 base::Bind(OnGotCategories, callback)); | 127 base::Bind(OnGotCategories, callback)); |
180 return true; | 128 return true; |
181 } | 129 } |
182 | |
183 const char* beginRecordingPath = "json/begin_recording?"; | 130 const char* beginRecordingPath = "json/begin_recording?"; |
184 if (path.find(beginRecordingPath) == 0) { | 131 if (path.find(beginRecordingPath) == 0) { |
185 std::string data = path.substr(strlen(beginRecordingPath)); | 132 std::string data = path.substr(strlen(beginRecordingPath)); |
186 return OnBeginRecording(data, callback); | 133 return OnBeginRecording(data, callback); |
187 } | 134 } |
188 if (path == "json/get_buffer_percent_full") { | 135 if (path == "json/get_buffer_percent_full") { |
189 return TracingController::GetInstance()->GetTraceBufferPercentFull( | 136 return TracingController::GetInstance()->GetTraceBufferPercentFull( |
190 base::Bind(OnTraceBufferPercentFullResult, callback)); | 137 base::Bind(OnTraceBufferPercentFullResult, callback)); |
191 } | 138 } |
192 if (path == "json/end_recording") { | 139 if (path == "json/end_recording") { |
193 return TracingController::GetInstance()->DisableRecording( | 140 return TracingController::GetInstance()->DisableRecording( |
194 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); | 141 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); |
195 } | 142 } |
196 | |
197 const char* enableMonitoringPath = "json/begin_monitoring?"; | |
198 if (path.find(enableMonitoringPath) == 0) { | |
199 std::string data = path.substr(strlen(enableMonitoringPath)); | |
200 return OnEnableMonitoring(data, callback); | |
201 } | |
202 if (path == "json/end_monitoring") { | |
203 return TracingController::GetInstance()->DisableMonitoring( | |
204 base::Bind(OnMonitoringDisabled, callback)); | |
205 } | |
206 if (path == "json/capture_monitoring") { | |
207 TracingController::GetInstance()->CaptureMonitoringSnapshot( | |
208 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback)); | |
209 return true; | |
210 } | |
211 | |
212 if (StartsWithASCII(path, "json/", true)) | 143 if (StartsWithASCII(path, "json/", true)) |
213 LOG(ERROR) << "Unhandled request to " << path; | 144 LOG(ERROR) << "Unhandled request to " << path; |
214 return false; | 145 return false; |
215 } | 146 } |
216 | 147 |
217 } // namespace | 148 } // namespace |
218 | 149 |
219 | 150 |
220 //////////////////////////////////////////////////////////////////////////////// | 151 //////////////////////////////////////////////////////////////////////////////// |
221 // | 152 // |
222 // TracingUI | 153 // TracingUI |
223 // | 154 // |
224 //////////////////////////////////////////////////////////////////////////////// | 155 //////////////////////////////////////////////////////////////////////////////// |
225 | 156 |
226 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { | 157 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
227 // Set up the chrome://tracing/ source. | 158 // Set up the chrome://tracing/ source. |
228 BrowserContext* browser_context = | 159 BrowserContext* browser_context = |
229 web_ui->GetWebContents()->GetBrowserContext(); | 160 web_ui->GetWebContents()->GetBrowserContext(); |
230 | 161 |
231 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); | 162 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); |
232 source->SetJsonPath("strings.js"); | 163 source->SetJsonPath("strings.js"); |
233 source->SetDefaultResource(IDR_TRACING_HTML); | 164 source->SetDefaultResource(IDR_TRACING_HTML); |
234 source->AddResourcePath("tracing.js", IDR_TRACING_JS); | 165 source->AddResourcePath("tracing.js", IDR_TRACING_JS); |
235 source->SetRequestFilter(base::Bind(OnTracingRequest)); | 166 source->SetRequestFilter(base::Bind(OnBeginRequest)); |
236 WebUIDataSource::Add(browser_context, source); | 167 WebUIDataSource::Add(browser_context, source); |
237 } | 168 } |
238 | 169 |
239 } // namespace content | 170 } // namespace content |
OLD | NEW |