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 "content/browser/battery_status/battery_status_manager_win.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/metrics/histogram.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 "content/browser/battery_status/battery_status_manager.h" |
12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
13 | 14 |
14 namespace content { | 15 namespace content { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; | 19 typedef BatteryStatusService::BatteryUpdateCallback BatteryCallback; |
19 | 20 |
20 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; | 21 const wchar_t kWindowClassName[] = L"BatteryStatusMessageWindow"; |
21 | 22 |
| 23 // This enum is used for histogram. Don't change the order of the existing |
| 24 // values. |
| 25 enum NumberBatteriesType { |
| 26 NO_BATTERY = 0, |
| 27 ONE_OR_MORE_BATTERIES = 1, |
| 28 UNKNOWN_BATTERIES = 2, |
| 29 BATTERY_TYPES_COUNT = 3, |
| 30 }; |
| 31 |
| 32 void UpdateNumberBatteriesHistogram(NumberBatteriesType count) { |
| 33 UMA_HISTOGRAM_ENUMERATION("BatteryStatus.NumberBatteriesWin", |
| 34 count, |
| 35 BATTERY_TYPES_COUNT); |
| 36 } |
| 37 |
| 38 void UpdateNumberBatteriesHistogram() { |
| 39 SYSTEM_POWER_STATUS win_status; |
| 40 if (!GetSystemPowerStatus(&win_status)) { |
| 41 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); |
| 42 return; |
| 43 } |
| 44 |
| 45 if (win_status.BatteryFlag == 255) |
| 46 UpdateNumberBatteriesHistogram(UNKNOWN_BATTERIES); |
| 47 else if (win_status.BatteryFlag == 128) |
| 48 UpdateNumberBatteriesHistogram(NO_BATTERY); |
| 49 else |
| 50 UpdateNumberBatteriesHistogram(ONE_OR_MORE_BATTERIES); |
| 51 } |
| 52 |
22 // Message-only window for handling battery changes on Windows. | 53 // Message-only window for handling battery changes on Windows. |
23 class BatteryStatusObserver | 54 class BatteryStatusObserver |
24 : public base::RefCountedThreadSafe<BatteryStatusObserver> { | 55 : public base::RefCountedThreadSafe<BatteryStatusObserver> { |
25 public: | 56 public: |
26 explicit BatteryStatusObserver(const BatteryCallback& callback) | 57 explicit BatteryStatusObserver(const BatteryCallback& callback) |
27 : power_handle_(NULL), | 58 : power_handle_(NULL), |
28 battery_change_handle_(NULL), | 59 battery_change_handle_(NULL), |
29 callback_(callback) { | 60 callback_(callback) { |
30 } | 61 } |
31 | 62 |
(...skipping 29 matching lines...) Expand all Loading... |
61 // versions prior to Vista, see crbug.com/402466. | 92 // versions prior to Vista, see crbug.com/402466. |
62 power_handle_ = | 93 power_handle_ = |
63 RegisterNotification(&GUID_ACDC_POWER_SOURCE); | 94 RegisterNotification(&GUID_ACDC_POWER_SOURCE); |
64 battery_change_handle_ = | 95 battery_change_handle_ = |
65 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); | 96 RegisterNotification(&GUID_BATTERY_PERCENTAGE_REMAINING); |
66 } else { | 97 } else { |
67 // Could not create a message window, execute callback with the default | 98 // Could not create a message window, execute callback with the default |
68 // values. | 99 // values. |
69 callback_.Run(blink::WebBatteryStatus()); | 100 callback_.Run(blink::WebBatteryStatus()); |
70 } | 101 } |
| 102 |
| 103 UpdateNumberBatteriesHistogram(); |
71 } | 104 } |
72 | 105 |
73 void StopOnUI() { | 106 void StopOnUI() { |
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
75 if (!window_) | 108 if (!window_) |
76 return; | 109 return; |
77 | 110 |
78 if (power_handle_) | 111 if (power_handle_) |
79 UnregisterNotification(power_handle_); | 112 UnregisterNotification(power_handle_); |
80 if (battery_change_handle_) | 113 if (battery_change_handle_) |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 230 } |
198 | 231 |
199 // static | 232 // static |
200 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( | 233 scoped_ptr<BatteryStatusManager> BatteryStatusManager::Create( |
201 const BatteryStatusService::BatteryUpdateCallback& callback) { | 234 const BatteryStatusService::BatteryUpdateCallback& callback) { |
202 return scoped_ptr<BatteryStatusManager>( | 235 return scoped_ptr<BatteryStatusManager>( |
203 new BatteryStatusManagerWin(callback)); | 236 new BatteryStatusManagerWin(callback)); |
204 } | 237 } |
205 | 238 |
206 } // namespace content | 239 } // namespace content |
OLD | NEW |