From 8daa992faf0927b265f41c97084e754918648535 Mon Sep 17 00:00:00 2001 From: Tetsuharu OHZEKI Date: Mon, 8 Apr 2013 14:38:52 -0400 Subject: [PATCH] Bug 840722 - Add an object which represents the viewport metadata in browser.js.r=kats --- mobile/android/chrome/content/browser.js | 102 +++++++++++++++-------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index da5111fd435..529322d6a5b 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -1133,7 +1133,7 @@ var BrowserApp = { if (focused) { // _zoomToElement will handle not sending any message if this input is already mostly filling the screen BrowserEventHandler._zoomToElement(focused, -1, false, - aAllowZoom && !this.isTablet && !ViewportHandler.getViewportMetadata(aBrowser.contentWindow).hasMetaViewport); + aAllowZoom && !this.isTablet && !ViewportHandler.getViewportMetadata(aBrowser.contentWindow).isSpecified); } }, @@ -3388,13 +3388,13 @@ Tab.prototype = { aMetadata.minZoom = aMetadata.maxZoom = NaN; } - let scaleRatio = aMetadata.scaleRatio = ViewportHandler.getScaleRatio(); + let scaleRatio = aMetadata.scaleRatio; - if ("defaultZoom" in aMetadata && aMetadata.defaultZoom > 0) + if (aMetadata.defaultZoom > 0) aMetadata.defaultZoom *= scaleRatio; - if ("minZoom" in aMetadata && aMetadata.minZoom > 0) + if (aMetadata.minZoom > 0) aMetadata.minZoom *= scaleRatio; - if ("maxZoom" in aMetadata && aMetadata.maxZoom > 0) + if (aMetadata.maxZoom > 0) aMetadata.maxZoom *= scaleRatio; ViewportHandler.setMetadataForDocument(this.browser.contentDocument, aMetadata); @@ -3422,13 +3422,8 @@ Tab.prototype = { let metadata = this.metadata; if (metadata.autoSize) { - if ("scaleRatio" in metadata) { - viewportW = screenW / metadata.scaleRatio; - viewportH = screenH / metadata.scaleRatio; - } else { - viewportW = screenW; - viewportH = screenH; - } + viewportW = screenW / metadata.scaleRatio; + viewportH = screenH / metadata.scaleRatio; } else { viewportW = metadata.width; viewportH = metadata.height; @@ -4965,14 +4960,7 @@ var ViewportHandler = { }, /** - * Returns an object with the page's preferred viewport properties: - * defaultZoom (optional float): The initial scale when the page is loaded. - * minZoom (optional float): The minimum zoom level. - * maxZoom (optional float): The maximum zoom level. - * width (optional int): The CSS viewport width in px. - * height (optional int): The CSS viewport height in px. - * autoSize (boolean): Resize the CSS viewport when the window resizes. - * allowZoom (boolean): Let the user zoom in or out. + * Returns the ViewportMetadata object. */ getViewportMetadata: function getViewportMetadata(aWindow) { let windowUtils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils); @@ -5004,12 +4992,22 @@ var ViewportHandler = { if (isNaN(scale) && isNaN(minScale) && isNaN(maxScale) && allowZoomStr == "" && widthStr == "" && heightStr == "") { // Only check for HandheldFriendly if we don't have a viewport meta tag let handheldFriendly = windowUtils.getDocumentMetadata("HandheldFriendly"); - if (handheldFriendly == "true") - return { defaultZoom: 1, autoSize: true, allowZoom: true }; + if (handheldFriendly == "true") { + return new ViewportMetadata({ + defaultZoom: 1, + autoSize: true, + allowZoom: true + }); + } let doctype = aWindow.document.doctype; - if (doctype && /(WAP|WML|Mobile)/.test(doctype.publicId)) - return { defaultZoom: 1, autoSize: true, allowZoom: true }; + if (doctype && /(WAP|WML|Mobile)/.test(doctype.publicId)) { + return new ViewportMetadata({ + defaultZoom: 1, + autoSize: true, + allowZoom: true + }); + } hasMetaViewport = false; let defaultZoom = Services.prefs.getIntPref("browser.viewport.defaultZoom"); @@ -5029,7 +5027,7 @@ var ViewportHandler = { (!widthStr && (heightStr == "device-height" || scale == 1.0))); } - return { + return new ViewportMetadata({ defaultZoom: scale, minZoom: minScale, maxZoom: maxScale, @@ -5037,8 +5035,8 @@ var ViewportHandler = { height: height, autoSize: autoSize, allowZoom: allowZoom, - hasMetaViewport: hasMetaViewport - }; + isSpecified: hasMetaViewport + }); }, clamp: function(num, min, max) { @@ -5073,7 +5071,7 @@ var ViewportHandler = { * metadata is available for that document. */ getMetadataForDocument: function getMetadataForDocument(aDocument) { - let metadata = this._metadata.get(aDocument, this.getDefaultMetadata()); + let metadata = this._metadata.get(aDocument, new ViewportMetadata()); return metadata; }, @@ -5083,18 +5081,48 @@ var ViewportHandler = { this._metadata.delete(aDocument); else this._metadata.set(aDocument, aMetadata); - }, - - /** Returns the default viewport metadata for a document. */ - getDefaultMetadata: function getDefaultMetadata() { - return { - autoSize: false, - allowZoom: true, - scaleRatio: ViewportHandler.getScaleRatio() - }; } + }; +/** + * An object which represents the page's preferred viewport properties: + * width (int): The CSS viewport width in px. + * height (int): The CSS viewport height in px. + * defaultZoom (float): The initial scale when the page is loaded. + * minZoom (float): The minimum zoom level. + * maxZoom (float): The maximum zoom level. + * autoSize (boolean): Resize the CSS viewport when the window resizes. + * allowZoom (boolean): Let the user zoom in or out. + * isSpecified (boolean): Whether the page viewport is specified or not. + * scaleRatio (float): The device-pixel-to-CSS-px ratio. + */ +function ViewportMetadata(aMetadata = {}) { + this.width = ("width" in aMetadata) ? aMetadata.width : 0; + this.height = ("height" in aMetadata) ? aMetadata.height : 0; + this.defaultZoom = ("defaultZoom" in aMetadata) ? aMetadata.defaultZoom : 0; + this.minZoom = ("minZoom" in aMetadata) ? aMetadata.minZoom : 0; + this.maxZoom = ("maxZoom" in aMetadata) ? aMetadata.maxZoom : 0; + this.autoSize = ("autoSize" in aMetadata) ? aMetadata.autoSize : false; + this.allowZoom = ("allowZoom" in aMetadata) ? aMetadata.allowZoom : true; + this.isSpecified = ("isSpecified" in aMetadata) ? aMetadata.isSpecified : false; + this.scaleRatio = ViewportHandler.getScaleRatio(); + Object.seal(this); +} + +ViewportMetadata.prototype = { + width: null, + height: null, + defaultZoom: null, + minZoom: null, + maxZoom: null, + autoSize: null, + allowZoom: null, + isSpecified: null, + scaleRatio: null, +}; + + /** * Handler for blocked popups, triggered by DOMUpdatePageReport events in browser.xml */