| 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 <string> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/singleton.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 namespace { |
| 18 |
| 19 constexpr char kArcTracingAgentName[] = "arc"; |
| 20 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
| 21 |
| 22 void OnStopTracing(bool success) { |
| 23 LOG_IF(WARNING, !success) << "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() = default; |
| 78 ~ArcTraceAgentImpl() override = default; |
| 79 |
| 80 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
| 81 base::ThreadChecker thread_checker_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 // static |
| 89 ArcTraceAgent* ArcTraceAgent::GetInstance() { |
| 90 return ArcTraceAgentImpl::GetInstance(); |
| 91 } |
| 92 |
| 93 ArcTraceAgent::~ArcTraceAgent() = default; |
| 94 |
| 95 ArcTraceAgent::Delegate::~Delegate() = default; |
| 96 |
| 97 } // namespace chromeos |
| OLD | NEW |