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 // * Metrics are only collected once per day. | |
Daniel Erat
2014/11/04 17:04:03
this is outdated now, right?
jeremy
2014/11/05 11:40:19
Done.
| |
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 | |
48 public: | |
49 PowerUsageMonitor(); | |
50 virtual ~PowerUsageMonitor(); | |
Daniel Erat
2014/11/04 17:04:03
nit: override instead of virtual
jeremy
2014/11/05 11:40:19
Done.
| |
51 | |
52 // Start monitoring power usage. | |
53 // Note that the actual monitoring will be delayed until 30 minutes after | |
54 // system boot. | |
55 void Start(); | |
56 | |
57 double discharge_amount() { | |
Daniel Erat
2014/11/04 17:04:03
nit: move above Start() since this is inlined; als
jeremy
2014/11/05 11:40:19
Done.
| |
58 return initial_battery_level_ - current_battery_level_; | |
59 } | |
60 | |
61 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
62 void SetRenderersForTest(bool has_live_renderers); | |
63 | |
64 // Overridden from base::PowerObserver: | |
65 void OnPowerStateChange(bool on_battery_power) override {}; | |
Daniel Erat
2014/11/04 17:04:03
don't inline this and OnResume(): http://www.chrom
jeremy
2014/11/05 11:40:19
Done.
| |
66 void OnResume() override {}; | |
67 void OnSuspend() override; | |
68 | |
69 // Overridden from NotificationObserver: | |
70 void Observe(int type, | |
71 const NotificationSource& source, | |
72 const NotificationDetails& details) override; | |
73 | |
74 private: | |
75 friend class PowerUsageMonitorTest; | |
76 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
77 | |
78 // Start monitoring system power usage. | |
79 // This function may be called after a delay, see Start() for details. | |
80 void StartInternal(); | |
81 | |
82 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | |
83 | |
84 void DischargeStarted(double battery_level); | |
85 void WallPowerConnected(double battery_level); | |
86 | |
87 void CancelPendingHistogramReporting(); | |
88 | |
89 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
90 scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | |
91 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 // Scale is [0.0, 1.0]. | |
105 double initial_battery_level_; | |
106 | |
107 // Current battery level, 0 if on wall power. | |
Daniel Erat
2014/11/04 17:04:03
nit: document the range here (specifically the upp
jeremy
2014/11/05 11:40:19
Done.
| |
108 double current_battery_level_; | |
109 | |
110 // Timestamp when wall power was disconnected, null Time object otherwise. | |
111 base::Time start_discharge_time_; | |
112 | |
113 // IDs of live renderer processes. | |
114 base::hash_set<int> live_renderer_ids_; | |
115 | |
116 private: | |
117 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
118 }; | |
119 | |
120 } // namespace content | |
121 | |
122 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |