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

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
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
148 // Does the actual work to apply or remove the desired power save block.
149 void ApplyBlock();
150 void RemoveBlock();
151
152 // Returns the equivalent POWER_REQUEST_TYPE for this request.
153 POWER_REQUEST_TYPE RequestType();
154
155 private:
156 friend class base::RefCountedThreadSafe<PowerSaveBlocker::Delegate>;
157 ~Delegate() {}
158
159 PowerSaveBlockerType type_;
160 const std::string reason_;
161 base::win::ScopedHandle handle_;
162
163 DISALLOW_COPY_AND_ASSIGN(Delegate);
164 };
165
166 void PowerSaveBlocker::Delegate::ApplyBlock() {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
168 base::win::Version os_version = base::win::GetVersion();
169 if (os_version < base::win::VERSION_WIN7)
170 return ApplySimpleBlock(type_, 1);
171
172 handle_.Set(CreatePowerRequest(RequestType(), reason_));
173 }
174
175 void PowerSaveBlocker::Delegate::RemoveBlock() {
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 base::win::Version os_version = base::win::GetVersion();
178 if (os_version < base::win::VERSION_WIN7)
179 return ApplySimpleBlock(type_, -1);
180
181 DeletePowerRequest(RequestType(), handle_.Take());
182 }
183
184 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() {
185 if (type_ == kPowerSaveBlockPreventDisplaySleep)
186 return PowerRequestDisplayRequired;
187
188 return PowerRequestExecutionRequired;
189 }
190
191 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type,
192 const std::string& reason)
193 : delegate_(new Delegate(type, reason)) {
194 BrowserThread::PostTask(
195 BrowserThread::UI, FROM_HERE,
196 base::Bind(&Delegate::ApplyBlock, delegate_));
197 }
198
199 PowerSaveBlocker::~PowerSaveBlocker() {
200 BrowserThread::PostTask(
201 BrowserThread::UI, FROM_HERE,
202 base::Bind(&Delegate::RemoveBlock, delegate_));
203 }
204
205 } // 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