Bug 821801: Make nsContentUtils::ViewportInfo into a separate class in order to allow better state tracking. [r=kats]

This commit is contained in:
Scott Johnson 2012-12-17 15:24:41 -06:00
parent bd4c006009
commit 7934ac6871
9 changed files with 174 additions and 113 deletions

View File

@ -40,6 +40,7 @@ nsDOMFile.h \
nsLineBreaker.h \
nsReferencedElement.h \
nsTreeSanitizer.h \
nsViewportInfo.h \
nsXMLNameSpaceMap.h \
nsHostObjectProtocolHandler.h \
$(NULL)

View File

@ -101,6 +101,8 @@ class nsPIDOMWindow;
class nsIDocumentLoaderFactory;
class nsIDOMHTMLInputElement;
class nsViewportInfo;
namespace mozilla {
class Selection;
@ -130,40 +132,6 @@ enum EventNameType {
EventNameType_All = 0xFFFF
};
/**
* Information retrieved from the <meta name="viewport"> tag. See
* GetViewportInfo for more information on this functionality.
*/
struct ViewportInfo
{
// Default zoom indicates the level at which the display is 'zoomed in'
// initially for the user, upon loading of the page.
double defaultZoom;
// The minimum zoom level permitted by the page.
double minZoom;
// The maximum zoom level permitted by the page.
double maxZoom;
// The width of the viewport, specified by the <meta name="viewport"> tag,
// in CSS pixels.
uint32_t width;
// The height of the viewport, specified by the <meta name="viewport"> tag,
// in CSS pixels.
uint32_t height;
// Whether or not we should automatically size the viewport to the device's
// width. This is true if the document has been optimized for mobile, and
// the width property of a specified <meta name="viewport"> tag is either
// not specified, or is set to the special value 'device-width'.
bool autoSize;
// Whether or not the user can zoom in and out on the page. Default is true.
bool allowZoom;
};
struct EventNameMapping
{
nsIAtom* mAtom;
@ -1549,16 +1517,9 @@ public:
* NOTE: If the site is optimized for mobile (via the doctype), this
* will return viewport information that specifies default information.
*/
static ViewportInfo GetViewportInfo(nsIDocument* aDocument,
uint32_t aDisplayWidth,
uint32_t aDisplayHeight);
/**
* Constrain the viewport calculations from the GetViewportInfo() function
* in order to always return sane minimum/maximum values. This modifies the
* ViewportInfo struct passed as an input parameter, in place.
*/
static void ConstrainViewportValues(ViewportInfo& aViewInfo);
static nsViewportInfo GetViewportInfo(nsIDocument* aDocument,
uint32_t aDisplayWidth,
uint32_t aDisplayHeight);
/**
* The device-pixel-to-CSS-px ratio used to adjust meta viewport values.

View File

@ -0,0 +1,107 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsViewportInfo_h___
#define nsViewportInfo_h___
#include "nsContentUtils.h"
/**
* Default values for the nsViewportInfo class.
*/
static const double kViewportMinScale = 0.0;
static const double kViewportMaxScale = 10.0;
static const uint32_t kViewportMinWidth = 200;
static const uint32_t kViewportMaxWidth = 10000;
static const uint32_t kViewportMinHeight = 223;
static const uint32_t kViewportMaxHeight = 10000;
static const int32_t kViewportDefaultScreenWidth = 980;
/**
* Information retrieved from the <meta name="viewport"> tag. See
* nsContentUtils::GetViewportInfo for more information on this functionality.
*/
class NS_STACK_CLASS nsViewportInfo
{
public:
nsViewportInfo(uint32_t aDisplayWidth, uint32_t aDisplayHeight) :
mDefaultZoom(1.0),
mMinZoom(kViewportMinScale),
mMaxZoom(kViewportMaxScale),
mWidth(aDisplayWidth),
mHeight(aDisplayHeight),
mAutoSize(true),
mAllowZoom(true)
{
ConstrainViewportValues();
}
nsViewportInfo(double aDefaultZoom,
double aMinZoom,
double aMaxZoom,
uint32_t aWidth,
uint32_t aHeight,
bool aAutoSize,
bool aAllowZoom) :
mDefaultZoom(aDefaultZoom),
mMinZoom(aMinZoom),
mMaxZoom(aMaxZoom),
mWidth(aWidth),
mHeight(aHeight),
mAutoSize(aAutoSize),
mAllowZoom(aAllowZoom)
{
ConstrainViewportValues();
}
double GetDefaultZoom() { return mDefaultZoom; }
void SetDefaultZoom(const double aDefaultZoom);
double GetMinZoom() { return mMinZoom; }
double GetMaxZoom() { return mMaxZoom; }
uint32_t GetWidth() { return mWidth; }
uint32_t GetHeight() { return mHeight; }
bool IsAutoSizeEnabled() { return mAutoSize; }
bool IsZoomAllowed() { return mAllowZoom; }
private:
/**
* Constrain the viewport calculations from the
* nsContentUtils::GetViewportInfo() function in order to always return
* sane minimum/maximum values.
*/
void ConstrainViewportValues();
// Default zoom indicates the level at which the display is 'zoomed in'
// initially for the user, upon loading of the page.
double mDefaultZoom;
// The minimum zoom level permitted by the page.
double mMinZoom;
// The maximum zoom level permitted by the page.
double mMaxZoom;
// The width of the viewport, specified by the <meta name="viewport"> tag,
// in CSS pixels.
uint32_t mWidth;
// The height of the viewport, specified by the <meta name="viewport"> tag,
// in CSS pixels.
uint32_t mHeight;
// Whether or not we should automatically size the viewport to the device's
// width. This is true if the document has been optimized for mobile, and
// the width property of a specified <meta name="viewport"> tag is either
// not specified, or is set to the special value 'device-width'.
bool mAutoSize;
// Whether or not the user can zoom in and out on the page. Default is true.
bool mAllowZoom;
};
#endif

View File

@ -120,6 +120,7 @@ CPPSRCS = \
nsTraversal.cpp \
nsTreeSanitizer.cpp \
nsTreeWalker.cpp \
nsViewportInfo.cpp \
WebSocket.cpp \
nsXHTMLContentSerializer.cpp \
nsXMLContentSerializer.cpp \

View File

@ -169,6 +169,7 @@
#include "DecoderTraits.h"
#include "nsWrapperCacheInlines.h"
#include "nsViewportInfo.h"
extern "C" int MOZ_XMLTranslateEntity(const char* ptr, const char* end,
const char** next, PRUnichar* result);
@ -241,17 +242,6 @@ bool nsContentUtils::sFragmentParsingActive = false;
namespace {
/**
* Default values for the ViewportInfo structure.
*/
static const double kViewportMinScale = 0.0;
static const double kViewportMaxScale = 10.0;
static const uint32_t kViewportMinWidth = 200;
static const uint32_t kViewportMaxWidth = 10000;
static const uint32_t kViewportMinHeight = 223;
static const uint32_t kViewportMaxHeight = 10000;
static const int32_t kViewportDefaultScreenWidth = 980;
static const char kJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
static NS_DEFINE_CID(kParserServiceCID, NS_PARSERSERVICE_CID);
static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
@ -5038,32 +5028,11 @@ static void ProcessViewportToken(nsIDocument *aDocument,
(c == '\t') || (c == '\n') || (c == '\r'))
/* static */
void
nsContentUtils::ConstrainViewportValues(ViewportInfo& aViewInfo)
{
// Constrain the min/max zoom as specified at:
// dev.w3.org/csswg/css-device-adapt section 6.2
aViewInfo.maxZoom = NS_MAX(aViewInfo.minZoom, aViewInfo.maxZoom);
aViewInfo.defaultZoom = NS_MIN(aViewInfo.defaultZoom, aViewInfo.maxZoom);
aViewInfo.defaultZoom = NS_MAX(aViewInfo.defaultZoom, aViewInfo.minZoom);
}
/* static */
ViewportInfo
nsViewportInfo
nsContentUtils::GetViewportInfo(nsIDocument *aDocument,
uint32_t aDisplayWidth,
uint32_t aDisplayHeight)
{
ViewportInfo ret;
ret.defaultZoom = 1.0;
ret.autoSize = true;
ret.allowZoom = true;
ret.width = aDisplayWidth;
ret.height = aDisplayHeight;
ret.minZoom = kViewportMinScale;
ret.maxZoom = kViewportMaxScale;
nsAutoString viewport;
aDocument->GetHeaderData(nsGkAtoms::viewport, viewport);
if (viewport.IsEmpty()) {
@ -5080,7 +5049,7 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument,
(docId.Find("Mobile") != -1) ||
(docId.Find("WML") != -1))
{
nsContentUtils::ConstrainViewportValues(ret);
nsViewportInfo ret(aDisplayWidth, aDisplayHeight);
return ret;
}
}
@ -5089,7 +5058,7 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument,
nsAutoString handheldFriendly;
aDocument->GetHeaderData(nsGkAtoms::handheldFriendly, handheldFriendly);
if (handheldFriendly.EqualsLiteral("true")) {
nsContentUtils::ConstrainViewportValues(ret);
nsViewportInfo ret(aDisplayWidth, aDisplayHeight);
return ret;
}
}
@ -5219,15 +5188,8 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument,
allowZoom = false;
}
ret.allowZoom = allowZoom;
ret.width = width;
ret.height = height;
ret.defaultZoom = scaleFloat;
ret.minZoom = scaleMinFloat;
ret.maxZoom = scaleMaxFloat;
ret.autoSize = autoSize;
nsContentUtils::ConstrainViewportValues(ret);
nsViewportInfo ret(scaleFloat, scaleMinFloat, scaleMaxFloat, width, height,
autoSize, allowZoom);
return ret;
}

