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 "chromeos/trace/arc_trace_agent.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/singleton.h" | |
| 11 #include "base/threading/thread_checker.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 | |
| 14 namespace chromeos { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 constexpr char kArcTracingAgentName[] = "arc"; | |
| 19 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; | |
| 20 | |
| 21 void OnStopTracing(bool success) { | |
| 22 if (!success) | |
| 23 LOG(WARNING) << "Failed to stop ARC tracing."; | |
| 24 } | |
| 25 | |
| 26 class ArcTraceAgentImpl : public ArcTraceAgent { | |
| 27 public: | |
| 28 // base::trace_event::TracingAgent overrides: | |
| 29 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | |
| 30 | |
| 31 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | |
| 32 | |
| 33 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | |
| 34 const StartAgentTracingCallback& callback) override { | |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 36 // delegate_ may be nullptr if ARC is not enabled on the system. In such | |
| 37 // case, simply do nothing. | |
| 38 if (!delegate_) { | |
| 39 // Use PostTask as the convention of TraceAgent. The caller expects | |
| 40 // callback to be called after this function returns. | |
| 41 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 42 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 delegate_->StartTracing(trace_config, | |
| 47 base::Bind(callback, GetTracingAgentName())); | |
| 48 } | |
| 49 | |
| 50 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 52 if (delegate_) | |
| 53 delegate_->StopTracing(base::Bind(OnStopTracing)); | |
| 54 | |
| 55 // Trace data is collect via systrace (debugd) in dev-mode. Simply | |
| 56 // return empty data here. | |
| 57 std::string no_data; | |
| 58 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
| 59 base::RefCountedString::TakeString(&no_data)); | |
| 60 } | |
| 61 | |
| 62 // ArcTraceAgent overrides: | |
| 63 void SetDelegate(Delegate* delegate) override { | |
| 64 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 65 delegate_ = delegate; | |
| 66 } | |
| 67 | |
| 68 static ArcTraceAgentImpl* GetInstance() { | |
| 69 return base::Singleton<ArcTraceAgentImpl>::get(); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 // This allows constructor and destructor to be private and usable only | |
| 74 // by the Singleton class. | |
| 75 friend struct base::DefaultSingletonTraits<ArcTraceAgentImpl>; | |
| 76 | |
| 77 ArcTraceAgentImpl() : delegate_(nullptr), weak_ptr_factory_(this) {} | |
|
Luis Héctor Chávez
2017/02/16 16:45:10
prefer initializing |delegate_| in L81.
Earl Ou
2017/02/17 02:12:59
Done.
| |
| 78 | |
| 79 ~ArcTraceAgentImpl() override = default; | |
| 80 | |
| 81 Delegate* delegate_; // Owned by ArcServiceLauncher. | |
| 82 base::ThreadChecker thread_checker_; | |
| 83 base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); | |
| 86 }; | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 // static | |
| 91 ArcTraceAgent* ArcTraceAgent::GetInstance() { | |
| 92 return ArcTraceAgentImpl::GetInstance(); | |
| 93 } | |
| 94 | |
| 95 } // namespace chromeos | |
| OLD | NEW |