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), |
22 weak_factory_(this) { | |
30 RegisterCommandHandler(devtools::Tracing::start::kName, | 23 RegisterCommandHandler(devtools::Tracing::start::kName, |
31 base::Bind(&DevToolsTracingHandler::OnStart, | 24 base::Bind(&DevToolsTracingHandler::OnStart, |
32 base::Unretained(this))); | 25 base::Unretained(this))); |
33 RegisterCommandHandler(devtools::Tracing::end::kName, | 26 RegisterCommandHandler(devtools::Tracing::end::kName, |
34 base::Bind(&DevToolsTracingHandler::OnEnd, | 27 base::Bind(&DevToolsTracingHandler::OnEnd, |
35 base::Unretained(this))); | 28 base::Unretained(this))); |
36 } | 29 } |
37 | 30 |
38 DevToolsTracingHandler::~DevToolsTracingHandler() { | 31 DevToolsTracingHandler::~DevToolsTracingHandler() { |
39 } | 32 } |
40 | 33 |
41 void DevToolsTracingHandler::OnEndTracingComplete() { | 34 void DevToolsTracingHandler::ReadRecordingResult(const base::FilePath& path) { |
35 if (!is_running_) | |
36 return; | |
37 | |
42 is_running_ = false; | 38 is_running_ = false; |
43 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); | 39 |
40 // Hand-craft protocol notification message so we can substitute JSON | |
41 // that we already got as string as a bare object, not a quoted string. | |
42 std::string message = base::StringPrintf( | |
43 "{ \"method\": \"%s\", \"params\": { \"%s\": ", | |
44 devtools::Tracing::dataCollected::kName, | |
45 devtools::Tracing::dataCollected::kValue); | |
46 if (!base::ReadFileToString(path, &message)) | |
47 LOG(ERROR) << "Failed to read file: " << path.value(); | |
48 base::DeleteFile(path, false); | |
49 message.append("} }"); | |
50 SendRawMessage(message); | |
44 } | 51 } |
45 | 52 |
46 void DevToolsTracingHandler::OnTraceDataCollected( | 53 void DevToolsTracingHandler::BeginReadingRecordingResult( |
47 const scoped_refptr<base::RefCountedString>& trace_fragment) { | 54 const base::FilePath& path) { |
48 if (is_running_) { | 55 BrowserThread::PostTask( |
49 // Hand-craft protocol notification message so we can substitute JSON | 56 BrowserThread::FILE, FROM_HERE, |
50 // that we already got as string as a bare object, not a quoted string. | 57 base::Bind(&DevToolsTracingHandler::ReadRecordingResult, |
51 std::string message = base::StringPrintf( | 58 weak_factory_.GetWeakPtr(), path)); |
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 } | 59 } |
59 | 60 |
60 // Note, if you add more options here you also need to update: | 61 void DevToolsTracingHandler::OnTraceBufferPercentFullResult( |
61 // base/debug/trace_event_impl:TraceOptionsFromString | 62 float percent_full) { |
62 base::debug::TraceLog::Options DevToolsTracingHandler::TraceOptionsFromString( | 63 base::DictionaryValue* params = new base::DictionaryValue(); |
63 const std::string& options) { | 64 params->SetDouble(devtools::Tracing::traceBufferPercentFull::kValue, |
64 std::vector<std::string> split; | 65 percent_full); |
65 std::vector<std::string>::iterator iter; | 66 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 } | 67 } |
84 | 68 |
85 scoped_refptr<DevToolsProtocol::Response> | 69 scoped_refptr<DevToolsProtocol::Response> |
86 DevToolsTracingHandler::OnStart( | 70 DevToolsTracingHandler::OnStart( |
87 scoped_refptr<DevToolsProtocol::Command> command) { | 71 scoped_refptr<DevToolsProtocol::Command> command) { |
88 std::string categories; | 72 std::string category_filter; |
89 base::DictionaryValue* params = command->params(); | 73 TracingController::Options options; |
90 if (params) | 74 if (!TracingControllerImpl::ParseTracingParams(command->params(), |
pfeldman
2013/11/16 13:11:20
You should declare complete scheme of this command
| |
91 params->GetString(devtools::Tracing::start::kCategories, &categories); | 75 &category_filter, &options)) { |
92 | 76 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 } | 77 } |
100 | 78 |
101 TraceController::GetInstance()->BeginTracing(this, categories, options); | 79 if (TracingController::GetInstance()->EnableRecording( |
102 is_running_ = true; | 80 category_filter, options, |
81 TracingController::EnableRecordingDoneCallback())) { | |
82 is_running_ = true; | |
83 return command->SuccessResponse(NULL); | |
84 } | |
85 return command->InternalErrorResponse("Failed to start tracing"); | |
86 } | |
87 | |
88 scoped_refptr<DevToolsProtocol::Response> | |
89 DevToolsTracingHandler::OnGetTraceBufferPercentFull( | |
90 scoped_refptr<DevToolsProtocol::Command> command) { | |
91 TracingController::GetInstance()->GetTraceBufferPercentFull( | |
92 base::Bind(&DevToolsTracingHandler::OnTraceBufferPercentFullResult, | |
93 weak_factory_.GetWeakPtr())); | |
103 return command->SuccessResponse(NULL); | 94 return command->SuccessResponse(NULL); |
104 } | 95 } |
105 | 96 |
106 scoped_refptr<DevToolsProtocol::Response> | 97 scoped_refptr<DevToolsProtocol::Response> |
107 DevToolsTracingHandler::OnEnd( | 98 DevToolsTracingHandler::OnEnd( |
108 scoped_refptr<DevToolsProtocol::Command> command) { | 99 scoped_refptr<DevToolsProtocol::Command> command) { |
109 TraceController::GetInstance()->EndTracingAsync(this); | 100 TracingController::GetInstance()->DisableRecording( |
101 base::FilePath(), | |
102 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
103 weak_factory_.GetWeakPtr())); | |
110 return command->SuccessResponse(NULL); | 104 return command->SuccessResponse(NULL); |
111 } | 105 } |
112 | 106 |
113 } // namespace content | 107 } // namespace content |
OLD | NEW |