Chromium Code Reviews| Index: chromeos/trace/arc_trace_agent.cc |
| diff --git a/chromeos/trace/arc_trace_agent.cc b/chromeos/trace/arc_trace_agent.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5cc631823abf32d4ae4c47fb405bdd4d1ac604d9 |
| --- /dev/null |
| +++ b/chromeos/trace/arc_trace_agent.cc |
| @@ -0,0 +1,101 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/trace/arc_trace_agent.h" |
| + |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace { |
| + |
| +constexpr char kArcTracingAgentName[] = "arc"; |
| +constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
| + |
| +void OnStopTracing(bool success) { |
| + if (!success) |
| + LOG(WARNING) << "Failed to stop ARC tracing."; |
|
Luis Héctor Chávez
2017/02/27 20:47:18
nit: LOG_IF(WARNING, !success)
Earl Ou
2017/03/02 07:57:59
Done.
|
| +} |
| + |
| +class ArcTraceAgentImpl : public ArcTraceAgent { |
| + public: |
| + // base::trace_event::TracingAgent overrides: |
| + std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
| + |
| + std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
| + |
| + void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
| + const StartAgentTracingCallback& callback) override { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + // delegate_ may be nullptr if ARC is not enabled on the system. In such |
| + // case, simply do nothing. |
| + if (!delegate_) { |
| + // Use PostTask as the convention of TraceAgent. The caller expects |
| + // callback to be called after this function returns. |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
| + return; |
| + } |
| + |
| + delegate_->StartTracing(trace_config, |
| + base::Bind(callback, GetTracingAgentName())); |
| + } |
| + |
| + void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (delegate_) |
| + delegate_->StopTracing(base::Bind(OnStopTracing)); |
| + |
| + // Trace data is collect via systrace (debugd) in dev-mode. Simply |
| + // return empty data here. |
| + std::string no_data; |
| + callback.Run(GetTracingAgentName(), GetTraceEventLabel(), |
| + base::RefCountedString::TakeString(&no_data)); |
| + } |
| + |
| + // ArcTraceAgent overrides: |
| + void SetDelegate(Delegate* delegate) override { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + delegate_ = delegate; |
| + } |
| + |
| + static ArcTraceAgentImpl* GetInstance() { |
| + return base::Singleton<ArcTraceAgentImpl>::get(); |
| + } |
| + |
| + private: |
| + // This allows constructor and destructor to be private and usable only |
| + // by the Singleton class. |
| + friend struct base::DefaultSingletonTraits<ArcTraceAgentImpl>; |
| + |
| + ArcTraceAgentImpl() : weak_ptr_factory_(this) {} |
| + |
| + ~ArcTraceAgentImpl() override = default; |
| + |
| + Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
| + base::ThreadChecker thread_checker_; |
| + base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); |
| +}; |
| + |
| +} // namespace |
| + |
| +// static |
| +ArcTraceAgent* ArcTraceAgent::GetInstance() { |
| + return ArcTraceAgentImpl::GetInstance(); |
| +} |
| + |
| +ArcTraceAgent::~ArcTraceAgent() = default; |
| + |
| +ArcTraceAgent::Delegate::~Delegate() = default; |
| + |
| +} // namespace chromeos |