| 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 "device/power_save_blocker/power_save_blocker.h" |
| 6 |
| 5 #include <windows.h> | 7 #include <windows.h> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/win/scoped_handle.h" | 13 #include "base/win/scoped_handle.h" |
| 12 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
| 13 #include "device/power_save_blocker/power_save_blocker_impl.h" | |
| 14 | 15 |
| 15 namespace device { | 16 namespace device { |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 int g_blocker_count[2]; | 19 int g_blocker_count[2]; |
| 19 | 20 |
| 20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, | 21 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, |
| 21 const std::string& description) { | 22 const std::string& description) { |
| 22 if (type == PowerRequestExecutionRequired && | 23 if (type == PowerRequestExecutionRequired && |
| 23 base::win::GetVersion() < base::win::VERSION_WIN8) { | 24 base::win::GetVersion() < base::win::VERSION_WIN8) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (!g_blocker_count[type]) | 77 if (!g_blocker_count[type]) |
| 77 flags &= ~this_flag; | 78 flags &= ~this_flag; |
| 78 else | 79 else |
| 79 flags |= this_flag; | 80 flags |= this_flag; |
| 80 | 81 |
| 81 SetThreadExecutionState(flags); | 82 SetThreadExecutionState(flags); |
| 82 } | 83 } |
| 83 | 84 |
| 84 } // namespace | 85 } // namespace |
| 85 | 86 |
| 86 class PowerSaveBlockerImpl::Delegate | 87 class PowerSaveBlocker::Delegate |
| 87 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 88 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { |
| 88 public: | 89 public: |
| 89 Delegate(PowerSaveBlockerType type, | 90 Delegate(PowerSaveBlockerType type, |
| 90 const std::string& description, | 91 const std::string& description, |
| 91 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) | 92 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) |
| 92 : type_(type), | 93 : type_(type), |
| 93 description_(description), | 94 description_(description), |
| 94 ui_task_runner_(ui_task_runner) {} | 95 ui_task_runner_(ui_task_runner) {} |
| 95 | 96 |
| 96 // Does the actual work to apply or remove the desired power save block. | 97 // Does the actual work to apply or remove the desired power save block. |
| 97 void ApplyBlock(); | 98 void ApplyBlock(); |
| 98 void RemoveBlock(); | 99 void RemoveBlock(); |
| 99 | 100 |
| 100 // Returns the equivalent POWER_REQUEST_TYPE for this request. | 101 // Returns the equivalent POWER_REQUEST_TYPE for this request. |
| 101 POWER_REQUEST_TYPE RequestType(); | 102 POWER_REQUEST_TYPE RequestType(); |
| 102 | 103 |
| 103 private: | 104 private: |
| 104 friend class base::RefCountedThreadSafe<Delegate>; | 105 friend class base::RefCountedThreadSafe<Delegate>; |
| 105 ~Delegate() {} | 106 ~Delegate() {} |
| 106 | 107 |
| 107 PowerSaveBlockerType type_; | 108 PowerSaveBlockerType type_; |
| 108 const std::string description_; | 109 const std::string description_; |
| 109 base::win::ScopedHandle handle_; | 110 base::win::ScopedHandle handle_; |
| 110 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | 111 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
| 111 | 112 |
| 112 DISALLOW_COPY_AND_ASSIGN(Delegate); | 113 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 113 }; | 114 }; |
| 114 | 115 |
| 115 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | 116 void PowerSaveBlocker::Delegate::ApplyBlock() { |
| 116 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 117 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 117 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 118 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 118 return ApplySimpleBlock(type_, 1); | 119 return ApplySimpleBlock(type_, 1); |
| 119 | 120 |
| 120 handle_.Set(CreatePowerRequest(RequestType(), description_)); | 121 handle_.Set(CreatePowerRequest(RequestType(), description_)); |
| 121 } | 122 } |
| 122 | 123 |
| 123 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 124 void PowerSaveBlocker::Delegate::RemoveBlock() { |
| 124 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 125 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 125 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 126 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 126 return ApplySimpleBlock(type_, -1); | 127 return ApplySimpleBlock(type_, -1); |
| 127 | 128 |
| 128 DeletePowerRequest(RequestType(), handle_.Take()); | 129 DeletePowerRequest(RequestType(), handle_.Take()); |
| 129 } | 130 } |
| 130 | 131 |
| 131 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() { | 132 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() { |
| 132 if (type_ == kPowerSaveBlockPreventDisplaySleep) | 133 if (type_ == kPowerSaveBlockPreventDisplaySleep) |
| 133 return PowerRequestDisplayRequired; | 134 return PowerRequestDisplayRequired; |
| 134 | 135 |
| 135 if (base::win::GetVersion() < base::win::VERSION_WIN8) | 136 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 136 return PowerRequestSystemRequired; | 137 return PowerRequestSystemRequired; |
| 137 | 138 |
| 138 return PowerRequestExecutionRequired; | 139 return PowerRequestExecutionRequired; |
| 139 } | 140 } |
| 140 | 141 |
| 141 PowerSaveBlockerImpl::PowerSaveBlockerImpl( | 142 PowerSaveBlocker::PowerSaveBlocker( |
| 142 PowerSaveBlockerType type, | 143 PowerSaveBlockerType type, |
| 143 Reason reason, | 144 Reason reason, |
| 144 const std::string& description, | 145 const std::string& description, |
| 145 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 146 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
| 146 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) | 147 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) |
| 147 : delegate_(new Delegate(type, description, ui_task_runner)), | 148 : delegate_(new Delegate(type, description, ui_task_runner)), |
| 148 ui_task_runner_(ui_task_runner), | 149 ui_task_runner_(ui_task_runner), |
| 149 blocking_task_runner_(blocking_task_runner) { | 150 blocking_task_runner_(blocking_task_runner) { |
| 150 ui_task_runner_->PostTask(FROM_HERE, | 151 ui_task_runner_->PostTask(FROM_HERE, |
| 151 base::Bind(&Delegate::ApplyBlock, delegate_)); | 152 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 152 } | 153 } |
| 153 | 154 |
| 154 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 155 PowerSaveBlocker::~PowerSaveBlocker() { |
| 155 ui_task_runner_->PostTask(FROM_HERE, | 156 ui_task_runner_->PostTask(FROM_HERE, |
| 156 base::Bind(&Delegate::RemoveBlock, delegate_)); | 157 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 157 } | 158 } |
| 158 | 159 |
| 159 } // namespace device | 160 } // namespace device |
| OLD | NEW |