OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/system_monitor/system_monitor.h" | 5 #include "base/system_monitor/system_monitor.h" |
| 6 #include "base/utf_string_conversions.h" |
| 7 #include "base/win/windows_version.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 // Maps a request's reason to the handle and requests count. |
| 12 typedef std::map<std::string, std::pair<HANDLE, int> > HandleMap; |
| 13 |
| 14 #if _WIN32_WINNT <= _WIN32_WINNT_WIN7 |
| 15 POWER_REQUEST_TYPE PowerRequestExecutionRequired = |
| 16 static_cast<POWER_REQUEST_TYPE>(PowerRequestAwayModeRequired + 1); |
| 17 #endif |
| 18 |
| 19 POWER_REQUEST_TYPE PowerRequestTestRequired = |
| 20 static_cast<POWER_REQUEST_TYPE>(PowerRequestExecutionRequired + 10); |
| 21 |
| 22 POWER_REQUEST_TYPE PowerRequirementToType( |
| 23 base::SystemMonitor::PowerRequirement requirement) { |
| 24 switch (requirement) { |
| 25 case base::SystemMonitor::DISPLAY_REQUIRED: |
| 26 return PowerRequestDisplayRequired; |
| 27 case base::SystemMonitor::SYSTEM_REQUIRED: |
| 28 return PowerRequestSystemRequired; |
| 29 case base::SystemMonitor::CPU_REQUIRED: |
| 30 return PowerRequestExecutionRequired; |
| 31 case base::SystemMonitor::TEST_REQUIRED: |
| 32 return PowerRequestTestRequired; |
| 33 } |
| 34 NOTREACHED(); |
| 35 return PowerRequestTestRequired; |
| 36 } |
| 37 |
| 38 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, const std::string& reason) { |
| 39 typedef HANDLE (WINAPI* PowerCreateRequestPtr)(PREASON_CONTEXT); |
| 40 typedef BOOL (WINAPI* PowerSetRequestPtr)(HANDLE, POWER_REQUEST_TYPE); |
| 41 |
| 42 if (type == PowerRequestTestRequired) |
| 43 return NULL; |
| 44 |
| 45 if (type == PowerRequestExecutionRequired && |
| 46 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 47 return INVALID_HANDLE_VALUE; |
| 48 } |
| 49 |
| 50 static PowerCreateRequestPtr PowerCreateRequestFn = NULL; |
| 51 static PowerSetRequestPtr PowerSetRequestFn = NULL; |
| 52 |
| 53 if (!PowerCreateRequestFn || !PowerSetRequestFn) { |
| 54 HMODULE module = GetModuleHandle(L"kernel32.dll"); |
| 55 PowerCreateRequestFn = reinterpret_cast<PowerCreateRequestPtr>( |
| 56 GetProcAddress(module, "PowerCreateRequest")); |
| 57 PowerSetRequestFn = reinterpret_cast<PowerSetRequestPtr>( |
| 58 GetProcAddress(module, "PowerSetRequest")); |
| 59 |
| 60 if (!PowerCreateRequestFn || !PowerSetRequestFn) |
| 61 return INVALID_HANDLE_VALUE; |
| 62 } |
| 63 string16 wide_reason = ASCIIToUTF16(reason); |
| 64 REASON_CONTEXT context = {0}; |
| 65 context.Version = POWER_REQUEST_CONTEXT_VERSION; |
| 66 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; |
| 67 context.Reason.SimpleReasonString = const_cast<wchar_t*>(wide_reason.c_str()); |
| 68 |
| 69 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); |
| 70 if (!handle.IsValid()) |
| 71 return INVALID_HANDLE_VALUE; |
| 72 |
| 73 if (PowerSetRequestFn(handle, type)) |
| 74 return handle.Take(); |
| 75 |
| 76 // Something went wrong. |
| 77 return INVALID_HANDLE_VALUE; |
| 78 } |
| 79 |
| 80 // Takes ownership of the |handle|. |
| 81 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { |
| 82 if (type == PowerRequestTestRequired) |
| 83 return; |
| 84 |
| 85 base::win::ScopedHandle request_handle(handle); |
| 86 if (!request_handle.IsValid()) |
| 87 return; |
| 88 |
| 89 if (type == PowerRequestExecutionRequired && |
| 90 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 91 return; |
| 92 } |
| 93 |
| 94 typedef BOOL (WINAPI* PowerClearRequestPtr)(HANDLE, POWER_REQUEST_TYPE); |
| 95 HMODULE module = GetModuleHandle(L"kernel32.dll"); |
| 96 PowerClearRequestPtr PowerClearRequestFn = |
| 97 reinterpret_cast<PowerClearRequestPtr>( |
| 98 GetProcAddress(module, "PowerClearRequest")); |
| 99 |
| 100 if (!PowerClearRequestFn) |
| 101 return; |
| 102 |
| 103 BOOL success = PowerClearRequest(request_handle, type); |
| 104 DCHECK(success); |
| 105 } |
| 106 |
| 107 } // namespace. |
6 | 108 |
7 namespace base { | 109 namespace base { |
8 | 110 |
9 void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { | 111 void SystemMonitor::ProcessWmPowerBroadcastMessage(int event_id) { |
10 PowerEvent power_event; | 112 PowerEvent power_event; |
11 switch (event_id) { | 113 switch (event_id) { |
12 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. | 114 case PBT_APMPOWERSTATUSCHANGE: // The power status changed. |
13 power_event = POWER_STATE_EVENT; | 115 power_event = POWER_STATE_EVENT; |
14 break; | 116 break; |
15 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. | 117 case PBT_APMRESUMEAUTOMATIC: // Resume from suspend. |
(...skipping 13 matching lines...) Expand all Loading... |
29 // PBT_APMBATTERYLOW - removed in Vista. | 131 // PBT_APMBATTERYLOW - removed in Vista. |
30 // PBT_APMOEMEVENT - removed in Vista. | 132 // PBT_APMOEMEVENT - removed in Vista. |
31 // PBT_APMQUERYSUSPEND - removed in Vista. | 133 // PBT_APMQUERYSUSPEND - removed in Vista. |
32 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. | 134 // PBT_APMQUERYSUSPENDFAILED - removed in Vista. |
33 // PBT_APMRESUMECRITICAL - removed in Vista. | 135 // PBT_APMRESUMECRITICAL - removed in Vista. |
34 // PBT_POWERSETTINGCHANGE - user changed the power settings. | 136 // PBT_POWERSETTINGCHANGE - user changed the power settings. |
35 } | 137 } |
36 ProcessPowerMessage(power_event); | 138 ProcessPowerMessage(power_event); |
37 } | 139 } |
38 | 140 |
| 141 void SystemMonitor::BeginPowerRequirement(PowerRequirement requirement, |
| 142 const std::string& reason) { |
| 143 thread_checker_.CalledOnValidThread(); |
| 144 |
| 145 HandleMap::iterator i = handles_.find(reason); |
| 146 if (i != handles_.end()) { |
| 147 // This is not the first request, just increase the requests count. |
| 148 i->second.second++; |
| 149 DCHECK_GT(i->second.second, 1); |
| 150 return; |
| 151 } |
| 152 |
| 153 HANDLE handle = CreatePowerRequest(PowerRequirementToType(requirement), |
| 154 reason); |
| 155 |
| 156 if (handle != INVALID_HANDLE_VALUE) |
| 157 handles_[reason] = std::pair<HANDLE, int>(handle, 1); |
| 158 } |
| 159 |
| 160 void SystemMonitor::EndPowerRequirement(PowerRequirement requirement, |
| 161 const std::string& reason) { |
| 162 thread_checker_.CalledOnValidThread(); |
| 163 |
| 164 HandleMap::iterator i = handles_.find(reason); |
| 165 if (i == handles_.end()) { |
| 166 NOTREACHED(); |
| 167 return; |
| 168 } |
| 169 |
| 170 // Decrease the requests count and see if this the last request. |
| 171 i->second.second--; |
| 172 DCHECK_GE(i->second.second, 0); |
| 173 |
| 174 if (i->second.second) |
| 175 return; |
| 176 |
| 177 DeletePowerRequest(PowerRequirementToType(requirement), i->second.first); |
| 178 handles_.erase(i); |
| 179 } |
| 180 |
| 181 size_t SystemMonitor::GetPowerRequirementsCountForTest() const { |
| 182 return handles_.size(); |
| 183 } |
| 184 |
39 // Function to query the system to see if it is currently running on | 185 // Function to query the system to see if it is currently running on |
40 // battery power. Returns true if running on battery. | 186 // battery power. Returns true if running on battery. |
41 bool SystemMonitor::IsBatteryPower() { | 187 bool SystemMonitor::IsBatteryPower() { |
42 SYSTEM_POWER_STATUS status; | 188 SYSTEM_POWER_STATUS status; |
43 if (!GetSystemPowerStatus(&status)) { | 189 if (!GetSystemPowerStatus(&status)) { |
44 DLOG(ERROR) << "GetSystemPowerStatus failed: " << GetLastError(); | 190 DLOG(ERROR) << "GetSystemPowerStatus failed: " << GetLastError(); |
45 return false; | 191 return false; |
46 } | 192 } |
47 return (status.ACLineStatus == 0); | 193 return (status.ACLineStatus == 0); |
48 } | 194 } |
49 | 195 |
50 } // namespace base | 196 } // namespace base |
OLD | NEW |