| 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..c873da861dc5427b45d8dad462a40cb5cc456631
|
| --- /dev/null
|
| +++ b/chromeos/trace/arc_trace_agent.cc
|
| @@ -0,0 +1,99 @@
|
| +// 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 "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";
|
| +
|
| +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_) {
|
| + std::string no_data;
|
| + callback.Run(GetTracingAgentName(), GetTraceEventLabel(),
|
| + base::RefCountedString::TakeString(&no_data));
|
| + return;
|
| + }
|
| +
|
| + delegate_->StopTracing(base::Bind(&ArcTraceAgentImpl::OnStopAgentTracing,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + callback));
|
| + }
|
| +
|
| + // 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() : delegate_(nullptr), weak_ptr_factory_(this) {}
|
| +
|
| + ~ArcTraceAgentImpl() override = default;
|
| +
|
| + void OnStopAgentTracing(const StopAgentTracingCallback& callback,
|
| + const std::string& report) {
|
| + std::string data(report);
|
| + callback.Run(kArcTracingAgentName, kArcTraceLabel,
|
| + base::RefCountedString::TakeString(&data));
|
| + }
|
| +
|
| + Delegate* delegate_; // 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();
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|