OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/protocol/tracing_handler.h" | 5 #include "content/browser/devtools/protocol/tracing_handler.h" |
6 | 6 |
7 #include <cmath> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/debug/trace_event_impl.h" | |
11 #include "base/strings/string_split.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "base/time/time.h" | |
14 #include "base/timer/timer.h" | |
15 | |
7 namespace content { | 16 namespace content { |
8 namespace devtools { | 17 namespace devtools { |
9 namespace tracing { | 18 namespace tracing { |
10 | 19 |
11 typedef DevToolsProtocolClient::Response Response; | 20 typedef DevToolsProtocolClient::Response Response; |
12 | 21 |
13 TracingHandler::TracingHandler() { | 22 namespace { |
23 | |
24 const char kRecordUntilFull[] = "record-until-full"; | |
25 const char kRecordContinuously[] = "record-continuously"; | |
26 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; | |
27 const char kEnableSampling[] = "enable-sampling"; | |
28 const double kMinimumReportingInterval = 250.0; | |
29 | |
30 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { | |
31 public: | |
32 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler) | |
33 : tracing_handler_(handler) {} | |
34 | |
35 virtual void AddTraceChunk(const std::string& chunk) override { | |
36 if (TracingHandler* h = tracing_handler_.get()) | |
37 h->OnTraceDataCollected(chunk); | |
38 } | |
39 virtual void Close() override { | |
40 if (TracingHandler* h = tracing_handler_.get()) | |
41 h->OnTraceComplete(); | |
42 } | |
43 | |
44 private: | |
45 virtual ~DevToolsTraceSinkProxy() {} | |
46 | |
47 base::WeakPtr<TracingHandler> tracing_handler_; | |
48 }; | |
49 | |
50 } // namespace | |
51 | |
52 TracingHandler::TracingHandler(TracingHandler::Target target) | |
53 : target_(target), | |
54 is_recording_(false), | |
55 weak_factory_(this) { | |
14 } | 56 } |
15 | 57 |
16 TracingHandler::~TracingHandler() { | 58 TracingHandler::~TracingHandler() { |
17 } | 59 } |
18 | 60 |
19 void TracingHandler::SetClient(scoped_ptr<Client> client) { | 61 void TracingHandler::SetClient(scoped_ptr<Client> client) { |
20 client_.swap(client); | 62 client_.swap(client); |
21 } | 63 } |
22 | 64 |
23 Response TracingHandler::Start(const std::string& categories, | 65 void TracingHandler::Detached() { |
24 const std::string& options, | 66 if (is_recording_) |
25 const double* buffer_usage_reporting_interval) { | 67 DisableRecording(true); |
26 return Response::FallThrough(); | 68 } |
69 | |
70 void TracingHandler::OnTraceDataCollected(const std::string& trace_fragment) { | |
71 // Hand-craft protocol notification message so we can substitute JSON | |
72 // that we already got as string as a bare object, not a quoted string. | |
73 std::string message( | |
74 "{ \"method\": \"Tracing.dataCollected\", \"params\": { \"value\": ["); | |
75 const size_t messageSuffixSize = 10; | |
76 message.reserve(message.size() + trace_fragment.size() + messageSuffixSize); | |
77 message += trace_fragment; | |
78 message += "] } }", client_->SendRawMessage(message); | |
caseq
2014/10/16 12:07:21
please fix formatting here!
dgozman
2014/10/17 13:53:08
Done.
| |
79 } | |
80 | |
81 void TracingHandler::OnTraceComplete() { | |
82 TracingCompleteParams params; | |
83 client_->TracingComplete(params); | |
27 } | 84 } |
28 | 85 |
29 scoped_refptr<DevToolsProtocol::Response> TracingHandler::Start( | 86 scoped_refptr<DevToolsProtocol::Response> TracingHandler::Start( |
30 const std::string& categories, | 87 const std::string& categories, |
31 const std::string& options, | 88 const std::string& options_str, |
32 const double* buffer_usage_reporting_interval, | 89 const double* buffer_usage_reporting_interval, |
33 scoped_refptr<DevToolsProtocol::Command> command) { | 90 scoped_refptr<DevToolsProtocol::Command> command) { |
34 return NULL; | 91 if (is_recording_) |
35 } | 92 return command->InternalErrorResponse("Tracing is already started"); |
93 is_recording_ = true; | |
36 | 94 |
37 Response TracingHandler::End() { | 95 base::debug::TraceOptions options = TraceOptionsFromString(options_str); |
38 return Response::FallThrough(); | 96 if (buffer_usage_reporting_interval) |
97 SetupTimer(*buffer_usage_reporting_interval); | |
98 | |
99 // If inspected target is a render process Tracing.start will be handled by | |
100 // tracing agent in the renderer. | |
101 if (target_ == Renderer) { | |
102 TracingController::GetInstance()->EnableRecording( | |
103 base::debug::CategoryFilter(categories), | |
104 options, | |
105 TracingController::EnableRecordingDoneCallback()); | |
106 return nullptr; | |
107 } | |
108 | |
109 TracingController::GetInstance()->EnableRecording( | |
110 base::debug::CategoryFilter(categories), | |
111 options, | |
112 base::Bind(&TracingHandler::OnRecordingEnabled, | |
113 weak_factory_.GetWeakPtr(), | |
114 command)); | |
115 return command->AsyncResponsePromise(); | |
39 } | 116 } |
40 | 117 |
41 scoped_refptr<DevToolsProtocol::Response> TracingHandler::End( | 118 scoped_refptr<DevToolsProtocol::Response> TracingHandler::End( |
42 scoped_refptr<DevToolsProtocol::Command> command) { | 119 scoped_refptr<DevToolsProtocol::Command> command) { |
43 return NULL; | 120 if (!is_recording_) |
121 return command->InternalErrorResponse("Tracing is not started"); | |
122 DisableRecording(false); | |
123 // If inspected target is a render process Tracing.end will be handled by | |
124 // tracing agent in the renderer. | |
125 if (target_ == Renderer) | |
126 return nullptr; | |
127 return command->SuccessResponse(nullptr); | |
44 } | 128 } |
45 | 129 |
46 scoped_refptr<DevToolsProtocol::Response> TracingHandler::GetCategories( | 130 scoped_refptr<DevToolsProtocol::Response> TracingHandler::GetCategories( |
47 scoped_refptr<DevToolsProtocol::Command> command) { | 131 scoped_refptr<DevToolsProtocol::Command> command) { |
48 return NULL; | 132 TracingController::GetInstance()->GetCategories( |
133 base::Bind(&TracingHandler::OnCategoriesReceived, | |
134 weak_factory_.GetWeakPtr(), | |
135 command)); | |
136 return command->AsyncResponsePromise(); | |
137 } | |
138 | |
139 void TracingHandler::OnRecordingEnabled( | |
140 scoped_refptr<DevToolsProtocol::Command> command) { | |
141 StartResponse response; | |
142 client_->SendStartResponse(command, response); | |
143 } | |
144 | |
145 void TracingHandler::OnBufferUsage(float usage) { | |
146 BufferUsageParams params; | |
147 params.set_value(usage); | |
148 client_->BufferUsage(params); | |
149 } | |
150 | |
151 void TracingHandler::OnCategoriesReceived( | |
152 scoped_refptr<DevToolsProtocol::Command> command, | |
153 const std::set<std::string>& category_set) { | |
154 std::vector<std::string> categories(category_set.begin(), category_set.end()); | |
155 GetCategoriesResponse response; | |
156 response.set_categories(categories); | |
157 client_->SendGetCategoriesResponse(command, response); | |
158 } | |
159 | |
160 base::debug::TraceOptions TracingHandler::TraceOptionsFromString( | |
161 const std::string& options) { | |
162 std::vector<std::string> split; | |
163 std::vector<std::string>::iterator iter; | |
164 base::debug::TraceOptions ret; | |
165 | |
166 base::SplitString(options, ',', &split); | |
167 for (iter = split.begin(); iter != split.end(); ++iter) { | |
168 if (*iter == kRecordUntilFull) { | |
169 ret.record_mode = base::debug::RECORD_UNTIL_FULL; | |
170 } else if (*iter == kRecordContinuously) { | |
171 ret.record_mode = base::debug::RECORD_CONTINUOUSLY; | |
172 } else if (*iter == kRecordAsMuchAsPossible) { | |
173 ret.record_mode = base::debug::RECORD_AS_MUCH_AS_POSSIBLE; | |
174 } else if (*iter == kEnableSampling) { | |
175 ret.enable_sampling = true; | |
176 } | |
177 } | |
178 return ret; | |
179 } | |
180 | |
181 void TracingHandler::SetupTimer(double usage_reporting_interval) { | |
182 if (usage_reporting_interval == 0) return; | |
183 | |
184 if (usage_reporting_interval < kMinimumReportingInterval) | |
185 usage_reporting_interval = kMinimumReportingInterval; | |
186 | |
187 base::TimeDelta interval = base::TimeDelta::FromMilliseconds( | |
188 std::ceil(usage_reporting_interval)); | |
189 buffer_usage_poll_timer_.reset(new base::Timer( | |
190 FROM_HERE, | |
191 interval, | |
192 base::Bind( | |
193 base::IgnoreResult(&TracingController::GetTraceBufferPercentFull), | |
194 base::Unretained(TracingController::GetInstance()), | |
195 base::Bind(&TracingHandler::OnBufferUsage, | |
196 weak_factory_.GetWeakPtr())), | |
197 true)); | |
198 buffer_usage_poll_timer_->Reset(); | |
199 } | |
200 | |
201 void TracingHandler::DisableRecording(bool abort) { | |
202 is_recording_ = false; | |
203 buffer_usage_poll_timer_.reset(); | |
204 TracingController::GetInstance()->DisableRecording( | |
205 abort ? nullptr : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr())); | |
49 } | 206 } |
50 | 207 |
51 } // namespace tracing | 208 } // namespace tracing |
52 } // namespace devtools | 209 } // namespace devtools |
53 } // namespace content | 210 } // namespace content |
OLD | NEW |