| 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 <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/strings/pattern.h" | 10 #include "base/strings/pattern.h" |
| 11 #include "content/browser/tracing/tracing_controller_impl.h" | 11 #include "content/browser/tracing/tracing_controller_impl.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "third_party/zlib/zlib.h" | 13 #include "third_party/zlib/zlib.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const char kChromeTraceLabel[] = "traceEvents"; | 19 const char kChromeTraceLabel[] = "traceEvents"; |
| 20 const char kMetadataTraceLabel[] = "metadata"; | 20 const char kMetadataTraceLabel[] = "metadata"; |
| 21 | 21 |
| 22 class StringTraceDataEndpoint : public TracingController::TraceDataEndpoint { | 22 class StringTraceDataEndpoint : public TraceDataEndpoint { |
| 23 public: | 23 public: |
| 24 typedef base::Callback<void(std::unique_ptr<const base::DictionaryValue>, | 24 typedef base::Callback<void(std::unique_ptr<const base::DictionaryValue>, |
| 25 base::RefCountedString*)> | 25 base::RefCountedString*)> |
| 26 CompletionCallback; | 26 CompletionCallback; |
| 27 | 27 |
| 28 explicit StringTraceDataEndpoint(CompletionCallback callback) | 28 explicit StringTraceDataEndpoint(CompletionCallback callback) |
| 29 : completion_callback_(callback) {} | 29 : completion_callback_(callback) {} |
| 30 | 30 |
| 31 void ReceiveTraceFinalContents( | 31 void ReceiveTraceFinalContents( |
| 32 std::unique_ptr<const base::DictionaryValue> metadata, | 32 std::unique_ptr<const base::DictionaryValue> metadata, |
| 33 const std::string& contents) override { | 33 const std::string& contents) override { |
| 34 std::string tmp = contents; | 34 std::string tmp = contents; |
| 35 scoped_refptr<base::RefCountedString> str = | 35 scoped_refptr<base::RefCountedString> str = |
| 36 base::RefCountedString::TakeString(&tmp); | 36 base::RefCountedString::TakeString(&tmp); |
| 37 | 37 |
| 38 BrowserThread::PostTask( | 38 BrowserThread::PostTask( |
| 39 BrowserThread::UI, FROM_HERE, | 39 BrowserThread::UI, FROM_HERE, |
| 40 base::Bind(completion_callback_, base::Passed(std::move(metadata)), | 40 base::Bind(completion_callback_, base::Passed(std::move(metadata)), |
| 41 base::RetainedRef(str))); | 41 base::RetainedRef(str))); |
| 42 } | 42 } |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 ~StringTraceDataEndpoint() override {} | 45 ~StringTraceDataEndpoint() override {} |
| 46 | 46 |
| 47 CompletionCallback completion_callback_; | 47 CompletionCallback completion_callback_; |
| 48 | 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(StringTraceDataEndpoint); | 49 DISALLOW_COPY_AND_ASSIGN(StringTraceDataEndpoint); |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class FileTraceDataEndpoint : public TracingController::TraceDataEndpoint { | 52 class FileTraceDataEndpoint : public TraceDataEndpoint { |
| 53 public: | 53 public: |
| 54 explicit FileTraceDataEndpoint(const base::FilePath& trace_file_path, | 54 explicit FileTraceDataEndpoint(const base::FilePath& trace_file_path, |
| 55 const base::Closure& callback) | 55 const base::Closure& callback) |
| 56 : file_path_(trace_file_path), | 56 : file_path_(trace_file_path), |
| 57 completion_callback_(callback), | 57 completion_callback_(callback), |
| 58 file_(NULL) {} | 58 file_(NULL) {} |
| 59 | 59 |
| 60 void ReceiveTraceChunk(const std::string& chunk) override { | 60 void ReceiveTraceChunk(const std::string& chunk) override { |
| 61 std::string tmp = chunk; | 61 std::string tmp = chunk; |
| 62 scoped_refptr<base::RefCountedString> chunk_ptr = | 62 scoped_refptr<base::RefCountedString> chunk_ptr = |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 base::FilePath file_path_; | 111 base::FilePath file_path_; |
| 112 base::Closure completion_callback_; | 112 base::Closure completion_callback_; |
| 113 FILE* file_; | 113 FILE* file_; |
| 114 | 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(FileTraceDataEndpoint); | 115 DISALLOW_COPY_AND_ASSIGN(FileTraceDataEndpoint); |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 class StringTraceDataSink : public TracingController::TraceDataSink { | 118 class StringTraceDataSink : public TracingController::TraceDataSink { |
| 119 public: | 119 public: |
| 120 explicit StringTraceDataSink( | 120 explicit StringTraceDataSink(scoped_refptr<TraceDataEndpoint> endpoint) |
| 121 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) | |
| 122 : endpoint_(endpoint) {} | 121 : endpoint_(endpoint) {} |
| 123 | 122 |
| 124 void AddTraceChunk(const std::string& chunk) override { | 123 void AddTraceChunk(const std::string& chunk) override { |
| 125 std::string trace_string; | 124 std::string trace_string; |
| 126 if (trace_.empty()) | 125 if (trace_.empty()) |
| 127 trace_string = "{\"" + std::string(kChromeTraceLabel) + "\":["; | 126 trace_string = "{\"" + std::string(kChromeTraceLabel) + "\":["; |
| 128 else | 127 else |
| 129 trace_string = ","; | 128 trace_string = ","; |
| 130 trace_string += chunk; | 129 trace_string += chunk; |
| 131 | 130 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 152 } | 151 } |
| 153 | 152 |
| 154 AddTraceChunkAndPassToEndpoint("}"); | 153 AddTraceChunkAndPassToEndpoint("}"); |
| 155 | 154 |
| 156 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), trace_); | 155 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), trace_); |
| 157 } | 156 } |
| 158 | 157 |
| 159 private: | 158 private: |
| 160 ~StringTraceDataSink() override {} | 159 ~StringTraceDataSink() override {} |
| 161 | 160 |
| 162 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 161 scoped_refptr<TraceDataEndpoint> endpoint_; |
| 163 std::string trace_; | 162 std::string trace_; |
| 164 | 163 |
| 165 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); | 164 DISALLOW_COPY_AND_ASSIGN(StringTraceDataSink); |
| 166 }; | 165 }; |
| 167 | 166 |
| 168 class CompressedStringTraceDataSink : public TracingController::TraceDataSink { | 167 class CompressedStringTraceDataSink : public TracingController::TraceDataSink { |
| 169 public: | 168 public: |
| 170 explicit CompressedStringTraceDataSink( | 169 explicit CompressedStringTraceDataSink( |
| 171 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) | 170 scoped_refptr<TraceDataEndpoint> endpoint) |
| 172 : endpoint_(endpoint), already_tried_open_(false) {} | 171 : endpoint_(endpoint), already_tried_open_(false) {} |
| 173 | 172 |
| 174 void AddTraceChunk(const std::string& chunk) override { | 173 void AddTraceChunk(const std::string& chunk) override { |
| 175 std::string tmp = chunk; | 174 std::string tmp = chunk; |
| 176 scoped_refptr<base::RefCountedString> chunk_ptr = | 175 scoped_refptr<base::RefCountedString> chunk_ptr = |
| 177 base::RefCountedString::TakeString(&tmp); | 176 base::RefCountedString::TakeString(&tmp); |
| 178 BrowserThread::PostTask( | 177 BrowserThread::PostTask( |
| 179 BrowserThread::FILE, FROM_HERE, | 178 BrowserThread::FILE, FROM_HERE, |
| 180 base::Bind(&CompressedStringTraceDataSink::AddTraceChunkOnFileThread, | 179 base::Bind(&CompressedStringTraceDataSink::AddTraceChunkOnFileThread, |
| 181 this, chunk_ptr)); | 180 this, chunk_ptr)); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 279 } |
| 281 AddTraceChunkAndCompressOnFileThread("}", true); | 280 AddTraceChunkAndCompressOnFileThread("}", true); |
| 282 | 281 |
| 283 deflateEnd(stream_.get()); | 282 deflateEnd(stream_.get()); |
| 284 stream_.reset(); | 283 stream_.reset(); |
| 285 | 284 |
| 286 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), | 285 endpoint_->ReceiveTraceFinalContents(GetMetadataCopy(), |
| 287 compressed_trace_data_); | 286 compressed_trace_data_); |
| 288 } | 287 } |
| 289 | 288 |
| 290 scoped_refptr<TracingController::TraceDataEndpoint> endpoint_; | 289 scoped_refptr<TraceDataEndpoint> endpoint_; |
| 291 std::unique_ptr<z_stream> stream_; | 290 std::unique_ptr<z_stream> stream_; |
| 292 bool already_tried_open_; | 291 bool already_tried_open_; |
| 293 std::string compressed_trace_data_; | 292 std::string compressed_trace_data_; |
| 294 | 293 |
| 295 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); | 294 DISALLOW_COPY_AND_ASSIGN(CompressedStringTraceDataSink); |
| 296 }; | 295 }; |
| 297 | 296 |
| 298 } // namespace | 297 } // namespace |
| 299 | 298 |
| 300 TracingController::TraceDataSink::TraceDataSink() {} | 299 TracingController::TraceDataSink::TraceDataSink() {} |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 } | 341 } |
| 343 | 342 |
| 344 scoped_refptr<TracingController::TraceDataSink> | 343 scoped_refptr<TracingController::TraceDataSink> |
| 345 TracingController::CreateStringSink( | 344 TracingController::CreateStringSink( |
| 346 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, | 345 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, |
| 347 base::RefCountedString*)>& callback) { | 346 base::RefCountedString*)>& callback) { |
| 348 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); | 347 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); |
| 349 } | 348 } |
| 350 | 349 |
| 351 scoped_refptr<TracingController::TraceDataSink> | 350 scoped_refptr<TracingController::TraceDataSink> |
| 352 TracingController::CreateCompressedStringSink( | 351 TracingControllerImpl::CreateCompressedStringSink( |
| 353 scoped_refptr<TracingController::TraceDataEndpoint> endpoint) { | 352 scoped_refptr<TraceDataEndpoint> endpoint) { |
| 354 return new CompressedStringTraceDataSink(endpoint); | 353 return new CompressedStringTraceDataSink(endpoint); |
| 355 } | 354 } |
| 356 | 355 |
| 357 scoped_refptr<TracingController::TraceDataSink> | 356 scoped_refptr<TracingController::TraceDataSink> |
| 358 TracingController::CreateFileSink(const base::FilePath& file_path, | 357 TracingController::CreateFileSink(const base::FilePath& file_path, |
| 359 const base::Closure& callback) { | 358 const base::Closure& callback) { |
| 360 return new StringTraceDataSink( | 359 return new StringTraceDataSink( |
| 361 CreateFileEndpoint(file_path, callback)); | 360 TracingControllerImpl::CreateFileEndpoint(file_path, callback)); |
| 362 } | 361 } |
| 363 | 362 |
| 364 scoped_refptr<TracingController::TraceDataEndpoint> | 363 scoped_refptr<TraceDataEndpoint> TracingControllerImpl::CreateCallbackEndpoint( |
| 365 TracingController::CreateCallbackEndpoint( | |
| 366 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, | 364 const base::Callback<void(std::unique_ptr<const base::DictionaryValue>, |
| 367 base::RefCountedString*)>& callback) { | 365 base::RefCountedString*)>& callback) { |
| 368 return new StringTraceDataEndpoint(callback); | 366 return new StringTraceDataEndpoint(callback); |
| 369 } | 367 } |
| 370 | 368 |
| 371 scoped_refptr<TracingController::TraceDataEndpoint> | 369 scoped_refptr<TraceDataEndpoint> TracingControllerImpl::CreateFileEndpoint( |
| 372 TracingController::CreateFileEndpoint(const base::FilePath& file_path, | 370 const base::FilePath& file_path, |
| 373 const base::Closure& callback) { | 371 const base::Closure& callback) { |
| 374 return new FileTraceDataEndpoint(file_path, callback); | 372 return new FileTraceDataEndpoint(file_path, callback); |
| 375 } | 373 } |
| 376 | 374 |
| 377 } // namespace content | 375 } // namespace content |
| OLD | NEW |