| 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 <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/base64.h" | 11 #include "base/base64.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/debug/trace_event.h" | 15 #include "base/debug/trace_event.h" |
| 16 #include "base/file_util.h" | |
| 17 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
| 18 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
| 19 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
| 20 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 23 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 24 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 25 #include "base/values.h" | 24 #include "base/values.h" |
| 26 #include "content/browser/tracing/grit/tracing_resources.h" | 25 #include "content/browser/tracing/grit/tracing_resources.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 base::RefCountedString* res = new base::RefCountedString(); | 128 base::RefCountedString* res = new base::RefCountedString(); |
| 130 callback.Run(res); | 129 callback.Run(res); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void OnTraceBufferPercentFullResult( | 132 void OnTraceBufferPercentFullResult( |
| 134 const WebUIDataSource::GotDataCallback& callback, float result) { | 133 const WebUIDataSource::GotDataCallback& callback, float result) { |
| 135 std::string str = base::DoubleToString(result); | 134 std::string str = base::DoubleToString(result); |
| 136 callback.Run(base::RefCountedString::TakeString(&str)); | 135 callback.Run(base::RefCountedString::TakeString(&str)); |
| 137 } | 136 } |
| 138 | 137 |
| 139 void ReadRecordingResult(const WebUIDataSource::GotDataCallback& callback, | |
| 140 const base::FilePath& path) { | |
| 141 std::string tmp; | |
| 142 if (!base::ReadFileToString(path, &tmp)) | |
| 143 LOG(ERROR) << "Failed to read file " << path.value(); | |
| 144 base::DeleteFile(path, false); | |
| 145 callback.Run(base::RefCountedString::TakeString(&tmp)); | |
| 146 } | |
| 147 | |
| 148 void BeginReadingRecordingResult( | |
| 149 const WebUIDataSource::GotDataCallback& callback, | |
| 150 const base::FilePath& path) { | |
| 151 BrowserThread::PostTask( | |
| 152 BrowserThread::FILE, FROM_HERE, | |
| 153 base::Bind(ReadRecordingResult, callback, path)); | |
| 154 } | |
| 155 | |
| 156 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 138 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 157 | 139 |
| 158 bool EnableMonitoring(const std::string& data64, | 140 bool EnableMonitoring(const std::string& data64, |
| 159 const WebUIDataSource::GotDataCallback& callback) { | 141 const WebUIDataSource::GotDataCallback& callback) { |
| 160 base::debug::TraceOptions tracing_options; | 142 base::debug::TraceOptions tracing_options; |
| 161 base::debug::CategoryFilter category_filter(""); | 143 base::debug::CategoryFilter category_filter(""); |
| 162 if (!GetTracingOptions(data64, &category_filter, &tracing_options)) | 144 if (!GetTracingOptions(data64, &category_filter, &tracing_options)) |
| 163 return false; | 145 return false; |
| 164 | 146 |
| 165 return TracingController::GetInstance()->EnableMonitoring( | 147 return TracingController::GetInstance()->EnableMonitoring( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 std::string monitoring_options_json; | 180 std::string monitoring_options_json; |
| 199 base::JSONWriter::Write(monitoring_options.get(), &monitoring_options_json); | 181 base::JSONWriter::Write(monitoring_options.get(), &monitoring_options_json); |
| 200 | 182 |
| 201 base::RefCountedString* monitoring_options_base64 = | 183 base::RefCountedString* monitoring_options_base64 = |
| 202 new base::RefCountedString(); | 184 new base::RefCountedString(); |
| 203 base::Base64Encode(monitoring_options_json, | 185 base::Base64Encode(monitoring_options_json, |
| 204 &monitoring_options_base64->data()); | 186 &monitoring_options_base64->data()); |
| 205 callback.Run(monitoring_options_base64); | 187 callback.Run(monitoring_options_base64); |
| 206 } | 188 } |
| 207 | 189 |
| 208 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback, | |
| 209 const base::FilePath& path) { | |
| 210 std::string tmp; | |
| 211 if (!base::ReadFileToString(path, &tmp)) | |
| 212 LOG(ERROR) << "Failed to read file " << path.value(); | |
| 213 base::DeleteFile(path, false); | |
| 214 callback.Run(base::RefCountedString::TakeString(&tmp)); | |
| 215 } | |
| 216 | |
| 217 void OnMonitoringSnapshotCaptured( | |
| 218 const WebUIDataSource::GotDataCallback& callback, | |
| 219 const base::FilePath& path) { | |
| 220 BrowserThread::PostTask( | |
| 221 BrowserThread::FILE, FROM_HERE, | |
| 222 base::Bind(ReadMonitoringSnapshot, callback, path)); | |
| 223 } | |
| 224 | |
| 225 bool OnBeginJSONRequest(const std::string& path, | 190 bool OnBeginJSONRequest(const std::string& path, |
| 226 const WebUIDataSource::GotDataCallback& callback) { | 191 const WebUIDataSource::GotDataCallback& callback) { |
| 227 if (path == "json/categories") { | 192 if (path == "json/categories") { |
| 228 return TracingController::GetInstance()->GetCategories( | 193 return TracingController::GetInstance()->GetCategories( |
| 229 base::Bind(OnGotCategories, callback)); | 194 base::Bind(OnGotCategories, callback)); |
| 230 } | 195 } |
| 231 | 196 |
| 232 const char* beginRecordingPath = "json/begin_recording?"; | 197 const char* beginRecordingPath = "json/begin_recording?"; |
| 233 if (StartsWithASCII(path, beginRecordingPath, true)) { | 198 if (StartsWithASCII(path, beginRecordingPath, true)) { |
| 234 std::string data = path.substr(strlen(beginRecordingPath)); | 199 std::string data = path.substr(strlen(beginRecordingPath)); |
| 235 return BeginRecording(data, callback); | 200 return BeginRecording(data, callback); |
| 236 } | 201 } |
| 237 if (path == "json/get_buffer_percent_full") { | 202 if (path == "json/get_buffer_percent_full") { |
| 238 return TracingController::GetInstance()->GetTraceBufferPercentFull( | 203 return TracingController::GetInstance()->GetTraceBufferPercentFull( |
| 239 base::Bind(OnTraceBufferPercentFullResult, callback)); | 204 base::Bind(OnTraceBufferPercentFullResult, callback)); |
| 240 } | 205 } |
| 241 if (path == "json/end_recording") { | 206 if (path == "json/end_recording") { |
| 242 return TracingController::GetInstance()->DisableRecording( | 207 return TracingController::GetInstance()->DisableRecording( |
| 243 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); | 208 TracingControllerImpl::CreateStringSink(callback)); |
| 244 } | 209 } |
| 245 | 210 |
| 246 const char* enableMonitoringPath = "json/begin_monitoring?"; | 211 const char* enableMonitoringPath = "json/begin_monitoring?"; |
| 247 if (path.find(enableMonitoringPath) == 0) { | 212 if (path.find(enableMonitoringPath) == 0) { |
| 248 std::string data = path.substr(strlen(enableMonitoringPath)); | 213 std::string data = path.substr(strlen(enableMonitoringPath)); |
| 249 return EnableMonitoring(data, callback); | 214 return EnableMonitoring(data, callback); |
| 250 } | 215 } |
| 251 if (path == "json/end_monitoring") { | 216 if (path == "json/end_monitoring") { |
| 252 return TracingController::GetInstance()->DisableMonitoring( | 217 return TracingController::GetInstance()->DisableMonitoring( |
| 253 base::Bind(OnMonitoringDisabled, callback)); | 218 base::Bind(OnMonitoringDisabled, callback)); |
| 254 } | 219 } |
| 255 if (path == "json/capture_monitoring") { | 220 if (path == "json/capture_monitoring") { |
| 256 TracingController::GetInstance()->CaptureMonitoringSnapshot( | 221 TracingController::GetInstance()->CaptureMonitoringSnapshot( |
| 257 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback)); | 222 TracingControllerImpl::CreateStringSink(callback)); |
| 258 return true; | 223 return true; |
| 259 } | 224 } |
| 260 if (path == "json/get_monitoring_status") { | 225 if (path == "json/get_monitoring_status") { |
| 261 GetMonitoringStatus(callback); | 226 GetMonitoringStatus(callback); |
| 262 return true; | 227 return true; |
| 263 } | 228 } |
| 264 | 229 |
| 265 LOG(ERROR) << "Unhandled request to " << path; | 230 LOG(ERROR) << "Unhandled request to " << path; |
| 266 return false; | 231 return false; |
| 267 } | 232 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 if (success) { | 370 if (success) { |
| 406 web_ui()->CallJavascriptFunction("onUploadComplete", | 371 web_ui()->CallJavascriptFunction("onUploadComplete", |
| 407 base::StringValue(report_id)); | 372 base::StringValue(report_id)); |
| 408 } else { | 373 } else { |
| 409 web_ui()->CallJavascriptFunction("onUploadError", | 374 web_ui()->CallJavascriptFunction("onUploadError", |
| 410 base::StringValue(error_message)); | 375 base::StringValue(error_message)); |
| 411 } | 376 } |
| 412 } | 377 } |
| 413 | 378 |
| 414 } // namespace content | 379 } // namespace content |
| OLD | NEW |