| 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 "device/battery/battery_status_manager_win.h" | 5 #include "device/battery/battery_status_manager_win.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // e.g. when a power source is connected. | 71 // e.g. when a power source is connected. |
| 72 // TODO(timvolodine) : consider polling for battery changes on windows | 72 // TODO(timvolodine) : consider polling for battery changes on windows |
| 73 // versions prior to Vista, see crbug.com/402466. | 73 // versions prior to Vista, see crbug.com/402466. |
| 74 power_handle_ = | 74 power_handle_ = |
| 75 RegisterNotification(&GUID_ACDC_POWER_SOURCE); | 75 RegisterNotification(&GUID_ACDC_POWER_SOURCE); |
| 76 battery_change_handle_ = | 76 battery_change_handle_ = |
| 77 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); | 77 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); |
| 78 } else { | 78 } else { |
| 79 // Could not create a message window, execute callback with the default | 79 // Could not create a message window, execute callback with the default |
| 80 // values. | 80 // values. |
| 81 callback_.Run(BatteryStatus()); | 81 callback_.Run(mojom::BatteryStatus()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 UpdateNumberBatteriesHistogram(); | 84 UpdateNumberBatteriesHistogram(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void Stop() { | 87 void Stop() { |
| 88 if (power_handle_) { | 88 if (power_handle_) { |
| 89 UnregisterNotification(power_handle_); | 89 UnregisterNotification(power_handle_); |
| 90 power_handle_ = NULL; | 90 power_handle_ = NULL; |
| 91 } | 91 } |
| 92 if (battery_change_handle_) { | 92 if (battery_change_handle_) { |
| 93 UnregisterNotification(battery_change_handle_); | 93 UnregisterNotification(battery_change_handle_); |
| 94 battery_change_handle_ = NULL; | 94 battery_change_handle_ = NULL; |
| 95 } | 95 } |
| 96 window_.reset(); | 96 window_.reset(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 void BatteryChanged() { | 100 void BatteryChanged() { |
| 101 SYSTEM_POWER_STATUS win_status; | 101 SYSTEM_POWER_STATUS win_status; |
| 102 if (GetSystemPowerStatus(&win_status)) | 102 if (GetSystemPowerStatus(&win_status)) |
| 103 callback_.Run(ComputeWebBatteryStatus(win_status)); | 103 callback_.Run(ComputeWebBatteryStatus(win_status)); |
| 104 else | 104 else |
| 105 callback_.Run(BatteryStatus()); | 105 callback_.Run(mojom::BatteryStatus()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 bool HandleMessage(UINT message, | 108 bool HandleMessage(UINT message, |
| 109 WPARAM wparam, | 109 WPARAM wparam, |
| 110 LPARAM lparam, | 110 LPARAM lparam, |
| 111 LRESULT* result) { | 111 LRESULT* result) { |
| 112 switch(message) { | 112 switch(message) { |
| 113 case WM_POWERBROADCAST: | 113 case WM_POWERBROADCAST: |
| 114 if (wparam == PBT_APMPOWERSTATUSCHANGE || | 114 if (wparam == PBT_APMPOWERSTATUSCHANGE || |
| 115 wparam == PBT_POWERSETTINGCHANGE) { | 115 wparam == PBT_POWERSETTINGCHANGE) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 void StopListeningBatteryChange() override { battery_observer_->Stop(); } | 174 void StopListeningBatteryChange() override { battery_observer_->Stop(); } |
| 175 | 175 |
| 176 private: | 176 private: |
| 177 std::unique_ptr<BatteryStatusObserver> battery_observer_; | 177 std::unique_ptr<BatteryStatusObserver> battery_observer_; |
| 178 | 178 |
| 179 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); | 179 DISALLOW_COPY_AND_ASSIGN(BatteryStatusManagerWin); |
| 180 }; | 180 }; |
| 181 | 181 |
| 182 } // namespace | 182 } // namespace |
| 183 | 183 |
| 184 BatteryStatus ComputeWebBatteryStatus(const SYSTEM_POWER_STATUS& win_status) { | 184 mojom::BatteryStatus ComputeWebBatteryStatus( |
| 185 BatteryStatus status; | 185 const SYSTEM_POWER_STATUS& win_status) { |
| 186 mojom::BatteryStatus status; |
| 186 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; | 187 status.charging = win_status.ACLineStatus != WIN_AC_LINE_STATUS_OFFLINE; |
| 187 | 188 |
| 188 // Set level if available. Otherwise keep the default value which is 1. | 189 // Set level if available. Otherwise keep the default value which is 1. |
| 189 if (win_status.BatteryLifePercent != 255) { | 190 if (win_status.BatteryLifePercent != 255) { |
| 190 // Convert percentage to a value between 0 and 1 with 2 significant digits. | 191 // Convert percentage to a value between 0 and 1 with 2 significant digits. |
| 191 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; | 192 status.level = static_cast<double>(win_status.BatteryLifePercent) / 100.; |
| 192 } | 193 } |
| 193 | 194 |
| 194 if (!status.charging) { | 195 if (!status.charging) { |
| 195 // Set discharging_time if available otherwise keep the default value, | 196 // Set discharging_time if available otherwise keep the default value, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 207 } | 208 } |
| 208 | 209 |
| 209 // static | 210 // static |
| 210 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 211 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
| 211 const BatteryStatusService::BatteryUpdateCallback& callback) { | 212 const BatteryStatusService::BatteryUpdateCallback& callback) { |
| 212 return std::unique_ptr<BatteryStatusManager>( | 213 return std::unique_ptr<BatteryStatusManager>( |
| 213 new BatteryStatusManagerWin(callback)); | 214 new BatteryStatusManagerWin(callback)); |
| 214 } | 215 } |
| 215 | 216 |
| 216 } // namespace device | 217 } // namespace device |
| OLD | NEW |