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 #include "content/browser/battery_status/battery_status_manager_win.h" | 5 #include "device/battery/battery_status_manager_win.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
8 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
9 #include "base/win/message_window.h" | 10 #include "base/win/message_window.h" |
10 #include "base/win/windows_version.h" | 11 #include "base/win/windows_version.h" |
11 #include "content/browser/battery_status/battery_status_manager.h" | 12 #include "device/battery/battery_status_manager.h" |
12 #include "content/public/browser/browser_thread.h" | |
13 | 13 |
14 namespace content { | 14 namespace device { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; | 18 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; |
19 | 19 |
20 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; | 20 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; |
21 | 21 |
22 // Message-only window for handling battery changes on Windows. | 22 // Message-only window for handling battery changes on Windows. |
23 class BatteryStatusObserver | 23 class BatteryStatusObserver |
24 : public base::RefCountedThreadSafe<BatteryStatusObserver> { | 24 : public base::RefCountedThreadSafe<BatteryStatusObserver> { |
25 public: | 25 public: |
26 explicit BatteryStatusObserver(const BatteryCallback& callback) | 26 explicit BatteryStatusObserver(const BatteryCallback& callback) |
27 : power_handle_(NULL), | 27 : power_handle_(NULL), |
28 battery_change_handle_(NULL), | 28 battery_change_handle_(NULL), |
29 callback_(callback) { | 29 callback_(callback) { |
30 } | 30 } |
31 | 31 |
32 virtual ~BatteryStatusObserver() { DCHECK(!window_); } | 32 virtual ~BatteryStatusObserver() { DCHECK(!window_); } |
33 | 33 |
34 void Start() { | 34 void Start() { |
35 // Need to start on the UI thread to receive battery status notifications. | |
36 BrowserThread::PostTask( | |
37 BrowserThread::UI, | |
38 FROM_HERE, | |
39 base::Bind(&BatteryStatusObserver::StartOnUI, this)); | |
40 } | |
41 | |
42 void Stop() { | |
43 BrowserThread::PostTask( | |
44 BrowserThread::UI, | |
45 FROM_HERE, | |
46 base::Bind(&BatteryStatusObserver::StopOnUI, this)); | |
47 } | |
48 | |
49 private: | |
50 void StartOnUI() { | |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
52 if (window_) | |
53 return; | |
54 | |
55 if (CreateMessageWindow()) { | 35 if (CreateMessageWindow()) { |
56 BatteryChanged(); | 36 BatteryChanged(); |
57 // RegisterPowerSettingNotification function work from Windows Vista | 37 // RegisterPowerSettingNotification function work from Windows Vista |
58 // onwards. However even without them we will receive notifications, | 38 // onwards. However even without them we will receive notifications, |
59 // e.g. when a power source is connected. | 39 // e.g. when a power source is connected. |
60 // TODO(timvolodine) : consider polling for battery changes on windows | 40 // TODO(timvolodine) : consider polling for battery changes on windows |
61 // versions prior to Vista, see crbug.com/402466. | 41 // versions prior to Vista, see crbug.com/402466. |
62 power_handle_ = | 42 power_handle_ = |
63 RegisterNotification(&GUID_ACDC_POWER_SOURCE); | 43 RegisterNotification(&GUID_ACDC_POWER_SOURCE); |
64 battery_change_handle_ = | 44 battery_change_handle_ = |
65 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); | 45 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); |
66 } else { | 46 } else { |
67 // Could not create a message window, execute callback with the default | 47 // Could not create a message window, execute callback with the default |
68 // values. | 48 // values. |
69 callback_.Run(blink::WebBatteryStatus()); | 49 callback_.Run(BatteryStatus()); |
70 } | 50 } |
71 } | 51 } |
72 | 52 |
73 void StopOnUI() { | 53 void Stop() { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
75 if (!window_) | |
76 return; | |
77 | |
78 if (power_handle_) | 54 if (power_handle_) |
79 UnregisterNotification(power_handle_); | 55 UnregisterNotification(power_handle_); |
80 if (battery_change_handle_) | 56 if (battery_change_handle_) |
81 UnregisterNotification(battery_change_handle_); | 57 UnregisterNotification(battery_change_handle_); |
82 window_.reset(); | 58 window_.reset(); |
83 } | 59 } |
84 | 60 |
61 private: | |
85 void BatteryChanged() { | 62 void BatteryChanged() { |
86 SYSTEM_POWER_STATUS win_status; | 63 SYSTEM_POWER_STATUS win_status; |
87 if (GetSystemPowerStatus(&win_status)) | 64 if (GetSystemPowerStatus(&win_status)) |
88 callback_.Run(ComputeWebBatteryStatus(win_status)); | 65 callback_.Run(ComputeWebBatteryStatus(win_status)); |
89 else | 66 else |
90 callback_.Run(blink::WebBatteryStatus()); | 67 callback_.Run(BatteryStatus()); |
91 } | 68 } |
92 | 69 |
93 bool HandleMessage(UINT message, | 70 bool HandleMessage(UINT message, |
94 WPARAM wparam, | 71 WPARAM wparam, |
95 LPARAM lparam, | 72 LPARAM lparam, |
96 LRESULT* result) { | 73 LRESULT* result) { |
97 switch(message) { | 74 switch(message) { |
98 case WM_POWERBROADCAST: | 75 case WM_POWERBROADCAST: |
99 if (wparam == PBT_APMPOWERSTATUSCHANGE || | 76 if (wparam == PBT_APMPOWERSTATUSCHANGE || |
100 wparam == PBT_POWERSETTINGCHANGE) { | 77 wparam == PBT_POWERSETTINGCHANGE) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 | 122 |
146 class BatteryStatusManagerWin : public BatteryStatusManager { | 123 class BatteryStatusManagerWin : public BatteryStatusManager { |
147 public: | 124 public: |
148 explicit BatteryStatusManagerWin(const BatteryCallback& callback) | 125 explicit BatteryStatusManagerWin(const BatteryCallback& callback) |
149 : battery_observer_(new BatteryStatusObserver(callback)) {} | 126 : battery_observer_(new BatteryStatusObserver(callback)) {} |
150 virtual ~BatteryStatusManagerWin() { battery_observer_->Stop(); } | 127 virtual ~BatteryStatusManagerWin() { battery_observer_->Stop(); } |
151 | 128 |
152 public: | 129 public: |
153 // BatteryStatusManager: | 130 // BatteryStatusManager: |
154 virtual bool StartListeningBatteryChange() OVERRIDE { | 131 virtual bool StartListeningBatteryChange() OVERRIDE { |
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
156 battery_observer_->Start(); | 132 battery_observer_->Start(); |
157 return true; | 133 return true; |
158 } | 134 } |
159 | 135 |
160 virtual void StopListeningBatteryChange() OVERRIDE { | 136 virtual void StopListeningBatteryChange() OVERRIDE { |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
162 battery_observer_->Stop(); | 137 battery_observer_->Stop(); |
163 } | 138 } |
164 | 139 |
165 private: | 140 private: |
166 scoped_refptr<BatteryStatusObserver> battery_observer_; | 141 scoped_refptr<BatteryStatusObserver> battery_observer_; |
timvolodine
2014/09/25 23:08:33
same here, doesn't look like we need a scoped_refp
ppi
2014/09/29 16:43:27
Done.
| |
167 | 142 |
168 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); | 143 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); |
169 }; | 144 }; |
170 | 145 |
171 } // namespace | 146 } // namespace |
172 | 147 |
173 blink::WebBatteryStatus ComputeWebBatteryStatus( | 148 BatteryStatus ComputeWebBatteryStatus(const SYSTEM_POWER_STATUS& win_status) { |
174 const SYSTEM_POWER_STATUS& win_status) { | 149 BatteryStatus status; |
175 blink::WebBatteryStatus status; | |
176 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; | 150 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; |
177 | 151 |
178 // Set level if available. Otherwise keep the default value which is 1. | 152 // Set level if available. Otherwise keep the default value which is 1. |
179 if (win_status.BatteryLifePercent != 255) { | 153 if (win_status.BatteryLifePercent != 255) { |
180 // Convert percentage to a value between 0 and 1 with 2 significant digits. | 154 // Convert percentage to a value between 0 and 1 with 2 significant digits. |
181 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; | 155 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; |
182 } | 156 } |
183 | 157 |
184 if (!status.charging) { | 158 if (!status.charging) { |
185 // Set dischargingTime if available otherwise keep the default value, | 159 // Set discharging_time if available otherwise keep the default value, |
186 // which is +Infinity. | 160 // which is +Infinity. |
187 if (win_status.BatteryLifeTime != (DWORD)-1) | 161 if (win_status.BatteryLifeTime != (DWORD)-1) |
188 status.dischargingTime = win_status.BatteryLifeTime; | 162 status.discharging_time = win_status.BatteryLifeTime; |
189 status.chargingTime = std::numeric_limits<double>::infinity(); | 163 status.charging_time = std::numeric_limits<double>::infinity(); |
190 } else { | 164 } else { |
191 // Set chargingTime to +Infinity if not fully charged, otherwise leave the | 165 // Set charging_time to +Infinity if not fully charged, otherwise leave the |
192 // default value, which is 0. | 166 // default value, which is 0. |
193 if (status.level < 1) | 167 if (status.level < 1) |
194 status.chargingTime = std::numeric_limits<double>::infinity(); | 168 status.charging_time = std::numeric_limits<double>::infinity(); |
195 } | 169 } |
196 return status; | 170 return status; |
197 } | 171 } |
198 | 172 |
199 // static | 173 // static |
200 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 174 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
201 const BatteryStatusService::BatteryUpdateCallback& callback) { | 175 const BatteryStatusService::BatteryUpdateCallback& callback) { |
202 return scoped_ptr<BatteryStatusManager>( | 176 return scoped_ptr<BatteryStatusManager>( |
203 new BatteryStatusManagerWin(callback)); | 177 new BatteryStatusManagerWin(callback)); |
204 } | 178 } |
205 | 179 |
206 } // namespace content | 180 } // namespace device |
OLD | NEW |