Infrastructure for catching Obj-C exceptions. Cocoa appshell usage. b=163260 r=shebs sr=roc

This commit is contained in:
joshmoz%gmail.com 2008-02-13 23:11:11 +00:00
Родитель cdacd44de0
Коммит c1783c02ea
3 изменённых файлов: 189 добавлений и 2 удалений

Просмотреть файл

@ -54,6 +54,7 @@
#include "nsServiceManagerUtils.h" #include "nsServiceManagerUtils.h"
#include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestor.h"
#include "nsIWebBrowserChrome.h" #include "nsIWebBrowserChrome.h"
#include "nsObjCExceptions.h"
// defined in nsChildView.mm // defined in nsChildView.mm
extern nsIRollupListener * gRollupListener; extern nsIRollupListener * gRollupListener;
@ -125,6 +126,8 @@ nsAppShell::nsAppShell()
nsAppShell::~nsAppShell() nsAppShell::~nsAppShell()
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
if (mCFRunLoop) { if (mCFRunLoop) {
if (mCFRunLoopSource) { if (mCFRunLoopSource) {
::CFRunLoopRemoveSource(mCFRunLoop, mCFRunLoopSource, ::CFRunLoopRemoveSource(mCFRunLoop, mCFRunLoopSource,
@ -163,6 +166,8 @@ nsAppShell::~nsAppShell()
// on the current thread, which is the main thread). // on the current thread, which is the main thread).
if (!mNotifiedWillTerminate) if (!mNotifiedWillTerminate)
[mMainPool release]; [mMainPool release];
NS_OBJC_END_TRY_ABORT_BLOCK
} }
// Init // Init
@ -174,6 +179,8 @@ nsAppShell::~nsAppShell()
nsresult nsresult
nsAppShell::Init() nsAppShell::Init()
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// No event loop is running yet (unless Camino is running, or another // No event loop is running yet (unless Camino is running, or another
// embedding app that uses NSApplicationMain()). Avoid autoreleasing // embedding app that uses NSApplicationMain()). Avoid autoreleasing
// objects to mMainPool. The appshell retains objects it needs to be // objects to mMainPool. The appshell retains objects it needs to be
@ -238,6 +245,8 @@ nsAppShell::Init()
[localPool release]; [localPool release];
return rv; return rv;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
} }
// ProcessGeckoEvents // ProcessGeckoEvents
@ -260,6 +269,8 @@ nsAppShell::Init()
void void
nsAppShell::ProcessGeckoEvents(void* aInfo) nsAppShell::ProcessGeckoEvents(void* aInfo)
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
nsAppShell* self = static_cast<nsAppShell*> (aInfo); nsAppShell* self = static_cast<nsAppShell*> (aInfo);
if (self->mRunningEventLoop) { if (self->mRunningEventLoop) {
@ -313,6 +324,8 @@ nsAppShell::ProcessGeckoEvents(void* aInfo)
// Each Release() here is balanced by exactly one AddRef() in // Each Release() here is balanced by exactly one AddRef() in
// ScheduleNativeEventCallback(). // ScheduleNativeEventCallback().
NS_RELEASE(self); NS_RELEASE(self);
NS_OBJC_END_TRY_ABORT_BLOCK;
} }
// WillTerminate // WillTerminate
@ -366,6 +379,8 @@ nsAppShell::WillTerminate()
void void
nsAppShell::ScheduleNativeEventCallback() nsAppShell::ScheduleNativeEventCallback()
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
if (mTerminated) if (mTerminated)
return; return;
@ -376,6 +391,8 @@ nsAppShell::ScheduleNativeEventCallback()
// This will invoke ProcessGeckoEvents on the main thread. // This will invoke ProcessGeckoEvents on the main thread.
::CFRunLoopSourceSignal(mCFRunLoopSource); ::CFRunLoopSourceSignal(mCFRunLoopSource);
::CFRunLoopWakeUp(mCFRunLoop); ::CFRunLoopWakeUp(mCFRunLoop);
NS_OBJC_END_TRY_ABORT_BLOCK;
} }
// ProcessNextNativeEvent // ProcessNextNativeEvent
@ -395,6 +412,9 @@ PRBool
nsAppShell::ProcessNextNativeEvent(PRBool aMayWait) nsAppShell::ProcessNextNativeEvent(PRBool aMayWait)
{ {
PRBool moreEvents = PR_FALSE; PRBool moreEvents = PR_FALSE;
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
PRBool eventProcessed = PR_FALSE; PRBool eventProcessed = PR_FALSE;
NSString* currentMode = nil; NSString* currentMode = nil;
@ -510,6 +530,8 @@ nsAppShell::ProcessNextNativeEvent(PRBool aMayWait)
mRunningEventLoop = wasRunningEventLoop; mRunningEventLoop = wasRunningEventLoop;
NS_OBJC_END_TRY_ABORT_BLOCK;
return moreEvents; return moreEvents;
} }
@ -562,7 +584,7 @@ nsAppShell::Run(void)
return NS_OK; return NS_OK;
mStarted = PR_TRUE; mStarted = PR_TRUE;
[NSApp run]; NS_OBJC_TRY_ABORT([NSApp run]);
return NS_OK; return NS_OK;
} }
@ -570,6 +592,8 @@ nsAppShell::Run(void)
NS_IMETHODIMP NS_IMETHODIMP
nsAppShell::Exit(void) nsAppShell::Exit(void)
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// This method is currently called more than once -- from (according to // This method is currently called more than once -- from (according to
// mento) an nsAppExitEvent dispatched by nsAppStartup::Quit() and from an // mento) an nsAppExitEvent dispatched by nsAppStartup::Quit() and from an
// XPCOM shutdown notification that nsBaseAppShell has registered to // XPCOM shutdown notification that nsBaseAppShell has registered to
@ -600,6 +624,8 @@ nsAppShell::Exit(void)
[NSApp stop:nsnull]; [NSApp stop:nsnull];
return nsBaseAppShell::Exit(); return nsBaseAppShell::Exit();
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
} }
// OnProcessNextEvent // OnProcessNextEvent
@ -616,6 +642,8 @@ NS_IMETHODIMP
nsAppShell::OnProcessNextEvent(nsIThreadInternal *aThread, PRBool aMayWait, nsAppShell::OnProcessNextEvent(nsIThreadInternal *aThread, PRBool aMayWait,
PRUint32 aRecursionDepth) PRUint32 aRecursionDepth)
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
mRecursionDepth = aRecursionDepth; mRecursionDepth = aRecursionDepth;
NS_ASSERTION(mAutoreleasePools, NS_ASSERTION(mAutoreleasePools,
@ -625,6 +653,8 @@ nsAppShell::OnProcessNextEvent(nsIThreadInternal *aThread, PRBool aMayWait,
::CFArrayAppendValue(mAutoreleasePools, pool); ::CFArrayAppendValue(mAutoreleasePools, pool);
return nsBaseAppShell::OnProcessNextEvent(aThread, aMayWait, aRecursionDepth); return nsBaseAppShell::OnProcessNextEvent(aThread, aMayWait, aRecursionDepth);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
} }
// AfterProcessNextEvent // AfterProcessNextEvent
@ -638,6 +668,8 @@ NS_IMETHODIMP
nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread, nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
PRUint32 aRecursionDepth) PRUint32 aRecursionDepth)
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
mRecursionDepth = aRecursionDepth; mRecursionDepth = aRecursionDepth;
CFIndex count = ::CFArrayGetCount(mAutoreleasePools); CFIndex count = ::CFArrayGetCount(mAutoreleasePools);
@ -651,6 +683,8 @@ nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
[pool release]; [pool release];
return nsBaseAppShell::AfterProcessNextEvent(aThread, aRecursionDepth); return nsBaseAppShell::AfterProcessNextEvent(aThread, aRecursionDepth);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
} }
// AppShellDelegate implementation // AppShellDelegate implementation
@ -661,6 +695,8 @@ nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
// Constructs the AppShellDelegate object // Constructs the AppShellDelegate object
- (id)initWithAppShell:(nsAppShell*)aAppShell - (id)initWithAppShell:(nsAppShell*)aAppShell
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
if ((self = [self init])) { if ((self = [self init])) {
mAppShell = aAppShell; mAppShell = aAppShell;
@ -675,13 +711,19 @@ nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
} }
return self; return self;
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
} }
- (void)dealloc - (void)dealloc
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[super dealloc]; [super dealloc];
NS_OBJC_END_TRY_ABORT_BLOCK;
} }
// applicationWillTerminate: // applicationWillTerminate:
@ -689,7 +731,11 @@ nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
// Notify the nsAppShell that native event processing should be discontinued. // Notify the nsAppShell that native event processing should be discontinued.
- (void)applicationWillTerminate:(NSNotification*)aNotification - (void)applicationWillTerminate:(NSNotification*)aNotification
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
mAppShell->WillTerminate(); mAppShell->WillTerminate();
NS_OBJC_END_TRY_ABORT_BLOCK;
} }
// beginMenuTracking // beginMenuTracking
@ -699,12 +745,15 @@ nsAppShell::AfterProcessNextEvent(nsIThreadInternal *aThread,
// send ourselves (whose 'sender' will be @"org.mozilla.gecko.PopupWindow"). // send ourselves (whose 'sender' will be @"org.mozilla.gecko.PopupWindow").
- (void)beginMenuTracking:(NSNotification*)aNotification - (void)beginMenuTracking:(NSNotification*)aNotification
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
NSString *sender = [aNotification object]; NSString *sender = [aNotification object];
if (!sender || ![sender isEqualToString:@"org.mozilla.gecko.PopupWindow"]) { if (!sender || ![sender isEqualToString:@"org.mozilla.gecko.PopupWindow"]) {
if (gRollupListener && gRollupWidget) if (gRollupListener && gRollupWidget)
gRollupListener->Rollup(nsnull); gRollupListener->Rollup(nsnull);
} }
NS_OBJC_END_TRY_ABORT_BLOCK;
} }
@end @end

