| 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/devtools/devtools_tracing_handler.h" | 5 #include "content/browser/devtools/devtools_tracing_handler.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | |
| 11 #include "base/debug/trace_event_impl.h" | 10 #include "base/debug/trace_event_impl.h" |
| 12 #include "base/file_util.h" | |
| 13 #include "base/json/json_reader.h" | |
| 14 #include "base/json/json_writer.h" | |
| 15 #include "base/location.h" | |
| 16 #include "base/memory/ref_counted_memory.h" | |
| 17 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 18 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 19 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 20 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 21 #include "base/values.h" | 15 #include "base/values.h" |
| 22 #include "content/browser/devtools/devtools_http_handler_impl.h" | 16 #include "content/browser/devtools/devtools_http_handler_impl.h" |
| 23 #include "content/browser/devtools/devtools_protocol_constants.h" | 17 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 24 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/tracing_controller.h" | 19 #include "content/public/browser/tracing_controller.h" |
| 26 | 20 |
| 27 namespace content { | 21 namespace content { |
| 28 | 22 |
| 29 namespace { | 23 namespace { |
| 30 | 24 |
| 31 const char kRecordUntilFull[] = "record-until-full"; | 25 const char kRecordUntilFull[] = "record-until-full"; |
| 32 const char kRecordContinuously[] = "record-continuously"; | 26 const char kRecordContinuously[] = "record-continuously"; |
| 33 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; | 27 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; |
| 34 const char kEnableSampling[] = "enable-sampling"; | 28 const char kEnableSampling[] = "enable-sampling"; |
| 35 | 29 |
| 36 void ReadFile( | 30 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { |
| 37 const base::FilePath& path, | 31 public: |
| 38 const base::Callback<void(const scoped_refptr<base::RefCountedString>&)> | 32 explicit DevToolsTraceSinkProxy(base::WeakPtr<DevToolsTracingHandler> handler) |
| 39 callback) { | 33 : tracing_handler_(handler) { |
| 40 std::string trace_data; | 34 } |
| 41 if (!base::ReadFileToString(path, &trace_data)) | 35 |
| 42 LOG(ERROR) << "Failed to read file: " << path.value(); | 36 virtual void AddTraceChunk(const std::string& chunk) OVERRIDE { |
| 43 base::DeleteFile(path, false); | 37 if (DevToolsTracingHandler* h = tracing_handler_.get()) |
| 44 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 38 h->OnTraceDataCollected(chunk); |
| 45 base::Bind(callback, make_scoped_refptr( | 39 } |
| 46 base::RefCountedString::TakeString(&trace_data)))); | 40 virtual void Close() OVERRIDE { |
| 47 } | 41 if (DevToolsTracingHandler* h = tracing_handler_.get()) |
| 42 h->OnTraceComplete(); |
| 43 delete this; |
| 44 } |
| 45 |
| 46 private: |
| 47 virtual ~DevToolsTraceSinkProxy() {} |
| 48 |
| 49 base::WeakPtr<DevToolsTracingHandler> tracing_handler_; |
| 50 }; |
| 48 | 51 |
| 49 } // namespace | 52 } // namespace |
| 50 | 53 |
| 51 const char* DevToolsTracingHandler::kDefaultCategories = | 54 const char* DevToolsTracingHandler::kDefaultCategories = |
| 52 "-*,disabled-by-default-devtools.timeline*"; | 55 "-*,disabled-by-default-devtools.timeline*"; |
| 53 const double DevToolsTracingHandler::kDefaultReportingInterval = 1000.0; | 56 const double DevToolsTracingHandler::kDefaultReportingInterval = 1000.0; |
| 54 const double DevToolsTracingHandler::kMinimumReportingInterval = 250.0; | 57 const double DevToolsTracingHandler::kMinimumReportingInterval = 250.0; |
| 55 | 58 |
| 56 DevToolsTracingHandler::DevToolsTracingHandler( | 59 DevToolsTracingHandler::DevToolsTracingHandler( |
| 57 DevToolsTracingHandler::Target target) | 60 DevToolsTracingHandler::Target target) |
| 58 : weak_factory_(this), target_(target), is_recording_(false) { | 61 : weak_factory_(this), target_(target), is_recording_(false) { |
| 59 RegisterCommandHandler(devtools::Tracing::start::kName, | 62 RegisterCommandHandler(devtools::Tracing::start::kName, |
| 60 base::Bind(&DevToolsTracingHandler::OnStart, | 63 base::Bind(&DevToolsTracingHandler::OnStart, |
| 61 base::Unretained(this))); | 64 base::Unretained(this))); |
| 62 RegisterCommandHandler(devtools::Tracing::end::kName, | 65 RegisterCommandHandler(devtools::Tracing::end::kName, |
| 63 base::Bind(&DevToolsTracingHandler::OnEnd, | 66 base::Bind(&DevToolsTracingHandler::OnEnd, |
| 64 base::Unretained(this))); | 67 base::Unretained(this))); |
| 65 RegisterCommandHandler(devtools::Tracing::getCategories::kName, | 68 RegisterCommandHandler(devtools::Tracing::getCategories::kName, |
| 66 base::Bind(&DevToolsTracingHandler::OnGetCategories, | 69 base::Bind(&DevToolsTracingHandler::OnGetCategories, |
| 67 base::Unretained(this))); | 70 base::Unretained(this))); |
| 68 } | 71 } |
| 69 | 72 |
| 70 DevToolsTracingHandler::~DevToolsTracingHandler() { | 73 DevToolsTracingHandler::~DevToolsTracingHandler() { |
| 71 } | 74 } |
| 72 | 75 |
| 73 void DevToolsTracingHandler::BeginReadingRecordingResult( | |
| 74 const base::FilePath& path) { | |
| 75 BrowserThread::PostTask( | |
| 76 BrowserThread::FILE, FROM_HERE, | |
| 77 base::Bind(&ReadFile, path, | |
| 78 base::Bind(&DevToolsTracingHandler::ReadRecordingResult, | |
| 79 weak_factory_.GetWeakPtr()))); | |
| 80 } | |
| 81 | |
| 82 void DevToolsTracingHandler::ReadRecordingResult( | |
| 83 const scoped_refptr<base::RefCountedString>& trace_data) { | |
| 84 if (trace_data->data().size()) { | |
| 85 scoped_ptr<base::Value> trace_value(base::JSONReader::Read( | |
| 86 trace_data->data())); | |
| 87 base::DictionaryValue* dictionary = NULL; | |
| 88 bool ok = trace_value->GetAsDictionary(&dictionary); | |
| 89 DCHECK(ok); | |
| 90 base::ListValue* list = NULL; | |
| 91 ok = dictionary->GetList("traceEvents", &list); | |
| 92 DCHECK(ok); | |
| 93 std::string buffer; | |
| 94 for (size_t i = 0; i < list->GetSize(); ++i) { | |
| 95 std::string item; | |
| 96 base::Value* item_value; | |
| 97 list->Get(i, &item_value); | |
| 98 base::JSONWriter::Write(item_value, &item); | |
| 99 if (buffer.size()) | |
| 100 buffer.append(","); | |
| 101 buffer.append(item); | |
| 102 const size_t kMessageSizeThreshold = 1024 * 1024; | |
| 103 if (buffer.size() > kMessageSizeThreshold) { | |
| 104 OnTraceDataCollected(buffer); | |
| 105 buffer.clear(); | |
| 106 } | |
| 107 } | |
| 108 if (buffer.size()) | |
| 109 OnTraceDataCollected(buffer); | |
| 110 } | |
| 111 | |
| 112 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); | |
| 113 } | |
| 114 | |
| 115 void DevToolsTracingHandler::OnTraceDataCollected( | 76 void DevToolsTracingHandler::OnTraceDataCollected( |
| 116 const std::string& trace_fragment) { | 77 const std::string& trace_fragment) { |
| 117 // Hand-craft protocol notification message so we can substitute JSON | 78 // Hand-craft protocol notification message so we can substitute JSON |
| 118 // that we already got as string as a bare object, not a quoted string. | 79 // that we already got as string as a bare object, not a quoted string. |
| 119 std::string message = base::StringPrintf( | 80 std::string message = base::StringPrintf( |
| 120 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", | 81 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", |
| 121 devtools::Tracing::dataCollected::kName, | 82 devtools::Tracing::dataCollected::kName, |
| 122 devtools::Tracing::dataCollected::kParamValue, | 83 devtools::Tracing::dataCollected::kParamValue, |
| 123 trace_fragment.c_str()); | 84 trace_fragment.c_str()); |
| 124 SendRawMessage(message); | 85 SendRawMessage(message); |
| 125 } | 86 } |
| 126 | 87 |
| 88 void DevToolsTracingHandler::OnTraceComplete() { |
| 89 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); |
| 90 } |
| 91 |
| 127 base::debug::TraceOptions DevToolsTracingHandler::TraceOptionsFromString( | 92 base::debug::TraceOptions DevToolsTracingHandler::TraceOptionsFromString( |
| 128 const std::string& options) { | 93 const std::string& options) { |
| 129 std::vector<std::string> split; | 94 std::vector<std::string> split; |
| 130 std::vector<std::string>::iterator iter; | 95 std::vector<std::string>::iterator iter; |
| 131 base::debug::TraceOptions ret; | 96 base::debug::TraceOptions ret; |
| 132 | 97 |
| 133 base::SplitString(options, ',', &split); | 98 base::SplitString(options, ',', &split); |
| 134 for (iter = split.begin(); iter != split.end(); ++iter) { | 99 for (iter = split.begin(); iter != split.end(); ++iter) { |
| 135 if (*iter == kRecordUntilFull) { | 100 if (*iter == kRecordUntilFull) { |
| 136 ret.record_mode = base::debug::RECORD_UNTIL_FULL; | 101 ret.record_mode = base::debug::RECORD_UNTIL_FULL; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 SendNotification(devtools::Tracing::bufferUsage::kName, params); | 179 SendNotification(devtools::Tracing::bufferUsage::kName, params); |
| 215 } | 180 } |
| 216 | 181 |
| 217 scoped_refptr<DevToolsProtocol::Response> | 182 scoped_refptr<DevToolsProtocol::Response> |
| 218 DevToolsTracingHandler::OnEnd( | 183 DevToolsTracingHandler::OnEnd( |
| 219 scoped_refptr<DevToolsProtocol::Command> command) { | 184 scoped_refptr<DevToolsProtocol::Command> command) { |
| 220 // If inspected target is a render process Tracing.end will be handled by | 185 // If inspected target is a render process Tracing.end will be handled by |
| 221 // tracing agent in the renderer. | 186 // tracing agent in the renderer. |
| 222 if (target_ == Renderer) | 187 if (target_ == Renderer) |
| 223 return NULL; | 188 return NULL; |
| 224 DisableRecording( | 189 DisableRecording(false); |
| 225 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
| 226 weak_factory_.GetWeakPtr())); | |
| 227 return command->SuccessResponse(NULL); | 190 return command->SuccessResponse(NULL); |
| 228 } | 191 } |
| 229 | 192 |
| 230 void DevToolsTracingHandler::DisableRecording( | 193 void DevToolsTracingHandler::DisableRecording(bool abort) { |
| 231 const TracingController::TracingFileResultCallback& callback) { | |
| 232 is_recording_ = false; | 194 is_recording_ = false; |
| 233 buffer_usage_poll_timer_.reset(); | 195 buffer_usage_poll_timer_.reset(); |
| 234 TracingController::GetInstance()->DisableRecording(base::FilePath(), | 196 TracingController::GetInstance()->DisableRecording( |
| 235 callback); | 197 abort ? NULL : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr())); |
| 236 } | 198 } |
| 237 | 199 |
| 238 void DevToolsTracingHandler::OnClientDetached() { | 200 void DevToolsTracingHandler::OnClientDetached() { |
| 239 if (is_recording_) | 201 if (is_recording_) |
| 240 DisableRecording(); | 202 DisableRecording(true); |
| 241 } | 203 } |
| 242 | 204 |
| 243 scoped_refptr<DevToolsProtocol::Response> | 205 scoped_refptr<DevToolsProtocol::Response> |
| 244 DevToolsTracingHandler::OnGetCategories( | 206 DevToolsTracingHandler::OnGetCategories( |
| 245 scoped_refptr<DevToolsProtocol::Command> command) { | 207 scoped_refptr<DevToolsProtocol::Command> command) { |
| 246 TracingController::GetInstance()->GetCategories( | 208 TracingController::GetInstance()->GetCategories( |
| 247 base::Bind(&DevToolsTracingHandler::OnCategoriesReceived, | 209 base::Bind(&DevToolsTracingHandler::OnCategoriesReceived, |
| 248 weak_factory_.GetWeakPtr(), | 210 weak_factory_.GetWeakPtr(), |
| 249 command)); | 211 command)); |
| 250 return command->AsyncResponsePromise(); | 212 return command->AsyncResponsePromise(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 276 base::debug::CategoryFilter(category_filter), | 238 base::debug::CategoryFilter(category_filter), |
| 277 base::debug::TraceOptions(), | 239 base::debug::TraceOptions(), |
| 278 TracingController::EnableRecordingDoneCallback()); | 240 TracingController::EnableRecordingDoneCallback()); |
| 279 SendNotification(devtools::Tracing::started::kName, NULL); | 241 SendNotification(devtools::Tracing::started::kName, NULL); |
| 280 } | 242 } |
| 281 | 243 |
| 282 void DevToolsTracingHandler::DisableTracing() { | 244 void DevToolsTracingHandler::DisableTracing() { |
| 283 if (!is_recording_) | 245 if (!is_recording_) |
| 284 return; | 246 return; |
| 285 is_recording_ = false; | 247 is_recording_ = false; |
| 286 DisableRecording( | 248 DisableRecording(false); |
| 287 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
| 288 weak_factory_.GetWeakPtr())); | |
| 289 } | 249 } |
| 290 | 250 |
| 291 | 251 |
| 292 } // namespace content | 252 } // namespace content |
| OLD | NEW |