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/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/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 } // namespace |
109 | 109 |
110 class PowerSaveBlocker::Delegate | 110 class PowerSaveBlockerImpl::Delegate |
111 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { | 111 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
112 public: | 112 public: |
113 Delegate(PowerSaveBlockerType type, const std::string& reason) | 113 Delegate(PowerSaveBlockerType type, const std::string& reason) |
114 : type_(type), reason_(reason) {} | 114 : type_(type), reason_(reason) {} |
115 | 115 |
116 // Does the actual work to apply or remove the desired power save block. | 116 // Does the actual work to apply or remove the desired power save block. |
117 void ApplyBlock(); | 117 void ApplyBlock(); |
118 void RemoveBlock(); | 118 void RemoveBlock(); |
119 | 119 |
120 // Returns the equivalent POWER_REQUEST_TYPE for this request. | 120 // Returns the equivalent POWER_REQUEST_TYPE for this request. |
121 POWER_REQUEST_TYPE RequestType(); | 121 POWER_REQUEST_TYPE RequestType(); |
122 | 122 |
123 private: | 123 private: |
124 friend class base::RefCountedThreadSafe<Delegate>; | 124 friend class base::RefCountedThreadSafe<Delegate>; |
125 ~Delegate() {} | 125 ~Delegate() {} |
126 | 126 |
127 PowerSaveBlockerType type_; | 127 PowerSaveBlockerType type_; |
128 const std::string reason_; | 128 const std::string reason_; |
129 base::win::ScopedHandle handle_; | 129 base::win::ScopedHandle handle_; |
130 | 130 |
131 DISALLOW_COPY_AND_ASSIGN(Delegate); | 131 DISALLOW_COPY_AND_ASSIGN(Delegate); |
132 }; | 132 }; |
133 | 133 |
134 void PowerSaveBlocker::Delegate::ApplyBlock() { | 134 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
136 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 136 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
137 return ApplySimpleBlock(type_, 1); | 137 return ApplySimpleBlock(type_, 1); |
138 | 138 |
139 handle_.Set(CreatePowerRequest(RequestType(), reason_)); | 139 handle_.Set(CreatePowerRequest(RequestType(), reason_)); |
140 } | 140 } |
141 | 141 |
142 void PowerSaveBlocker::Delegate::RemoveBlock() { | 142 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
144 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 144 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
145 return ApplySimpleBlock(type_, -1); | 145 return ApplySimpleBlock(type_, -1); |
146 | 146 |
147 DeletePowerRequest(RequestType(), handle_.Take()); | 147 DeletePowerRequest(RequestType(), handle_.Take()); |
148 } | 148 } |
149 | 149 |
150 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() { | 150 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() { |
151 if (type_ == kPowerSaveBlockPreventDisplaySleep) | 151 if (type_ == kPowerSaveBlockPreventDisplaySleep) |
152 return PowerRequestDisplayRequired; | 152 return PowerRequestDisplayRequired; |
153 | 153 |
154 if (base::win::GetVersion() < base::win::VERSION_WIN8) | 154 if (base::win::GetVersion() < base::win::VERSION_WIN8) |
155 return PowerRequestSystemRequired; | 155 return PowerRequestSystemRequired; |
156 | 156 |
157 return PowerRequestExecutionRequired; | 157 return PowerRequestExecutionRequired; |
158 } | 158 } |
159 | 159 |
160 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type, | 160 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
161 const std::string& reason) | 161 const std::string& reason) |
162 : delegate_(new Delegate(type, reason)) { | 162 : delegate_(new Delegate(type, reason)) { |
163 BrowserThread::PostTask( | 163 BrowserThread::PostTask( |
164 BrowserThread::UI, FROM_HERE, | 164 BrowserThread::UI, FROM_HERE, |
165 base::Bind(&Delegate::ApplyBlock, delegate_)); | 165 base::Bind(&Delegate::ApplyBlock, delegate_)); |
166 } | 166 } |
167 | 167 |
168 PowerSaveBlocker::~PowerSaveBlocker() { | 168 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
169 BrowserThread::PostTask( | 169 BrowserThread::PostTask( |
170 BrowserThread::UI, FROM_HERE, | 170 BrowserThread::UI, FROM_HERE, |
171 base::Bind(&Delegate::RemoveBlock, delegate_)); | 171 base::Bind(&Delegate::RemoveBlock, delegate_)); |
172 } | 172 } |
173 | 173 |
174 } // namespace content | 174 } // namespace content |
OLD | NEW |