Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(967)

Side by Side Diff: content/browser/tracing/tracing_controller_impl_data_sinks.cc

Issue 2833873003: WIP: The tracing service prototype
Patch Set: sync Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/browser/tracing/tracing_controller_impl.cc ('k') | content/child/child_thread_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #include <utility> 4 #include <utility>
5 5
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/strings/pattern.h" 11 #include "base/strings/pattern.h"
12 #include "content/browser/tracing/tracing_controller_impl.h" 12 #include "content/browser/tracing/tracing_controller_impl.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "third_party/zlib/zlib.h" 14 #include "third_party/zlib/zlib.h"
15 15
16 namespace content { 16 namespace content {
17 17
18 namespace { 18 namespace {
19 19
20 const char kChromeTraceLabel[] = "traceEvents";
21 const char kMetadataTraceLabel[] = "metadata";
22
23 class StringTraceDataEndpoint : public TraceDataEndpoint { 20 class StringTraceDataEndpoint : public TraceDataEndpoint {
24 public: 21 public:
25 typedef base::Callback<void(std::unique_ptr<const base::DictionaryValue>, 22 typedef base::Callback<void(std::unique_ptr<const base::DictionaryValue>,
26 base::RefCountedString*)> 23 base::RefCountedString*)>
27 CompletionCallback; 24 CompletionCallback;
28 25
29 explicit StringTraceDataEndpoint(CompletionCallback callback) 26 explicit StringTraceDataEndpoint(CompletionCallback callback)
30 : completion_callback_(callback) {} 27 : completion_callback_(callback) {}
31 28
32 void ReceiveTraceFinalContents( 29 void ReceiveTraceFinalContents(
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 private: 133 private:
137 std::map<std::string, std::string> additional_tracing_agent_trace_; 134 std::map<std::string, std::string> additional_tracing_agent_trace_;
138 std::unique_ptr<base::DictionaryValue> metadata_; 135 std::unique_ptr<base::DictionaryValue> metadata_;
139 136
140 DISALLOW_COPY_AND_ASSIGN(TraceDataSinkImplBase); 137 DISALLOW_COPY_AND_ASSIGN(TraceDataSinkImplBase);
141 }; 138 };
142 139
143 class JSONTraceDataSink : public TraceDataSinkImplBase { 140 class JSONTraceDataSink : public TraceDataSinkImplBase {
144 public: 141 public:
145 explicit JSONTraceDataSink(scoped_refptr<TraceDataEndpoint> endpoint) 142 explicit JSONTraceDataSink(scoped_refptr<TraceDataEndpoint> endpoint)
146 : endpoint_(endpoint), had_received_first_chunk_(false) {} 143 : endpoint_(endpoint) {}
147 144
148 void AddTraceChunk(const std::string& chunk) override { 145 void AddTraceChunk(const std::string& chunk) override {
149 std::string trace_string; 146 endpoint_->ReceiveTraceChunk(base::MakeUnique<std::string>(chunk));
150 if (had_received_first_chunk_)
151 trace_string = ",";
152 else
153 trace_string = "{\"" + std::string(kChromeTraceLabel) + "\":[";
154 trace_string += chunk;
155 had_received_first_chunk_ = true;
156
157 endpoint_->ReceiveTraceChunk(base::MakeUnique<std::string>(trace_string));
158 } 147 }
159 148
160 void Close() override { 149 void Close() override {
161 endpoint_->ReceiveTraceChunk(base::MakeUnique<std::string>("]"));
162
163 for (auto const &it : GetAgentTrace())
164 endpoint_->ReceiveTraceChunk(
165 base::MakeUnique<std::string>(",\"" + it.first + "\": " + it.second));
166
167 std::unique_ptr<base::DictionaryValue> metadata(TakeMetadata()); 150 std::unique_ptr<base::DictionaryValue> metadata(TakeMetadata());
168 std::string metadataJSON;
169
170 if (base::JSONWriter::Write(*metadata, &metadataJSON) &&
171 !metadataJSON.empty()) {
172 endpoint_->ReceiveTraceChunk(base::MakeUnique<std::string>(
173 ",\"" + std::string(kMetadataTraceLabel) + "\": " + metadataJSON));
174 }
175
176 endpoint_->ReceiveTraceChunk(base::MakeUnique<std::string>("}"));
177 endpoint_->ReceiveTraceFinalContents(std::move(metadata)); 151 endpoint_->ReceiveTraceFinalContents(std::move(metadata));
178 } 152 }
179 153
180 private: 154 private:
181 ~JSONTraceDataSink() override {} 155 ~JSONTraceDataSink() override {}
182 156
183 scoped_refptr<TraceDataEndpoint> endpoint_; 157 scoped_refptr<TraceDataEndpoint> endpoint_;
184 bool had_received_first_chunk_;
185 DISALLOW_COPY_AND_ASSIGN(JSONTraceDataSink); 158 DISALLOW_COPY_AND_ASSIGN(JSONTraceDataSink);
186 }; 159 };
187 160
188 class CompressedTraceDataEndpoint : public TraceDataEndpoint { 161 class CompressedTraceDataEndpoint : public TraceDataEndpoint {
189 public: 162 public:
190 explicit CompressedTraceDataEndpoint( 163 explicit CompressedTraceDataEndpoint(
191 scoped_refptr<TraceDataEndpoint> endpoint) 164 scoped_refptr<TraceDataEndpoint> endpoint)
192 : endpoint_(endpoint), already_tried_open_(false) {} 165 : endpoint_(endpoint), already_tried_open_(false) {}
193 166
194 void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) override { 167 void ReceiveTraceChunk(std::unique_ptr<std::string> chunk) override {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return new StringTraceDataEndpoint(callback); 310 return new StringTraceDataEndpoint(callback);
338 } 311 }
339 312
340 scoped_refptr<TracingController::TraceDataSink> 313 scoped_refptr<TracingController::TraceDataSink>
341 TracingControllerImpl::CreateJSONSink( 314 TracingControllerImpl::CreateJSONSink(
342 scoped_refptr<TraceDataEndpoint> endpoint) { 315 scoped_refptr<TraceDataEndpoint> endpoint) {
343 return new JSONTraceDataSink(endpoint); 316 return new JSONTraceDataSink(endpoint);
344 } 317 }
345 318
346 } // namespace content 319 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/tracing/tracing_controller_impl.cc ('k') | content/child/child_thread_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698