SharedTimerMac.cpp [plain text]
#import "config.h"
#import "SharedTimer.h"
#include <Foundation/Foundation.h>
#include <wtf/Assertions.h>
@class PowerNotifier;
namespace WebCore {
static PowerNotifier *powerNotifier;
static CFRunLoopTimerRef sharedTimer;
static void (*sharedTimerFiredFunction)();
static void timerFired(CFRunLoopTimerRef, void*);
}
@interface PowerNotifier : NSObject
@end
@implementation PowerNotifier
- (id)init
{
self = [super init];
if (self)
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(didWake:)
name:NSWorkspaceDidWakeNotification
object:nil];
return self;
}
- (void)didWake:(NSNotification *)notification
{
if (WebCore::sharedTimer) {
WebCore::stopSharedTimer();
WebCore::timerFired(0, 0);
}
}
@end
namespace WebCore {
void setSharedTimerFiredFunction(void (*f)())
{
ASSERT(!sharedTimerFiredFunction || sharedTimerFiredFunction == f);
sharedTimerFiredFunction = f;
}
static void timerFired(CFRunLoopTimerRef, void*)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sharedTimerFiredFunction();
[pool drain];
}
void setSharedTimerFireTime(double fireTime)
{
ASSERT(sharedTimerFiredFunction);
if (sharedTimer) {
CFRunLoopTimerInvalidate(sharedTimer);
CFRelease(sharedTimer);
}
CFAbsoluteTime fireDate = fireTime - kCFAbsoluteTimeIntervalSince1970;
sharedTimer = CFRunLoopTimerCreate(0, fireDate, 0, 0, 0, timerFired, 0);
CFRunLoopAddTimer(CFRunLoopGetCurrent(), sharedTimer, kCFRunLoopCommonModes);
if (!powerNotifier) {
powerNotifier = [[PowerNotifier alloc] init];
CFRetain(powerNotifier);
[powerNotifier release];
}
}
void stopSharedTimer()
{
if (sharedTimer) {
CFRunLoopTimerInvalidate(sharedTimer);
CFRelease(sharedTimer);
sharedTimer = 0;
}
}
}