Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: services/device/battery/battery_status_manager_win.cc

Issue 2818673003: [DeviceService] Expose battery monitoring solely via the Device Service (Closed)
Patch Set: Java file format change Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "services/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"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "base/win/message_window.h" 13 #include "base/win/message_window.h"
14 #include "base/win/windows_version.h" 14 #include "base/win/windows_version.h"
15 #include "device/battery/battery_status_manager.h" 15 #include "services/device/battery/battery_status_manager.h"
16 16
17 namespace device { 17 namespace device {
18 18
19 namespace { 19 namespace {
20 20
21 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; 21 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback;
22 22
23 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; 23 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow";
24 24
25 // This enum is used for histogram. Don't change the order of the existing 25 // This enum is used for histogram. Don't change the order of the existing
26 // values. 26 // values.
27 enum NumberBatteriesType { 27 enum NumberBatteriesType {
28 UNKNOWN_BATTERIES = 0, 28 UNKNOWN_BATTERIES = 0,
29 NO_BATTERY = 1, 29 NO_BATTERY = 1,
30 ONE_OR_MORE_BATTERIES = 2, 30 ONE_OR_MORE_BATTERIES = 2,
31 BATTERY_TYPES_COUNT = 3, 31 BATTERY_TYPES_COUNT = 3,
32 }; 32 };
33 33
34 void UpdateNumberBatteriesHistogram(NumberBatteriesType count) { 34 void UpdateNumberBatteriesHistogram(NumberBatteriesType count) {
35 UMA_HISTOGRAM_ENUMERATION("BatteryStatus.NumberBatteriesWin", 35 UMA_HISTOGRAM_ENUMERATION("BatteryStatus.NumberBatteriesWin", count,
36 count,
37 BATTERY_TYPES_COUNT); 36 BATTERY_TYPES_COUNT);
38 } 37 }
39 38
40 void UpdateNumberBatteriesHistogram() { 39 void UpdateNumberBatteriesHistogram() {
41 SYSTEM_POWER_STATUS win_status; 40 SYSTEM_POWER_STATUS win_status;
42 if (!GetSystemPowerStatus(&win_status)) { 41 if (!GetSystemPowerStatus(&win_status)) {
43 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); 42 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES);
44 return; 43 return;
45 } 44 }
46 45
47 if (win_status.BatteryFlag == 255) 46 if (win_status.BatteryFlag == 255)
48 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); 47 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES);
49 else if (win_status.BatteryFlag == 128) 48 else if (win_status.BatteryFlag == 128)
50 UpdateNumberBatteriesHistogram(NO_BATTERY); 49 UpdateNumberBatteriesHistogram(NO_BATTERY);
51 else 50 else
52 UpdateNumberBatteriesHistogram(ONE_OR_MORE_BATTERIES); 51 UpdateNumberBatteriesHistogram(ONE_OR_MORE_BATTERIES);
53 } 52 }
54 53
55 // Message-only window for handling battery changes on Windows. 54 // Message-only window for handling battery changes on Windows.
56 class BatteryStatusObserver { 55 class BatteryStatusObserver {
57 public: 56 public:
58 explicit BatteryStatusObserver(const BatteryCallback& callback) 57 explicit BatteryStatusObserver(const BatteryCallback& callback)
59 : power_handle_(NULL), 58 : power_handle_(NULL),
60 battery_change_handle_(NULL), 59 battery_change_handle_(NULL),
61 callback_(callback) { 60 callback_(callback) {}
62 }
63 61
64 ~BatteryStatusObserver() { DCHECK(!window_); } 62 ~BatteryStatusObserver() { DCHECK(!window_); }
65 63
66 void Start() { 64 void Start() {
67 if (CreateMessageWindow()) { 65 if (CreateMessageWindow()) {
68 BatteryChanged(); 66 BatteryChanged();
69 // RegisterPowerSettingNotification function work from Windows Vista 67 // RegisterPowerSettingNotification function work from Windows Vista
70 // onwards. However even without them we will receive notifications, 68 // onwards. However even without them we will receive notifications,
71 // e.g. when a power source is connected. 69 // e.g. when a power source is connected.
72 // TODO(timvolodine) : consider polling for battery changes on windows 70 // TODO(timvolodine) : consider polling for battery changes on windows
73 // versions prior to Vista, see crbug.com/402466. 71 // versions prior to Vista, see crbug.com/402466.
74 power_handle_ = 72 power_handle_ = RegisterNotification(&GUID_ACDC_POWER_SOURCE);
75 RegisterNotification(&GUID_ACDC_POWER_SOURCE);
76 battery_change_handle_ = 73 battery_change_handle_ =
77 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); 74 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING);
78 } else { 75 } else {
79 // Could not create a message window, execute callback with the default 76 // Could not create a message window, execute callback with the default
80 // values. 77 // values.
81 callback_.Run(mojom::BatteryStatus()); 78 callback_.Run(mojom::BatteryStatus());
82 } 79 }
83 80
84 UpdateNumberBatteriesHistogram(); 81 UpdateNumberBatteriesHistogram();
85 } 82 }
(...skipping 16 matching lines...) Expand all
102 if (GetSystemPowerStatus(&win_status)) 99 if (GetSystemPowerStatus(&win_status))
103 callback_.Run(ComputeWebBatteryStatus(win_status)); 100 callback_.Run(ComputeWebBatteryStatus(win_status));
104 else 101 else
105 callback_.Run(mojom::BatteryStatus()); 102 callback_.Run(mojom::BatteryStatus());
106 } 103 }
107 104
108 bool HandleMessage(UINT message, 105 bool HandleMessage(UINT message,
109 WPARAM wparam, 106 WPARAM wparam,
110 LPARAM lparam, 107 LPARAM lparam,
111 LRESULT* result) { 108 LRESULT* result) {
112 switch(message) { 109 switch (message) {
113 case WM_POWERBROADCAST: 110 case WM_POWERBROADCAST:
114 if (wparam == PBT_APMPOWERSTATUSCHANGE || 111 if (wparam == PBT_APMPOWERSTATUSCHANGE ||
115 wparam == PBT_POWERSETTINGCHANGE) { 112 wparam == PBT_POWERSETTINGCHANGE) {
116 BatteryChanged(); 113 BatteryChanged();
117 } 114 }
118 *result = NULL; 115 *result = NULL;
119 return true; 116 return true;
120 default: 117 default:
121 return false; 118 return false;
122 } 119 }
123 } 120 }
124 121
125 HPOWERNOTIFY RegisterNotification(LPCGUID power_setting) { 122 HPOWERNOTIFY RegisterNotification(LPCGUID power_setting) {
126 if (base::win::GetVersion() < base::win::VERSION_VISTA) 123 if (base::win::GetVersion() < base::win::VERSION_VISTA)
127 return NULL; 124 return NULL;
128 125
129 return RegisterPowerSettingNotification(window_->hwnd(), power_setting, 126 return RegisterPowerSettingNotification(window_->hwnd(), power_setting,
130 DEVICE_NOTIFY_WINDOW_HANDLE); 127 DEVICE_NOTIFY_WINDOW_HANDLE);
131 } 128 }
132 129
133 BOOL UnregisterNotification(HPOWERNOTIFY handle) { 130 BOOL UnregisterNotification(HPOWERNOTIFY handle) {
134 if (base::win::GetVersion() < base::win::VERSION_VISTA) 131 if (base::win::GetVersion() < base::win::VERSION_VISTA)
135 return FALSE; 132 return FALSE;
136 133
137 return UnregisterPowerSettingNotification(handle); 134 return UnregisterPowerSettingNotification(handle);
138 } 135 }
139 136
140 bool CreateMessageWindow() { 137 bool CreateMessageWindow() {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 } 205 }
209 206
210 // static 207 // static
211 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( 208 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create(
212 const BatteryStatusService::BatteryUpdateCallback& callback) { 209 const BatteryStatusService::BatteryUpdateCallback& callback) {
213 return std::unique_ptr<BatteryStatusManager>( 210 return std::unique_ptr<BatteryStatusManager>(
214 new BatteryStatusManagerWin(callback)); 211 new BatteryStatusManagerWin(callback));
215 } 212 }
216 213
217 } // namespace device 214 } // namespace device
OLDNEW
« no previous file with comments | « services/device/battery/battery_status_manager_win.h ('k') | services/device/battery/battery_status_manager_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698