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..fd8ef548a1932a618f79ed9aeffdc842c8ef0f88 |
--- /dev/null |
+++ b/content/browser/power_profiler/power_profiler_service.cc |
@@ -0,0 +1,134 @@ |
+// 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) |
+ , data_provider_(PowerDataProviderFactory::Create()) { |
qsr
2014/01/27 10:01:47
comma should be on the previous line.
Pan
2014/01/28 13:32:58
thanks, done.
|
+ if (data_provider_.get()) { |
+ status_ = INITIALIZED; |
+ InitializeDelays(); |
+ task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
+ BrowserThread::GetBlockingPool()->GetSequenceToken()); |
+ } |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+PowerProfilerService::PowerProfilerService( |
+ scoped_ptr<PowerDataProvider> provider) |
qsr
2014/01/27 10:01:47
I sent you a patch off-review to show exactly what
Pan
2014/01/28 13:32:58
thanks, I missed the one to run a nested loop, it
|
+ : status_(UNINITIALIZED) |
+ , data_provider_(provider.Pass()) { |
+ if (data_provider_.get()) { |
+ status_ = INITIALIZED; |
+ InitializeDelays(); |
+ task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
+ BrowserThread::GetBlockingPool()->GetSequenceToken()); |
+ } |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+bool PowerProfilerService::IsAvailable() { |
+ return status_ != UNINITIALIZED; |
+} |
+ |
+void PowerProfilerService::InitializeDelays() { |
+ delays_[PowerProfilerObserver::LOW] = base::TimeDelta::FromMilliseconds(4000); |
+ delays_[PowerProfilerObserver::NORMAL] |
qsr
2014/01/27 10:01:47
'=' should be on the previous line, more generally
Pan
2014/01/28 13:32:58
thanks, done
|
+ = base::TimeDelta::FromMilliseconds(200); |
+ delays_[PowerProfilerObserver::HIGH] = base::TimeDelta::FromMilliseconds(50); |
+} |
+ |
+PowerProfilerService* PowerProfilerService::GetInstance() { |
+ return Singleton<PowerProfilerService>::get(); |
+} |
+ |
+void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) { |
+ if (UNINITIALIZED == status_) |
+ return; |
+ |
+ observers_.AddObserver(obs); |
+ UpdateResolution(); |
+ if (PROFILING != status_) |
+ Start(); |
+} |
+ |
+void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) { |
+ observers_.RemoveObserver(obs); |
+ UpdateResolution(); |
+ if (!observers_.might_have_observers()) |
+ Stop(); |
+} |
+ |
+void PowerProfilerService::UpdateResolution() { |
qsr
2014/01/27 10:01:47
I've been thinking about resolution again, and I f
Pan
2014/01/28 09:58:04
I've thought it should works similar to Android se
|
+ if (UNINITIALIZED == status_) |
+ return; |
+ |
+ PowerProfilerObserver::Resolution resolution = PowerProfilerObserver::LOW; |
+ ObserverListBase<PowerProfilerObserver>::Iterator it_inside_observer_macro( |
+ observers_); |
+ PowerProfilerObserver* obs; |
+ while ((obs = it_inside_observer_macro.GetNext()) != 0) |
+ if (obs->resolution() > resolution) |
+ resolution = obs->resolution(); |
+ |
+ // if delay_ will be reset, reschedule the timer immediately |
+ if (delay_ != delays_[resolution]) { |
+ delay_ = delays_[resolution]; |
+ |
+ if (PROFILING == status_) |
qsr
2014/01/27 10:01:47
Add {} around statement as it is more than one lin
Pan
2014/01/28 13:32:58
Done.
|
+ query_power_timer_.Start(FROM_HERE, |
+ delay_, this, &PowerProfilerService::QueryData); |
+ } |
+} |
+ |
+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 |