Bug 861729 - Remove nsPIDOMWindow::GetExtantDocument r=Ms2ger
authorDavid Zbarsky <dzbarsky@gmail.com>
Wed, 24 Apr 2013 00:22:37 -0400
changeset 140659 95262d6936423d885c738e4481679e457f6ff9f8
parent 140658 96c0eae610a29d7cffa7581f1e54018368fd26ed
child 140660 787e5340d7afdd8579d77d5ccebcf72a0871d944
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
bugs861729
milestone23.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 861729 - Remove nsPIDOMWindow::GetExtantDocument r=Ms2ger
content/base/public/nsContentUtils.h
content/base/src/nsContentUtils.cpp
content/base/src/nsDOMFile.cpp
content/base/src/nsDOMMutationObserver.h
content/base/src/nsDocument.cpp
content/base/src/nsFrameLoader.cpp
content/base/src/nsXMLHttpRequest.cpp
content/events/src/nsDOMEvent.cpp
content/events/src/nsDOMEventTargetHelper.cpp
content/events/src/nsEventDispatcher.cpp
content/events/src/nsEventListenerManager.cpp
content/events/src/nsEventStateManager.cpp
content/html/content/src/HTMLImageElement.cpp
content/html/content/src/HTMLInputElement.cpp
content/html/content/src/HTMLOptionElement.cpp
content/html/document/src/nsHTMLDocument.cpp
content/xml/document/src/XMLDocument.cpp
content/xul/templates/src/nsXULTemplateBuilder.cpp
docshell/base/nsDocShell.cpp
dom/base/Navigator.cpp
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMException.cpp
dom/base/nsDOMWindowUtils.cpp
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
dom/base/nsGlobalWindowCommands.cpp
dom/base/nsHistory.cpp
dom/base/nsJSEnvironment.cpp
dom/base/nsJSTimeoutHandler.cpp
dom/base/nsLocation.cpp
dom/base/nsPIDOMWindow.h
dom/base/nsWindowMemoryReporter.cpp
dom/indexedDB/IDBDatabase.h
dom/media/MediaManager.cpp
dom/power/WakeLock.cpp
dom/src/jsurl/nsJSProtocolHandler.cpp
dom/workers/WorkerPrivate.cpp
embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
embedding/components/windowwatcher/src/nsWindowWatcher.cpp
js/xpconnect/src/XPCComponents.cpp
layout/base/nsDocumentViewer.cpp
layout/base/nsPresShell.cpp
--- a/content/base/public/nsContentUtils.h
+++ b/content/base/public/nsContentUtils.h
@@ -420,26 +420,26 @@ public:
    * 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 nsIDOMDocument *GetDocumentFromCaller();
+  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 nsIDOMDocument *GetDocumentFromContext();
+  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 nsINameSpaceManager* NameSpaceManager()
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -1704,48 +1704,48 @@ nsContentUtils::GetWindowFromCaller()
     nsCOMPtr<nsPIDOMWindow> win =
       do_QueryInterface(nsJSUtils::GetDynamicScriptGlobal(cx));
     return win;
   }
 
   return nullptr;
 }
 
-nsIDOMDocument *
+nsIDocument*
 nsContentUtils::GetDocumentFromCaller()
 {
   JSContext *cx = nullptr;
   JSObject *obj = nullptr;
   sXPConnect->GetCaller(&cx, &obj);
   NS_ASSERTION(cx && obj, "Caller ensures something is running");
 
   JSAutoCompartment ac(cx, obj);
 
   nsCOMPtr<nsPIDOMWindow> win =
     do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(obj));
   if (!win) {
     return nullptr;
   }
 
-  return win->GetExtantDocument();
-}
-
-nsIDOMDocument *
+  return win->GetExtantDoc();
+}
+
+nsIDocument*
 nsContentUtils::GetDocumentFromContext()
 {
   JSContext *cx = nullptr;
   sThreadJSContextStack->Peek(&cx);
 
   if (cx) {
     nsIScriptGlobalObject *sgo = nsJSUtils::GetDynamicScriptGlobal(cx);
 
     if (sgo) {
       nsCOMPtr<nsPIDOMWindow> pwin = do_QueryInterface(sgo);
       if (pwin) {
-        return pwin->GetExtantDocument();
+        return pwin->GetExtantDoc();
       }
     }
   }
 
   return nullptr;
 }
 
 bool
