if (NS_FAILED(rv = content->GetDocument(*getter_AddRefs(document)))) {
if (NS_FAILED(rv = content->GetDocument(getter_AddRefs(document)))) {
XULPopupListenerImpl::PreLaunchPopup(nsIDOMEvent* aMouseEvent){  PRUint16 button;  nsCOMPtr<nsIDOMMouseEvent> mouseEvent;  mouseEvent = do_QueryInterface(aMouseEvent);  if (!mouseEvent) {    //non-ui event passed in.  bad things.    return NS_OK;  }  // check if someone has attempted to prevent this action.  nsCOMPtr<nsIDOMNSUIEvent> nsUIEvent;  nsUIEvent = do_QueryInterface(mouseEvent);  if (!nsUIEvent) {    return NS_OK;  }  PRBool preventDefault;  nsUIEvent->GetPreventDefault(&preventDefault);  if (preventDefault) {    // someone called preventDefault. bail.    return NS_OK;  }  // Get the node that was clicked on.  nsCOMPtr<nsIDOMEventTarget> target;  mouseEvent->GetTarget( getter_AddRefs( target ) );  nsCOMPtr<nsIDOMNode> targetNode;  if (target) targetNode = do_QueryInterface(target);  // This is a gross hack to deal with a recursive popup situation happening in AIM code.   // See http://bugzilla.mozilla.org/show_bug.cgi?id=96920.  // If a menu item child was clicked on that leads to a popup needing  // to show, we know (guaranteed) that we're dealing with a menu or  // submenu of an already-showing popup.  We don't need to do anything at all.  if (popupType == eXULPopupType_popup) {    nsCOMPtr<nsIContent> targetContent = do_QueryInterface(target);    nsCOMPtr<nsIAtom> tag;    targetContent->GetTag(*getter_AddRefs(tag));    if (tag && (tag.get() == nsXULAtoms::menu || tag.get() == nsXULAtoms::menuitem))      return NS_OK;  }  // Get the document with the popup.  nsCOMPtr<nsIDocument> document;  nsCOMPtr<nsIContent> content = do_QueryInterface(mElement);  nsresult rv;  if (NS_FAILED(rv = content->GetDocument(*getter_AddRefs(document)))) {    NS_ERROR("Unable to retrieve the document.");    return rv;  }  // Turn the document into a XUL document so we can use SetPopupNode.  nsCOMPtr<nsIDOMXULDocument> xulDocument = do_QueryInterface(document);  if (xulDocument == nsnull) {    NS_ERROR("Popup attached to an element that isn't in XUL!");    return NS_ERROR_FAILURE;  }  // Store clicked-on node in xul document for context menus and menu popups.  xulDocument->SetPopupNode( targetNode );  nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(aMouseEvent));  switch (popupType) {    case eXULPopupType_popup:      // Check for left mouse button down      mouseEvent->GetButton(&button);      if (button == 0) {        // Time to launch a popup menu.        LaunchPopup(aMouseEvent);        if (nsevent) {            nsevent->PreventBubble();        }        aMouseEvent->PreventDefault();      }      break;    case eXULPopupType_context:    // Time to launch a context menu#ifndef NS_CONTEXT_MENU_IS_MOUSEUP    // If the context menu launches on mousedown,    // we have to fire focus on the content we clicked on    FireFocusOnTargetContent(targetNode);#endif    LaunchPopup(aMouseEvent);    if (nsevent) {        nsevent->PreventBubble();    }    aMouseEvent->PreventDefault();    break;  }  return NS_OK;}