1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/devtools/devtools_tracing_handler.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "content/browser/devtools/devtools_http_handler_impl.h"
14 #include "content/browser/devtools/devtools_protocol_constants.h"
15 #include "content/public/browser/trace_controller.h"
16 #include "content/public/browser/trace_subscriber.h"
22 const char kRecordUntilFull
[] = "record-until-full";
23 const char kRecordContinuously
[] = "record-continuously";
24 const char kEnableSampling
[] = "enable-sampling";
28 DevToolsTracingHandler::DevToolsTracingHandler()
29 : is_running_(false) {
30 RegisterCommandHandler(devtools::Tracing::start::kName
,
31 base::Bind(&DevToolsTracingHandler::OnStart
,
32 base::Unretained(this)));
33 RegisterCommandHandler(devtools::Tracing::end::kName
,
34 base::Bind(&DevToolsTracingHandler::OnEnd
,
35 base::Unretained(this)));
38 DevToolsTracingHandler::~DevToolsTracingHandler() {
41 void DevToolsTracingHandler::OnEndTracingComplete() {
43 SendNotification(devtools::Tracing::tracingComplete::kName
, NULL
);
46 void DevToolsTracingHandler::OnTraceDataCollected(
47 const scoped_refptr
<base::RefCountedString
>& trace_fragment
) {
49 // Hand-craft protocol notification message so we can substitute JSON
50 // that we already got as string as a bare object, not a quoted string.
51 std::string message
= base::StringPrintf(
52 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }",
53 devtools::Tracing::dataCollected::kName
,
54 devtools::Tracing::dataCollected::kValue
,
55 trace_fragment
->data().c_str());
56 SendRawMessage(message
);
60 // Note, if you add more options here you also need to update:
61 // base/debug/trace_event_impl:TraceOptionsFromString
62 base::debug::TraceLog::Options
DevToolsTracingHandler::TraceOptionsFromString(
63 const std::string
& options
) {
64 std::vector
<std::string
> split
;
65 std::vector
<std::string
>::iterator iter
;
68 base::SplitString(options
, ',', &split
);
69 for (iter
= split
.begin(); iter
!= split
.end(); ++iter
) {
70 if (*iter
== kRecordUntilFull
) {
71 ret
|= base::debug::TraceLog::RECORD_UNTIL_FULL
;
72 } else if (*iter
== kRecordContinuously
) {
73 ret
|= base::debug::TraceLog::RECORD_CONTINUOUSLY
;
74 } else if (*iter
== kEnableSampling
) {
75 ret
|= base::debug::TraceLog::ENABLE_SAMPLING
;
78 if (!(ret
& base::debug::TraceLog::RECORD_UNTIL_FULL
) &&
79 !(ret
& base::debug::TraceLog::RECORD_CONTINUOUSLY
))
80 ret
|= base::debug::TraceLog::RECORD_UNTIL_FULL
;
82 return static_cast<base::debug::TraceLog::Options
>(ret
);
85 scoped_refptr
<DevToolsProtocol::Response
>
86 DevToolsTracingHandler::OnStart(
87 scoped_refptr
<DevToolsProtocol::Command
> command
) {
88 std::string categories
;
89 base::DictionaryValue
* params
= command
->params();
91 params
->GetString(devtools::Tracing::start::kCategories
, &categories
);
93 base::debug::TraceLog::Options options
=
94 base::debug::TraceLog::RECORD_UNTIL_FULL
;
95 if (params
&& params
->HasKey(devtools::Tracing::start::kTraceOptions
)) {
96 std::string options_param
;
97 params
->GetString(devtools::Tracing::start::kTraceOptions
, &options_param
);
98 options
= TraceOptionsFromString(options_param
);
101 TraceController::GetInstance()->BeginTracing(this, categories
, options
);
103 return command
->SuccessResponse(NULL
);
106 scoped_refptr
<DevToolsProtocol::Response
>
107 DevToolsTracingHandler::OnEnd(
108 scoped_refptr
<DevToolsProtocol::Command
> command
) {
109 TraceController::GetInstance()->EndTracingAsync(this);
110 return command
->SuccessResponse(NULL
);
113 } // namespace content