| OLD | NEW |
| 1 // Copyright (c) 2012 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 "content/browser/power_save_blocker_impl.h" | 5 #include "content/browser/power_save_blocker_impl.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/win/scoped_handle.h" | 12 #include "base/win/scoped_handle.h" |
| 13 #include "base/win/windows_version.h" | 13 #include "base/win/windows_version.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 int g_blocker_count[2]; | 19 int g_blocker_count[2]; |
| 20 | 20 |
| 21 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, | 21 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, |
| 22 const std::string& description) { | 22 const std::string& description) { |
| 23 typedef HANDLE (WINAPI* PowerCreateRequestPtr)(PREASON_CONTEXT); | |
| 24 typedef BOOL (WINAPI* PowerSetRequestPtr)(HANDLE, POWER_REQUEST_TYPE); | |
| 25 | |
| 26 if (type == PowerRequestExecutionRequired && | 23 if (type == PowerRequestExecutionRequired && |
| 27 base::win::GetVersion() < base::win::VERSION_WIN8) { | 24 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 28 return INVALID_HANDLE_VALUE; | 25 return INVALID_HANDLE_VALUE; |
| 29 } | 26 } |
| 30 | 27 |
| 31 static PowerCreateRequestPtr PowerCreateRequestFn = NULL; | |
| 32 static PowerSetRequestPtr PowerSetRequestFn = NULL; | |
| 33 | |
| 34 if (!PowerCreateRequestFn || !PowerSetRequestFn) { | |
| 35 HMODULE module = GetModuleHandle(L"kernel32.dll"); | |
| 36 PowerCreateRequestFn = reinterpret_cast<PowerCreateRequestPtr>( | |
| 37 GetProcAddress(module, "PowerCreateRequest")); | |
| 38 PowerSetRequestFn = reinterpret_cast<PowerSetRequestPtr>( | |
| 39 GetProcAddress(module, "PowerSetRequest")); | |
| 40 | |
| 41 if (!PowerCreateRequestFn || !PowerSetRequestFn) | |
| 42 return INVALID_HANDLE_VALUE; | |
| 43 } | |
| 44 base::string16 wide_description = base::ASCIIToUTF16(description); | 28 base::string16 wide_description = base::ASCIIToUTF16(description); |
| 45 REASON_CONTEXT context = {0}; | 29 REASON_CONTEXT context = {0}; |
| 46 context.Version = POWER_REQUEST_CONTEXT_VERSION; | 30 context.Version = POWER_REQUEST_CONTEXT_VERSION; |
| 47 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; | 31 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; |
| 48 context.Reason.SimpleReasonString = | 32 context.Reason.SimpleReasonString = |
| 49 const_cast<wchar_t*>(wide_description.c_str()); | 33 const_cast<wchar_t*>(wide_description.c_str()); |
| 50 | 34 |
| 51 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); | 35 base::win::ScopedHandle handle(::PowerCreateRequest(&context)); |
| 52 if (!handle.IsValid()) | 36 if (!handle.IsValid()) |
| 53 return INVALID_HANDLE_VALUE; | 37 return INVALID_HANDLE_VALUE; |
| 54 | 38 |
| 55 if (PowerSetRequestFn(handle.Get(), type)) | 39 if (::PowerSetRequest(handle.Get(), type)) |
| 56 return handle.Take(); | 40 return handle.Take(); |
| 57 | 41 |
| 58 // Something went wrong. | 42 // Something went wrong. |
| 59 return INVALID_HANDLE_VALUE; | 43 return INVALID_HANDLE_VALUE; |
| 60 } | 44 } |
| 61 | 45 |
| 62 // Takes ownership of the |handle|. | 46 // Takes ownership of the |handle|. |
| 63 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { | 47 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { |
| 64 base::win::ScopedHandle request_handle(handle); | 48 base::win::ScopedHandle request_handle(handle); |
| 65 if (!request_handle.IsValid()) | 49 if (!request_handle.IsValid()) |
| 66 return; | 50 return; |
| 67 | 51 |
| 68 if (type == PowerRequestExecutionRequired && | 52 if (type == PowerRequestExecutionRequired && |
| 69 base::win::GetVersion() < base::win::VERSION_WIN8) { | 53 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 70 return; | 54 return; |
| 71 } | 55 } |
| 72 | 56 |
| 73 typedef BOOL (WINAPI* PowerClearRequestPtr)(HANDLE, POWER_REQUEST_TYPE); | 57 BOOL success = ::PowerClearRequest(request_handle.Get(), type); |
| 74 HMODULE module = GetModuleHandle(L"kernel32.dll"); | |
| 75 PowerClearRequestPtr PowerClearRequestFn = | |
| 76 reinterpret_cast<PowerClearRequestPtr>( | |
| 77 GetProcAddress(module, "PowerClearRequest")); | |
| 78 | |
| 79 if (!PowerClearRequestFn) | |
| 80 return; | |
| 81 | |
| 82 BOOL success = PowerClearRequestFn(request_handle.Get(), type); | |
| 83 DCHECK(success); | 58 DCHECK(success); |
| 84 } | 59 } |
| 85 | 60 |
| 86 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, | 61 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, |
| 87 int delta) { | 62 int delta) { |
| 88 g_blocker_count[type] += delta; | 63 g_blocker_count[type] += delta; |
| 89 DCHECK_GE(g_blocker_count[type], 0); | 64 DCHECK_GE(g_blocker_count[type], 0); |
| 90 | 65 |
| 91 if (g_blocker_count[type] > 1) | 66 if (g_blocker_count[type] > 1) |
| 92 return; | 67 return; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 base::Bind(&Delegate::ApplyBlock, delegate_)); | 144 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 170 } | 145 } |
| 171 | 146 |
| 172 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 147 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 173 BrowserThread::PostTask( | 148 BrowserThread::PostTask( |
| 174 BrowserThread::UI, FROM_HERE, | 149 BrowserThread::UI, FROM_HERE, |
| 175 base::Bind(&Delegate::RemoveBlock, delegate_)); | 150 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 176 } | 151 } |
| 177 | 152 |
| 178 } // namespace content | 153 } // namespace content |
| OLD | NEW |