Chromium Code Reviews| 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, | |
| 38 const char separator) { | |
|
caseq
2016/03/12 00:13:32
nit: nuke const from parameter passed by value.
Zhen Wang
2016/03/13 18:17:06
Done.
| |
| 39 std::string out_str; | |
| 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 } | |
| 69 return std::move(out_list); | |
| 70 } | |
| 71 | |
| 72 return value.CreateDeepCopy(); | |
| 73 } | |
| 74 | |
| 33 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { | 75 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { |
| 34 public: | 76 public: |
| 35 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler) | 77 explicit DevToolsTraceSinkProxy(base::WeakPtr<TracingHandler> handler) |
| 36 : tracing_handler_(handler) {} | 78 : tracing_handler_(handler) {} |
| 37 | 79 |
| 38 void AddTraceChunk(const std::string& chunk) override { | 80 void AddTraceChunk(const std::string& chunk) override { |
| 39 if (TracingHandler* h = tracing_handler_.get()) | 81 if (TracingHandler* h = tracing_handler_.get()) |
| 40 h->OnTraceDataCollected(chunk); | 82 h->OnTraceDataCollected(chunk); |
| 41 } | 83 } |
| 42 void Close() override { | 84 void Close() override { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 | 165 |
| 124 void TracingHandler::OnTraceComplete() { | 166 void TracingHandler::OnTraceComplete() { |
| 125 client_->TracingComplete(TracingCompleteParams::Create()); | 167 client_->TracingComplete(TracingCompleteParams::Create()); |
| 126 } | 168 } |
| 127 | 169 |
| 128 void TracingHandler::OnTraceToStreamComplete(const std::string& stream_handle) { | 170 void TracingHandler::OnTraceToStreamComplete(const std::string& stream_handle) { |
| 129 client_->TracingComplete( | 171 client_->TracingComplete( |
| 130 TracingCompleteParams::Create()->set_stream(stream_handle)); | 172 TracingCompleteParams::Create()->set_stream(stream_handle)); |
| 131 } | 173 } |
| 132 | 174 |
| 133 Response TracingHandler::Start(DevToolsCommandId command_id, | 175 Response TracingHandler::Start( |
| 134 const std::string* categories, | 176 DevToolsCommandId command_id, |
| 135 const std::string* options, | 177 const std::string* categories, |
| 136 const double* buffer_usage_reporting_interval, | 178 const std::string* options, |
| 137 const std::string* transfer_mode) { | 179 const double* buffer_usage_reporting_interval, |
| 180 const std::string* transfer_mode, | |
| 181 const scoped_ptr<base::DictionaryValue>& config) { | |
| 138 if (IsTracing()) | 182 if (IsTracing()) |
| 139 return Response::InternalError("Tracing is already started"); | 183 return Response::InternalError("Tracing is already started"); |
| 140 | 184 |
| 185 if (config && (categories || options)) { | |
| 186 return Response::InternalError( | |
| 187 "Both trace config and categories/options are specified. Only one way " | |
|
caseq
2016/03/12 00:13:32
nit: "Either trace config (preferred) or categorie
Zhen Wang
2016/03/13 18:17:06
Done.
| |
| 188 "should be used; and using trace config is preferred."); | |
| 189 } | |
| 190 | |
| 141 did_initiate_recording_ = true; | 191 did_initiate_recording_ = true; |
| 142 return_as_stream_ = | 192 return_as_stream_ = |
| 143 transfer_mode && *transfer_mode == start::kTransferModeReturnAsStream; | 193 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) | 194 if (buffer_usage_reporting_interval) |
| 148 SetupTimer(*buffer_usage_reporting_interval); | 195 SetupTimer(*buffer_usage_reporting_interval); |
| 149 | 196 |
| 197 base::trace_event::TraceConfig trace_config; | |
| 198 if (config) { | |
| 199 trace_config = GetTraceConfigFromDevtoolsConfig(*config); | |
| 200 } else if (categories || options) { | |
| 201 trace_config = base::trace_event::TraceConfig( | |
| 202 categories ? *categories : std::string(), | |
| 203 options ? *options : std::string()); | |
| 204 } | |
| 205 | |
| 150 // If inspected target is a render process Tracing.start will be handled by | 206 // If inspected target is a render process Tracing.start will be handled by |
| 151 // tracing agent in the renderer. | 207 // tracing agent in the renderer. |
| 152 TracingController::GetInstance()->StartTracing( | 208 TracingController::GetInstance()->StartTracing( |
| 153 trace_config, | 209 trace_config, |
| 154 base::Bind(&TracingHandler::OnRecordingEnabled, | 210 base::Bind(&TracingHandler::OnRecordingEnabled, |
| 155 weak_factory_.GetWeakPtr(), | 211 weak_factory_.GetWeakPtr(), |
| 156 command_id)); | 212 command_id)); |
| 157 | 213 |
| 158 return target_ == Renderer ? Response::FallThrough() : Response::OK(); | 214 return target_ == Renderer ? Response::FallThrough() : Response::OK(); |
| 159 } | 215 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 | 330 |
| 275 bool TracingHandler::IsTracing() const { | 331 bool TracingHandler::IsTracing() const { |
| 276 return TracingController::GetInstance()->IsTracing(); | 332 return TracingController::GetInstance()->IsTracing(); |
| 277 } | 333 } |
| 278 | 334 |
| 279 bool TracingHandler::IsStartupTracingActive() { | 335 bool TracingHandler::IsStartupTracingActive() { |
| 280 return ::tracing::TraceConfigFile::GetInstance()->IsEnabled() && | 336 return ::tracing::TraceConfigFile::GetInstance()->IsEnabled() && |
| 281 TracingController::GetInstance()->IsTracing(); | 337 TracingController::GetInstance()->IsTracing(); |
| 282 } | 338 } |
| 283 | 339 |
| 340 // static | |
| 341 base::trace_event::TraceConfig TracingHandler::GetTraceConfigFromDevtoolsConfig( | |
| 342 const base::DictionaryValue& devtools_config) { | |
| 343 scoped_ptr<base::Value> value = ConvertDictKeyStyle(devtools_config); | |
| 344 DCHECK(value && value->IsType(base::Value::TYPE_DICTIONARY)); | |
| 345 scoped_ptr<base::DictionaryValue> tracing_dict( | |
| 346 static_cast<base::DictionaryValue*>(value.release())); | |
| 347 | |
| 348 std::string mode; | |
| 349 if (tracing_dict->GetString(kRecordModeParam, &mode)) | |
| 350 tracing_dict->SetString(kRecordModeParam, ConvertFromCamelCase(mode, '-')); | |
| 351 | |
| 352 return base::trace_event::TraceConfig(*tracing_dict); | |
| 353 } | |
| 354 | |
| 284 } // namespace tracing | 355 } // namespace tracing |
| 285 } // namespace devtools | 356 } // namespace devtools |
| 286 } // namespace content | 357 } // namespace content |
| OLD | NEW |