From 10dd21a3ef32653de312381f4f1520f0f2dc53be Mon Sep 17 00:00:00 2001 From: Andrew McCreight Date: Fri, 26 Jun 2015 18:44:14 -0700 Subject: [PATCH] Bug 886459, part 3 - Remove simple uses of nsIJSRuntimeService to get the JSRuntime. r=bholley --- caps/nsScriptSecurityManager.cpp | 3 +-- dom/base/nsFrameMessageManager.cpp | 10 ---------- dom/base/nsJSEnvironment.cpp | 24 ++++-------------------- dom/base/nsScriptLoader.cpp | 9 ++------- dom/ipc/nsIContentChild.cpp | 11 ++--------- dom/ipc/nsIContentParent.cpp | 12 ++---------- dom/xul/nsXULElement.cpp | 4 ---- ipc/testshell/XPCShellEnvironment.cpp | 12 ++---------- js/xpconnect/src/XPCJSRuntime.cpp | 6 ++++++ js/xpconnect/src/XPCShellImpl.cpp | 13 +++---------- js/xpconnect/src/xpcpublic.h | 3 +++ toolkit/xre/nsXREDirProvider.cpp | 12 +++--------- 12 files changed, 28 insertions(+), 91 deletions(-) diff --git a/caps/nsScriptSecurityManager.cpp b/caps/nsScriptSecurityManager.cpp index 4ee2706e3754..465c34776540 100644 --- a/caps/nsScriptSecurityManager.cpp +++ b/caps/nsScriptSecurityManager.cpp @@ -1256,8 +1256,7 @@ nsresult nsScriptSecurityManager::Init() //-- Register security check callback in the JS engine // Currently this is used to control access to function.caller - rv = nsXPConnect::XPConnect()->GetRuntime(&sRuntime); - NS_ENSURE_SUCCESS(rv, rv); + sRuntime = xpc::GetJSRuntime(); static const JSSecurityCallbacks securityCallbacks = { ContentSecurityPolicyPermitsJSAction, diff --git a/dom/base/nsFrameMessageManager.cpp b/dom/base/nsFrameMessageManager.cpp index b98bc376d185..32cc3e139156 100644 --- a/dom/base/nsFrameMessageManager.cpp +++ b/dom/base/nsFrameMessageManager.cpp @@ -26,7 +26,6 @@ #include "nsIMemoryReporter.h" #include "nsIProtocolHandler.h" #include "nsIScriptSecurityManager.h" -#include "nsIJSRuntimeService.h" #include "nsIDOMClassInfo.h" #include "xpcpublic.h" #include "mozilla/CycleCollectedJSRuntime.h" @@ -1822,15 +1821,6 @@ nsMessageManagerScriptExecutor::InitChildGlobalInternal( nsISupports* aScope, const nsACString& aID) { - - nsCOMPtr runtimeSvc = - do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - NS_ENSURE_TRUE(runtimeSvc, false); - - JSRuntime* rt = nullptr; - runtimeSvc->GetRuntime(&rt); - NS_ENSURE_TRUE(rt, false); - AutoSafeJSContext cx; nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal)); diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index fe12e5260a04..b68129ef2b2d 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -14,7 +14,6 @@ #include "nsDOMCID.h" #include "nsIServiceManager.h" #include "nsIXPConnect.h" -#include "nsIJSRuntimeService.h" #include "nsCOMPtr.h" #include "nsISupportsPrimitives.h" #include "nsReadableUtils.h" @@ -197,11 +196,6 @@ static bool sDidPaintAfterPreviousICCSlice = false; static nsScriptNameSpaceManager *gNameSpaceManager; -static nsIJSRuntimeService *sRuntimeService; - -static const char kJSRuntimeServiceContractID[] = - "@mozilla.org/js/xpc/RuntimeService;1"; - static PRTime sFirstCollectionTime; static JSRuntime *sRuntime; @@ -651,10 +645,8 @@ nsJSContext::~nsJSContext() if (!sContextCount && sDidShutdown) { // The last context is being deleted, and we're already in the - // process of shutting down, release the JS runtime service, and - // the security manager. + // process of shutting down, release the security manager. - NS_IF_RELEASE(sRuntimeService); NS_IF_RELEASE(sSecurityManager); } } @@ -2372,7 +2364,6 @@ mozilla::dom::StartupJSEnvironment() sNeedsFullCC = false; sNeedsGCAfterCC = false; gNameSpaceManager = nullptr; - sRuntimeService = nullptr; sRuntime = nullptr; sIsInitialized = false; sDidShutdown = false; @@ -2673,13 +2664,8 @@ nsJSContext::EnsureStatics() MOZ_CRASH(); } - rv = CallGetService(kJSRuntimeServiceContractID, &sRuntimeService); - if (NS_FAILED(rv)) { - MOZ_CRASH(); - } - - rv = sRuntimeService->GetRuntime(&sRuntime); - if (NS_FAILED(rv)) { + sRuntime = xpc::GetJSRuntime(); + if (!sRuntime) { MOZ_CRASH(); } @@ -2850,9 +2836,7 @@ mozilla::dom::ShutdownJSEnvironment() if (!sContextCount) { // We're being shutdown, and there are no more contexts - // alive, release the JS runtime service and the security manager. - - NS_IF_RELEASE(sRuntimeService); + // alive, release the security manager. NS_IF_RELEASE(sSecurityManager); } diff --git a/dom/base/nsScriptLoader.cpp b/dom/base/nsScriptLoader.cpp index b2b0ea516da3..f7aaca2df1d1 100644 --- a/dom/base/nsScriptLoader.cpp +++ b/dom/base/nsScriptLoader.cpp @@ -12,6 +12,7 @@ #include "jsapi.h" #include "jsfriendapi.h" +#include "xpcpublic.h" #include "nsIUnicodeDecoder.h" #include "nsIContent.h" #include "nsJSUtils.h" @@ -19,7 +20,6 @@ #include "mozilla/dom/Element.h" #include "nsGkAtoms.h" #include "nsNetUtil.h" -#include "nsIJSRuntimeService.h" #include "nsIScriptGlobalObject.h" #include "nsIScriptContext.h" #include "nsIScriptSecurityManager.h" @@ -831,12 +831,7 @@ NotifyOffThreadScriptLoadCompletedRunnable::Run() // The result of the off thread parse was not actually needed to process // the request (disappearing window, some other error, ...). Finish the // request to avoid leaks in the JS engine. - nsCOMPtr svc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - NS_ENSURE_TRUE(svc, NS_ERROR_FAILURE); - JSRuntime *rt; - svc->GetRuntime(&rt); - NS_ENSURE_TRUE(rt, NS_ERROR_FAILURE); - JS::FinishOffThreadScript(nullptr, rt, mToken); + JS::FinishOffThreadScript(nullptr, xpc::GetJSRuntime(), mToken); } return rv; diff --git a/dom/ipc/nsIContentChild.cpp b/dom/ipc/nsIContentChild.cpp index b895d1bb2b3e..9331d1dd3f16 100644 --- a/dom/ipc/nsIContentChild.cpp +++ b/dom/ipc/nsIContentChild.cpp @@ -15,8 +15,8 @@ #include "mozilla/dom/ipc/BlobChild.h" #include "mozilla/ipc/InputStreamUtils.h" -#include "nsIJSRuntimeService.h" #include "nsPrintfCString.h" +#include "xpcpublic.h" using namespace mozilla::ipc; using namespace mozilla::jsipc; @@ -27,14 +27,7 @@ namespace dom { PJavaScriptChild* nsIContentChild::AllocPJavaScriptChild() { - nsCOMPtr svc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - NS_ENSURE_TRUE(svc, nullptr); - - JSRuntime *rt; - svc->GetRuntime(&rt); - NS_ENSURE_TRUE(svc, nullptr); - - return NewJavaScriptChild(rt); + return NewJavaScriptChild(xpc::GetJSRuntime()); } bool diff --git a/dom/ipc/nsIContentParent.cpp b/dom/ipc/nsIContentParent.cpp index 37e7dfdaf688..1ec21a1fb410 100644 --- a/dom/ipc/nsIContentParent.cpp +++ b/dom/ipc/nsIContentParent.cpp @@ -19,8 +19,8 @@ #include "mozilla/unused.h" #include "nsFrameMessageManager.h" -#include "nsIJSRuntimeService.h" #include "nsPrintfCString.h" +#include "xpcpublic.h" using namespace mozilla::jsipc; @@ -49,15 +49,7 @@ nsIContentParent::AsContentParent() PJavaScriptParent* nsIContentParent::AllocPJavaScriptParent() { - nsCOMPtr svc = - do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - NS_ENSURE_TRUE(svc, nullptr); - - JSRuntime *rt; - svc->GetRuntime(&rt); - NS_ENSURE_TRUE(svc, nullptr); - - return NewJavaScriptParent(rt); + return NewJavaScriptParent(xpc::GetJSRuntime()); } bool diff --git a/dom/xul/nsXULElement.cpp b/dom/xul/nsXULElement.cpp index 54536f7cd31d..8312dbbebb3b 100644 --- a/dom/xul/nsXULElement.cpp +++ b/dom/xul/nsXULElement.cpp @@ -40,7 +40,6 @@ #include "mozilla/EventStates.h" #include "nsFocusManager.h" #include "nsHTMLStyleSheet.h" -#include "nsIJSRuntimeService.h" #include "nsNameSpaceManager.h" #include "nsIObjectInputStream.h" #include "nsIObjectOutputStream.h" @@ -2767,9 +2766,6 @@ NotifyOffThreadScriptCompletedRunnable::Run() // Note: this unroots mScript so that it is available to be collected by the // JS GC. The receiver needs to root the script before performing a call that // could GC. - nsCOMPtr svc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - NS_ENSURE_TRUE(svc, NS_ERROR_FAILURE); - JSScript *script; { AutoSafeJSContext cx; diff --git a/ipc/testshell/XPCShellEnvironment.cpp b/ipc/testshell/XPCShellEnvironment.cpp index df990d956d93..c1b2c0ed3f8f 100644 --- a/ipc/testshell/XPCShellEnvironment.cpp +++ b/ipc/testshell/XPCShellEnvironment.cpp @@ -26,7 +26,6 @@ #include "nsIChannel.h" #include "nsIClassInfo.h" #include "nsIDirectoryService.h" -#include "nsIJSRuntimeService.h" #include "nsIPrincipal.h" #include "nsIScriptSecurityManager.h" #include "nsIURI.h" @@ -476,15 +475,8 @@ XPCShellEnvironment::Init() // is unbuffered by default setbuf(stdout, 0); - nsCOMPtr rtsvc = - do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - if (!rtsvc) { - NS_ERROR("failed to get nsJSRuntimeService!"); - return false; - } - - JSRuntime *rt; - if (NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) { + JSRuntime *rt = xpc::GetJSRuntime(); + if (!rt) { NS_ERROR("failed to get JSRuntime from nsJSRuntimeService!"); return false; } diff --git a/js/xpconnect/src/XPCJSRuntime.cpp b/js/xpconnect/src/XPCJSRuntime.cpp index 8ba588d400f5..584c954a8a32 100644 --- a/js/xpconnect/src/XPCJSRuntime.cpp +++ b/js/xpconnect/src/XPCJSRuntime.cpp @@ -1708,6 +1708,12 @@ xpc::GetCurrentCompartmentName(JSContext* cx, nsCString& name) GetCompartmentName(compartment, name, &anonymizeID, false); } +JSRuntime* +xpc::GetJSRuntime() +{ + return XPCJSRuntime::Get()->Runtime(); +} + static int64_t JSMainRuntimeGCHeapDistinguishedAmount() { diff --git a/js/xpconnect/src/XPCShellImpl.cpp b/js/xpconnect/src/XPCShellImpl.cpp index 4c9b2c90637a..851c7aa9bd06 100644 --- a/js/xpconnect/src/XPCShellImpl.cpp +++ b/js/xpconnect/src/XPCShellImpl.cpp @@ -22,7 +22,6 @@ #include "nsArrayEnumerator.h" #include "nsCOMArray.h" #include "nsDirectoryServiceUtils.h" -#include "nsIJSRuntimeService.h" #include "nsCOMPtr.h" #include "nsAutoPtr.h" #include "nsJSPrincipals.h" @@ -1391,15 +1390,9 @@ XRE_XPCShellMain(int argc, char** argv, char** envp) return 1; } - nsCOMPtr rtsvc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1"); - // get the JSRuntime from the runtime svc - if (!rtsvc) { - printf("failed to get nsJSRuntimeService!\n"); - return 1; - } - - if (NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) { - printf("failed to get JSRuntime from nsJSRuntimeService!\n"); + rt = xpc::GetJSRuntime(); + if (!rt) { + printf("failed to get JSRuntime from XPConnect!\n"); return 1; } diff --git a/js/xpconnect/src/xpcpublic.h b/js/xpconnect/src/xpcpublic.h index c84a1bb355b4..f7f9818ca17b 100644 --- a/js/xpconnect/src/xpcpublic.h +++ b/js/xpconnect/src/xpcpublic.h @@ -535,6 +535,9 @@ DispatchScriptErrorEvent(nsPIDOMWindow* win, JSRuntime* rt, xpc::ErrorReport* xp extern void GetCurrentCompartmentName(JSContext*, nsCString& name); +JSRuntime* +GetJSRuntime(); + } // namespace xpc namespace mozilla { diff --git a/toolkit/xre/nsXREDirProvider.cpp b/toolkit/xre/nsXREDirProvider.cpp index 5d06b3288f3b..705d8915b84a 100644 --- a/toolkit/xre/nsXREDirProvider.cpp +++ b/toolkit/xre/nsXREDirProvider.cpp @@ -10,7 +10,6 @@ #include "jsapi.h" #include "xpcpublic.h" -#include "nsIJSRuntimeService.h" #include "nsIAddonInterposition.h" #include "nsIAppStartup.h" #include "nsIDirectoryEnumerator.h" @@ -893,14 +892,9 @@ nsXREDirProvider::DoShutdown() // Phase 2c: Now that things are torn down, force JS GC so that things which depend on // resources which are about to go away in "profile-before-change" are destroyed first. - nsCOMPtr rtsvc - (do_GetService("@mozilla.org/js/xpc/RuntimeService;1")); - if (rtsvc) - { - JSRuntime *rt = nullptr; - rtsvc->GetRuntime(&rt); - if (rt) - ::JS_GC(rt); + JSRuntime *rt = xpc::GetJSRuntime(); + if (rt) { + JS_GC(rt); } // Phase 3: Notify observers of a profile change