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 #ifndef CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
6 #define CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/containers/hash_tables.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/power_monitor/power_monitor.h" | |
12 #include "base/time/time.h" | |
13 #include "content/browser/battery_status/battery_status_service.h" | |
14 #include "content/public/browser/browser_message_filter.h" | |
15 #include "content/public/browser/notification_observer.h" | |
16 #include "content/public/browser/notification_registrar.h" | |
17 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" | |
18 | |
19 namespace content { | |
20 | |
21 // Record statistics on power usage. | |
22 // | |
23 // Two main statics are recorded by this class: | |
24 // * Power.BatteryDischarge_{5,15,30} - delta between battery level when | |
25 // unplugged from wallpower, over the specified period - in minutes. | |
26 // * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge | |
27 // from the device being unplugged until it's plugged back in, if said period | |
28 // was longer than 30 minutes. | |
29 // | |
30 // Heuristics: | |
31 // * Metrics are only collected once per day. | |
32 // * If the machine goes to sleep or all renderers are closed then the current | |
33 // measurement is cancelled. | |
34 // | |
35 // Threading: Most methods are called on the IO thread. | |
Avi (use Gerrit)
2014/10/21 15:12:49
This is not a helpful comment. Don't use descripti
jeremy
2014/10/23 14:32:47
Thanks, that is indeed clearer - I've removed the
| |
36 class PowerUsageMonitor : public base::PowerObserver, NotificationObserver { | |
Daniel Erat
2014/10/21 16:01:21
add 'public' to NotificationObserver too (not sure
jeremy
2014/10/23 14:32:47
Done.
| |
37 public: | |
38 class SystemInterface { | |
39 public: | |
40 SystemInterface(); | |
41 virtual ~SystemInterface(); | |
42 | |
43 void SetPowerUsageMonitor(PowerUsageMonitor* monitor) { | |
Daniel Erat
2014/10/21 16:01:21
set_power_usage_monitor
jeremy
2014/10/23 14:32:47
Done.
| |
44 power_usage_monitor_ = monitor; | |
45 } | |
46 | |
47 virtual void ScheduleHistogramReport(base::TimeDelta delay); | |
Daniel Erat
2014/10/21 16:01:21
make this class be a pure interface, please
jeremy
2014/10/23 14:32:47
Done.
| |
48 virtual void CancelPendingHistgramReports(); | |
Daniel Erat
2014/10/21 16:01:21
s/Histgram/Histogram/
jeremy
2014/10/23 14:32:47
Done.
| |
49 | |
50 // Allow tests to override clock. | |
51 virtual base::Time Now(); | |
52 | |
53 protected: | |
54 virtual void ReportBatteryLevelHistogram(base::TimeDelta discharge_time); | |
55 | |
56 private: | |
57 PowerUsageMonitor* power_usage_monitor_; | |
Daniel Erat
2014/10/21 16:01:21
document that this isn't owned
jeremy
2014/10/23 14:32:47
Done.
| |
58 | |
59 // Used to cancel in progress timers. | |
60 base::WeakPtrFactory<SystemInterface> weak_ptr_factory_; | |
61 }; | |
62 | |
63 public: | |
64 PowerUsageMonitor(); | |
65 virtual ~PowerUsageMonitor(); | |
66 | |
67 // Start monitoring power usage. | |
68 void Start(); | |
69 void StartOnIOThread(); | |
70 | |
71 void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); | |
72 | |
73 double DischargeAmount() { | |
74 return initial_battery_level_ - current_battery_level_; | |
75 } | |
76 | |
77 // Called by SystemInterface after a histogram was succesfully recorded. | |
78 void BatteryLevelReported(); | |
79 | |
80 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> recorder); | |
Daniel Erat
2014/10/21 16:01:21
s/recorder/interface/
jeremy
2014/10/23 14:32:47
Done.
| |
81 void SetNumLiveRenderersForTest(int num_renderers); | |
82 | |
83 // Overridden from base::PowerObserver: | |
84 virtual void OnPowerStateChange(bool on_battery_power) override {}; | |
85 virtual void OnResume() override {}; | |
86 virtual void OnSuspend() override; | |
Avi (use Gerrit)
2014/10/21 15:12:50
The new style is to drop the "virtual" and only ha
jeremy
2014/10/23 14:32:46
Done.
| |
87 | |
88 // Overridden from NotificationObserver: | |
89 virtual void Observe(int type, | |
90 const NotificationSource& source, | |
91 const NotificationDetails& details) override; | |
92 | |
93 private: | |
94 void ReportBatteryLevelHistogram(base::Time discharge_time); | |
95 void DischargeStarted(double battery_level); | |
96 void WallPowerConnected(double battery_level); | |
97 | |
98 void CancelPendingHistogramRecordingOnIOThread(); | |
99 | |
100 BatteryStatusService::BatteryUpdateCallback callback_; | |
101 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; | |
102 | |
103 NotificationRegistrar registrar_; | |
104 | |
105 scoped_ptr<SystemInterface> system_interface_; | |
106 | |
107 bool was_on_battery_power_; | |
Daniel Erat
2014/10/21 16:01:21
add a comment describing what this represents -- w
jeremy
2014/10/23 14:32:47
Done.
| |
108 double initial_battery_level_; | |
Daniel Erat
2014/10/21 16:01:21
initial when -- when the object was first created?
jeremy
2014/10/23 14:32:47
Done.
| |
109 double current_battery_level_; | |
110 base::Time start_discharge_time_; | |
111 | |
112 // Timestamp for when the first histogram is reported in a 24 hour period. | |
113 // Used for rate-limiting. | |
114 base::Time first_histogram_report_timestamp_; | |
115 | |
116 base::hash_set<int> live_renderer_ids_; | |
117 size_t num_live_renderers_; | |
Avi (use Gerrit)
2014/10/21 15:12:50
It bothers me that we have num_live_renderers_ as
Daniel Erat
2014/10/21 16:01:21
agreed; please remove this.
jeremy
2014/10/23 14:32:47
Done.
| |
118 | |
119 private: | |
120 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
121 }; | |
122 | |
123 } // namespace content | |
124 | |
125 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |