[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / tracing / power_tracing_agent.cc
blob409240c4fe7f014f15ae81fb8303f7057a8f89f8
1 // Copyright 2015 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.
5 #include "base/lazy_instance.h"
6 #include "base/memory/singleton.h"
7 #include "base/trace_event/trace_event_impl.h"
8 #include "content/browser/tracing/battor_power_trace_provider.h"
9 #include "content/browser/tracing/power_tracing_agent.h"
10 #include "content/public/browser/browser_thread.h"
12 namespace content {
14 PowerTracingAgent::PowerTracingAgent() : is_tracing_(false) {
15 battor_trace_provider_.reset(new BattorPowerTraceProvider());
18 PowerTracingAgent::~PowerTracingAgent() {}
20 bool PowerTracingAgent::StartTracing() {
21 // Tracing session already in progress.
22 if (is_tracing_)
23 return false;
25 // TODO(prabhur) Start tracing probably needs to be done in a
26 // separate thread since it involves talking to the h/w.
27 // Revisit once battor h/w communication is enabled.
28 is_tracing_ = battor_trace_provider_->StartTracing();
29 return is_tracing_;
32 void PowerTracingAgent::StopTracing(const OutputCallback& callback) {
33 // No tracing session in progress.
34 if (!is_tracing_)
35 return;
37 // Stop tracing & collect logs.
38 OutputCallback on_stop_power_tracing_done_callback = base::Bind(
39 &PowerTracingAgent::OnStopTracingDone, base::Unretained(this), callback);
40 BrowserThread::PostTask(
41 BrowserThread::UI, FROM_HERE,
42 base::Bind(&PowerTracingAgent::FlushOnThread, base::Unretained(this),
43 on_stop_power_tracing_done_callback));
46 void PowerTracingAgent::OnStopTracingDone(
47 const OutputCallback& callback,
48 const scoped_refptr<base::RefCountedString>& result) {
49 is_tracing_ = false;
50 // Pass the serialized events.
51 callback.Run(result);
54 // static
55 PowerTracingAgent* PowerTracingAgent::GetInstance() {
56 return base::Singleton<PowerTracingAgent>::get();
59 void PowerTracingAgent::FlushOnThread(const OutputCallback& callback) {
60 // Pass the result to the UI Thread.
62 // TODO(prabhur) StopTracing & GetLog need to be called on a
63 // separate thread depending on how BattorPowerTraceProvider is implemented.
64 battor_trace_provider_->StopTracing();
65 std::string battor_logs;
66 battor_trace_provider_->GetLog(&battor_logs);
67 scoped_refptr<base::RefCountedString> result =
68 base::RefCountedString::TakeString(&battor_logs);
69 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
70 base::Bind(callback, result));
73 } // namespace content