Bug 910322 - Strongly type the viewport size in nsViewportInfo. r=mbrubeck, Ms2ger

This commit is contained in:
Kartikaya Gupta 2013-09-03 15:12:24 -04:00
parent 3368290646
commit b77f1bc2b4
11 changed files with 71 additions and 74 deletions

View File

@ -25,6 +25,7 @@
#include "mozilla/TimeStamp.h"
#include "nsContentListDeclarations.h"
#include "nsMathUtils.h"
#include "Units.h"
class imgICache;
class imgIContainer;
@ -1530,8 +1531,7 @@ public:
* will return viewport information that specifies default information.
*/
static nsViewportInfo GetViewportInfo(nsIDocument* aDocument,
uint32_t aDisplayWidth,
uint32_t aDisplayHeight);
const mozilla::ScreenIntSize& aDisplaySize);
// Call EnterMicroTask when you're entering JS execution.
// Usually the best way to do this is to use nsAutoMicroTask.

View File

@ -26,6 +26,7 @@
#include "nsPropertyTable.h" // for member
#include "nsTHashtable.h" // for member
#include "mozilla/dom/DocumentBinding.h"
#include "Units.h"
class imgIRequest;
class nsAString;
@ -621,8 +622,7 @@ public:
*/
Element* GetRootElement() const;
virtual nsViewportInfo GetViewportInfo(uint32_t aDisplayWidth,
uint32_t aDisplayHeight) = 0;
virtual nsViewportInfo GetViewportInfo(const mozilla::ScreenIntSize& aDisplaySize) = 0;
/**
* True iff this doc will ignore manual character encoding overrides.

View File

@ -14,10 +14,8 @@
*/
static const mozilla::LayoutDeviceToScreenScale kViewportMinScale(0.0f);
static const mozilla::LayoutDeviceToScreenScale kViewportMaxScale(10.0f);
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 mozilla::CSSIntSize kViewportMinSize(200, 223);
static const mozilla::CSSIntSize kViewportMaxSize(10000, 10000);
static const int32_t kViewportDefaultScreenWidth = 980;
/**
@ -27,13 +25,12 @@ static const int32_t kViewportDefaultScreenWidth = 980;
class MOZ_STACK_CLASS nsViewportInfo
{
public:
nsViewportInfo(uint32_t aDisplayWidth, uint32_t aDisplayHeight) :
nsViewportInfo(const mozilla::ScreenIntSize& aDisplaySize) :
mDefaultZoom(1.0),
mWidth(aDisplayWidth),
mHeight(aDisplayHeight),
mAutoSize(true),
mAllowZoom(true)
{
mSize = mozilla::gfx::RoundedToInt(mozilla::ScreenSize(aDisplaySize) / mDefaultZoom);
mozilla::CSSToLayoutDeviceScale pixelRatio(1.0f);
mMinZoom = pixelRatio * kViewportMinScale;
mMaxZoom = pixelRatio * kViewportMaxScale;
@ -43,15 +40,13 @@ class MOZ_STACK_CLASS nsViewportInfo
nsViewportInfo(const mozilla::CSSToScreenScale& aDefaultZoom,
const mozilla::CSSToScreenScale& aMinZoom,
const mozilla::CSSToScreenScale& aMaxZoom,
uint32_t aWidth,
uint32_t aHeight,
const mozilla::CSSIntSize& aSize,
bool aAutoSize,
bool aAllowZoom) :
mDefaultZoom(aDefaultZoom),
mMinZoom(aMinZoom),
mMaxZoom(aMaxZoom),
mWidth(aWidth),
mHeight(aHeight),
mSize(aSize),
mAutoSize(aAutoSize),
mAllowZoom(aAllowZoom)
{
@ -63,8 +58,7 @@ class MOZ_STACK_CLASS nsViewportInfo
mozilla::CSSToScreenScale GetMinZoom() { return mMinZoom; }
mozilla::CSSToScreenScale GetMaxZoom() { return mMaxZoom; }
uint32_t GetWidth() { return mWidth; }
uint32_t GetHeight() { return mHeight; }
mozilla::CSSIntSize GetSize() { return mSize; }
bool IsAutoSizeEnabled() { return mAutoSize; }
bool IsZoomAllowed() { return mAllowZoom; }
@ -88,13 +82,8 @@ class MOZ_STACK_CLASS nsViewportInfo
// The maximum zoom level permitted by the page.
mozilla::CSSToScreenScale 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;
// The size of the viewport, specified by the <meta name="viewport"> tag.
mozilla::CSSIntSize mSize;
// 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

View File

@ -4899,10 +4899,9 @@ static void ProcessViewportToken(nsIDocument *aDocument,
/* static */
nsViewportInfo
nsContentUtils::GetViewportInfo(nsIDocument *aDocument,
uint32_t aDisplayWidth,
uint32_t aDisplayHeight)
const ScreenIntSize& aDisplaySize)
{
return aDocument->GetViewportInfo(aDisplayWidth, aDisplayHeight);
return aDocument->GetViewportInfo(aDisplaySize);
}
/* static */

