| 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/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 base::string16 wide_reason = base::ASCIIToUTF16(reason); | 42 base::string16 wide_reason = base::ASCIIToUTF16(reason); |
| 43 REASON_CONTEXT context = {0}; | 43 REASON_CONTEXT context = {0}; |
| 44 context.Version = POWER_REQUEST_CONTEXT_VERSION; | 44 context.Version = POWER_REQUEST_CONTEXT_VERSION; |
| 45 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; | 45 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; |
| 46 context.Reason.SimpleReasonString = const_cast<wchar_t*>(wide_reason.c_str()); | 46 context.Reason.SimpleReasonString = const_cast<wchar_t*>(wide_reason.c_str()); |
| 47 | 47 |
| 48 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); | 48 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); |
| 49 if (!handle.IsValid()) | 49 if (!handle.IsValid()) |
| 50 return INVALID_HANDLE_VALUE; | 50 return INVALID_HANDLE_VALUE; |
| 51 | 51 |
| 52 if (PowerSetRequestFn(handle, type)) | 52 if (PowerSetRequestFn(handle.Get(), type)) |
| 53 return handle.Take(); | 53 return handle.Take(); |
| 54 | 54 |
| 55 // Something went wrong. | 55 // Something went wrong. |
| 56 return INVALID_HANDLE_VALUE; | 56 return INVALID_HANDLE_VALUE; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Takes ownership of the |handle|. | 59 // Takes ownership of the |handle|. |
| 60 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { | 60 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { |
| 61 base::win::ScopedHandle request_handle(handle); | 61 base::win::ScopedHandle request_handle(handle); |
| 62 if (!request_handle.IsValid()) | 62 if (!request_handle.IsValid()) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 if (type == PowerRequestExecutionRequired && | 65 if (type == PowerRequestExecutionRequired && |
| 66 base::win::GetVersion() < base::win::VERSION_WIN8) { | 66 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 67 return; | 67 return; |
| 68 } | 68 } |
| 69 | 69 |
| 70 typedef BOOL (WINAPI* PowerClearRequestPtr)(HANDLE, POWER_REQUEST_TYPE); | 70 typedef BOOL (WINAPI* PowerClearRequestPtr)(HANDLE, POWER_REQUEST_TYPE); |
| 71 HMODULE module = GetModuleHandle(L"kernel32.dll"); | 71 HMODULE module = GetModuleHandle(L"kernel32.dll"); |
| 72 PowerClearRequestPtr PowerClearRequestFn = | 72 PowerClearRequestPtr PowerClearRequestFn = |
| 73 reinterpret_cast<PowerClearRequestPtr>( | 73 reinterpret_cast<PowerClearRequestPtr>( |
| 74 GetProcAddress(module, "PowerClearRequest")); | 74 GetProcAddress(module, "PowerClearRequest")); |
| 75 | 75 |
| 76 if (!PowerClearRequestFn) | 76 if (!PowerClearRequestFn) |
| 77 return; | 77 return; |
| 78 | 78 |
| 79 BOOL success = PowerClearRequestFn(request_handle, type); | 79 BOOL success = PowerClearRequestFn(request_handle.Get(), type); |
| 80 DCHECK(success); | 80 DCHECK(success); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, | 83 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, |
| 84 int delta) { | 84 int delta) { |
| 85 g_blocker_count[type] += delta; | 85 g_blocker_count[type] += delta; |
| 86 DCHECK_GE(g_blocker_count[type], 0); | 86 DCHECK_GE(g_blocker_count[type], 0); |
| 87 | 87 |
| 88 if (g_blocker_count[type] > 1) | 88 if (g_blocker_count[type] > 1) |
| 89 return; | 89 return; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 base::Bind(&Delegate::ApplyBlock, delegate_)); | 165 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 166 } | 166 } |
| 167 | 167 |
| 168 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 168 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 169 BrowserThread::PostTask( | 169 BrowserThread::PostTask( |
| 170 BrowserThread::UI, FROM_HERE, | 170 BrowserThread::UI, FROM_HERE, |
| 171 base::Bind(&Delegate::RemoveBlock, delegate_)); | 171 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 172 } | 172 } |
| 173 | 173 |
| 174 } // namespace content | 174 } // namespace content |
| OLD | NEW |