| 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/browser/power_profiler/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 { | |
| 13 | |
| 14 // Default sampling period, as recommended by Intel Power Gadget. | |
| 15 // Section 3.1 of http://software.intel.com/en-us/blogs/2013/10/03/using-the-int
el-power-gadget-api-on-windows | |
| 16 const int kDefaultSamplePeriodMs = 50; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 PowerProfilerService::PowerProfilerService() | |
| 23 : status_(UNINITIALIZED), | |
| 24 sample_period_(base::TimeDelta::FromMilliseconds(kDefaultSamplePeriodMs)), | |
| 25 data_provider_(PowerDataProvider::Create()) { | |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 27 | |
| 28 // No provider supported for current platform. | |
| 29 if (!data_provider_.get()) | |
| 30 return; | |
| 31 | |
| 32 status_ = INITIALIZED; | |
| 33 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
| 34 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
| 35 } | |
| 36 | |
| 37 PowerProfilerService::PowerProfilerService( | |
| 38 scoped_ptr<PowerDataProvider> provider, | |
| 39 scoped_refptr<base::TaskRunner> task_runner, | |
| 40 const base::TimeDelta& sample_period) | |
| 41 : task_runner_(task_runner), | |
| 42 status_(UNINITIALIZED), | |
| 43 sample_period_(sample_period), | |
| 44 data_provider_(provider.Pass()) { | |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 46 | |
| 47 if (data_provider_.get()) | |
| 48 status_ = INITIALIZED; | |
| 49 } | |
| 50 | |
| 51 PowerProfilerService::~PowerProfilerService() { | |
| 52 } | |
| 53 | |
| 54 bool PowerProfilerService::IsAvailable() { | |
| 55 return status_ != UNINITIALIZED; | |
| 56 } | |
| 57 | |
| 58 PowerProfilerService* PowerProfilerService::GetInstance() { | |
| 59 return Singleton<PowerProfilerService>::get(); | |
| 60 } | |
| 61 | |
| 62 void PowerProfilerService::AddObserver(PowerProfilerObserver* observer) { | |
| 63 if (status_ == UNINITIALIZED) | |
| 64 return; | |
| 65 | |
| 66 observers_.AddObserver(observer); | |
| 67 if (status_ != PROFILING) | |
| 68 Start(); | |
| 69 } | |
| 70 | |
| 71 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* observer) { | |
| 72 observers_.RemoveObserver(observer); | |
| 73 | |
| 74 if (!observers_.might_have_observers()) | |
| 75 Stop(); | |
| 76 } | |
| 77 | |
| 78 void PowerProfilerService::Start() { | |
| 79 DCHECK(status_ == INITIALIZED); | |
| 80 status_ = PROFILING; | |
| 81 | |
| 82 // Send out power events immediately. | |
| 83 QueryData(); | |
| 84 | |
| 85 query_power_timer_.Start(FROM_HERE, | |
| 86 sample_period_, this, &PowerProfilerService::QueryData); | |
| 87 } | |
| 88 | |
| 89 void PowerProfilerService::Stop() { | |
| 90 DCHECK(status_ == PROFILING); | |
| 91 | |
| 92 query_power_timer_.Stop(); | |
| 93 status_ = INITIALIZED; | |
| 94 } | |
| 95 | |
| 96 void PowerProfilerService::QueryData() { | |
| 97 task_runner_->PostTask( | |
| 98 FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner, | |
| 99 base::Unretained(this))); | |
| 100 } | |
| 101 | |
| 102 void PowerProfilerService::Notify(const PowerEventVector& events) { | |
| 103 FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(events)); | |
| 104 } | |
| 105 | |
| 106 void PowerProfilerService::QueryDataOnTaskRunner() { | |
| 107 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 108 DCHECK(status_ == PROFILING); | |
| 109 | |
| 110 // Get data and notify. | |
| 111 PowerEventVector events = data_provider_->GetData(); | |
| 112 if (events.size() != 0) { | |
| 113 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 114 &PowerProfilerService::Notify, base::Unretained(this), events)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 } // namespace content | |
| OLD | NEW |