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 class ArcTraceAgentImpl : public ArcTraceAgent { |
| 22 public: |
| 23 // base::trace_event::TracingAgent overrides: |
| 24 std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
| 25 |
| 26 std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
| 27 |
| 28 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
| 29 const StartAgentTracingCallback& callback) override { |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 31 // delegate_ may be nullptr if ARC is not enabled on the system. In such |
| 32 // case, simply do nothing. |
| 33 if (!delegate_) { |
| 34 // Use PostTask as the convention of TraceAgent. The caller expects |
| 35 // callback to be called after this function returns. |
| 36 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 37 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
| 38 return; |
| 39 } |
| 40 |
| 41 delegate_->StartTracing(trace_config, |
| 42 base::Bind(callback, GetTracingAgentName())); |
| 43 } |
| 44 |
| 45 void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); |
| 47 if (!delegate_) { |
| 48 std::string no_data; |
| 49 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), |
| 50 base::RefCountedString::TakeString(&no_data)); |
| 51 return; |
| 52 } |
| 53 |
| 54 delegate_->StopTracing(base::Bind(&ArcTraceAgentImpl::OnStopAgentTracing, |
| 55 weak_ptr_factory_.GetWeakPtr(), |
| 56 callback)); |
| 57 } |
| 58 |
| 59 // ArcTraceAgent overrides: |
| 60 void SetDelegate(Delegate* delegate) override { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 delegate_ = delegate; |
| 63 } |
| 64 |
| 65 static ArcTraceAgentImpl* GetInstance() { |
| 66 return base::Singleton<ArcTraceAgentImpl>::get(); |
| 67 } |
| 68 |
| 69 private: |
| 70 // This allows constructor and destructor to be private and usable only |
| 71 // by the Singleton class. |
| 72 friend struct base::DefaultSingletonTraits<ArcTraceAgentImpl>; |
| 73 |
| 74 ArcTraceAgentImpl() : delegate_(nullptr), weak_ptr_factory_(this) {} |
| 75 |
| 76 ~ArcTraceAgentImpl() override = default; |
| 77 |
| 78 void OnStopAgentTracing(const StopAgentTracingCallback& callback, |
| 79 const std::string& report) { |
| 80 std::string data(report); |
| 81 callback.Run(kArcTracingAgentName, kArcTraceLabel, |
| 82 base::RefCountedString::TakeString(&data)); |
| 83 } |
| 84 |
| 85 Delegate* delegate_; // Owned by ArcServiceLauncher. |
| 86 base::ThreadChecker thread_checker_; |
| 87 base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); |
| 90 }; |
| 91 |
| 92 } // namespace |
| 93 |
| 94 // static |
| 95 ArcTraceAgent* ArcTraceAgent::GetInstance() { |
| 96 return ArcTraceAgentImpl::GetInstance(); |
| 97 } |
| 98 |
| 99 } // namespace chromeos |
OLD | NEW |