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

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

Issue 8251008: Add screen power save block level. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ready Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
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/threading/platform_thread.h" 9 #include "base/threading/platform_thread.h"
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
11 #include "content/browser/browser_thread.h" 11 #include "content/browser/browser_thread.h"
12 12
13 namespace { 13 namespace {
14 14
15 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a 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 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 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 . 18 // http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt. subproj/IOPMLibPrivate.c .
19 base::Thread* g_power_thread; 19 base::Thread* g_power_thread;
20 IOPMAssertionID g_power_assertion; 20 IOPMAssertionID g_power_assertion;
21 21
22 void CreateSleepAssertion() { 22 void CreateSleepAssertion(PowerSaveBlocker::PowerSaveBlockerType type) {
23 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); 23 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id());
24 IOReturn result; 24 IOReturn result;
25 DCHECK_EQ(g_power_assertion, kIOPMNullAssertionID);
26 25
27 // Block just idle sleep; allow display sleep. 26 if (g_power_assertion != kIOPMNullAssertionID) {
28 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa2004/qa1340.html> 27 result = IOPMAssertionRelease(g_power_assertion);
29 // for more details. 28 g_power_assertion = kIOPMNullAssertionID;
30 result = IOPMAssertionCreate(kIOPMAssertionTypeNoIdleSleep, 29 LOG_IF(ERROR, result != kIOReturnSuccess)
31 kIOPMAssertionLevelOn, 30 << "IOPMAssertionRelease: " << result;
32 &g_power_assertion); 31 }
33 LOG_IF(ERROR, result != kIOReturnSuccess)
34 << "IOPMAssertionCreate: " << result;
35 }
36 32
37 void ReleaseSleepAssertion() { 33 CFStringRef level = NULL;
38 DCHECK_EQ(base::PlatformThread::CurrentId(), g_power_thread->thread_id()); 34 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more
39 IOReturn result; 35 // details.
40 DCHECK_NE(g_power_assertion, kIOPMNullAssertionID); 36 switch (type) {
41 result = IOPMAssertionRelease(g_power_assertion); 37 case PowerSaveBlocker::kPowerSaveBlockPreventSystemSleep:
42 g_power_assertion = kIOPMNullAssertionID; 38 level = kIOPMAssertionTypeNoIdleSleep;
43 LOG_IF(ERROR, result != kIOReturnSuccess) 39 break;
44 << "IOPMAssertionRelease: " << result; 40 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
41 level = kIOPMAssertionTypeNoDisplaySleep;
42 break;
43 default:
44 break;
45 }
46 if (level) {
47 result = IOPMAssertionCreate(level,
48 kIOPMAssertionLevelOn,
49 &g_power_assertion);
50 LOG_IF(ERROR, result != kIOReturnSuccess)
51 << "IOPMAssertionCreate: " << result;
52 }
45 } 53 }
46 54
47 } // namespace 55 } // namespace
48 56
49 // Called only from UI thread. 57 // Called only from UI thread.
50 void PowerSaveBlocker::ApplyBlock(bool blocking) { 58 void PowerSaveBlocker::ApplyBlock(PowerSaveBlockerType type) {
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
52 60
53 if (!g_power_thread) { 61 if (!g_power_thread) {
54 g_power_assertion = kIOPMNullAssertionID; 62 g_power_assertion = kIOPMNullAssertionID;
55 g_power_thread = new base::Thread("PowerSaveBlocker"); 63 g_power_thread = new base::Thread("PowerSaveBlocker");
56 g_power_thread->Start(); 64 g_power_thread->Start();
57 } 65 }
58 66
59 MessageLoop* loop = g_power_thread->message_loop(); 67 g_power_thread->message_loop()->
60 if (blocking) 68 PostTask(FROM_HERE, NewRunnableFunction(CreateSleepAssertion, type));
61 loop->PostTask(FROM_HERE, NewRunnableFunction(CreateSleepAssertion));
62 else
63 loop->PostTask(FROM_HERE, NewRunnableFunction(ReleaseSleepAssertion));
64 } 69 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698