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/browser/power_profiler/power_data_provider_factory.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 | |
13 namespace { | |
14 | |
15 // Default sampling period, recommended by Intel Power Gadget. | |
jeremy
2014/02/17 06:31:13
recommended -> as recommended.
Would also be grea
Pan
2014/02/17 07:53:37
Done.
| |
16 const int kDefaultSamplePeriod = 50; | |
jeremy
2014/02/17 06:31:13
kDefaultSamplePeriodMS
Pan
2014/02/17 07:53:37
Done.
| |
17 | |
18 } // namespace | |
19 | |
20 namespace content { | |
21 | |
22 PowerProfilerService::PowerProfilerService() | |
23 : status_(UNINITIALIZED), | |
24 sample_period_(base::TimeDelta::FromMilliseconds(kDefaultSamplePeriod)), | |
25 data_provider_(PowerDataProviderFactory::Create()) { | |
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
27 | |
28 if (data_provider_.get()) { | |
29 status_ = INITIALIZED; | |
30 task_runner_ = BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | |
31 BrowserThread::GetBlockingPool()->GetSequenceToken()); | |
32 } | |
33 } | |
34 | |
35 PowerProfilerService::PowerProfilerService( | |
36 scoped_ptr<PowerDataProvider> provider, | |
37 scoped_refptr<base::TaskRunner> task_runner, | |
38 const base::TimeDelta& delay) | |
39 : task_runner_(task_runner), | |
40 status_(UNINITIALIZED), | |
41 sample_period_(delay), | |
jeremy
2014/02/17 06:31:13
delay -> sample_period
Pan
2014/02/17 07:53:37
Done.
| |
42 data_provider_(provider.Pass()) { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
44 | |
45 if (data_provider_.get()) | |
46 status_ = INITIALIZED; | |
47 } | |
48 | |
49 PowerProfilerService::~PowerProfilerService() { | |
50 } | |
51 | |
52 bool PowerProfilerService::IsAvailable() { | |
53 return status_ != UNINITIALIZED; | |
54 } | |
55 | |
56 PowerProfilerService* PowerProfilerService::GetInstance() { | |
57 return Singleton<PowerProfilerService>::get(); | |
58 } | |
59 | |
60 void PowerProfilerService::AddObserver(PowerProfilerObserver* observer) { | |
61 if (status_ == UNINITIALIZED) | |
62 return; | |
63 | |
64 observers_.AddObserver(observer); | |
65 if (status_ != PROFILING) | |
66 Start(); | |
67 } | |
68 | |
69 void PowerProfilerService::RemoveObserver(PowerProfilerObserver* observer) { | |
70 observers_.RemoveObserver(observer); | |
71 | |
72 if (!observers_.might_have_observers()) | |
73 Stop(); | |
74 } | |
75 | |
76 void PowerProfilerService::Start() { | |
77 DCHECK(status_ == INITIALIZED); | |
78 status_ = PROFILING; | |
79 | |
80 // Send out power events immediately. | |
81 QueryData(); | |
82 | |
83 query_power_timer_.Start(FROM_HERE, | |
84 sample_period_, this, &PowerProfilerService::QueryData); | |
85 } | |
86 | |
87 void PowerProfilerService::Stop() { | |
88 DCHECK(status_ == PROFILING); | |
89 | |
90 query_power_timer_.Stop(); | |
91 status_ = INITIALIZED; | |
92 } | |
93 | |
94 void PowerProfilerService::QueryData() { | |
95 task_runner_->PostTask( | |
96 FROM_HERE, base::Bind(&PowerProfilerService::QueryDataOnTaskRunner, | |
97 base::Unretained(this))); | |
98 } | |
99 | |
100 void PowerProfilerService::Notify(const PowerEventVector& events) { | |
101 FOR_EACH_OBSERVER(PowerProfilerObserver, observers_, OnPowerEvent(events)); | |
102 } | |
103 | |
104 void PowerProfilerService::QueryDataOnTaskRunner() { | |
105 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
106 if (status_ != PROFILING) | |
107 return; | |
jeremy
2014/02/17 06:31:13
Should this be a DCHECK ?
Pan
2014/02/17 07:53:37
right, thanks.
| |
108 | |
109 // Get data and notify. | |
110 PowerEventVector events = data_provider_->GetData(); | |
111 if (events.size() != 0) { | |
112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
113 &PowerProfilerService::Notify, base::Unretained(this), events)); | |
114 } | |
115 } | |
116 | |
117 } // namespace content | |
OLD | NEW |