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