Bug 575999 - Context menu pops up when clicking on links [r=mfinkle]
authorVivien Nicolas <21@vingtetun.org>
Fri, 09 Jul 2010 04:15:23 -0400
changeset 66357 8752de3db1fc88827913bb3449707ea5b556c8cb
parent 66356 f20e2f6da064bb1fab8ad8b0c54c0ce2c68cfd24
child 66358 712a168618796eef5dab2afae62430ee22eccf2c
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs575999
Bug 575999 - Context menu pops up when clicking on links [r=mfinkle]
mobile/chrome/content/content.js
--- a/mobile/chrome/content/content.js
+++ b/mobile/chrome/content/content.js
@@ -461,48 +461,49 @@ Content.prototype = {
       case "Browser:MouseDown":
         if (this._overlayTimeout)
           return;
 
         let element = elementFromPoint(x, y);
         if (!element)
           return;
 
-        this._sendMouseEvent("mousedown", element, x, y);
-
-        // If we don't release the implicit capture, we'll get dragging problems
-        // when a contextmenu is displayed
-        element.ownerDocument.releaseCapture();
-
         if (element.mozMatchesSelector("*:link,*:visited,*:link *,*:visited *,*[role=button],button,input,option,select,textarea,label")) {
           this._overlayTimeout = content.setTimeout(function() {
             let rects = getContentClientRects(element);
-            sendSyncMessage("Browser:Highlight", { rects: rects });
+            sendAsyncMessage("Browser:Highlight", { rects: rects });
+            this._overlayTimeout = 0;
           }, kTapOverlayTimeout);
         }
+
+        this._startContextTimeout(element);
         break;
 
       case "Browser:MouseUp": {
         let element = elementFromPoint(x, y);
         if (modifiers == Ci.nsIDOMNSEvent.CONTROL_MASK) {
           let uri = Util.getHrefForElement(element);
           if (uri)
             sendAsyncMessage("Browser:OpenURI", { uri: uri });
         } else if (!this._formAssistant.open(element)) {
+          this._sendMouseEvent("mousedown", element, x, y);
           this._sendMouseEvent("mouseup", element, x, y);
         }
         break;
       }
 
       case "Browser:MouseCancel":
-        this._cancelMouseEvent();
         if (this._overlayTimeout) {
           content.clearTimeout(this._overlayTimeout);
           this._overlayTimeout = 0;
         }
+
+        if (this._contextTimeout) {
+          content.clearTimeout(this._contextTimeout);
+        }
         break;
 
       case "Browser:SaveAs":
         if (json.type != Ci.nsIPrintSettings.kOutputFormatPDF)
           return;
 
         let printSettings = Cc["@mozilla.org/gfx/printsettings-service;1"]
                               .getService(Ci.nsIPrintSettingsService)
@@ -575,22 +576,26 @@ Content.prototype = {
       }
     }
 
     let scrollOffset = Util.getScrollOffset(content);
     let windowUtils = Util.getWindowUtils(content);
     windowUtils.sendMouseEvent(aName, aX - scrollOffset.x, aY - scrollOffset.y, 0, 1, 0, true);
   },
 
-  _cancelMouseEvent: function _cancelMouseEvent() {
-    // We use a mouseup with a clickcount=0 to cancel the contextmenu timer in
-    // nsEventStateManager.cpp
-    let scrollOffset = Util.getScrollOffset(content);
-    let windowUtils = Util.getWindowUtils(content);
-    windowUtils.sendMouseEvent("mouseup", scrollOffset.x, scrollOffset.y, 0, 0, 0, true);
+  _contextTimeout: null,
+  _startContextTimeout: function startContextTimeout(aElement) {
+    if (this._contextTimeout)
+      content.clearTimeout(this._contextTimeout);
+
+    this._contextTimeout = content.setTimeout(function() {
+      let event = content.document.createEvent("PopupEvents");
+      event.initEvent("contextmenu", true, true);
+      aElement.dispatchEvent(event);
+    }, 500);
   },
 
   startLoading: function startLoading() {
     this._loading = true;
     this._coalescer.start();
   },
 
   stopLoading: function stopLoading() {
@@ -764,17 +769,17 @@ var ContextHandler = {
       onVoiceLink: false,
       onImage: false,
       onLoadedImage: false,
       linkURL: "",
       linkProtocol: null,
       mediaURL: ""
     };
 
-    let popupNode = elementFromPoint(aEvent.clientX, aEvent.clientY);
+    let popupNode = aEvent.originalTarget;
 
     // Do checks for nodes that never have children.
     if (popupNode.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) {
       // See if the user clicked on an image.
       if (popupNode instanceof Ci.nsIImageLoadingContent && popupNode.currentURI) {
         state.onImage = true;
         state.mediaURL = popupNode.currentURI.spec;
 
@@ -805,17 +810,17 @@ var ContextHandler = {
 
       elem = elem.parentNode;
     }
     
     sendAsyncMessage("Browser:ContextMenu", state);
   }
 };
 
-//ContextHandler.init();
+ContextHandler.init();
 
 
 var FormSubmitObserver = {
   init: function init() {
     gObserverService.addObserver(this, "formsubmit", false);
   },
 
   notify: function notify(aFormElement, aWindow, aActionURI, aCancelSubmit) {