| 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 26 matching lines...) Expand all Loading... |
| 37 category_list->AppendString(*it); | 37 category_list->AppendString(*it); |
| 38 } | 38 } |
| 39 | 39 |
| 40 base::RefCountedString* res = new base::RefCountedString(); | 40 base::RefCountedString* res = new base::RefCountedString(); |
| 41 base::JSONWriter::Write(category_list.get(), &res->data()); | 41 base::JSONWriter::Write(category_list.get(), &res->data()); |
| 42 callback.Run(res); | 42 callback.Run(res); |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool GetTracingOptions(const std::string& data64, | 45 bool GetTracingOptions(const std::string& data64, |
| 46 std::string* category_filter_string, | 46 std::string* category_filter_string, |
| 47 int* tracing_options) | 47 int* tracing_options) { |
| 48 { | |
| 49 std::string data; | 48 std::string data; |
| 50 if (!base::Base64Decode(data64, &data)) { | 49 if (!base::Base64Decode(data64, &data)) { |
| 51 LOG(ERROR) << "Options were not base64 encoded."; | 50 LOG(ERROR) << "Options were not base64 encoded."; |
| 52 return false; | 51 return false; |
| 53 } | 52 } |
| 54 | 53 |
| 55 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); | 54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); |
| 56 if (!optionsRaw) { | 55 if (!optionsRaw) { |
| 57 LOG(ERROR) << "Options were not valid JSON"; | 56 LOG(ERROR) << "Options were not valid JSON"; |
| 58 return false; | 57 return false; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 83 *tracing_options |= TracingController::ENABLE_SYSTRACE; | 82 *tracing_options |= TracingController::ENABLE_SYSTRACE; |
| 84 if (use_sampling) | 83 if (use_sampling) |
| 85 *tracing_options |= TracingController::ENABLE_SAMPLING; | 84 *tracing_options |= TracingController::ENABLE_SAMPLING; |
| 86 if (use_continuous_tracing) | 85 if (use_continuous_tracing) |
| 87 *tracing_options |= TracingController::RECORD_CONTINUOUSLY; | 86 *tracing_options |= TracingController::RECORD_CONTINUOUSLY; |
| 88 return true; | 87 return true; |
| 89 } | 88 } |
| 90 | 89 |
| 91 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 90 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 92 | 91 |
| 93 bool OnBeginRecording(const std::string& data64, | 92 bool BeginRecording(const std::string& data64, |
| 94 const WebUIDataSource::GotDataCallback& callback) { | 93 const WebUIDataSource::GotDataCallback& callback) { |
| 95 std::string category_filter_string; | 94 std::string category_filter_string; |
| 96 int tracing_options = 0; | 95 int tracing_options = 0; |
| 97 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | 96 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) |
| 98 return false; | 97 return false; |
| 99 | 98 |
| 100 return TracingController::GetInstance()->EnableRecording( | 99 return TracingController::GetInstance()->EnableRecording( |
| 101 category_filter_string, | 100 category_filter_string, |
| 102 static_cast<TracingController::Options>(tracing_options), | 101 static_cast<TracingController::Options>(tracing_options), |
| 103 base::Bind(&OnRecordingEnabledAck, callback)); | 102 base::Bind(&OnRecordingEnabledAck, callback)); |
| 104 } | 103 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 126 void BeginReadingRecordingResult( | 125 void BeginReadingRecordingResult( |
| 127 const WebUIDataSource::GotDataCallback& callback, | 126 const WebUIDataSource::GotDataCallback& callback, |
| 128 const base::FilePath& path) { | 127 const base::FilePath& path) { |
| 129 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
| 130 BrowserThread::FILE, FROM_HERE, | 129 BrowserThread::FILE, FROM_HERE, |
| 131 base::Bind(ReadRecordingResult, callback, path)); | 130 base::Bind(ReadRecordingResult, callback, path)); |
| 132 } | 131 } |
| 133 | 132 |
| 134 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 133 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 135 | 134 |
| 136 bool OnEnableMonitoring(const std::string& data64, | 135 bool EnableMonitoring(const std::string& data64, |
| 137 const WebUIDataSource::GotDataCallback& callback) { | 136 const WebUIDataSource::GotDataCallback& callback) { |
| 138 std::string category_filter_string; | 137 std::string category_filter_string; |
| 139 int tracing_options = 0; | 138 int tracing_options = 0; |
| 140 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | 139 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) |
| 141 return false; | 140 return false; |
| 142 | 141 |
| 143 return TracingController::GetInstance()->EnableMonitoring( | 142 return TracingController::GetInstance()->EnableMonitoring( |
| 144 category_filter_string, | 143 category_filter_string, |
| 145 static_cast<TracingController::Options>(tracing_options), | 144 static_cast<TracingController::Options>(tracing_options), |
| 146 base::Bind(OnMonitoringEnabledAck, callback)); | 145 base::Bind(OnMonitoringEnabledAck, callback)); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | 148 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { |
| 150 base::RefCountedString* res = new base::RefCountedString(); | 149 base::RefCountedString* res = new base::RefCountedString(); |
| 151 callback.Run(res); | 150 callback.Run(res); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { | 153 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { |
| 155 base::RefCountedString* res = new base::RefCountedString(); | 154 base::RefCountedString* res = new base::RefCountedString(); |
| 156 callback.Run(res); | 155 callback.Run(res); |
| 157 } | 156 } |
| 158 | 157 |
| 158 void GetMonitoringStatus(const WebUIDataSource::GotDataCallback& callback) { |
| 159 bool is_monitoring; |
| 160 std::string category_filter; |
| 161 TracingController::Options options; |
| 162 TracingController::GetInstance()->GetMonitoringStatus( |
| 163 &is_monitoring, &category_filter, &options); |
| 164 |
| 165 scoped_ptr<base::DictionaryValue> |
| 166 monitoring_options(new base::DictionaryValue()); |
| 167 monitoring_options->SetBoolean("isMonitoring", is_monitoring); |
| 168 monitoring_options->SetString("categoryFilter", category_filter); |
| 169 monitoring_options->SetBoolean("useSystemTracing", |
| 170 (options & TracingController::ENABLE_SYSTRACE) != 0); |
| 171 monitoring_options->SetBoolean("useContinuousTracing", |
| 172 (options & TracingController::RECORD_CONTINUOUSLY) != 0); |
| 173 monitoring_options->SetBoolean("useSampling", |
| 174 (options & TracingController::ENABLE_SAMPLING) != 0); |
| 175 |
| 176 std::string monitoring_options_json; |
| 177 base::JSONWriter::Write(monitoring_options.get(), &monitoring_options_json); |
| 178 |
| 179 base::RefCountedString* monitoring_options_base64 = |
| 180 new base::RefCountedString(); |
| 181 base::Base64Encode(monitoring_options_json, |
| 182 &monitoring_options_base64->data()); |
| 183 callback.Run(monitoring_options_base64); |
| 184 } |
| 185 |
| 159 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback, | 186 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback, |
| 160 const base::FilePath& path) { | 187 const base::FilePath& path) { |
| 161 std::string tmp; | 188 std::string tmp; |
| 162 if (!base::ReadFileToString(path, &tmp)) | 189 if (!base::ReadFileToString(path, &tmp)) |
| 163 LOG(ERROR) << "Failed to read file " << path.value(); | 190 LOG(ERROR) << "Failed to read file " << path.value(); |
| 164 base::DeleteFile(path, false); | 191 base::DeleteFile(path, false); |
| 165 callback.Run(base::RefCountedString::TakeString(&tmp)); | 192 callback.Run(base::RefCountedString::TakeString(&tmp)); |
| 166 } | 193 } |
| 167 | 194 |
| 168 void OnMonitoringSnapshotCaptured( | 195 void OnMonitoringSnapshotCaptured( |
| 169 const WebUIDataSource::GotDataCallback& callback, | 196 const WebUIDataSource::GotDataCallback& callback, |
| 170 const base::FilePath& path) { | 197 const base::FilePath& path) { |
| 171 BrowserThread::PostTask( | 198 BrowserThread::PostTask( |
| 172 BrowserThread::FILE, FROM_HERE, | 199 BrowserThread::FILE, FROM_HERE, |
| 173 base::Bind(ReadMonitoringSnapshot, callback, path)); | 200 base::Bind(ReadMonitoringSnapshot, callback, path)); |
| 174 } | 201 } |
| 175 | 202 |
| 176 bool OnBeginJSONRequest(const std::string& path, | 203 bool OnBeginJSONRequest(const std::string& path, |
| 177 const WebUIDataSource::GotDataCallback& callback) { | 204 const WebUIDataSource::GotDataCallback& callback) { |
| 178 if (path == "json/categories") { | 205 if (path == "json/categories") { |
| 179 return TracingController::GetInstance()->GetCategories( | 206 return TracingController::GetInstance()->GetCategories( |
| 180 base::Bind(OnGotCategories, callback)); | 207 base::Bind(OnGotCategories, callback)); |
| 181 } | 208 } |
| 182 | 209 |
| 183 const char* beginRecordingPath = "json/begin_recording?"; | 210 const char* beginRecordingPath = "json/begin_recording?"; |
| 184 if (StartsWithASCII(path, beginRecordingPath, true)) { | 211 if (StartsWithASCII(path, beginRecordingPath, true)) { |
| 185 std::string data = path.substr(strlen(beginRecordingPath)); | 212 std::string data = path.substr(strlen(beginRecordingPath)); |
| 186 return OnBeginRecording(data, callback); | 213 return BeginRecording(data, callback); |
| 187 } | 214 } |
| 188 if (path == "json/get_buffer_percent_full") { | 215 if (path == "json/get_buffer_percent_full") { |
| 189 return TracingController::GetInstance()->GetTraceBufferPercentFull( | 216 return TracingController::GetInstance()->GetTraceBufferPercentFull( |
| 190 base::Bind(OnTraceBufferPercentFullResult, callback)); | 217 base::Bind(OnTraceBufferPercentFullResult, callback)); |
| 191 } | 218 } |
| 192 if (path == "json/end_recording") { | 219 if (path == "json/end_recording") { |
| 193 return TracingController::GetInstance()->DisableRecording( | 220 return TracingController::GetInstance()->DisableRecording( |
| 194 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); | 221 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); |
| 195 } | 222 } |
| 196 | 223 |
| 197 const char* enableMonitoringPath = "json/begin_monitoring?"; | 224 const char* enableMonitoringPath = "json/begin_monitoring?"; |
| 198 if (path.find(enableMonitoringPath) == 0) { | 225 if (path.find(enableMonitoringPath) == 0) { |
| 199 std::string data = path.substr(strlen(enableMonitoringPath)); | 226 std::string data = path.substr(strlen(enableMonitoringPath)); |
| 200 return OnEnableMonitoring(data, callback); | 227 return EnableMonitoring(data, callback); |
| 201 } | 228 } |
| 202 if (path == "json/end_monitoring") { | 229 if (path == "json/end_monitoring") { |
| 203 return TracingController::GetInstance()->DisableMonitoring( | 230 return TracingController::GetInstance()->DisableMonitoring( |
| 204 base::Bind(OnMonitoringDisabled, callback)); | 231 base::Bind(OnMonitoringDisabled, callback)); |
| 205 } | 232 } |
| 206 if (path == "json/capture_monitoring") { | 233 if (path == "json/capture_monitoring") { |
| 207 TracingController::GetInstance()->CaptureMonitoringSnapshot( | 234 TracingController::GetInstance()->CaptureMonitoringSnapshot( |
| 208 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback)); | 235 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback)); |
| 209 return true; | 236 return true; |
| 210 } | 237 } |
| 238 if (path == "json/get_monitoring_status") { |
| 239 GetMonitoringStatus(callback); |
| 240 return true; |
| 241 } |
| 242 |
| 211 LOG(ERROR) << "Unhandled request to " << path; | 243 LOG(ERROR) << "Unhandled request to " << path; |
| 212 return false; | 244 return false; |
| 213 } | 245 } |
| 214 | 246 |
| 215 bool OnTracingRequest(const std::string& path, | 247 bool OnTracingRequest(const std::string& path, |
| 216 const WebUIDataSource::GotDataCallback& callback) { | 248 const WebUIDataSource::GotDataCallback& callback) { |
| 217 if (StartsWithASCII(path, "json/", true)) { | 249 if (StartsWithASCII(path, "json/", true)) { |
| 218 if (!OnBeginJSONRequest(path, callback)) { | 250 if (!OnBeginJSONRequest(path, callback)) { |
| 219 std::string error("##ERROR##"); | 251 std::string error("##ERROR##"); |
| 220 callback.Run(base::RefCountedString::TakeString(&error)); | 252 callback.Run(base::RefCountedString::TakeString(&error)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 250 TracingUI::~TracingUI() { | 282 TracingUI::~TracingUI() { |
| 251 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); | 283 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); |
| 252 } | 284 } |
| 253 | 285 |
| 254 void TracingUI::OnMonitoringStateChanged(bool is_monitoring) { | 286 void TracingUI::OnMonitoringStateChanged(bool is_monitoring) { |
| 255 web_ui()->CallJavascriptFunction( | 287 web_ui()->CallJavascriptFunction( |
| 256 "onMonitoringStateChanged", base::FundamentalValue(is_monitoring)); | 288 "onMonitoringStateChanged", base::FundamentalValue(is_monitoring)); |
| 257 } | 289 } |
| 258 | 290 |
| 259 } // namespace content | 291 } // namespace content |
| OLD | NEW |