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_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
6 #define CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/containers/hash_tables.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/power_monitor/power_monitor.h" | |
12 #include "base/time/time.h" | |
13 #include "content/browser/battery_status/battery_status_service.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 "third_party/WebKit/public/platform/WebBatteryStatus.h" | |
18 | |
19 namespace content { | |
20 | |
21 // Record statistics on power usage. | |
22 // | |
23 // Two main statics are recorded by this class: | |
24 // * Power.BatteryDischarge_{5,15,30} - delta between battery level when | |
25 // unplugged from wallpower, over the specified period - in minutes. | |
26 // * Power.BatteryDischargeRateWhenUnplugged - the rate of battery discharge | |
27 // from the device being unplugged until it's plugged back in, if said period | |
28 // was longer than 30 minutes. | |
29 // | |
30 // Heuristics: | |
31 // * Metrics are only collected once per day. | |
32 // * If the machine goes to sleep or all renderers are closed then the current | |
33 // measurement is cancelled. | |
34 // | |
35 // Threading: Most methods are called on the IO thread. | |
36 class PowerUsageMonitor : base::PowerObserver, NotificationObserver { | |
Daniel Erat
2014/09/30 23:55:24
use public inheritance
| |
37 public: | |
38 class HistogramRecorder { | |
39 public: | |
40 HistogramRecorder(); | |
41 virtual ~HistogramRecorder(); | |
42 | |
43 virtual void ReportBatteryLevelHistogram(base::TimeDelta discharge_time); | |
Daniel Erat
2014/09/30 23:55:24
why virtual? are you planning to create a derived
| |
44 | |
45 void CancelPendingHistgramReports(); | |
Daniel Erat
2014/09/30 23:55:25
add blank line after this
| |
46 base::WeakPtr<HistogramRecorder> GetWeakPtr() { | |
47 return weak_ptr_factory_.GetWeakPtr(); | |
48 } | |
49 | |
50 // Used to cancel in progress timers. | |
Daniel Erat
2014/09/30 23:55:24
this should be private
| |
51 base::WeakPtrFactory<HistogramRecorder> weak_ptr_factory_; | |
52 }; | |
53 | |
54 public: | |
55 // Returns the PowerUsageMonitor singleton. | |
56 static PowerUsageMonitor* GetInstance(); | |
57 | |
58 // Start monitoring power usage. | |
59 void Initialize(); | |
60 void StartOnIOThread(); | |
61 | |
62 void OnBatteryStatusUpdate(const blink::WebBatteryStatus& status); | |
63 | |
64 double DischargeAmount() { | |
65 return initial_battery_level_ - current_battery_level_; | |
66 } | |
67 | |
68 // Called by HistogramRecorder after a histogram was succesfully recorded. | |
69 void BatteryLevelReported(); | |
70 | |
71 void SetHistogramRecorderForTest(scoped_ptr<HistogramRecorder> recorder); | |
72 | |
73 // POverridden from basde::PowerObserver: | |
Daniel Erat
2014/09/30 23:55:24
fix typos on this line
| |
74 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {}; | |
75 virtual void OnResume() OVERRIDE {}; | |
76 virtual void OnSuspend() OVERRIDE; | |
77 | |
78 // Overridden from NotificationObserver: | |
79 virtual void Observe(int type, | |
80 const NotificationSource& source, | |
81 const NotificationDetails& details) OVERRIDE; | |
82 | |
83 | |
84 private: | |
85 friend struct DefaultSingletonTraits<PowerUsageMonitor>; | |
86 | |
87 PowerUsageMonitor(); | |
88 virtual ~PowerUsageMonitor(); | |
89 | |
90 void ReportBatteryLevelHistogram(base::Time discharge_time); | |
91 void DischargeStarted(double battery_level); | |
92 void WallPowerConnected(double battery_level); | |
93 | |
94 void CancelPendingHistogramRecordingOnIOThread(); | |
95 | |
96 BatteryStatusService::BatteryUpdateCallback callback_; | |
97 scoped_ptr<BatteryStatusService::BatteryUpdateSubscription> subscription_; | |
98 | |
99 NotificationRegistrar registrar_; | |
100 | |
101 scoped_ptr<HistogramRecorder> histogram_recorder_; | |
102 | |
103 bool was_on_battery_power_; | |
104 double initial_battery_level_; | |
105 double current_battery_level_; | |
106 base::Time start_discharge_time_; | |
107 | |
108 // Timestamp for when the first histogram is reported in a 24 hour period. | |
109 // Used for rate-limiting. | |
110 base::Time first_histogram_report_timestamp_; | |
111 | |
112 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
| |
113 | |
114 private: | |
115 DISALLOW_COPY_AND_ASSIGN(PowerUsageMonitor); | |
116 }; | |
117 | |
118 } // namespace content | |
119 | |
120 #endif // CHROME_CONTENT_BROWSER_POWER_USAGE_MONITOR_IMPL_H_ | |
OLD | NEW |