--- a/content/base/src/nsDOMFile.cpp
+++ b/content/base/src/nsDOMFile.cpp
@@ -255,33 +255,32 @@ nsDOMFileBase::MozSlice(int64_t aStart, 
                         nsIDOMBlob **aBlob)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsIScriptGlobalObject* sgo = nsJSUtils::GetDynamicScriptGlobal(aCx);
   if (sgo) {
     nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(sgo);
     if (window) {
-      nsCOMPtr<nsIDocument> document =
-        do_QueryInterface(window->GetExtantDocument());
+      nsCOMPtr<nsIDocument> document = window->GetExtantDoc();
       if (document) {
         document->WarnOnceAbout(nsIDocument::eMozSlice);
       }
     }
   }
 
   return Slice(aStart, aEnd, aContentType, optional_argc, aBlob);
 }
 
 NS_IMETHODIMP
 nsDOMFileBase::GetInternalStream(nsIInputStream **aStream)
 {
   // Must be overridden
   NS_NOTREACHED("Must override GetInternalStream");
-  
+
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 NS_IMETHODIMP
 nsDOMFileBase::GetInternalUrl(nsIPrincipal* aPrincipal, nsAString& aURL)
 {
   NS_ENSURE_STATE(aPrincipal);
 
--- a/content/base/src/nsDOMMutationObserver.h
+++ b/content/base/src/nsDOMMutationObserver.h
@@ -403,17 +403,17 @@ protected:
   void RescheduleForRun();
 
   nsDOMMutationRecord* CurrentRecord(const nsAString& aType);
   bool HasCurrentRecord(const nsAString& aType);
 
   bool Suppressed()
   {
     if (mOwner) {
-      nsCOMPtr<nsIDocument> d = do_QueryInterface(mOwner->GetExtantDocument());
+      nsCOMPtr<nsIDocument> d = mOwner->GetExtantDoc();
       return d && d->IsInSyncOperation();
     }
     return false;
   }
 
   static void HandleMutationsInternal();
 
   static void AddCurrentlyHandlingObserver(nsDOMMutationObserver* aObserver);
--- a/content/base/src/nsDocument.cpp
+++ b/content/base/src/nsDocument.cpp
@@ -11231,17 +11231,17 @@ nsAutoSyncOperation::nsAutoSyncOperation
   nsContentUtils::SetMicroTaskLevel(0);
   if (aDoc) {
     nsPIDOMWindow* win = aDoc->GetWindow();
     if (win) {
       nsCOMPtr<nsIDOMWindow> topWindow;
       win->GetTop(getter_AddRefs(topWindow));
       nsCOMPtr<nsPIDOMWindow> top = do_QueryInterface(topWindow);
       if (top) {
-        nsCOMPtr<nsIDocument> doc = do_QueryInterface(top->GetExtantDocument());
+        nsCOMPtr<nsIDocument> doc = top->GetExtantDoc();
         MarkDocumentTreeToBeInSyncOperation(doc, &mDocuments);
       }
     }
   }
 }
 
 nsAutoSyncOperation::~nsAutoSyncOperation()
 {
--- a/content/base/src/nsFrameLoader.cpp
+++ b/content/base/src/nsFrameLoader.cpp
@@ -1090,20 +1090,18 @@ nsFrameLoader::SwapWithOtherLoader(nsFra
     do_QueryInterface(otherWindow->GetChromeEventHandler());
 
   NS_ASSERTION(SameCOMIdentity(ourFrameElement, ourContent) &&
                SameCOMIdentity(otherFrameElement, otherContent) &&
                SameCOMIdentity(ourChromeEventHandler, ourContent) &&
                SameCOMIdentity(otherChromeEventHandler, otherContent),
                "How did that happen, exactly?");
 
-  nsCOMPtr<nsIDocument> ourChildDocument =
-    do_QueryInterface(ourWindow->GetExtantDocument());
-  nsCOMPtr<nsIDocument> otherChildDocument =
-    do_QueryInterface(otherWindow->GetExtantDocument());
+  nsCOMPtr<nsIDocument> ourChildDocument = ourWindow->GetExtantDoc();
+  nsCOMPtr<nsIDocument> otherChildDocument = otherWindow ->GetExtantDoc();
   if (!ourChildDocument || !otherChildDocument) {
     // This shouldn't be happening
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
   nsCOMPtr<nsIDocument> ourParentDocument =
     ourChildDocument->GetParentDocument();
   nsCOMPtr<nsIDocument> otherParentDocument =
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -385,17 +385,17 @@ nsXMLHttpRequest::InitParameters(bool aA
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(GetOwner());
   if (!window || !window->GetDocShell()) {
     return;
   }
 
   // Chrome is always allowed access, so do the permission check only
   // for non-chrome pages.
   if (!nsContentUtils::IsCallerChrome()) {
-    nsCOMPtr<nsIDocument> doc = do_QueryInterface(window->GetExtantDocument());
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
     if (!doc) {
       return;
     }
 
     nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
     nsCOMPtr<nsIPermissionManager> permMgr =
       do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
     if (!permMgr)
@@ -555,17 +555,17 @@ nsXMLHttpRequest::GetChannel(nsIChannel 
 
   return NS_OK;
 }
 
 static void LogMessage(const char* aWarning, nsPIDOMWindow* aWindow)
 {
   nsCOMPtr<nsIDocument> doc;
   if (aWindow) {
-    doc = do_QueryInterface(aWindow->GetExtantDocument());
+    doc = aWindow->GetExtantDoc();
   }
   nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
                                   "DOM", doc,
                                   nsContentUtils::eDOM_PROPERTIES,
                                   aWarning);
 }
 
 /* readonly attribute nsIDOMDocument responseXML; */
@@ -2856,17 +2856,17 @@ nsXMLHttpRequest::Send(nsIVariant* aVari
     nsCOMPtr<nsIDocument> suspendedDoc;
     nsCOMPtr<nsIRunnable> resumeTimeoutRunnable;
     if (GetOwner()) {
       nsCOMPtr<nsIDOMWindow> topWindow;
       if (NS_SUCCEEDED(GetOwner()->GetTop(getter_AddRefs(topWindow)))) {
         nsCOMPtr<nsPIDOMWindow> suspendedWindow(do_QueryInterface(topWindow));
         if (suspendedWindow &&
             (suspendedWindow = suspendedWindow->GetCurrentInnerWindow())) {
-          suspendedDoc = do_QueryInterface(suspendedWindow->GetExtantDocument());
+          suspendedDoc = suspendedWindow->GetExtantDoc();
           if (suspendedDoc) {
             suspendedDoc->SuppressEventHandling();
           }
           suspendedWindow->SuspendTimeouts(1, false);
           resumeTimeoutRunnable = new nsResumeTimeoutsEvent(suspendedWindow);
         }
       }
     }
--- a/content/events/src/nsDOMEvent.cpp
+++ b/content/events/src/nsDOMEvent.cpp
@@ -316,17 +316,17 @@ NS_IMETHODIMP
 nsDOMEvent::Initialize(nsISupports* aOwner, JSContext* aCx, JSObject* aObj,
                        uint32_t aArgc, JS::Value* aArgv)
 {
   NS_ENSURE_TRUE(aArgc >= 1, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
 
   bool trusted = false;
   nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aOwner);
   if (w) {
-    nsCOMPtr<nsIDocument> d = do_QueryInterface(w->GetExtantDocument());
+    nsCOMPtr<nsIDocument> d = w->GetExtantDoc();
     if (d) {
       trusted = nsContentUtils::IsChromeDoc(d);
       nsIPresShell* s = d->GetShell();
       if (s) {
         InitPresContextData(s->GetPresContext());
       }
     }
   }
@@ -364,17 +364,17 @@ nsDOMEvent::InitFromCtor(const nsAString
 }
 
 bool
 nsDOMEvent::Init(mozilla::dom::EventTarget* aGlobal)
 {
   bool trusted = false;
   nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal);
   if (w) {
-    nsCOMPtr<nsIDocument> d = do_QueryInterface(w->GetExtantDocument());
+    nsCOMPtr<nsIDocument> d = w->GetExtantDoc();
     if (d) {
       trusted = nsContentUtils::IsChromeDoc(d);
       nsIPresShell* s = d->GetShell();
       if (s) {
         InitPresContextData(s->GetPresContext());
       }
     }
   }
@@ -516,17 +516,17 @@ nsDOMEvent::PreventDefault()
     mEvent->mFlags.mDefaultPrevented = true;
 
     // Need to set an extra flag for drag events.
     if (mEvent->eventStructType == NS_DRAG_EVENT && IsTrusted()) {
       nsCOMPtr<nsINode> node = do_QueryInterface(mEvent->currentTarget);
       if (!node) {
         nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(mEvent->currentTarget);
         if (win) {
-          node = do_QueryInterface(win->GetExtantDocument());
+          node = win->GetExtantDoc();
         }
       }
       if (node && !nsContentUtils::IsChromeDoc(node->OwnerDoc())) {
         mEvent->mFlags.mDefaultPreventedByContent = true;
       }
     }
   }
 
--- a/content/events/src/nsDOMEventTargetHelper.cpp
+++ b/content/events/src/nsDOMEventTargetHelper.cpp
@@ -21,18 +21,18 @@ using namespace mozilla::dom;
 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMEventTargetHelper)
   NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
 NS_IMPL_CYCLE_COLLECTION_TRACE_END
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsDOMEventTargetHelper)
   if (MOZ_UNLIKELY(cb.WantDebugInfo())) {
     char name[512];
     nsAutoString uri;
-    if (tmp->mOwnerWindow && tmp->mOwnerWindow->GetExtantDocument()) {
-      tmp->mOwnerWindow->GetExtantDocument()->GetDocumentURI(uri);
+    if (tmp->mOwnerWindow && tmp->mOwnerWindow->GetExtantDoc()) {
+      tmp->mOwnerWindow->GetExtantDoc()->GetDocumentURI(uri);
     }
     PR_snprintf(name, sizeof(name), "nsDOMEventTargetHelper %s",
                 NS_ConvertUTF16toUTF8(uri).get());
     cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name);
   } else {
     NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsDOMEventTargetHelper, tmp->mRefCnt.get())
   }
 
--- a/content/events/src/nsEventDispatcher.cpp
+++ b/content/events/src/nsEventDispatcher.cpp
@@ -458,17 +458,17 @@ nsEventDispatcher::Dispatch(nsISupports*
     }
   }
 
   if (aEvent->mFlags.mOnlyChromeDispatch) {
     nsCOMPtr<nsINode> node = do_QueryInterface(aTarget);
     if (!node) {
       nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aTarget);
       if (win) {
-        node = do_QueryInterface(win->GetExtantDocument());
+        node = win->GetExtantDoc();
       }
     }
 
     NS_ENSURE_STATE(node);
     nsIDocument* doc = node->OwnerDoc();
     if (!nsContentUtils::IsChromeDoc(doc)) {
       nsPIDOMWindow* win = doc ? doc->GetInnerWindow() : nullptr;
       // If we can't dispatch the event to chrome, do nothing.
--- a/content/events/src/nsEventListenerManager.cpp
+++ b/content/events/src/nsEventListenerManager.cpp
@@ -277,17 +277,17 @@ nsEventListenerManager::AddEventListener
     }
   } else if (aType >= NS_MUTATION_START && aType <= NS_MUTATION_END) {
     // For mutation listeners, we need to update the global bit on the DOM window.
     // Otherwise we won't actually fire the mutation event.
     mMayHaveMutationListeners = true;
     // Go from our target to the nearest enclosing DOM window.
     nsPIDOMWindow* window = GetInnerWindowForTarget();
     if (window) {
-      nsCOMPtr<nsIDocument> doc = do_QueryInterface(window->GetExtantDocument());
+      nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
       if (doc) {
         doc->WarnOnceAbout(nsIDocument::eMutationEvent);
       }
       // If aType is NS_MUTATION_SUBTREEMODIFIED, we need to listen all
       // mutations. nsContentUtils::HasMutationListeners relies on this.
       window->SetMutationListeners((aType == NS_MUTATION_SUBTREEMODIFIED) ?
                                    kAllMutationBits :
                                    MutationBitForEventType(aType));
@@ -331,17 +331,17 @@ nsEventListenerManager::AddEventListener
       window->SetHasTouchEventListeners();
     }
   } else if (aTypeAtom == nsGkAtoms::onmouseenter ||
              aTypeAtom == nsGkAtoms::onmouseleave) {
     mMayHaveMouseEnterLeaveEventListener = true;
     nsPIDOMWindow* window = GetInnerWindowForTarget();
     if (window) {
 #ifdef DEBUG
-      nsCOMPtr<nsIDocument> d = do_QueryInterface(window->GetExtantDocument());
+      nsCOMPtr<nsIDocument> d = window->GetExtantDoc();
       NS_WARN_IF_FALSE(!nsContentUtils::IsChromeDoc(d),
                        "Please do not use mouseenter/leave events in chrome. "
                        "They are slower than mouseover/out!");
 #endif
       window->SetHasMouseEnterLeaveEventListeners();
     }
 #ifdef MOZ_GAMEPAD
   } else if (aType >= NS_GAMEPAD_START &&
@@ -823,17 +823,17 @@ nsEventListenerManager::CompileEventHand
     uint32_t lineNo = 0;
     nsAutoCString url (NS_LITERAL_CSTRING("-moz-evil:lying-event-listener"));
     nsCOMPtr<nsIDocument> doc;
     if (content) {
       doc = content->OwnerDoc();
     } else {
       win = do_QueryInterface(mTarget);
       if (win) {
-        doc = do_QueryInterface(win->GetExtantDocument());
+        doc = win->GetExtantDoc();
       }
     }
 
     if (doc) {
       nsIURI *uri = doc->GetDocumentURI();
       if (uri) {
         uri->GetSpec(url);
         lineNo = 1;
--- a/content/events/src/nsEventStateManager.cpp
+++ b/content/events/src/nsEventStateManager.cpp
@@ -259,23 +259,17 @@ nsUITimerCallback::Notify(nsITimer* aTim
 #define NS_MODIFIER_ALT      4
 #define NS_MODIFIER_META     8
 #define NS_MODIFIER_OS       16
 
 static nsIDocument *
 GetDocumentFromWindow(nsIDOMWindow *aWindow)
 {
   nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aWindow);
-  nsCOMPtr<nsIDocument> doc;
-
-  if (win) {
-    doc = do_QueryInterface(win->GetExtantDocument());
-  }
-
-  return doc;
+  return win ? win->GetExtantDoc() : nullptr;
 }
 
 static int32_t
 GetAccessModifierMaskFromPref(int32_t aItemType)
 {
   int32_t accessKey = Preferences::GetInt("ui.key.generalAccessKey", -1);
   switch (accessKey) {
     case -1:                             break; // use the individual prefs
@@ -3251,17 +3245,17 @@ nsEventStateManager::PostHandleEvent(nsP
           if (currentWindow && mDocument->GetWindow() &&
               currentWindow != mDocument->GetWindow() &&
               !nsContentUtils::IsChromeDoc(mDocument)) {
             nsCOMPtr<nsIDOMWindow> currentTop;
             nsCOMPtr<nsIDOMWindow> newTop;
             currentWindow->GetTop(getter_AddRefs(currentTop));
             mDocument->GetWindow()->GetTop(getter_AddRefs(newTop));
             nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(currentWindow);
-            nsCOMPtr<nsIDocument> currentDoc = do_QueryInterface(win->GetExtantDocument());
+            nsCOMPtr<nsIDocument> currentDoc = win->GetExtantDoc();
             if (nsContentUtils::IsChromeDoc(currentDoc) ||
                 (currentTop && newTop && currentTop != newTop)) {
               fm->SetFocusedWindow(mDocument->GetWindow());
             }
           }
         }
       }
       SetActiveManager(this, activeContent);
--- a/content/html/content/src/HTMLImageElement.cpp
+++ b/content/html/content/src/HTMLImageElement.cpp
@@ -47,18 +47,17 @@ NS_NewHTMLImageElement(already_AddRefed<
 {
   /*
    * HTMLImageElement's will be created without a nsINodeInfo passed in
    * if someone says "var img = new Image();" in JavaScript, in a case like
    * that we request the nsINodeInfo from the document's nodeinfo list.
    */
   nsCOMPtr<nsINodeInfo> nodeInfo(aNodeInfo);
   if (!nodeInfo) {
-    nsCOMPtr<nsIDocument> doc =
-      do_QueryInterface(nsContentUtils::GetDocumentFromCaller());
+    nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromCaller();
     NS_ENSURE_TRUE(doc, nullptr);
 
     nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::img, nullptr,
                                                    kNameSpaceID_XHTML,
                                                    nsIDOMNode::ELEMENT_NODE);
   }
 
   return new mozilla::dom::HTMLImageElement(nodeInfo.forget());
--- a/content/html/content/src/HTMLInputElement.cpp
+++ b/content/html/content/src/HTMLInputElement.cpp
@@ -391,18 +391,17 @@ HTMLInputElement::AsyncClickHandler::Run
 
     if (!pm) {
       return NS_OK;
     }
 
     uint32_t permission;
     pm->TestPermission(doc->NodePrincipal(), &permission);
     if (permission == nsIPopupWindowManager::DENY_POPUP) {
-      nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
-      nsGlobalWindow::FirePopupBlockedEvent(domDoc, win, nullptr, EmptyString(), EmptyString());
+      nsGlobalWindow::FirePopupBlockedEvent(doc, win, nullptr, EmptyString(), EmptyString());
       return NS_OK;
     }
   }
 
   // Get Loc title
   nsXPIDLString title;
   nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                      "FileUpload", title);
--- a/content/html/content/src/HTMLOptionElement.cpp
+++ b/content/html/content/src/HTMLOptionElement.cpp
@@ -38,18 +38,17 @@ NS_NewHTMLOptionElement(already_AddRefed
 {
   /*
    * HTMLOptionElement's will be created without a nsINodeInfo passed in
    * if someone says "var opt = new Option();" in JavaScript, in a case like
    * that we request the nsINodeInfo from the document's nodeinfo list.
    */
   nsCOMPtr<nsINodeInfo> nodeInfo(aNodeInfo);
   if (!nodeInfo) {
-    nsCOMPtr<nsIDocument> doc =
-      do_QueryInterface(nsContentUtils::GetDocumentFromCaller());
+    nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromCaller();
     NS_ENSURE_TRUE(doc, nullptr);
 
     nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::option, nullptr,
                                                    kNameSpaceID_XHTML,
                                                    nsIDOMNode::ELEMENT_NODE);
   }
 
   return new mozilla::dom::HTMLOptionElement(nodeInfo.forget());
--- a/content/html/document/src/nsHTMLDocument.cpp
+++ b/content/html/document/src/nsHTMLDocument.cpp
@@ -1499,18 +1499,17 @@ nsHTMLDocument::Open(JSContext* cx,
   if (inUnload) {
     NS_ADDREF_THIS();
     return this;
   }
 
   // Note: We want to use GetDocumentFromContext 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 =
-    do_QueryInterface(nsContentUtils::GetDocumentFromContext());
+  nsCOMPtr<nsIDocument> callerDoc = nsContentUtils::GetDocumentFromContext();
   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/content/xml/document/src/XMLDocument.cpp
+++ b/content/xml/document/src/XMLDocument.cpp
@@ -315,18 +315,17 @@ XMLDocument::Load(const nsAString& aUrl,
     GetScriptHandlingObject(hasHadScriptObject);
   if (!scriptObject && hasHadScriptObject) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return false;
   }
 
   ReportUseOfDeprecatedMethod(this, "UseOfDOM3LoadMethodWarning");
 
-  nsCOMPtr<nsIDocument> callingDoc =
-    do_QueryInterface(nsContentUtils::GetDocumentFromContext());
+  nsCOMPtr<nsIDocument> callingDoc = nsContentUtils::GetDocumentFromContext();
 
   nsIURI *baseURI = mDocumentURI;
   nsAutoCString charset;
 
   if (callingDoc) {
     baseURI = callingDoc->GetDocBaseURI();
     charset = callingDoc->GetDocumentCharacterSet();
   }
--- a/content/xul/templates/src/nsXULTemplateBuilder.cpp
+++ b/content/xul/templates/src/nsXULTemplateBuilder.cpp
@@ -1069,18 +1069,17 @@ nsXULTemplateBuilder::Observe(nsISupport
                               const char* aTopic,
                               const PRUnichar* aData)
 {
     // Uuuuber hack to clean up circular references that the cycle collector
     // doesn't know about. See bug 394514.
     if (!strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC)) {
         nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aSubject);
         if (window) {
-            nsCOMPtr<nsIDocument> doc =
-                do_QueryInterface(window->GetExtantDocument());
+            nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
             if (doc && doc == mObservedDocument)
                 NodeWillBeDestroyed(doc);
         }
     } else if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
         UninitTrue();
     }
     return NS_OK;
 }
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -7043,20 +7043,17 @@ nsDocShell::CanSavePresentation(uint32_t
         GetSameTypeParent(getter_AddRefs(root));
         if (root && root != this) {
             return false;  // this is a subframe load
         }
     }
 
     // If the document does not want its presentation cached, then don't.
     nsCOMPtr<nsIDocument> doc = mScriptGlobal->GetExtantDoc();
-    if (!doc || !doc->CanSavePresentation(aNewRequest))
-        return false;
-
-    return true;
+    return doc && doc->CanSavePresentation(aNewRequest);
 }
 
 void
 nsDocShell::ReattachEditorToWindow(nsISHEntry *aSHEntry)
 {
     NS_ASSERTION(!mEditorData,
                  "Why reattach an editor when we already have one?");
     NS_ASSERTION(aSHEntry && aSHEntry->HasDetachedEditor(),
@@ -8672,18 +8669,17 @@ nsDocShell::InternalLoad(nsIURI * aURI,
                                      EmptyString(), // Features
                                      getter_AddRefs(newWin));
 
             // In some cases the Open call doesn't actually result in a new
             // window being opened.  We can detect these cases by examining the
             // document in |newWin|, if any.
             nsCOMPtr<nsPIDOMWindow> piNewWin = do_QueryInterface(newWin);
             if (piNewWin) {
-                nsCOMPtr<nsIDocument> newDoc =
-                    do_QueryInterface(piNewWin->GetExtantDocument());
+                nsCOMPtr<nsIDocument> newDoc = piNewWin->GetExtantDoc();
                 if (!newDoc || newDoc->IsInitialDocument()) {
                     isNewWindow = true;
                     aFlags |= INTERNAL_LOAD_FLAGS_FIRST_LOAD;
                 }
             }
 
             nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(newWin);
             targetDocShell = do_QueryInterface(webNav);
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -476,17 +476,17 @@ Navigator::GetCookieEnabled(bool* aCooki
   // Note that the code for getting the URI here matches that in
   // nsHTMLDocument::SetCookie.
   nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mWindow));
 
   if (!win || !win->GetDocShell()) {
     return NS_OK;
   }
 
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(win->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
   if (!doc) {
     return NS_OK;
   }
 
   nsCOMPtr<nsIURI> codebaseURI;
   doc->NodePrincipal()->GetURI(getter_AddRefs(codebaseURI));
 
   if (!codebaseURI) {
@@ -621,27 +621,26 @@ Navigator::HasDesktopNotificationSupport
   return Preferences::GetBool("notification.feature.enabled", false);
 }
 
 namespace {
 
 class VibrateWindowListener : public nsIDOMEventListener
 {
 public:
-  VibrateWindowListener(nsIDOMWindow *aWindow, nsIDOMDocument *aDocument)
+  VibrateWindowListener(nsIDOMWindow* aWindow, nsIDocument* aDocument)
   {
     mWindow = do_GetWeakReference(aWindow);
     mDocument = do_GetWeakReference(aDocument);
 
-    nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(aDocument);
     NS_NAMED_LITERAL_STRING(visibilitychange, "visibilitychange");
-    target->AddSystemEventListener(visibilitychange,
-                                   this, /* listener */
-                                   true, /* use capture */
-                                   false /* wants untrusted */);
+    aDocument->AddSystemEventListener(visibilitychange,
+                                      this, /* listener */
+                                      true, /* use capture */
+                                      false /* wants untrusted */);
   }
 
   virtual ~VibrateWindowListener()
   {
   }
 
   void RemoveListener();
 
@@ -749,22 +748,19 @@ Navigator::RemoveIdleObserver(nsIIdleObs
 }
 
 NS_IMETHODIMP
 Navigator::Vibrate(const JS::Value& aPattern, JSContext* cx)
 {
   nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mWindow);
   NS_ENSURE_TRUE(win, NS_OK);
 
-  nsCOMPtr<nsIDOMDocument> domDoc = win->GetExtantDocument();
-  NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
-
-  bool hidden = true;
-  domDoc->GetHidden(&hidden);
-  if (hidden) {
+  nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
+  NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
+  if (doc->Hidden()) {
     // Hidden documents cannot start or stop a vibration.
     return NS_OK;
   }
 
   nsAutoTArray<uint32_t, 8> pattern;
 
   // null or undefined pattern is an error.
   if (JSVAL_IS_NULL(aPattern) || JSVAL_IS_VOID(aPattern)) {
@@ -814,17 +810,17 @@ Navigator::Vibrate(const JS::Value& aPat
     // If gVibrateWindowListener is null, this is the first time we've vibrated,
     // and we need to register a listener to clear gVibrateWindowListener on
     // shutdown.
     ClearOnShutdown(&gVibrateWindowListener);
   }
   else {
     gVibrateWindowListener->RemoveListener();
   }
-  gVibrateWindowListener = new VibrateWindowListener(win, domDoc);
+  gVibrateWindowListener = new VibrateWindowListener(win, doc);
 
   nsCOMPtr<nsIDOMWindow> domWindow =
     do_QueryInterface(static_cast<nsIDOMWindow*>(win));
   hal::Vibrate(pattern, domWindow);
   return NS_OK;
 }
 
 //*****************************************************************************
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -3263,17 +3263,17 @@ nsWindowSH::GlobalScopePolluterNewResolv
 
     return JS_TRUE;
   }
 
   //
   // The rest of this function is for HTML documents only.
   //
   nsCOMPtr<nsIHTMLDocument> htmlDoc =
-    do_QueryInterface(win->GetExtantDocument());
+    do_QueryInterface(win->GetExtantDoc());
   if (!htmlDoc)
     return true;
   nsHTMLDocument *document = static_cast<nsHTMLDocument*>(htmlDoc.get());
 
   nsDependentJSString str(id);
   nsCOMPtr<nsISupports> result;
   nsWrapperCache *cache;
   {
@@ -6195,17 +6195,17 @@ nsDocumentSH::PostCreate(nsIXPConnectWra
 
   nsIScriptGlobalObject *sgo = doc->GetScriptGlobalObject();
   nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(sgo);
   if (!win) {
     // No window, nothing else to do here
     return NS_OK;
   }
 
-  nsIDOMDocument* currentDoc = win->GetExtantDocument();
+  nsIDocument* currentDoc = win->GetExtantDoc();
 
   if (SameCOMIdentity(doc, currentDoc)) {
     jsval winVal;
 
     nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
     nsresult rv = WrapNative(cx, obj, win, &NS_GET_IID(nsIDOMWindow), false,
                              &winVal, getter_AddRefs(holder));
     NS_ENSURE_SUCCESS(rv, rv);
--- a/dom/base/nsDOMException.cpp
+++ b/dom/base/nsDOMException.cpp
@@ -178,18 +178,17 @@ NS_IMETHODIMP
 nsDOMException::GetCode(uint16_t* aCode)
 {
   NS_ENSURE_ARG_POINTER(aCode);
   *aCode = mCode;
 
   // Warn only when the code was changed (other than DOM Core)
   // or the code is useless (zero)
   if (NS_ERROR_GET_MODULE(mResult) != NS_ERROR_MODULE_DOM || !mCode) {
-    nsCOMPtr<nsIDocument> doc =
-      do_QueryInterface(nsContentUtils::GetDocumentFromCaller());
+    nsCOMPtr<nsIDocument> doc = nsContentUtils::GetDocumentFromCaller();
     if (doc) {
       doc->WarnOnceAbout(nsIDocument::eDOMExceptionCode);
     }
   }
 
   return NS_OK;
 }
 
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -183,24 +183,24 @@ nsDOMWindowUtils::GetDocumentMetadata(co
                                       nsAString& aValue)
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   if (window) {
-    nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
     if (doc) {
       nsCOMPtr<nsIAtom> name = do_GetAtom(aName);
       doc->GetHeaderData(name, aValue);
       return NS_OK;
     }
   }
-  
+
   aValue.Truncate();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMWindowUtils::Redraw(uint32_t aCount, uint32_t *aDurationOut)
 {
   if (!nsContentUtils::IsCallerChrome()) {
@@ -261,17 +261,17 @@ nsDOMWindowUtils::GetViewportInfo(uint32
                                   double *aDefaultZoom, bool *aAllowZoom,
                                   double *aMinZoom, double *aMaxZoom,
                                   uint32_t *aWidth, uint32_t *aHeight,
                                   bool *aAutoSize)
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   nsViewportInfo info = nsContentUtils::GetViewportInfo(doc, aDisplayWidth, aDisplayHeight);
   *aDefaultZoom = info.GetDefaultZoom();
   *aAllowZoom = info.IsZoomAllowed();
   *aMinZoom = info.GetMinZoom();
   *aMaxZoom = info.GetMaxZoom();
   *aWidth = info.GetWidth();
@@ -1242,17 +1242,17 @@ nsDOMWindowUtils::ElementFromPoint(float
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   Element* el =
     doc->ElementFromPointHelper(aX, aY, aIgnoreRootScrollFrame, aFlushLayout);
   nsCOMPtr<nsIDOMElement> retval = do_QueryInterface(el);
   retval.forget(aReturn);
   return NS_OK;
 }
@@ -1267,17 +1267,17 @@ nsDOMWindowUtils::NodesFromRect(float aX
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   return doc->NodesFromRectHelper(aX, aY, aTopSize, aRightSize, aBottomSize, aLeftSize, 
                                   aIgnoreRootScrollFrame, aFlushLayout, aReturn);
 }
 
 static already_AddRefed<gfxImageSurface>
 CanvasToImageSurface(nsIDOMHTMLCanvasElement* aCanvas)
@@ -1419,17 +1419,17 @@ nsDOMWindowUtils::SuppressEventHandling(
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
 
   if (aSuppress) {
     doc->SuppressEventHandling();
   } else {
     doc->UnsuppressEventHandlingAndFireEvents(true);
   }
 
@@ -1441,17 +1441,17 @@ nsDOMWindowUtils::GetScrollXY(bool aFlus
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   if (aFlushLayout) {
     doc->FlushPendingNotifications(Flush_Layout);
   }
 
   nsPoint scrollPos(0,0);
   nsIPresShell *presShell = doc->GetShell();
@@ -2455,25 +2455,25 @@ nsDOMWindowUtils::GetCursorType(int16_t 
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   NS_ENSURE_ARG_POINTER(aCursor);
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
-  NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);  
+  NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
 
   bool isSameDoc = false;
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
 
   NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
 
   do {
-    if (nsEventStateManager::sMouseOverDocument == doc.get()) {
+    if (nsEventStateManager::sMouseOverDocument == doc) {
       isSameDoc = true;
       break;
     }
   } while ((doc = doc->GetParentDocument()));
 
   if (!isSameDoc) {
     *aCursor = eCursor_none;
     return NS_OK;
@@ -2932,27 +2932,23 @@ nsDOMWindowUtils::GetPlugins(JSContext* 
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsIDOMDocument* ddoc = window->GetExtantDocument();
-
-  nsresult rv;
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(ddoc, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
 
   nsTArray<nsIObjectLoadingContent*> plugins;
   doc->GetPlugins(plugins);
 
   JSObject* jsPlugins = nullptr;
-  rv = nsTArrayToJSArray(cx, plugins, &jsPlugins);
+  nsresult rv = nsTArrayToJSArray(cx, plugins, &jsPlugins);
   NS_ENSURE_SUCCESS(rv, rv);
 
   *aPlugins = OBJECT_TO_JSVAL(jsPlugins);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMWindowUtils::SetScrollPositionClampingScrollPortSize(float aWidth, float aHeight)
@@ -3009,34 +3005,34 @@ nsDOMWindowUtils::RemoteFrameFullscreenC
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   doc->RemoteFrameFullscreenChanged(aFrameElement, aNewOrigin);
   return NS_OK;
 }
 
 nsresult
 nsDOMWindowUtils::RemoteFrameFullscreenReverted()
 {
   if (!nsContentUtils::IsCallerChrome()) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
   NS_ENSURE_STATE(window);
 
-  nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   NS_ENSURE_STATE(doc);
 
   doc->RemoteFrameFullscreenReverted();
   return NS_OK;
 }
 
 nsresult
 nsDOMWindowUtils::ExitFullscreen()
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -163,16 +163,17 @@
 #include "nsServiceManagerUtils.h"
 #ifdef MOZ_XUL
 #include "nsXULPopupManager.h"
 #include "nsIDOMXULControlElement.h"
 #include "nsMenuPopupFrame.h"
 #endif
 
 #include "xpcprivate.h"
+#include "nsDOMEvent.h"
 
 #ifdef NS_PRINTING
 #include "nsIPrintSettings.h"
 #include "nsIPrintSettingsService.h"
 #include "nsIWebBrowserPrint.h"
 #endif
 
 #include "nsWindowRoot.h"
@@ -2138,17 +2139,17 @@ nsGlobalWindow::SetNewDocument(nsIDocume
   if (IsFrozen()) {
     // This outer is now getting its first inner, thaw the outer now
     // that it's ready and is getting an inner window.
 
     Thaw();
   }
 
   NS_ASSERTION(!GetCurrentInnerWindow() ||
-               GetCurrentInnerWindow()->GetExtantDocument() == mDocument,
+               GetCurrentInnerWindow()->GetExtantDoc() == mDoc,
                "Uh, mDocument doesn't match the current inner window "
                "document!");
 
   bool wouldReuseInnerWindow = WouldReuseInnerWindow(aDocument);
   if (aForceReuseInnerWindow &&
       !wouldReuseInnerWindow &&
       mDoc &&
       mDoc->NodePrincipal() != aDocument->NodePrincipal()) {
@@ -6094,17 +6095,17 @@ nsGlobalWindow::SetResizable(bool aResiz
   // nop
 
   return NS_OK;
 }
 
 static void
 ReportUseOfDeprecatedMethod(nsGlobalWindow* aWindow, const char* aWarning)
 {
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(aWindow->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
   nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
                                   "DOM Events", doc,
                                   nsContentUtils::eDOM_PROPERTIES,
                                   aWarning);
 }
 
 NS_IMETHODIMP
 nsGlobalWindow::CaptureEvents(int32_t aEventFlags)
@@ -6156,39 +6157,38 @@ bool IsPopupBlocked(nsIDOMDocument* aDoc
     uint32_t permission = nsIPopupWindowManager::ALLOW_POPUP;
     pm->TestPermission(doc->NodePrincipal(), &permission);
     blocked = (permission == nsIPopupWindowManager::DENY_POPUP);
   }
   return blocked;
 }
 
 /* static */
-void 
-nsGlobalWindow::FirePopupBlockedEvent(nsIDOMDocument* aDoc,
+void
+nsGlobalWindow::FirePopupBlockedEvent(nsIDocument* aDoc,
                                       nsIDOMWindow *aRequestingWindow, nsIURI *aPopupURI,
                                       const nsAString &aPopupWindowName,
                                       const nsAString &aPopupWindowFeatures)
 {
   if (aDoc) {
     // Fire a "DOMPopupBlocked" event so that the UI can hear about
     // blocked popups.
-    nsCOMPtr<nsIDOMEvent> event;
-    aDoc->CreateEvent(NS_LITERAL_STRING("PopupBlockedEvents"),
-                      getter_AddRefs(event));
+    ErrorResult rv;
+    nsRefPtr<nsDOMEvent> event =
+      aDoc->CreateEvent(NS_LITERAL_STRING("PopupBlockedEvents"), rv);
     if (event) {
-      nsCOMPtr<nsIDOMPopupBlockedEvent> pbev(do_QueryInterface(event));
+      nsCOMPtr<nsIDOMPopupBlockedEvent> pbev(do_QueryObject(event));
       pbev->InitPopupBlockedEvent(NS_LITERAL_STRING("DOMPopupBlocked"),
                                   true, true, aRequestingWindow,
                                   aPopupURI, aPopupWindowName,
                                   aPopupWindowFeatures);
       event->SetTrusted(true);
 
-      nsCOMPtr<nsIDOMEventTarget> targ(do_QueryInterface(aDoc));
       bool defaultActionEnabled;
-      targ->DispatchEvent(event, &defaultActionEnabled);
+      aDoc->DispatchEvent(event, &defaultActionEnabled);
     }
   }
 }
 
 void FirePopupWindowEvent(nsIDOMDocument* aDoc)
 {
   // Fire a "PopupWindow" event
   nsCOMPtr<nsIDocument> doc(do_QueryInterface(aDoc));
@@ -6319,19 +6319,21 @@ nsGlobalWindow::FireAbuseEvents(bool aBl
 
   // use the base URI to build what would have been the popup's URI
   nsCOMPtr<nsIIOService> ios(do_GetService(NS_IOSERVICE_CONTRACTID));
   if (ios)
     ios->NewURI(NS_ConvertUTF16toUTF8(aPopupURL), 0, baseURL,
                 getter_AddRefs(popupURI));
 
   // fire an event chock full of informative URIs
-  if (aBlocked)
-    FirePopupBlockedEvent(topDoc, this, popupURI, aPopupWindowName,
+  if (aBlocked) {
+    nsCOMPtr<nsIDocument> topDocument = do_QueryInterface(topDoc);
+    FirePopupBlockedEvent(topDocument, this, popupURI, aPopupWindowName,
                           aPopupWindowFeatures);
+  }
   if (aWindow)
     FirePopupWindowEvent(topDoc);
 }
 
 NS_IMETHODIMP
 nsGlobalWindow::Open(const nsAString& aUrl, const nsAString& aName,
                      const nsAString& aOptions, nsIDOMWindow **_retval)
 {
@@ -6848,17 +6850,17 @@ nsGlobalWindow::PostMessageMoz(const JS:
 
   nsAutoString origin;
   if (callerOuterURI) {
     // if the principal has a URI, use that to generate the origin
     nsContentUtils::GetUTFOrigin(callerPrin, origin);
   }
   else if (callerInnerWin) {
     // otherwise use the URI of the document to generate origin
-    nsCOMPtr<nsIDocument> doc = do_QueryInterface(callerInnerWin->GetExtantDocument());
+    nsCOMPtr<nsIDocument> doc = callerInnerWin->GetExtantDoc();
     if (!doc)
       return NS_OK;
     callerOuterURI = doc->GetDocumentURI();
     // if the principal has a URI, use that to generate the origin
     nsContentUtils::GetUTFOrigin(callerOuterURI, origin);
   }
   else {
     // in case of a sandbox with a system principal origin can be empty
@@ -7168,17 +7170,17 @@ nsGlobalWindow::EnterModalState()
         frameSelection->SetMouseDownState(false);
       }
     }
   }
 
   if (topWin->mModalStateDepth == 0) {
     NS_ASSERTION(!mSuspendedDoc, "Shouldn't have mSuspendedDoc here!");
 
-    mSuspendedDoc = do_QueryInterface(topWin->GetExtantDocument());
+    mSuspendedDoc = topWin->GetExtantDoc();
     if (mSuspendedDoc && mSuspendedDoc->EventHandlingSuppressed()) {
       mSuspendedDoc->SuppressEventHandling();
     } else {
       mSuspendedDoc = nullptr;
     }
   }
   topWin->mModalStateDepth++;
 
@@ -7281,18 +7283,17 @@ nsGlobalWindow::LeaveModalState(nsIDOMWi
   topWin->mModalStateDepth--;
 
   if (topWin->mModalStateDepth == 0) {
     nsCOMPtr<nsIRunnable> runner = new nsPendingTimeoutRunner(topWin);
     if (NS_FAILED(NS_DispatchToCurrentThread(runner)))
       NS_WARNING("failed to dispatch pending timeout runnable");
 
     if (mSuspendedDoc) {
-      nsCOMPtr<nsIDocument> currentDoc =
-        do_QueryInterface(topWin->GetExtantDocument());
+      nsCOMPtr<nsIDocument> currentDoc = topWin->GetExtantDoc();
       mSuspendedDoc->UnsuppressEventHandlingAndFireEvents(currentDoc == mSuspendedDoc);
       mSuspendedDoc = nullptr;
     }
   }
 
   if (aCallerWin) {
     nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(aCallerWin));
     if (sgo) {
@@ -7772,17 +7773,17 @@ public:
 NS_IMETHODIMP
 nsGlobalWindow::UpdateCommands(const nsAString& anAction)
 {
   nsPIDOMWindow *rootWindow = nsGlobalWindow::GetPrivateRoot();
   if (!rootWindow)
     return NS_OK;
 
   nsCOMPtr<nsIDOMXULDocument> xulDoc =
-    do_QueryInterface(rootWindow->GetExtantDocument());
+    do_QueryInterface(rootWindow->GetExtantDoc());
   // See if we contain a XUL document.
   if (xulDoc) {
     // Retrieve the command dispatcher and call updateCommands on it.
     nsCOMPtr<nsIDOMXULCommandDispatcher> xulCommandDispatcher;
     xulDoc->GetCommandDispatcher(getter_AddRefs(xulCommandDispatcher));
     if (xulCommandDispatcher) {
       nsContentUtils::AddScriptRunner(new CommandDispatcher(xulCommandDispatcher,
                                                             anAction));
@@ -9934,28 +9935,25 @@ nsGlobalWindow::OpenInternal(const nsASt
       // window.open() from JavaScript, make sure there's a document in
       // the new window. We do this by simply asking the new window for
       // its document, this will synchronously create an empty document
       // if there is no document in the window.
       // XXXbz should this just use EnsureInnerWindow()?
 #ifdef DEBUG_jst
       {
         nsCOMPtr<nsPIDOMWindow> pidomwin(do_QueryInterface(*aReturn));
-
-        nsIDOMDocument *temp = pidomwin->GetExtantDocument();
-
-        NS_ASSERTION(temp, "No document in new window!!!");
+        NS_ASSERTION(pidomwin->GetExtantDoc(), "No document in new window!!!");
       }
 #endif
 
       nsCOMPtr<nsIDOMDocument> doc;
       (*aReturn)->GetDocument(getter_AddRefs(doc));
     }
   }
-    
+
   if (checkForPopup) {
     if (abuseLevel >= openControlled) {
       nsGlobalWindow *opened = static_cast<nsGlobalWindow *>(*aReturn);
       if (!opened->IsPopupSpamWindow()) {
         opened->SetPopupSpamWindow(true);
         ++gOpenPopupSpamCount;
       }
     }
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -579,17 +579,17 @@ public:
   void MaybeForgiveSpamCount();
   bool IsClosedOrClosing() {
     return (mIsClosed ||
             mInClose ||
             mHavePendingClose ||
             mCleanedUp);
   }
 
-  static void FirePopupBlockedEvent(nsIDOMDocument* aDoc,
+  static void FirePopupBlockedEvent(nsIDocument* aDoc,
                                     nsIDOMWindow *aRequestingWindow, nsIURI *aPopupURI,
                                     const nsAString &aPopupWindowName,
                                     const nsAString &aPopupWindowFeatures);
 
   virtual uint32_t GetSerial() {
     return mSerial;
   }
 
--- a/dom/base/nsGlobalWindowCommands.cpp
+++ b/dom/base/nsGlobalWindowCommands.cpp
@@ -340,17 +340,17 @@ nsClipboardCommand::IsCommandEnabled(con
   *outCmdEnabled = false;
 
   if (strcmp(aCommandName, "cmd_copy"))
     return NS_OK;
 
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aContext);
   NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
 
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(window->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
   *outCmdEnabled = nsCopySupport::CanCopy(doc);
   return NS_OK;
 }
 
 nsresult
 nsClipboardCommand::DoCommand(const char *aCommandName, nsISupports *aContext)
 {
   if (strcmp(aCommandName, "cmd_copy"))
--- a/dom/base/nsHistory.cpp
+++ b/dom/base/nsHistory.cpp
@@ -226,18 +226,17 @@ nsHistory::Go(int32_t aDelta)
       // history.go(0) (aka location.reload()) was called on a window
       // that is handling a resize event. Sites do this since Netscape
       // 4.x needed it, but we don't, and it's a horrible experience
       // for nothing.  In stead of reloading the page, just clear
       // style data and reflow the page since some sites may use this
       // trick to work around gecko reflow bugs, and this should have
       // the same effect.
 
-      nsCOMPtr<nsIDocument> doc =
-        do_QueryInterface(window->GetExtantDocument());
+      nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
 
       nsIPresShell *shell;
       nsPresContext *pcx;
       if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
         pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW);
       }
 
       return NS_OK;
@@ -333,18 +332,17 @@ nsHistory::GetState(nsIVariant **aState)
 
   nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mInnerWindow));
   if (!win)
     return NS_ERROR_NOT_AVAILABLE;
 
   if (!nsContentUtils::CanCallerAccess(win->GetOuterWindow()))
     return NS_ERROR_DOM_SECURITY_ERR;
 
-  nsCOMPtr<nsIDocument> doc =
-    do_QueryInterface(win->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
   if (!doc)
     return NS_ERROR_NOT_AVAILABLE;
 
   return doc->GetStateObject(aState);
 }
 
 NS_IMETHODIMP
 nsHistory::Item(uint32_t aIndex, nsAString& aReturn)
--- a/dom/base/nsJSEnvironment.cpp
+++ b/dom/base/nsJSEnvironment.cpp
@@ -615,17 +615,17 @@ JSObject2Win(JSContext *cx, JSObject *ob
 void
 PrintWinURI(nsGlobalWindow *win)
 {
   if (!win) {
     printf("No window passed in.\n");
     return;
   }
 
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(win->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
   if (!doc) {
     printf("No document in the window.\n");
     return;
   }
 
   nsIURI *uri = doc->GetDocumentURI();
   if (!uri) {
     printf("Document doesn't have a URI.\n");
--- a/dom/base/nsJSTimeoutHandler.cpp
+++ b/dom/base/nsJSTimeoutHandler.cpp
@@ -236,17 +236,17 @@ nsJSScriptTimeoutHandler::Init(nsGlobalW
 
     // Return an error that nsGlobalWindow can recognize and turn into NS_OK.
     return NS_ERROR_DOM_TYPE_ERR;
   }
 
   if (expr) {
     // if CSP is enabled, and setTimeout/setInterval was called with a string
     // or object, disable the registration and log an error
-    nsCOMPtr<nsIDocument> doc = do_QueryInterface(aWindow->GetExtantDocument());
+    nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
 
     if (doc) {
       nsCOMPtr<nsIContentSecurityPolicy> csp;
       nsresult rv = doc->NodePrincipal()->GetCsp(getter_AddRefs(csp));
       NS_ENSURE_SUCCESS(rv, rv);
 
       if (csp) {
         bool allowsEval = true;
--- a/dom/base/nsLocation.cpp
+++ b/dom/base/nsLocation.cpp
@@ -782,17 +782,17 @@ nsLocation::Reload(bool aForceget)
   if (window && window->IsHandlingResizeEvent()) {
     // location.reload() was called on a window that is handling a
     // resize event. Sites do this since Netscape 4.x needed it, but
     // we don't, and it's a horrible experience for nothing. In stead
     // of reloading the page, just clear style data and reflow the
     // page since some sites may use this trick to work around gecko
     // reflow bugs, and this should have the same effect.
 
-    nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
 
     nsIPresShell *shell;
     nsPresContext *pcx;
     if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
       pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW);
     }
 
     return NS_OK;
--- a/dom/base/nsPIDOMWindow.h
+++ b/dom/base/nsPIDOMWindow.h
@@ -167,21 +167,16 @@ public:
     }
 
     win->mMutationBits |= aType;
   }
 
   virtual void MaybeUpdateTouchState() {}
   virtual void UpdateTouchState() {}
 
-  // GetExtantDocument provides a backdoor to the DOM GetDocument accessor
-  nsIDOMDocument* GetExtantDocument() const
-  {
-    return mDocument;
-  }
   nsIDocument* GetExtantDoc() const
   {
     return mDoc;
   }
   nsIURI* GetDocumentURI() const;
   nsIURI* GetDocBaseURI() const;
 
   nsIDocument* GetDoc()
--- a/dom/base/nsWindowMemoryReporter.cpp
+++ b/dom/base/nsWindowMemoryReporter.cpp
@@ -53,17 +53,17 @@ nsWindowMemoryReporter::Init()
 }
 
 static already_AddRefed<nsIURI>
 GetWindowURI(nsIDOMWindow *aWindow)
 {
   nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(aWindow);
   NS_ENSURE_TRUE(pWindow, NULL);
 
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(pWindow->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = pWindow->GetExtantDoc();
   nsCOMPtr<nsIURI> uri;
 
   if (doc) {
     uri = doc->GetDocumentURI();
   }
 
   if (!uri) {
     nsCOMPtr<nsIScriptObjectPrincipal> scriptObjPrincipal =
--- a/dom/indexedDB/IDBDatabase.h
+++ b/dom/indexedDB/IDBDatabase.h
@@ -97,18 +97,17 @@ public:
   }
 
   already_AddRefed<nsIDocument> GetOwnerDocument()
   {
     if (!GetOwner()) {
       return nullptr;
     }
 
-    nsCOMPtr<nsIDocument> doc =
-      do_QueryInterface(GetOwner()->GetExtantDocument());
+    nsCOMPtr<nsIDocument> doc = GetOwner()->GetExtantDoc();
     return doc.forget();
   }
 
   void DisconnectFromActorParent();
 
   void CloseInternal(bool aIsDead);
 
   void EnterSetVersionTransaction();
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -970,19 +970,18 @@ MediaManager::GetUserMedia(bool aPrivile
         do_GetService(NS_POPUPWINDOWMANAGER_CONTRACTID);
       if (!pm) {
         return NS_OK;
       }
       uint32_t permission;
       nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
       pm->TestPermission(doc->NodePrincipal(), &permission);
       if (permission == nsIPopupWindowManager::DENY_POPUP) {
-        nsCOMPtr<nsIDOMDocument> domDoc = aWindow->GetExtantDocument();
         nsGlobalWindow::FirePopupBlockedEvent(
-          domDoc, aWindow, nullptr, EmptyString(), EmptyString()
+          doc, aWindow, nullptr, EmptyString(), EmptyString()
         );
         return NS_OK;
       }
     }
   }
 #endif
 
   static bool created = false;
--- a/dom/power/WakeLock.cpp
+++ b/dom/power/WakeLock.cpp
@@ -6,17 +6,16 @@
 #include "WakeLock.h"
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/Hal.h"
 #include "mozilla/HalWakeLock.h"
 #include "nsDOMClassInfoID.h"
 #include "nsError.h"
 #include "nsIDOMWindow.h"
 #include "nsIDOMEvent.h"
-#include "nsIDOMDocument.h"
 #include "nsPIDOMWindow.h"
 #include "PowerManager.h"
 
 DOMCI_DATA(MozWakeLock, mozilla::dom::power::WakeLock)
 
 using namespace mozilla::hal;
 
 namespace mozilla {
@@ -63,19 +62,19 @@ WakeLock::Init(const nsAString &aTopic, 
   mWindow = do_GetWeakReference(aWindow);
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aWindow);
 
   /**
    * Null windows are allowed. A wake lock without associated window
    * is always considered invisible.
    */
   if (window) {
-    nsCOMPtr<nsIDOMDocument> domDoc = window->GetExtantDocument();
-    NS_ENSURE_STATE(domDoc);
-    domDoc->GetHidden(&mHidden);
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
+    NS_ENSURE_STATE(doc);
+    mHidden = doc->Hidden();
   }
 
   AttachEventListener();
   DoLock();
 
   return NS_OK;
 }
 
@@ -162,27 +161,26 @@ WakeLock::DoUnlock()
                         mContentParentID);
   }
 }
 
 void
 WakeLock::AttachEventListener()
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
-  
+
   if (window) {
-    nsCOMPtr<nsIDOMDocument> domDoc = window->GetExtantDocument();
-    if (domDoc) {
-      nsCOMPtr<EventTarget> target = do_QueryInterface(domDoc);
-      target->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
-                                     this,
-                                     /* useCapture = */ true,
-                                     /* wantsUntrusted = */ false);
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
+    if (doc) {
+      doc->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
+                                  this,
+                                  /* useCapture = */ true,
+                                  /* wantsUntrusted = */ false);
 
-      target = do_QueryInterface(window);
+      nsCOMPtr<EventTarget> target = do_QueryInterface(window);
       target->AddSystemEventListener(NS_LITERAL_STRING("pagehide"),
                                      this,
                                      /* useCapture = */ true,
                                      /* wantsUntrusted = */ false);
       target->AddSystemEventListener(NS_LITERAL_STRING("pageshow"),
                                      this,
                                      /* useCapture = */ true,
                                      /* wantsUntrusted = */ false);
@@ -191,23 +189,22 @@ WakeLock::AttachEventListener()
 }
 
 void
 WakeLock::DetachEventListener()
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
 
   if (window) {
-    nsCOMPtr<nsIDOMDocument> domDoc = window->GetExtantDocument();
-    if (domDoc) {
-      nsCOMPtr<EventTarget> target = do_QueryInterface(domDoc);
-      target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
-                                        this,
-                                        /* useCapture = */ true);
-      target = do_QueryInterface(window);
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
+    if (doc) {
+      doc->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
+                                     this,
+                                     /* useCapture = */ true);
+      nsCOMPtr<EventTarget> target = do_QueryInterface(window);
       target->RemoveSystemEventListener(NS_LITERAL_STRING("pagehide"),
                                         this,
                                         /* useCapture = */ true);
       target->RemoveSystemEventListener(NS_LITERAL_STRING("pageshow"),
                                         this,
                                         /* useCapture = */ true);
     }
   }
--- a/dom/src/jsurl/nsJSProtocolHandler.cpp
+++ b/dom/src/jsurl/nsJSProtocolHandler.cpp
@@ -636,18 +636,17 @@ nsJSChannel::AsyncOpen(nsIStreamListener
         nsresult rv = loadGroup->AddRequest(this, nullptr);
         if (NS_FAILED(rv)) {
             mIsActive = false;
             CleanupStrongRefs();
             return rv;
         }
     }
 
-    mDocumentOnloadBlockedOn =
-        do_QueryInterface(mOriginalInnerWindow->GetExtantDocument());
+    mDocumentOnloadBlockedOn = mOriginalInnerWindow->GetExtantDoc();
     if (mDocumentOnloadBlockedOn) {
         // If we're a document channel, we need to actually block onload on our
         // _parent_ document.  This is because we don't actually set our
         // LOAD_DOCUMENT_URI flag, so a docloader we're loading in as the
         // document channel will claim to not be busy, and our parent's onload
         // could fire too early.
         nsLoadFlags loadFlags;
         mStreamChannel->GetLoadFlags(&loadFlags);
@@ -662,17 +661,17 @@ nsJSChannel::AsyncOpen(nsIStreamListener
 
 
     mPopupState = win->GetPopupControlState();
 
     void (nsJSChannel::*method)();
     if (mIsAsync) {
         // post an event to do the rest
         method = &nsJSChannel::EvaluateScript;
-    } else {   
+    } else {
         EvaluateScript();
         if (mOpenedStreamChannel) {
             // That will handle notifying things
             return NS_OK;
         }
 
         NS_ASSERTION(NS_FAILED(mStatus), "We should have failed _somehow_");
 
--- a/dom/workers/WorkerPrivate.cpp
+++ b/dom/workers/WorkerPrivate.cpp
@@ -2500,17 +2500,17 @@ WorkerPrivate::Create(JSContext* aCx, JS
         JS_ReportError(aCx, "Couldn't get script context for this worker!");
         return nullptr;
       }
 
       parentContext = scriptContext->GetNativeContext();
 
       // If we're called from a window then we can dig out the principal and URI
       // from the document.
-      document = do_QueryInterface(window->GetExtantDocument());
+      document = window->GetExtantDoc();
       if (!document) {
         JS_ReportError(aCx, "No document in this window!");
         return nullptr;
       }
 
       baseURI = document->GetDocBaseURI();
 
       // Use the document's NodePrincipal as our principal if we're not being
@@ -3109,17 +3109,17 @@ WorkerPrivate::CheckXHRParamsAllowed(nsP
 {
   AssertIsOnMainThread();
   NS_ASSERTION(aWindow, "Wrong cannot be null");
 
   if (!aWindow->GetDocShell()) {
     return false;
   }
 
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(aWindow->GetExtantDocument());
+  nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
   if (!doc) {
     return false;
   }
 
   nsCOMPtr<nsIPermissionManager> permMgr = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
   if (!permMgr) {
     return false;
   }
--- a/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
+++ b/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
@@ -1,22 +1,27 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsAutoWindowStateHelper.h"
 
+#include "nsDOMEvent.h"
+#include "nsGUIEvent.h"
+#include "nsIDocument.h"
+#include "nsIDOMEvent.h"
 #include "nsIDOMWindow.h"
 #include "nsPIDOMWindow.h"
-#include "nsIDOMEventTarget.h"
-#include "nsIDOMEvent.h"
 #include "nsString.h"
 #include "nsGUIEvent.h"
 
+using namespace mozilla;
+using namespace mozilla::dom;
+
 /****************************************************************
  ****************** nsAutoWindowStateHelper *********************
  ****************************************************************/
 
 nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsIDOMWindow *aWindow)
   : mWindow(aWindow),
     mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
 {
@@ -45,24 +50,27 @@ nsAutoWindowStateHelper::DispatchEventTo
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow);
   if (!window) {
     return true;
   }
 
   // The functions of nsContentUtils do not provide the required behavior,
   // so the following is inlined.
-  nsIDOMDocument* doc = window->GetExtantDocument();
+  nsIDocument* doc = window->GetExtantDoc();
   if (!doc) {
     return true;
   }
 
-  nsCOMPtr<nsIDOMEvent> event;
-  doc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
+  ErrorResult rv;
+  nsRefPtr<nsDOMEvent> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv);
+  if (rv.Failed()) {
+    return false;
+  }
   NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false);
   event->SetTrusted(true);
   event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
 
-  nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(window));
+  nsCOMPtr<EventTarget> target = do_QueryInterface(window);
   bool defaultActionEnabled;
   target->DispatchEvent(event, &defaultActionEnabled);
   return defaultActionEnabled;
 }
--- a/embedding/components/windowwatcher/src/nsWindowWatcher.cpp
+++ b/embedding/components/windowwatcher/src/nsWindowWatcher.cpp
@@ -767,18 +767,17 @@ nsWindowWatcher::OpenWindowInternal(nsID
   if (newMuCV) {
     nsCOMPtr<nsIDocShellTreeItem> parentItem;
     GetWindowTreeItem(aParent, getter_AddRefs(parentItem));
 
     if (aCalledFromJS) {
       nsCOMPtr<nsIDocShellTreeItem> callerItem = GetCallerTreeItem(parentItem);
       nsCOMPtr<nsPIDOMWindow> callerWin = do_GetInterface(callerItem);
       if (callerWin) {
-        nsCOMPtr<nsIDocument> doc =
-          do_QueryInterface(callerWin->GetExtantDocument());
+        nsCOMPtr<nsIDocument> doc = callerWin->GetExtantDoc();
         if (doc) {
           newMuCV->SetDefaultCharacterSet(doc->GetDocumentCharacterSet());
         }
       }
     }
     else {
       nsCOMPtr<nsIDocShell> parentDocshell = do_QueryInterface(parentItem);
       // parentDocshell may be null if the parent got closed in the meantime
@@ -906,18 +905,18 @@ nsWindowWatcher::OpenWindowInternal(nsID
       nsCOMPtr<nsPIDOMWindow> w(do_QueryInterface(sgo));
       if (w) {
         /* use the URL from the *extant* document, if any. The usual accessor
            GetDocument will synchronously create an about:blank document if
            it has no better answer, and we only care about a real document.
            Also using GetDocument to force document creation seems to
            screw up focus in the hidden window; see bug 36016.
         */
-        nsCOMPtr<nsIDocument> doc(do_QueryInterface(w->GetExtantDocument()));
-        if (doc) { 
+        nsCOMPtr<nsIDocument> doc = w->GetExtantDoc();
+        if (doc) {
           // Set the referrer
           loadInfo->SetReferrer(doc->GetDocumentURI());
         }
       }
     }
   }
 
   if (isNewToplevelWindow) {
@@ -1794,18 +1793,17 @@ nsWindowWatcher::ReadyOpenedDocShellItem
           do_QueryInterface(aOpenedItem);
         NS_ASSERTION(docloader, "How can we not have a docloader here?");
 
         nsCOMPtr<nsIChannel> chan;
         docloader->GetDocumentChannel(getter_AddRefs(chan));
         NS_ASSERTION(!chan, "Why is there a document channel?");
 #endif
 
-        nsCOMPtr<nsIDocument> doc =
-          do_QueryInterface(piOpenedWindow->GetExtantDocument());
+        nsCOMPtr<nsIDocument> doc = piOpenedWindow->GetExtantDoc();
         if (doc) {
           doc->SetIsInitialDocument(true);
         }
       }
     }
     rv = CallQueryInterface(piOpenedWindow, aOpenedWindow);
   }
   return rv;
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -4945,18 +4945,17 @@ ContentComponentsGetterOp(JSContext *cx,
     // Do Telemetry on how often this happens.
     Telemetry::Accumulate(Telemetry::COMPONENTS_OBJECT_ACCESSED_BY_CONTENT, true);
 
     // Warn once.
     JSAutoCompartment ac(cx, obj);
     nsCOMPtr<nsPIDOMWindow> win =
         do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(obj));
     if (win) {
-        nsCOMPtr<nsIDocument> doc =
-            do_QueryInterface(win->GetExtantDocument());
+        nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
         if (doc)
             doc->WarnOnceAbout(nsIDocument::eComponents, /* asError = */ true);
     }
 
     return true;
 }
 
 // static
--- a/layout/base/nsDocumentViewer.cpp
+++ b/layout/base/nsDocumentViewer.cpp
@@ -928,18 +928,17 @@ nsDocumentViewer::InitInternal(nsIWidget
 
     // Set script-context-owner in the document
 
     nsCOMPtr<nsPIDOMWindow> window;
     requestor->GetInterface(NS_GET_IID(nsPIDOMWindow),
                             getter_AddRefs(window));
 
     if (window) {
-      nsCOMPtr<nsIDocument> curDoc =
-        do_QueryInterface(window->GetExtantDocument());
+      nsCOMPtr<nsIDocument> curDoc = window->GetExtantDoc();
       if (aForceSetNewDocument || curDoc != mDocument) {
         window->SetNewDocument(mDocument, aState, false);
         nsJSContext::LoadStart();
       }
     }
   }
 
   if (aDoCreation && mPresContext) {
@@ -3338,17 +3337,17 @@ nsDocumentViewer::GetPopupNode(nsIDOMNod
     NS_ENSURE_TRUE(root, NS_ERROR_FAILURE);
 
     // get the popup node
     nsCOMPtr<nsIDOMNode> node = root->GetPopupNode();
 #ifdef MOZ_XUL
     if (!node) {
       nsPIDOMWindow* rootWindow = root->GetWindow();
       if (rootWindow) {
-        nsCOMPtr<nsIDocument> rootDoc = do_QueryInterface(rootWindow->GetExtantDocument());
+        nsCOMPtr<nsIDocument> rootDoc = rootWindow->GetExtantDoc();
         if (rootDoc) {
           nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
           if (pm) {
             node = pm->GetLastTriggerPopupNode(rootDoc);
           }
         }
       }
     }
--- a/layout/base/nsPresShell.cpp
+++ b/layout/base/nsPresShell.cpp
@@ -5975,17 +5975,17 @@ PresShell::HandleEvent(nsIFrame        *
     if (NS_IsEventTargetedAtFocusedWindow(aEvent)) {
       nsCOMPtr<nsPIDOMWindow> window = GetFocusedDOMWindowInOurWindow();
       // No DOM window in same top level window has not been focused yet,
       // discard the events.
       if (!window) {
         return NS_OK;
       }
 
-      retargetEventDoc = do_QueryInterface(window->GetExtantDocument());
+      retargetEventDoc = window->GetExtantDoc();
       if (!retargetEventDoc)
         return NS_OK;
     } else if (capturingContent) {
       // if the mouse is being captured then retarget the mouse event at the
       // document that is being captured.
       retargetEventDoc = capturingContent->GetCurrentDoc();
 #ifdef ANDROID
     } else if (aEvent->eventStructType == NS_TOUCH_EVENT) {