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/browser/battery_status/battery_status_service.h" | |
timvolodine
2014/10/28 23:44:03
note that battery API has recently been moved to d
jeremy
2014/11/02 15:06:21
Done.
| |
15 #include "content/public/browser/browser_message_filter.h" | |
16 #include "content/public/browser/notification_observer.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 #include "third_party/WebKit/public/platform/WebBatteryStatus.h" | |
Daniel Erat
2014/10/27 16:15:58
it feels strange to have content/ depend on blink
sadrul
2014/10/27 20:57:39
We have similar issues for events too, where we co
timvolodine
2014/10/28 23:44:03
as noted above after the move to device/battery we
| |
19 | |
20 namespace content { | |
21 | |
22 // Record statistics on power usage. | |
23 // | |
24 // Two main statics are recorded by this class: | |
25 // * Power.BatteryDischarge_{5,15,30} - delta between battery level when | |
26 // unplugged from wallpower, over the specified period - in minutes. | |
27 // * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge | |
28 // from the device being unplugged until it's plugged back in, if said period | |
29 // was longer than 30 minutes. | |
30 // | |
31 // Heuristics: | |
32 // * Metrics are only collected once per day. | |
33 // * If the machine goes to sleep or all renderers are closed then the current | |
34 // measurement is cancelled. | |
35 class PowerUsageMonitor : public base::PowerObserver, | |
36 public NotificationObserver { | |
37 public: | |
38 class SystemInterface { | |
39 public: | |
40 virtual ~SystemInterface() {} | |
41 | |
42 virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0; | |
43 virtual void CancelPendingHistogramReports() = 0; | |
44 | |
45 // Allow tests to override clock. | |
46 virtual base::Time Now() = 0; | |
47 | |
48 protected: | |
49 virtual void ReportBatteryLevelHistogram( | |
Daniel Erat
2014/10/27 16:15:58
it doesn't make sense to me to declare a private o
jeremy
2014/11/02 15:06:21
Done.
| |
50 base::Time start_time, | |
51 base::TimeDelta discharge_time) = 0; | |
52 }; | |
53 | |
54 public: | |
55 PowerUsageMonitor(); | |
56 virtual ~PowerUsageMonitor(); | |
57 | |
58 // Start monitoring power usage. | |
59 void Start(); | |
60 | |
61 double discharge_amount() { | |
62 return initial_battery_level_ - current_battery_level_; | |
63 } | |
64 | |
65 // Called by SystemInterface after a histogram was succesfully recorded. | |
66 void BatteryLevelReported(); | |
67 | |
68 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
69 void SetRenderersForTest(bool has_live_renderers); | |
70 | |
71 // Overridden from base::PowerObserver: | |
72 void OnPowerStateChange(bool on_battery_power) override {}; | |
73 void OnResume() override {}; | |
74 void OnSuspend() override; | |
75 | |
76 // Overridden from NotificationObserver: | |
77 void Observe(int type, | |
78 const NotificationSource& source, | |
79 const NotificationDetails& details) override; | |
80 | |
81 private: | |
82 friend class PowerUsageMonitorTest; | |
83 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
84 | |
85 void StartOnIOThread(); | |
86 | |
87 void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); | |
88 | |
89 void DischargeStarted(double battery_level); | |
90 void WallPowerConnected(double battery_level); | |
91 | |
92 void CancelPendingHistogramReporting(); | |
93 | |
94 BatteryStatusService::BatteryUpdateCallback callback_; | |
95 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; | |
96 | |
97 NotificationRegistrar registrar_; | |
98 | |
99 scoped_ptr<SystemInterface> system_interface_; | |
100 | |
101 // True if monitoring was started (Start() called). | |
102 bool started_; | |
103 | |
104 // True if the system is running on battery power, false if on wall power. | |
105 bool on_battery_power_; | |
106 | |
107 // Battery level when wall power disconnected, 0 if on wall power. | |
108 double initial_battery_level_; | |
Daniel Erat
2014/10/27 16:15:58
document the scale for this. looking at the docs f
jeremy
2014/11/02 15:06:21
Done.
| |
109 | |
110 // Current battery level, 0 if on wall power. | |
111 double current_battery_level_; | |
112 | |
113 // Timestamp when wall power was disconnected, null Time object otherwise. | |
114 base::Time start_discharge_time_; | |
115 | |
116 // Timestamp for when the first histogram is reported in a 24 hour period. | |
117 // Used for rate-limiting. | |
118 base::Time first_histogram_report_time_; | |
119 | |
120 // IDs of live renderer processes. | |
121 base::hash_set<int> live_renderer_ids_; | |
122 | |
123 private: | |
124 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
125 }; | |
126 | |
127 } // namespace content | |
128 | |
129 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |