OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/power_save_blocker.h" | 5 #include "content/browser/power_save_blocker.h" |
6 | 6 |
7 #include <IOKit/pwr_mgt/IOPMLib.h> | 7 #include <IOKit/pwr_mgt/IOPMLib.h> |
8 | 8 |
| 9 #include "base/bind.h" |
9 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
11 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a | 16 // 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 // 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 // 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 // http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.
subproj/IOPMLibPrivate.c . |
19 base::Thread* g_power_thread; | 20 base::Thread* g_power_thread; |
20 IOPMAssertionID g_power_assertion; | 21 IOPMAssertionID g_power_assertion; |
21 | 22 |
22 void CreateSleepAssertion() { | 23 void CreateSleepAssertion(PowerSaveBlocker::PowerSaveBlockerType type) { |
23 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); | 24 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); |
24 IOReturn result; | 25 IOReturn result; |
25 DCHECK_EQ(g_power_assertion, kIOPMNullAssertionID); | |
26 | 26 |
27 // Block just idle sleep; allow display sleep. | 27 if (g_power_assertion != kIOPMNullAssertionID) { |
28 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa2004/qa1340.html> | 28 result = IOPMAssertionRelease(g_power_assertion); |
29 // for more details. | 29 g_power_assertion = kIOPMNullAssertionID; |
30 result = IOPMAssertionCreate(kIOPMAssertionTypeNoIdleSleep, | 30 LOG_IF(ERROR, result != kIOReturnSuccess) |
31 kIOPMAssertionLevelOn, | 31 << "IOPMAssertionRelease: " << result; |
32 &g_power_assertion); | 32 } |
33 LOG_IF(ERROR, result != kIOReturnSuccess) | |
34 << "IOPMAssertionCreate: " << result; | |
35 } | |
36 | 33 |
37 void ReleaseSleepAssertion() { | 34 CFStringRef level = NULL; |
38 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); | 35 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more |
39 IOReturn result; | 36 // details. |
40 DCHECK_NE(g_power_assertion, kIOPMNullAssertionID); | 37 switch (type) { |
41 result = IOPMAssertionRelease(g_power_assertion); | 38 case PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep: |
42 g_power_assertion = kIOPMNullAssertionID; | 39 level = kIOPMAssertionTypeNoIdleSleep; |
43 LOG_IF(ERROR, result != kIOReturnSuccess) | 40 break; |
44 << "IOPMAssertionRelease: " << result; | 41 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: |
| 42 level = kIOPMAssertionTypeNoDisplaySleep; |
| 43 break; |
| 44 default: |
| 45 break; |
| 46 } |
| 47 if (level) { |
| 48 result = IOPMAssertionCreate(level, |
| 49 kIOPMAssertionLevelOn, |
| 50 &g_power_assertion); |
| 51 LOG_IF(ERROR, result != kIOReturnSuccess) |
| 52 << "IOPMAssertionCreate: " << result; |
| 53 } |
45 } | 54 } |
46 | 55 |
47 } // namespace | 56 } // namespace |
48 | 57 |
49 // Called only from UI thread. | 58 // Called only from UI thread. |
50 void PowerSaveBlocker::ApplyBlock(bool blocking) { | 59 // static |
| 60 void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) { |
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
52 | 62 |
53 if (!g_power_thread) { | 63 if (!g_power_thread) { |
54 g_power_assertion = kIOPMNullAssertionID; | 64 g_power_assertion = kIOPMNullAssertionID; |
55 g_power_thread = new base::Thread("PowerSaveBlocker"); | 65 g_power_thread = new base::Thread("PowerSaveBlocker"); |
56 g_power_thread->Start(); | 66 g_power_thread->Start(); |
57 } | 67 } |
58 | 68 |
59 MessageLoop* loop = g_power_thread->message_loop(); | 69 g_power_thread->message_loop()-> |
60 if (blocking) | 70 PostTask(FROM_HERE, base::Bind(CreateSleepAssertion, type)); |
61 loop->PostTask(FROM_HERE, NewRunnableFunction(CreateSleepAssertion)); | |
62 else | |
63 loop->PostTask(FROM_HERE, NewRunnableFunction(ReleaseSleepAssertion)); | |
64 } | 71 } |
OLD | NEW |