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