From c628779566b518035b5863a3062ff2362e77196c Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sat, 29 Jun 2013 18:44:06 -0600 Subject: [PATCH] Bug 865745 - Remove mCx from nsFrameScriptExecutor. r=smaug --- content/base/src/nsFrameLoader.cpp | 2 - content/base/src/nsFrameMessageManager.cpp | 115 +----------------- content/base/src/nsFrameMessageManager.h | 30 +---- .../base/src/nsInProcessTabChildGlobal.cpp | 1 - dom/ipc/TabChild.cpp | 2 - dom/ipc/TabChild.h | 2 - 6 files changed, 4 insertions(+), 148 deletions(-) diff --git a/content/base/src/nsFrameLoader.cpp b/content/base/src/nsFrameLoader.cpp index 4e7c7d3a2fe7..06f331f83e14 100644 --- a/content/base/src/nsFrameLoader.cpp +++ b/content/base/src/nsFrameLoader.cpp @@ -2219,8 +2219,6 @@ public: static_cast(mFrameLoader->mChildMessageManager.get()); if (tabChild && tabChild->GetInnerManager()) { nsCOMPtr kungFuDeathGrip(tabChild->GetGlobal()); - nsFrameScriptCx cx(static_cast(tabChild), tabChild); - StructuredCloneData data; data.mData = mData.data(); data.mDataLength = mData.nbytes(); diff --git a/content/base/src/nsFrameMessageManager.cpp b/content/base/src/nsFrameMessageManager.cpp index f135d46c2310..4eebd1bc34bc 100644 --- a/content/base/src/nsFrameMessageManager.cpp +++ b/content/base/src/nsFrameMessageManager.cpp @@ -840,81 +840,6 @@ NS_NewGlobalMessageManager(nsIMessageBroadcaster** aResult) return CallQueryInterface(mm, aResult); } -void -ContentScriptErrorReporter(JSContext* aCx, - const char* aMessage, - JSErrorReport* aReport) -{ - nsresult rv; - nsCOMPtr scriptError = - do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv); - if (NS_FAILED(rv)) { - return; - } - nsAutoString message, filename, line; - uint32_t lineNumber, columnNumber, flags, errorNumber; - - if (aReport) { - if (aReport->ucmessage) { - message.Assign(static_cast(aReport->ucmessage)); - } - filename.AssignWithConversion(aReport->filename); - line.Assign(static_cast(aReport->uclinebuf)); - lineNumber = aReport->lineno; - columnNumber = aReport->uctokenptr - aReport->uclinebuf; - flags = aReport->flags; - errorNumber = aReport->errorNumber; - } else { - lineNumber = columnNumber = errorNumber = 0; - flags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag; - } - - if (message.IsEmpty()) { - message.AssignWithConversion(aMessage); - } - - rv = scriptError->Init(message, filename, line, - lineNumber, columnNumber, flags, - "Message manager content script"); - if (NS_FAILED(rv)) { - return; - } - - nsCOMPtr consoleService = - do_GetService(NS_CONSOLESERVICE_CONTRACTID); - if (consoleService) { - (void) consoleService->LogMessage(scriptError); - } - -#ifdef DEBUG - // Print it to stderr as well, for the benefit of those invoking - // mozilla with -console. - nsAutoCString error; - error.Assign("JavaScript "); - if (JSREPORT_IS_STRICT(flags)) { - error.Append("strict "); - } - if (JSREPORT_IS_WARNING(flags)) { - error.Append("warning: "); - } else { - error.Append("error: "); - } - error.Append(aReport->filename); - error.Append(", line "); - error.AppendInt(lineNumber, 10); - error.Append(": "); - if (aReport->ucmessage) { - AppendUTF16toUTF8(static_cast(aReport->ucmessage), - error); - } else { - error.Append(aMessage); - } - - fprintf(stderr, "%s\n", error.get()); - fflush(stderr); -#endif -} - nsDataHashtable* nsFrameScriptExecutor::sCachedScripts = nullptr; nsScriptCacheCleaner* nsFrameScriptExecutor::sScriptCacheCleaner = nullptr; @@ -937,20 +862,6 @@ nsFrameScriptExecutor::DidCreateGlobal() void nsFrameScriptExecutor::DestroyCx() { - if (mCxStackRefCnt) { - mDelayedCxDestroy = true; - return; - } - mDelayedCxDestroy = false; - if (mCx) { - nsIXPConnect* xpc = nsContentUtils::XPConnect(); - if (xpc) { - xpc->ReleaseJSContext(mCx, true); - } else { - JS_DestroyContext(mCx); - } - } - mCx = nullptr; mGlobal = nullptr; } @@ -1094,26 +1005,16 @@ nsFrameScriptExecutor::InitTabChildGlobalInternal(nsISupports* aScope, runtimeSvc->GetRuntime(&rt); NS_ENSURE_TRUE(rt, false); - JSContext* cx_ = JS_NewContext(rt, 8192); - NS_ENSURE_TRUE(cx_, false); - AutoPushJSContext cx(cx_); - - mCx = cx; - + AutoSafeJSContext cx; nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal)); - JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_PRIVATE_IS_NSISUPPORTS); - JS_SetErrorReporter(cx, ContentScriptErrorReporter); - nsIXPConnect* xpc = nsContentUtils::XPConnect(); const uint32_t flags = nsIXPConnect::INIT_JS_STANDARD_CLASSES; - - JS_SetContextPrivate(cx, aScope); - JS::CompartmentOptions options; options.setZone(JS::SystemZone) .setVersion(JSVERSION_LATEST); + nsresult rv = xpc->InitClassesWithNewWrappedGlobal(cx, aScope, mPrincipal, flags, options, getter_AddRefs(mGlobal)); @@ -1123,8 +1024,6 @@ nsFrameScriptExecutor::InitTabChildGlobalInternal(nsISupports* aScope, JS::Rooted global(cx, mGlobal->GetJSObject()); NS_ENSURE_TRUE(global, false); - JS_SetGlobalObject(cx, global); - // Set the location information for the new global, so that tools like // about:memory may use that information. xpc::SetLocationForGlobal(global, aID); @@ -1139,21 +1038,13 @@ nsFrameScriptExecutor::Traverse(nsFrameScriptExecutor *tmp, nsCycleCollectionTraversalCallback &cb) { NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal) - nsIXPConnect* xpc = nsContentUtils::XPConnect(); - if (xpc) { - NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCx"); - xpc->NoteJSContext(tmp->mCx, cb); - } } // static void nsFrameScriptExecutor::Unlink(nsFrameScriptExecutor* aTmp) { - if (aTmp->mCx) { - JSAutoRequest ar(aTmp->mCx); - JS_SetGlobalObject(aTmp->mCx, nullptr); - } + aTmp->mGlobal = nullptr; } NS_IMPL_ISUPPORTS1(nsScriptCacheCleaner, nsIObserver) diff --git a/content/base/src/nsFrameMessageManager.h b/content/base/src/nsFrameMessageManager.h index ae3b1bf79b4c..71dc772971b0 100644 --- a/content/base/src/nsFrameMessageManager.h +++ b/content/base/src/nsFrameMessageManager.h @@ -257,11 +257,6 @@ private: bool* aValid); }; -void -ContentScriptErrorReporter(JSContext* aCx, - const char* aMessage, - JSErrorReport* aReport); - class nsScriptCacheCleaner; struct nsFrameJSScriptExecutorHolder @@ -284,8 +279,7 @@ public: } protected: friend class nsFrameScriptCx; - nsFrameScriptExecutor() : mCx(nullptr), mCxStackRefCnt(0), - mDelayedCxDestroy(false) + nsFrameScriptExecutor() { MOZ_COUNT_CTOR(nsFrameScriptExecutor); } ~nsFrameScriptExecutor() { MOZ_COUNT_DTOR(nsFrameScriptExecutor); } @@ -301,33 +295,11 @@ protected: nsCycleCollectionTraversalCallback &cb); static void Unlink(nsFrameScriptExecutor* aTmp); nsCOMPtr mGlobal; - JSContext* mCx; - uint32_t mCxStackRefCnt; - bool mDelayedCxDestroy; nsCOMPtr mPrincipal; static nsDataHashtable* sCachedScripts; static nsScriptCacheCleaner* sScriptCacheCleaner; }; -class nsFrameScriptCx -{ -public: - nsFrameScriptCx(nsISupports* aOwner, nsFrameScriptExecutor* aExec) - : mOwner(aOwner), mExec(aExec) - { - ++(mExec->mCxStackRefCnt); - } - ~nsFrameScriptCx() - { - if (--(mExec->mCxStackRefCnt) == 0 && - mExec->mDelayedCxDestroy) { - mExec->DestroyCx(); - } - } - nsCOMPtr mOwner; - nsFrameScriptExecutor* mExec; -}; - class nsScriptCacheCleaner MOZ_FINAL : public nsIObserver { NS_DECL_ISUPPORTS diff --git a/content/base/src/nsInProcessTabChildGlobal.cpp b/content/base/src/nsInProcessTabChildGlobal.cpp index f070c110be38..ca39d918943d 100644 --- a/content/base/src/nsInProcessTabChildGlobal.cpp +++ b/content/base/src/nsInProcessTabChildGlobal.cpp @@ -118,7 +118,6 @@ nsInProcessTabChildGlobal::nsInProcessTabChildGlobal(nsIDocShell* aShell, nsInProcessTabChildGlobal::~nsInProcessTabChildGlobal() { - NS_ASSERTION(!mCx, "Couldn't release JSContext?!?"); } /* [notxpcom] boolean markForCC (); */ diff --git a/dom/ipc/TabChild.cpp b/dom/ipc/TabChild.cpp index ad515e7d398d..bba5111f91af 100644 --- a/dom/ipc/TabChild.cpp +++ b/dom/ipc/TabChild.cpp @@ -1446,7 +1446,6 @@ TabChild::DispatchMessageManagerMessage(const nsAString& aMessageName, } nsCOMPtr kungFuDeathGrip(GetGlobal()); - nsFrameScriptCx frameScriptCx(static_cast(this), this); // Let the BrowserElementScrolling helper (if it exists) for this // content manipulate the frame state. nsRefPtr mm = @@ -2011,7 +2010,6 @@ TabChild::RecvAsyncMessage(const nsString& aMessage, { if (mTabChildGlobal) { nsCOMPtr kungFuDeathGrip(GetGlobal()); - nsFrameScriptCx cx(static_cast(this), this); StructuredCloneData cloneData = UnpackClonedMessageDataForChild(aData); nsRefPtr mm = static_cast(mTabChildGlobal->mMessageManager.get()); diff --git a/dom/ipc/TabChild.h b/dom/ipc/TabChild.h index 937de42e667d..fd781b909148 100644 --- a/dom/ipc/TabChild.h +++ b/dom/ipc/TabChild.h @@ -282,8 +282,6 @@ public: nsIWebNavigation* WebNavigation() { return mWebNav; } - JSContext* GetJSContext() { return mCx; } - nsIPrincipal* GetPrincipal() { return mPrincipal; } /** Return the DPI of the widget this TabChild draws to. */