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