| OLD | NEW |
| 1 // Copyright (c) 2011 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/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 11 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 | 14 |
| 14 using content::BrowserThread; | 15 using content::BrowserThread; |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a | 19 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a |
| 19 // synchronous MIG call to configd, so if it is called on the main thread the UI | 20 // synchronous MIG call to configd, so if it is called on the main thread the UI |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 65 |
| 65 if (!g_power_thread) { | 66 if (!g_power_thread) { |
| 66 g_power_assertion = kIOPMNullAssertionID; | 67 g_power_assertion = kIOPMNullAssertionID; |
| 67 g_power_thread = new base::Thread("PowerSaveBlocker"); | 68 g_power_thread = new base::Thread("PowerSaveBlocker"); |
| 68 g_power_thread->Start(); | 69 g_power_thread->Start(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 g_power_thread->message_loop()-> | 72 g_power_thread->message_loop()-> |
| 72 PostTask(FROM_HERE, base::Bind(CreateSleepAssertion, type)); | 73 PostTask(FROM_HERE, base::Bind(CreateSleepAssertion, type)); |
| 73 } | 74 } |
| 75 |
| 76 // TODO(avi): Remove ^^^ this code in favor of vvv this code. |
| 77 // http://crbug.com/126591 |
| 78 |
| 79 namespace { |
| 80 |
| 81 // 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 |
| 83 // 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 . |
| 85 struct PowerSaveBlockerLazyInstanceTraits { |
| 86 static const bool kRegisterOnExit = false; |
| 87 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 88 |
| 89 static base::Thread* New(void* instance) { |
| 90 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); |
| 91 thread->Start(); |
| 92 return thread; |
| 93 } |
| 94 static void Delete(base::Thread* instance) { } |
| 95 }; |
| 96 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> |
| 97 g_power_thread2 = LAZY_INSTANCE_INITIALIZER; |
| 98 |
| 99 } // namespace |
| 100 |
| 101 namespace content { |
| 102 |
| 103 class PowerSaveBlocker2::Delegate |
| 104 : public base::RefCountedThreadSafe<PowerSaveBlocker2::Delegate> { |
| 105 public: |
| 106 Delegate(PowerSaveBlockerType type, const std::string& reason) |
| 107 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {} |
| 108 |
| 109 // Does the actual work to apply or remove the desired power save block. |
| 110 void ApplyBlock(); |
| 111 void RemoveBlock(); |
| 112 |
| 113 private: |
| 114 friend class base::RefCountedThreadSafe<PowerSaveBlocker2::Delegate>; |
| 115 ~Delegate() {} |
| 116 PowerSaveBlockerType type_; |
| 117 std::string reason_; |
| 118 IOPMAssertionID assertion_; |
| 119 }; |
| 120 |
| 121 void PowerSaveBlocker2::Delegate::ApplyBlock() { |
| 122 DCHECK_EQ(base::PlatformThread::CurrentId(), |
| 123 g_power_thread2.Pointer()->thread_id()); |
| 124 |
| 125 CFStringRef level = NULL; |
| 126 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more |
| 127 // details. |
| 128 switch (type_) { |
| 129 case PowerSaveBlocker2::kPowerSaveBlockPreventAppSuspension: |
| 130 level = kIOPMAssertionTypeNoIdleSleep; |
| 131 break; |
| 132 case PowerSaveBlocker2::kPowerSaveBlockPreventDisplaySleep: |
| 133 level = kIOPMAssertionTypeNoDisplaySleep; |
| 134 break; |
| 135 default: |
| 136 NOTREACHED(); |
| 137 break; |
| 138 } |
| 139 if (level) { |
| 140 // TODO(avi): Switch to IOPMAssertionCreateWithName when 10.6 is the minimum |
| 141 // system supported by Chromium. |
| 142 IOReturn result = IOPMAssertionCreate(level, |
| 143 kIOPMAssertionLevelOn, |
| 144 &assertion_); |
| 145 LOG_IF(ERROR, result != kIOReturnSuccess) |
| 146 << "IOPMAssertionCreate: " << result; |
| 147 } |
| 148 } |
| 149 |
| 150 void PowerSaveBlocker2::Delegate::RemoveBlock() { |
| 151 DCHECK_EQ(base::PlatformThread::CurrentId(), |
| 152 g_power_thread2.Pointer()->thread_id()); |
| 153 |
| 154 if (assertion_ != kIOPMNullAssertionID) { |
| 155 IOReturn result = IOPMAssertionRelease(assertion_); |
| 156 LOG_IF(ERROR, result != kIOReturnSuccess) |
| 157 << "IOPMAssertionRelease: " << result; |
| 158 } |
| 159 } |
| 160 |
| 161 PowerSaveBlocker2::PowerSaveBlocker2(PowerSaveBlockerType type, |
| 162 const std::string& reason) |
| 163 : delegate_(new PowerSaveBlocker2::Delegate(type, reason)) { |
| 164 g_power_thread2.Pointer()->message_loop()->PostTask( |
| 165 FROM_HERE, |
| 166 base::Bind(&PowerSaveBlocker2::Delegate::ApplyBlock, delegate_)); |
| 167 } |
| 168 |
| 169 PowerSaveBlocker2::~PowerSaveBlocker2() { |
| 170 g_power_thread2.Pointer()->message_loop()->PostTask( |
| 171 FROM_HERE, |
| 172 base::Bind(&PowerSaveBlocker2::Delegate::RemoveBlock, delegate_)); |
| 173 } |
| 174 |
| 175 } // namespace content |
| OLD | NEW |