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..3a111e737e543fea391e74bde97c2406945007c3 |
| --- /dev/null |
| +++ b/content/browser/power_profiler/power_profiler_service.cc |
| @@ -0,0 +1,107 @@ |
| +// 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/browser/power_profiler/power_profiler_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/threading/sequenced_worker_pool.h" |
| +#include "content/browser/power_profiler/power_data_provider_factory.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace content { |
| + |
| +PowerProfilerService::PowerProfilerService() |
| + : status_(UNINITIALIZED), |
| + delay_(base::TimeDelta::FromMilliseconds(50)), |
|
jeremy
2014/02/16 14:09:19
Where does this value come from?
Recommend initia
Pan
2014/02/17 03:17:15
This value is recommended by Intel Power Gadget, c
|
| + data_provider_(PowerDataProviderFactory::Create()) { |
| + if (data_provider_.get()) { |
| + status_ = INITIALIZED; |
| + task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| + BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| + } |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
|
jeremy
2014/02/16 14:09:19
IMHO this should be at the start of the function.
Pan
2014/02/17 03:17:15
Done.
|
| +} |
| + |
| +PowerProfilerService::PowerProfilerService( |
| + scoped_ptr<PowerDataProvider> provider, |
| + scoped_refptr<base::TaskRunner> task_runner, |
| + const 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) { |
|
jeremy
2014/02/16 14:09:19
obs -> observer
Pan
2014/02/17 03:17:15
Done.
|
| + if (UNINITIALIZED == status_) |
|
jeremy
2014/02/16 14:09:19
status_ == UNINITALIZED
constant on rhs in compar
Pan
2014/02/17 03:17:15
Done.
|
| + 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. |
|
jeremy
2014/02/16 14:09:19
Send
Pan
2014/02/17 03:17:15
Done.
|
| + QueryData(); |
| + |
| + query_power_timer_.Start(FROM_HERE, |
| + delay_, this, &PowerProfilerService::QueryData); |
| +} |
| + |
| +void PowerProfilerService::Stop() { |
| + DCHECK(PROFILING == status_); |
| + |
| + // stop timer, set status to INITIALIZED |
|
jeremy
2014/02/16 14:09:19
I think this comment is redundant.
Pan
2014/02/17 03:17:15
Done.
|
| + 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 PowerEventVector& events) { |
| + FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(events)); |
| +} |
| + |
| +void PowerProfilerService::QueryDataOnTaskRunner() { |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + if (PROFILING != status_) |
| + return; |
| + |
| + // Get Data and Notify |
|
jeremy
2014/02/16 14:09:19
. at end of comment.
Data and Notify shouldn't be
Pan
2014/02/17 03:17:15
Done.
|
| + PowerEventVector events = data_provider_->GetData(); |
| + if (events.size() != 0) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( |
| + &PowerProfilerService::Notify, base::Unretained(this), events)); |
| + } |
| +} |
| + |
| +} // namespace content |