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 const base::TimeDelta PowerProfilerService::delay_ = | |
| 15 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.
| |
| 16 | |
| 17 PowerProfilerService::PowerProfilerService() | |
| 18 : status_(UNINITIALIZED), | |
| 19 data_provider_(PowerDataProviderFactory::Create()) { | |
| 20 if (data_provider_.get()) { | |
| 21 status_ = INITIALIZED; | |
| 22 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 23 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 24 } | |
| 25 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.
| |
| 26 } | |
| 27 | |
| 28 PowerProfilerService::PowerProfilerService( | |
| 29 scoped_ptr<PowerDataProvider> provider, | |
| 30 scoped_refptr<base::TaskRunner> task_runner) | |
| 31 : task_runner_(task_runner), | |
| 32 status_(UNINITIALIZED), | |
| 33 data_provider_(provider.Pass()) { | |
| 34 if (data_provider_.get()) | |
| 35 status_ = INITIALIZED; | |
| 36 | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 38 } | |
| 39 | |
| 40 bool PowerProfilerService::IsAvailable() { | |
| 41 return status_ != UNINITIALIZED; | |
| 42 } | |
| 43 | |
| 44 PowerProfilerService* PowerProfilerService::GetInstance() { | |
| 45 return Singleton<PowerProfilerService>::get(); | |
| 46 } | |
| 47 | |
| 48 void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) { | |
| 49 if (UNINITIALIZED == status_) | |
| 50 return; | |
| 51 | |
| 52 observers_.AddObserver(obs); | |
| 53 if (PROFILING != status_) | |
| 54 Start(); | |
| 55 } | |
| 56 | |
| 57 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) { | |
| 58 observers_.RemoveObserver(obs); | |
| 59 | |
| 60 if (!observers_.might_have_observers()) | |
| 61 Stop(); | |
| 62 } | |
| 63 | |
| 64 void PowerProfilerService::Start() { | |
| 65 DCHECK(INITIALIZED == status_); | |
| 66 status_ = PROFILING; | |
| 67 | |
| 68 // send out power events immediately. | |
| 69 QueryData(); | |
| 70 | |
| 71 query_power_timer_.Start(FROM_HERE, | |
| 72 delay_, this, &PowerProfilerService::QueryData); | |
| 73 } | |
| 74 | |
| 75 void PowerProfilerService::Stop() { | |
| 76 DCHECK(PROFILING == status_); | |
| 77 | |
| 78 // stop timer, set status to INITIALIZED | |
| 79 query_power_timer_.Stop(); | |
| 80 status_ = INITIALIZED; | |
| 81 } | |
| 82 | |
| 83 void PowerProfilerService::QueryData() { | |
| 84 task_runner_->PostTask( | |
| 85 FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner, | |
| 86 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.
| |
| 87 } | |
| 88 | |
| 89 void PowerProfilerService::Notify(const PowerEvent& event) { | |
| 90 FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(event)); | |
| 91 } | |
| 92 | |
| 93 void PowerProfilerService::QueryDataOnTaskRunner() { | |
| 94 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 95 if (PROFILING != status_) | |
| 96 return; | |
| 97 | |
| 98 // Get Data and Notify | |
| 99 PowerEvent event; | |
| 100 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.
| |
| 101 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 102 &PowerProfilerService::Notify, base::Unretained(this), event)); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 } // namespace content | |
| OLD | NEW |