Bug 678607 - Work around Apple bug to avoid crashing with two-finger swipe on OS X Lion. r=bgirard

This commit is contained in:
Steven Michaud 2011-08-18 15:39:54 -05:00
parent ff65f150f8
commit 41d1b78f96

View File

@ -306,6 +306,15 @@ nsresult nsChildView::Create(nsIWidget *aParent,
if (!gChildViewMethodsSwizzled) {
nsToolkit::SwizzleMethods([NSView class], @selector(mouseDownCanMoveWindow),
@selector(nsChildView_NSView_mouseDownCanMoveWindow));
#ifdef __LP64__
if (nsToolkit::OnLionOrLater()) {
nsToolkit::SwizzleMethods([NSEvent class], @selector(addLocalMonitorForEventsMatchingMask:handler:),
@selector(nsChildView_NSEvent_addLocalMonitorForEventsMatchingMask:handler:),
PR_TRUE);
nsToolkit::SwizzleMethods([NSEvent class], @selector(removeMonitor:),
@selector(nsChildView_NSEvent_removeMonitor:), PR_TRUE);
}
#endif
#ifndef NP_NO_CARBON
TextInputHandler::SwizzleMethods();
#endif
@ -5092,3 +5101,48 @@ ChildViewMouseTracker::WindowAcceptsEvent(NSWindow* aWindow, NSEvent* aEvent,
}
@end
#ifdef __LP64__
// When using blocks, at least on OS X 10.7, the OS sometimes calls
// +[NSEvent removeMonitor:] more than once on a single event monitor, which
// causes crashes. See bug 678607. We hook these methods to work around
// the problem.
@interface NSEvent (MethodSwizzling)
+ (id)nsChildView_NSEvent_addLocalMonitorForEventsMatchingMask:(unsigned long long)mask handler:(id)block;
+ (void)nsChildView_NSEvent_removeMonitor:(id)eventMonitor;
@end
// This is a local copy of the AppKit frameworks sEventObservers hashtable.
// It only stores "local monitors". We use it to ensure that +[NSEvent
// removeMonitor:] is never called more than once on the same local monitor.
static NSHashTable *sLocalEventObservers = nil;
@implementation NSEvent (MethodSwizzling)
+ (id)nsChildView_NSEvent_addLocalMonitorForEventsMatchingMask:(unsigned long long)mask handler:(id)block
{
if (!sLocalEventObservers) {
sLocalEventObservers = [[NSHashTable hashTableWithOptions:
NSHashTableStrongMemory | NSHashTableObjectPointerPersonality] retain];
}
id retval =
[self nsChildView_NSEvent_addLocalMonitorForEventsMatchingMask:mask handler:block];
if (sLocalEventObservers && retval && ![sLocalEventObservers containsObject:retval]) {
[sLocalEventObservers addObject:retval];
}
return retval;
}
+ (void)nsChildView_NSEvent_removeMonitor:(id)eventMonitor
{
if (sLocalEventObservers && [eventMonitor isKindOfClass: ::NSClassFromString(@"_NSLocalEventObserver")]) {
if (![sLocalEventObservers containsObject:eventMonitor]) {
return;
}
[sLocalEventObservers removeObject:eventMonitor];
}
[self nsChildView_NSEvent_removeMonitor:eventMonitor];
}
@end
#endif // #ifdef __LP64__