Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/public/browser/power_profiler_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/threading/sequenced_worker_pool.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 PowerProfilerService::PowerProfilerService() | |
| 15 : status_(UNINITIALIZED) | |
| 16 , 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.
| |
| 17 if (data_provider_.get()) { | |
| 18 status_ = INITIALIZED; | |
| 19 InitializeDelays(); | |
| 20 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 21 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 22 } | |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 24 } | |
| 25 | |
| 26 PowerProfilerService::PowerProfilerService( | |
| 27 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
| |
| 28 : status_(UNINITIALIZED) | |
| 29 , data_provider_(provider.Pass()) { | |
| 30 if (data_provider_.get()) { | |
| 31 status_ = INITIALIZED; | |
| 32 InitializeDelays(); | |
| 33 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 34 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 35 } | |
| 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 37 } | |
| 38 | |
| 39 bool PowerProfilerService::IsAvailable() { | |
| 40 return status_ != UNINITIALIZED; | |
| 41 } | |
| 42 | |
| 43 void PowerProfilerService::InitializeDelays() { | |
| 44 delays_[PowerProfilerObserver::LOW] = base::TimeDelta::FromMilliseconds(4000); | |
| 45 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
| |
| 46 = base::TimeDelta::FromMilliseconds(200); | |
| 47 delays_[PowerProfilerObserver::HIGH] = base::TimeDelta::FromMilliseconds(50); | |
| 48 } | |
| 49 | |
| 50 PowerProfilerService* PowerProfilerService::GetInstance() { | |
| 51 return Singleton<PowerProfilerService>::get(); | |
| 52 } | |
| 53 | |
| 54 void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) { | |
| 55 if (UNINITIALIZED == status_) | |
| 56 return; | |
| 57 | |
| 58 observers_.AddObserver(obs); | |
| 59 UpdateResolution(); | |
| 60 if (PROFILING != status_) | |
| 61 Start(); | |
| 62 } | |
| 63 | |
| 64 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) { | |
| 65 observers_.RemoveObserver(obs); | |
| 66 UpdateResolution(); | |
| 67 if (!observers_.might_have_observers()) | |
| 68 Stop(); | |
| 69 } | |
| 70 | |
| 71 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
| |
| 72 if (UNINITIALIZED == status_) | |
| 73 return; | |
| 74 | |
| 75 PowerProfilerObserver::Resolution resolution = PowerProfilerObserver::LOW; | |
| 76 ObserverListBase<PowerProfilerObserver>::Iterator it_inside_observer_macro( | |
| 77 observers_); | |
| 78 PowerProfilerObserver* obs; | |
| 79 while ((obs = it_inside_observer_macro.GetNext()) != 0) | |
| 80 if (obs->resolution() > resolution) | |
| 81 resolution = obs->resolution(); | |
| 82 | |
| 83 // if delay_ will be reset, reschedule the timer immediately | |
| 84 if (delay_ != delays_[resolution]) { | |
| 85 delay_ = delays_[resolution]; | |
| 86 | |
| 87 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.
| |
| 88 query_power_timer_.Start(FROM_HERE, | |
| 89 delay_, this, &PowerProfilerService::QueryData); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void PowerProfilerService::Start() { | |
| 94 DCHECK(INITIALIZED == status_); | |
| 95 status_ = PROFILING; | |
| 96 | |
| 97 // send out power events immediately. | |
| 98 QueryData(); | |
| 99 | |
| 100 query_power_timer_.Start(FROM_HERE, | |
| 101 delay_, this, &PowerProfilerService::QueryData); | |
| 102 } | |
| 103 | |
| 104 void PowerProfilerService::Stop() { | |
| 105 DCHECK(PROFILING == status_); | |
| 106 | |
| 107 // stop timer, set status to INITIALIZED | |
| 108 query_power_timer_.Stop(); | |
| 109 status_ = INITIALIZED; | |
| 110 } | |
| 111 | |
| 112 void PowerProfilerService::QueryData() { | |
| 113 task_runner_->PostTask( | |
| 114 FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner, | |
| 115 base::Unretained(this))); | |
| 116 } | |
| 117 | |
| 118 void PowerProfilerService::Notify(const PowerEvent& event) { | |
| 119 FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(event)); | |
| 120 } | |
| 121 | |
| 122 void PowerProfilerService::QueryDataOnTaskRunner() { | |
| 123 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 124 if (PROFILING != status_) | |
| 125 return; | |
| 126 | |
| 127 // Get Data and Notify | |
| 128 PowerEvent event; | |
| 129 if(data_provider_->GetData(&event)) | |
| 130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 131 &PowerProfilerService::Notify, base::Unretained(this), event)); | |
| 132 } | |
| 133 | |
| 134 } // namespace content | |
| OLD | NEW |