Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Side by Side Diff: content/browser/power_save_blocker_win.cc

Issue 10538013: Content: Implement PowerSaveBlocker2 for windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.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"
11 #include "base/win/scoped_handle.h"
12 #include "base/win/windows_version.h"
10 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
11 14
12 using content::BrowserThread; 15 using content::BrowserThread;
13 16
14 // Called only from UI thread. 17 // Called only from UI thread.
15 // static 18 // static
16 void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) { 19 void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) {
17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
18 21
19 DWORD flags = ES_CONTINUOUS; 22 DWORD flags = ES_CONTINUOUS;
20 23
21 switch (type) { 24 switch (type) {
22 case kPowerSaveBlockPreventSystemSleep: 25 case kPowerSaveBlockPreventSystemSleep:
23 flags |= ES_SYSTEM_REQUIRED; 26 flags |= ES_SYSTEM_REQUIRED;
24 break; 27 break;
25 case kPowerSaveBlockPreventDisplaySleep: 28 case kPowerSaveBlockPreventDisplaySleep:
26 flags |= ES_DISPLAY_REQUIRED; 29 flags |= ES_DISPLAY_REQUIRED;
27 break; 30 break;
28 default: 31 default:
29 break; 32 break;
30 } 33 }
31 34
32 SetThreadExecutionState(flags); 35 SetThreadExecutionState(flags);
33 } 36 }
37
38 // TODO(rvargas): Remove after the old interface goes away.
39 #define PowerSaveBlocker PowerSaveBlocker2
Mike Mammarella 2012/06/05 18:28:49 Hey, this is clever. I'm going to do the same thin
40
41 namespace {
42
43 int g_blocker_count[2];
44
45 #if _WIN32_WINNT <= _WIN32_WINNT_WIN7
46 POWER_REQUEST_TYPE PowerRequestExecutionRequired =
47 static_cast<POWER_REQUEST_TYPE>(PowerRequestAwayModeRequired + 1);
48 #endif
49
50 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, const std::string& reason) {
51 typedef HANDLE (WINAPI* PowerCreateRequestPtr)(PREASON_CONTEXT);
52 typedef BOOL (WINAPI* PowerSetRequestPtr)(HANDLE, POWER_REQUEST_TYPE);
53
54 if (type == PowerRequestExecutionRequired &&
55 base::win::GetVersion() < base::win::VERSION_WIN8) {
56 return INVALID_HANDLE_VALUE;
57 }
58
59 static PowerCreateRequestPtr PowerCreateRequestFn = NULL;
60 static PowerSetRequestPtr PowerSetRequestFn = NULL;
61
62 if (!PowerCreateRequestFn || !PowerSetRequestFn) {
63 HMODULE module = GetModuleHandle(L"kernel32.dll");
64 PowerCreateRequestFn = reinterpret_cast<PowerCreateRequestPtr>(
65 GetProcAddress(module, "PowerCreateRequest"));
66 PowerSetRequestFn = reinterpret_cast<PowerSetRequestPtr>(
67 GetProcAddress(module, "PowerSetRequest"));
68
69 if (!PowerCreateRequestFn || !PowerSetRequestFn)
70 return INVALID_HANDLE_VALUE;
71 }
72 string16 wide_reason = ASCIIToUTF16(reason);
73 REASON_CONTEXT context = {0};
74 context.Version = POWER_REQUEST_CONTEXT_VERSION;
75 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
76 context.Reason.SimpleReasonString = const_cast<wchar_t*>(wide_reason.c_str());
77
78 base::win::ScopedHandle handle(PowerCreateRequestFn(&context));
79 if (!handle.IsValid())
80 return INVALID_HANDLE_VALUE;
81
82 if (PowerSetRequestFn(handle, type))
83 return handle.Take();
84
85 // Something went wrong.
86 return INVALID_HANDLE_VALUE;
87 }
88
89 // Takes ownership of the |handle|.
90 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) {
91 base::win::ScopedHandle request_handle(handle);
92 if (!request_handle.IsValid())
93 return;
94
95 if (type == PowerRequestExecutionRequired &&
96 base::win::GetVersion() < base::win::VERSION_WIN8) {
97 return;
98 }
99
100 typedef BOOL (WINAPI* PowerClearRequestPtr)(HANDLE, POWER_REQUEST_TYPE);
101 HMODULE module = GetModuleHandle(L"kernel32.dll");
102 PowerClearRequestPtr PowerClearRequestFn =
103 reinterpret_cast<PowerClearRequestPtr>(
104 GetProcAddress(module, "PowerClearRequest"));
105
106 if (!PowerClearRequestFn)
107 return;
108
109 BOOL success = PowerClearRequestFn(request_handle, type);
110 DCHECK(success);
111 }
112
113 void ApplySimpleBlock(content::PowerSaveBlocker::PowerSaveBlockerType type,
114 int delta) {
115 g_blocker_count[type] += delta;
116 DCHECK_GE(g_blocker_count[type], 0);
117
118 if (g_blocker_count[type] > 1)
119 return;
120
121 DWORD this_flag = 0;
122 if (type == content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension)
123 this_flag |= ES_SYSTEM_REQUIRED;
124 else
125 this_flag |= ES_DISPLAY_REQUIRED;
126
127 DCHECK(this_flag);
128
129 static DWORD flags = ES_CONTINUOUS;
130 if (!g_blocker_count[type])
131 flags &= ~this_flag;
132 else
133 flags |= this_flag;
134
135 SetThreadExecutionState(flags);
136 }
137
138 } // namespace.
139
140 namespace content {
141
142 class PowerSaveBlocker::Delegate
143 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> {
144 public:
145 Delegate(PowerSaveBlockerType type, const std::string& reason)
146 : type_(type), reason_(reason) {}
147 ~Delegate() {}
Avi (use Gerrit) 2012/06/05 18:48:34 When I copy this code, I get a style failure: err
rvargas (doing something else) 2012/06/05 18:58:19 And this is fixed now.
148
149 // Does the actual work to apply or remove the desired power save block.
150 void ApplyBlock();
151 void RemoveBlock();
152
153 // Returns the equivalent POWER_REQUEST_TYPE for this request.
154 POWER_REQUEST_TYPE RequestType();
155
156 private:
157 PowerSaveBlockerType type_;
158 const std::string reason_;
159 base::win::ScopedHandle handle_;
160 };
161
162 void PowerSaveBlocker::Delegate::ApplyBlock() {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
164 base::win::Version os_version = base::win::GetVersion();
165 if (os_version < base::win::VERSION_WIN7)
166 return ApplySimpleBlock(type_, 1);
167
168 handle_.Set(CreatePowerRequest(RequestType(), reason_));
169 }
170
171 void PowerSaveBlocker::Delegate::RemoveBlock() {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
173 base::win::Version os_version = base::win::GetVersion();
174 if (os_version < base::win::VERSION_WIN7)
175 return ApplySimpleBlock(type_, -1);
176
177 DeletePowerRequest(RequestType(), handle_.Take());
178 }
179
180 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() {
181 if (type_ == kPowerSaveBlockPreventDisplaySleep)
182 return PowerRequestDisplayRequired;
183
184 return PowerRequestExecutionRequired;
185 }
186
187 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type,
188 const std::string& reason)
189 : delegate_(new PowerSaveBlocker::Delegate(type, reason)) {
Avi (use Gerrit) 2012/06/05 18:11:55 The only thing I can do: nitpick. Too many spaces
Mike Mammarella 2012/06/05 18:28:49 You don't need the PowerSaveBlocker:: here or belo
rvargas (doing something else) 2012/06/05 18:39:50 Done.
rvargas (doing something else) 2012/06/05 18:39:50 Done.
190 BrowserThread::PostTask(
191 BrowserThread::UI, FROM_HERE,
192 base::Bind(&PowerSaveBlocker::Delegate::ApplyBlock, delegate_));
193 }
194
195 PowerSaveBlocker::~PowerSaveBlocker(void) {
Avi (use Gerrit) 2012/06/05 18:11:55 no void
rvargas (doing something else) 2012/06/05 18:39:50 Ouch!. done.
196 BrowserThread::PostTask(
197 BrowserThread::UI, FROM_HERE,
198 base::Bind(&PowerSaveBlocker::Delegate::RemoveBlock, delegate_));
199 }
200
201 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698