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 : delay_(0) | |
| 16 , status_(UNINITIALIZED) | |
| 17 , data_provider_(PowerDataProviderFactory::Create()) { | |
| 18 | |
| 19 if (!data_provider_->IsDummy()) { | |
| 20 status_ = INITIALIZED; | |
| 21 InitializeDelays(); | |
| 22 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 23 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 24 } | |
| 25 DCHECK(base::MessageLoop::current()); | |
|
qsr
2014/01/22 15:30:01
Not really useful. More useful would be to test yo
| |
| 26 } | |
| 27 | |
| 28 PowerProfilerService::~PowerProfilerService() { | |
| 29 if (query_power_timer_.IsRunning()) | |
| 30 query_power_timer_.Stop(); | |
|
qsr
2014/01/22 15:30:01
Not needed. Destroying the timer will stop it.
| |
| 31 } | |
| 32 | |
| 33 void PowerProfilerService::InitializeDelays() { | |
| 34 delays_[PowerProfilerObserver::LOW] = 4000; | |
| 35 delays_[PowerProfilerObserver::NORMAL] = 200; | |
| 36 delays_[PowerProfilerObserver::HIGH] = 50; | |
| 37 } | |
| 38 | |
| 39 PowerProfilerService* PowerProfilerService::GetInstance() { | |
| 40 return Singleton<PowerProfilerService>::get(); | |
| 41 } | |
| 42 | |
| 43 void PowerProfilerService::AddObserver(PowerProfilerObserver* obs) { | |
| 44 if (UNINITIALIZED == status_) | |
| 45 return; | |
| 46 | |
| 47 observers_.insert(obs); | |
| 48 UpdateResolution(); | |
| 49 if (observers_.size() == 1) | |
| 50 Start(); | |
| 51 } | |
| 52 | |
| 53 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* obs) { | |
| 54 observers_.erase(obs); | |
| 55 UpdateResolution(); | |
| 56 if (observers_.empty()) | |
| 57 Stop(); | |
| 58 } | |
| 59 | |
| 60 void PowerProfilerService::UpdateResolution() { | |
| 61 if (UNINITIALIZED == status_) | |
| 62 return; | |
| 63 | |
| 64 PowerProfilerObserver::Resolution resolution = PowerProfilerObserver::LOW; | |
| 65 std::set<PowerProfilerObserver*>::iterator it = observers_.begin(); | |
| 66 for (; it != observers_.end(); ++it) | |
| 67 if ((*it)->resolution() > resolution) | |
| 68 resolution = (*it)->resolution(); | |
| 69 | |
| 70 // if delay_ will be reset, reschedule the timer immediately | |
| 71 if (delay_ != delays_[resolution]) { | |
| 72 delay_ = delays_[resolution]; | |
| 73 | |
| 74 if (query_power_timer_.IsRunning()) { | |
| 75 query_power_timer_.Stop(); | |
|
qsr
2014/01/22 15:30:01
Stop is not useful, restarting it will stop the cu
| |
| 76 query_power_timer_.Start(FROM_HERE, | |
| 77 base::TimeDelta::FromMilliseconds(delay_), this, | |
| 78 &PowerProfilerService::OnTimer); | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void PowerProfilerService::Start() { | |
| 84 DCHECK (!query_power_timer_.IsRunning() && delay_); | |
| 85 status_ = PROFILING; | |
| 86 | |
| 87 // send out power events immediately. | |
| 88 task_runner_->PostTask( | |
| 89 FROM_HERE, | |
| 90 base::Bind(&PowerProfilerService::ProcessData, base::Unretained(this))); | |
| 91 | |
| 92 query_power_timer_.Start(FROM_HERE, | |
| 93 base::TimeDelta::FromMilliseconds(delay_), this, | |
| 94 &PowerProfilerService::OnTimer); | |
| 95 } | |
| 96 | |
| 97 void PowerProfilerService::Stop() { | |
| 98 // stop timer, set status to INITIALIZED | |
| 99 if (PROFILING == status_) { | |
| 100 query_power_timer_.Stop(); | |
| 101 status_ = INITIALIZED; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void PowerProfilerService::OnTimer() { | |
| 106 task_runner_->PostTask( | |
| 107 FROM_HERE, | |
| 108 base::Bind(&PowerProfilerService::ProcessData, base::Unretained(this))); | |
| 109 } | |
| 110 | |
| 111 void PowerProfilerService::Notify(PowerEvent* event) { | |
|
qsr
2014/01/22 15:30:01
You do not want to notify from your task runner, a
| |
| 112 std::set<PowerProfilerObserver*>::iterator it = observers_.begin(); | |
| 113 for (; it != observers_.end(); ++it) | |
| 114 (*it)->Send(event); | |
| 115 } | |
| 116 | |
| 117 void PowerProfilerService::ProcessData() { | |
| 118 if (PROFILING != status_) | |
| 119 return; | |
| 120 | |
| 121 // Get Data and Notify | |
| 122 PowerEvent event; | |
| 123 if(data_provider_->GetData(&event)) | |
| 124 Notify(&event); | |
| 125 } | |
| 126 | |
| 127 } // namespace content | |
| OLD | NEW |