Bug 728614 - Part 4: Remove the viewport structure from browser.js; r=Cwiiis
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 21 Feb 2012 13:17:56 -0500
changeset 89204 75fdc3a367da0a5d0f314e1d1dd752af36d0cf39
parent 89203 77ba22defa986dd6de6d2304aa092f4c315b7f44
child 89205 181ba66322a009a0b84d117a015980ea91e54eb3
push id22242
push userkgupta@mozilla.com
push dateWed, 14 Mar 2012 15:19:09 +0000
treeherdermozilla-central@936ef50fa498 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersCwiiis
bugs728614
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 728614 - Part 4: Remove the viewport structure from browser.js; r=Cwiiis
mobile/android/chrome/content/browser.js
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -826,18 +826,18 @@ var BrowserApp = {
 
       // tell gecko to scroll the field into view. this will scroll any nested scrollable elements
       // as well as the browser's content window, and modify the scrollX and scrollY on the content window.
       focused.scrollIntoView(false);
 
       // As Gecko isn't aware of the zoom level we're drawing with, the element may not entirely be in view
       // yet. Check for that, and scroll some extra to compensate, if necessary.
       let focusedRect = focused.getBoundingClientRect();
-      let visibleContentWidth = tab._viewport.width / tab._viewport.zoom;
-      let visibleContentHeight = tab._viewport.height / tab._viewport.zoom;
+      let visibleContentWidth = gScreenWidth / tab._zoom;
+      let visibleContentHeight = gScreenHeight / tab._zoom;
 
       let positionChanged = false;
       let scrollX = win.scrollX;
       let scrollY = win.scrollY;
 
       if (focusedRect.right >= visibleContentWidth && focusedRect.left > 0) {
         // the element is too far off the right side, so we need to scroll to the right more
         scrollX += Math.min(focusedRect.left, focusedRect.right - visibleContentWidth);
@@ -1443,18 +1443,17 @@ let gTabIDFactory = 0;
 let gScreenWidth = 1;
 let gScreenHeight = 1;
 
 function Tab(aURL, aParams) {
   this.browser = null;
   this.id = 0;
   this.showProgress = true;
   this.create(aURL, aParams);
-  this._viewport = { x: 0, y: 0, width: gScreenWidth, height: gScreenHeight,
-                     pageWidth: gScreenWidth, pageHeight: gScreenHeight, zoom: 1.0 };
+  this._zoom = 1.0;
   this.documentIdForCurrentViewport = null;
   this.userScrollPos = { x: 0, y: 0 };
   this._pluginCount = 0;
   this._pluginOverlayShowing = false;
 }
 
 Tab.prototype = {
   create: function(aURL, aParams) {
@@ -1585,95 +1584,97 @@ Tab.prototype = {
     aViewport.y /= aViewport.zoom;
 
     // Set scroll position
     let win = this.browser.contentWindow;
     win.scrollTo(aViewport.x, aViewport.y);
     this.userScrollPos.x = win.scrollX;
     this.userScrollPos.y = win.scrollY;
 
-    this._viewport.width = gScreenWidth = aViewport.width;
-    this._viewport.height = gScreenHeight = aViewport.height;
+    gScreenWidth = aViewport.width;
+    gScreenHeight = aViewport.height;
     dump("### gScreenWidth = " + gScreenWidth + "\n");
 
     let zoom = aViewport.zoom;
     let cwu = window.top.QueryInterface(Ci.nsIInterfaceRequestor)
                          .getInterface(Ci.nsIDOMWindowUtils);
 
-    if (Math.abs(zoom - this._viewport.zoom) >= 1e-6) {
-      this._viewport.zoom = zoom;
+    if (Math.abs(zoom - this._zoom) >= 1e-6) {
+      this._zoom = zoom;
       cwu.setResolution(zoom, zoom);
     }
 
     cwu.setDisplayPortForElement(-kBufferAmount / zoom, -kBufferAmount / zoom,
                                  (gScreenWidth + kBufferAmount * 2) / zoom,
                                  (gScreenHeight + kBufferAmount * 2) / zoom,
                                  this.browser.contentDocument.documentElement);
   },
 
   get viewport() {
+    let viewport = {
+      width: gScreenWidth,
+      height: gScreenHeight,
+      pageWidth: gScreenWidth,
+      pageHeight: gScreenHeight,
+      zoom: this._zoom
+    };
+
     // Update the viewport to current dimensions
-    this._viewport.x = this.browser.contentWindow.scrollX || 0;
-    this._viewport.y = this.browser.contentWindow.scrollY || 0;
+    viewport.x = this.browser.contentWindow.scrollX || 0;
+    viewport.y = this.browser.contentWindow.scrollY || 0;
 
     // Transform coordinates based on zoom
-    this._viewport.x = Math.round(this._viewport.x * this._viewport.zoom);
-    this._viewport.y = Math.round(this._viewport.y * this._viewport.zoom);
+    viewport.x = Math.round(viewport.x * viewport.zoom);
+    viewport.y = Math.round(viewport.y * viewport.zoom);
 
     let doc = this.browser.contentDocument;
     if (doc != null) {
-      let pageWidth = this._viewport.width, pageHeight = this._viewport.height;
+      let pageWidth = viewport.width, pageHeight = viewport.height;
       if (doc instanceof SVGDocument) {
         let rect = doc.rootElement.getBoundingClientRect();
         // we need to add rect.left and rect.top twice so that the SVG is drawn
         // centered on the page; if we add it only once then the SVG will be
         // on the bottom-right of the page and if we don't add it at all then
         // we end up with a cropped SVG (see bug 712065)
         pageWidth = Math.ceil(rect.left + rect.width + rect.left);
         pageHeight = Math.ceil(rect.top + rect.height + rect.top);
       } else {
         let body = doc.body || { scrollWidth: pageWidth, scrollHeight: pageHeight };
         let html = doc.documentElement || { scrollWidth: pageWidth, scrollHeight: pageHeight };
         pageWidth = Math.max(body.scrollWidth, html.scrollWidth);
         pageHeight = Math.max(body.scrollHeight, html.scrollHeight);
       }
 
       /* Transform the page width and height based on the zoom factor. */
-      pageWidth *= this._viewport.zoom;
-      pageHeight *= this._viewport.zoom;
+      pageWidth *= viewport.zoom;
+      pageHeight *= viewport.zoom;
 
       /*
        * Avoid sending page sizes of less than screen size before we hit DOMContentLoaded, because
        * this causes the page size to jump around wildly during page load. After the page is loaded,
        * send updates regardless of page size; we'll zoom to fit the content as needed.
        */
       if (doc.readyState === 'complete' || (pageWidth >= gScreenWidth && pageHeight >= gScreenHeight)) {
-        this._viewport.pageWidth = pageWidth;
-        this._viewport.pageHeight = pageHeight;
+        viewport.pageWidth = pageWidth;
+        viewport.pageHeight = pageHeight;
       }
     }
 
-    return this._viewport;
+    return viewport;
   },
 
   updateViewport: function(aReset, aZoomLevel) {
     dump("### JS side updateViewport " + aReset + " zoom " + aZoomLevel + "\n");
 
-    if (!aZoomLevel)
-      aZoomLevel = this.getDefaultZoomLevel();
-
-    let win = this.browser.contentWindow;
-    let zoom = (aReset ? aZoomLevel : this._viewport.zoom);
-    let xpos = ((aReset && win) ? win.scrollX * zoom : this._viewport.x);
-    let ypos = ((aReset && win) ? win.scrollY * zoom : this._viewport.y);
-
-    this.viewport = { x: xpos, y: ypos,
-                      width: this._viewport.width, height: this._viewport.height,
-                      pageWidth: gScreenWidth, pageHeight: gScreenHeight,
-                      zoom: zoom };
+    if (aReset) {
+      if (!aZoomLevel)
+        aZoomLevel = this.getDefaultZoomLevel();
+      this._zoom = aZoomLevel;
+    }
+
     this.sendViewportUpdate();
   },
 
   sendViewportUpdate: function() {
     if (BrowserApp.selectedTab != this)
       return;
     sendMessageToJava({
       gecko: {
@@ -2007,18 +2008,18 @@ Tab.prototype = {
   },
 
   /** Update viewport when the metadata or the window size changes. */
   updateViewportSize: function updateViewportSize() {
     let browser = this.browser;
     if (!browser)
       return;
 
-    let screenW = this._viewport.width;
-    let screenH = this._viewport.height;
+    let screenW = gScreenWidth;
+    let screenH = gScreenHeight;
     let viewportW, viewportH;
 
     let metadata = this.metadata;
     if (metadata.autoSize) {
       if ("scaleRatio" in metadata) {
         viewportW = screenW / metadata.scaleRatio;
         viewportH = screenH / metadata.scaleRatio;
       } else {
@@ -2040,35 +2041,34 @@ Tab.prototype = {
       if (!validW)
         viewportW = validH ? (viewportH * (screenW / screenH)) : BrowserApp.defaultBrowserWidth;
       if (!validH)
         viewportH = viewportW * (screenH / screenW);
     }
 
     // Make sure the viewport height is not shorter than the window when
     // the page is zoomed out to show its full width.
-    let minScale = this.getPageZoomLevel(screenW);
+    let minScale = this.getPageZoomLevel();
     viewportH = Math.max(viewportH, screenH / minScale);
 
     let oldBrowserWidth = this.browserWidth;
     this.setBrowserSize(viewportW, viewportH);
 
     // Avoid having the scroll position jump around after device rotation.
     let win = this.browser.contentWindow;
     this.userScrollPos.x = win.scrollX;
     this.userScrollPos.y = win.scrollY;
 
     // If the browser width changes, we change the zoom proportionally. This ensures sensible
     // behavior when rotating the device on pages with automatically-resizing viewports.
 
     if (viewportW == oldBrowserWidth)
       return;
 
-    let viewport = this.viewport;
-    let newZoom = oldBrowserWidth * viewport.zoom / viewportW;
+    let newZoom = oldBrowserWidth * this._zoom / viewportW;
     this.updateViewport(true, newZoom);
   },
 
   getDefaultZoomLevel: function getDefaultZoomLevel() {
     let md = this.metadata;
     if ("defaultZoom" in md && md.defaultZoom)
       return md.defaultZoom;
 
@@ -2077,17 +2077,17 @@ Tab.prototype = {
   },
 
   getPageZoomLevel: function getPageZoomLevel() {
     // This may get called during a Viewport:Change message while the document
     // has not loaded yet.
     if (!this.browser.contentDocument || !this.browser.contentDocument.body)
       return 1.0;
 
-    return this._viewport.width / this.browser.contentDocument.body.clientWidth;
+    return gScreenWidth / this.browser.contentDocument.body.clientWidth;
   },
 
   setBrowserSize: function(aWidth, aHeight) {
     this.browserWidth = aWidth;
 
     if (!this.browser.contentWindow)
       return;
     let cwu = this.browser.contentWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
@@ -2259,34 +2259,34 @@ var BrowserEventHandler = {
   },
 
   onDoubleTap: function(aData) {
     let data = JSON.parse(aData);
 
     let rect = {};
     let win = BrowserApp.selectedBrowser.contentWindow;
     
-    let zoom = BrowserApp.selectedTab._viewport.zoom;
+    let zoom = BrowserApp.selectedTab._zoom;
     let element = ElementTouchHelper.anyElementFromPoint(win, data.x, data.y);
     if (!element) {
       this._zoomOut();
       return;
     }
 
     win = element.ownerDocument.defaultView;
     while (element && win.getComputedStyle(element,null).display == "inline")
       element = element.parentNode;
     if (!element || element == this._zoomedToElement) {
       this._zoomOut();
     } else if (element) {
       const margin = 15;
       this._zoomedToElement = element;
       rect = ElementTouchHelper.getBoundingContentRect(element);
 
-      let zoom = BrowserApp.selectedTab.viewport.zoom;
+      let zoom = BrowserApp.selectedTab._zoom;
       rect.x *= zoom;
       rect.y *= zoom;
       rect.w *= zoom;
       rect.h *= zoom;
 
       setTimeout(function() {
         rect.type = "Browser:ZoomToRect";
         rect.x -= margin;
@@ -2740,24 +2740,25 @@ var FormAssistant = {
           break;
 
         // Keep track of input element so we can fill it in if the user
         // selects an autocomplete suggestion
         this._currentInputElement = currentElement;
         let suggestions = this._getAutocompleteSuggestions(currentElement.value, currentElement);
 
         let rect = ElementTouchHelper.getBoundingContentRect(currentElement);
-        let viewport = BrowserApp.selectedTab.viewport;
+        let zoom = BrowserApp.selectedTab._zoom;
+        let win  = BrowserApp.selectedTab.browser.contentWindow;
 
         sendMessageToJava({
           gecko: {
             type:  "FormAssist:AutoComplete",
             suggestions: suggestions,
-            rect: [rect.x - (viewport.x / viewport.zoom), rect.y - (viewport.y / viewport.zoom), rect.w, rect.h],
-            zoom: viewport.zoom
+            rect: [rect.x - (win.scrollX / zoom), rect.y - (win.scrollY / zoom), rect.w, rect.h],
+            zoom: zoom
           }
         });
     }
   },
 
   _isAutocomplete: function (aElement) {
     if (!(aElement instanceof HTMLInputElement) ||
         (aElement.getAttribute("type") == "password") ||