Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 271ff0b

Browse files
committed
Bug 1937785 - Remove nsPresContext::mKeyboardHeight and introduce MobileViewportManager::mPendingKeyboardHeight. r=botond
There's a race condition where the keyboard height change notification has arrived but nsDocumentViewer size change hasn't yet arrived. MobileViewportManager::mPendingKeyboardHeight is introduced to fix the race. Differential Revision: https://phabricator.services.mozilla.com/D234262
1 parent 1d97ba6 commit 271ff0b

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

layout/base/MobileViewportManager.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,12 @@ void MobileViewportManager::RefreshViewportSize(bool aForceAdjustResolution) {
648648
return;
649649
}
650650

651+
// Now it's time to update the keyboard height
652+
if (mPendingKeyboardHeight) {
653+
mKeyboardHeight = *mPendingKeyboardHeight;
654+
mPendingKeyboardHeight.reset();
655+
}
656+
651657
nsViewportInfo viewportInfo =
652658
mContext->GetViewportInfo(GetLayoutDisplaySize());
653659
MVM_LOG("%p: viewport info has zooms min=%f max=%f default=%f,valid=%d\n",
@@ -760,10 +766,11 @@ ParentLayerSize MobileViewportManager::GetCompositionSizeWithoutDynamicToolbar()
760766

761767
void MobileViewportManager::UpdateKeyboardHeight(
762768
ScreenIntCoord aKeyboardHeight) {
763-
if (aKeyboardHeight == mKeyboardHeight) {
769+
if (mPendingKeyboardHeight == Some(aKeyboardHeight)) {
764770
return;
765771
}
766-
mKeyboardHeight = aKeyboardHeight;
772+
773+
mPendingKeyboardHeight = Some(aKeyboardHeight);
767774
mInvalidViewport = true;
768775
}
769776

layout/base/MobileViewportManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class MobileViewportManager final : public nsIDOMEventListener,
153153
*/
154154
nsRect InitialVisibleArea();
155155

156+
mozilla::ScreenIntCoord GetKeyboardHeight() const { return mKeyboardHeight; }
157+
156158
private:
157159
~MobileViewportManager();
158160

@@ -243,6 +245,7 @@ class MobileViewportManager final : public nsIDOMEventListener,
243245
* The software keyboard height.
244246
*/
245247
mozilla::ScreenIntCoord mKeyboardHeight;
248+
mozilla::Maybe<mozilla::ScreenIntCoord> mPendingKeyboardHeight;
246249
};
247250

248251
#endif

layout/base/PresShell.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11418,7 +11418,11 @@ void PresShell::MaybeRecreateMobileViewportManager(bool aAfterInitialization) {
1141811418
("Created MVM %p (type %d) for URI %s", mMobileViewportManager.get(),
1141911419
(int)*mvmType, uri ? uri->GetSpecOrDefault().get() : "(null)"));
1142011420
}
11421+
if (BrowserChild* browserChild = BrowserChild::GetFrom(this)) {
11422+
mMobileViewportManager->UpdateKeyboardHeight(browserChild->GetKeyboardHeight());
11423+
}
1142111424
}
11425+
1142211426
if (aAfterInitialization) {
1142311427
// Setting the initial viewport will trigger a reflow.
1142411428
if (mMobileViewportManager) {

layout/base/nsPresContext.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ nsPresContext::nsPresContext(dom::Document* aDocument, nsPresContextType aType)
247247
mCurAppUnitsPerDevPixel(0),
248248
mDynamicToolbarMaxHeight(0),
249249
mDynamicToolbarHeight(0),
250-
mKeyboardHeight(0),
251250
mPageSize(-1, -1),
252251
mPageScale(0.0),
253252
mPPScale(1.0f),
@@ -722,8 +721,6 @@ nsresult nsPresContext::Init(nsDeviceContext* aDeviceContext) {
722721
#if defined(MOZ_WIDGET_ANDROID)
723722
if (IsRootContentDocumentCrossProcess()) {
724723
if (BrowserChild* browserChild = BrowserChild::GetFrom(GetDocShell())) {
725-
mKeyboardHeight = browserChild->GetKeyboardHeight();
726-
727724
if (MOZ_LIKELY(!Preferences::HasUserValue(
728725
"layout.dynamic-toolbar-max-height"))) {
729726
mDynamicToolbarMaxHeight = browserChild->GetDynamicToolbarMaxHeight();
@@ -3129,7 +3126,7 @@ void nsPresContext::UpdateDynamicToolbarOffset(ScreenIntCoord aOffset) {
31293126

31303127
dom::InteractiveWidget interactiveWidget = mDocument->InteractiveWidget();
31313128
if (interactiveWidget == InteractiveWidget::OverlaysContent &&
3132-
mKeyboardHeight > 0) {
3129+
GetKeyboardHeight() > 0) {
31333130
// On overlays-content mode, the toolbar offset change should NOT affect
31343131
// the visual viewport while the software keyboard is being shown since
31353132
// the toolbar will be positioned somewhere in the middle of the visual
@@ -3163,8 +3160,6 @@ void nsPresContext::UpdateDynamicToolbarOffset(ScreenIntCoord aOffset) {
31633160

31643161
void nsPresContext::UpdateKeyboardHeight(ScreenIntCoord aHeight) {
31653162
MOZ_ASSERT(IsRootContentDocumentCrossProcess());
3166-
mKeyboardHeight = aHeight;
3167-
31683163
if (!mPresShell) {
31693164
return;
31703165
}
@@ -3175,8 +3170,13 @@ void nsPresContext::UpdateKeyboardHeight(ScreenIntCoord aHeight) {
31753170
}
31763171
}
31773172

3173+
ScreenIntCoord nsPresContext::GetKeyboardHeight() const {
3174+
MobileViewportManager* mvm = mPresShell->GetMobileViewportManager();
3175+
return mvm ? mvm->GetKeyboardHeight() : ScreenIntCoord(0);
3176+
}
3177+
31783178
bool nsPresContext::IsKeyboardHiddenOrResizesContentMode() const {
3179-
return mKeyboardHeight == 0 ||
3179+
return GetKeyboardHeight() == 0 ||
31803180
mDocument->InteractiveWidget() == InteractiveWidget::ResizesContent;
31813181
}
31823182

layout/base/nsPresContext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
442442

443443
void UpdateKeyboardHeight(mozilla::ScreenIntCoord aHeight);
444444

445-
mozilla::ScreenIntCoord GetKeyboardHeight() const { return mKeyboardHeight; }
445+
mozilla::ScreenIntCoord GetKeyboardHeight() const;
446446

447447
/**
448448
* Returns true if the software keyboard is hidden or
@@ -1247,8 +1247,6 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr {
12471247
// The maximum height of the dynamic toolbar on mobile.
12481248
mozilla::ScreenIntCoord mDynamicToolbarMaxHeight;
12491249
mozilla::ScreenIntCoord mDynamicToolbarHeight;
1250-
// The software keyboard height.
1251-
mozilla::ScreenIntCoord mKeyboardHeight;
12521250
// Safe area insets support
12531251
mozilla::LayoutDeviceIntMargin mSafeAreaInsets;
12541252
nsSize mPageSize;

0 commit comments

Comments
 (0)