View File

@ -6805,12 +6805,11 @@ nsIDocument::AdoptNode(nsINode& aAdoptedNode, ErrorResult& rv)
}
nsViewportInfo
nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
uint32_t aDisplayHeight)
nsDocument::GetViewportInfo(const ScreenIntSize& aDisplaySize)
{
switch (mViewportType) {
case DisplayWidthHeight:
return nsViewportInfo(aDisplayWidth, aDisplayHeight);
return nsViewportInfo(aDisplaySize);
case Unknown:
{
nsAutoString viewport;
@ -6830,8 +6829,7 @@ nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
{
// We're making an assumption that the docType can't change here
mViewportType = DisplayWidthHeight;
nsViewportInfo ret(aDisplayWidth, aDisplayHeight);
return ret;
return nsViewportInfo(aDisplaySize);
}
}
}
@ -6840,8 +6838,7 @@ nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
GetHeaderData(nsGkAtoms::handheldFriendly, handheldFriendly);
if (handheldFriendly.EqualsLiteral("true")) {
mViewportType = DisplayWidthHeight;
nsViewportInfo ret(aDisplayWidth, aDisplayHeight);
return ret;
return nsViewportInfo(aDisplaySize);
}
}
@ -6898,14 +6895,13 @@ nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
}
nsresult widthErrorCode, heightErrorCode;
mViewportWidth = widthStr.ToInteger(&widthErrorCode);
mViewportHeight = heightStr.ToInteger(&heightErrorCode);
mViewportSize.width = widthStr.ToInteger(&widthErrorCode);
mViewportSize.height = heightStr.ToInteger(&heightErrorCode);
// If width or height has not been set to a valid number by this point,
// fall back to a default value.
mValidWidth = (!widthStr.IsEmpty() && NS_SUCCEEDED(widthErrorCode) && mViewportWidth > 0);
mValidHeight = (!heightStr.IsEmpty() && NS_SUCCEEDED(heightErrorCode) && mViewportHeight > 0);
mValidWidth = (!widthStr.IsEmpty() && NS_SUCCEEDED(widthErrorCode) && mViewportSize.width > 0);
mValidHeight = (!heightStr.IsEmpty() && NS_SUCCEEDED(heightErrorCode) && mViewportSize.height > 0);
mAllowZoom = true;
nsAutoString userScalable;
@ -6926,22 +6922,22 @@ nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
}
case Specified:
default:
uint32_t width = mViewportWidth, height = mViewportHeight;
CSSIntSize size = mViewportSize;
if (!mValidWidth) {
if (mValidHeight && aDisplayWidth > 0 && aDisplayHeight > 0) {
width = uint32_t((height * aDisplayWidth) / aDisplayHeight);
if (mValidHeight && !aDisplaySize.IsEmpty()) {
size.width = int32_t(size.height * aDisplaySize.width / aDisplaySize.height);
} else {
width = Preferences::GetInt("browser.viewport.desktopWidth",
kViewportDefaultScreenWidth);
size.width = Preferences::GetInt("browser.viewport.desktopWidth",
kViewportDefaultScreenWidth);
}
}
if (!mValidHeight) {
if (aDisplayWidth > 0 && aDisplayHeight > 0) {
height = uint32_t((width * aDisplayHeight) / aDisplayWidth);
if (!aDisplaySize.IsEmpty()) {
size.height = int32_t(size.width * aDisplaySize.height / aDisplaySize.width);
} else {
height = width;
size.height = size.width;
}
}
// Now convert the scale into device pixels per CSS pixel.
@ -6952,38 +6948,36 @@ nsDocument::GetViewportInfo(uint32_t aDisplayWidth,
CSSToScreenScale scaleMaxFloat = mScaleMaxFloat * pixelRatio;
if (mAutoSize) {
// aDisplayWidth and aDisplayHeight are in device pixels; convert them to
// CSS pixels for the viewport size.
width = aDisplayWidth / pixelRatio.scale;
height = aDisplayHeight / pixelRatio.scale;
// aDisplaySize is in screen pixels; convert them to CSS pixels for the viewport size.
CSSToScreenScale defaultPixelScale = pixelRatio * LayoutDeviceToScreenScale(1.0f);
size = mozilla::gfx::RoundedToInt(ScreenSize(aDisplaySize) / defaultPixelScale);
}
width = std::min(width, kViewportMaxWidth);
width = std::max(width, kViewportMinWidth);
size.width = clamped(size.width, kViewportMinSize.width, kViewportMaxSize.width);
// Also recalculate the default zoom, if it wasn't specified in the metadata,
// and the width is specified.
if (mScaleStrEmpty && !mWidthStrEmpty) {
CSSToScreenScale defaultScale(float(aDisplayWidth) / float(width));
CSSToScreenScale defaultScale(float(aDisplaySize.width) / float(size.width));
scaleFloat = (scaleFloat > defaultScale) ? scaleFloat : defaultScale;
}
height = std::min(height, kViewportMaxHeight);
height = std::max(height, kViewportMinHeight);
size.height = clamped(size.height, kViewportMinSize.height, kViewportMaxSize.height);
// We need to perform a conversion, but only if the initial or maximum
// scale were set explicitly by the user.
if (mValidScaleFloat) {
width = std::max(width, (uint32_t)(aDisplayWidth / scaleFloat.scale));
height = std::max(height, (uint32_t)(aDisplayHeight / scaleFloat.scale));
CSSIntSize displaySize = RoundedToInt(ScreenSize(aDisplaySize) / scaleFloat);
size.width = std::max(size.width, displaySize.width);
size.height = std::max(size.height, displaySize.height);
} else if (mValidMaxScale) {
width = std::max(width, (uint32_t)(aDisplayWidth / scaleMaxFloat.scale));
height = std::max(height, (uint32_t)(aDisplayHeight / scaleMaxFloat.scale));
CSSIntSize displaySize = RoundedToInt(ScreenSize(aDisplaySize) / scaleMaxFloat);
size.width = std::max(size.width, displaySize.width);
size.height = std::max(size.height, displaySize.height);
}
nsViewportInfo ret(scaleFloat, scaleMinFloat, scaleMaxFloat, width, height,
mAutoSize, mAllowZoom);
return ret;
return nsViewportInfo(scaleFloat, scaleMinFloat, scaleMaxFloat, size,
mAutoSize, mAllowZoom);
}
}

View File

@ -751,9 +751,7 @@ public:
nsRadioGroupStruct* GetRadioGroup(const nsAString& aName) const;
nsRadioGroupStruct* GetOrCreateRadioGroup(const nsAString& aName);
virtual nsViewportInfo GetViewportInfo(uint32_t aDisplayWidth,
uint32_t aDisplayHeight) MOZ_OVERRIDE;
virtual nsViewportInfo GetViewportInfo(const mozilla::ScreenIntSize& aDisplaySize) MOZ_OVERRIDE;
private:
nsRadioGroupStruct* GetRadioGroupInternal(const nsAString& aName) const;
@ -1415,7 +1413,7 @@ private:
mozilla::LayoutDeviceToScreenScale mScaleFloat;
mozilla::CSSToLayoutDeviceScale mPixelRatio;
bool mAutoSize, mAllowZoom, mValidScaleFloat, mValidMaxScale, mScaleStrEmpty, mWidthStrEmpty;
uint32_t mViewportWidth, mViewportHeight;
mozilla::CSSIntSize mViewportSize;
nsrefcnt mStackRefCnt;
bool mNeedsReleaseAfterStackRefCntRelease;

View File

@ -281,13 +281,13 @@ nsDOMWindowUtils::GetViewportInfo(uint32_t aDisplayWidth,
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
NS_ENSURE_STATE(doc);
nsViewportInfo info = nsContentUtils::GetViewportInfo(doc, aDisplayWidth, aDisplayHeight);
nsViewportInfo info = nsContentUtils::GetViewportInfo(doc, ScreenIntSize(aDisplayWidth, aDisplayHeight));
*aDefaultZoom = info.GetDefaultZoom().scale;
*aAllowZoom = info.IsZoomAllowed();
*aMinZoom = info.GetMinZoom().scale;
*aMaxZoom = info.GetMaxZoom().scale;
*aWidth = info.GetWidth();
*aHeight = info.GetHeight();
*aWidth = info.GetSize().width;
*aHeight = info.GetSize().height;
*aAutoSize = info.IsAutoSizeEnabled();
return NS_OK;
}

View File

@ -554,15 +554,14 @@ TabChild::HandlePossibleViewportChange()
nsCOMPtr<nsIDOMWindowUtils> utils(GetDOMWindowUtils());
nsViewportInfo viewportInfo =
nsContentUtils::GetViewportInfo(document, mInnerSize.width, mInnerSize.height);
nsViewportInfo viewportInfo = nsContentUtils::GetViewportInfo(document, mInnerSize);
SendUpdateZoomConstraints(viewportInfo.IsZoomAllowed(),
viewportInfo.GetMinZoom(),
viewportInfo.GetMaxZoom());
float screenW = mInnerSize.width;
float screenH = mInnerSize.height;
CSSSize viewport(viewportInfo.GetWidth(), viewportInfo.GetHeight());
CSSSize viewport(viewportInfo.GetSize());
// We're not being displayed in any way; don't bother doing anything because
// that will just confuse future adjustments.

View File

@ -115,6 +115,12 @@ struct SizeTyped :
};
typedef SizeTyped<UnknownUnits> Size;
template<class units>
IntSizeTyped<units> RoundedToInt(const SizeTyped<units>& aSize) {
return IntSizeTyped<units>(int32_t(floorf(aSize.width + 0.5f)),
int32_t(floorf(aSize.height + 0.5f)));
}
}
}

