OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/devtools_tracing_handler.h" | 5 #include "content/browser/devtools/devtools_tracing_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util.h" | |
10 #include "base/json/json_reader.h" | |
11 #include "base/json/json_writer.h" | |
9 #include "base/location.h" | 12 #include "base/location.h" |
10 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
11 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 15 #include "base/values.h" |
13 #include "content/browser/devtools/devtools_http_handler_impl.h" | 16 #include "content/browser/devtools/devtools_http_handler_impl.h" |
14 #include "content/browser/devtools/devtools_protocol_constants.h" | 17 #include "content/browser/devtools/devtools_protocol_constants.h" |
15 #include "content/public/browser/trace_controller.h" | 18 #include "content/browser/tracing/tracing_controller_impl.h" |
16 #include "content/public/browser/trace_subscriber.h" | 19 #include "content/public/browser/browser_thread.h" |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 const char kRecordUntilFull[] = "record-until-full"; | 25 const char kRecordUntilFull[] = "record-until-full"; |
23 const char kRecordContinuously[] = "record-continuously"; | 26 const char kRecordContinuously[] = "record-continuously"; |
24 const char kEnableSampling[] = "enable-sampling"; | 27 const char kEnableSampling[] = "enable-sampling"; |
25 | 28 |
26 } // namespace | 29 } // namespace |
27 | 30 |
28 DevToolsTracingHandler::DevToolsTracingHandler() | 31 DevToolsTracingHandler::DevToolsTracingHandler() |
29 : is_running_(false) { | 32 : is_running_(false), |
33 weak_factory_(this) { | |
30 RegisterCommandHandler(devtools::Tracing::start::kName, | 34 RegisterCommandHandler(devtools::Tracing::start::kName, |
31 base::Bind(&DevToolsTracingHandler::OnStart, | 35 base::Bind(&DevToolsTracingHandler::OnStart, |
32 base::Unretained(this))); | 36 base::Unretained(this))); |
33 RegisterCommandHandler(devtools::Tracing::end::kName, | 37 RegisterCommandHandler(devtools::Tracing::end::kName, |
34 base::Bind(&DevToolsTracingHandler::OnEnd, | 38 base::Bind(&DevToolsTracingHandler::OnEnd, |
35 base::Unretained(this))); | 39 base::Unretained(this))); |
36 } | 40 } |
37 | 41 |
38 DevToolsTracingHandler::~DevToolsTracingHandler() { | 42 DevToolsTracingHandler::~DevToolsTracingHandler() { |
39 } | 43 } |
40 | 44 |
41 void DevToolsTracingHandler::OnEndTracingComplete() { | 45 void DevToolsTracingHandler::ReadRecordingResult(const base::FilePath& path) { |
pfeldman
2013/11/20 00:10:10
Please make sure order of methods in cc matches th
Xianzhu
2013/11/20 00:37:22
Done.
| |
46 if (!is_running_) | |
47 return; | |
48 | |
49 std::string trace_data; | |
50 if (!base::ReadFileToString(path, &trace_data)) { | |
pfeldman
2013/11/20 00:10:10
Why do we need to deal with files in the handler?
Xianzhu
2013/11/20 00:37:22
This is how the new TracingController interface is
| |
51 LOG(ERROR) << "Failed to read file: " << path.value(); | |
52 OnTraceDataCollected(""); | |
53 } else { | |
54 scoped_ptr<base::Value> trace_value(base::JSONReader::Read(trace_data)); | |
55 DictionaryValue* dictionary = NULL; | |
56 bool ok = trace_value->GetAsDictionary(&dictionary); | |
57 DCHECK(ok); | |
58 ListValue* list = NULL; | |
59 ok = dictionary->GetList("traceEvents", &list); | |
60 DCHECK(ok); | |
61 std::string buffer; | |
62 for (size_t i = 0; i < list->GetSize(); ++i) { | |
63 std::string item; | |
64 base::Value* item_value; | |
65 list->Get(i, &item_value); | |
66 base::JSONWriter::Write(item_value, &item); | |
67 if (buffer.size()) | |
68 buffer.append(","); | |
69 buffer.append(item); | |
70 if (i % 1000 == 0) { | |
71 OnTraceDataCollected(buffer); | |
72 buffer.clear(); | |
73 } | |
74 } | |
75 if (buffer.size()) | |
76 OnTraceDataCollected(buffer); | |
77 } | |
78 base::DeleteFile(path, false); | |
79 | |
42 is_running_ = false; | 80 is_running_ = false; |
pfeldman
2013/11/20 00:10:10
Do we still need is_running_?
Xianzhu
2013/11/20 00:37:22
Seems useless (even in the original code). Removed
| |
43 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); | 81 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); |
44 } | 82 } |
45 | 83 |
46 void DevToolsTracingHandler::OnTraceDataCollected( | 84 void DevToolsTracingHandler::OnTraceDataCollected( |
47 const scoped_refptr<base::RefCountedString>& trace_fragment) { | 85 const std::string& trace_fragment) { |
48 if (is_running_) { | 86 // Hand-craft protocol notification message so we can substitute JSON |
49 // Hand-craft protocol notification message so we can substitute JSON | 87 // that we already got as string as a bare object, not a quoted string. |
50 // that we already got as string as a bare object, not a quoted string. | 88 std::string message = base::StringPrintf( |
51 std::string message = base::StringPrintf( | 89 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", |
52 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", | 90 devtools::Tracing::dataCollected::kName, |
53 devtools::Tracing::dataCollected::kName, | 91 devtools::Tracing::dataCollected::kValue, |
54 devtools::Tracing::dataCollected::kValue, | 92 trace_fragment.c_str()); |
55 trace_fragment->data().c_str()); | 93 SendRawMessage(message); |
56 SendRawMessage(message); | |
57 } | |
58 } | 94 } |
59 | 95 |
60 // Note, if you add more options here you also need to update: | 96 void DevToolsTracingHandler::BeginReadingRecordingResult( |
61 // base/debug/trace_event_impl:TraceOptionsFromString | 97 const base::FilePath& path) { |
62 base::debug::TraceLog::Options DevToolsTracingHandler::TraceOptionsFromString( | 98 BrowserThread::PostTask( |
99 BrowserThread::FILE, FROM_HERE, | |
100 base::Bind(&DevToolsTracingHandler::ReadRecordingResult, | |
101 weak_factory_.GetWeakPtr(), path)); | |
102 } | |
103 | |
104 TracingController::Options DevToolsTracingHandler::TraceOptionsFromString( | |
63 const std::string& options) { | 105 const std::string& options) { |
64 std::vector<std::string> split; | 106 std::vector<std::string> split; |
65 std::vector<std::string>::iterator iter; | 107 std::vector<std::string>::iterator iter; |
66 int ret = 0; | 108 int ret = 0; |
67 | 109 |
68 base::SplitString(options, ',', &split); | 110 base::SplitString(options, ',', &split); |
69 for (iter = split.begin(); iter != split.end(); ++iter) { | 111 for (iter = split.begin(); iter != split.end(); ++iter) { |
70 if (*iter == kRecordUntilFull) { | 112 if (*iter == kRecordUntilFull) { |
71 ret |= base::debug::TraceLog::RECORD_UNTIL_FULL; | 113 ret &= ~TracingController::RECORD_CONTINUOUSLY; |
pfeldman
2013/11/20 00:10:10
Is this change intentional?
Xianzhu
2013/11/20 00:37:22
Yes. TracingController::Options doesn't have RECOR
| |
72 } else if (*iter == kRecordContinuously) { | 114 } else if (*iter == kRecordContinuously) { |
73 ret |= base::debug::TraceLog::RECORD_CONTINUOUSLY; | 115 ret |= TracingController::RECORD_CONTINUOUSLY; |
74 } else if (*iter == kEnableSampling) { | 116 } else if (*iter == kEnableSampling) { |
75 ret |= base::debug::TraceLog::ENABLE_SAMPLING; | 117 ret |= TracingController::ENABLE_SAMPLING; |
76 } | 118 } |
77 } | 119 } |
78 if (!(ret & base::debug::TraceLog::RECORD_UNTIL_FULL) && | 120 return static_cast<TracingController::Options>(ret); |
79 !(ret & base::debug::TraceLog::RECORD_CONTINUOUSLY)) | |
80 ret |= base::debug::TraceLog::RECORD_UNTIL_FULL; | |
81 | |
82 return static_cast<base::debug::TraceLog::Options>(ret); | |
83 } | 121 } |
84 | 122 |
85 scoped_refptr<DevToolsProtocol::Response> | 123 scoped_refptr<DevToolsProtocol::Response> |
86 DevToolsTracingHandler::OnStart( | 124 DevToolsTracingHandler::OnStart( |
87 scoped_refptr<DevToolsProtocol::Command> command) { | 125 scoped_refptr<DevToolsProtocol::Command> command) { |
88 std::string categories; | 126 std::string categories; |
89 base::DictionaryValue* params = command->params(); | 127 base::DictionaryValue* params = command->params(); |
90 if (params) | 128 if (params) |
91 params->GetString(devtools::Tracing::start::kCategories, &categories); | 129 params->GetString(devtools::Tracing::start::kCategories, &categories); |
92 | 130 |
93 base::debug::TraceLog::Options options = | 131 TracingController::Options options = TracingController::DEFAULT_OPTIONS; |
94 base::debug::TraceLog::RECORD_UNTIL_FULL; | |
95 if (params && params->HasKey(devtools::Tracing::start::kTraceOptions)) { | 132 if (params && params->HasKey(devtools::Tracing::start::kTraceOptions)) { |
96 std::string options_param; | 133 std::string options_param; |
97 params->GetString(devtools::Tracing::start::kTraceOptions, &options_param); | 134 params->GetString(devtools::Tracing::start::kTraceOptions, &options_param); |
98 options = TraceOptionsFromString(options_param); | 135 options = TraceOptionsFromString(options_param); |
99 } | 136 } |
100 | 137 |
101 TraceController::GetInstance()->BeginTracing(this, categories, options); | 138 TracingController::GetInstance()->EnableRecording( |
139 categories, options, | |
140 TracingController::EnableRecordingDoneCallback()); | |
102 is_running_ = true; | 141 is_running_ = true; |
103 return command->SuccessResponse(NULL); | 142 return command->SuccessResponse(NULL); |
104 } | 143 } |
105 | 144 |
106 scoped_refptr<DevToolsProtocol::Response> | 145 scoped_refptr<DevToolsProtocol::Response> |
107 DevToolsTracingHandler::OnEnd( | 146 DevToolsTracingHandler::OnEnd( |
108 scoped_refptr<DevToolsProtocol::Command> command) { | 147 scoped_refptr<DevToolsProtocol::Command> command) { |
109 TraceController::GetInstance()->EndTracingAsync(this); | 148 TracingController::GetInstance()->DisableRecording( |
149 base::FilePath(), | |
150 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
151 weak_factory_.GetWeakPtr())); | |
110 return command->SuccessResponse(NULL); | 152 return command->SuccessResponse(NULL); |
111 } | 153 } |
112 | 154 |
113 } // namespace content | 155 } // namespace content |
OLD | NEW |