Changeset 241180 in webkit


Ignore:
Timestamp:
Feb 7, 2019 5:45:42 PM (5 years ago)
Author:
Wenson Hsieh
Message:

[iOS] [WK2] Modernize code for applying autocorrection
https://bugs.webkit.org/show_bug.cgi?id=194397

Reviewed by Tim Horton.

  • UIProcess/ios/WKContentViewInteraction.h:
  • UIProcess/ios/WKContentViewInteraction.mm:

(-[WKContentView requestAutocorrectionRectsForString:withCompletionHandler:]):
(-[WKContentView applyAutocorrection:toString:withCompletionHandler:]):

Use BlockPtr instead of temporarily storing the completion handler.

  • WebProcess/WebPage/WebPage.h:
  • WebProcess/WebPage/WebPage.messages.in:

Change a LegacySync to Delayed.

  • WebProcess/WebPage/ios/WebPageIOS.mm:

(WebKit::WebPage::applyAutocorrection):
(WebKit::WebPage::syncApplyAutocorrection):
(WebKit::WebPage::applyAutocorrectionInternal):

Location:
trunk/Source/WebKit
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r241171 r241180  
     12019-02-07  Wenson Hsieh  <wenson_hsieh@apple.com>
     2
     3        [iOS] [WK2] Modernize code for applying autocorrection
     4        https://bugs.webkit.org/show_bug.cgi?id=194397
     5
     6        Reviewed by Tim Horton.
     7
     8        * UIProcess/ios/WKContentViewInteraction.h:
     9        * UIProcess/ios/WKContentViewInteraction.mm:
     10        (-[WKContentView requestAutocorrectionRectsForString:withCompletionHandler:]):
     11        (-[WKContentView applyAutocorrection:toString:withCompletionHandler:]):
     12
     13        Use BlockPtr instead of temporarily storing the completion handler.
     14
     15        * WebProcess/WebPage/WebPage.h:
     16        * WebProcess/WebPage/WebPage.messages.in:
     17
     18        Change a LegacySync to Delayed.
     19
     20        * WebProcess/WebPage/ios/WebPageIOS.mm:
     21        (WebKit::WebPage::applyAutocorrection):
     22        (WebKit::WebPage::syncApplyAutocorrection):
     23        (WebKit::WebPage::applyAutocorrectionInternal):
     24
    1252019-02-07  Wenson Hsieh  <wenson_hsieh@apple.com>
    226
  • trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h

    r241146 r241180  
    176176    CGRect textFirstRect;
    177177    CGRect textLastRect;
    178     UIWKAutocorrectionCompletionHandler autocorrectionHandler;
    179178};
    180179
  • trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm

    r241171 r241180  
    32183218{
    32193219    if (!input || ![input length]) {
    3220         completionHandler(nil);
    3221         return;
    3222     }
    3223 
    3224     RetainPtr<WKContentView> view = self;
    3225     _autocorrectionData.autocorrectionHandler = [completionHandler copy];
    3226     _page->requestAutocorrectionData(input, [view](const Vector<WebCore::FloatRect>& rects, const String& fontName, double fontSize, uint64_t traits, WebKit::CallbackBase::Error) {
     3220        if (completionHandler)
     3221            completionHandler(nil);
     3222        return;
     3223    }
     3224
     3225    _page->requestAutocorrectionData(input, [view = retainPtr(self), completion = makeBlockPtr(completionHandler)](auto& rects, auto& fontName, double fontSize, uint64_t traits, auto) {
    32273226        CGRect firstRect = CGRectZero;
    32283227        CGRect lastRect = CGRectZero;
     
    32383237        view->_autocorrectionData.textLastRect = lastRect;
    32393238
    3240         view->_autocorrectionData.autocorrectionHandler(rects.size() ? [WKAutocorrectionRects autocorrectionRectsWithRects:firstRect lastRect:lastRect] : nil);
    3241         [view->_autocorrectionData.autocorrectionHandler release];
    3242         view->_autocorrectionData.autocorrectionHandler = nil;
     3239        if (completion)
     3240            completion(rects.size() ? [WKAutocorrectionRects autocorrectionRectsWithRects:firstRect lastRect:lastRect] : nil);
    32433241    });
    32443242}
     
    33823380
    33833381    if (useSyncRequest) {
    3384         completionHandler(_page->applyAutocorrection(correction, input) ? [WKAutocorrectionRects autocorrectionRectsWithRects:_autocorrectionData.textFirstRect lastRect:_autocorrectionData.textLastRect] : nil);
    3385         return;
    3386     }
    3387     _autocorrectionData.autocorrectionHandler = [completionHandler copy];
    3388     RetainPtr<WKContentView> view = self;
    3389     _page->applyAutocorrection(correction, input, [view](const String& string, WebKit::CallbackBase::Error error) {
    3390         view->_autocorrectionData.autocorrectionHandler(!string.isNull() ? [WKAutocorrectionRects autocorrectionRectsWithRects:view->_autocorrectionData.textFirstRect lastRect:view->_autocorrectionData.textLastRect] : nil);
    3391         [view->_autocorrectionData.autocorrectionHandler release];
    3392         view->_autocorrectionData.autocorrectionHandler = nil;
     3382        if (completionHandler)
     3383            completionHandler(_page->applyAutocorrection(correction, input) ? [WKAutocorrectionRects autocorrectionRectsWithRects:_autocorrectionData.textFirstRect lastRect:_autocorrectionData.textLastRect] : nil);
     3384        return;
     3385    }
     3386
     3387    _page->applyAutocorrection(correction, input, [view = retainPtr(self), completion = makeBlockPtr(completionHandler)](auto& string, auto error) {
     3388        if (completion)
     3389            completion(!string.isNull() ? [WKAutocorrectionRects autocorrectionRectsWithRects:view->_autocorrectionData.textFirstRect lastRect:view->_autocorrectionData.textLastRect] : nil);
    33933390    });
    33943391}
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.h

    r241146 r241180  
    647647    void requestAutocorrectionData(const String& textForAutocorrection, CallbackID);
    648648    void applyAutocorrection(const String& correction, const String& originalText, CallbackID);
    649     void syncApplyAutocorrection(const String& correction, const String& originalText, bool& correctionApplied);
     649    void syncApplyAutocorrection(const String& correction, const String& originalText, CompletionHandler<void(bool)>&&);
    650650    void requestAutocorrectionContext(CallbackID);
    651651    void autocorrectionContextSync(CompletionHandler<void(WebAutocorrectionContext&&)>&&);
     
    11921192    InteractionInformationAtPosition positionInformation(const InteractionInformationRequest&);
    11931193    WebAutocorrectionContext autocorrectionContext();
     1194    bool applyAutocorrectionInternal(const String& correction, const String& originalText);
    11941195#endif
    11951196
  • trunk/Source/WebKit/WebProcess/WebPage/WebPage.messages.in

    r241146 r241180  
    7878    RequestAutocorrectionData(String textForAutocorrection, WebKit::CallbackID callbackID)
    7979    ApplyAutocorrection(String correction, String originalText, WebKit::CallbackID callbackID)
    80     SyncApplyAutocorrection(String correction, String originalText) -> (bool autocorrectionApplied) LegacySync
     80    SyncApplyAutocorrection(String correction, String originalText) -> (bool autocorrectionApplied) Delayed
    8181    RequestAutocorrectionContext(WebKit::CallbackID callbackID)
    8282    AutocorrectionContextSync() -> (struct WebKit::WebAutocorrectionContext context) Delayed
  • trunk/Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm

    r241146 r241180  
    18901890void WebPage::applyAutocorrection(const String& correction, const String& originalText, CallbackID callbackID)
    18911891{
    1892     bool correctionApplied;
    1893     syncApplyAutocorrection(correction, originalText, correctionApplied);
    1894     send(Messages::WebPageProxy::StringCallback(correctionApplied ? correction : String(), callbackID));
     1892    send(Messages::WebPageProxy::StringCallback(applyAutocorrectionInternal(correction, originalText) ? correction : String(), callbackID));
    18951893}
    18961894
     
    19131911}
    19141912
    1915 void WebPage::syncApplyAutocorrection(const String& correction, const String& originalText, bool& correctionApplied)
    1916 {
    1917     correctionApplied = false;
    1918 
    1919     Frame& frame = m_page->focusController().focusedOrMainFrame();
     1913void WebPage::syncApplyAutocorrection(const String& correction, const String& originalText, CompletionHandler<void(bool)>&& reply)
     1914{
     1915    reply(applyAutocorrectionInternal(correction, originalText));
     1916}
     1917
     1918bool WebPage::applyAutocorrectionInternal(const String& correction, const String& originalText)
     1919{
     1920    auto& frame = m_page->focusController().focusedOrMainFrame();
    19201921    if (!frame.selection().isCaretOrRange())
    1921         return;
     1922        return false;
    19221923
    19231924    RefPtr<Range> range;
     
    19521953        // Range selection.
    19531954        range = frame.selection().toNormalizedRange();
    1954         if (!range) {
    1955             correctionApplied = false;
    1956             return;
    1957         }
     1955        if (!range)
     1956            return false;
     1957
    19581958        textForRange = plainTextReplacingNoBreakSpace(range.get());
    19591959    }
    19601960
    1961     if (textForRange != originalText) {
    1962         correctionApplied = false;
    1963         return;
    1964     }
     1961    if (textForRange != originalText)
     1962        return false;
    19651963   
    19661964    // Correctly determine affinity, using logic currently only present in VisiblePosition
     
    19741972    else
    19751973        frame.editor().deleteWithDirection(DirectionBackward, CharacterGranularity, false, true);
    1976     correctionApplied = true;
     1974    return true;
    19771975}
    19781976
Note: See TracChangeset for help on using the changeset viewer.