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/gtest_prod_util.h" | |
11 #include "base/memory/singleton.h" | |
12 #include "base/power_monitor/power_monitor.h" | |
13 #include "base/time/time.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 "device/battery/battery_status_service.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 // * Data collection starts after system uptime exceeds 30 minutes. | |
32 // * If the machine goes to sleep or all renderers are closed then the current | |
33 // measurement is cancelled. | |
34 class PowerUsageMonitor : public base::PowerObserver, | |
35 public NotificationObserver { | |
36 public: | |
37 class SystemInterface { | |
38 public: | |
39 virtual ~SystemInterface() {} | |
40 | |
41 virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0; | |
42 virtual void CancelPendingHistogramReports() = 0; | |
43 | |
44 // Record the batter discharge rate over a period when the system is | |
Daniel Erat
2014/11/05 17:17:20
s/batter/battery/
jeremy
2014/11/06 17:12:56
Done.
| |
45 // on battery power. Possible values [0,100]. | |
Daniel Erat
2014/11/05 17:17:20
"rate" implies a numerator and denominator. what a
jeremy
2014/11/06 17:12:56
Done.
| |
46 virtual void RecordDischargeRate(int discharge_rate) = 0; | |
47 | |
48 // Allow tests to override clock. | |
49 virtual base::Time Now() = 0; | |
50 }; | |
51 | |
52 public: | |
53 PowerUsageMonitor(); | |
54 ~PowerUsageMonitor() override; | |
55 | |
56 double discharge_amount() const { | |
57 return initial_battery_level_ - current_battery_level_; | |
58 } | |
59 | |
60 // Start monitoring power usage. | |
61 // Note that the actual monitoring will be delayed until 30 minutes after | |
62 // system boot. | |
63 void Start(); | |
64 | |
65 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
66 | |
67 // Overridden from base::PowerObserver: | |
68 void OnPowerStateChange(bool on_battery_power) override; | |
69 void OnResume() override; | |
70 void OnSuspend() override; | |
71 | |
72 // Overridden from NotificationObserver: | |
73 void Observe(int type, | |
74 const NotificationSource& source, | |
75 const NotificationDetails& details) override; | |
76 | |
77 private: | |
78 friend class PowerUsageMonitorTest; | |
79 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
80 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); | |
81 | |
82 // Start monitoring system power usage. | |
83 // This function may be called after a delay, see Start() for details. | |
84 void StartInternal(); | |
85 | |
86 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | |
87 void OnRenderProcessNotification(int type, int rph_id); | |
88 | |
89 void DischargeStarted(double battery_level); | |
90 void WallPowerConnected(double battery_level); | |
91 | |
92 void CancelPendingHistogramReporting(); | |
93 | |
94 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
95 scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | |
96 subscription_; | |
97 | |
98 NotificationRegistrar registrar_; | |
99 | |
100 scoped_ptr<SystemInterface> system_interface_; | |
101 | |
102 // True if monitoring was started (Start() called). | |
103 bool started_; | |
104 | |
105 // True if collecting metrics for the current discharge cycle e.g. if no | |
106 // renderers are open we don't keep track of discharge. | |
107 bool tracking_discharge_; | |
108 | |
109 // True if the system is running on battery power, false if on wall power. | |
110 bool on_battery_power_; | |
111 | |
112 // Battery level when wall power disconnected. [0.0, 1.0] - 0 if on wall | |
113 // power, 1 means fully charged. | |
114 double initial_battery_level_; | |
115 | |
116 // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully | |
117 // charged. | |
118 double current_battery_level_; | |
119 | |
120 // Timestamp when wall power was disconnected, null Time object otherwise. | |
121 base::Time start_discharge_time_; | |
122 | |
123 // IDs of live renderer processes. | |
124 base::hash_set<int> live_renderer_ids_; | |
125 | |
126 private: | |
127 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
128 }; | |
129 | |
130 } // namespace content | |
131 | |
132 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |