| 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" |
| 11 #include "base/win/scoped_handle.h" | 11 #include "base/win/scoped_handle.h" |
| 12 #include "base/win/windows_version.h" | 12 #include "base/win/windows_version.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 int g_blocker_count[2]; | 18 int g_blocker_count[2]; |
| 19 | 19 |
| 20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, const std::string& reason) { | 20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, |
| 21 const std::string& description) { |
| 21 typedef HANDLE (WINAPI* PowerCreateRequestPtr)(PREASON_CONTEXT); | 22 typedef HANDLE (WINAPI* PowerCreateRequestPtr)(PREASON_CONTEXT); |
| 22 typedef BOOL (WINAPI* PowerSetRequestPtr)(HANDLE, POWER_REQUEST_TYPE); | 23 typedef BOOL (WINAPI* PowerSetRequestPtr)(HANDLE, POWER_REQUEST_TYPE); |
| 23 | 24 |
| 24 if (type == PowerRequestExecutionRequired && | 25 if (type == PowerRequestExecutionRequired && |
| 25 base::win::GetVersion() < base::win::VERSION_WIN8) { | 26 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| 26 return INVALID_HANDLE_VALUE; | 27 return INVALID_HANDLE_VALUE; |
| 27 } | 28 } |
| 28 | 29 |
| 29 static PowerCreateRequestPtr PowerCreateRequestFn = NULL; | 30 static PowerCreateRequestPtr PowerCreateRequestFn = NULL; |
| 30 static PowerSetRequestPtr PowerSetRequestFn = NULL; | 31 static PowerSetRequestPtr PowerSetRequestFn = NULL; |
| 31 | 32 |
| 32 if (!PowerCreateRequestFn || !PowerSetRequestFn) { | 33 if (!PowerCreateRequestFn || !PowerSetRequestFn) { |
| 33 HMODULE module = GetModuleHandle(L"kernel32.dll"); | 34 HMODULE module = GetModuleHandle(L"kernel32.dll"); |
| 34 PowerCreateRequestFn = reinterpret_cast<PowerCreateRequestPtr>( | 35 PowerCreateRequestFn = reinterpret_cast<PowerCreateRequestPtr>( |
| 35 GetProcAddress(module, "PowerCreateRequest")); | 36 GetProcAddress(module, "PowerCreateRequest")); |
| 36 PowerSetRequestFn = reinterpret_cast<PowerSetRequestPtr>( | 37 PowerSetRequestFn = reinterpret_cast<PowerSetRequestPtr>( |
| 37 GetProcAddress(module, "PowerSetRequest")); | 38 GetProcAddress(module, "PowerSetRequest")); |
| 38 | 39 |
| 39 if (!PowerCreateRequestFn || !PowerSetRequestFn) | 40 if (!PowerCreateRequestFn || !PowerSetRequestFn) |
| 40 return INVALID_HANDLE_VALUE; | 41 return INVALID_HANDLE_VALUE; |
| 41 } | 42 } |
| 42 base::string16 wide_reason = base::ASCIIToUTF16(reason); | 43 base::string16 wide_description = base::ASCIIToUTF16(description); |
| 43 REASON_CONTEXT context = {0}; | 44 REASON_CONTEXT context = {0}; |
| 44 context.Version = POWER_REQUEST_CONTEXT_VERSION; | 45 context.Version = POWER_REQUEST_CONTEXT_VERSION; |
| 45 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; | 46 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; |
| 46 context.Reason.SimpleReasonString = const_cast<wchar_t*>(wide_reason.c_str()); | 47 context.Reason.SimpleReasonString = |
| 48 const_cast<wchar_t*>(wide_description.c_str()); |
| 47 | 49 |
| 48 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); | 50 base::win::ScopedHandle handle(PowerCreateRequestFn(&context)); |
| 49 if (!handle.IsValid()) | 51 if (!handle.IsValid()) |
| 50 return INVALID_HANDLE_VALUE; | 52 return INVALID_HANDLE_VALUE; |
| 51 | 53 |
| 52 if (PowerSetRequestFn(handle.Get(), type)) | 54 if (PowerSetRequestFn(handle.Get(), type)) |
| 53 return handle.Take(); | 55 return handle.Take(); |
| 54 | 56 |
| 55 // Something went wrong. | 57 // Something went wrong. |
| 56 return INVALID_HANDLE_VALUE; | 58 return INVALID_HANDLE_VALUE; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 flags |= this_flag; | 105 flags |= this_flag; |
| 104 | 106 |
| 105 SetThreadExecutionState(flags); | 107 SetThreadExecutionState(flags); |
| 106 } | 108 } |
| 107 | 109 |
| 108 } // namespace | 110 } // namespace |
| 109 | 111 |
| 110 class PowerSaveBlockerImpl::Delegate | 112 class PowerSaveBlockerImpl::Delegate |
| 111 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 113 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| 112 public: | 114 public: |
| 113 Delegate(PowerSaveBlockerType type, const std::string& reason) | 115 Delegate(PowerSaveBlockerType type, const std::string& description) |
| 114 : type_(type), reason_(reason) {} | 116 : type_(type), description_(description) {} |
| 115 | 117 |
| 116 // Does the actual work to apply or remove the desired power save block. | 118 // Does the actual work to apply or remove the desired power save block. |
| 117 void ApplyBlock(); | 119 void ApplyBlock(); |
| 118 void RemoveBlock(); | 120 void RemoveBlock(); |
| 119 | 121 |
| 120 // Returns the equivalent POWER_REQUEST_TYPE for this request. | 122 // Returns the equivalent POWER_REQUEST_TYPE for this request. |
| 121 POWER_REQUEST_TYPE RequestType(); | 123 POWER_REQUEST_TYPE RequestType(); |
| 122 | 124 |
| 123 private: | 125 private: |
| 124 friend class base::RefCountedThreadSafe<Delegate>; | 126 friend class base::RefCountedThreadSafe<Delegate>; |
| 125 ~Delegate() {} | 127 ~Delegate() {} |
| 126 | 128 |
| 127 PowerSaveBlockerType type_; | 129 PowerSaveBlockerType type_; |
| 128 const std::string reason_; | 130 const std::string description_; |
| 129 base::win::ScopedHandle handle_; | 131 base::win::ScopedHandle handle_; |
| 130 | 132 |
| 131 DISALLOW_COPY_AND_ASSIGN(Delegate); | 133 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 132 }; | 134 }; |
| 133 | 135 |
| 134 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | 136 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 138 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 137 return ApplySimpleBlock(type_, 1); | 139 return ApplySimpleBlock(type_, 1); |
| 138 | 140 |
| 139 handle_.Set(CreatePowerRequest(RequestType(), reason_)); | 141 handle_.Set(CreatePowerRequest(RequestType(), description_)); |
| 140 } | 142 } |
| 141 | 143 |
| 142 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 144 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 146 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 145 return ApplySimpleBlock(type_, -1); | 147 return ApplySimpleBlock(type_, -1); |
| 146 | 148 |
| 147 DeletePowerRequest(RequestType(), handle_.Take()); | 149 DeletePowerRequest(RequestType(), handle_.Take()); |
| 148 } | 150 } |
| 149 | 151 |
| 150 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() { | 152 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() { |
| 151 if (type_ == kPowerSaveBlockPreventDisplaySleep) | 153 if (type_ == kPowerSaveBlockPreventDisplaySleep) |
| 152 return PowerRequestDisplayRequired; | 154 return PowerRequestDisplayRequired; |
| 153 | 155 |
| 154 if (base::win::GetVersion() < base::win::VERSION_WIN8) | 156 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 155 return PowerRequestSystemRequired; | 157 return PowerRequestSystemRequired; |
| 156 | 158 |
| 157 return PowerRequestExecutionRequired; | 159 return PowerRequestExecutionRequired; |
| 158 } | 160 } |
| 159 | 161 |
| 160 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 162 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| 161 const std::string& reason) | 163 Reason reason, |
| 162 : delegate_(new Delegate(type, reason)) { | 164 const std::string& description) |
| 165 : delegate_(new Delegate(type, description)) { |
| 163 BrowserThread::PostTask( | 166 BrowserThread::PostTask( |
| 164 BrowserThread::UI, FROM_HERE, | 167 BrowserThread::UI, FROM_HERE, |
| 165 base::Bind(&Delegate::ApplyBlock, delegate_)); | 168 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 166 } | 169 } |
| 167 | 170 |
| 168 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 171 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 169 BrowserThread::PostTask( | 172 BrowserThread::PostTask( |
| 170 BrowserThread::UI, FROM_HERE, | 173 BrowserThread::UI, FROM_HERE, |
| 171 base::Bind(&Delegate::RemoveBlock, delegate_)); | 174 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 172 } | 175 } |
| 173 | 176 |
| 174 } // namespace content | 177 } // namespace content |
| OLD | NEW |