OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/browser/power_save_blocker.h" | |
6 | |
7 #include <IOKit/pwr_mgt/IOPMLib.h> | |
8 | |
9 #include "base/threading/platform_thread.h" | |
10 #include "base/threading/thread.h" | |
11 #include "content/browser/browser_thread.h" | |
12 | |
13 namespace { | |
14 | |
15 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a | |
16 // synchronous MIG call to configd, so if it is called on the main thread the UI | |
17 // is at the mercy of another process. See http://crbug.com/79559 and | |
18 // http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.
subproj/IOPMLibPrivate.c . | |
19 base::Thread* g_power_thread; | |
20 IOPMAssertionID g_power_assertion; | |
21 | |
22 void CreateSleepAssertion() { | |
23 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); | |
24 IOReturn result; | |
25 DCHECK_EQ(g_power_assertion, kIOPMNullAssertionID); | |
26 | |
27 // Block just idle sleep; allow display sleep. | |
28 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa2004/qa1340.html> | |
29 // for more details. | |
30 result = IOPMAssertionCreate(kIOPMAssertionTypeNoIdleSleep, | |
31 kIOPMAssertionLevelOn, | |
32 &g_power_assertion); | |
33 LOG_IF(ERROR, result != kIOReturnSuccess) | |
34 << "IOPMAssertionCreate: " << result; | |
35 } | |
36 | |
37 void ReleaseSleepAssertion() { | |
38 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); | |
39 IOReturn result; | |
40 DCHECK_NE(g_power_assertion, kIOPMNullAssertionID); | |
41 result = IOPMAssertionRelease(g_power_assertion); | |
42 g_power_assertion = kIOPMNullAssertionID; | |
43 LOG_IF(ERROR, result != kIOReturnSuccess) | |
44 << "IOPMAssertionRelease: " << result; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 // Called only from UI thread. | |
50 void PowerSaveBlocker::ApplyBlock(bool blocking) { | |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
52 | |
53 if (!g_power_thread) { | |
54 g_power_assertion = kIOPMNullAssertionID; | |
55 g_power_thread = new base::Thread("PowerSaveBlocker"); | |
56 g_power_thread->Start(); | |
57 } | |
58 | |
59 MessageLoop* loop = g_power_thread->message_loop(); | |
60 if (blocking) | |
61 loop->PostTask(FROM_HERE, NewRunnableFunction(CreateSleepAssertion)); | |
62 else | |
63 loop->PostTask(FROM_HERE, NewRunnableFunction(ReleaseSleepAssertion)); | |
64 } | |
OLD | NEW |