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> | 7 #include <cmath> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/json/json_writer.h" | |
11 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
12 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
16 #include "base/trace_event/memory_dump_manager.h" | 17 #include "base/trace_event/memory_dump_manager.h" |
17 #include "base/trace_event/trace_event_impl.h" | 18 #include "base/trace_event/trace_event_impl.h" |
18 #include "base/trace_event/tracing_agent.h" | 19 #include "base/trace_event/tracing_agent.h" |
19 #include "components/tracing/trace_config_file.h" | 20 #include "components/tracing/trace_config_file.h" |
20 #include "content/browser/devtools/devtools_io_context.h" | 21 #include "content/browser/devtools/devtools_io_context.h" |
21 #include "content/browser/tracing/tracing_controller_impl.h" | 22 #include "content/browser/tracing/tracing_controller_impl.h" |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 namespace devtools { | 25 namespace devtools { |
25 namespace tracing { | 26 namespace tracing { |
26 | 27 |
27 using Response = DevToolsProtocolClient::Response; | 28 using Response = DevToolsProtocolClient::Response; |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const double kMinimumReportingInterval = 250.0; | 32 const double kMinimumReportingInterval = 250.0; |
32 | 33 |
34 const char kRecordModeParam[] = "record_mode"; | |
35 | |
36 // Convert from camel case to separator + lowercase. | |
37 std::string ConvertFromCamelCase(const std::string& in_str, char separator) { | |
38 std::string out_str; | |
39 for (const char& c : in_str) { | |
Primiano Tucci (use gerrit)
2016/03/14 16:53:02
maybe if you add a out_str.reserve(in_str.size())
Zhen Wang
2016/03/14 21:41:45
Done.
| |
40 if (isupper(c)) { | |
41 out_str.push_back(separator); | |
42 out_str.push_back(tolower(c)); | |
43 } else { | |
44 out_str.push_back(c); | |
45 } | |
46 } | |
47 return out_str; | |
48 } | |
49 | |
50 scoped_ptr<base::Value> ConvertDictKeyStyle(const base::Value& value) { | |
51 const base::DictionaryValue* dict = nullptr; | |
52 if (value.GetAsDictionary(&dict)) { | |
53 scoped_ptr<base::DictionaryValue> out_dict(new base::DictionaryValue()); | |
54 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | |
55 it.Advance()) { | |
56 out_dict->Set(ConvertFromCamelCase(it.key(), '_'), | |
57 ConvertDictKeyStyle(it.value())); | |
58 } | |
59 return std::move(out_dict); | |
60 } | |
61 | |
62 const base::ListValue* list = nullptr; | |
63 if (value.GetAsList(&list)) { | |
64 scoped_ptr<base::ListValue> out_list(new base::ListValue()); | |
65 for (const auto& value : *list) { | |
petrcermak
2016/03/14 18:05:09
nit: No need for braces
Zhen Wang
2016/03/14 21:41:45
Done.
| |
66 out_list->Append(ConvertDictKeyStyle(*value)); | |
67 } | |
68 return std::move(out_list); | |
69 } | |
70 | |
71 return value.CreateDeepCopy(); | |
72 } | |
73 | |
33 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { | 74 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { |
34 public: | 75 public: |
35 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler) | 76 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler) |
36 : tracing_handler_(handler) {} | 77 : tracing_handler_(handler) {} |
37 | 78 |
38 void AddTraceChunk(const std::string& chunk) override { | 79 void AddTraceChunk(const std::string& chunk) override { |
39 if (TracingHandler* h = tracing_handler_.get()) | 80 if (TracingHandler* h = tracing_handler_.get()) |
40 h->OnTraceDataCollected(chunk); | 81 h->OnTraceDataCollected(chunk); |
41 } | 82 } |
42 void Close() override { | 83 void Close() override { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 | 164 |
124 void TracingHandler::OnTraceComplete() { | 165 void TracingHandler::OnTraceComplete() { |
125 client_->TracingComplete(TracingCompleteParams::Create()); | 166 client_->TracingComplete(TracingCompleteParams::Create()); |
126 } | 167 } |
127 | 168 |
128 void TracingHandler::OnTraceToStreamComplete(const std::string& stream_handle) { | 169 void TracingHandler::OnTraceToStreamComplete(const std::string& stream_handle) { |
129 client_->TracingComplete( | 170 client_->TracingComplete( |
130 TracingCompleteParams::Create()->set_stream(stream_handle)); | 171 TracingCompleteParams::Create()->set_stream(stream_handle)); |
131 } | 172 } |
132 | 173 |
133 Response TracingHandler::Start(DevToolsCommandId command_id, | 174 Response TracingHandler::Start( |
134 const std::string* categories, | 175 DevToolsCommandId command_id, |
135 const std::string* options, | 176 const std::string* categories, |
136 const double* buffer_usage_reporting_interval, | 177 const std::string* options, |
137 const std::string* transfer_mode) { | 178 const double* buffer_usage_reporting_interval, |
179 const std::string* transfer_mode, | |
180 const scoped_ptr<base::DictionaryValue>& config) { | |
138 if (IsTracing()) | 181 if (IsTracing()) |
139 return Response::InternalError("Tracing is already started"); | 182 return Response::InternalError("Tracing is already started"); |
140 | 183 |
184 if (config && (categories || options)) { | |
185 return Response::InternalError( | |
186 "Either trace config (preferred) or categories/options should be " | |
petrcermak
2016/03/14 18:05:09
nit: I'd say "categories+options" instead of "cate
petrcermak
2016/03/14 18:05:09
nit: Since this is an exclusive "or", there should
Zhen Wang
2016/03/14 21:41:45
done
| |
187 "specified, but not both."); | |
188 } | |
189 | |
141 did_initiate_recording_ = true; | 190 did_initiate_recording_ = true; |
142 return_as_stream_ = | 191 return_as_stream_ = |
143 transfer_mode && *transfer_mode == start::kTransferModeReturnAsStream; | 192 transfer_mode && *transfer_mode == start::kTransferModeReturnAsStream; |
144 base::trace_event::TraceConfig trace_config( | |
145 categories ? *categories : std::string(), | |
146 options ? *options : std::string()); | |
147 if (buffer_usage_reporting_interval) | 193 if (buffer_usage_reporting_interval) |
148 SetupTimer(*buffer_usage_reporting_interval); | 194 SetupTimer(*buffer_usage_reporting_interval); |
149 | 195 |
196 base::trace_event::TraceConfig trace_config; | |
197 if (config) { | |
198 trace_config = GetTraceConfigFromDevtoolsConfig(*config); | |
199 } else if (categories || options) { | |
200 trace_config = base::trace_event::TraceConfig( | |
201 categories ? *categories : std::string(), | |
202 options ? *options : std::string()); | |
203 } | |
204 | |
150 // If inspected target is a render process Tracing.start will be handled by | 205 // If inspected target is a render process Tracing.start will be handled by |
151 // tracing agent in the renderer. | 206 // tracing agent in the renderer. |
152 TracingController::GetInstance()->StartTracing( | 207 TracingController::GetInstance()->StartTracing( |
153 trace_config, | 208 trace_config, |
154 base::Bind(&TracingHandler::OnRecordingEnabled, | 209 base::Bind(&TracingHandler::OnRecordingEnabled, |
155 weak_factory_.GetWeakPtr(), | 210 weak_factory_.GetWeakPtr(), |
156 command_id)); | 211 command_id)); |
157 | 212 |
158 return target_ == Renderer ? Response::FallThrough() : Response::OK(); | 213 return target_ == Renderer ? Response::FallThrough() : Response::OK(); |
159 } | 214 } |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 | 329 |
275 bool TracingHandler::IsTracing() const { | 330 bool TracingHandler::IsTracing() const { |
276 return TracingController::GetInstance()->IsTracing(); | 331 return TracingController::GetInstance()->IsTracing(); |
277 } | 332 } |
278 | 333 |
279 bool TracingHandler::IsStartupTracingActive() { | 334 bool TracingHandler::IsStartupTracingActive() { |
280 return ::tracing::TraceConfigFile::GetInstance()->IsEnabled() && | 335 return ::tracing::TraceConfigFile::GetInstance()->IsEnabled() && |
281 TracingController::GetInstance()->IsTracing(); | 336 TracingController::GetInstance()->IsTracing(); |
282 } | 337 } |
283 | 338 |
339 // static | |
340 base::trace_event::TraceConfig TracingHandler::GetTraceConfigFromDevtoolsConfig( | |
341 const base::DictionaryValue& devtools_config) { | |
342 scoped_ptr<base::Value> value = ConvertDictKeyStyle(devtools_config); | |
343 DCHECK(value && value->IsType(base::Value::TYPE_DICTIONARY)); | |
344 scoped_ptr<base::DictionaryValue> tracing_dict( | |
345 static_cast<base::DictionaryValue*>(value.release())); | |
346 | |
347 std::string mode; | |
348 if (tracing_dict->GetString(kRecordModeParam, &mode)) | |
349 tracing_dict->SetString(kRecordModeParam, ConvertFromCamelCase(mode, '-')); | |
350 | |
351 return base::trace_event::TraceConfig(*tracing_dict); | |
352 } | |
353 | |
284 } // namespace tracing | 354 } // namespace tracing |
285 } // namespace devtools | 355 } // namespace devtools |
286 } // namespace content | 356 } // namespace content |
OLD | NEW |