Chromium Code Reviews| 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 |
| 309 const char* kMetadataWhitelist[] = { | |
| 310 "command_line", | |
| 311 "cpu-*", | |
| 312 "field-trials", | |
| 313 "gpu-*", | |
| 314 "highres-ticks", | |
| 315 "network-type", | |
| 316 "num-cpus", | |
| 317 "os-*", | |
| 318 "physical-memory", | |
| 319 "product-version", | |
| 320 "user-agent" | |
| 321 }; | |
| 322 | |
| 310 } // namespace | 323 } // namespace |
| 311 | 324 |
| 325 TracingController::TraceDataSink::TraceDataSink() | |
| 326 : requires_anonymized_metadata_(false) { | |
| 327 } | |
| 328 | |
| 312 void TracingController::TraceDataSink::AddMetadata( | 329 void TracingController::TraceDataSink::AddMetadata( |
| 313 const base::DictionaryValue& data) { | 330 const base::DictionaryValue& data) { |
| 314 metadata_.MergeDictionary(&data); | 331 metadata_.MergeDictionary(&data); |
| 315 } | 332 } |
| 316 | 333 |
| 317 const base::DictionaryValue& | 334 void TracingController::TraceDataSink::EnableMetadataFilter() { |
| 318 TracingController::TraceDataSink::GetMetadata() const { | 335 requires_anonymized_metadata_ = true; |
| 319 return metadata_; | 336 } |
| 337 | |
| 338 scoped_ptr<const base::DictionaryValue> | |
| 339 TracingController::TraceDataSink::GetMetadataCopy() const { | |
| 340 if (!requires_anonymized_metadata_) | |
| 341 return scoped_ptr<const base::DictionaryValue>(metadata_.DeepCopy()).Pass(); | |
| 342 | |
| 343 scoped_ptr<base::DictionaryValue> metadata_copy(new base::DictionaryValue); | |
| 344 for (base::DictionaryValue::Iterator it(metadata_); !it.IsAtEnd(); | |
| 345 it.Advance()) { | |
| 346 bool whitelisted = false; | |
| 347 for (auto key : kMetadataWhitelist) { | |
| 348 if (base::MatchPattern(it.key(), key)) { | |
| 349 whitelisted = true; | |
| 350 break; | |
| 351 } | |
| 352 } | |
| 353 if (whitelisted) | |
| 354 metadata_copy->Set(it.key(), it.value().DeepCopy()); | |
| 355 else | |
| 356 metadata_copy->SetString(it.key(), "__stripped__"); | |
| 357 } | |
| 358 return metadata_copy.Pass();; | |
|
shatch
2015/12/07 18:01:16
nit: ;;
Zhen Wang
2015/12/07 22:25:28
Done.
| |
| 320 } | 359 } |
| 321 | 360 |
| 322 scoped_refptr<TracingController::TraceDataSink> | 361 scoped_refptr<TracingController::TraceDataSink> |
| 323 TracingController::CreateStringSink( | 362 TracingController::CreateStringSink( |
| 324 const base::Callback<void(scoped_ptr<const base::DictionaryValue>, | 363 const base::Callback<void(scoped_ptr<const base::DictionaryValue>, |
| 325 base::RefCountedString*)>& callback) { | 364 base::RefCountedString*)>& callback) { |
| 326 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); | 365 return new StringTraceDataSink(new StringTraceDataEndpoint(callback)); |
| 327 } | 366 } |
| 328 | 367 |
| 329 scoped_refptr<TracingController::TraceDataSink> | 368 scoped_refptr<TracingController::TraceDataSink> |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 346 return new StringTraceDataEndpoint(callback); | 385 return new StringTraceDataEndpoint(callback); |
| 347 } | 386 } |
| 348 | 387 |
| 349 scoped_refptr<TracingController::TraceDataEndpoint> | 388 scoped_refptr<TracingController::TraceDataEndpoint> |
| 350 TracingController::CreateFileEndpoint(const base::FilePath& file_path, | 389 TracingController::CreateFileEndpoint(const base::FilePath& file_path, |
| 351 const base::Closure& callback) { | 390 const base::Closure& callback) { |
| 352 return new FileTraceDataEndpoint(file_path, callback); | 391 return new FileTraceDataEndpoint(file_path, callback); |
| 353 } | 392 } |
| 354 | 393 |
| 355 } // namespace content | 394 } // namespace content |
| OLD | NEW |