| Index: content/browser/power_profiler/power_profiler_service.cc
|
| diff --git a/content/browser/power_profiler/power_profiler_service.cc b/content/browser/power_profiler/power_profiler_service.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ccc8e95d876754ef61de9bf3df1b816fe1da49fd
|
| --- /dev/null
|
| +++ b/content/browser/power_profiler/power_profiler_service.cc
|
| @@ -0,0 +1,106 @@
|
| +// Copyright 2014 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 "content/public/browser/power_profiler_service.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/threading/sequenced_worker_pool.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +
|
| +namespace content {
|
| +
|
| +PowerProfilerService::PowerProfilerService()
|
| + : status_(UNINITIALIZED),
|
| + delay_(base::TimeDelta::FromMilliseconds(50)),
|
| + data_provider_(PowerDataProviderFactory::Create()) {
|
| + if (data_provider_.get()) {
|
| + status_ = INITIALIZED;
|
| + task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
|
| + BrowserThread::GetBlockingPool()->GetSequenceToken());
|
| + }
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +}
|
| +
|
| +PowerProfilerService::PowerProfilerService(
|
| + scoped_ptr<PowerDataProvider> provider,
|
| + scoped_refptr<base::TaskRunner> task_runner,
|
| + base::TimeDelta delay)
|
| + : task_runner_(task_runner),
|
| + status_(UNINITIALIZED),
|
| + delay_(delay),
|
| + data_provider_(provider.Pass()) {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +
|
| + if (data_provider_.get())
|
| + status_ = INITIALIZED;
|
| +}
|
| +
|
| +bool PowerProfilerService::IsAvailable() {
|
| + return status_ != UNINITIALIZED;
|
| +}
|
| +
|
| +PowerProfilerService* PowerProfilerService::GetInstance() {
|
| + return Singleton<PowerProfilerService>::get();
|
| +}
|
| +
|
| +void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) {
|
| + if (UNINITIALIZED == status_)
|
| + return;
|
| +
|
| + observers_.AddObserver(obs);
|
| + if (PROFILING != status_)
|
| + Start();
|
| +}
|
| +
|
| +void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) {
|
| + observers_.RemoveObserver(obs);
|
| +
|
| + if (!observers_.might_have_observers())
|
| + Stop();
|
| +}
|
| +
|
| +void PowerProfilerService::Start() {
|
| + DCHECK(INITIALIZED == status_);
|
| + status_ = PROFILING;
|
| +
|
| + // send out power events immediately.
|
| + QueryData();
|
| +
|
| + query_power_timer_.Start(FROM_HERE,
|
| + delay_, this, &PowerProfilerService::QueryData);
|
| +}
|
| +
|
| +void PowerProfilerService::Stop() {
|
| + DCHECK(PROFILING == status_);
|
| +
|
| + // stop timer, set status to INITIALIZED
|
| + query_power_timer_.Stop();
|
| + status_ = INITIALIZED;
|
| +}
|
| +
|
| +void PowerProfilerService::QueryData() {
|
| + task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void PowerProfilerService::Notify(const PowerEvent& event) {
|
| + FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(event));
|
| +}
|
| +
|
| +void PowerProfilerService::QueryDataOnTaskRunner() {
|
| + DCHECK(task_runner_->RunsTasksOnCurrentThread());
|
| + if (PROFILING != status_)
|
| + return;
|
| +
|
| + // Get Data and Notify
|
| + PowerEvent event;
|
| + if (data_provider_->GetData(&event)) {
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
|
| + &PowerProfilerService::Notify, base::Unretained(this), event));
|
| + }
|
| +}
|
| +
|
| +} // namespace content
|
|
|