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..5c43cf1cb8e588d66c85eca8822d97b30775ba95 |
| --- /dev/null |
| +++ b/content/browser/power_profiler/power_profiler_service.cc |
| @@ -0,0 +1,127 @@ |
| +// 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() |
| + : delay_(0) |
| + , status_(UNINITIALIZED) |
| + , data_provider_(PowerDataProviderFactory::Create()) { |
| + |
| + if (!data_provider_->IsDummy()) { |
| + status_ = INITIALIZED; |
| + InitializeDelays(); |
| + task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| + BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| + } |
| + DCHECK(base::MessageLoop::current()); |
|
qsr
2014/01/22 15:30:01
Not really useful. More useful would be to test yo
|
| +} |
| + |
| +PowerProfilerService::~PowerProfilerService() { |
| + if (query_power_timer_.IsRunning()) |
| + query_power_timer_.Stop(); |
|
qsr
2014/01/22 15:30:01
Not needed. Destroying the timer will stop it.
|
| +} |
| + |
| +void PowerProfilerService::InitializeDelays() { |
| + delays_[PowerProfilerObserver::LOW] = 4000; |
| + delays_[PowerProfilerObserver::NORMAL] = 200; |
| + delays_[PowerProfilerObserver::HIGH] = 50; |
| +} |
| + |
| +PowerProfilerService* PowerProfilerService::GetInstance() { |
| + return Singleton<PowerProfilerService>::get(); |
| +} |
| + |
| +void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) { |
| + if (UNINITIALIZED == status_) |
| + return; |
| + |
| + observers_.insert(obs); |
| + UpdateResolution(); |
| + if (observers_.size() == 1) |
| + Start(); |
| +} |
| + |
| +void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) { |
| + observers_.erase(obs); |
| + UpdateResolution(); |
| + if (observers_.empty()) |
| + Stop(); |
| +} |
| + |
| +void PowerProfilerService::UpdateResolution() { |
| + if (UNINITIALIZED == status_) |
| + return; |
| + |
| + PowerProfilerObserver::Resolution resolution = PowerProfilerObserver::LOW; |
| + std::set<PowerProfilerObserver*>::iterator it = observers_.begin(); |
| + for (; it != observers_.end(); ++it) |
| + if ((*it)->resolution() > resolution) |
| + resolution = (*it)->resolution(); |
| + |
| + // if delay_ will be reset, reschedule the timer immediately |
| + if (delay_ != delays_[resolution]) { |
| + delay_ = delays_[resolution]; |
| + |
| + if (query_power_timer_.IsRunning()) { |
| + query_power_timer_.Stop(); |
|
qsr
2014/01/22 15:30:01
Stop is not useful, restarting it will stop the cu
|
| + query_power_timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(delay_), this, |
| + &PowerProfilerService::OnTimer); |
| + } |
| + } |
| +} |
| + |
| +void PowerProfilerService::Start() { |
| + DCHECK (!query_power_timer_.IsRunning() && delay_); |
| + status_ = PROFILING; |
| + |
| + // send out power events immediately. |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PowerProfilerService::ProcessData, base::Unretained(this))); |
| + |
| + query_power_timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromMilliseconds(delay_), this, |
| + &PowerProfilerService::OnTimer); |
| +} |
| + |
| +void PowerProfilerService::Stop() { |
| + // stop timer, set status to INITIALIZED |
| + if (PROFILING == status_) { |
| + query_power_timer_.Stop(); |
| + status_ = INITIALIZED; |
| + } |
| +} |
| + |
| +void PowerProfilerService::OnTimer() { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&PowerProfilerService::ProcessData, base::Unretained(this))); |
| +} |
| + |
| +void PowerProfilerService::Notify(PowerEvent* event) { |
|
qsr
2014/01/22 15:30:01
You do not want to notify from your task runner, a
|
| + std::set<PowerProfilerObserver*>::iterator it = observers_.begin(); |
| + for (; it != observers_.end(); ++it) |
| + (*it)->Send(event); |
| +} |
| + |
| +void PowerProfilerService::ProcessData() { |
| + if (PROFILING != status_) |
| + return; |
| + |
| + // Get Data and Notify |
| + PowerEvent event; |
| + if(data_provider_->GetData(&event)) |
| + Notify(&event); |
| +} |
| + |
| +} // namespace content |