View File

@ -0,0 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsViewportInfo.h"
void
nsViewportInfo::SetDefaultZoom(const double aDefaultZoom)
{
MOZ_ASSERT(aDefaultZoom >= 0.0f);
mDefaultZoom = aDefaultZoom;
}
void
nsViewportInfo::ConstrainViewportValues()
{
// Constrain the min/max zoom as specified at:
// dev.w3.org/csswg/css-device-adapt section 6.2
mMaxZoom = NS_MAX(mMinZoom, mMaxZoom);
mDefaultZoom = NS_MIN(mDefaultZoom, mMaxZoom);
mDefaultZoom = NS_MAX(mDefaultZoom, mMinZoom);
}

View File

@ -66,6 +66,7 @@
#include "nsDOMBlobBuilder.h"
#include "nsIDOMFileHandle.h"
#include "nsPrintfCString.h"
#include "nsViewportInfo.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -266,14 +267,14 @@ nsDOMWindowUtils::GetViewportInfo(uint32_t aDisplayWidth,
nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
NS_ENSURE_STATE(doc);
ViewportInfo info = nsContentUtils::GetViewportInfo(doc, aDisplayWidth, aDisplayHeight);
*aDefaultZoom = info.defaultZoom;
*aAllowZoom = info.allowZoom;
*aMinZoom = info.minZoom;
*aMaxZoom = info.maxZoom;
*aWidth = info.width;
*aHeight = info.height;
*aAutoSize = info.autoSize;
nsViewportInfo info = nsContentUtils::GetViewportInfo(doc, aDisplayWidth, aDisplayHeight);
*aDefaultZoom = info.GetDefaultZoom();
*aAllowZoom = info.IsZoomAllowed();
*aMinZoom = info.GetMinZoom();
*aMaxZoom = info.GetMaxZoom();
*aWidth = info.GetWidth();
*aHeight = info.GetHeight();
*aAutoSize = info.IsAutoSizeEnabled();
return NS_OK;
}

