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