Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Side by Side Diff: content/browser/devtools/protocol/tracing_handler.cc

Issue 635733003: [DevTools] Migrate DevToolsTracingHandler to generated handler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
29 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink {
30 public:
31 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler)
32 : tracing_handler_(handler) {}
33
34 virtual void AddTraceChunk(const std::string& chunk) override {
35 if (TracingHandler* h = tracing_handler_.get())
36 h->OnTraceDataCollected(chunk);
37 }
38 virtual void Close() override {
39 if (TracingHandler* h = tracing_handler_.get())
40 h->OnTraceComplete();
41 }
42
43 private:
44 virtual ~DevToolsTraceSinkProxy() {}
45
46 base::WeakPtr<TracingHandler> tracing_handler_;
47 };
48
49 } // namespace
50
51 const char* TracingHandler::kDefaultCategories =
52 "-*,disabled-by-default-devtools.timeline*";
53 const double TracingHandler::kDefaultReportingInterval = 1000.0;
54 const double TracingHandler::kMinimumReportingInterval = 250.0;
55
56 TracingHandler::TracingHandler(TracingHandler::Target target)
57 : target_(target),
58 is_recording_(false),
59 weak_factory_(this) {
14 } 60 }
15 61
16 TracingHandler::~TracingHandler() { 62 TracingHandler::~TracingHandler() {
17 } 63 }
18 64
19 void TracingHandler::SetClient(scoped_ptr<Client> client) { 65 void TracingHandler::SetClient(scoped_ptr<Client> client) {
20 client_.swap(client); 66 client_.swap(client);
21 } 67 }
22 68
23 Response TracingHandler::Start(const std::string& categories, 69 void TracingHandler::Detached() {
24 const std::string& options, 70 if (is_recording_)
25 const double* buffer_usage_reporting_interval) { 71 DisableRecording(true);
26 return Response::FallThrough(); 72 }
73
74 void TracingHandler::OnTraceDataCollected(const std::string& trace_fragment) {
75 // Hand-craft protocol notification message so we can substitute JSON
76 // that we already got as string as a bare object, not a quoted string.
77 std::string message(
78 "{ \"method\": \"Tracing.dataCollected\", \"params\": { \"value\": [");
79 const size_t messageSuffixSize = 10;
80 message.reserve(message.size() + trace_fragment.size() + messageSuffixSize);
81 message += trace_fragment;
82 message += "] } }", client_->SendRawMessage(message);
83 }
84
85 void TracingHandler::OnTraceComplete() {
86 TracingCompleteParams params;
87 client_->TracingComplete(params);
27 } 88 }
28 89
29 scoped_refptr<DevToolsProtocol::Response> TracingHandler::Start( 90 scoped_refptr<DevToolsProtocol::Response> TracingHandler::Start(
30 const std::string& categories, 91 const std::string& categories,
31 const std::string& options, 92 const std::string& options_str,
32 const double* buffer_usage_reporting_interval, 93 const double* buffer_usage_reporting_interval,
33 scoped_refptr<DevToolsProtocol::Command> command) { 94 scoped_refptr<DevToolsProtocol::Command> command) {
34 return NULL; 95 if (is_recording_)
35 } 96 return command->InternalErrorResponse("Tracing is already started");
97 is_recording_ = true;
36 98
37 Response TracingHandler::End() { 99 base::debug::TraceOptions options = TraceOptionsFromString(options_str);
38 return Response::FallThrough(); 100 if (buffer_usage_reporting_interval)
101 SetupTimer(*buffer_usage_reporting_interval);
102
103 // If inspected target is a render process Tracing.start will be handled by
104 // tracing agent in the renderer.
105 if (target_ == Renderer) {
106 TracingController::GetInstance()->EnableRecording(
107 base::debug::CategoryFilter(categories),
108 options,
109 TracingController::EnableRecordingDoneCallback());
110 return NULL;
vkuzkokov 2014/10/07 13:36:07 Consider using nullptr in this file.
dgozman 2014/10/14 12:42:22 Done.
111 }
112
113 TracingController::GetInstance()->EnableRecording(
114 base::debug::CategoryFilter(categories),
115 options,
116 base::Bind(&TracingHandler::OnRecordingEnabled,
117 weak_factory_.GetWeakPtr(),
118 command));
119 return command->AsyncResponsePromise();
39 } 120 }
40 121
41 scoped_refptr<DevToolsProtocol::Response> TracingHandler::End( 122 scoped_refptr<DevToolsProtocol::Response> TracingHandler::End(
42 scoped_refptr<DevToolsProtocol::Command> command) { 123 scoped_refptr<DevToolsProtocol::Command> command) {
43 return NULL; 124 if (!is_recording_)
125 return command->InternalErrorResponse("Tracing is not started");
126 DisableRecording(false);
127 // If inspected target is a render process Tracing.end will be handled by
128 // tracing agent in the renderer.
129 if (target_ == Renderer)
130 return NULL;
131 return command->SuccessResponse(NULL);
44 } 132 }
45 133
46 scoped_refptr<DevToolsProtocol::Response> TracingHandler::GetCategories( 134 scoped_refptr<DevToolsProtocol::Response> TracingHandler::GetCategories(
47 scoped_refptr<DevToolsProtocol::Command> command) { 135 scoped_refptr<DevToolsProtocol::Command> command) {
48 return NULL; 136 TracingController::GetInstance()->GetCategories(
137 base::Bind(&TracingHandler::OnCategoriesReceived,
138 weak_factory_.GetWeakPtr(),
139 command));
140 return command->AsyncResponsePromise();
141 }
142
143 void TracingHandler::OnRecordingEnabled(
144 scoped_refptr<DevToolsProtocol::Command> command) {
145 StartResponse response;
146 client_->SendStartResponse(command, response);
147 }
148
149 void TracingHandler::OnBufferUsage(float usage) {
150 BufferUsageParams params;
151 params.set_value(usage);
152 client_->BufferUsage(params);
153 }
154
155 void TracingHandler::OnCategoriesReceived(
156 scoped_refptr<DevToolsProtocol::Command> command,
157 const std::set<std::string>& category_set) {
158 std::vector<std::string> categories;
159 for (const auto& category : category_set)
caseq 2014/10/13 13:01:27 consider using range constructor for categories in
dgozman 2014/10/14 12:42:22 Done.
160 categories.push_back(category);
161
162 GetCategoriesResponse response;
163 response.set_categories(categories);
164 client_->SendGetCategoriesResponse(command, response);
165 }
166
167 base::debug::TraceOptions TracingHandler::TraceOptionsFromString(
168 const std::string& options) {
169 std::vector<std::string> split;
170 std::vector<std::string>::iterator iter;
171 base::debug::TraceOptions ret;
172
173 base::SplitString(options, ',', &split);
174 for (iter = split.begin(); iter != split.end(); ++iter) {
175 if (*iter == kRecordUntilFull) {
176 ret.record_mode = base::debug::RECORD_UNTIL_FULL;
177 } else if (*iter == kRecordContinuously) {
178 ret.record_mode = base::debug::RECORD_CONTINUOUSLY;
179 } else if (*iter == kRecordAsMuchAsPossible) {
180 ret.record_mode = base::debug::RECORD_AS_MUCH_AS_POSSIBLE;
181 } else if (*iter == kEnableSampling) {
182 ret.enable_sampling = true;
183 }
184 }
185 return ret;
186 }
187
188 void TracingHandler::SetupTimer(double usage_reporting_interval) {
189 if (usage_reporting_interval == 0) return;
190
191 if (usage_reporting_interval < kMinimumReportingInterval)
192 usage_reporting_interval = kMinimumReportingInterval;
193
194 base::TimeDelta interval = base::TimeDelta::FromMilliseconds(
195 std::ceil(usage_reporting_interval));
196 buffer_usage_poll_timer_.reset(new base::Timer(
197 FROM_HERE,
198 interval,
199 base::Bind(
200 base::IgnoreResult(&TracingController::GetTraceBufferPercentFull),
201 base::Unretained(TracingController::GetInstance()),
202 base::Bind(&TracingHandler::OnBufferUsage,
203 weak_factory_.GetWeakPtr())),
204 true));
205 buffer_usage_poll_timer_->Reset();
206 }
207
208 void TracingHandler::DisableRecording(bool abort) {
209 is_recording_ = false;
210 buffer_usage_poll_timer_.reset();
211 TracingController::GetInstance()->DisableRecording(
212 abort ? NULL : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr()));
49 } 213 }
50 214
51 } // namespace tracing 215 } // namespace tracing
52 } // namespace devtools 216 } // namespace devtools
53 } // namespace content 217 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698