| 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_H_ |
| 6 #define CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | 6 #define CHROME_BROWSER_POWER_USAGE_MONITOR_POWER_USAGE_MONITOR_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]. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 65 | 63 |
| 66 void SetSystemInterfaceForTest(std::unique_ptr<SystemInterface> interface); | 64 void SetSystemInterfaceForTest(std::unique_ptr<SystemInterface> interface); |
| 67 | 65 |
| 68 // Overridden from base::PowerObserver: | 66 // Overridden from base::PowerObserver: |
| 69 void OnPowerStateChange(bool on_battery_power) override; | 67 void OnPowerStateChange(bool on_battery_power) override; |
| 70 void OnResume() override; | 68 void OnResume() override; |
| 71 void OnSuspend() override; | 69 void OnSuspend() override; |
| 72 | 70 |
| 73 // Overridden from NotificationObserver: | 71 // Overridden from NotificationObserver: |
| 74 void Observe(int type, | 72 void Observe(int type, |
| 75 const NotificationSource& source, | 73 const content::NotificationSource& source, |
| 76 const NotificationDetails& details) override; | 74 const content::NotificationDetails& details) override; |
| 77 | 75 |
| 78 private: | 76 private: |
| 79 friend class PowerUsageMonitorTest; | 77 friend class PowerUsageMonitorTest; |
| 80 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); | 78 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnBatteryStatusUpdate); |
| 81 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); | 79 FRIEND_TEST_ALL_PREFIXES(PowerUsageMonitorTest, OnRenderProcessNotification); |
| 82 | 80 |
| 83 // Start monitoring system power usage. | 81 // Start monitoring system power usage. |
| 84 // This function may be called after a delay, see Start() for details. | 82 // This function may be called after a delay, see Start() for details. |
| 85 void StartInternal(); | 83 void StartInternal(); |
| 86 | 84 |
| 87 void OnBatteryStatusUpdate(const device::BatteryStatus& status); | 85 void OnBatteryStatusUpdate(const device::BatteryStatus& status); |
| 88 void OnRenderProcessNotification(int type, int rph_id); | 86 void OnRenderProcessNotification(int type, int rph_id); |
| 89 | 87 |
| 90 void DischargeStarted(double battery_level); | 88 void DischargeStarted(double battery_level); |
| 91 void WallPowerConnected(double battery_level); | 89 void WallPowerConnected(double battery_level); |
| 92 | 90 |
| 93 void CancelPendingHistogramReporting(); | 91 void CancelPendingHistogramReporting(); |
| 94 | 92 |
| 95 device::BatteryStatusService::BatteryUpdateCallback callback_; | 93 device::BatteryStatusService::BatteryUpdateCallback callback_; |
| 96 std::unique_ptr<device::BatteryStatusService::BatteryUpdateSubscription> | 94 std::unique_ptr<device::BatteryStatusService::BatteryUpdateSubscription> |
| 97 subscription_; | 95 subscription_; |
| 98 | 96 |
| 99 NotificationRegistrar registrar_; | 97 content::NotificationRegistrar registrar_; |
| 100 | 98 |
| 101 std::unique_ptr<SystemInterface> system_interface_; | 99 std::unique_ptr<SystemInterface> system_interface_; |
| 102 | 100 |
| 103 // True if monitoring was started (Start() called). | 101 // True if monitoring was started (Start() called). |
| 104 bool started_; | 102 bool started_; |
| 105 | 103 |
| 106 // True if collecting metrics for the current discharge cycle e.g. if no | 104 // True if collecting metrics for the current discharge cycle e.g. if no |
| 107 // renderers are open we don't keep track of discharge. | 105 // renderers are open we don't keep track of discharge. |
| 108 bool tracking_discharge_; | 106 bool tracking_discharge_; |
| 109 | 107 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 121 // Timestamp when wall power was disconnected, null Time object otherwise. | 119 // Timestamp when wall power was disconnected, null Time object otherwise. |
| 122 base::Time start_discharge_time_; | 120 base::Time start_discharge_time_; |
| 123 | 121 |
| 124 // IDs of live renderer processes. | 122 // IDs of live renderer processes. |
| 125 base::hash_set<int> live_renderer_ids_; | 123 base::hash_set<int> live_renderer_ids_; |
| 126 | 124 |
| 127 private: | 125 private: |
| 128 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | 126 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); |
| 129 }; | 127 }; |
| 130 | 128 |
| 131 } // namespace content | 129 #endif // CHROME_BROWSER_POWER_USAGE_MONITOR_POWER_USAGE_MONITOR_H_ |
| 132 | |
| 133 #endif // CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
| OLD | NEW |