2006-05-10 17:30:15 +00:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-05-10 17:30:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Runs the main native Cocoa run loop, interrupting it as needed to process
|
|
|
|
* Gecko events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2011-03-03 16:20:02 +00:00
|
|
|
#include "CustomCocoaEvents.h"
|
|
|
|
#include "mozilla/WidgetTraceEvent.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsAppShell.h"
|
2019-04-22 19:58:52 +00:00
|
|
|
#include "gfxPlatform.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsString.h"
|
2007-07-17 20:29:39 +00:00
|
|
|
#include "nsIRollupListener.h"
|
|
|
|
#include "nsIWidget.h"
|
2007-09-19 19:17:06 +00:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2008-02-13 23:11:11 +00:00
|
|
|
#include "nsObjCExceptions.h"
|
2012-05-17 07:53:20 +00:00
|
|
|
#include "nsCocoaFeatures.h"
|
2008-03-27 03:42:57 +00:00
|
|
|
#include "nsCocoaUtils.h"
|
2008-06-30 16:30:22 +00:00
|
|
|
#include "nsChildView.h"
|
2008-10-17 21:58:47 +00:00
|
|
|
#include "nsToolkit.h"
|
2011-02-24 04:48:12 +00:00
|
|
|
#include "TextInputHandler.h"
|
2018-04-30 01:21:20 +00:00
|
|
|
#include "mozilla/BackgroundHangMonitor.h"
|
2013-03-18 14:25:50 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2017-03-09 11:34:49 +00:00
|
|
|
#include "ScreenHelperCocoa.h"
|
2018-08-31 20:29:30 +00:00
|
|
|
#include "mozilla/Hal.h"
|
2017-03-09 11:34:49 +00:00
|
|
|
#include "mozilla/widget/ScreenManager.h"
|
2017-07-03 22:33:16 +00:00
|
|
|
#include "HeadlessScreenHelper.h"
|
2013-07-11 20:21:45 +00:00
|
|
|
#include "pratom.h"
|
2016-10-08 09:14:49 +00:00
|
|
|
#if !defined(RELEASE_OR_BETA) || defined(DEBUG)
|
2019-01-21 17:18:16 +00:00
|
|
|
# include "nsSandboxViolationSink.h"
|
2015-07-28 20:19:55 +00:00
|
|
|
#endif
|
2007-07-17 20:29:39 +00:00
|
|
|
|
2014-06-13 17:58:00 +00:00
|
|
|
#include <IOKit/pwr_mgt/IOPMLib.h>
|
|
|
|
#include "nsIDOMWakeLockListener.h"
|
|
|
|
#include "nsIPowerManagerService.h"
|
2009-10-01 04:15:05 +00:00
|
|
|
|
2019-11-05 12:37:02 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
|
2018-11-14 17:03:36 +00:00
|
|
|
using namespace mozilla;
|
2011-02-24 04:48:12 +00:00
|
|
|
using namespace mozilla::widget;
|
|
|
|
|
2017-08-29 07:28:28 +00:00
|
|
|
#define WAKE_LOCK_LOG(...) MOZ_LOG(gMacWakeLockLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
static mozilla::LazyLogModule gMacWakeLockLog("MacWakeLock");
|
|
|
|
|
2014-06-13 17:58:00 +00:00
|
|
|
// A wake lock listener that disables screen saver when requested by
|
|
|
|
// Gecko. For example when we're playing video in a foreground tab we
|
|
|
|
// don't want the screen saver to turn on.
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class MacWakeLockListener final : public nsIDOMMozWakeLockListener {
|
2019-01-21 17:18:16 +00:00
|
|
|
public:
|
2014-06-13 17:58:00 +00:00
|
|
|
NS_DECL_ISUPPORTS;
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
private:
|
2014-07-06 15:25:31 +00:00
|
|
|
~MacWakeLockListener() {}
|
|
|
|
|
2017-08-29 07:25:34 +00:00
|
|
|
IOPMAssertionID mAssertionNoDisplaySleepID = kIOPMNullAssertionID;
|
|
|
|
IOPMAssertionID mAssertionNoIdleSleepID = kIOPMNullAssertionID;
|
2014-06-13 17:58:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
NS_IMETHOD Callback(const nsAString& aTopic, const nsAString& aState) override {
|
2019-01-21 17:18:16 +00:00
|
|
|
if (!aTopic.EqualsASCII("screen") && !aTopic.EqualsASCII("audio-playing") &&
|
2017-08-29 02:54:28 +00:00
|
|
|
!aTopic.EqualsASCII("video-playing")) {
|
2014-06-13 17:58:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-08-29 07:25:34 +00:00
|
|
|
|
2017-08-29 07:28:23 +00:00
|
|
|
// we should still hold the lock for background audio.
|
2019-01-21 17:18:16 +00:00
|
|
|
if (aTopic.EqualsASCII("audio-playing") && aState.EqualsASCII("locked-background")) {
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("keep audio playing even in background");
|
2017-08-29 07:28:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
bool shouldKeepDisplayOn = aTopic.EqualsASCII("screen") || aTopic.EqualsASCII("video-playing");
|
|
|
|
CFStringRef assertionType =
|
|
|
|
shouldKeepDisplayOn ? kIOPMAssertionTypeNoDisplaySleep : kIOPMAssertionTypeNoIdleSleep;
|
|
|
|
IOPMAssertionID& assertionId =
|
|
|
|
shouldKeepDisplayOn ? mAssertionNoDisplaySleepID : mAssertionNoIdleSleepID;
|
2017-08-29 07:25:34 +00:00
|
|
|
|
2014-10-08 01:20:00 +00:00
|
|
|
// Note the wake lock code ensures that we're not sent duplicate
|
|
|
|
// "locked-foreground" notifications when multiple wake locks are held.
|
|
|
|
if (aState.EqualsASCII("locked-foreground")) {
|
2017-08-29 07:28:23 +00:00
|
|
|
if (assertionId != kIOPMNullAssertionID) {
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("already has a lock");
|
2017-08-29 07:28:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-10-08 01:20:00 +00:00
|
|
|
// Prevent screen saver.
|
2019-01-21 17:18:16 +00:00
|
|
|
CFStringRef cf_topic = ::CFStringCreateWithCharacters(
|
|
|
|
kCFAllocatorDefault, reinterpret_cast<const UniChar*>(aTopic.Data()), aTopic.Length());
|
|
|
|
IOReturn success = ::IOPMAssertionCreateWithName(assertionType, kIOPMAssertionLevelOn,
|
|
|
|
cf_topic, &assertionId);
|
2014-10-08 01:20:00 +00:00
|
|
|
CFRelease(cf_topic);
|
|
|
|
if (success != kIOReturnSuccess) {
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("failed to disable screensaver");
|
2014-06-13 17:58:00 +00:00
|
|
|
}
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("create screensaver");
|
2014-06-13 17:58:00 +00:00
|
|
|
} else {
|
2014-10-08 01:20:00 +00:00
|
|
|
// Re-enable screen saver.
|
2017-08-29 07:25:34 +00:00
|
|
|
if (assertionId != kIOPMNullAssertionID) {
|
|
|
|
IOReturn result = ::IOPMAssertionRelease(assertionId);
|
2014-10-08 01:20:00 +00:00
|
|
|
if (result != kIOReturnSuccess) {
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("failed to release screensaver");
|
2014-06-13 17:58:00 +00:00
|
|
|
}
|
2017-08-29 07:28:28 +00:00
|
|
|
WAKE_LOCK_LOG("Release screensaver");
|
2017-08-29 07:28:23 +00:00
|
|
|
assertionId = kIOPMNullAssertionID;
|
2014-06-13 17:58:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2019-01-21 17:18:16 +00:00
|
|
|
}; // MacWakeLockListener
|
2014-06-13 17:58:00 +00:00
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
// defined in nsCocoaWindow.mm
|
2019-01-21 17:18:16 +00:00
|
|
|
extern int32_t gXULModalLevel;
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool gAppShellMethodsSwizzled = false;
|
2009-03-18 22:51:40 +00:00
|
|
|
|
2011-03-03 16:20:02 +00:00
|
|
|
@implementation GeckoNSApplication
|
2011-12-01 17:50:04 +00:00
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)sendEvent:(NSEvent*)anEvent {
|
2018-05-16 17:31:02 +00:00
|
|
|
// Mark this function as non-idle because it's one of the exit points from
|
|
|
|
// the event loop (running inside of -[GeckoNSApplication nextEventMatchingMask:...])
|
|
|
|
// into non-idle code. So we basically unset the IDLE category from the inside.
|
|
|
|
AUTO_PROFILER_LABEL("-[GeckoNSApplication sendEvent:]", OTHER);
|
|
|
|
|
2018-04-30 01:21:20 +00:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
if ([anEvent type] == NSApplicationDefined && [anEvent subtype] == kEventSubtypeTrace) {
|
2011-03-03 16:20:02 +00:00
|
|
|
mozilla::SignalTracerThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[super sendEvent:anEvent];
|
|
|
|
}
|
2011-12-01 17:50:04 +00:00
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
#if defined(MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12 && \
|
2016-11-03 04:18:36 +00:00
|
|
|
__LP64__
|
|
|
|
// 10.12 changed `mask` to NSEventMask (unsigned long long) for x86_64 builds.
|
|
|
|
- (NSEvent*)nextEventMatchingMask:(NSEventMask)mask
|
|
|
|
#else
|
2011-12-01 17:50:04 +00:00
|
|
|
- (NSEvent*)nextEventMatchingMask:(NSUInteger)mask
|
2016-11-03 04:18:36 +00:00
|
|
|
#endif
|
2011-12-01 17:50:04 +00:00
|
|
|
untilDate:(NSDate*)expiration
|
|
|
|
inMode:(NSString*)mode
|
2019-01-21 17:18:16 +00:00
|
|
|
dequeue:(BOOL)flag {
|
2018-05-16 17:31:02 +00:00
|
|
|
// When we're waiting in the event loop, this is the last function under our
|
|
|
|
// control that's on the stack, so this is the function that we mark with the
|
|
|
|
// IDLE category.
|
|
|
|
// However, when we're processing an event or when our CFRunLoopSource runs,
|
|
|
|
// this function is still on the stack - "the event loop calls us". So we
|
|
|
|
// need to mark functions that enter non-idle code with a different profiler
|
|
|
|
// category, usually OTHER. This gives the profiler a rough approximation of
|
|
|
|
// idleness but isn't perfect. For example, sometimes there's some Cocoa-
|
|
|
|
// internal activity that's triggered from the event loop, and we'll
|
|
|
|
// misidentify the stacks for that activity as idle because there's no Gecko
|
|
|
|
// code on the stack that can change the stack's category to something
|
|
|
|
// non-idle.
|
2019-01-21 17:18:16 +00:00
|
|
|
AUTO_PROFILER_LABEL("-[GeckoNSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]",
|
|
|
|
IDLE);
|
2018-05-16 17:31:02 +00:00
|
|
|
|
2011-12-01 17:50:04 +00:00
|
|
|
if (expiration) {
|
2018-04-30 01:21:20 +00:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyWait();
|
2011-12-01 17:50:04 +00:00
|
|
|
}
|
2015-02-12 17:31:42 +00:00
|
|
|
NSEvent* nextEvent = [super nextEventMatchingMask:mask
|
2019-01-21 17:18:16 +00:00
|
|
|
untilDate:expiration
|
|
|
|
inMode:mode
|
|
|
|
dequeue:flag];
|
2015-02-12 17:31:42 +00:00
|
|
|
if (expiration) {
|
2018-04-30 01:21:20 +00:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
2015-02-12 17:31:42 +00:00
|
|
|
}
|
|
|
|
return nextEvent;
|
2011-12-01 17:50:04 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 16:20:02 +00:00
|
|
|
@end
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// AppShellDelegate
|
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// Cocoa bridge class. An object of this class is registered to receive
|
|
|
|
// notifications.
|
2006-05-10 17:30:15 +00:00
|
|
|
//
|
2019-01-21 17:18:16 +00:00
|
|
|
@interface AppShellDelegate : NSObject {
|
|
|
|
@private
|
|
|
|
nsAppShell* mAppShell;
|
2006-06-01 18:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithAppShell:(nsAppShell*)aAppShell;
|
2006-06-15 16:51:51 +00:00
|
|
|
- (void)applicationWillTerminate:(NSNotification*)aNotification;
|
2007-07-17 20:29:39 +00:00
|
|
|
- (void)beginMenuTracking:(NSNotification*)aNotification;
|
2006-05-10 17:30:15 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
// nsAppShell implementation
|
|
|
|
|
2007-03-22 23:04:51 +00:00
|
|
|
NS_IMETHODIMP
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::ResumeNative(void) {
|
2007-03-22 23:04:51 +00:00
|
|
|
nsresult retval = nsBaseAppShell::ResumeNative();
|
2019-01-21 17:18:16 +00:00
|
|
|
if (NS_SUCCEEDED(retval) && (mSuspendNativeCount == 0) && mSkippedNativeCallback) {
|
2011-10-01 00:20:33 +00:00
|
|
|
mSkippedNativeCallback = false;
|
2007-03-22 23:04:51 +00:00
|
|
|
ScheduleNativeEventCallback();
|
2007-04-06 00:16:07 +00:00
|
|
|
}
|
2007-03-22 23:04:51 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsAppShell::nsAppShell()
|
2019-01-21 17:18:16 +00:00
|
|
|
: mAutoreleasePools(nullptr),
|
|
|
|
mDelegate(nullptr),
|
|
|
|
mCFRunLoop(NULL),
|
|
|
|
mCFRunLoopSource(NULL),
|
|
|
|
mRunningEventLoop(false),
|
|
|
|
mStarted(false),
|
|
|
|
mTerminated(false),
|
|
|
|
mSkippedNativeCallback(false),
|
|
|
|
mNativeEventCallbackDepth(0),
|
|
|
|
mNativeEventScheduledDepth(0) {
|
2009-02-02 21:23:42 +00:00
|
|
|
// A Cocoa event loop is running here if (and only if) we've been embedded
|
2014-10-10 15:00:29 +00:00
|
|
|
// by a Cocoa app.
|
2011-10-01 00:20:33 +00:00
|
|
|
mRunningCocoaEmbedded = [NSApp isRunning] ? true : false;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::~nsAppShell() {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2018-08-31 20:29:30 +00:00
|
|
|
hal::Shutdown();
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
if (mCFRunLoop) {
|
|
|
|
if (mCFRunLoopSource) {
|
2019-01-21 17:18:16 +00:00
|
|
|
::CFRunLoopRemoveSource(mCFRunLoop, mCFRunLoopSource, kCFRunLoopCommonModes);
|
2007-09-19 19:17:06 +00:00
|
|
|
::CFRelease(mCFRunLoopSource);
|
|
|
|
}
|
|
|
|
::CFRelease(mCFRunLoop);
|
|
|
|
}
|
|
|
|
|
2006-06-07 00:06:11 +00:00
|
|
|
if (mAutoreleasePools) {
|
|
|
|
NS_ASSERTION(::CFArrayGetCount(mAutoreleasePools) == 0,
|
|
|
|
"nsAppShell destroyed without popping all autorelease pools");
|
|
|
|
::CFRelease(mAutoreleasePools);
|
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
[mDelegate release];
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 17:58:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(MacWakeLockListener, nsIDOMMozWakeLockListener)
|
|
|
|
mozilla::StaticRefPtr<MacWakeLockListener> sWakeLockListener;
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
static void AddScreenWakeLockListener() {
|
|
|
|
nsCOMPtr<nsIPowerManagerService> sPowerManagerService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2014-06-13 17:58:00 +00:00
|
|
|
if (sPowerManagerService) {
|
|
|
|
sWakeLockListener = new MacWakeLockListener();
|
|
|
|
sPowerManagerService->AddWakeLockListener(sWakeLockListener);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to retrieve PowerManagerService, wakelocks will be broken!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
static void RemoveScreenWakeLockListener() {
|
|
|
|
nsCOMPtr<nsIPowerManagerService> sPowerManagerService =
|
|
|
|
do_GetService(POWERMANAGERSERVICE_CONTRACTID);
|
2014-06-13 17:58:00 +00:00
|
|
|
if (sPowerManagerService) {
|
|
|
|
sPowerManagerService->RemoveWakeLockListener(sWakeLockListener);
|
|
|
|
sPowerManagerService = nullptr;
|
|
|
|
sWakeLockListener = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2014-11-11 21:28:18 +00:00
|
|
|
|
|
|
|
// An undocumented CoreGraphics framework method, present in the same form
|
|
|
|
// since at least OS X 10.5.
|
|
|
|
extern "C" CGError CGSSetDebugOptions(int options);
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// Init
|
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// Loads the nib (see bug 316076c21) and sets up the CFRunLoopSource used to
|
|
|
|
// interrupt the main native run loop.
|
2006-05-10 17:30:15 +00:00
|
|
|
//
|
|
|
|
// public
|
2019-01-21 17:18:16 +00:00
|
|
|
nsresult nsAppShell::Init() {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2014-10-10 15:00:29 +00:00
|
|
|
// No event loop is running yet (unless an embedding app that uses
|
|
|
|
// NSApplicationMain() is running).
|
2006-05-10 17:30:15 +00:00
|
|
|
NSAutoreleasePool* localPool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2006-06-07 00:06:11 +00:00
|
|
|
// mAutoreleasePools is used as a stack of NSAutoreleasePool objects created
|
|
|
|
// by |this|. CFArray is used instead of NSArray because NSArray wants to
|
|
|
|
// retain each object you add to it, and you can't retain an
|
|
|
|
// NSAutoreleasePool.
|
2012-07-30 14:20:58 +00:00
|
|
|
mAutoreleasePools = ::CFArrayCreateMutable(nullptr, 0, nullptr);
|
2006-06-07 00:06:11 +00:00
|
|
|
NS_ENSURE_STATE(mAutoreleasePools);
|
|
|
|
|
2019-04-09 16:42:02 +00:00
|
|
|
bool isNSApplicationProcessType = (XRE_GetProcessType() != GeckoProcessType_RDD) &&
|
|
|
|
(XRE_GetProcessType() != GeckoProcessType_Socket);
|
|
|
|
|
|
|
|
if (isNSApplicationProcessType) {
|
2018-11-14 18:07:28 +00:00
|
|
|
// Get the path of the nib file, which lives in the GRE location
|
|
|
|
nsCOMPtr<nsIFile> nibFile;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(nibFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nibFile->AppendNative(NS_LITERAL_CSTRING("res"));
|
|
|
|
nibFile->AppendNative(NS_LITERAL_CSTRING("MainMenu.nib"));
|
|
|
|
|
|
|
|
nsAutoCString nibPath;
|
|
|
|
rv = nibFile->GetNativePath(nibPath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// This call initializes NSApplication unless:
|
|
|
|
// 1) we're using xre -- NSApp's already been initialized by
|
|
|
|
// MacApplicationDelegate.mm's EnsureUseCocoaDockAPI().
|
|
|
|
// 2) an embedding app that uses NSApplicationMain() is running -- NSApp's
|
|
|
|
// already been initialized and its main run loop is already running.
|
2019-01-21 17:18:16 +00:00
|
|
|
[NSBundle loadNibFile:[NSString stringWithUTF8String:(const char*)nibPath.get()]
|
|
|
|
externalNameTable:[NSDictionary dictionaryWithObject:[GeckoNSApplication sharedApplication]
|
|
|
|
forKey:@"NSOwner"]
|
2018-11-14 18:07:28 +00:00
|
|
|
withZone:NSDefaultMallocZone()];
|
|
|
|
}
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2006-06-01 18:35:47 +00:00
|
|
|
mDelegate = [[AppShellDelegate alloc] initWithAppShell:this];
|
2006-05-10 17:30:15 +00:00
|
|
|
NS_ENSURE_STATE(mDelegate);
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
// Add a CFRunLoopSource to the main native run loop. The source is
|
|
|
|
// responsible for interrupting the run loop when Gecko events are ready.
|
|
|
|
|
|
|
|
mCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
|
|
|
|
NS_ENSURE_STATE(mCFRunLoop);
|
|
|
|
::CFRetain(mCFRunLoop);
|
|
|
|
|
|
|
|
CFRunLoopSourceContext context;
|
|
|
|
bzero(&context, sizeof(context));
|
|
|
|
// context.version = 0;
|
|
|
|
context.info = this;
|
|
|
|
context.perform = ProcessGeckoEvents;
|
2017-08-01 16:22:42 +00:00
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
mCFRunLoopSource = ::CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
|
|
|
|
NS_ENSURE_STATE(mCFRunLoopSource);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
::CFRunLoopAddSource(mCFRunLoop, mCFRunLoopSource, kCFRunLoopCommonModes);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2018-08-31 20:29:30 +00:00
|
|
|
hal::Init();
|
|
|
|
|
2017-03-09 11:34:49 +00:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
ScreenManager& screenManager = ScreenManager::GetSingleton();
|
2017-07-03 22:33:16 +00:00
|
|
|
|
|
|
|
if (gfxPlatform::IsHeadless()) {
|
|
|
|
screenManager.SetHelper(mozilla::MakeUnique<HeadlessScreenHelper>());
|
|
|
|
} else {
|
|
|
|
screenManager.SetHelper(mozilla::MakeUnique<ScreenHelperCocoa>());
|
|
|
|
}
|
2017-03-09 11:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:07:28 +00:00
|
|
|
nsresult rv = nsBaseAppShell::Init();
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2019-04-09 16:42:02 +00:00
|
|
|
if (isNSApplicationProcessType && !gAppShellMethodsSwizzled) {
|
2010-10-16 22:22:21 +00:00
|
|
|
// We should only replace the original terminate: method if we're not
|
2014-10-10 15:00:29 +00:00
|
|
|
// running in a Cocoa embedder. See bug 604901.
|
2010-10-16 22:22:21 +00:00
|
|
|
if (!mRunningCocoaEmbedded) {
|
|
|
|
nsToolkit::SwizzleMethods([NSApplication class], @selector(terminate:),
|
|
|
|
@selector(nsAppShell_NSApplication_terminate:));
|
|
|
|
}
|
2011-10-01 00:20:33 +00:00
|
|
|
gAppShellMethodsSwizzled = true;
|
2008-10-17 21:58:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 03:31:00 +00:00
|
|
|
// The bug that this works around was introduced in OS X 10.10.0
|
|
|
|
// and fixed in OS X 10.10.2. Order these version checks so as
|
|
|
|
// few as possible will actually end up running.
|
2019-01-21 17:18:16 +00:00
|
|
|
if (nsCocoaFeatures::OSXVersionMinor() == 10 && nsCocoaFeatures::OSXVersionBugFix() < 2 &&
|
2017-07-07 03:31:00 +00:00
|
|
|
nsCocoaFeatures::OSXVersionMajor() == 10) {
|
2014-11-11 21:28:18 +00:00
|
|
|
// Explicitly turn off CGEvent logging. This works around bug 1092855.
|
|
|
|
// If there are already CGEvents in the log, turning off logging also
|
|
|
|
// causes those events to be written to disk. But at this point no
|
|
|
|
// CGEvents have yet been processed. CGEvents are events (usually
|
|
|
|
// input events) pulled from the WindowServer. An option of 0x80000008
|
|
|
|
// turns on CGEvent logging.
|
|
|
|
CGSSetDebugOptions(0x80000007);
|
|
|
|
}
|
|
|
|
|
2016-10-08 09:14:49 +00:00
|
|
|
#if !defined(RELEASE_OR_BETA) || defined(DEBUG)
|
2016-06-20 18:41:18 +00:00
|
|
|
if (Preferences::GetBool("security.sandbox.mac.track.violations", false)) {
|
2015-07-28 20:19:55 +00:00
|
|
|
nsSandboxViolationSink::Start();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
[localPool release];
|
|
|
|
|
|
|
|
return rv;
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessGeckoEvents
|
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// The "perform" target of mCFRunLoop, called when mCFRunLoopSource is
|
|
|
|
// signalled from ScheduleNativeEventCallback.
|
2006-05-10 17:30:15 +00:00
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// Arrange for Gecko events to be processed on demand (in response to a call
|
|
|
|
// to ScheduleNativeEventCallback(), if processing of Gecko events via "native
|
2008-10-17 21:58:47 +00:00
|
|
|
// methods" hasn't been suspended). This happens in NativeEventCallback().
|
2006-05-10 17:30:15 +00:00
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// protected static
|
2019-01-21 17:18:16 +00:00
|
|
|
void nsAppShell::ProcessGeckoEvents(void* aInfo) {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2018-05-18 21:36:30 +00:00
|
|
|
AUTO_PROFILER_LABEL("nsAppShell::ProcessGeckoEvents", OTHER);
|
2014-05-23 21:12:29 +00:00
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell* self = static_cast<nsAppShell*>(aInfo);
|
2007-09-19 19:17:06 +00:00
|
|
|
|
|
|
|
if (self->mRunningEventLoop) {
|
2011-10-01 00:20:33 +00:00
|
|
|
self->mRunningEventLoop = false;
|
2006-05-26 18:38:19 +00:00
|
|
|
|
2007-09-24 22:31:49 +00:00
|
|
|
// The run loop may be sleeping -- [NSRunLoop runMode:...]
|
2007-09-19 19:17:06 +00:00
|
|
|
// won't return until it's given a reason to wake up. Awaken it by
|
|
|
|
// posting a bogus event. There's no need to make the event
|
|
|
|
// presentable.
|
2007-09-24 14:33:45 +00:00
|
|
|
//
|
|
|
|
// But _don't_ set windowNumber to '-1' -- that can lead to nasty
|
2016-02-17 15:09:39 +00:00
|
|
|
// weirdness like bmo bug 397039 (a crash in [NSApp sendEvent:] on one of
|
2007-09-24 14:33:45 +00:00
|
|
|
// these fake events, because the -1 has gotten changed into the number
|
|
|
|
// of an actual NSWindow object, and that NSWindow object has just been
|
|
|
|
// destroyed). Setting windowNumber to '0' seems to work fine -- this
|
|
|
|
// seems to prevent the OS from ever trying to associate our bogus event
|
|
|
|
// with a particular NSWindow object.
|
2006-05-10 17:30:15 +00:00
|
|
|
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
|
2019-01-21 17:18:16 +00:00
|
|
|
location:NSMakePoint(0, 0)
|
2006-05-10 17:30:15 +00:00
|
|
|
modifierFlags:0
|
|
|
|
timestamp:0
|
2007-09-24 14:33:45 +00:00
|
|
|
windowNumber:0
|
2006-05-10 17:30:15 +00:00
|
|
|
context:NULL
|
2011-03-03 16:20:02 +00:00
|
|
|
subtype:kEventSubtypeNone
|
2006-05-10 17:30:15 +00:00
|
|
|
data1:0
|
|
|
|
data2:0]
|
|
|
|
atStart:NO];
|
|
|
|
}
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
if (self->mSuspendNativeCount <= 0) {
|
2007-09-27 16:23:31 +00:00
|
|
|
++self->mNativeEventCallbackDepth;
|
2011-01-27 23:27:39 +00:00
|
|
|
self->NativeEventCallback();
|
2007-09-27 16:23:31 +00:00
|
|
|
--self->mNativeEventCallbackDepth;
|
2007-04-06 00:16:07 +00:00
|
|
|
} else {
|
2011-10-01 00:20:33 +00:00
|
|
|
self->mSkippedNativeCallback = true;
|
2007-04-06 00:16:07 +00:00
|
|
|
}
|
2007-03-22 23:04:51 +00:00
|
|
|
|
2014-05-10 16:58:07 +00:00
|
|
|
// Still needed to avoid crashes on quit in most Mochitests.
|
2007-01-19 19:21:00 +00:00
|
|
|
[NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined
|
2019-01-21 17:18:16 +00:00
|
|
|
location:NSMakePoint(0, 0)
|
2007-01-19 19:21:00 +00:00
|
|
|
modifierFlags:0
|
|
|
|
timestamp:0
|
2007-09-24 14:33:45 +00:00
|
|
|
windowNumber:0
|
2007-01-19 19:21:00 +00:00
|
|
|
context:NULL
|
2011-03-03 16:20:02 +00:00
|
|
|
subtype:kEventSubtypeNone
|
2007-01-19 19:21:00 +00:00
|
|
|
data1:0
|
|
|
|
data2:0]
|
|
|
|
atStart:NO];
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2009-06-17 15:30:46 +00:00
|
|
|
// Normally every call to ScheduleNativeEventCallback() results in
|
|
|
|
// exactly one call to ProcessGeckoEvents(). So each Release() here
|
|
|
|
// normally balances exactly one AddRef() in ScheduleNativeEventCallback().
|
|
|
|
// But if Exit() is called just after ScheduleNativeEventCallback(), the
|
|
|
|
// corresponding call to ProcessGeckoEvents() will never happen. We check
|
|
|
|
// for this possibility in two different places -- here and in Exit()
|
|
|
|
// itself. If we find here that Exit() has been called (that mTerminated
|
2011-10-01 00:20:33 +00:00
|
|
|
// is true), it's because we've been called recursively, that Exit() was
|
2009-06-17 15:30:46 +00:00
|
|
|
// called from self->NativeEventCallback() above, and that we're unwinding
|
|
|
|
// the recursion. In this case we'll never be called again, and we balance
|
|
|
|
// here any extra calls to ScheduleNativeEventCallback().
|
|
|
|
//
|
|
|
|
// When ProcessGeckoEvents() is called recursively, it's because of a
|
|
|
|
// call to ScheduleNativeEventCallback() from NativeEventCallback(). We
|
|
|
|
// balance the "extra" AddRefs here (rather than always in Exit()) in order
|
|
|
|
// to ensure that 'self' stays alive until the end of this method. We also
|
|
|
|
// make sure not to finish the balancing until all the recursion has been
|
|
|
|
// unwound.
|
|
|
|
if (self->mTerminated) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t releaseCount = 0;
|
2009-06-17 15:30:46 +00:00
|
|
|
if (self->mNativeEventScheduledDepth > self->mNativeEventCallbackDepth) {
|
2019-01-21 17:18:16 +00:00
|
|
|
releaseCount =
|
|
|
|
PR_ATOMIC_SET(&self->mNativeEventScheduledDepth, self->mNativeEventCallbackDepth);
|
2009-06-17 15:30:46 +00:00
|
|
|
}
|
2019-01-21 17:18:16 +00:00
|
|
|
while (releaseCount-- > self->mNativeEventCallbackDepth) self->Release();
|
2009-06-17 15:30:46 +00:00
|
|
|
} else {
|
|
|
|
// As best we can tell, every call to ProcessGeckoEvents() is triggered
|
|
|
|
// by a call to ScheduleNativeEventCallback(). But we've seen a few
|
|
|
|
// (non-reproducible) cases of double-frees that *might* have been caused
|
|
|
|
// by spontaneous calls (from the OS) to ProcessGeckoEvents(). So we
|
|
|
|
// deal with that possibility here.
|
2011-03-28 19:58:49 +00:00
|
|
|
if (PR_ATOMIC_DECREMENT(&self->mNativeEventScheduledDepth) < 0) {
|
|
|
|
PR_ATOMIC_SET(&self->mNativeEventScheduledDepth, 0);
|
2009-06-17 15:30:46 +00:00
|
|
|
NS_WARNING("Spontaneous call to ProcessGeckoEvents()!");
|
|
|
|
} else {
|
|
|
|
self->Release();
|
|
|
|
}
|
|
|
|
}
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2006-06-15 16:51:51 +00:00
|
|
|
// WillTerminate
|
|
|
|
//
|
|
|
|
// Called by the AppShellDelegate when an NSApplicationWillTerminate
|
|
|
|
// notification is posted. After this method is called, native events should
|
2007-09-19 19:17:06 +00:00
|
|
|
// no longer be processed. The NSApplicationWillTerminate notification is
|
|
|
|
// only posted when [NSApp terminate:] is called, which doesn't happen on a
|
|
|
|
// "normal" application quit.
|
2006-06-15 16:51:51 +00:00
|
|
|
//
|
|
|
|
// public
|
2019-01-21 17:18:16 +00:00
|
|
|
void nsAppShell::WillTerminate() {
|
|
|
|
if (mTerminated) return;
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2010-10-15 16:30:07 +00:00
|
|
|
// Make sure that the nsAppExitEvent posted by nsAppStartup::Quit() (called
|
|
|
|
// from [MacApplicationDelegate applicationShouldTerminate:]) gets run.
|
2007-09-19 19:17:06 +00:00
|
|
|
NS_ProcessPendingEvents(NS_GetCurrentThread());
|
|
|
|
|
2011-10-01 00:20:33 +00:00
|
|
|
mTerminated = true;
|
2006-06-15 16:51:51 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// ScheduleNativeEventCallback
|
|
|
|
//
|
|
|
|
// Called (possibly on a non-main thread) when Gecko has an event that
|
|
|
|
// needs to be processed. The Gecko event needs to be processed on the
|
|
|
|
// main thread, so the native run loop must be interrupted.
|
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// In nsBaseAppShell.cpp, the mNativeEventPending variable is used to
|
|
|
|
// ensure that ScheduleNativeEventCallback() is called no more than once
|
|
|
|
// per call to NativeEventCallback(). ProcessGeckoEvents() can skip its
|
|
|
|
// call to NativeEventCallback() if processing of Gecko events by native
|
|
|
|
// means is suspended (using nsIAppShell::SuspendNative()), which will
|
|
|
|
// suspend calls from nsBaseAppShell::OnDispatchedEvent() to
|
|
|
|
// ScheduleNativeEventCallback(). But when Gecko event processing by
|
|
|
|
// native means is resumed (in ResumeNative()), an extra call is made to
|
|
|
|
// ScheduleNativeEventCallback() (from ResumeNative()). This triggers
|
|
|
|
// another call to ProcessGeckoEvents(), which calls NativeEventCallback(),
|
|
|
|
// and nsBaseAppShell::OnDispatchedEvent() resumes calling
|
|
|
|
// ScheduleNativeEventCallback().
|
|
|
|
//
|
2006-05-10 17:30:15 +00:00
|
|
|
// protected virtual
|
2019-01-21 17:18:16 +00:00
|
|
|
void nsAppShell::ScheduleNativeEventCallback() {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
if (mTerminated) return;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2009-06-17 15:30:46 +00:00
|
|
|
// Each AddRef() here is normally balanced by exactly one Release() in
|
|
|
|
// ProcessGeckoEvents(). But there are exceptions, for which see
|
|
|
|
// ProcessGeckoEvents() and Exit().
|
2007-09-19 19:17:06 +00:00
|
|
|
NS_ADDREF_THIS();
|
2011-03-28 19:58:49 +00:00
|
|
|
PR_ATOMIC_INCREMENT(&mNativeEventScheduledDepth);
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
// This will invoke ProcessGeckoEvents on the main thread.
|
|
|
|
::CFRunLoopSourceSignal(mCFRunLoopSource);
|
|
|
|
::CFRunLoopWakeUp(mCFRunLoop);
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-10 16:58:07 +00:00
|
|
|
// Undocumented Cocoa Event Manager function, present in the same form since
|
|
|
|
// at least OS X 10.6.
|
|
|
|
extern "C" EventAttributes GetEventAttributes(EventRef inEvent);
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// ProcessNextNativeEvent
|
|
|
|
//
|
|
|
|
// If aMayWait is false, process a single native event. If it is true, run
|
|
|
|
// the native run loop until stopped by ProcessGeckoEvents.
|
|
|
|
//
|
|
|
|
// Returns true if more events are waiting in the native event queue.
|
|
|
|
//
|
|
|
|
// protected virtual
|
2019-01-21 17:18:16 +00:00
|
|
|
bool nsAppShell::ProcessNextNativeEvent(bool aMayWait) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool moreEvents = false;
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool eventProcessed = false;
|
2007-09-19 19:17:06 +00:00
|
|
|
NSString* currentMode = nil;
|
2006-05-26 18:38:19 +00:00
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
if (mTerminated) return false;
|
2006-06-15 16:51:51 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool wasRunningEventLoop = mRunningEventLoop;
|
2006-05-26 18:38:19 +00:00
|
|
|
mRunningEventLoop = aMayWait;
|
|
|
|
NSDate* waitUntil = nil;
|
2019-01-21 17:18:16 +00:00
|
|
|
if (aMayWait) waitUntil = [NSDate distantFuture];
|
2006-05-26 18:38:19 +00:00
|
|
|
|
2007-09-24 22:31:49 +00:00
|
|
|
NSRunLoop* currentRunLoop = [NSRunLoop currentRunLoop];
|
|
|
|
|
2014-05-10 16:58:07 +00:00
|
|
|
EventQueueRef currentEventQueue = GetCurrentEventQueue();
|
|
|
|
EventTargetRef eventDispatcherTarget = GetEventDispatcherTarget();
|
|
|
|
|
|
|
|
if (aMayWait) {
|
2018-04-30 01:21:20 +00:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyWait();
|
2014-05-10 16:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only call -[NSApp sendEvent:] (and indirectly send user-input events to
|
|
|
|
// Gecko) if aMayWait is true. Tbis ensures most calls to -[NSApp
|
|
|
|
// sendEvent:] happen under nsAppShell::Run(), at the lowest level of
|
|
|
|
// recursion -- thereby making it less likely Gecko will process user-input
|
|
|
|
// events in the wrong order or skip some of them. It also avoids eating
|
|
|
|
// too much CPU in nsBaseAppShell::OnProcessNextEvent() (which calls
|
|
|
|
// us) -- thereby avoiding the starvation of nsIRunnable events in
|
|
|
|
// nsThread::ProcessNextEvent(). For more information see bug 996848.
|
2006-05-26 18:38:19 +00:00
|
|
|
do {
|
2006-06-07 00:06:11 +00:00
|
|
|
// No autorelease pool is provided here, because OnProcessNextEvent
|
|
|
|
// and AfterProcessNextEvent are responsible for maintaining it.
|
|
|
|
NS_ASSERTION(mAutoreleasePools && ::CFArrayGetCount(mAutoreleasePools),
|
|
|
|
"No autorelease pool for native event");
|
2006-05-26 18:38:19 +00:00
|
|
|
|
2011-11-11 21:13:36 +00:00
|
|
|
if (aMayWait) {
|
2014-05-10 16:58:07 +00:00
|
|
|
currentMode = [currentRunLoop currentMode];
|
2019-01-21 17:18:16 +00:00
|
|
|
if (!currentMode) currentMode = NSDefaultRunLoopMode;
|
|
|
|
NSEvent* nextEvent = [NSApp nextEventMatchingMask:NSAnyEventMask
|
2014-05-10 16:58:07 +00:00
|
|
|
untilDate:waitUntil
|
|
|
|
inMode:currentMode
|
|
|
|
dequeue:YES];
|
|
|
|
if (nextEvent) {
|
2018-04-30 01:21:20 +00:00
|
|
|
mozilla::BackgroundHangMonitor().NotifyActivity();
|
2014-05-10 16:58:07 +00:00
|
|
|
[NSApp sendEvent:nextEvent];
|
2011-10-01 00:20:33 +00:00
|
|
|
eventProcessed = true;
|
2007-09-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-10 16:58:07 +00:00
|
|
|
// AcquireFirstMatchingEventInQueue() doesn't spin the (native) event
|
|
|
|
// loop, though it does queue up any newly available events from the
|
|
|
|
// window server.
|
2019-01-21 17:18:16 +00:00
|
|
|
EventRef currentEvent =
|
|
|
|
AcquireFirstMatchingEventInQueue(currentEventQueue, 0, NULL, kEventQueueOptionsNone);
|
2014-05-10 16:58:07 +00:00
|
|
|
if (!currentEvent) {
|
|
|
|
continue;
|
2014-05-07 16:13:27 +00:00
|
|
|
}
|
2014-05-10 16:58:07 +00:00
|
|
|
EventAttributes attrs = GetEventAttributes(currentEvent);
|
2014-06-02 21:43:31 +00:00
|
|
|
UInt32 eventKind = GetEventKind(currentEvent);
|
2014-06-30 17:28:45 +00:00
|
|
|
UInt32 eventClass = GetEventClass(currentEvent);
|
2019-01-21 17:18:16 +00:00
|
|
|
bool osCocoaEvent = ((eventClass == 'appl') || (eventClass == kEventClassAppleEvent) ||
|
|
|
|
((eventClass == 'cgs ') && (eventKind != NSApplicationDefined)));
|
2014-05-10 16:58:07 +00:00
|
|
|
// If attrs is kEventAttributeUserEvent or kEventAttributeMonitored
|
|
|
|
// (i.e. a user input event), we shouldn't process it here while
|
2014-06-02 21:43:31 +00:00
|
|
|
// aMayWait is false. Likewise if currentEvent will eventually be
|
2014-10-31 22:14:44 +00:00
|
|
|
// turned into an OS-defined Cocoa event, or otherwise needs AppKit
|
|
|
|
// processing. Doing otherwise risks doing too much work here, and
|
|
|
|
// preventing the event from being properly processed by the AppKit
|
|
|
|
// framework.
|
2014-06-02 21:43:31 +00:00
|
|
|
if ((attrs != kEventAttributeNone) || osCocoaEvent) {
|
2014-05-10 16:58:07 +00:00
|
|
|
// Since we can't process the next event here (while aMayWait is false),
|
|
|
|
// we want moreEvents to be false on return.
|
|
|
|
eventProcessed = false;
|
|
|
|
// This call to ReleaseEvent() matches a call to RetainEvent() in
|
|
|
|
// AcquireFirstMatchingEventInQueue() above.
|
|
|
|
ReleaseEvent(currentEvent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// This call to RetainEvent() matches a call to ReleaseEvent() in
|
|
|
|
// RemoveEventFromQueue() below.
|
|
|
|
RetainEvent(currentEvent);
|
|
|
|
RemoveEventFromQueue(currentEventQueue, currentEvent);
|
|
|
|
SendEventToEventTarget(currentEvent, eventDispatcherTarget);
|
|
|
|
// This call to ReleaseEvent() matches a call to RetainEvent() in
|
|
|
|
// AcquireFirstMatchingEventInQueue() above.
|
|
|
|
ReleaseEvent(currentEvent);
|
|
|
|
eventProcessed = true;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
2006-05-26 18:38:19 +00:00
|
|
|
} while (mRunningEventLoop);
|
|
|
|
|
2014-05-10 16:58:07 +00:00
|
|
|
if (eventProcessed) {
|
2019-01-21 17:18:16 +00:00
|
|
|
moreEvents = (AcquireFirstMatchingEventInQueue(currentEventQueue, 0, NULL,
|
|
|
|
kEventQueueOptionsNone) != NULL);
|
2007-09-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
2006-05-26 18:38:19 +00:00
|
|
|
mRunningEventLoop = wasRunningEventLoop;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
|
2009-04-21 23:53:52 +00:00
|
|
|
if (!moreEvents) {
|
|
|
|
nsChildView::UpdateCurrentInputEventCount();
|
|
|
|
}
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
return moreEvents;
|
2006-05-10 17:30:15 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 18:35:47 +00:00
|
|
|
// Run
|
|
|
|
//
|
2007-09-19 19:17:06 +00:00
|
|
|
// Overrides the base class's Run() method to call [NSApp run] (which spins
|
|
|
|
// the native run loop until the application quits). Since (unlike the base
|
|
|
|
// class's Run() method) we don't process any Gecko events here, they need
|
|
|
|
// to be processed elsewhere (in NativeEventCallback(), called from
|
|
|
|
// ProcessGeckoEvents()).
|
2006-06-01 18:35:47 +00:00
|
|
|
//
|
2014-10-10 15:00:29 +00:00
|
|
|
// Camino called [NSApp run] on its own (via NSApplicationMain()), and so
|
|
|
|
// didn't call nsAppShell::Run().
|
2006-06-01 18:35:47 +00:00
|
|
|
//
|
|
|
|
// public
|
|
|
|
NS_IMETHODIMP
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::Run(void) {
|
2007-09-19 19:17:06 +00:00
|
|
|
NS_ASSERTION(!mStarted, "nsAppShell::Run() called multiple times");
|
2019-01-21 17:18:16 +00:00
|
|
|
if (mStarted || mTerminated) return NS_OK;
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2011-10-01 00:20:33 +00:00
|
|
|
mStarted = true;
|
2014-06-13 17:58:00 +00:00
|
|
|
|
2017-08-01 16:22:42 +00:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
AddScreenWakeLockListener();
|
|
|
|
}
|
2014-06-13 17:58:00 +00:00
|
|
|
|
2017-07-21 23:16:59 +00:00
|
|
|
// We use the native Gecko event loop in content processes.
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (XRE_UseNativeEventProcessing()) {
|
|
|
|
NS_OBJC_TRY_ABORT([NSApp run]);
|
|
|
|
} else {
|
|
|
|
rv = nsBaseAppShell::Run();
|
|
|
|
}
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2017-08-01 16:22:42 +00:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
RemoveScreenWakeLockListener();
|
|
|
|
}
|
2014-06-13 17:58:00 +00:00
|
|
|
|
2017-07-21 23:16:59 +00:00
|
|
|
return rv;
|
2007-09-19 19:17:06 +00:00
|
|
|
}
|
2006-06-01 18:35:47 +00:00
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
NS_IMETHODIMP
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::Exit(void) {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
// This method is currently called more than once -- from (according to
|
|
|
|
// mento) an nsAppExitEvent dispatched by nsAppStartup::Quit() and from an
|
|
|
|
// XPCOM shutdown notification that nsBaseAppShell has registered to
|
|
|
|
// receive. So we need to ensure that multiple calls won't break anything.
|
|
|
|
// But we should also complain about it (since it isn't quite kosher).
|
2007-09-22 13:52:14 +00:00
|
|
|
if (mTerminated) {
|
|
|
|
NS_WARNING("nsAppShell::Exit() called redundantly");
|
2007-09-19 19:17:06 +00:00
|
|
|
return NS_OK;
|
2007-09-22 13:52:14 +00:00
|
|
|
}
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2011-10-01 00:20:33 +00:00
|
|
|
mTerminated = true;
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2016-10-08 09:14:49 +00:00
|
|
|
#if !defined(RELEASE_OR_BETA) || defined(DEBUG)
|
2016-06-20 18:41:18 +00:00
|
|
|
nsSandboxViolationSink::Stop();
|
2015-07-28 20:19:55 +00:00
|
|
|
#endif
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
// Quoting from Apple's doc on the [NSApplication stop:] method (from their
|
|
|
|
// doc on the NSApplication class): "If this method is invoked during a
|
|
|
|
// modal event loop, it will break that loop but not the main event loop."
|
|
|
|
// nsAppShell::Exit() shouldn't be called from a modal event loop. So if
|
|
|
|
// it is we complain about it (to users of debug builds) and call [NSApp
|
|
|
|
// stop:] one extra time. (I'm not sure if modal event loops can be nested
|
|
|
|
// -- Apple's docs don't say one way or the other. But the return value
|
|
|
|
// of [NSApp _isRunningModal] doesn't change immediately after a call to
|
|
|
|
// [NSApp stop:], so we have to assume that one extra call to [NSApp stop:]
|
|
|
|
// will do the job.)
|
|
|
|
BOOL cocoaModal = [NSApp _isRunningModal];
|
2019-01-21 17:18:16 +00:00
|
|
|
NS_ASSERTION(!cocoaModal, "Don't call nsAppShell::Exit() from a modal event loop!");
|
|
|
|
if (cocoaModal) [NSApp stop:nullptr];
|
2012-07-30 14:20:58 +00:00
|
|
|
[NSApp stop:nullptr];
|
2007-09-19 19:17:06 +00:00
|
|
|
|
2009-06-17 15:30:46 +00:00
|
|
|
// A call to Exit() just after a call to ScheduleNativeEventCallback()
|
|
|
|
// prevents the (normally) matching call to ProcessGeckoEvents() from
|
|
|
|
// happening. If we've been called from ProcessGeckoEvents() (as usually
|
|
|
|
// happens), we take care of it there. But if we have an unbalanced call
|
|
|
|
// to ScheduleNativeEventCallback() and ProcessGeckoEvents() isn't on the
|
|
|
|
// stack, we need to take care of the problem here.
|
|
|
|
if (!mNativeEventCallbackDepth && mNativeEventScheduledDepth) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t releaseCount = PR_ATOMIC_SET(&mNativeEventScheduledDepth, 0);
|
2019-01-21 17:18:16 +00:00
|
|
|
while (releaseCount-- > 0) NS_RELEASE_THIS();
|
2009-06-17 15:30:46 +00:00
|
|
|
}
|
|
|
|
|
2007-09-19 19:17:06 +00:00
|
|
|
return nsBaseAppShell::Exit();
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2006-06-01 18:35:47 +00:00
|
|
|
}
|
|
|
|
|
2006-06-07 00:06:11 +00:00
|
|
|
// OnProcessNextEvent
|
|
|
|
//
|
|
|
|
// This nsIThreadObserver method is called prior to processing an event.
|
|
|
|
// Set up an autorelease pool that will service any autoreleased Cocoa
|
|
|
|
// objects during this event. This includes native events processed by
|
|
|
|
// ProcessNextNativeEvent. The autorelease pool will be popped by
|
|
|
|
// AfterProcessNextEvent, it is important for these two methods to be
|
|
|
|
// tightly coupled.
|
|
|
|
//
|
|
|
|
// public
|
|
|
|
NS_IMETHODIMP
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::OnProcessNextEvent(nsIThreadInternal* aThread, bool aMayWait) {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
NS_ASSERTION(mAutoreleasePools, "No stack on which to store autorelease pool");
|
2006-06-07 00:06:11 +00:00
|
|
|
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
::CFArrayAppendValue(mAutoreleasePools, pool);
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 13:10:46 +00:00
|
|
|
return nsBaseAppShell::OnProcessNextEvent(aThread, aMayWait);
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2006-06-07 00:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterProcessNextEvent
|
|
|
|
//
|
|
|
|
// This nsIThreadObserver method is called after event processing is complete.
|
|
|
|
// The Cocoa implementation cleans up the autorelease pool create by the
|
|
|
|
// previous OnProcessNextEvent call.
|
|
|
|
//
|
|
|
|
// public
|
|
|
|
NS_IMETHODIMP
|
2019-01-21 17:18:16 +00:00
|
|
|
nsAppShell::AfterProcessNextEvent(nsIThreadInternal* aThread, bool aEventWasProcessed) {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2006-06-07 00:06:11 +00:00
|
|
|
CFIndex count = ::CFArrayGetCount(mAutoreleasePools);
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
NS_ASSERTION(mAutoreleasePools && count, "Processed an event, but there's no autorelease pool?");
|
2006-06-07 00:06:11 +00:00
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
const NSAutoreleasePool* pool =
|
|
|
|
static_cast<const NSAutoreleasePool*>(::CFArrayGetValueAtIndex(mAutoreleasePools, count - 1));
|
2006-06-07 00:06:11 +00:00
|
|
|
::CFArrayRemoveValueAtIndex(mAutoreleasePools, count - 1);
|
|
|
|
[pool release];
|
|
|
|
|
Bug 1179909: Refactor stable state handling. r=smaug
This is motivated by three separate but related problems:
1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1).
2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this).
3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread.
The solve this, this patch does the following:
1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime.
2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs.
3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with.
4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways:
a) It fires between microtasks, which was the motivation for starting this.
b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this.
5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.
2015-08-11 13:10:46 +00:00
|
|
|
return nsBaseAppShell::AfterProcessNextEvent(aThread, aEventWasProcessed);
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2006-06-07 00:06:11 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// AppShellDelegate implementation
|
|
|
|
|
|
|
|
@implementation AppShellDelegate
|
2006-06-01 18:35:47 +00:00
|
|
|
// initWithAppShell:
|
|
|
|
//
|
|
|
|
// Constructs the AppShellDelegate object
|
2019-01-21 17:18:16 +00:00
|
|
|
- (id)initWithAppShell:(nsAppShell*)aAppShell {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2006-06-01 18:35:47 +00:00
|
|
|
if ((self = [self init])) {
|
|
|
|
mAppShell = aAppShell;
|
2006-06-15 16:51:51 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationWillTerminate:)
|
|
|
|
name:NSApplicationWillTerminateNotification
|
|
|
|
object:NSApp];
|
2008-12-16 16:50:19 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(applicationDidBecomeActive:)
|
|
|
|
name:NSApplicationDidBecomeActiveNotification
|
|
|
|
object:NSApp];
|
2019-01-21 17:18:16 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter]
|
|
|
|
addObserver:self
|
|
|
|
selector:@selector(beginMenuTracking:)
|
|
|
|
name:@"com.apple.HIToolbox.beginMenuTrackingNotification"
|
|
|
|
object:nil];
|
2006-06-01 18:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2006-06-01 18:35:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)dealloc {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2006-06-15 16:51:51 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2007-07-17 20:29:39 +00:00
|
|
|
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
|
2006-06-15 16:51:51 +00:00
|
|
|
[super dealloc];
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2006-06-15 16:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// applicationWillTerminate:
|
|
|
|
//
|
|
|
|
// Notify the nsAppShell that native event processing should be discontinued.
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)applicationWillTerminate:(NSNotification*)aNotification {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2006-06-15 16:51:51 +00:00
|
|
|
mAppShell->WillTerminate();
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2006-06-15 16:51:51 +00:00
|
|
|
}
|
2007-07-17 20:29:39 +00:00
|
|
|
|
2008-12-16 16:50:19 +00:00
|
|
|
// applicationDidBecomeActive
|
|
|
|
//
|
2011-07-21 00:33:16 +00:00
|
|
|
// Make sure TextInputHandler::sLastModifierState is updated when we become
|
|
|
|
// active (since we won't have received [ChildView flagsChanged:] messages
|
|
|
|
// while inactive).
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)applicationDidBecomeActive:(NSNotification*)aNotification {
|
2008-12-16 16:50:19 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
// [NSEvent modifierFlags] is valid on every kind of event, so we don't need
|
|
|
|
// to worry about getting an NSInternalInconsistencyException here.
|
|
|
|
NSEvent* currentEvent = [NSApp currentEvent];
|
|
|
|
if (currentEvent) {
|
2011-07-21 00:33:16 +00:00
|
|
|
TextInputHandler::sLastModifierState =
|
2019-01-21 17:18:16 +00:00
|
|
|
[currentEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
2008-12-16 16:50:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:37:02 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(nullptr, NS_WIDGET_MAC_APP_ACTIVATE_OBSERVER_TOPIC, nullptr);
|
|
|
|
}
|
|
|
|
|
2008-12-16 16:50:19 +00:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2007-07-17 20:29:39 +00:00
|
|
|
// beginMenuTracking
|
|
|
|
//
|
|
|
|
// Roll up our context menu (if any) when some other app (or the OS) opens
|
|
|
|
// any sort of menu. But make sure we don't do this for notifications we
|
|
|
|
// send ourselves (whose 'sender' will be @"org.mozilla.gecko.PopupWindow").
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)beginMenuTracking:(NSNotification*)aNotification {
|
2008-02-13 23:11:11 +00:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2019-01-21 17:18:16 +00:00
|
|
|
NSString* sender = [aNotification object];
|
2007-07-17 20:29:39 +00:00
|
|
|
if (!sender || ![sender isEqualToString:@"org.mozilla.gecko.PopupWindow"]) {
|
2012-10-26 13:15:22 +00:00
|
|
|
nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
|
|
|
|
nsCOMPtr<nsIWidget> rollupWidget = rollupListener->GetRollupWidget();
|
2019-01-21 17:18:16 +00:00
|
|
|
if (rollupWidget) rollupListener->Rollup(0, true, nullptr, nullptr);
|
2007-07-17 20:29:39 +00:00
|
|
|
}
|
2008-02-13 23:11:11 +00:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-07-17 20:29:39 +00:00
|
|
|
}
|
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
@end
|
2008-10-17 21:58:47 +00:00
|
|
|
|
2010-10-15 16:30:07 +00:00
|
|
|
// We hook terminate: in order to make OS-initiated termination work nicely
|
|
|
|
// with Gecko's shutdown sequence. (Two ways to trigger OS-initiated
|
|
|
|
// termination: 1) Quit from the Dock menu; 2) Log out from (or shut down)
|
|
|
|
// your computer while the browser is active.)
|
2008-10-17 21:58:47 +00:00
|
|
|
@interface NSApplication (MethodSwizzling)
|
2010-10-16 22:22:21 +00:00
|
|
|
- (void)nsAppShell_NSApplication_terminate:(id)sender;
|
2008-10-17 21:58:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSApplication (MethodSwizzling)
|
|
|
|
|
2010-10-15 16:30:07 +00:00
|
|
|
// Called by the OS after [MacApplicationDelegate applicationShouldTerminate:]
|
|
|
|
// has returned NSTerminateNow. This method "subclasses" and replaces the
|
|
|
|
// OS's original implementation. The only thing the orginal method does which
|
|
|
|
// we need is that it posts NSApplicationWillTerminateNotification. Everything
|
|
|
|
// else is unneeded (because it's handled elsewhere), or actively interferes
|
|
|
|
// with Gecko's shutdown sequence. For example the original terminate: method
|
|
|
|
// causes the app to exit() inside [NSApp run] (called from nsAppShell::Run()
|
|
|
|
// above), which means that nothing runs after the call to nsAppStartup::Run()
|
|
|
|
// in XRE_Main(), which in particular means that ScopedXPCOMStartup's destructor
|
|
|
|
// and NS_ShutdownXPCOM() never get called.
|
2019-01-21 17:18:16 +00:00
|
|
|
- (void)nsAppShell_NSApplication_terminate:(id)sender {
|
2010-10-15 16:30:07 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:NSApplicationWillTerminateNotification
|
|
|
|
object:NSApp];
|
|
|
|
}
|
|
|
|
|
2008-10-17 21:58:47 +00:00
|
|
|
@end
|