From e4c6542ef9eec00a9b141e37259c5349454b9cb8 Mon Sep 17 00:00:00 2001 From: Masatoshi Kimura Date: Tue, 19 Jun 2012 19:01:10 -0400 Subject: [PATCH] Bug 764916 - Replace nsDOMClassInfo::ThrowJSException with xpc::Throw. r=mrbkap --- content/xbl/src/nsXBLBinding.cpp | 11 ++-- dom/base/nsDOMClassInfo.cpp | 88 +++---------------------- dom/base/nsDOMClassInfo.h | 2 - dom/base/nsDOMScriptObjectFactory.cpp | 24 ------- dom/base/nsJSEnvironment.cpp | 7 +- dom/base/test/Makefile.in | 1 + dom/base/test/test_nondomexception.html | 28 ++++++++ dom/indexedDB/IDBKeyRange.cpp | 5 +- dom/workers/WorkerPrivate.cpp | 5 +- js/xpconnect/src/XPCLocale.cpp | 9 +-- 10 files changed, 54 insertions(+), 126 deletions(-) create mode 100644 dom/base/test/test_nondomexception.html diff --git a/content/xbl/src/nsXBLBinding.cpp b/content/xbl/src/nsXBLBinding.cpp index cc41eeef2b84..047e8738dfdc 100644 --- a/content/xbl/src/nsXBLBinding.cpp +++ b/content/xbl/src/nsXBLBinding.cpp @@ -55,7 +55,7 @@ #include "nsNodeUtils.h" // Nasty hack. Maybe we could move some of the classinfo utility methods -// (e.g. WrapNative and ThrowJSException) over to nsContentUtils? +// (e.g. WrapNative) over to nsContentUtils? #include "nsDOMClassInfo.h" #include "nsJSUtils.h" @@ -118,7 +118,7 @@ XBLResolve(JSContext *cx, JSHandleObject obj, JSHandleId id, unsigned flags, if (~nodeClass->flags & (JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS)) { - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_UNEXPECTED); + xpc::Throw(cx, NS_ERROR_UNEXPECTED); return JS_FALSE; } @@ -135,7 +135,7 @@ XBLResolve(JSContext *cx, JSHandleObject obj, JSHandleId id, unsigned flags, nsCOMPtr content = do_QueryWrappedNative(xpcWrapper); if (!content) { - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_UNEXPECTED); + xpc::Throw(cx, NS_ERROR_UNEXPECTED); return JS_FALSE; } @@ -160,10 +160,7 @@ XBLResolve(JSContext *cx, JSHandleObject obj, JSHandleId id, unsigned flags, protoBinding->DocURI(), &didInstall); if (NS_FAILED(rv)) { - if (!::JS_IsExceptionPending(cx)) { - nsDOMClassInfo::ThrowJSException(cx, rv); - } - + xpc::Throw(cx, rv); return JS_FALSE; } diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 55fa98ce9bb3..15bc0c3cd269 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -2142,70 +2142,6 @@ nsDOMClassInfo::DefineStaticJSVals(JSContext *cx) return NS_OK; } -static nsresult -CreateExceptionFromResult(JSContext *cx, nsresult aResult) -{ - nsCOMPtr xs = - do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID); - if (!xs) { - return NS_ERROR_FAILURE; - } - - nsCOMPtr xm; - nsresult rv = xs->GetCurrentExceptionManager(getter_AddRefs(xm)); - if (NS_FAILED(rv)) { - return NS_ERROR_FAILURE; - } - - nsCOMPtr exception; - rv = xm->GetExceptionFromProvider(aResult, 0, getter_AddRefs(exception)); - if (NS_FAILED(rv) || !exception) { - return NS_ERROR_FAILURE; - } - - JS::Value jv; - nsCOMPtr holder; - rv = WrapNative(cx, ::JS_GetGlobalObject(cx), exception, - &NS_GET_IID(nsIException), false, &jv, - getter_AddRefs(holder)); - if (NS_FAILED(rv) || jv.isNull()) { - return NS_ERROR_FAILURE; - } - - JSAutoEnterCompartment ac; - - if (jv.isObject()) { - if (!ac.enter(cx, &jv.toObject())) { - return NS_ERROR_UNEXPECTED; - } - } - - JS_SetPendingException(cx, jv); - return NS_OK; -} - -// static -nsresult -nsDOMClassInfo::ThrowJSException(JSContext *cx, nsresult aResult) -{ - JSAutoRequest ar(cx); - - if (NS_SUCCEEDED(CreateExceptionFromResult(cx, aResult))) { - return NS_OK; - } - - // XXX This probably wants to be localized, but that can fail in ways that - // are hard to report correctly. - JSString *str = - JS_NewStringCopyZ(cx, "An error occurred throwing an exception"); - if (!str) { - // JS_NewStringCopyZ reported the error for us. - return NS_OK; - } - JS_SetPendingException(cx, STRING_TO_JSVAL(str)); - return NS_OK; -} - // static bool nsDOMClassInfo::ObjectIsNativeWrapper(JSContext* cx, JSObject* obj) @@ -7130,9 +7066,7 @@ LocationSetter(JSContext *cx, JSHandleObject obj, JSHandleId id, JSBool strict, { nsresult rv = LocationSetterGuts(cx, obj, vp); if (NS_FAILED(rv)) { - if (!::JS_IsExceptionPending(cx)) { - nsDOMClassInfo::ThrowJSException(cx, rv); - } + xpc::Throw(cx, rv); return JS_FALSE; } @@ -7805,9 +7739,7 @@ GetterShim(JSContext *cx, JSHandleObject obj, JSHandleId /* unused */, jsval *vp { nsresult rv = (*func)(cx, obj, vp); if (NS_FAILED(rv)) { - if (!::JS_IsExceptionPending(cx)) { - nsDOMClassInfo::ThrowJSException(cx, rv); - } + xpc::Throw(cx, rv); return JS_FALSE; } @@ -8972,7 +8904,7 @@ nsHTMLDocumentSH::GetDocumentAllNodeList(JSContext *cx, JSObject *obj, } if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_FAILURE); + xpc::Throw(cx, NS_ERROR_FAILURE); return JS_FALSE; } @@ -9024,7 +8956,7 @@ nsHTMLDocumentSH::DocumentAllGetProperty(JSContext *cx, JSHandleObject obj_, rv = nodeList->GetLength(&length); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return JS_FALSE; } @@ -9040,7 +8972,7 @@ nsHTMLDocumentSH::DocumentAllGetProperty(JSContext *cx, JSHandleObject obj_, result = doc->GetDocumentAllResult(str, &cache, &rv); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return JS_FALSE; } @@ -9068,7 +9000,7 @@ nsHTMLDocumentSH::DocumentAllGetProperty(JSContext *cx, JSHandleObject obj_, if (result) { rv = WrapNative(cx, JS_GetGlobalForScopeChain(cx), result, cache, true, vp); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return JS_FALSE; } @@ -9160,7 +9092,7 @@ nsHTMLDocumentSH::CallToGetPropMapper(JSContext *cx, unsigned argc, jsval *vp) // XXX: Should throw NS_ERROR_XPC_NOT_ENOUGH_ARGS for argc < 1, // and create a new NS_ERROR_XPC_TOO_MANY_ARGS for argc > 1? IE // accepts nothing other than one arg. - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_INVALID_ARG); + xpc::Throw(cx, NS_ERROR_INVALID_ARG); return JS_FALSE; } @@ -9254,7 +9186,7 @@ nsHTMLDocumentSH::DocumentAllHelperGetProperty(JSContext *cx, JSHandleObject obj nsresult rv; nsCOMPtr doc = do_QueryWrapper(cx, obj, &rv); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return JS_FALSE; } @@ -9332,7 +9264,7 @@ nsHTMLDocumentSH::DocumentAllTagsNewResolve(JSContext *cx, JSHandleObject obj, static_cast(tags), tags, true, &v, getter_AddRefs(holder)); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return JS_FALSE; } @@ -9432,7 +9364,7 @@ nsHTMLDocumentSH::NewResolve(nsIXPConnectWrappedNative *wrapper, JSContext *cx, // Insert the helper into our prototype chain. helper's prototype // is already obj's current prototype. if (!::JS_SetPrototype(cx, obj, helper)) { - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_UNEXPECTED); + xpc::Throw(cx, NS_ERROR_UNEXPECTED); return NS_ERROR_UNEXPECTED; } diff --git a/dom/base/nsDOMClassInfo.h b/dom/base/nsDOMClassInfo.h index 53d6f15dc38a..bef070ee408b 100644 --- a/dom/base/nsDOMClassInfo.h +++ b/dom/base/nsDOMClassInfo.h @@ -122,8 +122,6 @@ public: return new nsDOMClassInfo(aData); } - static nsresult ThrowJSException(JSContext *cx, nsresult aResult); - /* * The following two functions exist because of the way that Xray wrappers * work. In order to allow scriptable helpers to define non-IDL defined but diff --git a/dom/base/nsDOMScriptObjectFactory.cpp b/dom/base/nsDOMScriptObjectFactory.cpp index a29bf5db80a3..155f600ca0ce 100644 --- a/dom/base/nsDOMScriptObjectFactory.cpp +++ b/dom/base/nsDOMScriptObjectFactory.cpp @@ -55,7 +55,6 @@ nsDOMScriptObjectFactory::nsDOMScriptObjectFactory() xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM); xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_SVG); xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_XPATH); - xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_XPCONNECT); xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_INDEXEDDB); xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_FILEHANDLE); } @@ -141,8 +140,6 @@ nsDOMScriptObjectFactory::Observe(nsISupports *aSubject, NS_ERROR_MODULE_SVG); xs->UnregisterExceptionProvider(gExceptionProvider, NS_ERROR_MODULE_DOM_XPATH); - xs->UnregisterExceptionProvider(gExceptionProvider, - NS_ERROR_MODULE_XPCONNECT); xs->UnregisterExceptionProvider(gExceptionProvider, NS_ERROR_MODULE_DOM_INDEXEDDB); xs->UnregisterExceptionProvider(gExceptionProvider, @@ -156,27 +153,6 @@ nsDOMScriptObjectFactory::Observe(nsISupports *aSubject, return NS_OK; } -static nsresult -CreateXPConnectException(nsresult aResult, nsIException *aDefaultException, - nsIException **_retval) -{ - // See whether we already have a useful XPConnect exception. If we - // do, let's not create one with _less_ information! - nsCOMPtr exception(do_QueryInterface(aDefaultException)); - if (!exception) { - nsresult rv = NS_OK; - exception = do_CreateInstance("@mozilla.org/js/xpc/Exception;1", &rv); - NS_ENSURE_SUCCESS(rv, rv); - - rv = exception->Initialize(nsnull, aResult, nsnull, nsnull, nsnull, - nsnull); - NS_ENSURE_SUCCESS(rv, rv); - } - - exception.forget(_retval); - return NS_OK; -} - NS_IMETHODIMP nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName, nsDOMClassInfoExternalConstructorFnc aConstructorFptr, diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index 94d893ea172a..60fd53073cf2 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -40,7 +40,6 @@ #include "nsXPCOMCIDInternal.h" #include "nsIXULRuntime.h" -#include "nsDOMClassInfo.h" #include "xpcpublic.h" #include "jsdbgapi.h" // for JS_ClearWatchPointsForObject @@ -3700,7 +3699,7 @@ NS_DOMReadStructuredClone(JSContext* cx, } // Don't know what this is. Bail. - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_DOM_DATA_CLONE_ERR); + xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR); return nsnull; } @@ -3734,7 +3733,7 @@ NS_DOMWriteStructuredClone(JSContext* cx, } // Don't know what this is. Bail. - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_DOM_DATA_CLONE_ERR); + xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR); return JS_FALSE; } @@ -3743,7 +3742,7 @@ NS_DOMStructuredCloneError(JSContext* cx, uint32_t errorid) { // We don't currently support any extensions to structured cloning. - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_DOM_DATA_CLONE_ERR); + xpc::Throw(cx, NS_ERROR_DOM_DATA_CLONE_ERR); } //static diff --git a/dom/base/test/Makefile.in b/dom/base/test/Makefile.in index 8372dd032bb1..aa31dd533ff0 100644 --- a/dom/base/test/Makefile.in +++ b/dom/base/test/Makefile.in @@ -15,6 +15,7 @@ TEST_FILES = \ test_domrequest.html \ test_gsp-standards.html \ test_gsp-quirks.html \ + test_nondomexception.html \ $(NULL) libs:: $(TEST_FILES) diff --git a/dom/base/test/test_nondomexception.html b/dom/base/test/test_nondomexception.html new file mode 100644 index 000000000000..558db04e7a25 --- /dev/null +++ b/dom/base/test/test_nondomexception.html @@ -0,0 +1,28 @@ + + + + Test for non-DOM module exceptions + + + + +

