Bug 796938 - Switch nsHTMLDocument::Open and XMLDocument::Load to a new GetEntryDocument API and remove nsContentUtils::GetDocumentFromContext. r=smaug
authorBobby Holley <bobbyholley@gmail.com>
Tue, 19 Aug 2014 12:02:06 -0700
changeset 214755 768cb6193db5a31a86e72e52dc9977bc5ad380ff
parent 214754 5d6a67963f48ad5c212fe7d7c88b1db9acb6700c
child 214756 8cff1cd635f9b96c835cf8847564414fb3395513
push idunknown
push userunknown
push dateunknown
reviewerssmaug
bugs796938
milestone34.0a1
Bug 796938 - Switch nsHTMLDocument::Open and XMLDocument::Load to a new GetEntryDocument API and remove nsContentUtils::GetDocumentFromContext. r=smaug
content/base/public/nsContentUtils.h
content/base/src/nsContentUtils.cpp
content/html/document/src/nsHTMLDocument.cpp
dom/base/ScriptSettings.cpp
dom/base/ScriptSettings.h
dom/xml/XMLDocument.cpp
--- a/content/base/public/nsContentUtils.h
+++ b/content/base/public/nsContentUtils.h
@@ -400,48 +400,27 @@ public:
   static bool CanCallerAccess(nsIDOMNode *aNode);
   static bool CanCallerAccess(nsINode* aNode);
 
   // Check if the (JS) caller can access aWindow.
   // aWindow can be either outer or inner window.
   static bool CanCallerAccess(nsPIDOMWindow* aWindow);
 
   /**
-   * The two GetDocumentFrom* functions below allow a caller to get at a
-   * document that is relevant to the currently executing script.
-   *
    * GetDocumentFromCaller gets its document by looking at the last called
    * function and finding the document that the function itself relates to.
    * For example, consider two windows A and B in the same origin. B has a
    * function which does something that ends up needing the current document.
    * If a script in window A were to call B's function, GetDocumentFromCaller
    * would find that function (in B) and return B's document.
    *
-   * GetDocumentFromContext gets its document by looking at the currently
-   * executing context's global object and returning its document. Thus,
-   * given the example above, GetDocumentFromCaller would see that the
-   * currently executing script was in window A, and return A's document.
-   */
-  /**
-   * Get the document from the currently executing function. This will return
-   * the document that the currently executing function is in/from.
-   *
    * @return The document or null if no JS Context.
    */
   static nsIDocument* GetDocumentFromCaller();
 
-  /**
-   * Get the document through the JS context that's currently on the stack.
-   * If there's no JS context currently on the stack it will return null.
-   * This will return the document of the calling script.
-   *
-   * @return The document or null if no JS context
-   */
-  static nsIDocument* GetDocumentFromContext();
-
   // Check if a node is in the document prolog, i.e. before the document
   // element.
   static bool InProlog(nsINode *aNode);
 
   static nsIParserService* GetParserService();
 
   static nsNameSpaceManager* NameSpaceManager()
   {
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -1962,34 +1962,16 @@ nsContentUtils::GetDocumentFromCaller()
     do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(JS::CurrentGlobalOrNull(cx)));
   if (!win) {
     return nullptr;
   }
 
   return win->GetExtantDoc();
 }
 
