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

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

Issue 2075153002: Reland of 'Move content/browser/power_save_blocker to //device/power_save_blocker' (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: missing libs for component mac Created 4 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
« no previous file with comments | « content/browser/power_save_blocker_ozone.cc ('k') | content/browser/power_save_blocker_x11.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <windows.h>
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/win/scoped_handle.h"
12 #include "base/win/windows_version.h"
13 #include "content/browser/power_save_blocker_impl.h"
14
15 namespace content {
16 namespace {
17
18 int g_blocker_count[2];
19
20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type,
21 const std::string& description) {
22 if (type == PowerRequestExecutionRequired &&
23 base::win::GetVersion() < base::win::VERSION_WIN8) {
24 return INVALID_HANDLE_VALUE;
25 }
26
27 base::string16 wide_description = base::ASCIIToUTF16(description);
28 REASON_CONTEXT context = {0};
29 context.Version = POWER_REQUEST_CONTEXT_VERSION;
30 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
31 context.Reason.SimpleReasonString =
32 const_cast<wchar_t*>(wide_description.c_str());
33
34 base::win::ScopedHandle handle(::PowerCreateRequest(&context));
35 if (!handle.IsValid())
36 return INVALID_HANDLE_VALUE;
37
38 if (::PowerSetRequest(handle.Get(), type))
39 return handle.Take();
40
41 // Something went wrong.
42 return INVALID_HANDLE_VALUE;
43 }
44
45 // Takes ownership of the |handle|.
46 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) {
47 base::win::ScopedHandle request_handle(handle);
48 if (!request_handle.IsValid())
49 return;
50
51 if (type == PowerRequestExecutionRequired &&
52 base::win::GetVersion() < base::win::VERSION_WIN8) {
53 return;
54 }
55
56 BOOL success = ::PowerClearRequest(request_handle.Get(), type);
57 DCHECK(success);
58 }
59
60 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type,
61 int delta) {
62 g_blocker_count[type] += delta;
63 DCHECK_GE(g_blocker_count[type], 0);
64
65 if (g_blocker_count[type] > 1)
66 return;
67
68 DWORD this_flag = 0;
69 if (type == PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension)
70 this_flag |= ES_SYSTEM_REQUIRED;
71 else
72 this_flag |= ES_DISPLAY_REQUIRED;
73
74 DCHECK(this_flag);
75
76 static DWORD flags = ES_CONTINUOUS;
77 if (!g_blocker_count[type])
78 flags &= ~this_flag;
79 else
80 flags |= this_flag;
81
82 SetThreadExecutionState(flags);
83 }
84
85 } // namespace
86
87 class PowerSaveBlockerImpl::Delegate
88 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
89 public:
90 Delegate(PowerSaveBlockerType type,
91 const std::string& description,
92 scoped_refptr<base::SequencedTaskRunner> ui_task_runner)
93 : type_(type),
94 description_(description),
95 ui_task_runner_(ui_task_runner) {}
96
97 // Does the actual work to apply or remove the desired power save block.
98 void ApplyBlock();
99 void RemoveBlock();
100
101 // Returns the equivalent POWER_REQUEST_TYPE for this request.
102 POWER_REQUEST_TYPE RequestType();
103
104 private:
105 friend class base::RefCountedThreadSafe<Delegate>;
106 ~Delegate() {}
107
108 PowerSaveBlockerType type_;
109 const std::string description_;
110 base::win::ScopedHandle handle_;
111 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
112
113 DISALLOW_COPY_AND_ASSIGN(Delegate);
114 };
115
116 void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
117 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
118 if (base::win::GetVersion() < base::win::VERSION_WIN7)
119 return ApplySimpleBlock(type_, 1);
120
121 handle_.Set(CreatePowerRequest(RequestType(), description_));
122 }
123
124 void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
125 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
126 if (base::win::GetVersion() < base::win::VERSION_WIN7)
127 return ApplySimpleBlock(type_, -1);
128
129 DeletePowerRequest(RequestType(), handle_.Take());
130 }
131
132 POWER_REQUEST_TYPE PowerSaveBlockerImpl::Delegate::RequestType() {
133 if (type_ == kPowerSaveBlockPreventDisplaySleep)
134 return PowerRequestDisplayRequired;
135
136 if (base::win::GetVersion() < base::win::VERSION_WIN8)
137 return PowerRequestSystemRequired;
138
139 return PowerRequestExecutionRequired;
140 }
141
142 PowerSaveBlockerImpl::PowerSaveBlockerImpl(
143 PowerSaveBlockerType type,
144 Reason reason,
145 const std::string& description,
146 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
147 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner)
148 : delegate_(new Delegate(type, description, ui_task_runner)),
149 ui_task_runner_(ui_task_runner),
150 blocking_task_runner_(blocking_task_runner) {
151 ui_task_runner_->PostTask(FROM_HERE,
152 base::Bind(&Delegate::ApplyBlock, delegate_));
153 }
154
155 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
156 ui_task_runner_->PostTask(FROM_HERE,
157 base::Bind(&Delegate::RemoveBlock, delegate_));
158 }
159
160 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/power_save_blocker_ozone.cc ('k') | content/browser/power_save_blocker_x11.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698