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 CFRunLoopTimerRef sharedTimer;
46 static void (*sharedTimerFiredFunction)();
47 static void timerFired(CFRunLoopTimerRef, void*);
49 #if !defined(IOKIT_WITHOUT_LIBDISPATCH) && !PLATFORM(IOS) && __MAC_OS_X_VERSION_MAX_ALLOWED == 1060
50 extern "C" void IONotificationPortSetDispatchQueue(IONotificationPortRef notify, dispatch_queue_t queue);
54 WTF_MAKE_NONCOPYABLE(PowerObserver);
57 static PassOwnPtr<PowerObserver> create()
59 return adoptPtr(new PowerObserver);
66 static void didReceiveSystemPowerNotification(void* context, io_service_t, uint32_t messageType, void* messageArgument);
67 void didReceiveSystemPowerNotification(io_service_t, uint32_t messageType, void* messageArgument);
69 void restartSharedTimer();
71 io_connect_t m_powerConnection;
72 IONotificationPortRef m_notificationPort;
73 io_object_t m_notifierReference;
74 #ifdef IOKIT_WITHOUT_LIBDISPATCH
75 CFRunLoopSourceRef m_runLoopSource;
77 dispatch_queue_t m_dispatchQueue;
81 PowerObserver::PowerObserver()
82 : m_powerConnection(0)
83 , m_notificationPort(0)
84 , m_notifierReference(0)
85 #ifdef IOKIT_WITHOUT_LIBDISPATCH
88 , m_dispatchQueue(dispatch_queue_create("com.apple.WebKit.PowerObserver", 0))
91 m_powerConnection = IORegisterForSystemPower(this, &m_notificationPort, didReceiveSystemPowerNotification, &m_notifierReference);
92 if (!m_powerConnection)
95 #ifdef IOKIT_WITHOUT_LIBDISPATCH
96 m_runLoopSource = IONotificationPortGetRunLoopSource(m_notificationPort);
97 CFRunLoopAddSource(CFRunLoopGetMain(), m_runLoopSource, kCFRunLoopCommonModes);
99 IONotificationPortSetDispatchQueue(m_notificationPort, m_dispatchQueue);
103 PowerObserver::~PowerObserver()
105 if (!m_powerConnection)
108 #ifdef IOKIT_WITHOUT_LIBDISPATCH
109 CFRunLoopRemoveSource(CFRunLoopGetMain(), m_runLoopSource, kCFRunLoopCommonModes);
111 dispatch_release(m_dispatchQueue);
114 IODeregisterForSystemPower(&m_notifierReference);
115 IOServiceClose(m_powerConnection);
116 IONotificationPortDestroy(m_notificationPort);
119 void PowerObserver::didReceiveSystemPowerNotification(void* context, io_service_t service, uint32_t messageType, void* messageArgument)
121 static_cast<PowerObserver*>(context)->didReceiveSystemPowerNotification(service, messageType, messageArgument);
124 void PowerObserver::didReceiveSystemPowerNotification(io_service_t, uint32_t messageType, void* messageArgument)
126 IOAllowPowerChange(m_powerConnection, reinterpret_cast<long>(messageArgument));
128 // We only care about the "wake from sleep" message.
129 if (messageType != kIOMessageSystemWillPowerOn)
132 #ifdef IOKIT_WITHOUT_LIBDISPATCH
133 restartSharedTimer();
135 // We need to restart the timer on the main thread.
136 CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^() {
137 restartSharedTimer();
142 void PowerObserver::restartSharedTimer()
144 ASSERT(CFRunLoopGetCurrent() == CFRunLoopGetMain());
153 static PowerObserver* PowerObserver;
155 void setSharedTimerFiredFunction(void (*f)())
157 ASSERT(!sharedTimerFiredFunction || sharedTimerFiredFunction == f);
159 sharedTimerFiredFunction = f;
162 static void timerFired(CFRunLoopTimerRef, void*)
164 // FIXME: We can remove this global catch-all if we fix <rdar://problem/5299018>.
165 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
166 sharedTimerFiredFunction();
170 void setSharedTimerFireInterval(double interval)
172 ASSERT(sharedTimerFiredFunction);
175 CFRunLoopTimerInvalidate(sharedTimer);
176 CFRelease(sharedTimer);
179 CFAbsoluteTime fireDate = CFAbsoluteTimeGetCurrent() + interval;
180 sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0);
181 CFRunLoopAddTimer(CFRunLoopGetCurrent(), sharedTimer, kCFRunLoopCommonModes);
184 PowerObserver = PowerObserver::create().leakPtr();
187 void stopSharedTimer()
190 CFRunLoopTimerInvalidate(sharedTimer);
191 CFRelease(sharedTimer);
196 } // namespace WebCore