Bug 573443 - Remove Util.Timeout in Util.js [r=mfinkle]
authorVivien Nicolas <21@vingtetun.org>
Mon, 21 Jun 2010 22:04:59 +0200
changeset 66300 21a1e676bc9d3265e1a4859764d9d93f08955a36
parent 66299 e78a4ef0d3f7686490df469fdfdc23a3c4ad25f7
child 66301 a00c3a60c136dd56dca773f316f20f1e561a0023
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
bugs573443
Bug 573443 - Remove Util.Timeout in Util.js [r=mfinkle]
mobile/chrome/content/Util.js
mobile/chrome/content/browser.js
mobile/chrome/content/content.js
--- a/mobile/chrome/content/Util.js
+++ b/mobile/chrome/content/Util.js
@@ -206,80 +206,16 @@ let Util = {
   
   isPortrait: function isPortrait() {
     return (window.innerWidth < 500);
   }
 };
 
 
 /**
- * Helper class to nsITimer that adds a little more pizazz.  Callback can be an
- * object with a notify method or a function.
- */
-Util.Timeout = function(aCallback) {
-  this._callback = aCallback;
-  this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
-  this._active = false;
-}
-
-Util.Timeout.prototype = {
-  /** Timer callback. Don't call this manually. */
-  notify: function notify() {
-    this._active = false;
-    if (this._callback.notify)
-      this._callback.notify();
-    else
-      this._callback.apply(null);
-  },
-
-  /** Do the callback once.  Cancels other timeouts on this object. */
-  once: function once(aDelay, aCallback) {
-    if (aCallback)
-      this._callback = aCallback;
-    this.clear();
-    this._timer.initWithCallback(this, aDelay, this._timer.TYPE_ONE_SHOT);
-    this._active = true;
-    return this;
-  },
-
-  /** Do the callback every aDelay msecs. Cancels other timeouts on this object. */
-  interval: function interval(aDelay, aCallback) {
-    if (aCallback)
-      this._callback = aCallback;
-    this.clear();
-    this._timer.initWithCallback(this, aDelay, this._timer.TYPE_REPEATING_SLACK);
-    this._active = true;
-    return this;
-  },
-
-  /** Clear any pending timeouts. */
-  clear: function clear() {
-    if (this._active) {
-      this._timer.cancel();
-      this._active = false;
-    }
-    return this;
-  },
-
-  /** If there is a pending timeout, call it and cancel the timeout. */
-  flush: function flush() {
-    if (this._active) {
-      this.clear();
-      this.notify();
-    }
-    return this;
-  },
-
-  /** Return true iff we are waiting for a callback. */
-  isPending: function isPending() {
-    return this._active;
-  }
-};
-
-/**
  * Cache of commonly used elements.
  */
 let Elements = {};
 
 [
   ["browserBundle",      "bundle_browser"],
   ["contentShowing",     "bcast_contentShowing"],
   ["stack",              "stack"],
@@ -292,16 +228,17 @@ let Elements = {};
     let element = document.getElementById(id);
     if (!element)
       return null;
     delete Elements[name];
     return Elements[name] = element;
   });
 });
 
+
 /**
  * Simple Point class.
  *
  * Any method that takes an x and y may also take a point.
  */
 function Point(x, y) {
   this.set(x, y);
 }
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -570,17 +570,17 @@ var Browser = {
                                      label, false, "", null);
       }
       gPrefService.clearUserPref("extensions.disabledAddons");
     }
 
     // Force commonly used border-images into the image cache
     ImagePreloader.cache();
 
-    messageManager.addMessageListener("FennecViewportMetadata", this);
+    messageManager.addMessageListener("Browser:ViewportMetadata", this);
     messageManager.addMessageListener("Browser:MozApplicationManifest", OfflineApps);
 
     // broadcast a UIReady message so add-ons know we are finished with startup
     let event = document.createEvent("Events");
     event.initEvent("UIReady", true, false);
     window.dispatchEvent(event);
   },
 
