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/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
12 #include "base/sys_string_conversions.h" | 12 #include "base/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 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 | 16 |
| 17 namespace content { |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Power management cannot be done on the UI thread. IOPMAssertionCreate does a | 20 // 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 // 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 // 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 // http://www.opensource.apple.com/source/IOKitUser/IOKitUser-514.16.31/pwr_mgt.
subproj/IOPMLibPrivate.c . |
23 struct PowerSaveBlockerLazyInstanceTraits { | 24 struct PowerSaveBlockerLazyInstanceTraits { |
24 static const bool kRegisterOnExit = false; | 25 static const bool kRegisterOnExit = false; |
25 static const bool kAllowedToAccessOnNonjoinableThread = true; | 26 static const bool kAllowedToAccessOnNonjoinableThread = true; |
26 | 27 |
27 static base::Thread* New(void* instance) { | 28 static base::Thread* New(void* instance) { |
28 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); | 29 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); |
29 thread->Start(); | 30 thread->Start(); |
30 return thread; | 31 return thread; |
31 } | 32 } |
32 static void Delete(base::Thread* instance) { } | 33 static void Delete(base::Thread* instance) { } |
33 }; | 34 }; |
34 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> | 35 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> |
35 g_power_thread = LAZY_INSTANCE_INITIALIZER; | 36 g_power_thread = LAZY_INSTANCE_INITIALIZER; |
36 | 37 |
37 } // namespace | 38 } // namespace |
38 | 39 |
39 namespace content { | |
40 | |
41 class PowerSaveBlocker::Delegate | 40 class PowerSaveBlocker::Delegate |
42 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { | 41 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { |
43 public: | 42 public: |
44 Delegate(PowerSaveBlockerType type, const std::string& reason) | 43 Delegate(PowerSaveBlockerType type, const std::string& reason) |
45 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {} | 44 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {} |
46 | 45 |
47 // Does the actual work to apply or remove the desired power save block. | 46 // Does the actual work to apply or remove the desired power save block. |
48 void ApplyBlock(); | 47 void ApplyBlock(); |
49 void RemoveBlock(); | 48 void RemoveBlock(); |
50 | 49 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 base::Bind(&Delegate::ApplyBlock, delegate_)); | 104 base::Bind(&Delegate::ApplyBlock, delegate_)); |
106 } | 105 } |
107 | 106 |
108 PowerSaveBlocker::~PowerSaveBlocker() { | 107 PowerSaveBlocker::~PowerSaveBlocker() { |
109 g_power_thread.Pointer()->message_loop()->PostTask( | 108 g_power_thread.Pointer()->message_loop()->PostTask( |
110 FROM_HERE, | 109 FROM_HERE, |
111 base::Bind(&Delegate::RemoveBlock, delegate_)); | 110 base::Bind(&Delegate::RemoveBlock, delegate_)); |
112 } | 111 } |
113 | 112 |
114 } // namespace content | 113 } // namespace content |
OLD | NEW |