Chromium Code Reviews| 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.h" | 5 #include "content/public/browser/power_save_blocker.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/utf_string_conversions.h" | 10 #include "base/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 { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 | 98 |
| 99 static DWORD flags = ES_CONTINUOUS; | 99 static DWORD flags = ES_CONTINUOUS; |
| 100 if (!g_blocker_count[type]) | 100 if (!g_blocker_count[type]) |
| 101 flags &= ~this_flag; | 101 flags &= ~this_flag; |
| 102 else | 102 else |
| 103 flags |= this_flag; | 103 flags |= this_flag; |
| 104 | 104 |
| 105 SetThreadExecutionState(flags); | 105 SetThreadExecutionState(flags); |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace. | 108 class PowerSaveBlockerWin : public PowerSaveBlocker { |
| 109 public: | |
| 110 PowerSaveBlockerWin(PowerSaveBlockerType type, const std::string& reason); | |
| 111 virtual ~PowerSaveBlockerWin(); | |
| 109 | 112 |
| 110 class PowerSaveBlocker::Delegate | 113 private: |
| 111 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { | 114 class Delegate; |
| 115 | |
| 116 // A second object with different lifetime than the RAII container. | |
|
jam
2013/01/11 01:24:30
why do this ref counted stuff in every implementat
hashimoto
2013/01/11 08:39:44
Introduced PowerSaveBlockerImpl to avoid code dupl
jam
2013/01/11 17:28:38
this is different than what i was wondering about.
| |
| 117 scoped_refptr<Delegate> delegate_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerWin); | |
| 120 }; | |
| 121 | |
| 122 class PowerSaveBlockerWin::Delegate | |
| 123 : public base::RefCountedThreadSafe<PowerSaveBlockerWin::Delegate> { | |
| 112 public: | 124 public: |
| 113 Delegate(PowerSaveBlockerType type, const std::string& reason) | 125 Delegate(PowerSaveBlockerType type, const std::string& reason) |
| 114 : type_(type), reason_(reason) {} | 126 : type_(type), reason_(reason) {} |
| 115 | 127 |
| 116 // Does the actual work to apply or remove the desired power save block. | 128 // Does the actual work to apply or remove the desired power save block. |
| 117 void ApplyBlock(); | 129 void ApplyBlock(); |
| 118 void RemoveBlock(); | 130 void RemoveBlock(); |
| 119 | 131 |
| 120 // Returns the equivalent POWER_REQUEST_TYPE for this request. | 132 // Returns the equivalent POWER_REQUEST_TYPE for this request. |
| 121 POWER_REQUEST_TYPE RequestType(); | 133 POWER_REQUEST_TYPE RequestType(); |
| 122 | 134 |
| 123 private: | 135 private: |
| 124 friend class base::RefCountedThreadSafe<Delegate>; | 136 friend class base::RefCountedThreadSafe<Delegate>; |
| 125 ~Delegate() {} | 137 ~Delegate() {} |
| 126 | 138 |
| 127 PowerSaveBlockerType type_; | 139 PowerSaveBlockerType type_; |
| 128 const std::string reason_; | 140 const std::string reason_; |
| 129 base::win::ScopedHandle handle_; | 141 base::win::ScopedHandle handle_; |
| 130 | 142 |
| 131 DISALLOW_COPY_AND_ASSIGN(Delegate); | 143 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 132 }; | 144 }; |
| 133 | 145 |
| 134 void PowerSaveBlocker::Delegate::ApplyBlock() { | 146 void PowerSaveBlockerWin::Delegate::ApplyBlock() { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 148 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 137 return ApplySimpleBlock(type_, 1); | 149 return ApplySimpleBlock(type_, 1); |
| 138 | 150 |
| 139 handle_.Set(CreatePowerRequest(RequestType(), reason_)); | 151 handle_.Set(CreatePowerRequest(RequestType(), reason_)); |
| 140 } | 152 } |
| 141 | 153 |
| 142 void PowerSaveBlocker::Delegate::RemoveBlock() { | 154 void PowerSaveBlockerWin::Delegate::RemoveBlock() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 156 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
| 145 return ApplySimpleBlock(type_, -1); | 157 return ApplySimpleBlock(type_, -1); |
| 146 | 158 |
| 147 DeletePowerRequest(RequestType(), handle_.Take()); | 159 DeletePowerRequest(RequestType(), handle_.Take()); |
| 148 } | 160 } |
| 149 | 161 |
| 150 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() { | 162 POWER_REQUEST_TYPE PowerSaveBlockerWin::Delegate::RequestType() { |
| 151 if (type_ == kPowerSaveBlockPreventDisplaySleep) | 163 if (type_ == kPowerSaveBlockPreventDisplaySleep) |
| 152 return PowerRequestDisplayRequired; | 164 return PowerRequestDisplayRequired; |
| 153 | 165 |
| 154 if (base::win::GetVersion() < base::win::VERSION_WIN8) | 166 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
| 155 return PowerRequestSystemRequired; | 167 return PowerRequestSystemRequired; |
| 156 | 168 |
| 157 return PowerRequestExecutionRequired; | 169 return PowerRequestExecutionRequired; |
| 158 } | 170 } |
| 159 | 171 |
| 160 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type, | 172 PowerSaveBlockerWin::PowerSaveBlockerWin(PowerSaveBlockerType type, |
| 161 const std::string& reason) | 173 const std::string& reason) |
| 162 : delegate_(new Delegate(type, reason)) { | 174 : delegate_(new Delegate(type, reason)) { |
| 163 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
| 164 BrowserThread::UI, FROM_HERE, | 176 BrowserThread::UI, FROM_HERE, |
| 165 base::Bind(&Delegate::ApplyBlock, delegate_)); | 177 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 166 } | 178 } |
| 167 | 179 |
| 168 PowerSaveBlocker::~PowerSaveBlocker() { | 180 PowerSaveBlockerWin::~PowerSaveBlockerWin() { |
| 169 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
| 170 BrowserThread::UI, FROM_HERE, | 182 BrowserThread::UI, FROM_HERE, |
| 171 base::Bind(&Delegate::RemoveBlock, delegate_)); | 183 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 172 } | 184 } |
| 173 | 185 |
| 186 } // namespace | |
| 187 | |
| 188 // static | |
| 189 scoped_ptr<PowerSaveBlocker> PowerSaveBlocker::Create( | |
| 190 PowerSaveBlockerType type, | |
| 191 const std::string& reason) { | |
| 192 return scoped_ptr<PowerSaveBlocker>(new PowerSaveBlockerWin(type, reason)); | |
| 193 } | |
| 194 | |
| 174 } // namespace content | 195 } // namespace content |
| OLD | NEW |