Chromium Code Reviews| 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..97041ef7a184bb49a2af357dc46318dd161ac916 |
| --- /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 { |
| + |
| +const base::TimeDelta PowerProfilerService::delay_ = |
| + base::TimeDelta::FromMilliseconds(50); |
|
qsr
2014/01/28 13:44:15
This is a static initializer and is not allowed in
Pan
2014/01/28 14:22:11
thanks, Done.
|
| + |
| +PowerProfilerService::PowerProfilerService() |
| + : status_(UNINITIALIZED), |
| + data_provider_(PowerDataProviderFactory::Create()) { |
| + if (data_provider_.get()) { |
| + status_ = INITIALIZED; |
| + task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| + BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| + } |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
|
qsr
2014/01/28 13:44:15
You should move the DCHECK before doing anything e
Pan
2014/01/28 14:22:11
Done.
|
| +} |
| + |
| +PowerProfilerService::PowerProfilerService( |
| + scoped_ptr<PowerDataProvider> provider, |
| + scoped_refptr<base::TaskRunner> task_runner) |
| + : task_runner_(task_runner), |
| + status_(UNINITIALIZED), |
| + data_provider_(provider.Pass()) { |
| + if (data_provider_.get()) |
| + status_ = INITIALIZED; |
| + |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| +} |
| + |
| +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))); |
|
qsr
2014/01/28 13:44:15
base::Unretained should be aligned with &PowerProf
Pan
2014/01/28 14:22:11
Done.
|
| +} |
| + |
| +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)) { |
|
qsr
2014/01/28 13:44:15
add a space between if and (
Pan
2014/01/28 14:22:11
Done.
|
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| + &PowerProfilerService::Notify, base::Unretained(this), event)); |
| + } |
| +} |
| + |
| +} // namespace content |