Chromium Code Reviews| Index: content/browser/power_usage_monitor_impl.h |
| diff --git a/content/browser/power_usage_monitor_impl.h b/content/browser/power_usage_monitor_impl.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a890c7e11dd405434672a20a885dbbb06a75d3b1 |
| --- /dev/null |
| +++ b/content/browser/power_usage_monitor_impl.h |
| @@ -0,0 +1,125 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ |
| +#define CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ |
| + |
| +#include "base/basictypes.h" |
| +#include "base/containers/hash_tables.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/power_monitor/power_monitor.h" |
| +#include "base/time/time.h" |
| +#include "content/browser/battery_status/battery_status_service.h" |
| +#include "content/public/browser/browser_message_filter.h" |
| +#include "content/public/browser/notification_observer.h" |
| +#include "content/public/browser/notification_registrar.h" |
| +#include "third_party/WebKit/public/platform/WebBatteryStatus.h" |
| + |
| +namespace content { |
| + |
| +// Record statistics on power usage. |
| +// |
| +// Two main statics are recorded by this class: |
| +// * Power.BatteryDischarge_{5,15,30} - delta between battery level when |
| +// unplugged from wallpower, over the specified period - in minutes. |
| +// * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge |
| +// from the device being unplugged until it's plugged back in, if said period |
| +// was longer than 30 minutes. |
| +// |
| +// Heuristics: |
| +// * Metrics are only collected once per day. |
| +// * If the machine goes to sleep or all renderers are closed then the current |
| +// measurement is cancelled. |
| +class PowerUsageMonitor : public base::PowerObserver, |
| + public NotificationObserver { |
| + public: |
| + class SystemInterface { |
| + public: |
| + virtual ~SystemInterface() {} |
| + |
| + virtual void ScheduleHistogramReport(base::TimeDelta delay) = 0; |
| + virtual void CancelPendingHistogramReports() = 0; |
| + |
| + // Allow tests to override clock. |
| + virtual base::Time Now() = 0; |
| + |
| + protected: |
| + virtual void ReportBatteryLevelHistogram( |
| + base::Time start_time, |
| + base::TimeDelta discharge_time) = 0; |
| + }; |
| + |
| + public: |
| + PowerUsageMonitor(); |
| + virtual ~PowerUsageMonitor(); |
| + |
| + // Start monitoring power usage. |
| + void Start(); |
| + void StartOnIOThread(); |
|
Daniel Erat
2014/10/23 19:05:35
StartOnIOThread looks like it should be private in
jeremy
2014/10/27 08:25:12
Done.
|
| + |
| + void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); |
|
Daniel Erat
2014/10/23 19:05:35
i think that this should be private too
jeremy
2014/10/27 08:25:12
Done.
|
| + |
| + double DischargeAmount() { |
|
Daniel Erat
2014/10/23 19:05:35
rename to discharge_amount since it's inlined
jeremy
2014/10/27 08:25:12
Done.
|
| + return initial_battery_level_ - current_battery_level_; |
| + } |
| + |
| + // Called by SystemInterface after a histogram was succesfully recorded. |
| + void BatteryLevelReported(); |
| + |
| + void SetSystemInterfaceForTest(scoped_ptr<SystemInterface> interface); |
| + void SetNumLiveRenderersForTest(int num_renderers); |
|
Daniel Erat
2014/10/23 19:05:35
maybe just SetRenderersForTest?
jeremy
2014/10/27 08:25:12
Done.
|
| + |
| + // Overridden from base::PowerObserver: |
| + void OnPowerStateChange(bool on_battery_power) override {}; |
| + void OnResume() override {}; |
| + void OnSuspend() override; |
| + |
| + // Overridden from NotificationObserver: |
| + virtual void Observe(int type, |
|
Daniel Erat
2014/10/23 19:05:35
omit 'virtual' when using 'override'
jeremy
2014/10/27 08:25:12
Done.
|
| + const NotificationSource& source, |
| + const NotificationDetails& details) override; |
| + |
| + private: |
| + void ReportBatteryLevelHistogram(base::Time discharge_time); |
|
Daniel Erat
2014/10/23 19:05:35
this looks unused; delete it
jeremy
2014/10/27 08:25:12
Done.
|
| + void DischargeStarted(double battery_level); |
| + void WallPowerConnected(double battery_level); |
| + |
| + void CancelPendingHistogramRecording(); |
|
Daniel Erat
2014/10/23 19:05:35
s/Recording/Reporting/?
jeremy
2014/10/27 08:25:12
Done.
|
| + |
| + BatteryStatusService::BatteryUpdateCallback callback_; |
| + scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; |
| + |
| + NotificationRegistrar registrar_; |
| + |
| + scoped_ptr<SystemInterface> system_interface_; |
| + |
| + // True if monitoring was started (Start() called). |
| + bool started_; |
| + |
| + // True if the system is running on battery power, false if on wall power. |
| + bool on_battery_power_; |
| + |
| + // Battery level when wall power disconnected, 0 if on wall power. |
| + double initial_battery_level_; |
| + |
| + // Current battery level, 0 if on wall power. |
| + double current_battery_level_; |
| + |
| + // Timestamp when wall power was disconnected, null Time object otherwise. |
| + base::Time start_discharge_time_; |
| + |
| + // Timestamp for when the first histogram is reported in a 24 hour period. |
| + // Used for rate-limiting. |
| + base::Time first_histogram_report_timestamp_; |
|
Daniel Erat
2014/10/23 19:05:34
s/timestamp/time/ (or s/time/timestamp/ in start_d
jeremy
2014/10/27 08:25:12
Done.
|
| + |
| + // IDs of live rendere processes. |
|
Daniel Erat
2014/10/23 19:05:35
s/rendere/renderer/
jeremy
2014/10/27 08:25:12
Done.
|
| + base::hash_set<int> live_renderer_ids_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ |