nsCOMPtr<nsIPrefService> prefService = do_GetService(NS_PREFSERVICE_CONTRACTID);  NS_ENSURE_TRUE(prefService, NS_ERROR_FAILURE);  nsCOMPtr<nsIPrefBranch> prefBranch; prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));  PRBool eventEnabled; nsresult rv = prefBranch->GetBoolPref("dom.event.contextmenu.enabled", &eventEnabled); if (NS_SUCCEEDED(rv) && !eventEnabled) {
PRBool eventEnabled = nsContentUtils::GetBoolPref("dom.event.contextmenu.enabled", PR_TRUE); if (!eventEnabled) {
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;  }  // Get the node that was clicked on.  nsCOMPtr<nsIDOMEventTarget> target;  mouseEvent->GetTarget(getter_AddRefs(target));  nsCOMPtr<nsIDOMNode> targetNode = do_QueryInterface(target);  PRBool preventDefault;  nsUIEvent->GetPreventDefault(&preventDefault);  if (preventDefault && targetNode && popupType == eXULPopupType_context) {    // Someone called preventDefault on a context menu.    // Let's make sure they are allowed to do so.    nsCOMPtr<nsIPrefService> prefService =      do_GetService(NS_PREFSERVICE_CONTRACTID);    NS_ENSURE_TRUE(prefService, NS_ERROR_FAILURE);    nsCOMPtr<nsIPrefBranch> prefBranch;    prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));    PRBool eventEnabled;    nsresult rv = prefBranch->GetBoolPref("dom.event.contextmenu.enabled",                                          &eventEnabled);    if (NS_SUCCEEDED(rv) && !eventEnabled) {      // The user wants his contextmenus.  Let's make sure that this is a website      // and not chrome since there could be places in chrome which don't want      // contextmenus.      nsCOMPtr<nsIDocument> doc;      nsCOMPtr<nsIPrincipal> prin;      nsContentUtils::GetDocumentAndPrincipal(targetNode,                                              getter_AddRefs(doc),                                              getter_AddRefs(prin));      if (prin) {        nsCOMPtr<nsIPrincipal> system;        nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(system));        if (prin != system) {          // This isn't chrome.  Cancel the preventDefault() and          // let the event go forth.          preventDefault = PR_FALSE;        }      }    }  }  if (preventDefault) {    // someone called preventDefault. bail.    return NS_OK;  }  // 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);    nsIAtom *tag = targetContent->Tag();    if (tag == nsXULAtoms::menu || tag == nsXULAtoms::menuitem)      return NS_OK;  }  // Get the document with the popup.  nsCOMPtr<nsIContent> content = do_QueryInterface(mElement);  // Turn the document into a XUL document so we can use SetPopupNode.  nsCOMPtr<nsIDOMXULDocument> xulDocument = do_QueryInterface(content->GetDocument());  if (!xulDocument) {    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;}