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 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 44 bool GetTracingOptions(const std::string& data64, |
45 | 45 std::string* category_filter_string, |
46 bool OnBeginRecording(const std::string& data64, | 46 int* tracing_options) |
47 const WebUIDataSource::GotDataCallback& callback) { | 47 { |
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; | |
66 bool use_system_tracing; | 65 bool use_system_tracing; |
67 bool use_continuous_tracing; | 66 bool use_continuous_tracing; |
68 bool use_sampling; | 67 bool use_sampling; |
69 | 68 |
70 bool options_ok = true; | 69 bool options_ok = true; |
71 options_ok &= options->GetString("categoryFilter", &category_filter_string); | 70 options_ok &= options->GetString("categoryFilter", category_filter_string); |
72 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); | 71 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); |
73 options_ok &= options->GetBoolean("useContinuousTracing", | 72 options_ok &= options->GetBoolean("useContinuousTracing", |
74 &use_continuous_tracing); | 73 &use_continuous_tracing); |
75 options_ok &= options->GetBoolean("useSampling", &use_sampling); | 74 options_ok &= options->GetBoolean("useSampling", &use_sampling); |
76 if (!options_ok) { | 75 if (!options_ok) { |
77 LOG(ERROR) << "Malformed options"; | 76 LOG(ERROR) << "Malformed options"; |
78 return false; | 77 return false; |
79 } | 78 } |
80 | 79 |
| 80 *tracing_options = 0; |
| 81 if (use_system_tracing) |
| 82 *tracing_options |= TracingController::ENABLE_SYSTRACE; |
| 83 if (use_sampling) |
| 84 *tracing_options |= TracingController::ENABLE_SAMPLING; |
| 85 if (use_continuous_tracing) |
| 86 *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; |
81 int tracing_options = 0; | 95 int tracing_options = 0; |
82 if (use_system_tracing) | 96 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) |
83 tracing_options |= TracingController::ENABLE_SYSTRACE; | 97 return false; |
84 if (use_sampling) | |
85 tracing_options |= TracingController::ENABLE_SAMPLING; | |
86 if (use_continuous_tracing) | |
87 tracing_options |= TracingController::RECORD_CONTINUOUSLY; | |
88 | 98 |
89 return TracingController::GetInstance()->EnableRecording( | 99 return TracingController::GetInstance()->EnableRecording( |
90 category_filter_string, | 100 category_filter_string, |
91 static_cast<TracingController::Options>(tracing_options), | 101 static_cast<TracingController::Options>(tracing_options), |
92 base::Bind(&OnRecordingEnabledAck, callback)); | 102 base::Bind(&OnRecordingEnabledAck, callback)); |
93 } | 103 } |
94 | 104 |
95 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | 105 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { |
96 base::RefCountedString* res = new base::RefCountedString(); | 106 base::RefCountedString* res = new base::RefCountedString(); |
97 callback.Run(res); | 107 callback.Run(res); |
(...skipping 15 matching lines...) Expand all Loading... |
113 } | 123 } |
114 | 124 |
115 void BeginReadingRecordingResult( | 125 void BeginReadingRecordingResult( |
116 const WebUIDataSource::GotDataCallback& callback, | 126 const WebUIDataSource::GotDataCallback& callback, |
117 const base::FilePath& path) { | 127 const base::FilePath& path) { |
118 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
119 BrowserThread::FILE, FROM_HERE, | 129 BrowserThread::FILE, FROM_HERE, |
120 base::Bind(ReadRecordingResult, callback, path)); | 130 base::Bind(ReadRecordingResult, callback, path)); |
121 } | 131 } |
122 | 132 |
| 133 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 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 |
123 bool OnBeginJSONRequest(const std::string& path, | 175 bool OnBeginJSONRequest(const std::string& path, |
124 const WebUIDataSource::GotDataCallback& callback) { | 176 const WebUIDataSource::GotDataCallback& callback) { |
125 if (path == "json/categories") { | 177 if (path == "json/categories") { |
126 return TracingController::GetInstance()->GetCategories( | 178 return TracingController::GetInstance()->GetCategories( |
127 base::Bind(OnGotCategories, callback)); | 179 base::Bind(OnGotCategories, callback)); |
128 } | 180 } |
| 181 |
129 const char* beginRecordingPath = "json/begin_recording?"; | 182 const char* beginRecordingPath = "json/begin_recording?"; |
130 if (StartsWithASCII(path, beginRecordingPath, true)) { | 183 if (StartsWithASCII(path, beginRecordingPath, true)) { |
131 std::string data = path.substr(strlen(beginRecordingPath)); | 184 std::string data = path.substr(strlen(beginRecordingPath)); |
132 return OnBeginRecording(data, callback); | 185 return OnBeginRecording(data, callback); |
133 } | 186 } |
134 if (path == "json/get_buffer_percent_full") { | 187 if (path == "json/get_buffer_percent_full") { |
135 return TracingController::GetInstance()->GetTraceBufferPercentFull( | 188 return TracingController::GetInstance()->GetTraceBufferPercentFull( |
136 base::Bind(OnTraceBufferPercentFullResult, callback)); | 189 base::Bind(OnTraceBufferPercentFullResult, callback)); |
137 } | 190 } |
138 if (path == "json/end_recording") { | 191 if (path == "json/end_recording") { |
139 return TracingController::GetInstance()->DisableRecording( | 192 return TracingController::GetInstance()->DisableRecording( |
140 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); | 193 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); |
141 } | 194 } |
| 195 |
| 196 const char* enableMonitoringPath = "json/begin_monitoring?"; |
| 197 if (path.find(enableMonitoringPath) == 0) { |
| 198 std::string data = path.substr(strlen(enableMonitoringPath)); |
| 199 return OnEnableMonitoring(data, callback); |
| 200 } |
| 201 if (path == "json/end_monitoring") { |
| 202 return TracingController::GetInstance()->DisableMonitoring( |
| 203 base::Bind(OnMonitoringDisabled, callback)); |
| 204 } |
| 205 if (path == "json/capture_monitoring") { |
| 206 TracingController::GetInstance()->CaptureMonitoringSnapshot( |
| 207 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback)); |
| 208 return true; |
| 209 } |
142 LOG(ERROR) << "Unhandled request to " << path; | 210 LOG(ERROR) << "Unhandled request to " << path; |
143 return false; | 211 return false; |
144 } | 212 } |
145 | 213 |
146 bool OnBeginRequest(const std::string& path, | 214 bool OnTracingRequest(const std::string& path, |
147 const WebUIDataSource::GotDataCallback& callback) { | 215 const WebUIDataSource::GotDataCallback& callback) { |
148 if (StartsWithASCII(path, "json/", true)) { | 216 if (StartsWithASCII(path, "json/", true)) { |
149 if (!OnBeginJSONRequest(path, callback)) { | 217 if (!OnBeginJSONRequest(path, callback)) { |
150 std::string error("##ERROR##"); | 218 std::string error("##ERROR##"); |
151 callback.Run(base::RefCountedString::TakeString(&error)); | 219 callback.Run(base::RefCountedString::TakeString(&error)); |
152 } | 220 } |
153 return true; | 221 return true; |
154 } | 222 } |
155 return false; | 223 return false; |
156 } | 224 } |
157 | 225 |
158 } // namespace | 226 } // namespace |
159 | 227 |
160 | 228 |
161 //////////////////////////////////////////////////////////////////////////////// | 229 //////////////////////////////////////////////////////////////////////////////// |
162 // | 230 // |
163 // TracingUI | 231 // TracingUI |
164 // | 232 // |
165 //////////////////////////////////////////////////////////////////////////////// | 233 //////////////////////////////////////////////////////////////////////////////// |
166 | 234 |
167 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { | 235 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { |
168 // Set up the chrome://tracing/ source. | 236 // Set up the chrome://tracing/ source. |
169 BrowserContext* browser_context = | 237 BrowserContext* browser_context = |
170 web_ui->GetWebContents()->GetBrowserContext(); | 238 web_ui->GetWebContents()->GetBrowserContext(); |
171 | 239 |
172 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); | 240 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); |
173 source->SetJsonPath("strings.js"); | 241 source->SetJsonPath("strings.js"); |
174 source->SetDefaultResource(IDR_TRACING_HTML); | 242 source->SetDefaultResource(IDR_TRACING_HTML); |
175 source->AddResourcePath("tracing.js", IDR_TRACING_JS); | 243 source->AddResourcePath("tracing.js", IDR_TRACING_JS); |
176 source->SetRequestFilter(base::Bind(OnBeginRequest)); | 244 source->SetRequestFilter(base::Bind(OnTracingRequest)); |
177 WebUIDataSource::Add(browser_context, source); | 245 WebUIDataSource::Add(browser_context, source); |
178 } | 246 } |
179 | 247 |
180 } // namespace content | 248 } // namespace content |
OLD | NEW |