1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* -*- 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 "ZoomConstraintsClient.h"
#include <inttypes.h>
#include "mozilla/layers/APZCCallbackHelper.h"
#include "mozilla/layers/ScrollableLayerGuid.h"
#include "mozilla/layers/ZoomConstraints.h"
#include "mozilla/Preferences.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_apz.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Event.h"
#include "nsIFrame.h"
#include "nsIScrollableFrame.h"
#include "nsLayoutUtils.h"
#include "nsPoint.h"
#include "nsView.h"
#include "nsViewportInfo.h"
#include "Units.h"
#include "UnitTransforms.h"
static mozilla::LazyLogModule sApzZoomLog("apz.zoom");
#define ZCC_LOG(...) MOZ_LOG(sApzZoomLog, LogLevel::Debug, (__VA_ARGS__))
NS_IMPL_ISUPPORTS(ZoomConstraintsClient, nsIDOMEventListener, nsIObserver)
#define DOM_META_ADDED u"DOMMetaAdded"_ns
#define DOM_META_CHANGED u"DOMMetaChanged"_ns
#define FULLSCREEN_CHANGED u"fullscreenchange"_ns
#define BEFORE_FIRST_PAINT "before-first-paint"_ns
#define COMPOSITOR_REINITIALIZED "compositor-reinitialized"_ns
#define NS_PREF_CHANGED "nsPref:changed"_ns
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::layers;
ZoomConstraintsClient::ZoomConstraintsClient()
: mDocument(nullptr),
mPresShell(nullptr),
mZoomConstraints(false, false, CSSToParentLayerScale(1.f),
CSSToParentLayerScale(1.f)) {}
ZoomConstraintsClient::~ZoomConstraintsClient() = default;
static nsIWidget* GetWidget(PresShell* aPresShell) {
if (!aPresShell) {
return nullptr;
}
if (nsIFrame* rootFrame = aPresShell->GetRootFrame()) {
if (nsView* view = rootFrame->GetView()) {
return view->GetWidget();
}
}
return nullptr;
}
void ZoomConstraintsClient::Destroy() {
if (!(mPresShell && mDocument)) {
return;
}
ZCC_LOG("Destroying %p\n", this);
if (mEventTarget) {
mEventTarget->RemoveEventListener(DOM_META_ADDED, this, false);
mEventTarget->RemoveEventListener(DOM_META_CHANGED, this, false);
mEventTarget->RemoveSystemEventListener(FULLSCREEN_CHANGED, this, false);
mEventTarget = nullptr;
}
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
if (observerService) {
observerService->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
observerService->RemoveObserver(this, COMPOSITOR_REINITIALIZED.Data());
}
Preferences::RemoveObserver(this, "browser.ui.zoom.force-user-scalable");
if (mGuid) {
if (nsIWidget* widget = GetWidget(mPresShell)) {
ZCC_LOG("Sending null constraints in %p for { %u, %" PRIu64 " }\n", this,
mGuid->mPresShellId, mGuid->mScrollId);
widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId,
Nothing());
mGuid = Nothing();
}
}
mDocument = nullptr;
mPresShell = nullptr;
}
void ZoomConstraintsClient::Init(PresShell* aPresShell, Document* aDocument) {
if (!(aPresShell && aDocument)) {
return;
}
mPresShell = aPresShell;
mDocument = aDocument;
if (nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow()) {
mEventTarget = window->GetParentTarget();
}
if (mEventTarget) {
mEventTarget->AddEventListener(DOM_META_ADDED, this, false);
mEventTarget->AddEventListener(DOM_META_CHANGED, this, false);
mEventTarget->AddSystemEventListener(FULLSCREEN_CHANGED, this, false);
}
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
if (observerService) {
observerService->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
observerService->AddObserver(this, COMPOSITOR_REINITIALIZED.Data(), false);
}
Preferences::AddStrongObserver(this, "browser.ui.zoom.force-user-scalable");
}
NS_IMETHODIMP
ZoomConstraintsClient::HandleEvent(dom::Event* event) {
nsAutoString type;
event->GetType(type);
if (type.Equals(DOM_META_ADDED)) {
ZCC_LOG("Got a dom-meta-added event in %p\n", this);
RefreshZoomConstraints();
} else if (type.Equals(DOM_META_CHANGED)) {
ZCC_LOG("Got a dom-meta-changed event in %p\n", this);
RefreshZoomConstraints();
} else if (type.Equals(FULLSCREEN_CHANGED)) {
ZCC_LOG("Got a fullscreen-change event in %p\n", this);
RefreshZoomConstraints();
}
return NS_OK;
}
NS_IMETHODIMP
ZoomConstraintsClient::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
if (SameCOMIdentity(aSubject, ToSupports(mDocument)) &&
BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
ZCC_LOG("Got a before-first-paint event in %p\n", this);
RefreshZoomConstraints();
} else if (COMPOSITOR_REINITIALIZED.EqualsASCII(aTopic)) {
ZCC_LOG("Got a compositor-reinitialized notification in %p\n", this);
RefreshZoomConstraints();
} else if (NS_PREF_CHANGED.EqualsASCII(aTopic)) {
ZCC_LOG("Got a pref-change event in %p\n", this);
// We need to run this later because all the pref change listeners need
// to execute before we can be guaranteed that
// StaticPrefs::browser_ui_zoom_force_user_scalable() returns the updated
// value.
RefPtr<nsRunnableMethod<ZoomConstraintsClient>> event =
NewRunnableMethod("ZoomConstraintsClient::RefreshZoomConstraints", this,
&ZoomConstraintsClient::RefreshZoomConstraints);
mDocument->Dispatch(event.forget());
}
return NS_OK;
}
void ZoomConstraintsClient::ScreenSizeChanged() {
ZCC_LOG("Got a screen-size change notification in %p\n", this);
RefreshZoomConstraints();
}
static mozilla::layers::ZoomConstraints ComputeZoomConstraintsFromViewportInfo(
const nsViewportInfo& aViewportInfo, Document* aDocument) {
mozilla::layers::ZoomConstraints constraints;
constraints.mAllowZoom = aViewportInfo.IsZoomAllowed() &&
nsLayoutUtils::AllowZoomingForDocument(aDocument);
constraints.mAllowDoubleTapZoom =
constraints.mAllowZoom && StaticPrefs::apz_allow_double_tap_zooming();
if (constraints.mAllowZoom) {
constraints.mMinZoom.scale = aViewportInfo.GetMinZoom().scale;
constraints.mMaxZoom.scale = aViewportInfo.GetMaxZoom().scale;
} else {
constraints.mMinZoom.scale = aViewportInfo.GetDefaultZoom().scale;
constraints.mMaxZoom.scale = aViewportInfo.GetDefaultZoom().scale;
}
return constraints;
}
void ZoomConstraintsClient::RefreshZoomConstraints() {
mZoomConstraints = ZoomConstraints(false, false, CSSToParentLayerScale(1.f),
CSSToParentLayerScale(1.f));
nsIWidget* widget = GetWidget(mPresShell);
if (!widget) {
return;
}
uint32_t presShellId = 0;
ScrollableLayerGuid::ViewID viewId = ScrollableLayerGuid::NULL_SCROLL_ID;
bool scrollIdentifiersValid =
APZCCallbackHelper::GetOrCreateScrollIdentifiers(
mDocument->GetDocumentElement(), &presShellId, &viewId);
if (!scrollIdentifiersValid) {
return;
}
LayoutDeviceIntSize screenSize;
if (!nsLayoutUtils::GetDocumentViewerSize(mPresShell->GetPresContext(),
screenSize)) {
return;
}
nsViewportInfo viewportInfo = mDocument->GetViewportInfo(ViewAs<ScreenPixel>(
screenSize, PixelCastJustification::LayoutDeviceIsScreenForBounds));
mZoomConstraints =
ComputeZoomConstraintsFromViewportInfo(viewportInfo, mDocument);
if (mDocument->Fullscreen()) {
ZCC_LOG("%p is in fullscreen, disallowing zooming\n", this);
mZoomConstraints.mAllowZoom = false;
mZoomConstraints.mAllowDoubleTapZoom = false;
}
if (mDocument->IsStaticDocument()) {
ZCC_LOG("%p is in print or print preview, disallowing double tap zooming\n",
this);
mZoomConstraints.mAllowDoubleTapZoom = false;
}
if (nsContentUtils::IsPDFJS(mDocument->GetPrincipal())) {
ZCC_LOG("%p is pdf.js viewer, disallowing double tap zooming\n", this);
mZoomConstraints.mAllowDoubleTapZoom = false;
}
// On macOS the OS can send us a double tap zoom event from the touchpad and
// there are no touch screen macOS devices so we never wait to see if a second
// tap is coming so we can always allow double tap zooming on mac. We need
// this because otherwise the width check usually disables it.
bool allow_double_tap_always = false;
#ifdef XP_MACOSX
allow_double_tap_always =
StaticPrefs::apz_mac_enable_double_tap_zoom_touchpad_gesture();
#endif
if (!allow_double_tap_always && mZoomConstraints.mAllowDoubleTapZoom) {
// If the CSS viewport is narrower than the screen (i.e. width <=
// device-width) then we disable double-tap-to-zoom behaviour.
CSSToLayoutDeviceScale scale =
mPresShell->GetPresContext()->CSSToDevPixelScale();
if ((viewportInfo.GetSize() * scale).width <= screenSize.width) {
mZoomConstraints.mAllowDoubleTapZoom = false;
}
}
// We only ever create a ZoomConstraintsClient for an RCD, so the RSF of
// the presShell must be the RCD-RSF (if it exists).
MOZ_ASSERT(mPresShell->GetPresContext()->IsRootContentDocumentCrossProcess());
if (nsIScrollableFrame* rcdrsf =
mPresShell->GetRootScrollFrameAsScrollable()) {
ZCC_LOG("Notifying RCD-RSF that it is zoomable: %d\n",
mZoomConstraints.mAllowZoom);
rcdrsf->SetZoomableByAPZ(mZoomConstraints.mAllowZoom);
}
ScrollableLayerGuid newGuid(LayersId{0}, presShellId, viewId);
if (mGuid && mGuid.value() != newGuid) {
ZCC_LOG("Clearing old constraints in %p for { %u, %" PRIu64 " }\n", this,
mGuid->mPresShellId, mGuid->mScrollId);
// If the guid changes, send a message to clear the old one
widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId,
Nothing());
}
mGuid = Some(newGuid);
ZCC_LOG("Sending constraints %s in %p for { %u, %" PRIu64 " }\n",
ToString(mZoomConstraints).c_str(), this, presShellId, viewId);
widget->UpdateZoomConstraints(presShellId, viewId, Some(mZoomConstraints));
}
|