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 "device/power_save_blocker/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" |
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 |
16 namespace content { | 16 namespace device { |
17 namespace { | 17 namespace { |
18 | 18 |
19 // 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 |
20 // 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 |
21 // is at the mercy of another process. See http://crbug.com/79559 and | 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 . | 22 // http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.
subproj/IOPMLibPrivate.c |
| 23 // . |
23 struct PowerSaveBlockerLazyInstanceTraits { | 24 struct PowerSaveBlockerLazyInstanceTraits { |
24 static const bool kRegisterOnExit = false; | 25 static const bool kRegisterOnExit = false; |
25 #ifndef NDEBUG | 26 #ifndef NDEBUG |
26 static const bool kAllowedToAccessOnNonjoinableThread = true; | 27 static const bool kAllowedToAccessOnNonjoinableThread = true; |
27 #endif | 28 #endif |
28 | 29 |
29 static base::Thread* New(void* instance) { | 30 static base::Thread* New(void* instance) { |
30 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); | 31 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); |
31 thread->Start(); | 32 thread->Start(); |
32 return thread; | 33 return thread; |
33 } | 34 } |
34 static void Delete(base::Thread* instance) { } | 35 static void Delete(base::Thread* instance) {} |
35 }; | 36 }; |
36 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> | 37 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> |
37 g_power_thread = LAZY_INSTANCE_INITIALIZER; | 38 g_power_thread = LAZY_INSTANCE_INITIALIZER; |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
41 class PowerSaveBlockerImpl::Delegate | 42 class PowerSaveBlockerImpl::Delegate |
42 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { | 43 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> { |
43 public: | 44 public: |
44 Delegate(PowerSaveBlockerType type, const std::string& description) | 45 Delegate(PowerSaveBlockerType type, const std::string& description) |
(...skipping 29 matching lines...) Expand all Loading... |
74 break; | 75 break; |
75 default: | 76 default: |
76 NOTREACHED(); | 77 NOTREACHED(); |
77 break; | 78 break; |
78 } | 79 } |
79 if (level) { | 80 if (level) { |
80 base::ScopedCFTypeRef<CFStringRef> cf_description( | 81 base::ScopedCFTypeRef<CFStringRef> cf_description( |
81 base::SysUTF8ToCFStringRef(description_)); | 82 base::SysUTF8ToCFStringRef(description_)); |
82 IOReturn result = IOPMAssertionCreateWithName(level, kIOPMAssertionLevelOn, | 83 IOReturn result = IOPMAssertionCreateWithName(level, kIOPMAssertionLevelOn, |
83 cf_description, &assertion_); | 84 cf_description, &assertion_); |
84 LOG_IF(ERROR, result != kIOReturnSuccess) | 85 LOG_IF(ERROR, result != kIOReturnSuccess) << "IOPMAssertionCreate: " |
85 << "IOPMAssertionCreate: " << result; | 86 << result; |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
89 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { | 90 void PowerSaveBlockerImpl::Delegate::RemoveBlock() { |
90 DCHECK_EQ(base::PlatformThread::CurrentId(), | 91 DCHECK_EQ(base::PlatformThread::CurrentId(), |
91 g_power_thread.Pointer()->GetThreadId()); | 92 g_power_thread.Pointer()->GetThreadId()); |
92 | 93 |
93 if (assertion_ != kIOPMNullAssertionID) { | 94 if (assertion_ != kIOPMNullAssertionID) { |
94 IOReturn result = IOPMAssertionRelease(assertion_); | 95 IOReturn result = IOPMAssertionRelease(assertion_); |
95 LOG_IF(ERROR, result != kIOReturnSuccess) | 96 LOG_IF(ERROR, result != kIOReturnSuccess) << "IOPMAssertionRelease: " |
96 << "IOPMAssertionRelease: " << result; | 97 << result; |
97 } | 98 } |
98 } | 99 } |
99 | 100 |
100 PowerSaveBlockerImpl::PowerSaveBlockerImpl( | 101 PowerSaveBlockerImpl::PowerSaveBlockerImpl( |
101 PowerSaveBlockerType type, | 102 PowerSaveBlockerType type, |
102 Reason reason, | 103 Reason reason, |
103 const std::string& description, | 104 const std::string& description, |
104 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, | 105 scoped_refptr<base::SequencedTaskRunner> ui_task_runner, |
105 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) | 106 scoped_refptr<base::SingleThreadTaskRunner> blocking_task_runner) |
106 : delegate_(new Delegate(type, description)), | 107 : delegate_(new Delegate(type, description)), |
107 ui_task_runner_(ui_task_runner), | 108 ui_task_runner_(ui_task_runner), |
108 blocking_task_runner_(blocking_task_runner) { | 109 blocking_task_runner_(blocking_task_runner) { |
109 g_power_thread.Pointer()->message_loop()->PostTask( | 110 g_power_thread.Pointer()->message_loop()->PostTask( |
110 FROM_HERE, | 111 FROM_HERE, base::Bind(&Delegate::ApplyBlock, delegate_)); |
111 base::Bind(&Delegate::ApplyBlock, delegate_)); | |
112 } | 112 } |
113 | 113 |
114 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { | 114 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() { |
115 g_power_thread.Pointer()->message_loop()->PostTask( | 115 g_power_thread.Pointer()->message_loop()->PostTask( |
116 FROM_HERE, | 116 FROM_HERE, base::Bind(&Delegate::RemoveBlock, delegate_)); |
117 base::Bind(&Delegate::RemoveBlock, delegate_)); | |
118 } | 117 } |
119 | 118 |
120 } // namespace content | 119 } // namespace device |
OLD | NEW |