summaryrefslogtreecommitdiffstats
path: root/layout/base/GeckoMVMContext.cpp
blob: c8e7d8d72a264336defc1ae94375001c1226c6bb (plain)
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
/* 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 "GeckoMVMContext.h"

#include "mozilla/DisplayPortUtils.h"
#include "mozilla/PresShell.h"
#include "mozilla/Services.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/VisualViewport.h"
#include "nsCOMPtr.h"
#include "nsGlobalWindowInner.h"
#include "nsIDOMEventListener.h"
#include "nsIFrame.h"
#include "nsIObserverService.h"
#include "nsIScrollableFrame.h"
#include "nsLayoutUtils.h"
#include "nsPIDOMWindow.h"
#include "nsPresContext.h"

namespace mozilla {

GeckoMVMContext::GeckoMVMContext(dom::Document* aDocument,
                                 PresShell* aPresShell)
    : mDocument(aDocument), mPresShell(aPresShell) {
  if (nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow()) {
    mEventTarget = window->GetChromeEventHandler();
  }
}

void GeckoMVMContext::AddEventListener(const nsAString& aType,
                                       nsIDOMEventListener* aListener,
                                       bool aUseCapture) {
  if (mEventTarget) {
    mEventTarget->AddEventListener(aType, aListener, aUseCapture);
  }
}

void GeckoMVMContext::RemoveEventListener(const nsAString& aType,
                                          nsIDOMEventListener* aListener,
                                          bool aUseCapture) {
  if (mEventTarget) {
    mEventTarget->RemoveEventListener(aType, aListener, aUseCapture);
  }
}

void GeckoMVMContext::AddObserver(nsIObserver* aObserver, const char* aTopic,
                                  bool aOwnsWeak) {
  if (nsCOMPtr<nsIObserverService> observerService =
          services::GetObserverService()) {
    observerService->AddObserver(aObserver, aTopic, aOwnsWeak);
  }
}

void GeckoMVMContext::RemoveObserver(nsIObserver* aObserver,
                                     const char* aTopic) {
  if (nsCOMPtr<nsIObserverService> observerService =
          services::GetObserverService()) {
    observerService->RemoveObserver(aObserver, aTopic);
  }
}

void GeckoMVMContext::Destroy() {
  mEventTarget = nullptr;
  mDocument = nullptr;
  mPresShell = nullptr;
}

nsViewportInfo GeckoMVMContext::GetViewportInfo(
    const ScreenIntSize& aDisplaySize) const {
  MOZ_ASSERT(mDocument);
  return mDocument->GetViewportInfo(aDisplaySize);
}

CSSToLayoutDeviceScale GeckoMVMContext::CSSToDevPixelScale() const {
  MOZ_ASSERT(mPresShell);
  return mPresShell->GetPresContext()->CSSToDevPixelScale();
}

float GeckoMVMContext::GetResolution() const {
  MOZ_ASSERT(mPresShell);
  return mPresShell->GetResolution();
}

bool GeckoMVMContext::SubjectMatchesDocument(nsISupports* aSubject) const {
  MOZ_ASSERT(mDocument);
  return SameCOMIdentity(aSubject, ToSupports(mDocument));
}

Maybe<CSSRect> GeckoMVMContext::CalculateScrollableRectForRSF() const {
  MOZ_ASSERT(mPresShell);
  if (nsIScrollableFrame* rootScrollableFrame =
          mPresShell->GetRootScrollFrameAsScrollable()) {
    return Some(
        CSSRect::FromAppUnits(nsLayoutUtils::CalculateScrollableRectForFrame(
            rootScrollableFrame, nullptr)));
  }
  return Nothing();
}

bool GeckoMVMContext::IsResolutionUpdatedByApz() const {
  MOZ_ASSERT(mPresShell);
  return mPresShell->IsResolutionUpdatedByApz();
}

LayoutDeviceMargin
GeckoMVMContext::ScrollbarAreaToExcludeFromCompositionBounds() const {
  MOZ_ASSERT(mPresShell);
  return LayoutDeviceMargin::FromAppUnits(
      nsLayoutUtils::ScrollbarAreaToExcludeFromCompositionBoundsFor(
          mPresShell->GetRootScrollFrame()),
      mPresShell->GetPresContext()->AppUnitsPerDevPixel());
}

Maybe<LayoutDeviceIntSize> GeckoMVMContext::GetContentViewerSize() const {
  MOZ_ASSERT(mPresShell);
  LayoutDeviceIntSize result;
  if (nsLayoutUtils::GetContentViewerSize(mPresShell->GetPresContext(),
                                          result)) {
    return Some(result);
  }
  return Nothing();
}

bool GeckoMVMContext::AllowZoomingForDocument() const {
  MOZ_ASSERT(mDocument);
  return nsLayoutUtils::AllowZoomingForDocument(mDocument);
}

bool GeckoMVMContext::IsInReaderMode() const {
  MOZ_ASSERT(mDocument);
  nsString uri;
  if (NS_FAILED(mDocument->GetDocumentURI(uri))) {
    return false;
  }
  static auto readerModeUriPrefix = u"about:reader"_ns;
  return StringBeginsWith(uri, readerModeUriPrefix);
}

bool GeckoMVMContext::IsDocumentLoading() const {
  MOZ_ASSERT(mDocument);
  return mDocument->GetReadyStateEnum() == dom::Document::READYSTATE_LOADING;
}

void GeckoMVMContext::SetResolutionAndScaleTo(float aResolution,
                                              ResolutionChangeOrigin aOrigin) {
  MOZ_ASSERT(mPresShell);
  mPresShell->SetResolutionAndScaleTo(aResolution, aOrigin);
}

void GeckoMVMContext::SetVisualViewportSize(const CSSSize& aSize) {
  MOZ_ASSERT(mPresShell);
  mPresShell->SetVisualViewportSize(
      nsPresContext::CSSPixelsToAppUnits(aSize.width),
      nsPresContext::CSSPixelsToAppUnits(aSize.height));
}

void GeckoMVMContext::PostVisualViewportResizeEventByDynamicToolbar() {
  MOZ_ASSERT(mDocument);

  // We only fire visual viewport events and don't want to cause any explicit
  // reflows here since in general we don't use the up-to-date visual viewport
  // size for layout.
  if (auto* window = nsGlobalWindowInner::Cast(mDocument->GetInnerWindow())) {
    window->VisualViewport()->PostResizeEvent();
  }
}

void GeckoMVMContext::UpdateDisplayPortMargins() {
  MOZ_ASSERT(mPresShell);
  if (nsIFrame* root = mPresShell->GetRootScrollFrame()) {
    nsIContent* content = root->GetContent();
    bool hasDisplayPort = DisplayPortUtils::HasNonMinimalDisplayPort(content);
    bool hasResolution = mPresShell->GetResolution() != 1.0f;
    if (!hasDisplayPort && !hasResolution) {
      // We only want to update the displayport if there is one already, or
      // add one if there's a resolution on the document (see bug 1225508
      // comment 1).
      return;
    }
    nsRect displayportBase = nsRect(
        nsPoint(0, 0), nsLayoutUtils::CalculateCompositionSizeForFrame(root));
    // We only create MobileViewportManager for root content documents. If that
    // ever changes we'd need to limit the size of this displayport base rect
    // because non-toplevel documents have no limit on their size.
    MOZ_ASSERT(
        mPresShell->GetPresContext()->IsRootContentDocumentCrossProcess());
    DisplayPortUtils::SetDisplayPortBaseIfNotSet(content, displayportBase);
    nsIScrollableFrame* scrollable = do_QueryFrame(root);
    DisplayPortUtils::CalculateAndSetDisplayPortMargins(
        scrollable, DisplayPortUtils::RepaintMode::Repaint);
  }
}

void GeckoMVMContext::Reflow(const CSSSize& aNewSize) {
  RefPtr doc = mDocument;
  RefPtr ps = mPresShell;

  MOZ_ASSERT(doc);
  MOZ_ASSERT(ps);

  if (ps->ResizeReflowIgnoreOverride(CSSPixel::ToAppUnits(aNewSize.width),
                                     CSSPixel::ToAppUnits(aNewSize.height))) {
    doc->FlushPendingNotifications(FlushType::InterruptibleLayout);
  }
}

}  // namespace mozilla