Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: content/browser/power_save_blocker_mac.cc

Issue 11784016: chromeos: Block system suspend while uploading files to Drive (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: PowerSaveBlocker is a pure interface Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/public/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 15
(...skipping 11 matching lines...) Expand all
27 static base::Thread* New(void* instance) { 27 static base::Thread* New(void* instance) {
28 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker"); 28 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker");
29 thread->Start(); 29 thread->Start();
30 return thread; 30 return thread;
31 } 31 }
32 static void Delete(base::Thread* instance) { } 32 static void Delete(base::Thread* instance) { }
33 }; 33 };
34 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits> 34 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits>
35 g_power_thread = LAZY_INSTANCE_INITIALIZER; 35 g_power_thread = LAZY_INSTANCE_INITIALIZER;
36 36
37 } // namespace 37 class PowerSaveBlockerMac : public PowerSaveBlocker {
38 public:
39 PowerSaveBlockerMac(PowerSaveBlockerType type, const std::string& reason);
40 virtual ~PowerSaveBlockerMac();
38 41
39 class PowerSaveBlocker::Delegate 42 private:
40 : public base::RefCountedThreadSafe<PowerSaveBlocker::Delegate> { 43 class Delegate;
44
45 // A second object with different lifetime than the RAII container.
46 scoped_refptr<Delegate> delegate_;
47
48 DISALLOW_COPY_AND_ASSIGN(PowerSaveBlockerMac);
49 };
50
51 class PowerSaveBlockerMac::Delegate
52 : public base::RefCountedThreadSafe<PowerSaveBlockerMac::Delegate> {
41 public: 53 public:
42 Delegate(PowerSaveBlockerType type, const std::string& reason) 54 Delegate(PowerSaveBlockerType type, const std::string& reason)
43 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {} 55 : type_(type), reason_(reason), assertion_(kIOPMNullAssertionID) {}
44 56
45 // Does the actual work to apply or remove the desired power save block. 57 // Does the actual work to apply or remove the desired power save block.
46 void ApplyBlock(); 58 void ApplyBlock();
47 void RemoveBlock(); 59 void RemoveBlock();
48 60
49 private: 61 private:
50 friend class base::RefCountedThreadSafe<Delegate>; 62 friend class base::RefCountedThreadSafe<Delegate>;
51 ~Delegate() {} 63 ~Delegate() {}
52 PowerSaveBlockerType type_; 64 PowerSaveBlockerType type_;
53 std::string reason_; 65 std::string reason_;
54 IOPMAssertionID assertion_; 66 IOPMAssertionID assertion_;
55 }; 67 };
56 68
57 void PowerSaveBlocker::Delegate::ApplyBlock() { 69 void PowerSaveBlockerMac::Delegate::ApplyBlock() {
58 DCHECK_EQ(base::PlatformThread::CurrentId(), 70 DCHECK_EQ(base::PlatformThread::CurrentId(),
59 g_power_thread.Pointer()->thread_id()); 71 g_power_thread.Pointer()->thread_id());
60 72
61 CFStringRef level = NULL; 73 CFStringRef level = NULL;
62 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more 74 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more
63 // details. 75 // details.
64 switch (type_) { 76 switch (type_) {
65 case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension: 77 case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension:
66 level = kIOPMAssertionTypeNoIdleSleep; 78 level = kIOPMAssertionTypeNoIdleSleep;
67 break; 79 break;
68 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep: 80 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
69 level = kIOPMAssertionTypeNoDisplaySleep; 81 level = kIOPMAssertionTypeNoDisplaySleep;
70 break; 82 break;
71 default: 83 default:
72 NOTREACHED(); 84 NOTREACHED();
73 break; 85 break;
74 } 86 }
75 if (level) { 87 if (level) {
76 base::mac::ScopedCFTypeRef<CFStringRef> cf_reason( 88 base::mac::ScopedCFTypeRef<CFStringRef> cf_reason(
77 base::SysUTF8ToCFStringRef(reason_)); 89 base::SysUTF8ToCFStringRef(reason_));
78 IOReturn result = IOPMAssertionCreateWithName(level, 90 IOReturn result = IOPMAssertionCreateWithName(level,
79 kIOPMAssertionLevelOn, 91 kIOPMAssertionLevelOn,
80 cf_reason, 92 cf_reason,
81 &assertion_); 93 &assertion_);
82 LOG_IF(ERROR, result != kIOReturnSuccess) 94 LOG_IF(ERROR, result != kIOReturnSuccess)
83 << "IOPMAssertionCreate: " << result; 95 << "IOPMAssertionCreate: " << result;
84 } 96 }
85 } 97 }
86 98
87 void PowerSaveBlocker::Delegate::RemoveBlock() { 99 void PowerSaveBlockerMac::Delegate::RemoveBlock() {
88 DCHECK_EQ(base::PlatformThread::CurrentId(), 100 DCHECK_EQ(base::PlatformThread::CurrentId(),
89 g_power_thread.Pointer()->thread_id()); 101 g_power_thread.Pointer()->thread_id());
90 102
91 if (assertion_ != kIOPMNullAssertionID) { 103 if (assertion_ != kIOPMNullAssertionID) {
92 IOReturn result = IOPMAssertionRelease(assertion_); 104 IOReturn result = IOPMAssertionRelease(assertion_);
93 LOG_IF(ERROR, result != kIOReturnSuccess) 105 LOG_IF(ERROR, result != kIOReturnSuccess)
94 << "IOPMAssertionRelease: " << result; 106 << "IOPMAssertionRelease: " << result;
95 } 107 }
96 } 108 }
97 109
98 PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type, 110 PowerSaveBlockerMac::PowerSaveBlockerMac(PowerSaveBlockerType type,
99 const std::string& reason) 111 const std::string& reason)
100 : delegate_(new Delegate(type, reason)) { 112 : delegate_(new Delegate(type, reason)) {
101 g_power_thread.Pointer()->message_loop()->PostTask( 113 g_power_thread.Pointer()->message_loop()->PostTask(
102 FROM_HERE, 114 FROM_HERE,
103 base::Bind(&Delegate::ApplyBlock, delegate_)); 115 base::Bind(&Delegate::ApplyBlock, delegate_));
104 } 116 }
105 117
106 PowerSaveBlocker::~PowerSaveBlocker() { 118 PowerSaveBlockerMac::~PowerSaveBlockerMac() {
107 g_power_thread.Pointer()->message_loop()->PostTask( 119 g_power_thread.Pointer()->message_loop()->PostTask(
108 FROM_HERE, 120 FROM_HERE,
109 base::Bind(&Delegate::RemoveBlock, delegate_)); 121 base::Bind(&Delegate::RemoveBlock, delegate_));
110 } 122 }
111 123
124 } // namespace
125
126 // static
127 scoped_ptr<PowerSaveBlocker> PowerSaveBlocker::Create(
128 PowerSaveBlockerType type,
129 const std::string& reason) {
130 return scoped_ptr<PowerSaveBlocker>(new PowerSaveBlockerMac(type, reason));
131 }
132
112 } // namespace content 133 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698