Bug 911258: Part 1 - Remove nsIExceptionService/Manager usage. r=bholley
authorKyle Huey <khuey@kylehuey.com>
Sun, 08 Sep 2013 20:28:49 -0700
changeset 146109 7a9cc2c01bd0857d96a3939002ddd2f400023c11
parent 146108 aa8d73a7b08a9ad9ad8da214e1d6131f1f4e72ad
child 146110 c04e927b631b053fc9e8d26840bac101869c550e
push id25240
push useremorley@mozilla.com
push dateMon, 09 Sep 2013 12:09:06 +0000
treeherdermozilla-central@9846f07d5867 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs911258
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 911258: Part 1 - Remove nsIExceptionService/Manager usage. r=bholley
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/XPCThrower.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -2893,18 +2893,17 @@ XPCJSRuntime::XPCJSRuntime(nsXPConnect* 
    mWrappedJSToReleaseArray(),
    mNativesToReleaseArray(),
    mDoingFinalization(false),
    mVariantRoots(nullptr),
    mWrappedJSRoots(nullptr),
    mObjectHolderRoots(nullptr),
    mWatchdogManager(new WatchdogManager(this)),
    mJunkScope(nullptr),
-   mAsyncSnowWhiteFreer(new AsyncFreeSnowWhite()),
-   mExceptionManagerNotAvailable(false)
+   mAsyncSnowWhiteFreer(new AsyncFreeSnowWhite())
 {
 #ifdef XPC_CHECK_WRAPPERS_AT_SHUTDOWN
     DEBUG_WrappedNativeHashtable =
         PL_NewDHashTable(PL_DHashGetStubOps(), nullptr,
                          sizeof(PLDHashEntryStub), 128);
 #endif
 
     DOM_InitInterfaces();
--- a/js/xpconnect/src/XPCThrower.cpp
+++ b/js/xpconnect/src/XPCThrower.cpp
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* Code for throwing errors into JavaScript. */
 
 #include "xpcprivate.h"
 #include "xpcpublic.h"
 #include "XPCWrapper.h"
 #include "jsprf.h"
+#include "nsDOMException.h"
 
 bool XPCThrower::sVerbose = true;
 
 // static
 void
 XPCThrower::Throw(nsresult rv, JSContext* cx)
 {
     const char* format;
@@ -40,18 +41,17 @@ Throw(JSContext *cx, nsresult rv)
  * If there has already been an exception thrown, see if we're throwing the
  * same sort of exception, and if we are, don't clobber the old one. ccx
  * should be the current call context.
  */
 // static
 bool
 XPCThrower::CheckForPendingException(nsresult result, JSContext *cx)
 {
-    nsCOMPtr<nsIException> e;
-    XPCJSRuntime::Get()->GetPendingException(getter_AddRefs(e));
+    nsCOMPtr<nsIException> e = XPCJSRuntime::Get()->GetPendingException();
     if (!e)
         return false;
     XPCJSRuntime::Get()->SetPendingException(nullptr);
 
     nsresult e_result;
     if (NS_FAILED(e->GetResult(&e_result)) || e_result != result)
         return false;
 
@@ -173,42 +173,62 @@ XPCThrower::Verbosify(XPCCallContext& cc
 void
 XPCThrower::BuildAndThrowException(JSContext* cx, nsresult rv, const char* sz)
 {
     bool success = false;
 
     /* no need to set an expection if the security manager already has */
     if (rv == NS_ERROR_XPC_SECURITY_MANAGER_VETO && JS_IsExceptionPending(cx))
         return;
-    nsCOMPtr<nsIException> finalException;
-    nsCOMPtr<nsIException> defaultException;
-    nsXPCException::NewException(sz, rv, nullptr, nullptr, getter_AddRefs(defaultException));
 
-    nsIExceptionManager * exceptionManager = XPCJSRuntime::Get()->GetExceptionManager();
-    if (exceptionManager) {
-        // Ask the provider for the exception, if there is no provider
-        // we expect it to set e to null
-        exceptionManager->GetExceptionFromProvider(rv,
-                                                   defaultException,
-                                                   getter_AddRefs(finalException));
-        // We should get at least the defaultException back,
-        // but just in case
-        if (finalException == nullptr) {
-            finalException = defaultException;
+    XPCJSRuntime* runtime = XPCJSRuntime::Get();
+    nsCOMPtr<nsIException> existingException = runtime->GetPendingException();
+    if (existingException) {
+        nsresult nr;
+        if (NS_SUCCEEDED(existingException->GetResult(&nr)) && 
+            rv == nr) {
+            // Just reuse the existing exception.
+            return;
         }
     }
 
-    // XXX Should we put the following test and call to JS_ReportOutOfMemory
-    // inside this test?
-    if (finalException)
-        success = ThrowExceptionObject(cx, finalException);
-    // If we weren't able to build or throw an exception we're
+    nsCOMPtr<nsIException> finalException;
+    nsCOMPtr<nsIException> defaultException;
+    nsXPCException::NewException(sz, rv, nullptr, nullptr,
+                                 getter_AddRefs(defaultException));
+
+    // Do we use DOM exceptions for this error code?
+    switch (NS_ERROR_GET_MODULE(rv)) {
+    case NS_ERROR_MODULE_DOM:
+    case NS_ERROR_MODULE_SVG:
+    case NS_ERROR_MODULE_DOM_XPATH:
+    case NS_ERROR_MODULE_DOM_INDEXEDDB:
+    case NS_ERROR_MODULE_DOM_FILEHANDLE:
+        if (NS_IsMainThread()) {
+            NS_NewDOMException(rv, defaultException,
+                               getter_AddRefs(finalException));
+        }
+        break;
+
+    default:
+        break;
+    }
+
+    // If not, use the default.
+    if (finalException == nullptr) {
+        finalException = defaultException;
+    }
+
+    MOZ_ASSERT(finalException);
+    success = ThrowExceptionObject(cx, finalException);
+    // If we weren't able to throw an exception we're
     // most likely out of memory
-    if (!success)
+    if (!success) {
         JS_ReportOutOfMemory(cx);
+    }
 }
 
 static bool
 IsCallerChrome(JSContext* cx)
 {
     nsresult rv;
 
     nsCOMPtr<nsIScriptSecurityManager> secMan;
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -127,17 +127,17 @@
 #include "nsXPIDLString.h"
 #include "nsAutoJSValHolder.h"
 
 #include "nsThreadUtils.h"
 #include "nsIJSEngineTelemetryStats.h"
 
 #include "nsIConsoleService.h"
 #include "nsIScriptError.h"
-#include "nsIExceptionService.h"
+#include "nsIException.h"
 
 #include "nsVariant.h"
 #include "nsIPropertyBag.h"
 #include "nsIProperty.h"
 #include "nsCOMArray.h"
 #include "nsTArray.h"
 #include "nsBaseHashtable.h"
 #include "nsHashKeys.h"
@@ -734,58 +734,25 @@ public:
     void DebugDump(int16_t depth);
 
     void SystemIsBeingShutDown();
 
     PRThread* GetThreadRunningGC() const {return mThreadRunningGC;}
 
     ~XPCJSRuntime();
 
-    nsresult GetPendingException(nsIException** aException)
-    {
-        if (EnsureExceptionManager())
-            return mExceptionManager->GetCurrentException(aException);
-        nsCOMPtr<nsIException> out = mPendingException;
-        out.forget(aException);
-        return NS_OK;
-    }
-
-    nsresult SetPendingException(nsIException* aException)
-    {
-        if (EnsureExceptionManager())
-            return mExceptionManager->SetCurrentException(aException);
-
-        mPendingException = aException;
-        return NS_OK;
-    }
-
-    nsIExceptionManager* GetExceptionManager()
+    already_AddRefed<nsIException> GetPendingException()
     {
-        if (EnsureExceptionManager())
-            return mExceptionManager;
-        return nullptr;
+        nsCOMPtr<nsIException> out = mPendingException;
+        return out.forget();
     }
 
-    bool EnsureExceptionManager()
+    void SetPendingException(nsIException* aException)
     {
-        if (mExceptionManager)
-            return true;
-
-        if (mExceptionManagerNotAvailable)
-            return false;
-
-        nsCOMPtr<nsIExceptionService> xs =
-            do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
-        if (xs)
-            xs->GetCurrentExceptionManager(getter_AddRefs(mExceptionManager));
-        if (mExceptionManager)
-            return true;
-
-        mExceptionManagerNotAvailable = true;
-        return false;
+        mPendingException = aException;
     }
 
     XPCReadableJSStringWrapper *NewStringWrapper(const PRUnichar *str, uint32_t len);
     void DeleteString(nsAString *string);
 
 #ifdef XPC_CHECK_WRAPPERS_AT_SHUTDOWN
    void DEBUG_AddWrappedNative(nsIXPConnectWrappedNative* wrapper)
         {XPCAutoLock lock(GetMapLock());
@@ -860,18 +827,16 @@ private:
     nsRefPtr<WatchdogManager> mWatchdogManager;
     JS::GCSliceCallback mPrevGCSliceCallback;
     JSObject* mJunkScope;
     nsRefPtr<AsyncFreeSnowWhite> mAsyncSnowWhiteFreer;
 
     mozilla::TimeStamp mSlowScriptCheckpoint;
 
     nsCOMPtr<nsIException>   mPendingException;
-    nsCOMPtr<nsIExceptionManager> mExceptionManager;
-    bool mExceptionManagerNotAvailable;
 
 #define XPCCCX_STRING_CACHE_SIZE 2
 
     // String wrapper entry, holds a string, and a boolean that tells
     // whether the string is in use or not.
     //
     // NB: The string is not stored by value so that we avoid the cost of
     // construction/destruction.