| 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 "content/browser/tracing/arc_tracing_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 content { |
| 16 |
| 17 namespace { |
| 18 |
| 19 constexpr char kArcTracingAgentName[] = "arc"; |
| 20 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
| 21 |
| 22 void OnStopTracing(bool success) { |
| 23 DLOG_IF(WARNING, !success) << "Failed to stop ARC tracing."; |
| 24 } |
| 25 |
| 26 class ArcTracingAgentImpl : public ArcTracingAgent { |
| 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 TracingAgent. 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 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 59 FROM_HERE, |
| 60 base::Bind(callback, GetTracingAgentName(), GetTraceEventLabel(), |
| 61 base::RefCountedString::TakeString(&no_data))); |
| 62 } |
| 63 |
| 64 // ArcTracingAgent overrides: |
| 65 void SetDelegate(Delegate* delegate) override { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 67 delegate_ = delegate; |
| 68 } |
| 69 |
| 70 static ArcTracingAgentImpl* GetInstance() { |
| 71 return base::Singleton<ArcTracingAgentImpl>::get(); |
| 72 } |
| 73 |
| 74 private: |
| 75 // This allows constructor and destructor to be private and usable only |
| 76 // by the Singleton class. |
| 77 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; |
| 78 |
| 79 ArcTracingAgentImpl() = default; |
| 80 ~ArcTracingAgentImpl() override = default; |
| 81 |
| 82 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
| 83 base::ThreadChecker thread_checker_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); |
| 86 }; |
| 87 |
| 88 } // namespace |
| 89 |
| 90 // static |
| 91 ArcTracingAgent* ArcTracingAgent::GetInstance() { |
| 92 return ArcTracingAgentImpl::GetInstance(); |
| 93 } |
| 94 |
| 95 ArcTracingAgent::ArcTracingAgent() = default; |
| 96 ArcTracingAgent::~ArcTracingAgent() = default; |
| 97 |
| 98 ArcTracingAgent::Delegate::~Delegate() = default; |
| 99 |
| 100 } // namespace content |
| OLD | NEW |