2 * Copyright (C) 2006, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #import "SharedTimer.h"
29 #import <IOKit/IOMessage.h>
30 #import <IOKit/pwr_mgt/IOPMLib.h>
31 #import <wtf/Assertions.h>
32 #import <wtf/Noncopyable.h>
33 #import <wtf/PassOwnPtr.h>
34 #import <wtf/UnusedParam.h>
38 // On Snow Leopard and newer we'll ask IOKit to deliver notifications on a queue.
39 #if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED == 1050
40 #define IOKIT_WITHOUT_LIBDISPATCH 1
45 static const CFTimeInterval distantFuture = 60 * 60 * 24 * 365 * 10; // Decade.
47 static void (*sharedTimerFiredFunction)();
48 static void timerFired(CFRunLoopTimerRef, void*);
50 #if !defined(IOKIT_WITHOUT_LIBDISPATCH) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MAX_ALLOWED == 1060
51 extern "C" void IONotificationPortSetDispatchQueue(IONotificationPortRef notify, dispatch_queue_t queue);
55 WTF_MAKE_NONCOPYABLE(PowerObserver);
58 static PassOwnPtr<PowerObserver> create()
60 return adoptPtr(new PowerObserver);
67 static void didReceiveSystemPowerNotification(void* context, io_service_t, uint32_t messageType, void* messageArgument);
68 void didReceiveSystemPowerNotification(io_service_t, uint32_t messageType, void* messageArgument);
70 void restartSharedTimer();
72 io_connect_t m_powerConnection;
73 IONotificationPortRef m_notificationPort;
74 io_object_t m_notifierReference;
75 #ifdef IOKIT_WITHOUT_LIBDISPATCH
76 CFRunLoopSourceRef m_runLoopSource;
78 dispatch_queue_t m_dispatchQueue;
82 PowerObserver::PowerObserver()
83 : m_powerConnection(0)
84 , m_notificationPort(0)
85 , m_notifierReference(0)
86 #ifdef IOKIT_WITHOUT_LIBDISPATCH
89 , m_dispatchQueue(dispatch_queue_create("com.apple.WebKit.PowerObserver", 0))
92 m_powerConnection = IORegisterForSystemPower(this, &m_notificationPort, didReceiveSystemPowerNotification, &m_notifierReference);
93 if (!m_powerConnection)
96 #ifdef IOKIT_WITHOUT_LIBDISPATCH
97 m_runLoopSource = IONotificationPortGetRunLoopSource(m_notificationPort);
98 CFRunLoopAddSource(CFRunLoopGetMain(), m_runLoopSource, kCFRunLoopCommonModes);
100 IONotificationPortSetDispatchQueue(m_notificationPort, m_dispatchQueue);
104 PowerObserver::~PowerObserver()
106 if (!m_powerConnection)
109 #ifdef IOKIT_WITHOUT_LIBDISPATCH
110 CFRunLoopRemoveSource(CFRunLoopGetMain(), m_runLoopSource, kCFRunLoopCommonModes);
112 dispatch_release(m_dispatchQueue);
115 IODeregisterForSystemPower(&m_notifierReference);
116 IOServiceClose(m_powerConnection);
117 IONotificationPortDestroy(m_notificationPort);
120 void PowerObserver::didReceiveSystemPowerNotification(void* context, io_service_t service, uint32_t messageType, void* messageArgument)
122 static_cast<PowerObserver*>(context)->didReceiveSystemPowerNotification(service, messageType, messageArgument);
125 void PowerObserver::didReceiveSystemPowerNotification(io_service_t, uint32_t messageType, void* messageArgument)
127 IOAllowPowerChange(m_powerConnection, reinterpret_cast<long>(messageArgument));
129 // We only care about the "wake from sleep" message.
130 if (messageType != kIOMessageSystemWillPowerOn)
133 #ifdef IOKIT_WITHOUT_LIBDISPATCH
134 restartSharedTimer();
136 // We need to restart the timer on the main thread.
137 CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^() {
138 restartSharedTimer();
143 void PowerObserver::restartSharedTimer()
145 ASSERT(CFRunLoopGetCurrent() == CFRunLoopGetMain());
151 static CFRunLoopTimerRef sharedTimer()
153 static CFRunLoopTimerRef timer;
154 static dispatch_once_t onceToken;
155 dispatch_once(&onceToken, ^{
156 timer = CFRunLoopTimerCreate(0, CFAbsoluteTimeGetCurrent() + distantFuture, distantFuture, 0, 0, timerFired, 0);
157 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
159 static PowerObserver* powerObserver;
160 powerObserver = PowerObserver::create().leakPtr();
165 void setSharedTimerFiredFunction(void (*f)())
167 ASSERT(!sharedTimerFiredFunction || sharedTimerFiredFunction == f);
169 sharedTimerFiredFunction = f;
172 static void timerFired(CFRunLoopTimerRef, void*)
174 // FIXME: We can remove this global catch-all if we fix <rdar://problem/5299018>.
175 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
176 sharedTimerFiredFunction();
180 void setSharedTimerFireInterval(double interval)
182 ASSERT(sharedTimerFiredFunction);
183 CFAbsoluteTime fireDate = CFAbsoluteTimeGetCurrent() + interval;
184 CFRunLoopTimerSetNextFireDate(sharedTimer(), fireDate);
187 void stopSharedTimer()
189 CFRunLoopTimerSetNextFireDate(sharedTimer(), CFAbsoluteTimeGetCurrent() + distantFuture);
192 } // namespace WebCore