[Offline pages] Moving browser code to android path
[chromium-blink-merge.git] / content / browser / power_usage_monitor_impl.h
blob9b95ae4d4aebd9883a0f432189cedca0d1428a99
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.
5 #ifndef CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_
6 #define CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_
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"
20 namespace content {
22 // Record statistics on power usage.
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.
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() {}
42 virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0;
43 virtual void CancelPendingHistogramReports() = 0;
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;
49 // Allow tests to override clock.
50 virtual base::Time Now() = 0;
53 public:
54 PowerUsageMonitor();
55 ~PowerUsageMonitor() override;
57 double discharge_amount() const {
58 return initial_battery_level_ - current_battery_level_;
61 // Start monitoring power usage.
62 // Note that the actual monitoring will be delayed until 30 minutes after
63 // system boot.
64 void Start();
66 void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface);
68 // Overridden from base::PowerObserver:
69 void OnPowerStateChange(bool on_battery_power) override;
70 void OnResume() override;
71 void OnSuspend() override;
73 // Overridden from NotificationObserver:
74 void Observe(int type,
75 const NotificationSource& source,
76 const NotificationDetails& details) override;
78 private:
79 friend class PowerUsageMonitorTest;
80 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate);
81 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification);
83 // Start monitoring system power usage.
84 // This function may be called after a delay, see Start() for details.
85 void StartInternal();
87 void OnBatteryStatusUpdate(const device::BatteryStatus& status);
88 void OnRenderProcessNotification(int type, int rph_id);
90 void DischargeStarted(double battery_level);
91 void WallPowerConnected(double battery_level);
93 void CancelPendingHistogramReporting();
95 device::BatteryStatusService::BatteryUpdateCallback callback_;
96 scoped_ptr<device::BatteryStatusService::BatteryUpdateSubscription>
97 subscription_;
99 NotificationRegistrar registrar_;
101 scoped_ptr<SystemInterface> system_interface_;
103 // True if monitoring was started (Start() called).
104 bool started_;
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_;
110 // True if the system is running on battery power, false if on wall power.
111 bool on_battery_power_;
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_;
117 // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully
118 // charged.
119 double current_battery_level_;
121 // Timestamp when wall power was disconnected, null Time object otherwise.
122 base::Time start_discharge_time_;
124 // IDs of live renderer processes.
125 base::hash_set<int> live_renderer_ids_;
127 private:
128 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor);
131 } // namespace content
133 #endif // CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_