ported from UXP: Issue #2714 - Implement the Visual Viewport API (a036cfeb)

This commit is contained in:
2025-08-04 21:50:39 +08:00
parent 0f81abbe64
commit e822fb126f
12 changed files with 247 additions and 1 deletions
+144
View File
@@ -0,0 +1,144 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "VisualViewport.h"
#include "nsIScrollableFrame.h"
#include "nsIDocShell.h"
using namespace mozilla;
using namespace mozilla::dom;
VisualViewport::VisualViewport(nsPIDOMWindowInner* aWindow)
: DOMEventTargetHelper(aWindow)
{
}
VisualViewport::VisualViewport(nsIGlobalObject* aGlobal)
: DOMEventTargetHelper(aGlobal)
{
}
VisualViewport::~VisualViewport()
{
}
/* virtual */
JSObject*
VisualViewport::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return VisualViewportBinding::Wrap(aCx, this, aGivenProto);
}
/* XXXMC: We are effectively getting the size from the root scrollframe of the content
* and the scale from our DPP resolution.
* Since we are only on desktop, by definition have full view size (no "notch" or
* other mobile "don't touch" areas), we are hard-coding:
* - VisualViewportOffset = (0,0)
* - PageLeft/PageTop = 0.0
* - OffsetLeft/OffsetTop = 0.0
*/
CSSSize
VisualViewport::VisualViewportSize() const
{
CSSSize size = CSSSize(0,0);
nsIPresShell* presShell = GetPresShell();
if (presShell) {
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
if (sf) {
size = CSSRect::FromAppUnits(sf->GetScrollPortRect().Size());
}
}
return size;
}
double
VisualViewport::Width() const
{
CSSSize size = VisualViewportSize();
return size.width;
}
double
VisualViewport::Height() const
{
CSSSize size = VisualViewportSize();
return size.height;
}
double
VisualViewport::Scale() const
{
double scale = 1;
nsIPresShell* presShell = GetPresShell();
if (presShell) {
scale = presShell->GetResolution();
}
return scale;
}
CSSPoint
VisualViewport::VisualViewportOffset() const
{
CSSPoint offset = CSSPoint(0,0);
return offset;
}
CSSPoint
VisualViewport::LayoutViewportOffset() const
{
CSSPoint offset = CSSPoint(0,0);
nsIPresShell* presShell = GetPresShell();
if (presShell) {
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
if (sf) {
offset = CSSPoint::FromAppUnits(sf->GetScrollPosition());
}
}
return offset;
}
double
VisualViewport::PageLeft() const
{
return 0.0;
}
double
VisualViewport::PageTop() const
{
return 0.0;
}
double
VisualViewport::OffsetLeft() const
{
return 0.0;
}
double
VisualViewport::OffsetTop() const
{
return 0.0;
}
nsIPresShell*
VisualViewport::GetPresShell() const
{
nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
if (!window) {
return nullptr;
}
nsIDocShell* docShell = window->GetDocShell();
if (!docShell) {
return nullptr;
}
return docShell->GetPresShell();
}
+49
View File
@@ -0,0 +1,49 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 mozilla_dom_VisualViewport_h
#define mozilla_dom_VisualViewport_h
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/VisualViewportBinding.h"
#include "Units.h"
#include "nsIPresShell.h"
namespace mozilla {
namespace dom {
/* Visual Viewport API spec: https://wicg.github.io/visual-viewport/#the-visualviewport-interface */
class VisualViewport final: public mozilla::DOMEventTargetHelper
{
public:
explicit VisualViewport(nsPIDOMWindowInner* aWindow);
explicit VisualViewport(nsIGlobalObject* aGlobal);
double OffsetLeft() const;
double OffsetTop() const;
double PageLeft() const;
double PageTop() const;
double Width() const;
double Height() const;
double Scale() const;
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
private:
virtual ~VisualViewport();
CSSSize VisualViewportSize() const;
CSSPoint VisualViewportOffset() const;
CSSPoint LayoutViewportOffset() const;
nsIPresShell* GetPresShell() const;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_VisualViewport_h
+2
View File
@@ -210,6 +210,7 @@ EXPORTS.mozilla.dom += [
'TimeoutHandler.h',
'TimeoutManager.h',
'TreeWalker.h',
'VisualViewport.h',
'WebKitCSSMatrix.h',
'WebSocket.h',
'WindowOrientationObserver.h',
@@ -352,6 +353,7 @@ UNIFIED_SOURCES += [
'TimeoutManager.cpp',
'TimerClamping.cpp',
'TreeWalker.cpp',
'VisualViewport.cpp',
'WebKitCSSMatrix.cpp',
'WebSocket.cpp',
'WindowNamedPropertiesHandler.cpp',
+1 -1
View File
@@ -906,7 +906,7 @@ nsContentSink::PrefetchOrPreloadHref(const nsAString &aHref,
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aSource);
if (aLinkTypes & nsStyleLinkElement::ePRELOAD) {
nsContentPolicyType policyType;
bool isPreloadValid = Link::CheckPreloadAttrs(policyType,
bool isPreloadValid = dom::Link::CheckPreloadAttrs(policyType,
aDestination,
aType,
aMedia,
+11
View File
@@ -25,6 +25,7 @@
#include "mozilla/dom/Timeout.h"
#include "mozilla/dom/TimeoutHandler.h"
#include "mozilla/dom/TimeoutManager.h"
#include "mozilla/dom/VisualViewport.h"
#include "mozilla/IntegerPrintfMacros.h"
#if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK)
#include "mozilla/dom/WindowOrientationObserver.h"
@@ -4037,6 +4038,16 @@ nsGlobalWindow::GetNavigator()
return navigator;
}
VisualViewport*
nsGlobalWindow::VisualViewport()
{
if (!mVisualViewport) {
mVisualViewport = new mozilla::dom::VisualViewport(this);
}
return mVisualViewport;
}
nsScreen*
nsGlobalWindow::GetScreen(ErrorResult& aError)
{
+4
View File
@@ -136,6 +136,7 @@ class SpeechSynthesis;
class TabGroup;
class Timeout;
class U2F;
class VisualViewport;
class VRDisplay;
class VREventObserver;
class WakeLock;
@@ -1009,6 +1010,7 @@ public:
already_AddRefed<nsICSSDeclaration>
GetComputedStyle(mozilla::dom::Element& aElt, const nsAString& aPseudoElt,
mozilla::ErrorResult& aError) override;
mozilla::dom::VisualViewport* VisualViewport();
already_AddRefed<mozilla::dom::MediaQueryList> MatchMediaOuter(const nsAString& aQuery);
already_AddRefed<mozilla::dom::MediaQueryList> MatchMedia(const nsAString& aQuery,
mozilla::ErrorResult& aError);
@@ -1936,6 +1938,8 @@ protected:
RefPtr<nsHistory> mHistory;
RefPtr<mozilla::dom::CustomElementRegistry> mCustomElements;
RefPtr<mozilla::dom::VisualViewport> mVisualViewport;
// These member variables are used on both inner and the outer windows.
nsCOMPtr<nsIPrincipal> mDocumentPrincipal;
+4
View File
@@ -1155,6 +1155,10 @@ DOMInterfaces = {
'wrapperCache': False,
},
'VisualViewport': {
'nativeType': 'mozilla::dom::VisualViewport',
},
'VTTCue': {
'nativeType': 'mozilla::dom::TextTrackCue'
},
+21
View File
@@ -0,0 +1,21 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/.
*
* The origin of this IDL file is:
* https://wicg.github.io/visual-viewport/#the-visualviewport-interface
*/
interface VisualViewport : EventTarget {
readonly attribute double offsetLeft;
readonly attribute double offsetTop;
readonly attribute double pageLeft;
readonly attribute double pageTop;
readonly attribute double width;
readonly attribute double height;
readonly attribute double scale;
};
+6
View File
@@ -17,6 +17,7 @@
* https://w3c.github.io/requestidlecallback/
* https://webaudio.github.io/web-audio-api/#widl-Window-audioWorklet
* https://drafts.css-houdini.org/css-paint-api-1/#dom-window-paintworklet
* https://wicg.github.io/visual-viewport/#the-visualviewport-interface
*/
interface ApplicationCache;
@@ -523,3 +524,8 @@ partial interface Window {
dictionary WindowPostMessageOptions : StructuredSerializeOptions {
USVString targetOrigin = "/";
};
partial interface Window {
[SameObject, Pref="dom.visualviewport.enabled", Replaceable]
readonly attribute VisualViewport visualViewport;
};
+1
View File
@@ -577,6 +577,7 @@ WEBIDL_FILES = [
'VideoStreamTrack.webidl',
'VideoTrack.webidl',
'VideoTrackList.webidl',
'VisualViewport.webidl',
'VRDisplay.webidl',
'VTTCue.webidl',
'VTTRegion.webidl',
+1
View File
@@ -320,6 +320,7 @@ private:
DECL_GFX_PREF(Live, "dom.ipc.plugins.asyncdrawing.enabled", PluginAsyncDrawingEnabled, bool, false);
DECL_GFX_PREF(Live, "dom.meta-viewport.enabled", MetaViewportEnabled, bool, false);
DECL_GFX_PREF(Live, "dom.visualviewport.enabled", VisualViewportEnabled, bool, false);
DECL_GFX_PREF(Once, "dom.vr.enabled", VREnabled, bool, false);
DECL_GFX_PREF(Once, "dom.vr.oculus.enabled", VROculusEnabled, bool, true);
DECL_GFX_PREF(Once, "dom.vr.openvr.enabled", VROpenVREnabled, bool, false);
+3
View File
@@ -5460,6 +5460,9 @@ pref("intl.allow-insecure-text-input", false);
// Enable meta-viewport support in remote APZ-enabled frames.
pref("dom.meta-viewport.enabled", false);
// Disable the Visual Viewport API
pref("dom.visualviewport.enabled", false);
// Disable <meta http-equiv=set-cookie> support. See m-c bug 1457503 / UXP #1102.
pref("dom.meta-set-cookie.enabled", false);