blob: edecce917a0beb89a747665773bc50561c33e0f3 [file] [log] [blame]
[email protected]92269382012-03-03 09:00:081// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]36e93a772009-10-23 04:18:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]56f762c2011-07-15 15:40:455#include "content/browser/power_save_blocker.h"
[email protected]36e93a772009-10-23 04:18:056
7#include <windows.h>
8
[email protected]92269382012-03-03 09:00:089#include "base/logging.h"
[email protected]5dede9472012-06-06 00:23:5710#include "base/utf_string_conversions.h"
11#include "base/win/scoped_handle.h"
12#include "base/win/windows_version.h"
[email protected]c38831a12011-10-28 12:44:4913#include "content/public/browser/browser_thread.h"
[email protected]36e93a772009-10-23 04:18:0514
[email protected]631bb742011-11-02 11:29:3915using content::BrowserThread;
16
[email protected]b48a93fa2011-10-14 17:09:5417// Called only from UI thread.
18// static
19void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) {
[email protected]f8b3ef82010-10-11 02:45:5220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]36e93a772009-10-23 04:18:0521
22 DWORD flags = ES_CONTINUOUS;
23
[email protected]b48a93fa2011-10-14 17:09:5424 switch (type) {
25 case kPowerSaveBlockPreventSystemSleep:
26 flags |= ES_SYSTEM_REQUIRED;
27 break;
28 case kPowerSaveBlockPreventDisplaySleep:
29 flags |= ES_DISPLAY_REQUIRED;
30 break;
31 default:
32 break;
33 }
[email protected]36e93a772009-10-23 04:18:0534
35 SetThreadExecutionState(flags);
36}
[email protected]5dede9472012-06-06 00:23:5737
38// TODO(rvargas): Remove after the old interface goes away.
39#define PowerSaveBlocker PowerSaveBlocker2
40
41namespace {
42
43int g_blocker_count[2];
44
45#if _WIN32_WINNT <= _WIN32_WINNT_WIN7
46POWER_REQUEST_TYPE PowerRequestExecutionRequired =
47 static_cast<POWER_REQUEST_TYPE>(PowerRequestAwayModeRequired + 1);
48#endif
49
50HANDLE 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|.
90void 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
113void 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
140namespace content {
141
142class 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
166void PowerSaveBlocker::Delegate::ApplyBlock() {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8902dac2012-06-07 21:59:14168 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]5dede9472012-06-06 00:23:57169 return ApplySimpleBlock(type_, 1);
170
171 handle_.Set(CreatePowerRequest(RequestType(), reason_));
172}
173
174void PowerSaveBlocker::Delegate::RemoveBlock() {
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]8902dac2012-06-07 21:59:14176 if (base::win::GetVersion() < base::win::VERSION_WIN7)
[email protected]5dede9472012-06-06 00:23:57177 return ApplySimpleBlock(type_, -1);
178
179 DeletePowerRequest(RequestType(), handle_.Take());
180}
181
182POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() {
183 if (type_ == kPowerSaveBlockPreventDisplaySleep)
184 return PowerRequestDisplayRequired;
185
[email protected]8902dac2012-06-07 21:59:14186 if (base::win::GetVersion() < base::win::VERSION_WIN8)
187 return PowerRequestSystemRequired;
188
[email protected]5dede9472012-06-06 00:23:57189 return PowerRequestExecutionRequired;
190}
191
192PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type,
193 const std::string& reason)
194 : delegate_(new Delegate(type, reason)) {
195 BrowserThread::PostTask(
196 BrowserThread::UI, FROM_HERE,
197 base::Bind(&Delegate::ApplyBlock, delegate_));
198}
199
200PowerSaveBlocker::~PowerSaveBlocker() {
201 BrowserThread::PostTask(
202 BrowserThread::UI, FROM_HERE,
203 base::Bind(&Delegate::RemoveBlock, delegate_));
204}
205
206} // namespace content