1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/power_save_blocker.h"
#include <IOKit/pwr_mgt/IOPMLib.h>
#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
// Power management cannot be done on the UI thread. IOPMAssertionCreate does a
// synchronous MIG call to configd, so if it is called on the main thread the UI
// is at the mercy of another process. See http://crbug.com/79559 and
// http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.subproj/IOPMLibPrivate.c .
base::Thread* g_power_thread;
IOPMAssertionID g_power_assertion;
void CreateSleepAssertion(PowerSaveBlocker::PowerSaveBlockerType type) {
DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id());
IOReturn result;
if (g_power_assertion != kIOPMNullAssertionID) {
result = IOPMAssertionRelease(g_power_assertion);
g_power_assertion = kIOPMNullAssertionID;
LOG_IF(ERROR, result != kIOReturnSuccess)
<< "IOPMAssertionRelease: " << result;
}
CFStringRef level = NULL;
// See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more
// details.
switch (type) {
case PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep:
level = kIOPMAssertionTypeNoIdleSleep;
break;
case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
level = kIOPMAssertionTypeNoDisplaySleep;
break;
default:
break;
}
if (level) {
result = IOPMAssertionCreate(level,
kIOPMAssertionLevelOn,
&g_power_assertion);
LOG_IF(ERROR, result != kIOReturnSuccess)
<< "IOPMAssertionCreate: " << result;
}
}
} // namespace
// Called only from UI thread.
// static
void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!g_power_thread) {
g_power_assertion = kIOPMNullAssertionID;
g_power_thread = new base::Thread("PowerSaveBlocker");
g_power_thread->Start();
}
g_power_thread->message_loop()->
PostTask(FROM_HERE, base::Bind(CreateSleepAssertion, type));
}
// TODO(avi): Remove ^^^ this code in favor of vvv this code.
// http://crbug.com/126591
namespace {
// Power management cannot be done on the UI thread. IOPMAssertionCreate does a
// synchronous MIG call to configd, so if it is called on the main thread the UI
// is at the mercy of another process. See http://crbug.com/79559 and
// http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.subproj/IOPMLibPrivate.c .
struct PowerSaveBlockerLazyInstanceTraits {
static const bool kRegisterOnExit = false;
static const bool kAllowedToAccessOnNonjoinableThread = true;
static base::Thread* New(void* instance) {
base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker");
thread->Start();
return thread;
}
static void Delete(base::Thread* instance) { }
};
base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits>
g_power_thread2 = LAZY_INSTANCE_INITIALIZER;
} // namespace
namespace content {
class PowerSaveBlocker2::Delegate
: public base::RefCountedThreadSafe<PowerSaveBlocker2::Delegate> {
public:
Delegate(PowerSaveBlockerType type, const std::string& reason)
: type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {}
// Does the actual work to apply or remove the desired power save block.
void ApplyBlock();
void RemoveBlock();
private:
friend class base::RefCountedThreadSafe<PowerSaveBlocker2::Delegate>;
~Delegate() {}
PowerSaveBlockerType type_;
std::string reason_;
IOPMAssertionID assertion_;
};
void PowerSaveBlocker2::Delegate::ApplyBlock() {
DCHECK_EQ(base::PlatformThread::CurrentId(),
g_power_thread2.Pointer()->thread_id());
CFStringRef level = NULL;
// See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more
// details.
switch (type_) {
case PowerSaveBlocker2::kPowerSaveBlockPreventAppSuspension:
level = kIOPMAssertionTypeNoIdleSleep;
break;
case PowerSaveBlocker2::kPowerSaveBlockPreventDisplaySleep:
level = kIOPMAssertionTypeNoDisplaySleep;
break;
default:
NOTREACHED();
break;
}
if (level) {
// TODO(avi): Switch to IOPMAssertionCreateWithName when 10.6 is the minimum
// system supported by Chromium.
IOReturn result = IOPMAssertionCreate(level,
kIOPMAssertionLevelOn,
&assertion_);
LOG_IF(ERROR, result != kIOReturnSuccess)
<< "IOPMAssertionCreate: " << result;
}
}
void PowerSaveBlocker2::Delegate::RemoveBlock() {
DCHECK_EQ(base::PlatformThread::CurrentId(),
g_power_thread2.Pointer()->thread_id());
if (assertion_ != kIOPMNullAssertionID) {
IOReturn result = IOPMAssertionRelease(assertion_);
LOG_IF(ERROR, result != kIOReturnSuccess)
<< "IOPMAssertionRelease: " << result;
}
}
PowerSaveBlocker2::PowerSaveBlocker2(PowerSaveBlockerType type,
const std::string& reason)
: delegate_(new PowerSaveBlocker2::Delegate(type, reason)) {
g_power_thread2.Pointer()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&PowerSaveBlocker2::Delegate::ApplyBlock, delegate_));
}
PowerSaveBlocker2::~PowerSaveBlocker2() {
g_power_thread2.Pointer()->message_loop()->PostTask(
FROM_HERE,
base::Bind(&PowerSaveBlocker2::Delegate::RemoveBlock, delegate_));
}
} // namespace content
|