From 1847a73fead9215d9e1574bc9e823da525a8cb7a Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Fri, 19 Oct 2012 23:26:17 +0200 Subject: [PATCH] Bug 747607 - Remove GetContextFromStack. r=bz --- dom/base/nsLocation.cpp | 90 +++++------------------------------------ 1 file changed, 9 insertions(+), 81 deletions(-) diff --git a/dom/base/nsLocation.cpp b/dom/base/nsLocation.cpp index f03b108e9e44..c3159589e8c1 100644 --- a/dom/base/nsLocation.cpp +++ b/dom/base/nsLocation.cpp @@ -38,36 +38,6 @@ #include "nsContentUtils.h" #include "nsEventStateManager.h" -static nsresult -GetContextFromStack(nsIJSContextStack *aStack, JSContext **aContext) -{ - nsCOMPtr - iterator(do_CreateInstance("@mozilla.org/js/xpc/ContextStackIterator;1")); - NS_ENSURE_TRUE(iterator, NS_ERROR_FAILURE); - - nsresult rv = iterator->Reset(aStack); - NS_ENSURE_SUCCESS(rv, rv); - - bool done; - while (NS_SUCCEEDED(iterator->Done(&done)) && !done) { - rv = iterator->Prev(aContext); - NS_ASSERTION(NS_SUCCEEDED(rv), "Broken iterator implementation"); - - // Consider a null context the end of the line. - if (!*aContext) { - break; - } - - if (nsJSUtils::GetDynamicScriptContext(*aContext)) { - return NS_OK; - } - } - - *aContext = nullptr; - - return NS_OK; -} - static nsresult GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset) { @@ -78,11 +48,7 @@ GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset) nsCOMPtr stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); - JSContext *cx; - - rv = GetContextFromStack(stack, &cx); - NS_ENSURE_SUCCESS(rv, rv); - + JSContext *cx = nsContentUtils::GetCurrentJSContext(); if (cx) { nsCOMPtr window = do_QueryInterface(nsJSUtils::GetDynamicScriptGlobal(cx)); @@ -168,30 +134,14 @@ nsresult nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo) { *aLoadInfo = nullptr; - JSContext* cx; - if ((cx = nsContentUtils::GetCurrentJSContext())) { - nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); - NS_ENSURE_STATE(ssm); - // Check to see if URI is allowed. - nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI); - NS_ENSURE_SUCCESS(rv, rv); - } nsCOMPtr docShell(do_QueryReferent(mDocShell)); NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE); - nsresult rv; - // Get JSContext from stack. - nsCOMPtr - stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv)); - NS_ENSURE_SUCCESS(rv, rv); - - NS_ENSURE_SUCCESS(GetContextFromStack(stack, &cx), NS_ERROR_FAILURE); - nsCOMPtr owner; nsCOMPtr sourceURI; - if (cx) { + if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) { // No cx means that there's no JS running, or at least no JS that // was run through code that properly pushed a context onto the // context stack (as all code that runs JS off of web pages @@ -199,12 +149,11 @@ nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo) // we need to create the loadinfo etc. // Get security manager. - nsCOMPtr - secMan(do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv)); - NS_ENSURE_SUCCESS(rv, rv); + nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); + NS_ENSURE_STATE(ssm); // Check to see if URI is allowed. - rv = secMan->CheckLoadURIFromScript(cx, aURI); + nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI); NS_ENSURE_SUCCESS(rv, rv); // Make the load's referrer reflect changes to the document's URI caused by @@ -241,7 +190,7 @@ nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo) sourceURI = principalURI; } - owner = do_QueryInterface(secMan->GetCxSubjectPrincipal(cx)); + owner = do_QueryInterface(ssm->GetCxSubjectPrincipal(cx)); } // Create load info @@ -534,17 +483,7 @@ nsLocation::SetHref(const nsAString& aHref) nsAutoString oldHref; nsresult rv = NS_OK; - // Get JSContext from stack. - nsCOMPtr - stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv)); - - if (NS_FAILED(rv)) - return NS_ERROR_FAILURE; - - JSContext *cx; - - if (NS_FAILED(GetContextFromStack(stack, &cx))) - return NS_ERROR_FAILURE; + JSContext *cx = nsContentUtils::GetCurrentJSContext(); // According to HTML5 spec, |location.href = ...| must act as if // it were |location.replace(...)| before the page load finishes. @@ -923,19 +862,8 @@ NS_IMETHODIMP nsLocation::Replace(const nsAString& aUrl) { nsresult rv = NS_OK; - - // Get JSContext from stack. - nsCOMPtr - stack(do_GetService("@mozilla.org/js/xpc/ContextStack;1")); - - if (stack) { - JSContext *cx; - - rv = GetContextFromStack(stack, &cx); - NS_ENSURE_SUCCESS(rv, rv); - if (cx) { - return SetHrefWithContext(cx, aUrl, true); - } + if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) { + return SetHrefWithContext(cx, aUrl, true); } nsAutoString oldHref;