+ +
+
+
+ + diff --git a/dom/indexedDB/IDBKeyRange.cpp b/dom/indexedDB/IDBKeyRange.cpp index 7adf0328d2cf..132837e43793 100644 --- a/dom/indexedDB/IDBKeyRange.cpp +++ b/dom/indexedDB/IDBKeyRange.cpp @@ -10,7 +10,6 @@ #include "nsIXPConnect.h" -#include "nsDOMClassInfo.h" #include "nsJSUtils.h" #include "nsThreadUtils.h" #include "nsContentUtils.h" @@ -90,9 +89,7 @@ ThrowException(JSContext* aCx, nsresult aErrorCode) { NS_ASSERTION(NS_FAILED(aErrorCode), "Not an error code!"); - if (!JS_IsExceptionPending(aCx)) { - nsDOMClassInfo::ThrowJSException(aCx, aErrorCode); - } + xpc::Throw(aCx, aErrorCode); } inline diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 74018455011c..8ebb1a86e7ea 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -31,7 +31,6 @@ #include "js/MemoryMetrics.h" #include "nsAlgorithm.h" #include "nsContentUtils.h" -#include "nsDOMClassInfo.h" #include "nsDOMJSUtils.h" #include "nsGUIEvent.h" #include "nsJSEnvironment.h" @@ -2479,7 +2478,7 @@ WorkerPrivate::Create(JSContext* aCx, JSObject* aObj, WorkerPrivate* aParent, // First check to make sure the caller has permission to make a // ChromeWorker if they called the ChromeWorker constructor. if (aIsChromeWorker && !isChrome) { - nsDOMClassInfo::ThrowJSException(aCx, NS_ERROR_DOM_SECURITY_ERR); + xpc::Throw(aCx, NS_ERROR_DOM_SECURITY_ERR); return nsnull; } @@ -2508,7 +2507,7 @@ WorkerPrivate::Create(JSContext* aCx, JSObject* aObj, WorkerPrivate* aParent, if (!window || (globalWindow != window && !nsContentUtils::CanCallerAccess(window))) { - nsDOMClassInfo::ThrowJSException(aCx, NS_ERROR_DOM_SECURITY_ERR); + xpc::Throw(aCx, NS_ERROR_DOM_SECURITY_ERR); return nsnull; } diff --git a/js/xpconnect/src/XPCLocale.cpp b/js/xpconnect/src/XPCLocale.cpp index 02c8c5b6a5d7..69e2ac0138b2 100644 --- a/js/xpconnect/src/XPCLocale.cpp +++ b/js/xpconnect/src/XPCLocale.cpp @@ -11,7 +11,6 @@ #include "jsapi.h" #include "nsCollationCID.h" -#include "nsDOMClassInfo.h" #include "nsJSUtils.h" #include "nsICharsetConverterManager.h" #include "nsIPlatformCharset.h" @@ -20,6 +19,8 @@ #include "nsIServiceManager.h" #include "nsUnicharUtils.h" +#include "xpcpublic.h" + /** * JS locale callbacks implemented by XPCOM modules. This * implementation is "safe" up to the following restrictions @@ -208,7 +209,7 @@ struct XPCLocaleCallbacks : public JSLocaleCallbacks } if (!str) { - nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_OUT_OF_MEMORY); + xpc::Throw(cx, NS_ERROR_OUT_OF_MEMORY); return false; } @@ -240,7 +241,7 @@ struct XPCLocaleCallbacks : public JSLocaleCallbacks } if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return false; } @@ -256,7 +257,7 @@ struct XPCLocaleCallbacks : public JSLocaleCallbacks depStr1, depStr2, &result); if (NS_FAILED(rv)) { - nsDOMClassInfo::ThrowJSException(cx, rv); + xpc::Throw(cx, rv); return false; }