- Revision
- 290794
- Author
- [email protected]
- Date
- 2022-03-03 14:07:56 -0800 (Thu, 03 Mar 2022)
Log Message
[iOS] Further reduce hangs underneath -[WKContentView requestAutocorrectionContextWithCompletionHandler:]
https://bugs.webkit.org/show_bug.cgi?id=237387
rdar://89654600
Reviewed by Tim Horton.
Tweak the mitigations landed in r288925 to be even more aggressive. Currently, we preemptively send an
autocorrection context from the web process to the UI process after receiving an IPC message from the UI process
indicating that an input view has been shown. However, this message might arrive too late in cases where the web
process hangs immediately after we're done setting element focus, within the very same runloop; in such a
scenario, we'll still end up hanging underneath the sync autocorrection request underneath `-_elementDidFocus:`
in the UI process.
We can instead make this mitigation even more aggressive, by setting a flag on WebPage when we're about to
propagate an `ElementDidFocus` message to the UI process that (in lieu of the WebKit client) would normally
cause the keyboard to appear. During the next DOM selection change, we then consult this flag and preemptively
send an autocorrection context to the UI process.
Additionally, remove another safeguard in the original change in r288925 which effectively invalidated the
cached autocorrection context after the end of the runloop by setting `_autocorrectionContextNeedsUpdate`. This
was only done to narrow the scope of the fix to just the scenario where UIKit requests autocorrection contexts
multiple times in the same runloop iteration. However, this appears to be insufficient to tackle more scenarios
where UIKit frequently requests autocorrection contexts even though the selection has not changed; hopefully, by
leaving `_autocorrectionContextNeedsUpdate := NO`, we can mitigate some of these other cases.
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView requestAutocorrectionContextWithCompletionHandler:]):
* WebProcess/Plugins/PDF/PDFPlugin.mm:
(WebKit::PDFPlugin::notifySelectionChanged):
* WebProcess/WebCoreSupport/WebEditorClient.cpp:
(WebKit::WebEditorClient::respondToChangedSelection):
* WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm:
(WebKit::WebChromeClient::notifyRevealedSelectionByScrollingFrame):
(WebKit::WebChromeClient::didLayout):
* WebProcess/WebCoreSupport/ios/WebEditorClientIOS.mm:
(WebKit::WebEditorClient::overflowScrollPositionChanged):
(WebKit::WebEditorClient::subFrameScrollPositionChanged):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::didScrollSelection):
(WebKit::WebPage::didChangeSelection):
Split `didChangeSelection` into two methods: `didScrollSelection`, which is invoked from codepaths where we're
scheduling an editor state after overflow or mainframe scrolling, and `didChangeSelection`, which now takes a
`Frame` indicating the frame that contains the changed selection. On iOS, we handle this by both scheduling an
editor state update, but also preemptively sending the autocorrection context to the UI process if we've just
focused an element that will probably cause the keyboard to appear (unless the client explicitly prevents it).
Note that we must check the `frame` containing the changed selection and bail if the render tree is in the
middle of being destroyed, so that we don't end up triggering layout (and release asserting) in the case where
we're clearing the selection during document teardown.
(WebKit::WebPage::resetFocusedElementForFrame):
(WebKit::WebPage::elementDidFocus):
(WebKit::WebPage::elementDidBlur):
(WebKit::WebPage::didCommitLoad):
(WebKit::WebPage::didChangeOverflowScrollPosition): Deleted.
* WebProcess/WebPage/WebPage.h:
(WebKit::WebPage::setIsShowingInputViewForFocusedElement):
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::updateSelectionAppearance):
Pass in `frame` to `didChangeSelection`.
(WebKit::WebPage::setIsShowingInputViewForFocusedElement): Deleted.
Move this back into the header, now that it's a straightforward setter again.
Modified Paths
Diff
Modified: trunk/Source/WebKit/ChangeLog (290793 => 290794)
--- trunk/Source/WebKit/ChangeLog 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/ChangeLog 2022-03-03 22:07:56 UTC (rev 290794)
@@ -1,3 +1,72 @@
+2022-03-03 Wenson Hsieh <[email protected]>
+
+ [iOS] Further reduce hangs underneath -[WKContentView requestAutocorrectionContextWithCompletionHandler:]
+ https://bugs.webkit.org/show_bug.cgi?id=237387
+ rdar://89654600
+
+ Reviewed by Tim Horton.
+
+ Tweak the mitigations landed in r288925 to be even more aggressive. Currently, we preemptively send an
+ autocorrection context from the web process to the UI process after receiving an IPC message from the UI process
+ indicating that an input view has been shown. However, this message might arrive too late in cases where the web
+ process hangs immediately after we're done setting element focus, within the very same runloop; in such a
+ scenario, we'll still end up hanging underneath the sync autocorrection request underneath `-_elementDidFocus:`
+ in the UI process.
+
+ We can instead make this mitigation even more aggressive, by setting a flag on WebPage when we're about to
+ propagate an `ElementDidFocus` message to the UI process that (in lieu of the WebKit client) would normally
+ cause the keyboard to appear. During the next DOM selection change, we then consult this flag and preemptively
+ send an autocorrection context to the UI process.
+
+ Additionally, remove another safeguard in the original change in r288925 which effectively invalidated the
+ cached autocorrection context after the end of the runloop by setting `_autocorrectionContextNeedsUpdate`. This
+ was only done to narrow the scope of the fix to just the scenario where UIKit requests autocorrection contexts
+ multiple times in the same runloop iteration. However, this appears to be insufficient to tackle more scenarios
+ where UIKit frequently requests autocorrection contexts even though the selection has not changed; hopefully, by
+ leaving `_autocorrectionContextNeedsUpdate := NO`, we can mitigate some of these other cases.
+
+ * UIProcess/ios/WKContentViewInteraction.mm:
+ (-[WKContentView requestAutocorrectionContextWithCompletionHandler:]):
+ * WebProcess/Plugins/PDF/PDFPlugin.mm:
+ (WebKit::PDFPlugin::notifySelectionChanged):
+ * WebProcess/WebCoreSupport/WebEditorClient.cpp:
+ (WebKit::WebEditorClient::respondToChangedSelection):
+ * WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm:
+ (WebKit::WebChromeClient::notifyRevealedSelectionByScrollingFrame):
+ (WebKit::WebChromeClient::didLayout):
+ * WebProcess/WebCoreSupport/ios/WebEditorClientIOS.mm:
+ (WebKit::WebEditorClient::overflowScrollPositionChanged):
+ (WebKit::WebEditorClient::subFrameScrollPositionChanged):
+ * WebProcess/WebPage/WebPage.cpp:
+ (WebKit::WebPage::didScrollSelection):
+ (WebKit::WebPage::didChangeSelection):
+
+ Split `didChangeSelection` into two methods: `didScrollSelection`, which is invoked from codepaths where we're
+ scheduling an editor state after overflow or mainframe scrolling, and `didChangeSelection`, which now takes a
+ `Frame` indicating the frame that contains the changed selection. On iOS, we handle this by both scheduling an
+ editor state update, but also preemptively sending the autocorrection context to the UI process if we've just
+ focused an element that will probably cause the keyboard to appear (unless the client explicitly prevents it).
+
+ Note that we must check the `frame` containing the changed selection and bail if the render tree is in the
+ middle of being destroyed, so that we don't end up triggering layout (and release asserting) in the case where
+ we're clearing the selection during document teardown.
+
+ (WebKit::WebPage::resetFocusedElementForFrame):
+ (WebKit::WebPage::elementDidFocus):
+ (WebKit::WebPage::elementDidBlur):
+ (WebKit::WebPage::didCommitLoad):
+ (WebKit::WebPage::didChangeOverflowScrollPosition): Deleted.
+ * WebProcess/WebPage/WebPage.h:
+ (WebKit::WebPage::setIsShowingInputViewForFocusedElement):
+ * WebProcess/WebPage/ios/WebPageIOS.mm:
+ (WebKit::WebPage::updateSelectionAppearance):
+
+ Pass in `frame` to `didChangeSelection`.
+
+ (WebKit::WebPage::setIsShowingInputViewForFocusedElement): Deleted.
+
+ Move this back into the header, now that it's a straightforward setter again.
+
2022-03-03 Ross Kirsling <[email protected]>
REGRESSION(r290776): NetworkSession::swServer depends on ENABLE(SERVICE_WORKER)
Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (290793 => 290794)
--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm 2022-03-03 22:07:56 UTC (rev 290794)
@@ -4980,11 +4980,6 @@
if (!useSyncRequest)
return;
- callOnMainRunLoop([weakSelf = WeakObjCPtr<WKContentView>(self)]() {
- if (auto strongSelf = weakSelf.get())
- strongSelf->_autocorrectionContextNeedsUpdate = YES;
- });
-
if (!_page->process().connection()->waitForAndDispatchImmediately<Messages::WebPageProxy::HandleAutocorrectionContext>(_page->webPageID(), 1_s, IPC::WaitForOption::DispatchIncomingSyncMessagesWhileWaiting))
RELEASE_LOG(TextInput, "Timed out while waiting for autocorrection context.");
Modified: trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/Plugins/PDF/PDFPlugin.mm 2022-03-03 22:07:56 UTC (rev 290794)
@@ -2786,7 +2786,7 @@
{
if (!m_frame || !m_frame->page())
return;
- m_frame->page()->didChangeSelection();
+ m_frame->page()->didChangeSelection(*m_frame->coreFrame());
}
static const WebCore::Cursor& coreCursor(PDFLayerControllerCursorType type)
Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.cpp (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.cpp 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.cpp 2022-03-03 22:07:56 UTC (rev 290794)
@@ -217,7 +217,7 @@
if (!frame)
return;
- m_page->didChangeSelection();
+ m_page->didChangeSelection(*frame);
#if PLATFORM(GTK)
updateGlobalSelection(frame);
Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebChromeClientIOS.mm 2022-03-03 22:07:56 UTC (rev 290794)
@@ -76,7 +76,7 @@
void WebChromeClient::notifyRevealedSelectionByScrollingFrame(WebCore::Frame&)
{
- m_page.didChangeSelection();
+ m_page.didScrollSelection();
}
bool WebChromeClient::isStopping()
@@ -88,7 +88,7 @@
void WebChromeClient::didLayout(LayoutType type)
{
if (type == Scroll)
- m_page.didChangeSelection();
+ m_page.didScrollSelection();
}
void WebChromeClient::didStartOverflowScroll()
Modified: trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebEditorClientIOS.mm (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebEditorClientIOS.mm 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebCoreSupport/ios/WebEditorClientIOS.mm 2022-03-03 22:07:56 UTC (rev 290794)
@@ -94,12 +94,12 @@
void WebEditorClient::overflowScrollPositionChanged()
{
- m_page->didChangeOverflowScrollPosition();
+ m_page->didScrollSelection();
}
void WebEditorClient::subFrameScrollPositionChanged()
{
- m_page->didChangeOverflowScrollPosition();
+ m_page->didScrollSelection();
}
bool WebEditorClient::shouldAllowSingleClickToChangeSelection(WebCore::Node& targetNode, const WebCore::VisibleSelection& newSelection) const
Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp 2022-03-03 22:07:56 UTC (rev 290794)
@@ -6079,14 +6079,25 @@
sendEditorStateUpdate();
}
-void WebPage::didChangeOverflowScrollPosition()
+void WebPage::didScrollSelection()
{
didChangeSelectionOrOverflowScrollPosition();
}
-void WebPage::didChangeSelection()
+void WebPage::didChangeSelection(Frame& frame)
{
didChangeSelectionOrOverflowScrollPosition();
+
+#if PLATFORM(IOS_FAMILY)
+ if (!m_sendAutocorrectionContextAfterFocusingElement)
+ return;
+
+ if (UNLIKELY(!frame.document() || !frame.document()->hasLivingRenderTree() || frame.selection().isNone()))
+ return;
+
+ m_sendAutocorrectionContextAfterFocusingElement = false;
+ preemptivelySendAutocorrectionContext();
+#endif // PLATFORM(IOS_FAMILY)
}
void WebPage::didChangeSelectionOrOverflowScrollPosition()
@@ -6143,6 +6154,7 @@
if (frame->isMainFrame() || m_focusedElement->document().frame() == frame->coreFrame()) {
#if PLATFORM(IOS_FAMILY)
+ m_sendAutocorrectionContextAfterFocusingElement = false;
send(Messages::WebPageProxy::ElementDidBlur());
#elif PLATFORM(MAC)
send(Messages::WebPageProxy::SetEditableElementIsFocused(false));
@@ -6195,6 +6207,8 @@
if (element.document().fullscreenManager().isFullscreen())
element.document().fullscreenManager().cancelFullscreen();
#endif
+ if (m_userIsInteracting || m_keyboardIsAttached)
+ m_sendAutocorrectionContextAfterFocusingElement = true;
auto information = focusedElementInformation();
if (!information)
@@ -6230,6 +6244,9 @@
protectedThis->m_recentlyBlurredElement = nullptr;
});
m_hasPendingInputContextUpdateAfterBlurringAndRefocusingElement = false;
+#if PLATFORM(IOS_FAMILY)
+ m_sendAutocorrectionContextAfterFocusingElement = false;
+#endif
}
}
@@ -6544,6 +6561,7 @@
m_didUpdateRenderingAfterCommittingLoad = false;
#if PLATFORM(IOS_FAMILY)
+ m_sendAutocorrectionContextAfterFocusingElement = false;
m_hasReceivedVisibleContentRectsAfterDidCommitLoad = false;
m_hasRestoredExposedContentRectAfterDidCommitLoad = false;
m_lastTransactionIDWithScaleChange = firstTransactionIDAfterDidCommitLoad;
Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.h (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.h 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.h 2022-03-03 22:07:56 UTC (rev 290794)
@@ -824,7 +824,7 @@
void autofillLoginCredentials(const String&, const String&);
void setFocusedElementValue(const WebCore::ElementContext&, const String&);
void setFocusedElementSelectedIndex(const WebCore::ElementContext&, uint32_t index, bool allowMultipleSelection);
- void setIsShowingInputViewForFocusedElement(bool);
+ void setIsShowingInputViewForFocusedElement(bool showingInputView) { m_isShowingInputViewForFocusedElement = showingInputView; }
bool isShowingInputViewForFocusedElement() const { return m_isShowingInputViewForFocusedElement; }
void updateSelectionAppearance();
void getSelectionContext(CompletionHandler<void(const String&, const String&, const String&)>&&);
@@ -934,7 +934,8 @@
#endif
void didApplyStyle();
- void didChangeSelection();
+ void didScrollSelection();
+ void didChangeSelection(WebCore::Frame&);
void didChangeOverflowScrollPosition();
void didChangeContents();
void discardedComposition();
@@ -2315,7 +2316,8 @@
CompletionHandler<void(InteractionInformationAtPosition&&)> m_pendingSynchronousPositionInformationReply;
std::optional<std::pair<TransactionID, double>> m_lastLayerTreeTransactionIdAndPageScaleBeforeScalingPage;
-#endif
+ bool m_sendAutocorrectionContextAfterFocusingElement { false };
+#endif // PLATFORM(IOS_FAMILY)
WebCore::Timer m_layerVolatilityTimer;
Seconds m_layerVolatilityTimerInterval;
Modified: trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm (290793 => 290794)
--- trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm 2022-03-03 21:51:04 UTC (rev 290793)
+++ trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm 2022-03-03 22:07:56 UTC (rev 290794)
@@ -688,7 +688,7 @@
if (!editor.hasComposition() && frame->selection().selection().isNone())
return;
- didChangeSelection();
+ didChangeSelection(frame.get());
}
static void dispatchSyntheticMouseMove(Frame& mainFrame, const WebCore::FloatPoint& location, OptionSet<WebEvent::Modifier> modifiers, WebCore::PointerID pointerId = WebCore::mousePointerID)
@@ -1262,17 +1262,6 @@
m_focusedElement->blur();
}
-void WebPage::setIsShowingInputViewForFocusedElement(bool showingInputView)
-{
- if (m_isShowingInputViewForFocusedElement == showingInputView)
- return;
-
- m_isShowingInputViewForFocusedElement = showingInputView;
-
- if (showingInputView)
- preemptivelySendAutocorrectionContext();
-}
-
void WebPage::setFocusedElementValue(const WebCore::ElementContext& context, const String& value)
{
RefPtr<Element> element = elementForContext(context);