2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-06-17 19:32:42 +03:00
|
|
|
/* 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>
|
2015-12-22 09:14:12 +03:00
|
|
|
#include "gfxPrefs.h"
|
2015-06-17 19:32:42 +03:00
|
|
|
#include "LayersLogging.h"
|
|
|
|
#include "mozilla/layers/APZCCallbackHelper.h"
|
2018-11-01 23:15:46 +03:00
|
|
|
#include "mozilla/layers/ScrollableLayerGuid.h"
|
|
|
|
#include "mozilla/layers/ZoomConstraints.h"
|
2015-12-22 09:14:12 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-11-30 06:14:28 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2016-01-12 07:19:08 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
2015-06-17 19:32:42 +03:00
|
|
|
#include "nsDocument.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
#include "nsPoint.h"
|
|
|
|
#include "nsView.h"
|
|
|
|
#include "nsViewportInfo.h"
|
|
|
|
#include "Units.h"
|
|
|
|
#include "UnitTransforms.h"
|
|
|
|
|
|
|
|
#define ZCC_LOG(...)
|
|
|
|
// #define ZCC_LOG(...) printf_stderr("ZCC: " __VA_ARGS__)
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ZoomConstraintsClient, nsIDOMEventListener, nsIObserver)
|
|
|
|
|
2017-11-23 09:06:15 +03:00
|
|
|
#define DOM_META_ADDED NS_LITERAL_STRING("DOMMetaAdded")
|
|
|
|
#define DOM_META_CHANGED NS_LITERAL_STRING("DOMMetaChanged")
|
|
|
|
#define FULLSCREEN_CHANGED NS_LITERAL_STRING("fullscreenchange")
|
|
|
|
#define BEFORE_FIRST_PAINT NS_LITERAL_CSTRING("before-first-paint")
|
|
|
|
#define NS_PREF_CHANGED NS_LITERAL_CSTRING("nsPref:changed")
|
2015-06-17 19:32:42 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
ZoomConstraintsClient::ZoomConstraintsClient()
|
|
|
|
: mDocument(nullptr), mPresShell(nullptr) {}
|
|
|
|
|
|
|
|
ZoomConstraintsClient::~ZoomConstraintsClient() {}
|
|
|
|
|
|
|
|
static nsIWidget* GetWidget(nsIPresShell* aShell) {
|
2016-01-25 22:02:53 +03:00
|
|
|
if (!aShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-17 19:32:42 +03:00
|
|
|
if (nsIFrame* rootFrame = aShell->GetRootFrame()) {
|
2015-09-29 01:09:00 +03:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_UIKIT)
|
2015-06-17 19:32:42 +03:00
|
|
|
return rootFrame->GetNearestWidget();
|
|
|
|
#else
|
|
|
|
if (nsView* view = rootFrame->GetView()) {
|
|
|
|
return view->GetWidget();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomConstraintsClient::Destroy() {
|
|
|
|
if (!(mPresShell && mDocument)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZCC_LOG("Destroying %p\n", this);
|
|
|
|
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->RemoveEventListener(DOM_META_ADDED, this, false);
|
2015-09-01 21:04:56 +03:00
|
|
|
mEventTarget->RemoveEventListener(DOM_META_CHANGED, this, false);
|
2016-09-22 16:42:44 +03:00
|
|
|
mEventTarget->RemoveSystemEventListener(FULLSCREEN_CHANGED, this, false);
|
2015-06-17 19:32:42 +03:00
|
|
|
mEventTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
|
|
|
|
}
|
|
|
|
|
2015-09-03 17:30:41 +03:00
|
|
|
Preferences::RemoveObserver(this, "browser.ui.zoom.force-user-scalable");
|
|
|
|
|
2015-06-17 19:32:42 +03:00
|
|
|
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(nsIPresShell* aPresShell,
|
|
|
|
nsIDocument* aDocument) {
|
|
|
|
if (!(aPresShell && aDocument)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPresShell = aPresShell;
|
|
|
|
mDocument = aDocument;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow()) {
|
2016-06-02 18:02:14 +03:00
|
|
|
mEventTarget = window->GetParentTarget();
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->AddEventListener(DOM_META_ADDED, this, false);
|
2015-09-01 21:04:56 +03:00
|
|
|
mEventTarget->AddEventListener(DOM_META_CHANGED, this, false);
|
2016-09-22 16:42:44 +03:00
|
|
|
mEventTarget->AddSystemEventListener(FULLSCREEN_CHANGED, this, false);
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
|
|
|
|
}
|
2015-09-03 17:30:41 +03:00
|
|
|
|
|
|
|
Preferences::AddStrongObserver(this, "browser.ui.zoom.force-user-scalable");
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
ZoomConstraintsClient::HandleEvent(dom::Event* event) {
|
2015-06-17 19:32:42 +03:00
|
|
|
nsAutoString type;
|
|
|
|
event->GetType(type);
|
|
|
|
|
|
|
|
if (type.Equals(DOM_META_ADDED)) {
|
|
|
|
ZCC_LOG("Got a dom-meta-added event in %p\n", this);
|
|
|
|
RefreshZoomConstraints();
|
2015-09-01 21:04:56 +03:00
|
|
|
} else if (type.Equals(DOM_META_CHANGED)) {
|
|
|
|
ZCC_LOG("Got a dom-meta-changed event in %p\n", this);
|
|
|
|
RefreshZoomConstraints();
|
2016-09-21 23:19:10 +03:00
|
|
|
} else if (type.Equals(FULLSCREEN_CHANGED)) {
|
|
|
|
ZCC_LOG("Got a fullscreen-change event in %p\n", this);
|
|
|
|
RefreshZoomConstraints();
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
2015-09-01 21:04:56 +03:00
|
|
|
|
2015-06-17 19:32:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ZoomConstraintsClient::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
if (SameCOMIdentity(aSubject, mDocument) &&
|
|
|
|
BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
|
|
|
|
ZCC_LOG("Got a before-first-paint event in %p\n", this);
|
|
|
|
RefreshZoomConstraints();
|
2015-09-03 17:30:41 +03:00
|
|
|
} 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 gfxPrefs::ForceUserScalable()
|
|
|
|
// returns the updated value.
|
2017-03-15 07:08:13 +03:00
|
|
|
|
|
|
|
RefPtr<nsRunnableMethod<ZoomConstraintsClient>> event =
|
2017-06-12 22:34:10 +03:00
|
|
|
NewRunnableMethod("ZoomConstraintsClient::RefreshZoomConstraints", this,
|
|
|
|
&ZoomConstraintsClient::RefreshZoomConstraints);
|
2017-07-26 11:13:35 +03:00
|
|
|
mDocument->Dispatch(TaskCategory::Other, event.forget());
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-26 21:41:12 +03:00
|
|
|
void ZoomConstraintsClient::ScreenSizeChanged() {
|
|
|
|
ZCC_LOG("Got a screen-size change notification in %p\n", this);
|
|
|
|
RefreshZoomConstraints();
|
|
|
|
}
|
|
|
|
|
2018-02-23 08:03:45 +03:00
|
|
|
static mozilla::layers::ZoomConstraints ComputeZoomConstraintsFromViewportInfo(
|
2015-06-17 19:32:42 +03:00
|
|
|
const nsViewportInfo& aViewportInfo) {
|
|
|
|
mozilla::layers::ZoomConstraints constraints;
|
2015-07-23 06:42:08 +03:00
|
|
|
constraints.mAllowZoom =
|
|
|
|
aViewportInfo.IsZoomAllowed() && gfxPrefs::APZAllowZooming();
|
2018-07-24 23:51:01 +03:00
|
|
|
constraints.mAllowDoubleTapZoom =
|
|
|
|
constraints.mAllowZoom && gfxPrefs::APZAllowDoubleTapZooming();
|
2015-09-03 17:30:40 +03:00
|
|
|
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;
|
|
|
|
}
|
2015-06-17 19:32:42 +03:00
|
|
|
return constraints;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoomConstraintsClient::RefreshZoomConstraints() {
|
|
|
|
nsIWidget* widget = GetWidget(mPresShell);
|
|
|
|
if (!widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t presShellId = 0;
|
2018-11-01 23:15:46 +03:00
|
|
|
ScrollableLayerGuid::ViewID viewId = ScrollableLayerGuid::NULL_SCROLL_ID;
|
2015-06-17 19:32:42 +03:00
|
|
|
bool scrollIdentifiersValid =
|
|
|
|
APZCCallbackHelper::GetOrCreateScrollIdentifiers(
|
|
|
|
mDocument->GetDocumentElement(), &presShellId, &viewId);
|
|
|
|
if (!scrollIdentifiersValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-26 21:41:20 +03:00
|
|
|
LayoutDeviceIntSize screenSize;
|
|
|
|
if (!nsLayoutUtils::GetContentViewerSize(mPresShell->GetPresContext(),
|
|
|
|
screenSize)) {
|
|
|
|
return;
|
2015-06-17 19:32:42 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 20:21:57 +03:00
|
|
|
nsViewportInfo viewportInfo = mDocument->GetViewportInfo(ViewAs<ScreenPixel>(
|
2015-06-17 19:32:42 +03:00
|
|
|
screenSize, PixelCastJustification::LayoutDeviceIsScreenForBounds));
|
|
|
|
|
|
|
|
mozilla::layers::ZoomConstraints zoomConstraints =
|
|
|
|
ComputeZoomConstraintsFromViewportInfo(viewportInfo);
|
|
|
|
|
2016-09-21 23:19:10 +03:00
|
|
|
if (mDocument->Fullscreen()) {
|
|
|
|
ZCC_LOG("%p is in fullscreen, disallowing zooming\n", this);
|
|
|
|
zoomConstraints.mAllowZoom = false;
|
|
|
|
zoomConstraints.mAllowDoubleTapZoom = false;
|
|
|
|
}
|
|
|
|
|
2015-06-17 19:32:42 +03:00
|
|
|
if (zoomConstraints.mAllowDoubleTapZoom) {
|
|
|
|
// If the CSS viewport is narrower than the screen (i.e. width <=
|
|
|
|
// device-width) then we disable double-tap-to-zoom behaviour.
|
2015-09-29 03:14:26 +03:00
|
|
|
CSSToLayoutDeviceScale scale =
|
|
|
|
mPresShell->GetPresContext()->CSSToDevPixelScale();
|
2015-06-17 19:32:42 +03:00
|
|
|
if ((viewportInfo.GetSize() * scale).width <= screenSize.width) {
|
|
|
|
zoomConstraints.mAllowDoubleTapZoom = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 04:58:16 +03:00
|
|
|
// 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()->IsRootContentDocument());
|
|
|
|
if (nsIScrollableFrame* rcdrsf =
|
|
|
|
mPresShell->GetRootScrollFrameAsScrollable()) {
|
|
|
|
ZCC_LOG("Notifying RCD-RSF that it is zoomable: %d\n",
|
|
|
|
zoomConstraints.mAllowZoom);
|
|
|
|
rcdrsf->SetZoomableByAPZ(zoomConstraints.mAllowZoom);
|
|
|
|
}
|
|
|
|
|
2018-03-25 02:06:01 +03:00
|
|
|
ScrollableLayerGuid newGuid(LayersId{0}, presShellId, viewId);
|
2015-06-17 19:32:42 +03:00
|
|
|
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",
|
|
|
|
Stringify(zoomConstraints).c_str(), this, presShellId, viewId);
|
|
|
|
widget->UpdateZoomConstraints(presShellId, viewId, Some(zoomConstraints));
|
|
|
|
}
|