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 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 // Allow tests to override clock. | |
45 virtual base::Time Now() = 0; | |
46 | |
47 protected: | |
48 virtual void ReportBatteryLevelHistogram( | |
49 base::Time start_time, | |
50 base::TimeDelta discharge_time) = 0; | |
51 }; | |
52 | |
53 public: | |
54 PowerUsageMonitor(); | |
55 virtual ~PowerUsageMonitor(); | |
56 | |
57 // Start monitoring power usage. | |
58 void Start(); | |
59 void StartOnIOThread(); | |
Daniel Erat
2014/10/23 19:05:35
StartOnIOThread looks like it should be private in
jeremy
2014/10/27 08:25:12
Done.
| |
60 | |
61 void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); | |
Daniel Erat
2014/10/23 19:05:35
i think that this should be private too
jeremy
2014/10/27 08:25:12
Done.
| |
62 | |
63 double DischargeAmount() { | |
Daniel Erat
2014/10/23 19:05:35
rename to discharge_amount since it's inlined
jeremy
2014/10/27 08:25:12
Done.
| |
64 return initial_battery_level_ - current_battery_level_; | |
65 } | |
66 | |
67 // Called by SystemInterface after a histogram was succesfully recorded. | |
68 void BatteryLevelReported(); | |
69 | |
70 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
71 void SetNumLiveRenderersForTest(int num_renderers); | |
Daniel Erat
2014/10/23 19:05:35
maybe just SetRenderersForTest?
jeremy
2014/10/27 08:25:12
Done.
| |
72 | |
73 // Overridden from base::PowerObserver: | |
74 void OnPowerStateChange(bool on_battery_power) override {}; | |
75 void OnResume() override {}; | |
76 void OnSuspend() override; | |
77 | |
78 // Overridden from NotificationObserver: | |
79 virtual void Observe(int type, | |
Daniel Erat
2014/10/23 19:05:35
omit 'virtual' when using 'override'
jeremy
2014/10/27 08:25:12
Done.
| |
80 const NotificationSource& source, | |
81 const NotificationDetails& details) override; | |
82 | |
83 private: | |
84 void ReportBatteryLevelHistogram(base::Time discharge_time); | |
Daniel Erat
2014/10/23 19:05:35
this looks unused; delete it
jeremy
2014/10/27 08:25:12
Done.
| |
85 void DischargeStarted(double battery_level); | |
86 void WallPowerConnected(double battery_level); | |
87 | |
88 void CancelPendingHistogramRecording(); | |
Daniel Erat
2014/10/23 19:05:35
s/Recording/Reporting/?
jeremy
2014/10/27 08:25:12
Done.
| |
89 | |
90 BatteryStatusService::BatteryUpdateCallback callback_; | |
91 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; | |
92 | |
93 NotificationRegistrar registrar_; | |
94 | |
95 scoped_ptr<SystemInterface> system_interface_; | |
96 | |
97 // True if monitoring was started (Start() called). | |
98 bool started_; | |
99 | |
100 // True if the system is running on battery power, false if on wall power. | |
101 bool on_battery_power_; | |
102 | |
103 // Battery level when wall power disconnected, 0 if on wall power. | |
104 double initial_battery_level_; | |
105 | |
106 // Current battery level, 0 if on wall power. | |
107 double current_battery_level_; | |
108 | |
109 // Timestamp when wall power was disconnected, null Time object otherwise. | |
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_; | |
Daniel Erat
2014/10/23 19:05:34
s/timestamp/time/ (or s/time/timestamp/ in start_d
jeremy
2014/10/27 08:25:12
Done.
| |
115 | |
116 // IDs of live rendere processes. | |
Daniel Erat
2014/10/23 19:05:35
s/rendere/renderer/
jeremy
2014/10/27 08:25:12
Done.
| |
117 base::hash_set<int> live_renderer_ids_; | |
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 |