| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 status.SetInteger("approximateEventCount", approximate_event_count); | 138 status.SetInteger("approximateEventCount", approximate_event_count); |
| 139 | 139 |
| 140 std::string status_json; | 140 std::string status_json; |
| 141 base::JSONWriter::Write(status, &status_json); | 141 base::JSONWriter::Write(status, &status_json); |
| 142 | 142 |
| 143 base::RefCountedString* status_base64 = new base::RefCountedString(); | 143 base::RefCountedString* status_base64 = new base::RefCountedString(); |
| 144 base::Base64Encode(status_json, &status_base64->data()); | 144 base::Base64Encode(status_json, &status_base64->data()); |
| 145 callback.Run(status_base64); | 145 callback.Run(status_base64); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); | |
| 149 | |
| 150 bool StartMonitoring(const std::string& data64, | |
| 151 const WebUIDataSource::GotDataCallback& callback) { | |
| 152 base::trace_event::TraceConfig trace_config("", ""); | |
| 153 if (!GetTracingOptions(data64, &trace_config)) | |
| 154 return false; | |
| 155 | |
| 156 return TracingController::GetInstance()->StartMonitoring( | |
| 157 trace_config, | |
| 158 base::Bind(OnMonitoringEnabledAck, callback)); | |
| 159 } | |
| 160 | |
| 161 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | |
| 162 base::RefCountedString* res = new base::RefCountedString(); | |
| 163 callback.Run(res); | |
| 164 } | |
| 165 | |
| 166 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { | |
| 167 base::RefCountedString* res = new base::RefCountedString(); | |
| 168 callback.Run(res); | |
| 169 } | |
| 170 | |
| 171 void GetMonitoringStatus(const WebUIDataSource::GotDataCallback& callback) { | |
| 172 bool is_monitoring; | |
| 173 base::trace_event::TraceConfig config("", ""); | |
| 174 TracingController::GetInstance()->GetMonitoringStatus( | |
| 175 &is_monitoring, &config); | |
| 176 | |
| 177 base::DictionaryValue monitoring_options; | |
| 178 monitoring_options.SetBoolean("isMonitoring", is_monitoring); | |
| 179 monitoring_options.SetString("categoryFilter", | |
| 180 config.ToCategoryFilterString()); | |
| 181 monitoring_options.SetBoolean("useSystemTracing", config.IsSystraceEnabled()); | |
| 182 monitoring_options.SetBoolean( | |
| 183 "useContinuousTracing", | |
| 184 config.GetTraceRecordMode() == base::trace_event::RECORD_CONTINUOUSLY); | |
| 185 monitoring_options.SetBoolean("useSampling", config.IsSamplingEnabled()); | |
| 186 | |
| 187 std::string monitoring_options_json; | |
| 188 base::JSONWriter::Write(monitoring_options, &monitoring_options_json); | |
| 189 | |
| 190 base::RefCountedString* monitoring_options_base64 = | |
| 191 new base::RefCountedString(); | |
| 192 base::Base64Encode(monitoring_options_json, | |
| 193 &monitoring_options_base64->data()); | |
| 194 callback.Run(monitoring_options_base64); | |
| 195 } | |
| 196 | |
| 197 void TracingCallbackWrapperBase64( | 148 void TracingCallbackWrapperBase64( |
| 198 const WebUIDataSource::GotDataCallback& callback, | 149 const WebUIDataSource::GotDataCallback& callback, |
| 199 scoped_ptr<const base::DictionaryValue> metadata, | 150 scoped_ptr<const base::DictionaryValue> metadata, |
| 200 base::RefCountedString* data) { | 151 base::RefCountedString* data) { |
| 201 base::RefCountedString* data_base64 = new base::RefCountedString(); | 152 base::RefCountedString* data_base64 = new base::RefCountedString(); |
| 202 base::Base64Encode(data->data(), &data_base64->data()); | 153 base::Base64Encode(data->data(), &data_base64->data()); |
| 203 callback.Run(data_base64); | 154 callback.Run(data_base64); |
| 204 } | 155 } |
| 205 | 156 |
| 206 void AddCustomMetadata(TracingControllerImpl::TraceDataSink* trace_data_sink) { | 157 void AddCustomMetadata(TracingControllerImpl::TraceDataSink* trace_data_sink) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 234 } | 185 } |
| 235 if (path == "json/end_recording_compressed") { | 186 if (path == "json/end_recording_compressed") { |
| 236 scoped_refptr<TracingControllerImpl::TraceDataSink> data_sink = | 187 scoped_refptr<TracingControllerImpl::TraceDataSink> data_sink = |
| 237 TracingController::CreateCompressedStringSink( | 188 TracingController::CreateCompressedStringSink( |
| 238 TracingController::CreateCallbackEndpoint( | 189 TracingController::CreateCallbackEndpoint( |
| 239 base::Bind(TracingCallbackWrapperBase64, callback))); | 190 base::Bind(TracingCallbackWrapperBase64, callback))); |
| 240 AddCustomMetadata(data_sink.get()); | 191 AddCustomMetadata(data_sink.get()); |
| 241 return TracingController::GetInstance()->StopTracing(data_sink); | 192 return TracingController::GetInstance()->StopTracing(data_sink); |
| 242 } | 193 } |
| 243 | 194 |
| 244 const char* StartMonitoringPath = "json/begin_monitoring?"; | |
| 245 if (path.find(StartMonitoringPath) == 0) { | |
| 246 std::string data = path.substr(strlen(StartMonitoringPath)); | |
| 247 return StartMonitoring(data, callback); | |
| 248 } | |
| 249 if (path == "json/end_monitoring") { | |
| 250 return TracingController::GetInstance()->StopMonitoring( | |
| 251 base::Bind(OnMonitoringDisabled, callback)); | |
| 252 } | |
| 253 if (path == "json/capture_monitoring_compressed") { | |
| 254 scoped_refptr<TracingControllerImpl::TraceDataSink> data_sink = | |
| 255 TracingController::CreateCompressedStringSink( | |
| 256 TracingController::CreateCallbackEndpoint( | |
| 257 base::Bind(TracingCallbackWrapperBase64, callback))); | |
| 258 AddCustomMetadata(data_sink.get()); | |
| 259 TracingController::GetInstance()->CaptureMonitoringSnapshot(data_sink); | |
| 260 return true; | |
| 261 } | |
| 262 if (path == "json/get_monitoring_status") { | |
| 263 GetMonitoringStatus(callback); | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 LOG(ERROR) << "Unhandled request to " << path; | 195 LOG(ERROR) << "Unhandled request to " << path; |
| 268 return false; | 196 return false; |
| 269 } | 197 } |
| 270 | 198 |
| 271 bool OnTracingRequest(const std::string& path, | 199 bool OnTracingRequest(const std::string& path, |
| 272 const WebUIDataSource::GotDataCallback& callback) { | 200 const WebUIDataSource::GotDataCallback& callback) { |
| 273 if (base::StartsWith(path, "json/", base::CompareCase::SENSITIVE)) { | 201 if (base::StartsWith(path, "json/", base::CompareCase::SENSITIVE)) { |
| 274 if (!OnBeginJSONRequest(path, callback)) { | 202 if (!OnBeginJSONRequest(path, callback)) { |
| 275 std::string error("##ERROR##"); | 203 std::string error("##ERROR##"); |
| 276 callback.Run(base::RefCountedString::TakeString(&error)); | 204 callback.Run(base::RefCountedString::TakeString(&error)); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 source->AddResourcePath("tracing.js", IDR_TRACING_JS); | 238 source->AddResourcePath("tracing.js", IDR_TRACING_JS); |
| 311 source->SetRequestFilter(base::Bind(OnTracingRequest)); | 239 source->SetRequestFilter(base::Bind(OnTracingRequest)); |
| 312 WebUIDataSource::Add(browser_context, source); | 240 WebUIDataSource::Add(browser_context, source); |
| 313 TracingControllerImpl::GetInstance()->RegisterTracingUI(this); | 241 TracingControllerImpl::GetInstance()->RegisterTracingUI(this); |
| 314 } | 242 } |
| 315 | 243 |
| 316 TracingUI::~TracingUI() { | 244 TracingUI::~TracingUI() { |
| 317 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); | 245 TracingControllerImpl::GetInstance()->UnregisterTracingUI(this); |
| 318 } | 246 } |
| 319 | 247 |
| 320 void TracingUI::OnMonitoringStateChanged(bool is_monitoring) { | |
| 321 web_ui()->CallJavascriptFunction( | |
| 322 "onMonitoringStateChanged", base::FundamentalValue(is_monitoring)); | |
| 323 } | |
| 324 | |
| 325 void TracingUI::DoUploadBase64Encoded(const base::ListValue* args) { | 248 void TracingUI::DoUploadBase64Encoded(const base::ListValue* args) { |
| 326 std::string file_contents_base64; | 249 std::string file_contents_base64; |
| 327 if (!args || args->empty() || !args->GetString(0, &file_contents_base64)) { | 250 if (!args || args->empty() || !args->GetString(0, &file_contents_base64)) { |
| 328 web_ui()->CallJavascriptFunction("onUploadError", | 251 web_ui()->CallJavascriptFunction("onUploadError", |
| 329 base::StringValue("Missing data")); | 252 base::StringValue("Missing data")); |
| 330 return; | 253 return; |
| 331 } | 254 } |
| 332 | 255 |
| 333 std::string file_contents; | 256 std::string file_contents; |
| 334 base::Base64Decode(file_contents_base64, &file_contents); | 257 base::Base64Decode(file_contents_base64, &file_contents); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 web_ui()->CallJavascriptFunction("onUploadComplete", | 317 web_ui()->CallJavascriptFunction("onUploadComplete", |
| 395 base::StringValue(feedback)); | 318 base::StringValue(feedback)); |
| 396 } else { | 319 } else { |
| 397 web_ui()->CallJavascriptFunction("onUploadError", | 320 web_ui()->CallJavascriptFunction("onUploadError", |
| 398 base::StringValue(feedback)); | 321 base::StringValue(feedback)); |
| 399 } | 322 } |
| 400 trace_uploader_.reset(); | 323 trace_uploader_.reset(); |
| 401 } | 324 } |
| 402 | 325 |
| 403 } // namespace content | 326 } // namespace content |
| OLD | NEW |