Просмотреть файл

@ -114,6 +114,7 @@ SDK_HEADERS = \
nsISupportsBase.h \ nsISupportsBase.h \
nscore.h \ nscore.h \
nsCycleCollector.h \ nsCycleCollector.h \
nsObjCExceptions.h \
XPIDLSRCS = \ XPIDLSRCS = \
nsIConsoleListener.idl \ nsIConsoleListener.idl \

Просмотреть файл

@ -0,0 +1,137 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
* Robert O'Callahan <robert@ocallahan.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsObjCExceptions_h_
#define nsObjCExceptions_h_
#import <Foundation/NSException.h>
#import <Foundation/NSObjCRuntime.h>
#include <unistd.h>
#include <signal.h>
#include "nsError.h"
// See Mozilla bug 163260.
// This file can only be included in an Objective-C context.
static void nsObjCExceptionLog(NSException *e)
{
NSLog(@"%@: %@", [e name], [e reason]);
}
static void nsObjCExceptionAbort()
{
// We need to raise a mach-o signal here, the Mozilla crash reporter on
// Mac OS X does not respond to POSIX signals. Raising mach-o signals directly
// is tricky so we do it by just derefing a null pointer.
int* foo = NULL;
*foo = 1;
}
static void nsObjCExceptionLogAbort(NSException *e)
{
nsObjCExceptionLog(e);
nsObjCExceptionAbort();
}
#define NS_OBJC_TRY(_e, _fail) \
@try { _e; } \
@catch(NSException *_exn) { \
nsObjCExceptionLog(_exn); \
_fail; \
}
#define NS_OBJC_TRY_EXPR(_e, _fail) \
({ \
typeof(_e) _tmp; \
@try { _tmp = (_e); } \
@catch(NSException *_exn) { \
nsObjCExceptionLog(_exn); \
_fail; \
} \
_tmp; \
})
#define NS_OBJC_TRY_EXPR_NULL(_e) \
NS_OBJC_TRY_EXPR(_e, 0)
#define NS_OBJC_TRY_IGNORE(_e) \
NS_OBJC_TRY(_e, )
// To reduce code size the abort versions do not reuse above macros. This
// allows catch blocks to only contain one call.
#define NS_OBJC_TRY_ABORT(_e) \
@try { _e; } \
@catch(NSException *_exn) { \
nsObjCExceptionLogAbort(_exn); \
}
#define NS_OBJC_TRY_EXPR_ABORT(_e) \
({ \
typeof(_e) _tmp; \
@try { _tmp = (_e); } \
@catch(NSException *_exn) { \
nsObjCExceptionLogAbort(_exn); \
} \
_tmp; \
})
// For wrapping blocks of Obj-C calls. Terminates app after logging.
#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK @try {
#define NS_OBJC_END_TRY_ABORT_BLOCK } @catch(NSException *_exn) { \
nsObjCExceptionLogAbort(_exn); \
}
// Same as above ABORT_BLOCK but returns a value after the try/catch block to
// suppress compiler warnings. This allows us to avoid having to refactor code
// to get scoping right when wrapping an entire method.
#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL @try {
#define NS_OBJC_END_TRY_ABORT_BLOCK_NIL } @catch(NSException *_exn) { \
nsObjCExceptionLogAbort(_exn); \
} \
return nil;
#define NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT @try {
#define NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT } @catch(NSException *_exn) { \
nsObjCExceptionLogAbort(_exn);\
} \
return NS_ERROR_FAILURE;
#endif // nsObjCExceptions_h_