| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <windows.h> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/win/scoped_handle.h" | |
| 12 #include "base/win/windows_version.h" | |
| 13 #include "device/power_save_blocker/power_save_blocker_impl.h" | |
| 14 | |
| 15 namespace device { | |
| 16 namespace { | |
| 17 | |
| 18 int g_blocker_count[2]; | |
| 19 | |
| 20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, | |
| 21 const std::string& description) { | |
| 22 if (type == PowerRequestExecutionRequired && | |
| 23 base::win::GetVersion() < base::win::VERSION_WIN8) { | |
| 24 return INVALID_HANDLE_VALUE; | |
| 25 } | |
| 26 | |
| 27 base::string16 wide_description = base::ASCIIToUTF16(description); | |
| 28 REASON_CONTEXT context = {0}; | |
| 29 context.Version = POWER_REQUEST_CONTEXT_VERSION; | |
| 30 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; | |
| 31 context.Reason.SimpleReasonString = | |
| 32 const_cast<wchar_t*>(wide_description.c_str()); | |
| 33 | |
| 34 base::win::ScopedHandle handle(::PowerCreateRequest(&context)); | |
| 35 if (!handle.IsValid()) | |
| 36 return INVALID_HANDLE_VALUE; | |
| 37 | |
| 38 if (::PowerSetRequest(handle.Get(), type)) | |
| 39 return handle.Take(); | |
| 40 | |
| 41 // Something went wrong. | |
| 42 return INVALID_HANDLE_VALUE; | |
| 43 } | |
| 44 | |
| 45 // Takes ownership of the |handle|. | |
| 46 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { | |
| 47 base::win::ScopedHandle request_handle(handle); | |
| 48 if (!request_handle.IsValid()) | |
| 49 return; | |
| 50 | |
| 51 if (type == PowerRequestExecutionRequired && | |
| 52 base::win::GetVersion() < base::win::VERSION_WIN8) { | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 BOOL success = ::PowerClearRequest(request_handle.Get(), type); | |
| 57 DCHECK(success); | |
| 58 } | |
| 59 | |
| 60 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, int delta) { | |
| 61 g_blocker_count[type] += delta; | |
| 62 DCHECK_GE(g_blocker_count[type], 0); | |
| 63 | |
| 64 if (g_blocker_count[type] > 1) | |
| 65 return; | |
| 66 | |
| 67 DWORD this_flag = 0; | |
| 68 if (type == PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension) | |
| 69 this_flag |= ES_SYSTEM_REQUIRED; | |
| 70 else | |
| 71 this_flag |= ES_DISPLAY_REQUIRED; | |
| 72 | |
| 73 DCHECK(this_flag); | |
| 74 | |
| 75 static DWORD flags = ES_CONTINUOUS; | |
| 76 if (!g_blocker_count[type]) | |
| 77 flags &= ~this_flag; | |
| 78 else | |
| 79 flags |= this_flag; | |
| 80 | |
| 81 SetThreadExecutionState(flags); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 class PowerSaveBlockerImpl::Delegate | |
| 87 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | |
| 88 public: | |
| 89 Delegate(PowerSaveBlockerType type, | |
| 90 const std::string& description, | |
| 91 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | |
| 92 : type_(type), | |
| 93 description_(description), | |
| 94 ui_task_runner_(ui_task_runner) {} | |
| 95 | |
| 96 // Does the actual work to apply or remove the desired power save block. | |
| 97 void ApplyBlock(); | |
| 98 void RemoveBlock(); | |
| 99 | |
| 100 // Returns the equivalent POWER_REQUEST_TYPE for this request. | |
| 101 POWER_REQUEST_TYPE RequestType(); | |
| 102 | |
| 103 private: | |
| 104 friend class base::RefCountedThreadSafe<Delegate>; | |
| 105 ~Delegate() {} | |
| 106 | |
| 107 PowerSaveBlockerType type_; | |
| 108 const std::string description_; | |
| 109 base::win::ScopedHandle handle_; | |
| 110 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | |
| 111 | |
| 112 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 113 }; | |
| 114 | |
| 115 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | |
| 116 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 117 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
| 118 return ApplySimpleBlock(type_, 1); | |
| 119 | |
| 120 handle_.Set(CreatePowerRequest(RequestType(), description_)); | |
| 121 } | |
| 122 | |
| 123 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | |
| 124 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 125 if (base::win::GetVersion() < base::win::VERSION_WIN7) | |
| 126 return ApplySimpleBlock(type_, -1); | |
| 127 | |
| 128 DeletePowerRequest(RequestType(), handle_.Take()); | |
| 129 } | |
| 130 | |
| 131 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() { | |
| 132 if (type_ == kPowerSaveBlockPreventDisplaySleep) | |
| 133 return PowerRequestDisplayRequired; | |
| 134 | |
| 135 if (base::win::GetVersion() < base::win::VERSION_WIN8) | |
| 136 return PowerRequestSystemRequired; | |
| 137 | |
| 138 return PowerRequestExecutionRequired; | |
| 139 } | |
| 140 | |
| 141 PowerSaveBlockerImpl::PowerSaveBlockerImpl( | |
| 142 PowerSaveBlockerType type, | |
| 143 Reason reason, | |
| 144 const std::string& description, | |
| 145 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | |
| 146 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) | |
| 147 : delegate_(new Delegate(type, description, ui_task_runner)), | |
| 148 ui_task_runner_(ui_task_runner), | |
| 149 blocking_task_runner_(blocking_task_runner) { | |
| 150 ui_task_runner_->PostTask(FROM_HERE, | |
| 151 base::Bind(&Delegate::ApplyBlock, delegate_)); | |
| 152 } | |
| 153 | |
| 154 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | |
| 155 ui_task_runner_->PostTask(FROM_HERE, | |
| 156 base::Bind(&Delegate::RemoveBlock, delegate_)); | |
| 157 } | |
| 158 | |
| 159 } // namespace device | |
| OLD | NEW |