/src/mozilla-central/layout/base/ZoomConstraintsClient.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "ZoomConstraintsClient.h" |
8 | | |
9 | | #include <inttypes.h> |
10 | | #include "FrameMetrics.h" |
11 | | #include "gfxPrefs.h" |
12 | | #include "LayersLogging.h" |
13 | | #include "mozilla/layers/APZCCallbackHelper.h" |
14 | | #include "mozilla/Preferences.h" |
15 | | #include "mozilla/PresShell.h" |
16 | | #include "mozilla/dom/Event.h" |
17 | | #include "nsDocument.h" |
18 | | #include "nsIFrame.h" |
19 | | #include "nsLayoutUtils.h" |
20 | | #include "nsPoint.h" |
21 | | #include "nsView.h" |
22 | | #include "nsViewportInfo.h" |
23 | | #include "Units.h" |
24 | | #include "UnitTransforms.h" |
25 | | |
26 | | #define ZCC_LOG(...) |
27 | | // #define ZCC_LOG(...) printf_stderr("ZCC: " __VA_ARGS__) |
28 | | |
29 | | NS_IMPL_ISUPPORTS(ZoomConstraintsClient, nsIDOMEventListener, nsIObserver) |
30 | | |
31 | 0 | #define DOM_META_ADDED NS_LITERAL_STRING("DOMMetaAdded") |
32 | 0 | #define DOM_META_CHANGED NS_LITERAL_STRING("DOMMetaChanged") |
33 | 0 | #define FULLSCREEN_CHANGED NS_LITERAL_STRING("fullscreenchange") |
34 | 0 | #define BEFORE_FIRST_PAINT NS_LITERAL_CSTRING("before-first-paint") |
35 | 0 | #define NS_PREF_CHANGED NS_LITERAL_CSTRING("nsPref:changed") |
36 | | |
37 | | using namespace mozilla; |
38 | | using namespace mozilla::layers; |
39 | | |
40 | | ZoomConstraintsClient::ZoomConstraintsClient() : |
41 | | mDocument(nullptr), |
42 | | mPresShell(nullptr) |
43 | 0 | { |
44 | 0 | } |
45 | | |
46 | | ZoomConstraintsClient::~ZoomConstraintsClient() |
47 | 0 | { |
48 | 0 | } |
49 | | |
50 | | static nsIWidget* |
51 | | GetWidget(nsIPresShell* aShell) |
52 | 0 | { |
53 | 0 | if (!aShell) { |
54 | 0 | return nullptr; |
55 | 0 | } |
56 | 0 | if (nsIFrame* rootFrame = aShell->GetRootFrame()) { |
57 | | #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_UIKIT) |
58 | | return rootFrame->GetNearestWidget(); |
59 | | #else |
60 | 0 | if (nsView* view = rootFrame->GetView()) { |
61 | 0 | return view->GetWidget(); |
62 | 0 | } |
63 | 0 | #endif |
64 | 0 | } |
65 | 0 | return nullptr; |
66 | 0 | } |
67 | | |
68 | | void |
69 | | ZoomConstraintsClient::Destroy() |
70 | 0 | { |
71 | 0 | if (!(mPresShell && mDocument)) { |
72 | 0 | return; |
73 | 0 | } |
74 | 0 | |
75 | 0 | ZCC_LOG("Destroying %p\n", this); |
76 | 0 |
|
77 | 0 | if (mEventTarget) { |
78 | 0 | mEventTarget->RemoveEventListener(DOM_META_ADDED, this, false); |
79 | 0 | mEventTarget->RemoveEventListener(DOM_META_CHANGED, this, false); |
80 | 0 | mEventTarget->RemoveSystemEventListener(FULLSCREEN_CHANGED, this, false); |
81 | 0 | mEventTarget = nullptr; |
82 | 0 | } |
83 | 0 |
|
84 | 0 | nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService(); |
85 | 0 | if (observerService) { |
86 | 0 | observerService->RemoveObserver(this, BEFORE_FIRST_PAINT.Data()); |
87 | 0 | } |
88 | 0 |
|
89 | 0 | Preferences::RemoveObserver(this, "browser.ui.zoom.force-user-scalable"); |
90 | 0 |
|
91 | 0 | if (mGuid) { |
92 | 0 | if (nsIWidget* widget = GetWidget(mPresShell)) { |
93 | 0 | ZCC_LOG("Sending null constraints in %p for { %u, %" PRIu64 " }\n", |
94 | 0 | this, mGuid->mPresShellId, mGuid->mScrollId); |
95 | 0 | widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId, Nothing()); |
96 | 0 | mGuid = Nothing(); |
97 | 0 | } |
98 | 0 | } |
99 | 0 |
|
100 | 0 | mDocument = nullptr; |
101 | 0 | mPresShell = nullptr; |
102 | 0 | } |
103 | | |
104 | | void |
105 | | ZoomConstraintsClient::Init(nsIPresShell* aPresShell, nsIDocument* aDocument) |
106 | 0 | { |
107 | 0 | if (!(aPresShell && aDocument)) { |
108 | 0 | return; |
109 | 0 | } |
110 | 0 | |
111 | 0 | mPresShell = aPresShell; |
112 | 0 | mDocument = aDocument; |
113 | 0 |
|
114 | 0 | if (nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow()) { |
115 | 0 | mEventTarget = window->GetParentTarget(); |
116 | 0 | } |
117 | 0 | if (mEventTarget) { |
118 | 0 | mEventTarget->AddEventListener(DOM_META_ADDED, this, false); |
119 | 0 | mEventTarget->AddEventListener(DOM_META_CHANGED, this, false); |
120 | 0 | mEventTarget->AddSystemEventListener(FULLSCREEN_CHANGED, this, false); |
121 | 0 | } |
122 | 0 |
|
123 | 0 | nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService(); |
124 | 0 | if (observerService) { |
125 | 0 | observerService->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false); |
126 | 0 | } |
127 | 0 |
|
128 | 0 | Preferences::AddStrongObserver(this, "browser.ui.zoom.force-user-scalable"); |
129 | 0 | } |
130 | | |
131 | | NS_IMETHODIMP |
132 | | ZoomConstraintsClient::HandleEvent(dom::Event* event) |
133 | 0 | { |
134 | 0 | nsAutoString type; |
135 | 0 | event->GetType(type); |
136 | 0 |
|
137 | 0 | if (type.Equals(DOM_META_ADDED)) { |
138 | 0 | ZCC_LOG("Got a dom-meta-added event in %p\n", this); |
139 | 0 | RefreshZoomConstraints(); |
140 | 0 | } else if (type.Equals(DOM_META_CHANGED)) { |
141 | 0 | ZCC_LOG("Got a dom-meta-changed event in %p\n", this); |
142 | 0 | RefreshZoomConstraints(); |
143 | 0 | } else if (type.Equals(FULLSCREEN_CHANGED)) { |
144 | 0 | ZCC_LOG("Got a fullscreen-change event in %p\n", this); |
145 | 0 | RefreshZoomConstraints(); |
146 | 0 | } |
147 | 0 |
|
148 | 0 | return NS_OK; |
149 | 0 | } |
150 | | |
151 | | NS_IMETHODIMP |
152 | | ZoomConstraintsClient::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData) |
153 | 0 | { |
154 | 0 | if (SameCOMIdentity(aSubject, mDocument) && BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) { |
155 | 0 | ZCC_LOG("Got a before-first-paint event in %p\n", this); |
156 | 0 | RefreshZoomConstraints(); |
157 | 0 | } else if (NS_PREF_CHANGED.EqualsASCII(aTopic)) { |
158 | 0 | ZCC_LOG("Got a pref-change event in %p\n", this); |
159 | 0 | // We need to run this later because all the pref change listeners need |
160 | 0 | // to execute before we can be guaranteed that gfxPrefs::ForceUserScalable() |
161 | 0 | // returns the updated value. |
162 | 0 |
|
163 | 0 | RefPtr<nsRunnableMethod<ZoomConstraintsClient>> event = |
164 | 0 | NewRunnableMethod("ZoomConstraintsClient::RefreshZoomConstraints", |
165 | 0 | this, |
166 | 0 | &ZoomConstraintsClient::RefreshZoomConstraints); |
167 | 0 | mDocument->Dispatch(TaskCategory::Other, event.forget()); |
168 | 0 | } |
169 | 0 | return NS_OK; |
170 | 0 | } |
171 | | |
172 | | void |
173 | | ZoomConstraintsClient::ScreenSizeChanged() |
174 | 0 | { |
175 | 0 | ZCC_LOG("Got a screen-size change notification in %p\n", this); |
176 | 0 | RefreshZoomConstraints(); |
177 | 0 | } |
178 | | |
179 | | static mozilla::layers::ZoomConstraints |
180 | | ComputeZoomConstraintsFromViewportInfo(const nsViewportInfo& aViewportInfo) |
181 | 0 | { |
182 | 0 | mozilla::layers::ZoomConstraints constraints; |
183 | 0 | constraints.mAllowZoom = aViewportInfo.IsZoomAllowed() && gfxPrefs::APZAllowZooming(); |
184 | 0 | constraints.mAllowDoubleTapZoom = constraints.mAllowZoom && gfxPrefs::APZAllowDoubleTapZooming(); |
185 | 0 | if (constraints.mAllowZoom) { |
186 | 0 | constraints.mMinZoom.scale = aViewportInfo.GetMinZoom().scale; |
187 | 0 | constraints.mMaxZoom.scale = aViewportInfo.GetMaxZoom().scale; |
188 | 0 | } else { |
189 | 0 | constraints.mMinZoom.scale = aViewportInfo.GetDefaultZoom().scale; |
190 | 0 | constraints.mMaxZoom.scale = aViewportInfo.GetDefaultZoom().scale; |
191 | 0 | } |
192 | 0 | return constraints; |
193 | 0 | } |
194 | | |
195 | | void |
196 | | ZoomConstraintsClient::RefreshZoomConstraints() |
197 | 0 | { |
198 | 0 | nsIWidget* widget = GetWidget(mPresShell); |
199 | 0 | if (!widget) { |
200 | 0 | return; |
201 | 0 | } |
202 | 0 | |
203 | 0 | uint32_t presShellId = 0; |
204 | 0 | FrameMetrics::ViewID viewId = FrameMetrics::NULL_SCROLL_ID; |
205 | 0 | bool scrollIdentifiersValid = APZCCallbackHelper::GetOrCreateScrollIdentifiers( |
206 | 0 | mDocument->GetDocumentElement(), |
207 | 0 | &presShellId, &viewId); |
208 | 0 | if (!scrollIdentifiersValid) { |
209 | 0 | return; |
210 | 0 | } |
211 | 0 | |
212 | 0 | LayoutDeviceIntSize screenSize; |
213 | 0 | if (!nsLayoutUtils::GetContentViewerSize(mPresShell->GetPresContext(), screenSize)) { |
214 | 0 | return; |
215 | 0 | } |
216 | 0 | |
217 | 0 | nsViewportInfo viewportInfo = mDocument->GetViewportInfo( |
218 | 0 | ViewAs<ScreenPixel>(screenSize, PixelCastJustification::LayoutDeviceIsScreenForBounds)); |
219 | 0 |
|
220 | 0 | mozilla::layers::ZoomConstraints zoomConstraints = |
221 | 0 | ComputeZoomConstraintsFromViewportInfo(viewportInfo); |
222 | 0 |
|
223 | 0 | if (mDocument->Fullscreen()) { |
224 | 0 | ZCC_LOG("%p is in fullscreen, disallowing zooming\n", this); |
225 | 0 | zoomConstraints.mAllowZoom = false; |
226 | 0 | zoomConstraints.mAllowDoubleTapZoom = false; |
227 | 0 | } |
228 | 0 |
|
229 | 0 | if (zoomConstraints.mAllowDoubleTapZoom) { |
230 | 0 | // If the CSS viewport is narrower than the screen (i.e. width <= device-width) |
231 | 0 | // then we disable double-tap-to-zoom behaviour. |
232 | 0 | CSSToLayoutDeviceScale scale = |
233 | 0 | mPresShell->GetPresContext()->CSSToDevPixelScale(); |
234 | 0 | if ((viewportInfo.GetSize() * scale).width <= screenSize.width) { |
235 | 0 | zoomConstraints.mAllowDoubleTapZoom = false; |
236 | 0 | } |
237 | 0 | } |
238 | 0 |
|
239 | 0 | // We only ever create a ZoomConstraintsClient for an RCD, so the RSF of |
240 | 0 | // the presShell must be the RCD-RSF (if it exists). |
241 | 0 | MOZ_ASSERT(mPresShell->GetPresContext()->IsRootContentDocument()); |
242 | 0 | if (nsIScrollableFrame* rcdrsf = mPresShell->GetRootScrollFrameAsScrollable()) { |
243 | 0 | ZCC_LOG("Notifying RCD-RSF that it is zoomable: %d\n", zoomConstraints.mAllowZoom); |
244 | 0 | rcdrsf->SetZoomableByAPZ(zoomConstraints.mAllowZoom); |
245 | 0 | } |
246 | 0 |
|
247 | 0 | ScrollableLayerGuid newGuid(LayersId{0}, presShellId, viewId); |
248 | 0 | if (mGuid && mGuid.value() != newGuid) { |
249 | 0 | ZCC_LOG("Clearing old constraints in %p for { %u, %" PRIu64 " }\n", |
250 | 0 | this, mGuid->mPresShellId, mGuid->mScrollId); |
251 | 0 | // If the guid changes, send a message to clear the old one |
252 | 0 | widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId, Nothing()); |
253 | 0 | } |
254 | 0 | mGuid = Some(newGuid); |
255 | 0 | ZCC_LOG("Sending constraints %s in %p for { %u, %" PRIu64 " }\n", |
256 | 0 | Stringify(zoomConstraints).c_str(), this, presShellId, viewId); |
257 | 0 | widget->UpdateZoomConstraints(presShellId, viewId, Some(zoomConstraints)); |
258 | 0 | } |