Chromium Code Reviews| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | |
| 9 #include "base/location.h" | 10 #include "base/location.h" |
| 10 #include "base/strings/string_split.h" | |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/devtools/devtools_http_handler_impl.h" | 13 #include "content/browser/devtools/devtools_http_handler_impl.h" |
| 14 #include "content/browser/devtools/devtools_protocol_constants.h" | 14 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 15 #include "content/public/browser/trace_controller.h" | 15 #include "content/browser/tracing/tracing_controller_impl.h" |
| 16 #include "content/public/browser/trace_subscriber.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 | 19 |
| 20 namespace { | |
| 21 | |
| 22 const char kRecordUntilFull[] = "record-until-full"; | |
| 23 const char kRecordContinuously[] = "record-continuously"; | |
| 24 const char kEnableSampling[] = "enable-sampling"; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 DevToolsTracingHandler::DevToolsTracingHandler() | 20 DevToolsTracingHandler::DevToolsTracingHandler() |
| 29 : is_running_(false) { | 21 : is_running_(false) { |
| 30 RegisterCommandHandler(devtools::Tracing::start::kName, | 22 RegisterCommandHandler(devtools::Tracing::start::kName, |
| 31 base::Bind(&DevToolsTracingHandler::OnStart, | 23 base::Bind(&DevToolsTracingHandler::OnStart, |
| 32 base::Unretained(this))); | 24 base::Unretained(this))); |
| 33 RegisterCommandHandler(devtools::Tracing::end::kName, | 25 RegisterCommandHandler(devtools::Tracing::end::kName, |
| 34 base::Bind(&DevToolsTracingHandler::OnEnd, | 26 base::Bind(&DevToolsTracingHandler::OnEnd, |
| 35 base::Unretained(this))); | 27 base::Unretained(this))); |
| 36 } | 28 } |
| 37 | 29 |
| 38 DevToolsTracingHandler::~DevToolsTracingHandler() { | 30 DevToolsTracingHandler::~DevToolsTracingHandler() { |
| 39 } | 31 } |
| 40 | 32 |
| 41 void DevToolsTracingHandler::OnEndTracingComplete() { | 33 void DevToolsTracingHandler::ReadRecordingResult(const base::FilePath& path) { |
| 34 if (!is_running_) | |
| 35 return; | |
| 36 | |
| 42 is_running_ = false; | 37 is_running_ = false; |
| 43 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); | 38 |
| 39 // Hand-craft protocol notification message so we can substitute JSON | |
| 40 // that we already got as string as a bare object, not a quoted string. | |
| 41 std::string message = base::StringPrintf( | |
| 42 "{ \"method\": \"%s\", \"params\": { \"%s\": ", | |
| 43 devtools::Tracing::dataCollected::kName, | |
| 44 devtools::Tracing::dataCollected::kValue); | |
| 45 if (!base::ReadFileToString(path, &message)) | |
| 46 LOG(ERROR) << "Failed to read file: " << path.value(); | |
| 47 base::DeleteFile(path, false); | |
| 48 message.append("} }"); | |
| 49 SendRawMessage(message); | |
| 44 } | 50 } |
| 45 | 51 |
| 46 void DevToolsTracingHandler::OnTraceDataCollected( | 52 void DevToolsTracingHandler::BeginReadingRecordingResult( |
| 47 const scoped_refptr<base::RefCountedString>& trace_fragment) { | 53 const base::FilePath& path) { |
| 48 if (is_running_) { | 54 BrowserThread::PostTask( |
| 49 // Hand-craft protocol notification message so we can substitute JSON | 55 BrowserThread::FILE, FROM_HERE, |
| 50 // that we already got as string as a bare object, not a quoted string. | 56 base::Bind(&DevToolsTracingHandler::ReadRecordingResult, |
| 51 std::string message = base::StringPrintf( | 57 base::Unretained(this), path)); |
|
piman
2013/11/15 23:42:52
What makes Unretained here safe?
Xianzhu
2013/11/16 01:16:14
Used this because I saw existing Unretained(this)
| |
| 52 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", | |
| 53 devtools::Tracing::dataCollected::kName, | |
| 54 devtools::Tracing::dataCollected::kValue, | |
| 55 trace_fragment->data().c_str()); | |
| 56 SendRawMessage(message); | |
| 57 } | |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Note, if you add more options here you also need to update: | 60 void DevToolsTracingHandler::OnTraceBufferPercentFullResult( |
| 61 // base/debug/trace_event_impl:TraceOptionsFromString | 61 float percent_full) { |
| 62 base::debug::TraceLog::Options DevToolsTracingHandler::TraceOptionsFromString( | 62 base::DictionaryValue* params = new base::DictionaryValue(); |
| 63 const std::string& options) { | 63 params->SetDouble(devtools::Tracing::traceBufferPercentFull::kValue, |
| 64 std::vector<std::string> split; | 64 percent_full); |
| 65 std::vector<std::string>::iterator iter; | 65 SendNotification(devtools::Tracing::traceBufferPercentFull::kName, params); |
| 66 int ret = 0; | |
| 67 | |
| 68 base::SplitString(options, ',', &split); | |
| 69 for (iter = split.begin(); iter != split.end(); ++iter) { | |
| 70 if (*iter == kRecordUntilFull) { | |
| 71 ret |= base::debug::TraceLog::RECORD_UNTIL_FULL; | |
| 72 } else if (*iter == kRecordContinuously) { | |
| 73 ret |= base::debug::TraceLog::RECORD_CONTINUOUSLY; | |
| 74 } else if (*iter == kEnableSampling) { | |
| 75 ret |= base::debug::TraceLog::ENABLE_SAMPLING; | |
| 76 } | |
| 77 } | |
| 78 if (!(ret & base::debug::TraceLog::RECORD_UNTIL_FULL) && | |
| 79 !(ret & base::debug::TraceLog::RECORD_CONTINUOUSLY)) | |
| 80 ret |= base::debug::TraceLog::RECORD_UNTIL_FULL; | |
| 81 | |
| 82 return static_cast<base::debug::TraceLog::Options>(ret); | |
| 83 } | 66 } |
| 84 | 67 |
| 85 scoped_refptr<DevToolsProtocol::Response> | 68 scoped_refptr<DevToolsProtocol::Response> |
| 86 DevToolsTracingHandler::OnStart( | 69 DevToolsTracingHandler::OnStart( |
| 87 scoped_refptr<DevToolsProtocol::Command> command) { | 70 scoped_refptr<DevToolsProtocol::Command> command) { |
| 88 std::string categories; | 71 std::string category_filter; |
| 89 base::DictionaryValue* params = command->params(); | 72 TracingController::Options options; |
| 90 if (params) | 73 if (!TracingControllerImpl::ParseTracingParams(command->params(), |
| 91 params->GetString(devtools::Tracing::start::kCategories, &categories); | 74 &category_filter, &options)) { |
| 92 | 75 return command->InternalErrorResponse("Malformed params"); |
| 93 base::debug::TraceLog::Options options = | |
| 94 base::debug::TraceLog::RECORD_UNTIL_FULL; | |
| 95 if (params && params->HasKey(devtools::Tracing::start::kTraceOptions)) { | |
| 96 std::string options_param; | |
| 97 params->GetString(devtools::Tracing::start::kTraceOptions, &options_param); | |
| 98 options = TraceOptionsFromString(options_param); | |
| 99 } | 76 } |
| 100 | 77 |
| 101 TraceController::GetInstance()->BeginTracing(this, categories, options); | 78 if (TracingController::GetInstance()->EnableRecording( |
| 102 is_running_ = true; | 79 category_filter, options, |
| 80 TracingController::EnableRecordingDoneCallback())) { | |
| 81 is_running_ = true; | |
| 82 return command->SuccessResponse(NULL); | |
| 83 } | |
| 84 return command->InternalErrorResponse("Failed to start tracing"); | |
| 85 } | |
| 86 | |
| 87 scoped_refptr<DevToolsProtocol::Response> | |
| 88 DevToolsTracingHandler::OnGetTraceBufferPercentFull( | |
| 89 scoped_refptr<DevToolsProtocol::Command> command) { | |
| 90 TracingController::GetInstance()->GetTraceBufferPercentFull( | |
| 91 base::Bind(&DevToolsTracingHandler::OnTraceBufferPercentFullResult, | |
| 92 base::Unretained(this))); | |
|
piman
2013/11/15 23:42:52
Same here, what makes Unretained safe?
Xianzhu
2013/11/16 01:16:14
Done.
| |
| 103 return command->SuccessResponse(NULL); | 93 return command->SuccessResponse(NULL); |
| 104 } | 94 } |
| 105 | 95 |
| 106 scoped_refptr<DevToolsProtocol::Response> | 96 scoped_refptr<DevToolsProtocol::Response> |
| 107 DevToolsTracingHandler::OnEnd( | 97 DevToolsTracingHandler::OnEnd( |
| 108 scoped_refptr<DevToolsProtocol::Command> command) { | 98 scoped_refptr<DevToolsProtocol::Command> command) { |
| 109 TraceController::GetInstance()->EndTracingAsync(this); | 99 TracingController::GetInstance()->DisableRecording( |
| 100 base::FilePath(), | |
| 101 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
| 102 base::Unretained(this))); | |
|
piman
2013/11/15 23:42:52
And here.
Xianzhu
2013/11/16 01:16:14
Done.
| |
| 110 return command->SuccessResponse(NULL); | 103 return command->SuccessResponse(NULL); |
| 111 } | 104 } |
| 112 | 105 |
| 113 } // namespace content | 106 } // namespace content |
| OLD | NEW |