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..946ab8bff22870147bcc1b5539c3a446fc62ac1c |
| --- /dev/null |
| +++ b/content/browser/power_usage_monitor_impl.h |
| @@ -0,0 +1,120 @@ |
| +// 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. |
| +// |
| +// Threading: Most methods are called on the IO thread. |
| +class PowerUsageMonitor : base::PowerObserver, NotificationObserver { |
|
Daniel Erat
2014/09/30 23:55:24
use public inheritance
|
| + public: |
| + class HistogramRecorder { |
| + public: |
| + HistogramRecorder(); |
| + virtual ~HistogramRecorder(); |
| + |
| + virtual void ReportBatteryLevelHistogram(base::TimeDelta discharge_time); |
|
Daniel Erat
2014/09/30 23:55:24
why virtual? are you planning to create a derived
|
| + |
| + void CancelPendingHistgramReports(); |
|
Daniel Erat
2014/09/30 23:55:25
add blank line after this
|
| + base::WeakPtr<HistogramRecorder> GetWeakPtr() { |
| + return weak_ptr_factory_.GetWeakPtr(); |
| + } |
| + |
| + // Used to cancel in progress timers. |
|
Daniel Erat
2014/09/30 23:55:24
this should be private
|
| + base::WeakPtrFactory<HistogramRecorder> weak_ptr_factory_; |
| + }; |
| + |
| + public: |
| + // Returns the PowerUsageMonitor singleton. |
| + static PowerUsageMonitor* GetInstance(); |
| + |
| + // Start monitoring power usage. |
| + void Initialize(); |
| + void StartOnIOThread(); |
| + |
| + void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); |
| + |
| + double DischargeAmount() { |
| + return initial_battery_level_ - current_battery_level_; |
| + } |
| + |
| + // Called by HistogramRecorder after a histogram was succesfully recorded. |
| + void BatteryLevelReported(); |
| + |
| + void SetHistogramRecorderForTest(scoped_ptr<HistogramRecorder> recorder); |
| + |
| + // POverridden from basde::PowerObserver: |
|
Daniel Erat
2014/09/30 23:55:24
fix typos on this line
|
| + virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {}; |
| + virtual void OnResume() OVERRIDE {}; |
| + virtual void OnSuspend() OVERRIDE; |
| + |
| + // Overridden from NotificationObserver: |
| + virtual void Observe(int type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) OVERRIDE; |
| + |
| + |
| + private: |
| + friend struct DefaultSingletonTraits<PowerUsageMonitor>; |
| + |
| + PowerUsageMonitor(); |
| + virtual ~PowerUsageMonitor(); |
| + |
| + void ReportBatteryLevelHistogram(base::Time discharge_time); |
| + void DischargeStarted(double battery_level); |
| + void WallPowerConnected(double battery_level); |
| + |
| + void CancelPendingHistogramRecordingOnIOThread(); |
| + |
| + BatteryStatusService::BatteryUpdateCallback callback_; |
| + scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; |
| + |
| + NotificationRegistrar registrar_; |
| + |
| + scoped_ptr<HistogramRecorder> histogram_recorder_; |
| + |
| + bool was_on_battery_power_; |
| + double initial_battery_level_; |
| + double current_battery_level_; |
| + 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_; |
| + |
| + base::hash_set<int> live_renderers_; |
|
Daniel Erat
2014/09/30 23:55:25
hash_set is probably fine if you don't plan to eve
|
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ |