| 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_BROWSER_POWER_USAGE_MONITOR_POWER_USAGE_MONITOR_H_ | |
| 6 #define CHROME_BROWSER_POWER_USAGE_MONITOR_POWER_USAGE_MONITOR_H_ | |
| 7 | |
| 8 #include "base/containers/hash_tables.h" | |
| 9 #include "base/gtest_prod_util.h" | |
| 10 #include "base/macros.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 // Record statistics on power usage. | |
| 20 // | |
| 21 // Two main statics are recorded by this class: | |
| 22 // * Power.BatteryDischarge_{5,15,30} - delta between battery level when | |
| 23 // unplugged from wallpower, over the specified period - in minutes. | |
| 24 // * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge | |
| 25 // from the device being unplugged until it's plugged back in, if said period | |
| 26 // was longer than 30 minutes. | |
| 27 // | |
| 28 // Heuristics: | |
| 29 // * Data collection starts after system uptime exceeds 30 minutes. | |
| 30 // * If the machine goes to sleep or all renderers are closed then the current | |
| 31 // measurement is cancelled. | |
| 32 | |
| 33 class PowerUsageMonitor : public base::PowerObserver, | |
| 34 public content::NotificationObserver { | |
| 35 public: | |
| 36 class SystemInterface { | |
| 37 public: | |
| 38 virtual ~SystemInterface() {} | |
| 39 | |
| 40 virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0; | |
| 41 virtual void CancelPendingHistogramReports() = 0; | |
| 42 | |
| 43 // Record the battery discharge percent per hour over the time the system | |
| 44 // is on battery power, legal values [0,100]. | |
| 45 virtual void RecordDischargePercentPerHour(int percent_per_hour) = 0; | |
| 46 | |
| 47 // Allow tests to override clock. | |
| 48 virtual base::Time Now() = 0; | |
| 49 }; | |
| 50 | |
| 51 public: | |
| 52 PowerUsageMonitor(); | |
| 53 ~PowerUsageMonitor() override; | |
| 54 | |
| 55 double discharge_amount() const { | |
| 56 return initial_battery_level_ - current_battery_level_; | |
| 57 } | |
| 58 | |
| 59 // Start monitoring power usage. | |
| 60 // Note that the actual monitoring will be delayed until 30 minutes after | |
| 61 // system boot. | |
| 62 void Start(); | |
| 63 | |
| 64 void SetSystemInterfaceForTest( | |
| 65 std::unique_ptr<SystemInterface> system_interface); | |
| 66 | |
| 67 // Overridden from base::PowerObserver: | |
| 68 void OnPowerStateChange(bool on_battery_power) override; | |
| 69 void OnResume() override; | |
| 70 void OnSuspend() override; | |
| 71 | |
| 72 // Overridden from NotificationObserver: | |
| 73 void Observe(int type, | |
| 74 const content::NotificationSource& source, | |
| 75 const content::NotificationDetails& details) override; | |
| 76 | |
| 77 private: | |
| 78 friend class PowerUsageMonitorTest; | |
| 79 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | |
| 80 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); | |
| 81 | |
| 82 // Start monitoring system power usage. | |
| 83 // This function may be called after a delay, see Start() for details. | |
| 84 void StartInternal(); | |
| 85 | |
| 86 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | |
| 87 void OnRenderProcessNotification(int type, int rph_id); | |
| 88 | |
| 89 void DischargeStarted(double battery_level); | |
| 90 void WallPowerConnected(double battery_level); | |
| 91 | |
| 92 void CancelPendingHistogramReporting(); | |
| 93 | |
| 94 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
| 95 std::unique_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | |
| 96 subscription_; | |
| 97 | |
| 98 content::NotificationRegistrar registrar_; | |
| 99 | |
| 100 std::unique_ptr<SystemInterface> system_interface_; | |
| 101 | |
| 102 // True if monitoring was started (Start() called). | |
| 103 bool started_; | |
| 104 | |
| 105 // True if collecting metrics for the current discharge cycle e.g. if no | |
| 106 // renderers are open we don't keep track of discharge. | |
| 107 bool tracking_discharge_; | |
| 108 | |
| 109 // True if the system is running on battery power, false if on wall power. | |
| 110 bool on_battery_power_; | |
| 111 | |
| 112 // Battery level when wall power disconnected. [0.0, 1.0] - 0 if on wall | |
| 113 // power, 1 means fully charged. | |
| 114 double initial_battery_level_; | |
| 115 | |
| 116 // Current battery level. [0.0, 1.0] - 0 if on wall power, 1 means fully | |
| 117 // charged. | |
| 118 double current_battery_level_; | |
| 119 | |
| 120 // Timestamp when wall power was disconnected, null Time object otherwise. | |
| 121 base::Time start_discharge_time_; | |
| 122 | |
| 123 // IDs of live renderer processes. | |
| 124 base::hash_set<int> live_renderer_ids_; | |
| 125 | |
| 126 private: | |
| 127 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
| 128 }; | |
| 129 | |
| 130 #endif // CHROME_BROWSER_POWER_USAGE_MONITOR_POWER_USAGE_MONITOR_H_ | |
| OLD | NEW |