nsCOMPtr<nsIScriptGlobalObject> global; document->GetScriptGlobalObject(getter_AddRefs(global));
nsCOMPtr<nsIScriptGlobalObject> global = document->GetScriptGlobalObject();
XULPopupListenerImpl::LaunchPopup(PRInt32 aClientX, PRInt32 aClientY){  nsresult rv = NS_OK;  nsAutoString type(NS_LITERAL_STRING("popup"));  if ( popupType == eXULPopupType_context ) {    type.Assign(NS_LITERAL_STRING("context"));        // position the menu two pixels down and to the right from the current    // mouse position. This makes it easier to dismiss the menu by just    // clicking.    aClientX += 2;    aClientY += 2;  }  nsAutoString identifier;  mElement->GetAttribute(type, identifier);  if (identifier.IsEmpty()) {    if (type.EqualsIgnoreCase("popup"))      mElement->GetAttribute(NS_LITERAL_STRING("menu"), identifier);    else if (type.EqualsIgnoreCase("context"))      mElement->GetAttribute(NS_LITERAL_STRING("contextmenu"), identifier);    if (identifier.IsEmpty())      return rv;  }  // Try to find the popup content and the document.  nsCOMPtr<nsIContent> content = do_QueryInterface(mElement);  nsCOMPtr<nsIDocument> document = content->GetDocument();  // Turn the document into a DOM document so we can use getElementById  nsCOMPtr<nsIDOMDocument> domDocument = do_QueryInterface(document);  if (!domDocument) {    NS_ERROR("Popup attached to an element that isn't in XUL!");    return NS_ERROR_FAILURE;  }  // Handle the _child case for popups and context menus  nsCOMPtr<nsIDOMElement> popupContent;  if (identifier == NS_LITERAL_STRING("_child")) {    nsCOMPtr<nsIContent> popup;        nsIAtom* tag = nsXULAtoms::menupopup;    GetImmediateChild(content, tag, getter_AddRefs(popup));    if (popup)      popupContent = do_QueryInterface(popup);    else {      nsCOMPtr<nsIDOMDocumentXBL> nsDoc(do_QueryInterface(domDocument));      nsCOMPtr<nsIDOMNodeList> list;      nsDoc->GetAnonymousNodes(mElement, getter_AddRefs(list));      if (list) {        PRUint32 ctr,listLength;        nsCOMPtr<nsIDOMNode> node;        list->GetLength(&listLength);        for (ctr = 0; ctr < listLength; ctr++) {          list->Item(ctr, getter_AddRefs(node));          nsCOMPtr<nsIContent> childContent(do_QueryInterface(node));          nsCOMPtr<nsIAtom> childTag;          childContent->GetTag(getter_AddRefs(childTag));          if (childTag.get() == tag) {            popupContent = do_QueryInterface(childContent);            break;          }        }      }    }  }  else if (NS_FAILED(rv = domDocument->GetElementById(identifier,                                              getter_AddRefs(popupContent)))) {    // Use getElementById to obtain the popup content and gracefully fail if     // we didn't find any popup content in the document.     NS_ERROR("GetElementById had some kind of spasm.");    return rv;  }  if ( !popupContent )    return NS_OK;  // We have some popup content. Obtain our window.  nsCOMPtr<nsIScriptContext> context;  nsCOMPtr<nsIScriptGlobalObject> global;  document->GetScriptGlobalObject(getter_AddRefs(global));  if (global) {    if ((NS_OK == global->GetContext(getter_AddRefs(context))) && context) {      // Get the DOM window      nsCOMPtr<nsIDOMWindowInternal> domWindow = do_QueryInterface(global);      if (domWindow != nsnull) {        // Find out if we're anchored.        mPopupContent = popupContent.get();        nsAutoString anchorAlignment;        mPopupContent->GetAttribute(NS_LITERAL_STRING("popupanchor"), anchorAlignment);        nsAutoString popupAlignment;        mPopupContent->GetAttribute(NS_LITERAL_STRING("popupalign"), popupAlignment);        PRInt32 xPos = aClientX, yPos = aClientY;                ConvertPosition(mPopupContent, anchorAlignment, popupAlignment, yPos);        if (!anchorAlignment.IsEmpty() && !popupAlignment.IsEmpty())          xPos = yPos = -1;        nsCOMPtr<nsIBoxObject> popupBox;        nsCOMPtr<nsIDOMXULElement> xulPopupElt(do_QueryInterface(mPopupContent));        xulPopupElt->GetBoxObject(getter_AddRefs(popupBox));        nsCOMPtr<nsIPopupBoxObject> popupBoxObject(do_QueryInterface(popupBox));        if (popupBoxObject)          popupBoxObject->ShowPopup(mElement, mPopupContent, xPos, yPos,                                     type.get(), anchorAlignment.get(),                                     popupAlignment.get());      }    }  }  return NS_OK;}