@@ -1397,17 +1397,17 @@ var Browser = {
   },
 
   forceChromeReflow: function forceChromeReflow() {
     let dummy = getComputedStyle(document.documentElement, "").width;
   },
 
   receiveMessage: function receiveMessage(aMessage) {
     switch (aMessage.name) {
-      case "FennecViewportMetadata":
+      case "Browser:ViewportMetadata":
         let tab = Browser.getTabForBrowser(aMessage.target);
         tab.updateViewportMetadata(aMessage.json);
         break;
     }
   }
 };
 
 Browser.MainDragger = function MainDragger(browserView) {
--- a/mobile/chrome/content/content.js
+++ b/mobile/chrome/content/content.js
@@ -195,26 +195,33 @@ function getBoundingContentRect(contentE
   return new Rect(r.left + offset.x, r.top + offset.y, r.width, r.height);
 }
 
 
 /** Reponsible for sending messages about viewport size changes and painting. */
 function Coalescer() {
   this._pendingDirtyRect = new Rect(0, 0, 0, 0);
   this._pendingSizeChange = null;
-  this._timer = new Util.Timeout(this);
+  this._timer = null;
   // XXX When moving back and forward in docShell history, MozAfterPaint does not get called properly and
   // some dirty rectangles are never flagged properly.  To fix this, coalescer will fake a paint event that
   // dirties the entire viewport.
   this._incremental = false;
 }
 
 Coalescer.prototype = {
-  notify: function notify() {
-    this.flush();
+  start: function startCoalescing() {
+    this._emptyPage();
+    this._timer = content.document.defaultView.setInterval(this, 1000);
+  },
+
+  stop: function stopCoalescing() {
+    content.document.defaultView.clearInterval(this._timer);
+    this._timer = null;
+    this.flush()
   },
 
   handleEvent: function handleEvent(aEvent) {
     switch (aEvent.type) {
       case "MozAfterPaint": {
         let win = aEvent.originalTarget;
         let scrollOffset = Util.getScrollOffset(win);
         this.dirty(scrollOffset, aEvent.clientRects);
@@ -222,17 +229,17 @@ Coalescer.prototype = {
       }
       case "MozScrolledAreaChanged": {
         // XXX if it's possible to get a scroll area change with the same values,
         // it would be optimal if this didn't send the same message twice.
         let doc = aEvent.originalTarget;
         let win = doc.defaultView;
         let scrollOffset = Util.getScrollOffset(win);
         if (win.parent != win) // We are only interested in root scroll pane changes
-	  return;
+          return;
         this.sizeChange(scrollOffset, aEvent.x, aEvent.y, aEvent.width, aEvent.height);
         break;
       }
       case "MozApplicationManifest": {
         let doc = aEvent.originalTarget;
 
         sendAsyncMessage("Browser:MozApplicationManifest", {
           location: doc.documentURIObject.spec,
@@ -243,59 +250,51 @@ Coalescer.prototype = {
       }
       case "scroll":
         sendSyncMessage("Browser:PageScroll", {});
         break;
     }
   },
 
   /** Next scroll size change event will invalidate all previous content. See constructor. */
-  emptyPage: function emptyPage() {
+  _emptyPage: function _emptyPage() {
     this._incremental = false;
   },
 
-  startCoalescing: function startCoalescing() {
-    this._timer.interval(1000);
-  },
-  
-  stopCoalescing: function stopCoalescing() {
-    this._timer.flush();
-  },
-
   sizeChange: function sizeChange(scrollOffset, x, y, width, height) {
     // Adjust width and height from the incoming event properties so that we
     // ignore changes to width and height contributed by growth in page
     // quadrants other than x > 0 && y > 0.
     x = x + scrollOffset.x;
     y = y + scrollOffset.y;
     this._pendingSizeChange = {
       width: width + (x < 0 ? x : 0),
       height: height + (y < 0 ? y : 0)
     };
 
     // Clear any pending dirty rectangles since entire viewport will be invalidated
     // anyways.
-    var rect = this._pendingDirtyRect;
+    let rect = this._pendingDirtyRect;
     rect.top = rect.bottom;
     rect.left = rect.right;
 
-    if (!this._timer.isPending())
+    if (this._timer == null)
       this.flush()
   },
 
   dirty: function dirty(scrollOffset, clientRects) {
     if (!this._pendingSizeChange) {
-      var unionRect = this._pendingDirtyRect;
-      for (var i = clientRects.length - 1; i >= 0; i--) {
-        var e = clientRects.item(i);
+      let unionRect = this._pendingDirtyRect;
+      for (let i = clientRects.length - 1; i >= 0; i--) {
+        let e = clientRects.item(i);
         unionRect.expandToContain(new Rect(
           e.left + scrollOffset.x, e.top + scrollOffset.y, e.width, e.height));
       }
 
-      if (!this._timer.isPending())
+      if (this._timer == null)
         this.flush()
     }
   },
 
   flush: function flush() {
     let dirtyRect = this._pendingDirtyRect;
     let sizeChange = this._pendingSizeChange;
     if (sizeChange) {
@@ -337,17 +336,17 @@ ProgressController.prototype = {
   // Default loading callbacks do nothing
   _defaultLoadingController: {
     startLoading: function() {},
     stopLoading: function() {}
   },
 
   onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
     // ignore notification that aren't about the main document (iframes, etc)
-    var win = aWebProgress.DOMWindow;
+    let win = aWebProgress.DOMWindow;
     if (win != win.parent)
       return;
 
     // If you want to observe other state flags, be sure they're listed in the
     // Tab._createBrowser's call to addProgressListener
     if (aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
       if (aStateFlags & Ci.nsIWebProgressListener.STATE_START) {
         this._loadingController.startLoading();
@@ -647,17 +646,17 @@ FormNavigator.prototype = {
     let chosenRadios = {};
     for (let i=0; i < nodes.length; i++) {
       let node = nodes[i];
       if (node.type == "radio" && (!chosenRadios.hasOwnProperty(node.name) || node.checked))
         chosenRadios[node.name] = node;
     }
 
     // Second pass: Exclude all other radio buttons from the list.
-    var result = [];
+    let result = [];
     for (let i=0; i < nodes.length; i++) {
       let node = nodes[i];
       if (node.type == "radio" && chosenRadios[node.name] != node)
         continue;
       result.push(node);
     }
     return result;
   },
@@ -798,23 +797,22 @@ Content.prototype = {
       y = point.y;
     }
 
     windowUtils.sendMouseEvent(name, x - scrollOffset.x, y - scrollOffset.y, 0, 1, 0, true);
   },
 
   startLoading: function startLoading() {
     this._loading = true;
-    this._coalescer.emptyPage();
-    this._coalescer.startCoalescing();
+    this._coalescer.start();
   },
 
   stopLoading: function stopLoading() {
     this._loading = false;
-    this._coalescer.stopCoalescing();
+    this._coalescer.stop();
   },
 
   isSelected: function isSelected() {
     return this._selected;
   }
 };
 
 let contentObject = new Content();