View File

@ -231,6 +231,18 @@ gfx::RectTyped<dst> operator/(const gfx::IntRectTyped<src>& aRect, const gfx::Sc
float(aRect.height) / aScale.scale);
}
template<class src, class dst>
gfx::SizeTyped<dst> operator*(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<src, dst>& aScale) {
return gfx::SizeTyped<dst>(aSize.width * aScale.scale,
aSize.height * aScale.scale);
}
template<class src, class dst>
gfx::SizeTyped<dst> operator/(const gfx::SizeTyped<src>& aSize, const gfx::ScaleFactor<dst, src>& aScale) {
return gfx::SizeTyped<dst>(aSize.width / aScale.scale,
aSize.height / aScale.scale);
}
};
#endif

View File

@ -9612,7 +9612,7 @@ nsIPresShell::RecomputeFontSizeInflationEnabled()
screen->GetRect(&screenLeft, &screenTop, &screenWidth, &screenHeight);
nsViewportInfo vInf =
nsContentUtils::GetViewportInfo(GetDocument(), screenWidth, screenHeight);
nsContentUtils::GetViewportInfo(GetDocument(), ScreenIntSize(screenWidth, screenHeight));
if (vInf.GetDefaultZoom() >= CSSToScreenScale(1.0f) || vInf.IsAutoSizeEnabled()) {
mFontSizeInflationEnabled = false;