| 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/memory/ref_counted.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 9 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 10 #include "base/win/message_window.h" | 11 #include "base/win/message_window.h" |
| 11 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
| 12 #include "content/browser/battery_status/battery_status_manager.h" | 13 #include "device/battery/battery_status_manager.h" |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 | 14 |
| 15 namespace content { | 15 namespace device { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; | 19 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; |
| 20 | 20 |
| 21 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; | 21 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; |
| 22 | 22 |
| 23 // This enum is used for histogram. Don't change the order of the existing | 23 // This enum is used for histogram. Don't change the order of the existing |
| 24 // values. | 24 // values. |
| 25 enum NumberBatteriesType { | 25 enum NumberBatteriesType { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 | 44 |
| 45 if (win_status.BatteryFlag == 255) | 45 if (win_status.BatteryFlag == 255) |
| 46 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); | 46 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); |
| 47 else if (win_status.BatteryFlag == 128) | 47 else if (win_status.BatteryFlag == 128) |
| 48 UpdateNumberBatteriesHistogram(NO_BATTERY); | 48 UpdateNumberBatteriesHistogram(NO_BATTERY); |
| 49 else | 49 else |
| 50 UpdateNumberBatteriesHistogram(ONE_OR_MORE_BATTERIES); | 50 UpdateNumberBatteriesHistogram(ONE_OR_MORE_BATTERIES); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Message-only window for handling battery changes on Windows. | 53 // Message-only window for handling battery changes on Windows. |
| 54 class BatteryStatusObserver | 54 class BatteryStatusObserver { |
| 55 : public base::RefCountedThreadSafe<BatteryStatusObserver> { | |
| 56 public: | 55 public: |
| 57 explicit BatteryStatusObserver(const BatteryCallback& callback) | 56 explicit BatteryStatusObserver(const BatteryCallback& callback) |
| 58 : power_handle_(NULL), | 57 : power_handle_(NULL), |
| 59 battery_change_handle_(NULL), | 58 battery_change_handle_(NULL), |
| 60 callback_(callback) { | 59 callback_(callback) { |
| 61 } | 60 } |
| 62 | 61 |
| 63 virtual ~BatteryStatusObserver() { DCHECK(!window_); } | 62 ~BatteryStatusObserver() { DCHECK(!window_); } |
| 64 | 63 |
| 65 void Start() { | 64 void Start() { |
| 66 // Need to start on the UI thread to receive battery status notifications. | |
| 67 BrowserThread::PostTask( | |
| 68 BrowserThread::UI, | |
| 69 FROM_HERE, | |
| 70 base::Bind(&BatteryStatusObserver::StartOnUI, this)); | |
| 71 } | |
| 72 | |
| 73 void Stop() { | |
| 74 BrowserThread::PostTask( | |
| 75 BrowserThread::UI, | |
| 76 FROM_HERE, | |
| 77 base::Bind(&BatteryStatusObserver::StopOnUI, this)); | |
| 78 } | |
| 79 | |
| 80 private: | |
| 81 void StartOnUI() { | |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 83 if (window_) | |
| 84 return; | |
| 85 | |
| 86 if (CreateMessageWindow()) { | 65 if (CreateMessageWindow()) { |
| 87 BatteryChanged(); | 66 BatteryChanged(); |
| 88 // RegisterPowerSettingNotification function work from Windows Vista | 67 // RegisterPowerSettingNotification function work from Windows Vista |
| 89 // onwards. However even without them we will receive notifications, | 68 // onwards. However even without them we will receive notifications, |
| 90 // e.g. when a power source is connected. | 69 // e.g. when a power source is connected. |
| 91 // TODO(timvolodine) : consider polling for battery changes on windows | 70 // TODO(timvolodine) : consider polling for battery changes on windows |
| 92 // versions prior to Vista, see crbug.com/402466. | 71 // versions prior to Vista, see crbug.com/402466. |
| 93 power_handle_ = | 72 power_handle_ = |
| 94 RegisterNotification(&GUID_ACDC_POWER_SOURCE); | 73 RegisterNotification(&GUID_ACDC_POWER_SOURCE); |
| 95 battery_change_handle_ = | 74 battery_change_handle_ = |
| 96 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); | 75 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); |
| 97 } else { | 76 } else { |
| 98 // Could not create a message window, execute callback with the default | 77 // Could not create a message window, execute callback with the default |
| 99 // values. | 78 // values. |
| 100 callback_.Run(blink::WebBatteryStatus()); | 79 callback_.Run(BatteryStatus()); |
| 101 } | 80 } |
| 102 | 81 |
| 103 UpdateNumberBatteriesHistogram(); | 82 UpdateNumberBatteriesHistogram(); |
| 104 } | 83 } |
| 105 | 84 |
| 106 void StopOnUI() { | 85 void Stop() { |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 108 if (!window_) | |
| 109 return; | |
| 110 | |
| 111 if (power_handle_) | 86 if (power_handle_) |
| 112 UnregisterNotification(power_handle_); | 87 UnregisterNotification(power_handle_); |
| 113 if (battery_change_handle_) | 88 if (battery_change_handle_) |
| 114 UnregisterNotification(battery_change_handle_); | 89 UnregisterNotification(battery_change_handle_); |
| 115 window_.reset(); | 90 window_.reset(); |
| 116 } | 91 } |
| 117 | 92 |
| 93 private: |
| 118 void BatteryChanged() { | 94 void BatteryChanged() { |
| 119 SYSTEM_POWER_STATUS win_status; | 95 SYSTEM_POWER_STATUS win_status; |
| 120 if (GetSystemPowerStatus(&win_status)) | 96 if (GetSystemPowerStatus(&win_status)) |
| 121 callback_.Run(ComputeWebBatteryStatus(win_status)); | 97 callback_.Run(ComputeWebBatteryStatus(win_status)); |
| 122 else | 98 else |
| 123 callback_.Run(blink::WebBatteryStatus()); | 99 callback_.Run(BatteryStatus()); |
| 124 } | 100 } |
| 125 | 101 |
| 126 bool HandleMessage(UINT message, | 102 bool HandleMessage(UINT message, |
| 127 WPARAM wparam, | 103 WPARAM wparam, |
| 128 LPARAM lparam, | 104 LPARAM lparam, |
| 129 LRESULT* result) { | 105 LRESULT* result) { |
| 130 switch(message) { | 106 switch(message) { |
| 131 case WM_POWERBROADCAST: | 107 case WM_POWERBROADCAST: |
| 132 if (wparam == PBT_APMPOWERSTATUSCHANGE || | 108 if (wparam == PBT_APMPOWERSTATUSCHANGE || |
| 133 wparam == PBT_POWERSETTINGCHANGE) { | 109 wparam == PBT_POWERSETTINGCHANGE) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 154 |
| 179 class BatteryStatusManagerWin : public BatteryStatusManager { | 155 class BatteryStatusManagerWin : public BatteryStatusManager { |
| 180 public: | 156 public: |
| 181 explicit BatteryStatusManagerWin(const BatteryCallback& callback) | 157 explicit BatteryStatusManagerWin(const BatteryCallback& callback) |
| 182 : battery_observer_(new BatteryStatusObserver(callback)) {} | 158 : battery_observer_(new BatteryStatusObserver(callback)) {} |
| 183 virtual ~BatteryStatusManagerWin() { battery_observer_->Stop(); } | 159 virtual ~BatteryStatusManagerWin() { battery_observer_->Stop(); } |
| 184 | 160 |
| 185 public: | 161 public: |
| 186 // BatteryStatusManager: | 162 // BatteryStatusManager: |
| 187 virtual bool StartListeningBatteryChange() override { | 163 virtual bool StartListeningBatteryChange() override { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 189 battery_observer_->Start(); | 164 battery_observer_->Start(); |
| 190 return true; | 165 return true; |
| 191 } | 166 } |
| 192 | 167 |
| 193 virtual void StopListeningBatteryChange() override { | 168 virtual void StopListeningBatteryChange() override { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 195 battery_observer_->Stop(); | 169 battery_observer_->Stop(); |
| 196 } | 170 } |
| 197 | 171 |
| 198 private: | 172 private: |
| 199 scoped_refptr<BatteryStatusObserver> battery_observer_; | 173 scoped_ptr<BatteryStatusObserver> battery_observer_; |
| 200 | 174 |
| 201 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); | 175 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); |
| 202 }; | 176 }; |
| 203 | 177 |
| 204 } // namespace | 178 } // namespace |
| 205 | 179 |
| 206 blink::WebBatteryStatus ComputeWebBatteryStatus( | 180 BatteryStatus ComputeWebBatteryStatus(const SYSTEM_POWER_STATUS& win_status) { |
| 207 const SYSTEM_POWER_STATUS& win_status) { | 181 BatteryStatus status; |
| 208 blink::WebBatteryStatus status; | |
| 209 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; | 182 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; |
| 210 | 183 |
| 211 // Set level if available. Otherwise keep the default value which is 1. | 184 // Set level if available. Otherwise keep the default value which is 1. |
| 212 if (win_status.BatteryLifePercent != 255) { | 185 if (win_status.BatteryLifePercent != 255) { |
| 213 // Convert percentage to a value between 0 and 1 with 2 significant digits. | 186 // Convert percentage to a value between 0 and 1 with 2 significant digits. |
| 214 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; | 187 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; |
| 215 } | 188 } |
| 216 | 189 |
| 217 if (!status.charging) { | 190 if (!status.charging) { |
| 218 // Set dischargingTime if available otherwise keep the default value, | 191 // Set discharging_time if available otherwise keep the default value, |
| 219 // which is +Infinity. | 192 // which is +Infinity. |
| 220 if (win_status.BatteryLifeTime != (DWORD)-1) | 193 if (win_status.BatteryLifeTime != (DWORD)-1) |
| 221 status.dischargingTime = win_status.BatteryLifeTime; | 194 status.discharging_time = win_status.BatteryLifeTime; |
| 222 status.chargingTime = std::numeric_limits<double>::infinity(); | 195 status.charging_time = std::numeric_limits<double>::infinity(); |
| 223 } else { | 196 } else { |
| 224 // Set chargingTime to +Infinity if not fully charged, otherwise leave the | 197 // Set charging_time to +Infinity if not fully charged, otherwise leave the |
| 225 // default value, which is 0. | 198 // default value, which is 0. |
| 226 if (status.level < 1) | 199 if (status.level < 1) |
| 227 status.chargingTime = std::numeric_limits<double>::infinity(); | 200 status.charging_time = std::numeric_limits<double>::infinity(); |
| 228 } | 201 } |
| 229 return status; | 202 return status; |
| 230 } | 203 } |
| 231 | 204 |
| 232 // static | 205 // static |
| 233 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 206 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
| 234 const BatteryStatusService::BatteryUpdateCallback& callback) { | 207 const BatteryStatusService::BatteryUpdateCallback& callback) { |
| 235 return scoped_ptr<BatteryStatusManager>( | 208 return scoped_ptr<BatteryStatusManager>( |
| 236 new BatteryStatusManagerWin(callback)); | 209 new BatteryStatusManagerWin(callback)); |
| 237 } | 210 } |
| 238 | 211 |
| 239 } // namespace content | 212 } // namespace device |
| OLD | NEW |