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/common/content_export.h" | |
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 "device/battery/battery_status_service.h" | |
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 // * Data collection starts after system uptime exceeds 30 minutes. | |
33 // * If the machine goes to sleep or all renderers are closed then the current | |
34 // measurement is cancelled. | |
35 class CONTENT_EXPORT 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 // Record the battery discharge percent per hour over the time the system | |
46 // is on battery power, legal values [0,100]. | |
47 virtual void RecordDischargePercentPerHour(int percent_per_hour) = 0; | |
48 | |
49 // Allow tests to override clock. | |
50 virtual base::Time Now() = 0; | |
51 }; | |
52 | |
53 public: | |
54 PowerUsageMonitor(); | |
55 ~PowerUsageMonitor() override; | |
56 | |
57 double discharge_amount() const { | |
58 return initial_battery_level_ - current_battery_level_; | |
59 } | |
60 | |
61 // Start monitoring power usage. | |
62 // Note that the actual monitoring will be delayed until 30 minutes after | |
63 // system boot. | |
64 void Start(); | |
65 | |
66 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); | |
67 | |
68 // Overridden from base::PowerObserver: | |
69 void OnPowerStateChange(bool on_battery_power) override; | |
70 void OnResume() override; | |
71 void OnSuspend() override; | |
72 | |
73 // Overridden from NotificationObserver: | |
74 void Observe(int type, | |
75 const NotificationSource& source, | |
76 const NotificationDetails& details) override; | |
77 | |
78 private: | |
79 friend class PowerUsageMonitorTest; | |
80 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
81 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); | |
82 | |
83 // Start monitoring system power usage. | |
84 // This function may be called after a delay, see Start() for details. | |
85 void StartInternal(); | |
86 | |
87 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | |
88 void OnRenderProcessNotification(int type, int rph_id); | |
89 | |
90 void DischargeStarted(double battery_level); | |
91 void WallPowerConnected(double battery_level); | |
92 | |
93 void CancelPendingHistogramReporting(); | |
94 | |
95 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
96 scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | |
ppi
2014/11/16 16:03:08
From the point of view of Mojo-fication of browser
jeremy
2014/11/17 07:50:48
Thanks, wasn't aware of that.
Unless you are stro
ppi
2014/11/17 10:51:01
Porting to Mojo in a separate CL sounds good, than
| |
97 subscription_; | |
98 | |
99 NotificationRegistrar registrar_; | |
100 | |
101 scoped_ptr<SystemInterface> system_interface_; | |
102 | |
103 // True if monitoring was started (Start() called). | |
104 bool started_; | |
105 | |
106 // True if collecting metrics for the current discharge cycle e.g. if no | |
107 // renderers are open we don't keep track of discharge. | |
108 bool tracking_discharge_; | |
109 | |
110 // True if the system is running on battery power, false if on wall power. | |
111 bool on_battery_power_; | |
112 | |
113 // Battery level when wall power disconnected. [0.0, 1.0] - 0 if on wall | |
114 // power, 1 means fully charged. | |
115 double initial_battery_level_; | |
116 | |
117 // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully | |
118 // charged. | |
119 double current_battery_level_; | |
120 | |
121 // Timestamp when wall power was disconnected, null Time object otherwise. | |
122 base::Time start_discharge_time_; | |
123 | |
124 // IDs of live renderer processes. | |
125 base::hash_set<int> live_renderer_ids_; | |
126 | |
127 private: | |
128 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
129 }; | |
130 | |
131 } // namespace content | |
132 | |
133 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |