Add ICU message format support
[chromium-blink-merge.git] / content / browser / power_save_blocker_mac.cc
blob0ce80f6a2102886ed603031b5f3719d20bf966c8
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
3 // found in the LICENSE file.
5 #include "content/browser/power_save_blocker_impl.h"
7 #include <IOKit/pwr_mgt/IOPMLib.h>
9 #include "base/bind.h"
10 #include "base/lazy_instance.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread.h"
16 namespace content {
17 namespace {
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
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 .
23 struct PowerSaveBlockerLazyInstanceTraits {
24 static const bool kRegisterOnExit = false;
25 #ifndef NDEBUG
26 static const bool kAllowedToAccessOnNonjoinableThread = true;
27 #endif
29 static base::Thread* New(void* instance) {
30 base::Thread* thread = new (instance) base::Thread("PowerSaveBlocker");
31 thread->Start();
32 return thread;
34 static void Delete(base::Thread* instance) { }
36 base::LazyInstance<base::Thread, PowerSaveBlockerLazyInstanceTraits>
37 g_power_thread = LAZY_INSTANCE_INITIALIZER;
39 } // namespace
41 class PowerSaveBlockerImpl::Delegate
42 : public base::RefCountedThreadSafe<PowerSaveBlockerImpl::Delegate> {
43 public:
44 Delegate(PowerSaveBlockerType type, const std::string& description)
45 : type_(type),
46 description_(description),
47 assertion_(kIOPMNullAssertionID) {}
49 // Does the actual work to apply or remove the desired power save block.
50 void ApplyBlock();
51 void RemoveBlock();
53 private:
54 friend class base::RefCountedThreadSafe<Delegate>;
55 ~Delegate() {}
56 PowerSaveBlockerType type_;
57 std::string description_;
58 IOPMAssertionID assertion_;
61 void PowerSaveBlockerImpl::Delegate::ApplyBlock() {
62 DCHECK_EQ(base::PlatformThread::CurrentId(),
63 g_power_thread.Pointer()->GetThreadId());
65 CFStringRef level = NULL;
66 // See QA1340 <http://developer.apple.com/library/mac/#qa/qa1340/> for more
67 // details.
68 switch (type_) {
69 case PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension:
70 level = kIOPMAssertionTypeNoIdleSleep;
71 break;
72 case PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep:
73 level = kIOPMAssertionTypeNoDisplaySleep;
74 break;
75 default:
76 NOTREACHED();
77 break;
79 if (level) {
80 base::ScopedCFTypeRef<CFStringRef> cf_description(
81 base::SysUTF8ToCFStringRef(description_));
82 IOReturn result = IOPMAssertionCreateWithName(level, kIOPMAssertionLevelOn,
83 cf_description, &assertion_);
84 LOG_IF(ERROR, result != kIOReturnSuccess)
85 << "IOPMAssertionCreate: " << result;
89 void PowerSaveBlockerImpl::Delegate::RemoveBlock() {
90 DCHECK_EQ(base::PlatformThread::CurrentId(),
91 g_power_thread.Pointer()->GetThreadId());
93 if (assertion_ != kIOPMNullAssertionID) {
94 IOReturn result = IOPMAssertionRelease(assertion_);
95 LOG_IF(ERROR, result != kIOReturnSuccess)
96 << "IOPMAssertionRelease: " << result;
100 PowerSaveBlockerImpl::PowerSaveBlockerImpl(PowerSaveBlockerType type,
101 Reason reason,
102 const std::string& description)
103 : delegate_(new Delegate(type, description)) {
104 g_power_thread.Pointer()->message_loop()->PostTask(
105 FROM_HERE,
106 base::Bind(&Delegate::ApplyBlock, delegate_));
109 PowerSaveBlockerImpl::~PowerSaveBlockerImpl() {
110 g_power_thread.Pointer()->message_loop()->PostTask(
111 FROM_HERE,
112 base::Bind(&Delegate::RemoveBlock, delegate_));
115 } // namespace content