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

Side by Side Diff: device/power_save_blocker/power_save_blocker_win.cc

Issue 2909943003: Removing useless Win7 checks + standardize its use (Closed)
Patch Set: Fix reversed conditionnal Created 3 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
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 "device/power_save_blocker/power_save_blocker.h" 5 #include "device/power_save_blocker/power_save_blocker.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/scoped_handle.h" 14 #include "base/win/scoped_handle.h"
15 #include "base/win/windows_version.h" 15 #include "base/win/windows_version.h"
16 16
17 namespace device { 17 namespace device {
18 namespace { 18 namespace {
19 19
20 int g_blocker_count[2];
21
22 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type, 20 HANDLE CreatePowerRequest(POWER_REQUEST_TYPE type,
23 const std::string& description) { 21 const std::string& description) {
24 if (type == PowerRequestExecutionRequired && 22 if (type == PowerRequestExecutionRequired &&
25 base::win::GetVersion() < base::win::VERSION_WIN8) { 23 base::win::GetVersion() == base::win::VERSION_WIN7) {
26 return INVALID_HANDLE_VALUE; 24 return INVALID_HANDLE_VALUE;
27 } 25 }
28 26
29 base::string16 wide_description = base::ASCIIToUTF16(description); 27 base::string16 wide_description = base::ASCIIToUTF16(description);
30 REASON_CONTEXT context = {0}; 28 REASON_CONTEXT context = {0};
31 context.Version = POWER_REQUEST_CONTEXT_VERSION; 29 context.Version = POWER_REQUEST_CONTEXT_VERSION;
32 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING; 30 context.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
33 context.Reason.SimpleReasonString = 31 context.Reason.SimpleReasonString =
34 const_cast<wchar_t*>(wide_description.c_str()); 32 const_cast<wchar_t*>(wide_description.c_str());
35 33
36 base::win::ScopedHandle handle(::PowerCreateRequest(&context)); 34 base::win::ScopedHandle handle(::PowerCreateRequest(&context));
37 if (!handle.IsValid()) 35 if (!handle.IsValid())
38 return INVALID_HANDLE_VALUE; 36 return INVALID_HANDLE_VALUE;
39 37
40 if (::PowerSetRequest(handle.Get(), type)) 38 if (::PowerSetRequest(handle.Get(), type))
41 return handle.Take(); 39 return handle.Take();
42 40
43 // Something went wrong. 41 // Something went wrong.
44 return INVALID_HANDLE_VALUE; 42 return INVALID_HANDLE_VALUE;
45 } 43 }
46 44
47 // Takes ownership of the |handle|. 45 // Takes ownership of the |handle|.
48 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) { 46 void DeletePowerRequest(POWER_REQUEST_TYPE type, HANDLE handle) {
49 base::win::ScopedHandle request_handle(handle); 47 base::win::ScopedHandle request_handle(handle);
50 if (!request_handle.IsValid()) 48 if (!request_handle.IsValid())
51 return; 49 return;
52 50
53 if (type == PowerRequestExecutionRequired && 51 if (type == PowerRequestExecutionRequired &&
54 base::win::GetVersion() < base::win::VERSION_WIN8) { 52 base::win::GetVersion() == base::win::VERSION_WIN7) {
55 return; 53 return;
56 } 54 }
57 55
58 BOOL success = ::PowerClearRequest(request_handle.Get(), type); 56 BOOL success = ::PowerClearRequest(request_handle.Get(), type);
59 DCHECK(success); 57 DCHECK(success);
60 } 58 }
61 59
62 void ApplySimpleBlock(PowerSaveBlocker::PowerSaveBlockerType type, int delta) {
63 g_blocker_count[type] += delta;
64 DCHECK_GE(g_blocker_count[type], 0);
65
66 if (g_blocker_count[type] > 1)
67 return;
68
69 DWORD this_flag = 0;
70 if (type == PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension)
71 this_flag |= ES_SYSTEM_REQUIRED;
72 else
73 this_flag |= ES_DISPLAY_REQUIRED;
74
75 DCHECK(this_flag);
76
77 static DWORD flags = ES_CONTINUOUS;
78 if (!g_blocker_count[type])
79 flags &= ~this_flag;
80 else
81 flags |= this_flag;
82
83 SetThreadExecutionState(flags);
84 }
85
86 } // namespace 60 } // namespace
87 61
88 class PowerSaveBlocker::Delegate 62 class PowerSaveBlocker::Delegate
89 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { 63 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> {
90 public: 64 public:
91 Delegate(PowerSaveBlockerType type, 65 Delegate(PowerSaveBlockerType type,
92 const std::string& description, 66 const std::string& description,
93 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) 67 scoped_refptr<base::SequencedTaskRunner> ui_task_runner)
94 : type_(type), 68 : type_(type),
95 description_(description), 69 description_(description),
(...skipping 13 matching lines...) Expand all
109 PowerSaveBlockerType type_; 83 PowerSaveBlockerType type_;
110 const std::string description_; 84 const std::string description_;
111 base::win::ScopedHandle handle_; 85 base::win::ScopedHandle handle_;
112 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; 86 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
113 87
114 DISALLOW_COPY_AND_ASSIGN(Delegate); 88 DISALLOW_COPY_AND_ASSIGN(Delegate);
115 }; 89 };
116 90
117 void PowerSaveBlocker::Delegate::ApplyBlock() { 91 void PowerSaveBlocker::Delegate::ApplyBlock() {
118 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); 92 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
119 if (base::win::GetVersion() < base::win::VERSION_WIN7)
120 return ApplySimpleBlock(type_, 1);
121
122 handle_.Set(CreatePowerRequest(RequestType(), description_)); 93 handle_.Set(CreatePowerRequest(RequestType(), description_));
123 } 94 }
124 95
125 void PowerSaveBlocker::Delegate::RemoveBlock() { 96 void PowerSaveBlocker::Delegate::RemoveBlock() {
126 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence()); 97 DCHECK(ui_task_runner_->RunsTasksInCurrentSequence());
127 if (base::win::GetVersion() < base::win::VERSION_WIN7)
128 return ApplySimpleBlock(type_, -1);
129
130 DeletePowerRequest(RequestType(), handle_.Take()); 98 DeletePowerRequest(RequestType(), handle_.Take());
131 } 99 }
132 100
133 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() { 101 POWER_REQUEST_TYPE PowerSaveBlocker::Delegate::RequestType() {
134 if (type_ == kPowerSaveBlockPreventDisplaySleep) 102 if (type_ == kPowerSaveBlockPreventDisplaySleep)
135 return PowerRequestDisplayRequired; 103 return PowerRequestDisplayRequired;
136 104
137 if (base::win::GetVersion() < base::win::VERSION_WIN8) 105 if (base::win::GetVersion() == base::win::VERSION_WIN7)
138 return PowerRequestSystemRequired; 106 return PowerRequestSystemRequired;
139 107
140 return PowerRequestExecutionRequired; 108 return PowerRequestExecutionRequired;
141 } 109 }
142 110
143 PowerSaveBlocker::PowerSaveBlocker( 111 PowerSaveBlocker::PowerSaveBlocker(
144 PowerSaveBlockerType type, 112 PowerSaveBlockerType type,
145 Reason reason, 113 Reason reason,
146 const std::string& description, 114 const std::string& description,
147 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, 115 scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
148 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) 116 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner)
149 : delegate_(new Delegate(type, description, ui_task_runner)), 117 : delegate_(new Delegate(type, description, ui_task_runner)),
150 ui_task_runner_(ui_task_runner), 118 ui_task_runner_(ui_task_runner),
151 blocking_task_runner_(blocking_task_runner) { 119 blocking_task_runner_(blocking_task_runner) {
152 ui_task_runner_->PostTask(FROM_HERE, 120 ui_task_runner_->PostTask(FROM_HERE,
153 base::Bind(&Delegate::ApplyBlock, delegate_)); 121 base::Bind(&Delegate::ApplyBlock, delegate_));
154 } 122 }
155 123
156 PowerSaveBlocker::~PowerSaveBlocker() { 124 PowerSaveBlocker::~PowerSaveBlocker() {
157 ui_task_runner_->PostTask(FROM_HERE, 125 ui_task_runner_->PostTask(FROM_HERE,
158 base::Bind(&Delegate::RemoveBlock, delegate_)); 126 base::Bind(&Delegate::RemoveBlock, delegate_));
159 } 127 }
160 128
161 } // namespace device 129 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698