Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/resource_coordinator/public/cpp/tracing/chrome_agent.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/ref_counted_memory.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/trace_event/trace_log.h" | |
| 12 #include "base/values.h" | |
| 13 #include "services/service_manager/public/cpp/connector.h" | |
| 14 | |
| 15 namespace tracing { | |
| 16 | |
| 17 // static | |
| 18 ChromeAgent* ChromeAgent::GetOrCreateInstance( | |
| 19 mojom::AgentRegistryPtr agent_registry) { | |
| 20 static ChromeAgent* instance = nullptr; | |
| 21 if (!instance) { | |
| 22 DCHECK(agent_registry); | |
| 23 instance = new ChromeAgent(std::move(agent_registry)); | |
| 24 } | |
| 25 return instance; | |
| 26 } | |
| 27 | |
| 28 ChromeAgent::ChromeAgent(mojom::AgentRegistryPtr agent_registry) | |
| 29 : binding_(this) { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 // agent_registry can be null in tests. The constructor is private and cannot | |
| 32 // be directly used in non-test scenarios. GetOrCreateInstance makes sure that | |
| 33 // the constructor is called with a non-null agent_registry. | |
| 34 if (!agent_registry) | |
| 35 return; | |
| 36 agent_registry->RegisterAgent(binding_.CreateInterfacePtrAndBind(), | |
| 37 "traceEvents", mojom::TraceDataType::ARRAY, | |
| 38 false /* supports_explicit_clock_sync */); | |
| 39 } | |
| 40 | |
| 41 ChromeAgent::~ChromeAgent() { | |
| 42 if (base::trace_event::TraceLog::GetInstance()->IsEnabled()) | |
| 43 StopAndFlush(); | |
| 44 } | |
| 45 | |
| 46 void ChromeAgent::AddMetadataGeneratorFunction( | |
| 47 MetadataGeneratorFunction generator) { | |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 49 metadata_generator_functions_.push_back(generator); | |
| 50 } | |
| 51 | |
| 52 void ChromeAgent::StartTracing(const std::string& config, | |
| 53 mojom::RecorderPtr recorder, | |
| 54 const StartTracingCallback& callback) { | |
| 55 DCHECK(!recorder_); | |
| 56 recorder_ = std::move(recorder); | |
| 57 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) { | |
| 58 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 59 base::trace_event::TraceConfig(config), | |
| 60 base::trace_event::TraceLog::RECORDING_MODE); | |
| 61 } | |
| 62 callback.Run(); | |
| 63 } | |
| 64 | |
| 65 void ChromeAgent::StopAndFlush() { | |
| 66 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 67 if (!recorder_) | |
| 68 return; | |
| 69 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 70 base::Bind(&ChromeAgent::SendChunk, base::Unretained(this))); | |
| 71 } | |
| 72 | |
| 73 void ChromeAgent::RequestClockSyncMarker( | |
| 74 const std::string& sync_id, | |
| 75 const RequestClockSyncMarkerCallback& callback) { | |
| 76 NOTREACHED() << "This agent does not support explicit clock sync"; | |
| 77 } | |
| 78 | |
| 79 void ChromeAgent::RequestBufferStatus( | |
| 80 const RequestBufferStatusCallback& callback) { | |
| 81 auto status = base::trace_event::TraceLog::GetInstance()->GetStatus(); | |
| 82 callback.Run(status.event_capacity, status.event_count); | |
| 83 } | |
| 84 | |
| 85 void ChromeAgent::GetCategories(const GetCategoriesCallback& callback) { | |
| 86 std::vector<std::string> category_vector; | |
| 87 base::trace_event::TraceLog::GetInstance()->GetKnownCategoryGroups( | |
| 88 &category_vector); | |
| 89 callback.Run(base::JoinString(category_vector, ",")); | |
| 90 } | |
| 91 | |
| 92 void ChromeAgent::SendChunk( | |
|
Primiano Tucci (use gerrit)
2017/05/16 04:45:11
minor comment: I'd probably call this OnTraceLogFl
chiniforooshan
2017/05/16 15:14:44
Done.
| |
| 93 const scoped_refptr<base::RefCountedString>& events_str, | |
| 94 bool has_more_events) { | |
| 95 DCHECK(recorder_); | |
|
Primiano Tucci (use gerrit)
2017/05/16 04:45:11
I think the pattern is to not add dhcecks if we ar
chiniforooshan
2017/05/16 15:14:45
Done.
| |
| 96 if (!events_str->data().empty()) | |
| 97 recorder_->AddChunk(events_str->data()); | |
| 98 if (!has_more_events) { | |
| 99 for (const auto& generator : metadata_generator_functions_) { | |
| 100 auto metadata = generator.Run(); | |
| 101 if (metadata) | |
| 102 recorder_->AddMetadata(std::move(metadata)); | |
| 103 } | |
| 104 recorder_.reset(); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 } // namespace tracing | |
| OLD | NEW |