diff --git a/dom/base/nsPIWindowRoot.h b/dom/base/nsPIWindowRoot.h index ba8eeb13c1f6..70d490fccf75 100644 --- a/dom/base/nsPIWindowRoot.h +++ b/dom/base/nsPIWindowRoot.h @@ -14,6 +14,7 @@ class nsPIDOMWindowOuter; class nsIControllers; class nsIController; +class nsINode; namespace mozilla { namespace dom { @@ -33,8 +34,8 @@ public: virtual nsPIDOMWindowOuter* GetWindow()=0; // get and set the node that is the context of a popup menu - virtual nsIDOMNode* GetPopupNode() = 0; - virtual void SetPopupNode(nsIDOMNode* aNode) = 0; + virtual already_AddRefed GetPopupNode() = 0; + virtual void SetPopupNode(nsINode* aNode) = 0; /** * @param aForVisibleWindow true if caller needs controller which is diff --git a/dom/base/nsWindowRoot.cpp b/dom/base/nsWindowRoot.cpp index 1a3a5d8edc40..e8b6228c929d 100644 --- a/dom/base/nsWindowRoot.cpp +++ b/dom/base/nsWindowRoot.cpp @@ -371,15 +371,15 @@ nsWindowRoot::GetEnabledDisabledCommands(nsTArray& aEnabledCommands, } } -nsIDOMNode* +already_AddRefed nsWindowRoot::GetPopupNode() { - nsCOMPtr popupNode = do_QueryReferent(mPopupNode); - return popupNode; + nsCOMPtr popupNode = do_QueryReferent(mPopupNode); + return popupNode.forget(); } void -nsWindowRoot::SetPopupNode(nsIDOMNode* aNode) +nsWindowRoot::SetPopupNode(nsINode* aNode) { mPopupNode = do_GetWeakReference(aNode); } diff --git a/dom/base/nsWindowRoot.h b/dom/base/nsWindowRoot.h index 850bf6d23873..840668752d70 100644 --- a/dom/base/nsWindowRoot.h +++ b/dom/base/nsWindowRoot.h @@ -52,8 +52,8 @@ public: virtual void GetEnabledDisabledCommands(nsTArray& aEnabledCommands, nsTArray& aDisabledCommands) override; - virtual nsIDOMNode* GetPopupNode() override; - virtual void SetPopupNode(nsIDOMNode* aNode) override; + virtual already_AddRefed GetPopupNode() override; + virtual void SetPopupNode(nsINode* aNode) override; virtual void SetParentTarget(mozilla::dom::EventTarget* aTarget) override { diff --git a/dom/xul/XULDocument.cpp b/dom/xul/XULDocument.cpp index 2902f1a63e44..3e7042f61da3 100644 --- a/dom/xul/XULDocument.cpp +++ b/dom/xul/XULDocument.cpp @@ -1315,8 +1315,8 @@ XULDocument::GetHeight(ErrorResult& aRv) return height; } -JSObject* -GetScopeObjectOfNode(nsIDOMNode* node) +static JSObject* +GetScopeObjectOfNode(nsINode* node) { MOZ_ASSERT(node, "Must not be called with null."); @@ -1328,11 +1328,8 @@ GetScopeObjectOfNode(nsIDOMNode* node) // this, let's do the same check as nsNodeSH::PreCreate does to // determine the scope and if it fails let's just return null in // XULDocument::GetPopupNode. - nsCOMPtr inode = do_QueryInterface(node); - MOZ_ASSERT(inode, "How can this happen?"); - - nsIDocument* doc = inode->OwnerDoc(); - MOZ_ASSERT(inode, "This should never happen."); + nsIDocument* doc = node->OwnerDoc(); + MOZ_ASSERT(doc, "This should never happen."); nsIGlobalObject* global = doc->GetScopeObject(); return global ? global->GetGlobalJSObject() : nullptr; @@ -1348,10 +1345,11 @@ XULDocument::GetPopupNode(nsIDOMNode** aNode) { *aNode = nullptr; - nsCOMPtr node; + nsCOMPtr node; nsCOMPtr rootWin = GetWindowRoot(); - if (rootWin) + if (rootWin) { node = rootWin->GetPopupNode(); // addref happens here + } if (!node) { nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); @@ -1362,7 +1360,8 @@ XULDocument::GetPopupNode(nsIDOMNode** aNode) if (node && nsContentUtils::CanCallerAccess(node) && GetScopeObjectOfNode(node)) { - node.forget(aNode); + *aNode = node->AsDOMNode(); + NS_ADDREF(*aNode); } return NS_OK; @@ -1381,15 +1380,16 @@ XULDocument::GetPopupNode() NS_IMETHODIMP XULDocument::SetPopupNode(nsIDOMNode* aNode) { + nsCOMPtr node = do_QueryInterface(aNode); if (aNode) { // only allow real node objects - nsCOMPtr node = do_QueryInterface(aNode); NS_ENSURE_ARG(node); } nsCOMPtr rootWin = GetWindowRoot(); - if (rootWin) - rootWin->SetPopupNode(aNode); // addref happens here + if (rootWin) { + rootWin->SetPopupNode(node); + } return NS_OK; } @@ -1472,9 +1472,11 @@ XULDocument::GetTooltipNode(nsIDOMNode** aNode) nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); if (pm) { - nsCOMPtr node = pm->GetLastTriggerTooltipNode(this); - if (node && nsContentUtils::CanCallerAccess(node)) - node.forget(aNode); + nsCOMPtr node = pm->GetLastTriggerTooltipNode(this); + if (node && nsContentUtils::CanCallerAccess(node)) { + *aNode = node->AsDOMNode(); + NS_ADDREF(*aNode); + } } return NS_OK; diff --git a/layout/base/nsDocumentViewer.cpp b/layout/base/nsDocumentViewer.cpp index a9da6c848561..a250cf83805b 100644 --- a/layout/base/nsDocumentViewer.cpp +++ b/layout/base/nsDocumentViewer.cpp @@ -281,9 +281,9 @@ private: */ nsresult InitPresentationStuff(bool aDoInitialReflow); - nsresult GetPopupNode(nsIDOMNode** aNode); - nsresult GetPopupLinkNode(nsIDOMNode** aNode); - nsresult GetPopupImageNode(nsIImageLoadingContent** aNode); + already_AddRefed GetPopupNode(); + already_AddRefed GetPopupLinkNode(); + already_AddRefed GetPopupImageNode(); nsresult GetContentSizeInternal(int32_t* aWidth, int32_t* aHeight, nscoord aMaxWidth, nscoord aMaxHeight); @@ -2761,8 +2761,7 @@ NS_IMETHODIMP nsDocumentViewer::CopySelection() NS_IMETHODIMP nsDocumentViewer::CopyLinkLocation() { NS_ENSURE_TRUE(mPresShell, NS_ERROR_NOT_INITIALIZED); - nsCOMPtr node; - GetPopupLinkNode(getter_AddRefs(node)); + nsCOMPtr node = GetPopupLinkNode(); // make noise if we're not in a link NS_ENSURE_TRUE(node, NS_ERROR_FAILURE); @@ -2785,8 +2784,7 @@ NS_IMETHODIMP nsDocumentViewer::CopyLinkLocation() NS_IMETHODIMP nsDocumentViewer::CopyImage(int32_t aCopyFlags) { NS_ENSURE_TRUE(mPresShell, NS_ERROR_NOT_INITIALIZED); - nsCOMPtr node; - GetPopupImageNode(getter_AddRefs(node)); + nsCOMPtr node = GetPopupImageNode(); // make noise if we're not in an image NS_ENSURE_TRUE(node, NS_ERROR_FAILURE); @@ -2846,7 +2844,8 @@ NS_IMETHODIMP nsDocumentViewer::SetCommandNode(nsIDOMNode* aNode) nsCOMPtr root = window->GetTopWindowRoot(); NS_ENSURE_STATE(root); - root->SetPopupNode(aNode); + nsCOMPtr node = do_QueryInterface(aNode); + root->SetPopupNode(node); return NS_OK; } @@ -3612,26 +3611,22 @@ nsresult nsDocViewerSelectionListener::Init(nsDocumentViewer *aDocViewer) * not all content (images included) can receive focus. */ -nsresult -nsDocumentViewer::GetPopupNode(nsIDOMNode** aNode) +already_AddRefed +nsDocumentViewer::GetPopupNode() { - NS_ENSURE_ARG_POINTER(aNode); - - *aNode = nullptr; - // get the document nsIDocument* document = GetDocument(); - NS_ENSURE_TRUE(document, NS_ERROR_FAILURE); + NS_ENSURE_TRUE(document, nullptr); // get the private dom window nsCOMPtr window(document->GetWindow()); - NS_ENSURE_TRUE(window, NS_ERROR_NOT_AVAILABLE); + NS_ENSURE_TRUE(window, nullptr); if (window) { nsCOMPtr root = window->GetTopWindowRoot(); - NS_ENSURE_TRUE(root, NS_ERROR_FAILURE); + NS_ENSURE_TRUE(root, nullptr); // get the popup node - nsCOMPtr node = root->GetPopupNode(); + nsCOMPtr node = root->GetPopupNode(); #ifdef MOZ_XUL if (!node) { nsPIDOMWindowOuter* rootWindow = root->GetWindow(); @@ -3646,27 +3641,18 @@ nsDocumentViewer::GetPopupNode(nsIDOMNode** aNode) } } #endif - node.swap(*aNode); + return node.forget(); } - return NS_OK; + return nullptr; } // GetPopupLinkNode: return popup link node or fail -nsresult -nsDocumentViewer::GetPopupLinkNode(nsIDOMNode** aNode) +already_AddRefed +nsDocumentViewer::GetPopupLinkNode() { - NS_ENSURE_ARG_POINTER(aNode); - - // you get null unless i say so - *aNode = nullptr; - // find popup node - nsCOMPtr domNode; - nsresult rv = GetPopupNode(getter_AddRefs(domNode)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr node = do_QueryInterface(domNode); + nsCOMPtr node = GetPopupNode(); // find out if we have a link in our ancestry while (node) { @@ -3674,9 +3660,7 @@ nsDocumentViewer::GetPopupLinkNode(nsIDOMNode** aNode) if (content) { nsCOMPtr hrefURI = content->GetHrefURI(); if (hrefURI) { - *aNode = node->AsDOMNode(); - NS_IF_ADDREF(*aNode); // addref - return NS_OK; + return node.forget(); } } @@ -3685,27 +3669,17 @@ nsDocumentViewer::GetPopupLinkNode(nsIDOMNode** aNode) } // if we have no node, fail - return NS_ERROR_FAILURE; + return nullptr; } // GetPopupLinkNode: return popup image node or fail -nsresult -nsDocumentViewer::GetPopupImageNode(nsIImageLoadingContent** aNode) +already_AddRefed +nsDocumentViewer::GetPopupImageNode() { - NS_ENSURE_ARG_POINTER(aNode); - - // you get null unless i say so - *aNode = nullptr; - // find popup node - nsCOMPtr node; - nsresult rv = GetPopupNode(getter_AddRefs(node)); - NS_ENSURE_SUCCESS(rv, rv); - - if (node) - CallQueryInterface(node, aNode); - - return NS_OK; + nsCOMPtr node = GetPopupNode(); + nsCOMPtr img = do_QueryInterface(node); + return img.forget(); } /* @@ -3731,9 +3705,7 @@ NS_IMETHODIMP nsDocumentViewer::GetInLink(bool* aInLink) *aInLink = false; // get the popup link - nsCOMPtr node; - nsresult rv = GetPopupLinkNode(getter_AddRefs(node)); - if (NS_FAILED(rv)) return rv; + nsCOMPtr node = GetPopupLinkNode(); NS_ENSURE_TRUE(node, NS_ERROR_FAILURE); // if we made it here, we're in a link @@ -3753,9 +3725,7 @@ NS_IMETHODIMP nsDocumentViewer::GetInImage(bool* aInImage) *aInImage = false; // get the popup image - nsCOMPtr node; - nsresult rv = GetPopupImageNode(getter_AddRefs(node)); - if (NS_FAILED(rv)) return rv; + nsCOMPtr node = GetPopupImageNode(); if (!node) { return NS_ERROR_FAILURE; } diff --git a/layout/xul/nsXULPopupManager.cpp b/layout/xul/nsXULPopupManager.cpp index c4a07ec1352c..619209c94949 100644 --- a/layout/xul/nsXULPopupManager.cpp +++ b/layout/xul/nsXULPopupManager.cpp @@ -1706,20 +1706,20 @@ nsXULPopupManager::GetVisiblePopups(nsTArray& aPopups) } } -already_AddRefed +already_AddRefed nsXULPopupManager::GetLastTriggerNode(nsIDocument* aDocument, bool aIsTooltip) { if (!aDocument) return nullptr; - nsCOMPtr node; + nsCOMPtr node; // if mOpeningPopup is set, it means that a popupshowing event is being // fired. In this case, just use the cached node, as the popup is not yet in // the list of open popups. if (mOpeningPopup && mOpeningPopup->GetUncomposedDoc() == aDocument && aIsTooltip == mOpeningPopup->IsXULElement(nsGkAtoms::tooltip)) { - node = do_QueryInterface(nsMenuPopupFrame::GetTriggerContent(GetPopupFrameForContent(mOpeningPopup, false))); + node = nsMenuPopupFrame::GetTriggerContent(GetPopupFrameForContent(mOpeningPopup, false)); } else { nsMenuChainItem* item = mPopups; @@ -1727,7 +1727,7 @@ nsXULPopupManager::GetLastTriggerNode(nsIDocument* aDocument, bool aIsTooltip) // look for a popup of the same type and document. if ((item->PopupType() == ePopupTypeTooltip) == aIsTooltip && item->Content()->GetUncomposedDoc() == aDocument) { - node = do_QueryInterface(nsMenuPopupFrame::GetTriggerContent(item->Frame())); + node = nsMenuPopupFrame::GetTriggerContent(item->Frame()); if (node) break; } diff --git a/layout/xul/nsXULPopupManager.h b/layout/xul/nsXULPopupManager.h index bf62040f6cc1..b210101fe9bf 100644 --- a/layout/xul/nsXULPopupManager.h +++ b/layout/xul/nsXULPopupManager.h @@ -596,12 +596,12 @@ public: * aDocument. aDocument must be non-null and be a document contained within * the same window hierarchy as the popup to retrieve. */ - already_AddRefed GetLastTriggerPopupNode(nsIDocument* aDocument) + already_AddRefed GetLastTriggerPopupNode(nsIDocument* aDocument) { return GetLastTriggerNode(aDocument, false); } - already_AddRefed GetLastTriggerTooltipNode(nsIDocument* aDocument) + already_AddRefed GetLastTriggerTooltipNode(nsIDocument* aDocument) { return GetLastTriggerNode(aDocument, true); } @@ -798,7 +798,7 @@ private: protected: - already_AddRefed GetLastTriggerNode(nsIDocument* aDocument, bool aIsTooltip); + already_AddRefed GetLastTriggerNode(nsIDocument* aDocument, bool aIsTooltip); /** * Set mouse capturing for the current popup. This traps mouse clicks that diff --git a/layout/xul/nsXULTooltipListener.cpp b/layout/xul/nsXULTooltipListener.cpp index c1ad2d1af51d..66e7d218d989 100644 --- a/layout/xul/nsXULTooltipListener.cpp +++ b/layout/xul/nsXULTooltipListener.cpp @@ -103,12 +103,12 @@ nsXULTooltipListener::MouseOut(nsIDOMEvent* aEvent) // hide the tooltip if (currentTooltip) { // which node did the mouse leave? - nsCOMPtr targetNode = do_QueryInterface( + nsCOMPtr targetNode = do_QueryInterface( aEvent->InternalDOMEvent()->GetTarget()); nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); if (pm) { - nsCOMPtr tooltipNode = + nsCOMPtr tooltipNode = pm->GetLastTriggerTooltipNode(currentTooltip->GetUncomposedDoc()); if (tooltipNode == targetNode) { // if the target node is the current tooltip target node, the mouse