Bug 567186 - When possible, set viewport size before page finishes loading [r=mfinkle]
authorMatt Brubeck <mbrubeck@mozilla.com>
Thu, 27 May 2010 16:01:41 -0400
changeset 1597 b55a406e4297742f394d20a9dccd8c61182ffc59
parent 1596 84ea374b8a004f24483b989851238acf8e22fc2b
child 1598 18c4932b1725e8c557b834fdf417a6e7d1539f26
push id1436
push usermfinkle@mozilla.com
push dateThu, 27 May 2010 20:01:50 +0000
reviewersmfinkle
bugs567186
Bug 567186 - When possible, set viewport size before page finishes loading [r=mfinkle]
chrome/content/browser-ui.js
chrome/content/browser.js
--- a/chrome/content/browser-ui.js
+++ b/chrome/content/browser-ui.js
@@ -153,16 +153,32 @@ var BrowserUI = {
       if (browsers[i].contentWindow == aEvent.target) {
         Browser.closeTab(Browser.getTabAtIndex(i));
         aEvent.preventDefault();
         break;
       }
     }
   },
 
+  _metaAdded : function(aEvent) {
+    let meta = aEvent.originalTarget;
+    if (!meta)
+      return;
+
+    if (meta.name == "viewport" || meta.name == "HandheldFriendly") {
+      // Must have an owner document and not be in a frame
+      var ownerDoc = meta.ownerDocument;
+      if (!ownerDoc || ownerDoc.defaultView.frameElement)
+        return;
+
+      let tab = Browser.getTabForDocument(ownerDoc);
+      tab.updateViewportMetadata();
+    }
+  },
+
   _linkAdded : function(aEvent) {
     let link = aEvent.originalTarget;
     if (!link || !link.href)
       return;
 
     if (/\bicon\b/i(link.rel)) {
       // Must have an owner document and not be in a frame
       var ownerDoc = link.ownerDocument;
@@ -415,16 +431,17 @@ var BrowserUI = {
     tabs.addEventListener("TabOpen", this, true);
 
     let browsers = document.getElementById("browsers");
     browsers.addEventListener("DOMWindowClose", this, true);
 
     // XXX these really want to listen to only the current browser
     browsers.addEventListener("DOMTitleChanged", this, true);
     browsers.addEventListener("DOMLinkAdded", this, true);
+    browsers.addEventListener("DOMMetaAdded", this, true);
     browsers.addEventListener("DOMWillOpenModalDialog", this, true);
 
     // listening mousedown for automatically dismiss some popups (e.g. larry)
     window.addEventListener("mousedown", this, true);
 
     // listening escape to dismiss dialog on VK_ESCAPE
     window.addEventListener("keypress", this, true);
 
@@ -723,16 +740,19 @@ var BrowserUI = {
         this._domWillOpenModalDialog(aEvent);
         break;
       case "DOMTitleChanged":
         this._titleChanged(aEvent.target);
         break;
       case "DOMLinkAdded":
         this._linkAdded(aEvent);
         break;
+      case "DOMMetaAdded":
+        this._metaAdded(aEvent);
+        break;
       case "DOMWindowClose":
         this._domWindowClose(aEvent);
         break;
       case "TabSelect":
         this._tabSelect(aEvent);
         break;
       case "TabOpen":
       {
--- a/chrome/content/browser.js
+++ b/chrome/content/browser.js
@@ -3076,20 +3076,20 @@ Tab.prototype = {
       }
     }
     this.updateViewportSize();
   },
 
   /** Update browser size when the metadata or the window size changes. */
   updateViewportSize: function updateViewportSize() {
     let browser = this._browser;
-    let metaData = this._browserViewportState.metaData
-    if (!browser || !metaData)
+    if (!browser)
       return;
 
+    let metaData = this._browserViewportState.metaData || {};
     if (!metaData.autoSize) {
       let screenW = window.innerWidth;
       let screenH = window.innerHeight;
       let viewportW = metaData.width;
       let viewportH = metaData.height;
 
       // If (scale * width) < device-width, increase the width (bug 561413).
       let maxInitialZoom = metaData.defaultZoom || metaData.maxZoom;
@@ -3139,22 +3139,27 @@ Tab.prototype = {
         bv.invalidateEntireView();
         bv.setAggressive(false);
         // Sync up browser so previous and forward scroll positions are set. This is a good time to do
         // this because the resulting invalidation is irrelevant.
         bv.ignorePageScroll(true);
         Browser.scrollBrowserToContent();
       }
     }
+
+    this._browserViewportState.metaData = null;
+    this.updateViewportSize();
   },
 
   endLoading: function endLoading() {
     if (!this._loading) throw "Not Loading!";
 
-    this.updateViewportMetadata();
+    if (!this._browserViewportState.metaData)
+      this.updateViewportMetadata();
+
     this.setIcon(this._browser.mIconURL);
     this._loading = false;
 
     if (this == Browser.selectedTab) {
       let bv = Browser._browserView;
       bv.ignorePageScroll(false);
       bv.setAggressive(true);
     }