View File

@ -75,6 +75,7 @@
#include "PuppetWidget.h"
#include "StructuredCloneUtils.h"
#include "xpcpublic.h"
#include "nsViewportInfo.h"
#define BROWSER_ELEMENT_CHILD_SCRIPT \
NS_LITERAL_STRING("chrome://global/content/BrowserElementChild.js")
@ -374,16 +375,16 @@ TabChild::HandlePossibleViewportChange()
nsCOMPtr<nsIDOMWindowUtils> utils(GetDOMWindowUtils());
ViewportInfo viewportInfo =
nsViewportInfo viewportInfo =
nsContentUtils::GetViewportInfo(document, mInnerSize.width, mInnerSize.height);
SendUpdateZoomConstraints(viewportInfo.allowZoom,
viewportInfo.minZoom,
viewportInfo.maxZoom);
SendUpdateZoomConstraints(viewportInfo.IsZoomAllowed(),
viewportInfo.GetMinZoom(),
viewportInfo.GetMaxZoom());
float screenW = mInnerSize.width;
float screenH = mInnerSize.height;
float viewportW = viewportInfo.width;
float viewportH = viewportInfo.height;
float viewportW = viewportInfo.GetWidth();
float viewportH = viewportInfo.GetHeight();
// We're not being displayed in any way; don't bother doing anything because
// that will just confuse future adjustments.
@ -397,8 +398,8 @@ TabChild::HandlePossibleViewportChange()
// we have to call SetCSSViewport twice - once to set the width, and the
// second time to figure out the height based on the layout at that width.
float oldBrowserWidth = mOldViewportWidth;
mLastMetrics.mViewport.width = viewportInfo.width;
mLastMetrics.mViewport.height = viewportInfo.height;
mLastMetrics.mViewport.width = viewportW;
mLastMetrics.mViewport.height = viewportH;
if (!oldBrowserWidth) {
oldBrowserWidth = kDefaultViewportSize.width;
}
@ -445,7 +446,8 @@ TabChild::HandlePossibleViewportChange()
NS_ENSURE_TRUE_VOID(pageWidth); // (return early rather than divide by 0)
minScale = mInnerSize.width / pageWidth;
minScale = clamped((double)minScale, viewportInfo.minZoom, viewportInfo.maxZoom);
minScale = clamped((double)minScale, viewportInfo.GetMinZoom(),
viewportInfo.GetMaxZoom());
NS_ENSURE_TRUE_VOID(minScale); // (return early rather than divide by 0)
viewportH = NS_MAX(viewportH, screenH / minScale);
@ -484,15 +486,17 @@ TabChild::HandlePossibleViewportChange()
// FIXME/bug 799585(?): GetViewportInfo() returns a defaultZoom of
// 0.0 to mean "did not calculate a zoom". In that case, we default
// it to the intrinsic scale.
if (viewportInfo.defaultZoom < 0.01f) {
viewportInfo.defaultZoom = intrinsicScale.width;
if (viewportInfo.GetDefaultZoom() < 0.01f) {
viewportInfo.SetDefaultZoom(intrinsicScale.width);
}
MOZ_ASSERT(viewportInfo.minZoom <= viewportInfo.defaultZoom &&
viewportInfo.defaultZoom <= viewportInfo.maxZoom);
double defaultZoom = viewportInfo.GetDefaultZoom();
MOZ_ASSERT(viewportInfo.GetMinZoom() <= defaultZoom &&
defaultZoom <= viewportInfo.GetMaxZoom());
// GetViewportInfo() returns a resolution-dependent scale factor.
// Convert that to a resolution-indepedent zoom.
metrics.mZoom = gfxSize(viewportInfo.defaultZoom / intrinsicScale.width,
viewportInfo.defaultZoom / intrinsicScale.height);
metrics.mZoom = gfxSize(defaultZoom / intrinsicScale.width,
defaultZoom / intrinsicScale.height);
}
metrics.mDisplayPort = AsyncPanZoomController::CalculatePendingDisplayPort(

View File

@ -89,6 +89,7 @@
#include "sampler.h"
#include "nsAnimationManager.h"
#include "nsTransitionManager.h"
#include "nsViewportInfo.h"
using namespace mozilla;
using namespace mozilla::css;
@ -5213,11 +5214,11 @@ nsLayoutUtils::FontSizeInflationEnabled(nsPresContext *aPresContext)
int32_t screenLeft, screenTop, screenWidth, screenHeight;
screen->GetRect(&screenLeft, &screenTop, &screenWidth, &screenHeight);
ViewportInfo vInf =
nsViewportInfo vInf =
nsContentUtils::GetViewportInfo(aPresContext->PresShell()->GetDocument(),
screenWidth, screenHeight);
if (vInf.defaultZoom >= 1.0 || vInf.autoSize) {
if (vInf.GetDefaultZoom() >= 1.0 || vInf.IsAutoSizeEnabled()) {
return false;
}
}