| 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/file_util.h" | 16 #include "base/file_util.h" |
| 16 #include "base/format_macros.h" | 17 #include "base/format_macros.h" |
| 17 #include "base/json/json_reader.h" | 18 #include "base/json/json_reader.h" |
| 18 #include "base/json/json_writer.h" | 19 #include "base/json/json_writer.h" |
| 19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/string_split.h" | 22 #include "base/strings/string_split.h" |
| 22 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 23 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
| 24 #include "base/values.h" | 25 #include "base/values.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 47 it != categorySet.end(); it++) { | 48 it != categorySet.end(); it++) { |
| 48 category_list->AppendString(*it); | 49 category_list->AppendString(*it); |
| 49 } | 50 } |
| 50 | 51 |
| 51 base::RefCountedString* res = new base::RefCountedString(); | 52 base::RefCountedString* res = new base::RefCountedString(); |
| 52 base::JSONWriter::Write(category_list.get(), &res->data()); | 53 base::JSONWriter::Write(category_list.get(), &res->data()); |
| 53 callback.Run(res); | 54 callback.Run(res); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool GetTracingOptions(const std::string& data64, | 57 bool GetTracingOptions(const std::string& data64, |
| 57 std::string* category_filter_string, | 58 base::debug::CategoryFilter* category_filter, |
| 58 int* tracing_options) { | 59 base::debug::TraceOptions* tracing_options) { |
| 59 std::string data; | 60 std::string data; |
| 60 if (!base::Base64Decode(data64, &data)) { | 61 if (!base::Base64Decode(data64, &data)) { |
| 61 LOG(ERROR) << "Options were not base64 encoded."; | 62 LOG(ERROR) << "Options were not base64 encoded."; |
| 62 return false; | 63 return false; |
| 63 } | 64 } |
| 64 | 65 |
| 65 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); | 66 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); |
| 66 if (!optionsRaw) { | 67 if (!optionsRaw) { |
| 67 LOG(ERROR) << "Options were not valid JSON"; | 68 LOG(ERROR) << "Options were not valid JSON"; |
| 68 return false; | 69 return false; |
| 69 } | 70 } |
| 70 base::DictionaryValue* options; | 71 base::DictionaryValue* options; |
| 71 if (!optionsRaw->GetAsDictionary(&options)) { | 72 if (!optionsRaw->GetAsDictionary(&options)) { |
| 72 LOG(ERROR) << "Options must be dict"; | 73 LOG(ERROR) << "Options must be dict"; |
| 73 return false; | 74 return false; |
| 74 } | 75 } |
| 75 | 76 |
| 76 bool use_system_tracing; | 77 if (!category_filter) { |
| 77 bool use_continuous_tracing; | 78 LOG(ERROR) << "category_filter can't be passed as NULL"; |
| 78 bool use_sampling; | 79 return false; |
| 80 } |
| 81 |
| 82 if (!tracing_options) { |
| 83 LOG(ERROR) << "tracing_options can't be passed as NULL"; |
| 84 return false; |
| 85 } |
| 79 | 86 |
| 80 bool options_ok = true; | 87 bool options_ok = true; |
| 81 options_ok &= options->GetString("categoryFilter", category_filter_string); | 88 std::string category_filter_string; |
| 82 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); | 89 options_ok &= options->GetString("categoryFilter", &category_filter_string); |
| 83 options_ok &= options->GetBoolean("useContinuousTracing", | 90 *category_filter = base::debug::CategoryFilter(category_filter_string); |
| 84 &use_continuous_tracing); | 91 |
| 85 options_ok &= options->GetBoolean("useSampling", &use_sampling); | 92 options_ok &= options->GetBoolean("useSystemTracing", |
| 93 &tracing_options->enable_systrace); |
| 94 options_ok &= |
| 95 options->GetBoolean("useSampling", &tracing_options->enable_sampling); |
| 96 |
| 97 bool use_continuous_tracing; |
| 98 options_ok &= |
| 99 options->GetBoolean("useContinuousTracing", &use_continuous_tracing); |
| 100 |
| 101 if (use_continuous_tracing) |
| 102 tracing_options->record_mode = |
| 103 base::debug::RECORD_CONTINUOUSLY; |
| 104 else |
| 105 tracing_options->record_mode = base::debug::RECORD_UNTIL_FULL; |
| 106 |
| 86 if (!options_ok) { | 107 if (!options_ok) { |
| 87 LOG(ERROR) << "Malformed options"; | 108 LOG(ERROR) << "Malformed options"; |
| 88 return false; | 109 return false; |
| 89 } | 110 } |
| 90 | |
| 91 *tracing_options = 0; | |
| 92 if (use_system_tracing) | |
| 93 *tracing_options |= TracingController::ENABLE_SYSTRACE; | |
| 94 if (use_sampling) | |
| 95 *tracing_options |= TracingController::ENABLE_SAMPLING; | |
| 96 if (use_continuous_tracing) | |
| 97 *tracing_options |= TracingController::RECORD_CONTINUOUSLY; | |
| 98 return true; | 111 return true; |
| 99 } | 112 } |
| 100 | 113 |
| 101 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 114 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 102 | 115 |
| 103 bool BeginRecording(const std::string& data64, | 116 bool BeginRecording(const std::string& data64, |
| 104 const WebUIDataSource::GotDataCallback& callback) { | 117 const WebUIDataSource::GotDataCallback& callback) { |
| 105 std::string category_filter_string; | 118 base::debug::CategoryFilter category_filter(""); |
| 106 int tracing_options = 0; | 119 base::debug::TraceOptions tracing_options; |
| 107 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | 120 if (!GetTracingOptions(data64, &category_filter, &tracing_options)) |
| 108 return false; | 121 return false; |
| 109 | 122 |
| 110 return TracingController::GetInstance()->EnableRecording( | 123 return TracingController::GetInstance()->EnableRecording( |
| 111 category_filter_string, | 124 category_filter, |
| 112 static_cast<TracingController::Options>(tracing_options), | 125 tracing_options, |
| 113 base::Bind(&OnRecordingEnabledAck, callback)); | 126 base::Bind(&OnRecordingEnabledAck, callback)); |
| 114 } | 127 } |
| 115 | 128 |
| 116 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | 129 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { |
| 117 base::RefCountedString* res = new base::RefCountedString(); | 130 base::RefCountedString* res = new base::RefCountedString(); |
| 118 callback.Run(res); | 131 callback.Run(res); |
| 119 } | 132 } |
| 120 | 133 |
| 121 void OnTraceBufferPercentFullResult( | 134 void OnTraceBufferPercentFullResult( |
| 122 const WebUIDataSource::GotDataCallback& callback, float result) { | 135 const WebUIDataSource::GotDataCallback& callback, float result) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 138 const base::FilePath& path) { | 151 const base::FilePath& path) { |
| 139 BrowserThread::PostTask( | 152 BrowserThread::PostTask( |
| 140 BrowserThread::FILE, FROM_HERE, | 153 BrowserThread::FILE, FROM_HERE, |
| 141 base::Bind(ReadRecordingResult, callback, path)); | 154 base::Bind(ReadRecordingResult, callback, path)); |
| 142 } | 155 } |
| 143 | 156 |
| 144 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); | 157 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback); |
| 145 | 158 |
| 146 bool EnableMonitoring(const std::string& data64, | 159 bool EnableMonitoring(const std::string& data64, |
| 147 const WebUIDataSource::GotDataCallback& callback) { | 160 const WebUIDataSource::GotDataCallback& callback) { |
| 148 std::string category_filter_string; | 161 base::debug::TraceOptions tracing_options; |
| 149 int tracing_options = 0; | 162 base::debug::CategoryFilter category_filter(""); |
| 150 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options)) | 163 if (!GetTracingOptions(data64, &category_filter, &tracing_options)) |
| 151 return false; | 164 return false; |
| 152 | 165 |
| 153 return TracingController::GetInstance()->EnableMonitoring( | 166 return TracingController::GetInstance()->EnableMonitoring( |
| 154 category_filter_string, | 167 category_filter, |
| 155 static_cast<TracingController::Options>(tracing_options), | 168 tracing_options, |
| 156 base::Bind(OnMonitoringEnabledAck, callback)); | 169 base::Bind(OnMonitoringEnabledAck, callback)); |
| 157 } | 170 } |
| 158 | 171 |
| 159 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { | 172 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) { |
| 160 base::RefCountedString* res = new base::RefCountedString(); | 173 base::RefCountedString* res = new base::RefCountedString(); |
| 161 callback.Run(res); | 174 callback.Run(res); |
| 162 } | 175 } |
| 163 | 176 |
| 164 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { | 177 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) { |
| 165 base::RefCountedString* res = new base::RefCountedString(); | 178 base::RefCountedString* res = new base::RefCountedString(); |
| 166 callback.Run(res); | 179 callback.Run(res); |
| 167 } | 180 } |
| 168 | 181 |
| 169 void GetMonitoringStatus(const WebUIDataSource::GotDataCallback& callback) { | 182 void GetMonitoringStatus(const WebUIDataSource::GotDataCallback& callback) { |
| 170 bool is_monitoring; | 183 bool is_monitoring; |
| 171 std::string category_filter; | 184 base::debug::CategoryFilter category_filter(""); |
| 172 TracingController::Options options; | 185 base::debug::TraceOptions options; |
| 173 TracingController::GetInstance()->GetMonitoringStatus( | 186 TracingController::GetInstance()->GetMonitoringStatus( |
| 174 &is_monitoring, &category_filter, &options); | 187 &is_monitoring, &category_filter, &options); |
| 175 | 188 |
| 176 scoped_ptr<base::DictionaryValue> | 189 scoped_ptr<base::DictionaryValue> |
| 177 monitoring_options(new base::DictionaryValue()); | 190 monitoring_options(new base::DictionaryValue()); |
| 178 monitoring_options->SetBoolean("isMonitoring", is_monitoring); | 191 monitoring_options->SetBoolean("isMonitoring", is_monitoring); |
| 179 monitoring_options->SetString("categoryFilter", category_filter); | 192 monitoring_options->SetString("categoryFilter", category_filter.ToString()); |
| 180 monitoring_options->SetBoolean("useSystemTracing", | 193 monitoring_options->SetBoolean("useSystemTracing", options.enable_systrace); |
| 181 (options & TracingController::ENABLE_SYSTRACE) != 0); | 194 monitoring_options->SetBoolean( |
| 182 monitoring_options->SetBoolean("useContinuousTracing", | 195 "useContinuousTracing", |
| 183 (options & TracingController::RECORD_CONTINUOUSLY) != 0); | 196 options.record_mode == base::debug::RECORD_CONTINUOUSLY); |
| 184 monitoring_options->SetBoolean("useSampling", | 197 monitoring_options->SetBoolean("useSampling", options.enable_sampling); |
| 185 (options & TracingController::ENABLE_SAMPLING) != 0); | |
| 186 | 198 |
| 187 std::string monitoring_options_json; | 199 std::string monitoring_options_json; |
| 188 base::JSONWriter::Write(monitoring_options.get(), &monitoring_options_json); | 200 base::JSONWriter::Write(monitoring_options.get(), &monitoring_options_json); |
| 189 | 201 |
| 190 base::RefCountedString* monitoring_options_base64 = | 202 base::RefCountedString* monitoring_options_base64 = |
| 191 new base::RefCountedString(); | 203 new base::RefCountedString(); |
| 192 base::Base64Encode(monitoring_options_json, | 204 base::Base64Encode(monitoring_options_json, |
| 193 &monitoring_options_base64->data()); | 205 &monitoring_options_base64->data()); |
| 194 callback.Run(monitoring_options_base64); | 206 callback.Run(monitoring_options_base64); |
| 195 } | 207 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 if (success) { | 405 if (success) { |
| 394 web_ui()->CallJavascriptFunction("onUploadComplete", | 406 web_ui()->CallJavascriptFunction("onUploadComplete", |
| 395 base::StringValue(report_id)); | 407 base::StringValue(report_id)); |
| 396 } else { | 408 } else { |
| 397 web_ui()->CallJavascriptFunction("onUploadError", | 409 web_ui()->CallJavascriptFunction("onUploadError", |
| 398 base::StringValue(error_message)); | 410 base::StringValue(error_message)); |
| 399 } | 411 } |
| 400 } | 412 } |
| 401 | 413 |
| 402 } // namespace content | 414 } // namespace content |
| OLD | NEW |