| OLD | NEW |
| 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 "content/browser/tracing/tracing_controller_impl.h" | 4 #include "content/browser/tracing/tracing_controller_impl.h" |
| 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/strings/pattern.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "third_party/zlib/zlib.h" | 11 #include "third_party/zlib/zlib.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 class StringTraceDataEndpoint : public TracingController::TraceDataEndpoint { | 17 class StringTraceDataEndpoint : public TracingController::TraceDataEndpoint { |
| 17 public: | 18 public: |
| 18 typedef base::Callback<void(scoped_ptr<const base::DictionaryValue>, | 19 typedef base::Callback<void(scoped_ptr<const base::DictionaryValue>, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 } | 137 } |
| 137 | 138 |
| 138 void SetPowerTrace(const std::string& data) override { power_trace_ = data; } | 139 void SetPowerTrace(const std::string& data) override { power_trace_ = data; } |
| 139 | 140 |
| 140 void Close() override { | 141 void Close() override { |
| 141 AddTraceChunkAndPassToEndpoint("]"); | 142 AddTraceChunkAndPassToEndpoint("]"); |
| 142 if (!system_trace_.empty()) | 143 if (!system_trace_.empty()) |
| 143 AddTraceChunkAndPassToEndpoint(",\"systemTraceEvents\": " + | 144 AddTraceChunkAndPassToEndpoint(",\"systemTraceEvents\": " + |
| 144 system_trace_); | 145 system_trace_); |
| 145 std::string metadataJSON; | 146 std::string metadataJSON; |
| 146 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && | 147 if (base::JSONWriter::Write(*GetMetadataCopy(), &metadataJSON) && |
| 147 !metadataJSON.empty()) | 148 !metadataJSON.empty()) |
| 148 AddTraceChunkAndPassToEndpoint(",\"metadata\": " + metadataJSON); | 149 AddTraceChunkAndPassToEndpoint(",\"metadata\": " + metadataJSON); |
| 149 if (!power_trace_.empty()) { | 150 if (!power_trace_.empty()) { |
| 150 AddTraceChunkAndPassToEndpoint(",\"powerTraceAsString\": " + | 151 AddTraceChunkAndPassToEndpoint(",\"powerTraceAsString\": " + |
| 151 power_trace_); | 152 power_trace_); |
| 152 } | 153 } |
| 153 | 154 |
| 154 AddTraceChunkAndPassToEndpoint("}"); | 155 AddTraceChunkAndPassToEndpoint("}"); |
| 155 | 156 |
| 156 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); | 157 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), trace_); |
| 157 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), trace_); | |
| 158 } | 158 } |
| 159 | 159 |
| 160 private: | 160 private: |
| 161 ~StringTraceDataSink() override {} | 161 ~StringTraceDataSink() override {} |
| 162 | 162 |
| 163 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 163 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; |
| 164 std::string trace_; | 164 std::string trace_; |
| 165 std::string system_trace_; | 165 std::string system_trace_; |
| 166 std::string power_trace_; | 166 std::string power_trace_; |
| 167 | 167 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 | 271 |
| 272 if (compressed_trace_data_.empty()) | 272 if (compressed_trace_data_.empty()) |
| 273 AddTraceChunkAndCompressOnFileThread("{\"traceEvents\":[", false); | 273 AddTraceChunkAndCompressOnFileThread("{\"traceEvents\":[", false); |
| 274 | 274 |
| 275 AddTraceChunkAndCompressOnFileThread("]", false); | 275 AddTraceChunkAndCompressOnFileThread("]", false); |
| 276 if (!system_trace_.empty()) { | 276 if (!system_trace_.empty()) { |
| 277 AddTraceChunkAndCompressOnFileThread( | 277 AddTraceChunkAndCompressOnFileThread( |
| 278 ",\"systemTraceEvents\": " + system_trace_, false); | 278 ",\"systemTraceEvents\": " + system_trace_, false); |
| 279 } | 279 } |
| 280 std::string metadataJSON; | 280 std::string metadataJSON; |
| 281 if (base::JSONWriter::Write(GetMetadata(), &metadataJSON) && | 281 if (base::JSONWriter::Write(*GetMetadataCopy(), &metadataJSON) && |
| 282 !metadataJSON.empty()) { | 282 !metadataJSON.empty()) { |
| 283 AddTraceChunkAndCompressOnFileThread(",\"metadata\": " + metadataJSON, | 283 AddTraceChunkAndCompressOnFileThread(",\"metadata\": " + metadataJSON, |
| 284 false); | 284 false); |
| 285 } | 285 } |
| 286 if (!power_trace_.empty()) { | 286 if (!power_trace_.empty()) { |
| 287 AddTraceChunkAndCompressOnFileThread( | 287 AddTraceChunkAndCompressOnFileThread( |
| 288 ",\"powerTraceAsString\": " + power_trace_, false); | 288 ",\"powerTraceAsString\": " + power_trace_, false); |
| 289 } | 289 } |
| 290 AddTraceChunkAndCompressOnFileThread("}", true); | 290 AddTraceChunkAndCompressOnFileThread("}", true); |
| 291 | 291 |
| 292 deflateEnd(stream_.get()); | 292 deflateEnd(stream_.get()); |
| 293 stream_.reset(); | 293 stream_.reset(); |
| 294 | 294 |
| 295 scoped_ptr<const base::DictionaryValue> metadata(GetMetadata().DeepCopy()); | 295 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), |
| 296 endpoint_->ReceiveTraceFinalContents(metadata.Pass(), | |
| 297 compressed_trace_data_); | 296 compressed_trace_data_); |
| 298 } | 297 } |
| 299 | 298 |
| 300 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 299 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; |
| 301 scoped_ptr<z_stream> stream_; | 300 scoped_ptr<z_stream> stream_; |
| 302 bool already_tried_open_; | 301 bool already_tried_open_; |
| 303 std::string compressed_trace_data_; | 302 std::string compressed_trace_data_; |
| 304 std::string system_trace_; | 303 std::string system_trace_; |
| 305 std::string power_trace_; | 304 std::string power_trace_; |
| 306 | 305 |
| 307 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); | 306 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); |
| 308 }; | 307 }; |
| 309 | 308 |
| 310 } // namespace | 309 } // namespace |
| 311 | 310 |
| 311 TracingController::TraceDataSink::TraceDataSink() {} |
| 312 |
| 313 TracingController::TraceDataSink::~TraceDataSink() {} |
| 314 |
| 312 void TracingController::TraceDataSink::AddMetadata( | 315 void TracingController::TraceDataSink::AddMetadata( |
| 313 const base::DictionaryValue& data) { | 316 const base::DictionaryValue& data) { |
| 314 metadata_.MergeDictionary(&data); | 317 metadata_.MergeDictionary(&data); |
| 315 } | 318 } |
| 316 | 319 |
| 317 const base::DictionaryValue& | 320 void TracingController::TraceDataSink::SetMetadataFilterPredicate( |
| 318 TracingController::TraceDataSink::GetMetadata() const { | 321 const MetadataFilterPredicate& metadata_filter_predicate) { |
| 319 return metadata_; | 322 metadata_filter_predicate_ = metadata_filter_predicate; |
| 323 } |
| 324 |
| 325 scoped_ptr<const base::DictionaryValue> |
| 326 TracingController::TraceDataSink::GetMetadataCopy() const { |
| 327 if (metadata_filter_predicate_.is_null()) |
| 328 return scoped_ptr<const base::DictionaryValue>(metadata_.DeepCopy()).Pass(); |
| 329 |
| 330 scoped_ptr<base::DictionaryValue> metadata_copy(new base::DictionaryValue); |
| 331 for (base::DictionaryValue::Iterator it(metadata_); !it.IsAtEnd(); |
| 332 it.Advance()) { |
| 333 if (metadata_filter_predicate_.Run(it.key())) |
| 334 metadata_copy->Set(it.key(), it.value().DeepCopy()); |
| 335 else |
| 336 metadata_copy->SetString(it.key(), "__stripped__"); |
| 337 } |
| 338 return metadata_copy.Pass(); |
| 320 } | 339 } |
| 321 | 340 |
| 322 scoped_refptr<TracingController::TraceDataSink> | 341 scoped_refptr<TracingController::TraceDataSink> |
| 323 TracingController::CreateStringSink( | 342 TracingController::CreateStringSink( |
| 324 const base::Callback<void(scoped_ptr<const base::DictionaryValue>, | 343 const base::Callback<void(scoped_ptr<const base::DictionaryValue>, |
| 325 base::RefCountedString*)>& callback) { | 344 base::RefCountedString*)>& callback) { |
| 326 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); | 345 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); |
| 327 } | 346 } |
| 328 | 347 |
| 329 scoped_refptr<TracingController::TraceDataSink> | 348 scoped_refptr<TracingController::TraceDataSink> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 346 return new StringTraceDataEndpoint(callback); | 365 return new StringTraceDataEndpoint(callback); |
| 347 } | 366 } |
| 348 | 367 |
| 349 scoped_refptr<TracingController::TraceDataEndpoint> | 368 scoped_refptr<TracingController::TraceDataEndpoint> |
| 350 TracingController::CreateFileEndpoint(const base::FilePath& file_path, | 369 TracingController::CreateFileEndpoint(const base::FilePath& file_path, |
| 351 const base::Closure& callback) { | 370 const base::Closure& callback) { |
| 352 return new FileTraceDataEndpoint(file_path, callback); | 371 return new FileTraceDataEndpoint(file_path, callback); |
| 353 } | 372 } |
| 354 | 373 |
| 355 } // namespace content | 374 } // namespace content |
| OLD | NEW |