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 <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | |
11 #include "base/debug/trace_event_impl.h" | 10 #include "base/debug/trace_event_impl.h" |
12 #include "base/files/file_util.h" | |
13 #include "base/json/json_reader.h" | |
14 #include "base/json/json_writer.h" | |
15 #include "base/location.h" | |
16 #include "base/memory/ref_counted_memory.h" | |
17 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
18 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
19 #include "base/time/time.h" | 13 #include "base/time/time.h" |
20 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
21 #include "base/values.h" | 15 #include "base/values.h" |
22 #include "content/browser/devtools/devtools_http_handler_impl.h" | 16 #include "content/browser/devtools/devtools_http_handler_impl.h" |
23 #include "content/browser/devtools/devtools_protocol_constants.h" | 17 #include "content/browser/devtools/devtools_protocol_constants.h" |
24 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/tracing_controller.h" | 19 #include "content/public/browser/tracing_controller.h" |
26 | 20 |
27 namespace content { | 21 namespace content { |
28 | 22 |
29 namespace { | 23 namespace { |
30 | 24 |
31 const char kRecordUntilFull[] = "record-until-full"; | 25 const char kRecordUntilFull[] = "record-until-full"; |
32 const char kRecordContinuously[] = "record-continuously"; | 26 const char kRecordContinuously[] = "record-continuously"; |
33 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; | 27 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible"; |
34 const char kEnableSampling[] = "enable-sampling"; | 28 const char kEnableSampling[] = "enable-sampling"; |
35 | 29 |
36 void ReadFile( | 30 class DevToolsTraceSinkProxy : public TracingController::TraceDataSink { |
37 const base::FilePath& path, | 31 public: |
38 const base::Callback<void(const scoped_refptr<base::RefCountedString>&)> | 32 explicit DevToolsTraceSinkProxy(base::WeakPtr<DevToolsTracingHandler> handler) |
39 callback) { | 33 : tracing_handler_(handler) { |
40 std::string trace_data; | 34 } |
41 if (!base::ReadFileToString(path, &trace_data)) | 35 |
42 LOG(ERROR) << "Failed to read file: " << path.value(); | 36 virtual void AddTraceChunk(const std::string& chunk) OVERRIDE { |
43 base::DeleteFile(path, false); | 37 if (DevToolsTracingHandler* h = tracing_handler_.get()) |
44 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 38 h->OnTraceDataCollected(chunk); |
45 base::Bind(callback, make_scoped_refptr( | 39 } |
46 base::RefCountedString::TakeString(&trace_data)))); | 40 virtual void Close() OVERRIDE { |
47 } | 41 if (DevToolsTracingHandler* h = tracing_handler_.get()) |
| 42 h->OnTraceComplete(); |
| 43 } |
| 44 |
| 45 private: |
| 46 virtual ~DevToolsTraceSinkProxy() {} |
| 47 |
| 48 base::WeakPtr<DevToolsTracingHandler> tracing_handler_; |
| 49 }; |
48 | 50 |
49 } // namespace | 51 } // namespace |
50 | 52 |
51 const char* DevToolsTracingHandler::kDefaultCategories = | 53 const char* DevToolsTracingHandler::kDefaultCategories = |
52 "-*,disabled-by-default-devtools.timeline*"; | 54 "-*,disabled-by-default-devtools.timeline*"; |
53 const double DevToolsTracingHandler::kDefaultReportingInterval = 1000.0; | 55 const double DevToolsTracingHandler::kDefaultReportingInterval = 1000.0; |
54 const double DevToolsTracingHandler::kMinimumReportingInterval = 250.0; | 56 const double DevToolsTracingHandler::kMinimumReportingInterval = 250.0; |
55 | 57 |
56 DevToolsTracingHandler::DevToolsTracingHandler( | 58 DevToolsTracingHandler::DevToolsTracingHandler( |
57 DevToolsTracingHandler::Target target) | 59 DevToolsTracingHandler::Target target) |
58 : weak_factory_(this), target_(target), is_recording_(false) { | 60 : weak_factory_(this), target_(target), is_recording_(false) { |
59 RegisterCommandHandler(devtools::Tracing::start::kName, | 61 RegisterCommandHandler(devtools::Tracing::start::kName, |
60 base::Bind(&DevToolsTracingHandler::OnStart, | 62 base::Bind(&DevToolsTracingHandler::OnStart, |
61 base::Unretained(this))); | 63 base::Unretained(this))); |
62 RegisterCommandHandler(devtools::Tracing::end::kName, | 64 RegisterCommandHandler(devtools::Tracing::end::kName, |
63 base::Bind(&DevToolsTracingHandler::OnEnd, | 65 base::Bind(&DevToolsTracingHandler::OnEnd, |
64 base::Unretained(this))); | 66 base::Unretained(this))); |
65 RegisterCommandHandler(devtools::Tracing::getCategories::kName, | 67 RegisterCommandHandler(devtools::Tracing::getCategories::kName, |
66 base::Bind(&DevToolsTracingHandler::OnGetCategories, | 68 base::Bind(&DevToolsTracingHandler::OnGetCategories, |
67 base::Unretained(this))); | 69 base::Unretained(this))); |
68 } | 70 } |
69 | 71 |
70 DevToolsTracingHandler::~DevToolsTracingHandler() { | 72 DevToolsTracingHandler::~DevToolsTracingHandler() { |
71 } | 73 } |
72 | 74 |
73 void DevToolsTracingHandler::BeginReadingRecordingResult( | |
74 const base::FilePath& path) { | |
75 BrowserThread::PostTask( | |
76 BrowserThread::FILE, FROM_HERE, | |
77 base::Bind(&ReadFile, path, | |
78 base::Bind(&DevToolsTracingHandler::ReadRecordingResult, | |
79 weak_factory_.GetWeakPtr()))); | |
80 } | |
81 | |
82 void DevToolsTracingHandler::ReadRecordingResult( | |
83 const scoped_refptr<base::RefCountedString>& trace_data) { | |
84 if (trace_data->data().size()) { | |
85 scoped_ptr<base::Value> trace_value(base::JSONReader::Read( | |
86 trace_data->data())); | |
87 base::DictionaryValue* dictionary = NULL; | |
88 bool ok = trace_value->GetAsDictionary(&dictionary); | |
89 DCHECK(ok); | |
90 base::ListValue* list = NULL; | |
91 ok = dictionary->GetList("traceEvents", &list); | |
92 DCHECK(ok); | |
93 std::string buffer; | |
94 for (size_t i = 0; i < list->GetSize(); ++i) { | |
95 std::string item; | |
96 base::Value* item_value; | |
97 list->Get(i, &item_value); | |
98 base::JSONWriter::Write(item_value, &item); | |
99 if (buffer.size()) | |
100 buffer.append(","); | |
101 buffer.append(item); | |
102 const size_t kMessageSizeThreshold = 1024 * 1024; | |
103 if (buffer.size() > kMessageSizeThreshold) { | |
104 OnTraceDataCollected(buffer); | |
105 buffer.clear(); | |
106 } | |
107 } | |
108 if (buffer.size()) | |
109 OnTraceDataCollected(buffer); | |
110 } | |
111 | |
112 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); | |
113 } | |
114 | |
115 void DevToolsTracingHandler::OnTraceDataCollected( | 75 void DevToolsTracingHandler::OnTraceDataCollected( |
116 const std::string& trace_fragment) { | 76 const std::string& trace_fragment) { |
117 // Hand-craft protocol notification message so we can substitute JSON | 77 // Hand-craft protocol notification message so we can substitute JSON |
118 // that we already got as string as a bare object, not a quoted string. | 78 // that we already got as string as a bare object, not a quoted string. |
119 std::string message = base::StringPrintf( | 79 std::string message = base::StringPrintf( |
120 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", | 80 "{ \"method\": \"%s\", \"params\": { \"%s\": [ %s ] } }", |
121 devtools::Tracing::dataCollected::kName, | 81 devtools::Tracing::dataCollected::kName, |
122 devtools::Tracing::dataCollected::kParamValue, | 82 devtools::Tracing::dataCollected::kParamValue, |
123 trace_fragment.c_str()); | 83 trace_fragment.c_str()); |
124 SendRawMessage(message); | 84 SendRawMessage(message); |
125 } | 85 } |
126 | 86 |
| 87 void DevToolsTracingHandler::OnTraceComplete() { |
| 88 SendNotification(devtools::Tracing::tracingComplete::kName, NULL); |
| 89 } |
| 90 |
127 base::debug::TraceOptions DevToolsTracingHandler::TraceOptionsFromString( | 91 base::debug::TraceOptions DevToolsTracingHandler::TraceOptionsFromString( |
128 const std::string& options) { | 92 const std::string& options) { |
129 std::vector<std::string> split; | 93 std::vector<std::string> split; |
130 std::vector<std::string>::iterator iter; | 94 std::vector<std::string>::iterator iter; |
131 base::debug::TraceOptions ret; | 95 base::debug::TraceOptions ret; |
132 | 96 |
133 base::SplitString(options, ',', &split); | 97 base::SplitString(options, ',', &split); |
134 for (iter = split.begin(); iter != split.end(); ++iter) { | 98 for (iter = split.begin(); iter != split.end(); ++iter) { |
135 if (*iter == kRecordUntilFull) { | 99 if (*iter == kRecordUntilFull) { |
136 ret.record_mode = base::debug::RECORD_UNTIL_FULL; | 100 ret.record_mode = base::debug::RECORD_UNTIL_FULL; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 SendNotification(devtools::Tracing::bufferUsage::kName, params); | 178 SendNotification(devtools::Tracing::bufferUsage::kName, params); |
215 } | 179 } |
216 | 180 |
217 scoped_refptr<DevToolsProtocol::Response> | 181 scoped_refptr<DevToolsProtocol::Response> |
218 DevToolsTracingHandler::OnEnd( | 182 DevToolsTracingHandler::OnEnd( |
219 scoped_refptr<DevToolsProtocol::Command> command) { | 183 scoped_refptr<DevToolsProtocol::Command> command) { |
220 // If inspected target is a render process Tracing.end will be handled by | 184 // If inspected target is a render process Tracing.end will be handled by |
221 // tracing agent in the renderer. | 185 // tracing agent in the renderer. |
222 if (target_ == Renderer) | 186 if (target_ == Renderer) |
223 return NULL; | 187 return NULL; |
224 DisableRecording( | 188 DisableRecording(false); |
225 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
226 weak_factory_.GetWeakPtr())); | |
227 return command->SuccessResponse(NULL); | 189 return command->SuccessResponse(NULL); |
228 } | 190 } |
229 | 191 |
230 void DevToolsTracingHandler::DisableRecording( | 192 void DevToolsTracingHandler::DisableRecording(bool abort) { |
231 const TracingController::TracingFileResultCallback& callback) { | |
232 is_recording_ = false; | 193 is_recording_ = false; |
233 buffer_usage_poll_timer_.reset(); | 194 buffer_usage_poll_timer_.reset(); |
234 TracingController::GetInstance()->DisableRecording(base::FilePath(), | 195 TracingController::GetInstance()->DisableRecording( |
235 callback); | 196 abort ? NULL : new DevToolsTraceSinkProxy(weak_factory_.GetWeakPtr())); |
236 } | 197 } |
237 | 198 |
238 void DevToolsTracingHandler::OnClientDetached() { | 199 void DevToolsTracingHandler::OnClientDetached() { |
239 if (is_recording_) | 200 if (is_recording_) |
240 DisableRecording(); | 201 DisableRecording(true); |
241 } | 202 } |
242 | 203 |
243 scoped_refptr<DevToolsProtocol::Response> | 204 scoped_refptr<DevToolsProtocol::Response> |
244 DevToolsTracingHandler::OnGetCategories( | 205 DevToolsTracingHandler::OnGetCategories( |
245 scoped_refptr<DevToolsProtocol::Command> command) { | 206 scoped_refptr<DevToolsProtocol::Command> command) { |
246 TracingController::GetInstance()->GetCategories( | 207 TracingController::GetInstance()->GetCategories( |
247 base::Bind(&DevToolsTracingHandler::OnCategoriesReceived, | 208 base::Bind(&DevToolsTracingHandler::OnCategoriesReceived, |
248 weak_factory_.GetWeakPtr(), | 209 weak_factory_.GetWeakPtr(), |
249 command)); | 210 command)); |
250 return command->AsyncResponsePromise(); | 211 return command->AsyncResponsePromise(); |
(...skipping 25 matching lines...) Expand all Loading... |
276 base::debug::CategoryFilter(category_filter), | 237 base::debug::CategoryFilter(category_filter), |
277 base::debug::TraceOptions(), | 238 base::debug::TraceOptions(), |
278 TracingController::EnableRecordingDoneCallback()); | 239 TracingController::EnableRecordingDoneCallback()); |
279 SendNotification(devtools::Tracing::started::kName, NULL); | 240 SendNotification(devtools::Tracing::started::kName, NULL); |
280 } | 241 } |
281 | 242 |
282 void DevToolsTracingHandler::DisableTracing() { | 243 void DevToolsTracingHandler::DisableTracing() { |
283 if (!is_recording_) | 244 if (!is_recording_) |
284 return; | 245 return; |
285 is_recording_ = false; | 246 is_recording_ = false; |
286 DisableRecording( | 247 DisableRecording(false); |
287 base::Bind(&DevToolsTracingHandler::BeginReadingRecordingResult, | |
288 weak_factory_.GetWeakPtr())); | |
289 } | 248 } |
290 | 249 |
291 | 250 |
292 } // namespace content | 251 } // namespace content |
OLD | NEW |