| 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_impl.h" | 5 #include "content/browser/power_save_blocker_impl.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" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 static void Delete(base::Thread* instance) { } | 34 static void Delete(base::Thread* instance) { } |
| 35 }; | 35 }; |
| 36 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> | 36 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> |
| 37 g_power_thread = LAZY_INSTANCE_INITIALIZER; | 37 g_power_thread = LAZY_INSTANCE_INITIALIZER; |
| 38 | 38 |
| 39 } // namespace | 39 } // namespace |
| 40 | 40 |
| 41 class PowerSaveBlockerImpl::Delegate | 41 class PowerSaveBlockerImpl::Delegate |
| 42 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 42 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
| 43 public: | 43 public: |
| 44 Delegate(PowerSaveBlockerType type, const std::string& reason) | 44 Delegate(PowerSaveBlockerType type, const std::string& description) |
| 45 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {} | 45 : type_(type), |
| 46 description_(description), |
| 47 assertion_(kIOPMNullAssertionID) {} |
| 46 | 48 |
| 47 // Does the actual work to apply or remove the desired power save block. | 49 // Does the actual work to apply or remove the desired power save block. |
| 48 void ApplyBlock(); | 50 void ApplyBlock(); |
| 49 void RemoveBlock(); | 51 void RemoveBlock(); |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 friend class base::RefCountedThreadSafe<Delegate>; | 54 friend class base::RefCountedThreadSafe<Delegate>; |
| 53 ~Delegate() {} | 55 ~Delegate() {} |
| 54 PowerSaveBlockerType type_; | 56 PowerSaveBlockerType type_; |
| 55 std::string reason_; | 57 std::string description_; |
| 56 IOPMAssertionID assertion_; | 58 IOPMAssertionID assertion_; |
| 57 }; | 59 }; |
| 58 | 60 |
| 59 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { | 61 void PowerSaveBlockerImpl::Delegate::ApplyBlock() { |
| 60 DCHECK_EQ(base::PlatformThread::CurrentId(), | 62 DCHECK_EQ(base::PlatformThread::CurrentId(), |
| 61 g_power_thread.Pointer()->thread_id()); | 63 g_power_thread.Pointer()->thread_id()); |
| 62 | 64 |
| 63 CFStringRef level = NULL; | 65 CFStringRef level = NULL; |
| 64 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more | 66 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more |
| 65 // details. | 67 // details. |
| 66 switch (type_) { | 68 switch (type_) { |
| 67 case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: | 69 case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: |
| 68 level = kIOPMAssertionTypeNoIdleSleep; | 70 level = kIOPMAssertionTypeNoIdleSleep; |
| 69 break; | 71 break; |
| 70 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: | 72 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: |
| 71 level = kIOPMAssertionTypeNoDisplaySleep; | 73 level = kIOPMAssertionTypeNoDisplaySleep; |
| 72 break; | 74 break; |
| 73 default: | 75 default: |
| 74 NOTREACHED(); | 76 NOTREACHED(); |
| 75 break; | 77 break; |
| 76 } | 78 } |
| 77 if (level) { | 79 if (level) { |
| 78 base::ScopedCFTypeRef<CFStringRef> cf_reason( | 80 base::ScopedCFTypeRef<CFStringRef> cf_description( |
| 79 base::SysUTF8ToCFStringRef(reason_)); | 81 base::SysUTF8ToCFStringRef(description_)); |
| 80 IOReturn result = IOPMAssertionCreateWithName(level, | 82 IOReturn result = IOPMAssertionCreateWithName(level, kIOPMAssertionLevelOn, |
| 81 kIOPMAssertionLevelOn, | 83 cf_description, &assertion_); |
| 82 cf_reason, | |
| 83 &assertion_); | |
| 84 LOG_IF(ERROR, result != kIOReturnSuccess) | 84 LOG_IF(ERROR, result != kIOReturnSuccess) |
| 85 << "IOPMAssertionCreate: " << result; | 85 << "IOPMAssertionCreate: " << result; |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 89 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
| 90 DCHECK_EQ(base::PlatformThread::CurrentId(), | 90 DCHECK_EQ(base::PlatformThread::CurrentId(), |
| 91 g_power_thread.Pointer()->thread_id()); | 91 g_power_thread.Pointer()->thread_id()); |
| 92 | 92 |
| 93 if (assertion_ != kIOPMNullAssertionID) { | 93 if (assertion_ != kIOPMNullAssertionID) { |
| 94 IOReturn result = IOPMAssertionRelease(assertion_); | 94 IOReturn result = IOPMAssertionRelease(assertion_); |
| 95 LOG_IF(ERROR, result != kIOReturnSuccess) | 95 LOG_IF(ERROR, result != kIOReturnSuccess) |
| 96 << "IOPMAssertionRelease: " << result; | 96 << "IOPMAssertionRelease: " << result; |
| 97 } | 97 } |
| 98 } | 98 } |
| 99 | 99 |
| 100 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, | 100 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type, |
| 101 const std::string& reason) | 101 Reason reason, |
| 102 : delegate_(new Delegate(type, reason)) { | 102 const std::string& description) |
| 103 : delegate_(new Delegate(type, description)) { |
| 103 g_power_thread.Pointer()->message_loop()->PostTask( | 104 g_power_thread.Pointer()->message_loop()->PostTask( |
| 104 FROM_HERE, | 105 FROM_HERE, |
| 105 base::Bind(&Delegate::ApplyBlock, delegate_)); | 106 base::Bind(&Delegate::ApplyBlock, delegate_)); |
| 106 } | 107 } |
| 107 | 108 |
| 108 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 109 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
| 109 g_power_thread.Pointer()->message_loop()->PostTask( | 110 g_power_thread.Pointer()->message_loop()->PostTask( |
| 110 FROM_HERE, | 111 FROM_HERE, |
| 111 base::Bind(&Delegate::RemoveBlock, delegate_)); | 112 base::Bind(&Delegate::RemoveBlock, delegate_)); |
| 112 } | 113 } |
| 113 | 114 |
| 114 } // namespace content | 115 } // namespace content |
| OLD | NEW |