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