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

Side by Side Diff: device/battery/battery_status_manager_mac.cc

Issue 2801173004: Add '.mojom' suffix for battery mojom modules. (Closed)
Patch Set: Rebase only Created 3 years, 8 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.h" 5 #include "device/battery/battery_status_manager.h"
6 6
7 #include <CoreFoundation/CoreFoundation.h> 7 #include <CoreFoundation/CoreFoundation.h>
8 #include <IOKit/ps/IOPSKeys.h> 8 #include <IOKit/ps/IOPSKeys.h>
9 #include <IOKit/ps/IOPowerSources.h> 9 #include <IOKit/ps/IOPowerSources.h>
10 10
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 if (!string1 || !string2) 52 if (!string1 || !string2)
53 return false; 53 return false;
54 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo; 54 return CFStringCompare(string1, string2, 0) == kCFCompareEqualTo;
55 } 55 }
56 56
57 void UpdateNumberBatteriesHistogram(int count) { 57 void UpdateNumberBatteriesHistogram(int count) {
58 UMA_HISTOGRAM_CUSTOM_COUNTS( 58 UMA_HISTOGRAM_CUSTOM_COUNTS(
59 "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6); 59 "BatteryStatus.NumberBatteriesMac", count, 1, 5, 6);
60 } 60 }
61 61
62 void FetchBatteryStatus(CFDictionaryRef description, BatteryStatus* status) { 62 void FetchBatteryStatus(CFDictionaryRef description,
63 mojom::BatteryStatus* status) {
63 CFStringRef current_state = 64 CFStringRef current_state =
64 base::mac::GetValueFromDictionary<CFStringRef>(description, 65 base::mac::GetValueFromDictionary<CFStringRef>(description,
65 CFSTR(kIOPSPowerSourceStateKey)); 66 CFSTR(kIOPSPowerSourceStateKey));
66 67
67 bool on_battery_power = 68 bool on_battery_power =
68 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue)); 69 CFStringsAreEqual(current_state, CFSTR(kIOPSBatteryPowerValue));
69 bool is_charging = 70 bool is_charging =
70 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true); 71 GetValueAsBoolean(description, CFSTR(kIOPSIsChargingKey), true);
71 bool is_charged = 72 bool is_charged =
72 GetValueAsBoolean(description, CFSTR(kIOPSIsChargedKey), false); 73 GetValueAsBoolean(description, CFSTR(kIOPSIsChargedKey), false);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 SInt64 discharging_time = 108 SInt64 discharging_time =
108 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1); 109 GetValueAsSInt64(description, CFSTR(kIOPSTimeToEmptyKey), -1);
109 if (discharging_time != -1) { 110 if (discharging_time != -1) {
110 status->discharging_time = 111 status->discharging_time =
111 base::TimeDelta::FromMinutes(discharging_time).InSeconds(); 112 base::TimeDelta::FromMinutes(discharging_time).InSeconds();
112 } 113 }
113 } 114 }
114 } 115 }
115 } 116 }
116 117
117 std::vector<BatteryStatus> GetInternalBatteriesStates() { 118 std::vector<mojom::BatteryStatus> GetInternalBatteriesStates() {
118 std::vector<BatteryStatus> internal_sources; 119 std::vector<mojom::BatteryStatus> internal_sources;
119 120
120 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo()); 121 base::ScopedCFTypeRef<CFTypeRef> info(IOPSCopyPowerSourcesInfo());
121 base::ScopedCFTypeRef<CFArrayRef> power_sources_list( 122 base::ScopedCFTypeRef<CFArrayRef> power_sources_list(
122 IOPSCopyPowerSourcesList(info)); 123 IOPSCopyPowerSourcesList(info));
123 CFIndex count = CFArrayGetCount(power_sources_list); 124 CFIndex count = CFArrayGetCount(power_sources_list);
124 125
125 for (CFIndex i = 0; i < count; ++i) { 126 for (CFIndex i = 0; i < count; ++i) {
126 CFDictionaryRef description = IOPSGetPowerSourceDescription(info, 127 CFDictionaryRef description = IOPSGetPowerSourceDescription(info,
127 CFArrayGetValueAtIndex(power_sources_list, i)); 128 CFArrayGetValueAtIndex(power_sources_list, i));
128 129
129 if (!description) 130 if (!description)
130 continue; 131 continue;
131 132
132 CFStringRef transport_type = 133 CFStringRef transport_type =
133 base::mac::GetValueFromDictionary<CFStringRef>(description, 134 base::mac::GetValueFromDictionary<CFStringRef>(description,
134 CFSTR(kIOPSTransportTypeKey)); 135 CFSTR(kIOPSTransportTypeKey));
135 136
136 bool internal_source = 137 bool internal_source =
137 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType)); 138 CFStringsAreEqual(transport_type, CFSTR(kIOPSInternalType));
138 bool source_present = 139 bool source_present =
139 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false); 140 GetValueAsBoolean(description, CFSTR(kIOPSIsPresentKey), false);
140 141
141 if (internal_source && source_present) { 142 if (internal_source && source_present) {
142 BatteryStatus status; 143 mojom::BatteryStatus status;
143 FetchBatteryStatus(description, &status); 144 FetchBatteryStatus(description, &status);
144 internal_sources.push_back(status); 145 internal_sources.push_back(status);
145 } 146 }
146 } 147 }
147 148
148 return internal_sources; 149 return internal_sources;
149 } 150 }
150 151
151 void OnBatteryStatusChanged(const BatteryCallback& callback) { 152 void OnBatteryStatusChanged(const BatteryCallback& callback) {
152 std::vector<BatteryStatus> batteries(GetInternalBatteriesStates()); 153 std::vector<mojom::BatteryStatus> batteries(GetInternalBatteriesStates());
153 154
154 if (batteries.empty()) { 155 if (batteries.empty()) {
155 callback.Run(BatteryStatus()); 156 callback.Run(mojom::BatteryStatus());
156 return; 157 return;
157 } 158 }
158 159
159 // TODO(timvolodine): implement the case when there are multiple internal 160 // TODO(timvolodine): implement the case when there are multiple internal
160 // sources, e.g. when multiple batteries are present. Currently this will 161 // sources, e.g. when multiple batteries are present. Currently this will
161 // fail a DCHECK. 162 // fail a DCHECK.
162 DCHECK(batteries.size() == 1); 163 DCHECK(batteries.size() == 1);
163 callback.Run(batteries.front()); 164 callback.Run(batteries.front());
164 } 165 }
165 166
166 class BatteryStatusObserver { 167 class BatteryStatusObserver {
167 public: 168 public:
168 explicit BatteryStatusObserver(const BatteryCallback& callback) 169 explicit BatteryStatusObserver(const BatteryCallback& callback)
169 : callback_(callback) {} 170 : callback_(callback) {}
170 171
171 ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); } 172 ~BatteryStatusObserver() { DCHECK(!notifier_run_loop_source_); }
172 173
173 void Start() { 174 void Start() {
174 if (notifier_run_loop_source_) 175 if (notifier_run_loop_source_)
175 return; 176 return;
176 177
177 notifier_run_loop_source_.reset( 178 notifier_run_loop_source_.reset(
178 IOPSNotificationCreateRunLoopSource(CallOnBatteryStatusChanged, 179 IOPSNotificationCreateRunLoopSource(CallOnBatteryStatusChanged,
179 static_cast<void*>(&callback_))); 180 static_cast<void*>(&callback_)));
180 if (!notifier_run_loop_source_) { 181 if (!notifier_run_loop_source_) {
181 LOG(ERROR) << "Failed to create battery status notification run loop"; 182 LOG(ERROR) << "Failed to create battery status notification run loop";
182 // Make sure to execute to callback with the default values. 183 // Make sure to execute to callback with the default values.
183 callback_.Run(BatteryStatus()); 184 callback_.Run(mojom::BatteryStatus());
184 return; 185 return;
185 } 186 }
186 187
187 CallOnBatteryStatusChanged(static_cast<void*>(&callback_)); 188 CallOnBatteryStatusChanged(static_cast<void*>(&callback_));
188 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_, 189 CFRunLoopAddSource(CFRunLoopGetCurrent(), notifier_run_loop_source_,
189 kCFRunLoopDefaultMode); 190 kCFRunLoopDefaultMode);
190 UpdateNumberBatteriesHistogram(GetInternalBatteriesStates().size()); 191 UpdateNumberBatteriesHistogram(GetInternalBatteriesStates().size());
191 } 192 }
192 193
193 void Stop() { 194 void Stop() {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 } // end namespace 237 } // end namespace
237 238
238 // static 239 // static
239 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create( 240 std::unique_ptr<BatteryStatusManager> BatteryStatusManager::Create(
240 const BatteryStatusService::BatteryUpdateCallback& callback) { 241 const BatteryStatusService::BatteryUpdateCallback& callback) {
241 return std::unique_ptr<BatteryStatusManager>( 242 return std::unique_ptr<BatteryStatusManager>(
242 new BatteryStatusManagerMac(callback)); 243 new BatteryStatusManagerMac(callback));
243 } 244 }
244 245
245 } // namespace device 246 } // namespace device
OLDNEW
« no previous file with comments | « device/battery/battery_status_manager_linux_unittest.cc ('k') | device/battery/battery_status_manager_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698