-nsIDocument*
-nsContentUtils::GetDocumentFromContext()
-{
-  JSContext *cx = GetCurrentJSContext();
-  if (cx) {
-    nsIScriptGlobalObject *sgo = nsJSUtils::GetDynamicScriptGlobal(cx);
-
-    if (sgo) {
-      nsCOMPtr<nsPIDOMWindow> pwin = do_QueryInterface(sgo);
-      if (pwin) {
-        return pwin->GetExtantDoc();
-      }
-    }
-  }
-
-  return nullptr;
-}
-
 bool
 nsContentUtils::IsCallerChrome()
 {
   MOZ_ASSERT(NS_IsMainThread());
   if (SubjectPrincipal() == sSystemPrincipal) {
     return true;
   }
 
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -1424,20 +1424,20 @@ nsHTMLDocument::Open(JSContext* cx,
 
   bool inUnload;
   shell->GetIsInUnload(&inUnload);
   if (inUnload) {
     nsCOMPtr<nsIDocument> ret = this;
     return ret.forget();
   }
 
-  // Note: We want to use GetDocumentFromContext here because this document
+  // Note: We want to use GetEntryDocument here because this document
   // should inherit the security information of the document that's opening us,
   // (since if it's secure, then it's presumably trusted).
-  nsCOMPtr<nsIDocument> callerDoc = nsContentUtils::GetDocumentFromContext();
+  nsCOMPtr<nsIDocument> callerDoc = GetEntryDocument();
   if (!callerDoc) {
     // If we're called from C++ or in some other way without an originating
     // document we can't do a document.open w/o changing the principal of the
     // document to something like about:blank (as that's the only sane thing to
     // do when we don't know the origin of this call), and since we can't
     // change the principals of a document for security reasons we'll have to
     // refuse to go ahead with this call.
 
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -123,16 +123,23 @@ ScriptSettingsStackEntry::~ScriptSetting
 }
 
 nsIGlobalObject*
 GetEntryGlobal()
 {
   return ScriptSettingsStack::EntryGlobal();
 }
 
+nsIDocument*
+GetEntryDocument()
+{
+  nsCOMPtr<nsPIDOMWindow> entryWin = do_QueryInterface(GetEntryGlobal());
+  return entryWin ? entryWin->GetExtantDoc() : nullptr;
+}
+
 nsIGlobalObject*
 GetIncumbentGlobal()
 {
   // We need the current JSContext in order to check the JS for
   // scripted frames that may have appeared since anyone last
   // manipulated the stack. If it's null, that means that there
   // must be no entry global on the stack, and therefore no incumbent
   // global either.
--- a/dom/base/ScriptSettings.h
+++ b/dom/base/ScriptSettings.h
@@ -15,16 +15,17 @@
 
 #include "mozilla/Maybe.h"
 
 #include "jsapi.h"
 
 class nsPIDOMWindow;
 class nsGlobalWindow;
 class nsIScriptContext;
+class nsIDocument;
 
 namespace mozilla {
 namespace dom {
 
 // For internal use only - use AutoJSAPI instead.
 namespace danger {
 
 /**
@@ -98,16 +99,20 @@ void DestroyScriptSettings();
 //
 // Note that all of these functions return bonafide global objects. This means
 // that, for Windows, they always return the inner.
 
 // Returns the global associated with the top-most Candidate Entry Point on
 // the Script Settings Stack. See the HTML spec. This may be null.
 nsIGlobalObject* GetEntryGlobal();
 
+// If the entry global is a window, returns its extant document. Otherwise,
+// returns null.
+nsIDocument* GetEntryDocument();
+
 // Returns the global associated with the top-most entry of the the Script
 // Settings Stack. See the HTML spec. This may be null.
 nsIGlobalObject* GetIncumbentGlobal();
 
 // Returns the global associated with the current compartment. This may be null.
 nsIGlobalObject* GetCurrentGlobal();
 
 // JS-implemented WebIDL presents an interesting situation with respect to the
--- a/dom/xml/XMLDocument.cpp
+++ b/dom/xml/XMLDocument.cpp
@@ -297,18 +297,17 @@ XMLDocument::Load(const nsAString& aUrl,
     GetScriptHandlingObject(hasHadScriptObject);
   if (!scriptObject && hasHadScriptObject) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return false;
   }
 
   WarnOnceAbout(nsIDocument::eUseOfDOM3LoadMethod);
 
-  nsCOMPtr<nsIDocument> callingDoc = nsContentUtils::GetDocumentFromContext();
-
+  nsCOMPtr<nsIDocument> callingDoc = GetEntryDocument();
   nsIURI *baseURI = mDocumentURI;
   nsAutoCString charset;
 
   if (callingDoc) {
     baseURI = callingDoc->GetDocBaseURI();
     charset = callingDoc->GetDocumentCharacterSet();
   }