Title: [230814] trunk/Source/WebKit
Revision
230814
Author
aes...@apple.com
Date
2018-04-19 12:33:59 -0700 (Thu, 19 Apr 2018)

Log Message

[iOS] Implement find-in-page in the new WKPDFView
https://bugs.webkit.org/show_bug.cgi?id=184654
<rdar://problem/39331654>

Reviewed by Tim Horton.

This is theoretically covered by existing WKPDFView API tests, but that's currently blocked
by <rdar://problem/39475542>.

* UIProcess/ios/WKPDFView.mm:
(stringCompareOptions):

Mapped _WKFindOptions to NSStringCompareOptions.

(-[WKPDFView _resetFind]):

Cancelled an in-progress search and reset the search state.

(-[WKPDFView _findString:withOptions:maxCount:completion:]):

Stored the completion block, find string, and max count, then called
-[PDFHostViewController findString:withOptions:].

(-[WKPDFView web_countStringMatches:options:maxCount:]):

Called -_findString:withOptions:maxCount:completion: with a completion block that calls
FindClient::didCountStringMatches() with _findStringCount.

(-[WKPDFView _computeFocusedSearchResultIndexWithOptions:didWrapAround:]):

Computed the focused search result index, taking _findStringCount and wrap-around into
account. There are two interesting cases to mention here:

1. We can't change focus while a search is in progress, because we can't properly handle
wrap-around without a _findStringCount. If a search is in progress, store the requested
focus change in _focusedSearchResultPendingOffset, which will be applied once the search
finishes.

2. If wrap-around is about to happen but the find options do not allow it, we need to call
FindClient::didFailToFindString(). Handle this by returning NO, which will tell
-_focusOnSearchResultWithOptions: to call didFailToFindString() if a search is not in
progress.

(-[WKPDFView _focusOnSearchResultWithOptions:]):

If -_computeFocusedSearchResultIndexWithOptions:didWrapAround: failed while a search is in
progress, just return early. Otherwise, call FindClient::didFailToFindString(). If
computing the index did not fail, call -[PDFHostViewController focusOnSearchResultAtIndex:]
to change focus and then call FindClient::didFindString() to inform the client.

(-[WKPDFView web_findString:options:maxCount:]):

If the find string is equal to _findString, change focus. Otherwise, start a new search.

(-[WKPDFView web_hideFindUI]):

Called -_resetFind.

(-[WKPDFView pdfHostViewController:findStringUpdate:done:]):

Stored the count in _findStringCount and called _findCompletion once the search is done.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (230813 => 230814)


--- trunk/Source/WebKit/ChangeLog	2018-04-19 19:33:03 UTC (rev 230813)
+++ trunk/Source/WebKit/ChangeLog	2018-04-19 19:33:59 UTC (rev 230814)
@@ -1,3 +1,67 @@
+2018-04-19  Andy Estes  <aes...@apple.com>
+
+        [iOS] Implement find-in-page in the new WKPDFView
+        https://bugs.webkit.org/show_bug.cgi?id=184654
+        <rdar://problem/39331654>
+
+        Reviewed by Tim Horton.
+
+        This is theoretically covered by existing WKPDFView API tests, but that's currently blocked
+        by <rdar://problem/39475542>.
+
+        * UIProcess/ios/WKPDFView.mm:
+        (stringCompareOptions):
+
+        Mapped _WKFindOptions to NSStringCompareOptions.
+
+        (-[WKPDFView _resetFind]):
+
+        Cancelled an in-progress search and reset the search state.
+
+        (-[WKPDFView _findString:withOptions:maxCount:completion:]):
+
+        Stored the completion block, find string, and max count, then called
+        -[PDFHostViewController findString:withOptions:].
+
+        (-[WKPDFView web_countStringMatches:options:maxCount:]):
+
+        Called -_findString:withOptions:maxCount:completion: with a completion block that calls
+        FindClient::didCountStringMatches() with _findStringCount.
+
+        (-[WKPDFView _computeFocusedSearchResultIndexWithOptions:didWrapAround:]):
+
+        Computed the focused search result index, taking _findStringCount and wrap-around into
+        account. There are two interesting cases to mention here:
+
+        1. We can't change focus while a search is in progress, because we can't properly handle
+        wrap-around without a _findStringCount. If a search is in progress, store the requested
+        focus change in _focusedSearchResultPendingOffset, which will be applied once the search
+        finishes.
+
+        2. If wrap-around is about to happen but the find options do not allow it, we need to call
+        FindClient::didFailToFindString(). Handle this by returning NO, which will tell
+        -_focusOnSearchResultWithOptions: to call didFailToFindString() if a search is not in
+        progress.
+
+        (-[WKPDFView _focusOnSearchResultWithOptions:]):
+
+        If -_computeFocusedSearchResultIndexWithOptions:didWrapAround: failed while a search is in
+        progress, just return early. Otherwise, call FindClient::didFailToFindString(). If
+        computing the index did not fail, call -[PDFHostViewController focusOnSearchResultAtIndex:]
+        to change focus and then call FindClient::didFindString() to inform the client.
+
+        (-[WKPDFView web_findString:options:maxCount:]):
+
+        If the find string is equal to _findString, change focus. Otherwise, start a new search.
+
+        (-[WKPDFView web_hideFindUI]):
+
+        Called -_resetFind.
+
+        (-[WKPDFView pdfHostViewController:findStringUpdate:done:]):
+
+        Stored the count in _findStringCount and called _findCompletion once the search is done.
+
 2018-04-17  Filip Pizlo  <fpi...@apple.com>
 
         The InternalFunction hierarchy should be in IsoSubspaces

Modified: trunk/Source/WebKit/UIProcess/ios/WKPDFView.mm (230813 => 230814)


--- trunk/Source/WebKit/UIProcess/ios/WKPDFView.mm	2018-04-19 19:33:03 UTC (rev 230813)
+++ trunk/Source/WebKit/UIProcess/ios/WKPDFView.mm	2018-04-19 19:33:59 UTC (rev 230814)
@@ -28,11 +28,13 @@
 
 #if ENABLE(WKPDFVIEW)
 
+#import "FindClient.h"
 #import "WKWebViewInternal.h"
 #import "WeakObjCPtr.h"
 #import "WebPageProxy.h"
 #import "_WKWebViewPrintFormatterInternal.h"
 #import <PDFKit/PDFHostViewController.h>
+#import <wtf/BlockPtr.h>
 #import <wtf/MainThread.h>
 #import <wtf/RetainPtr.h>
 
@@ -40,12 +42,18 @@
 @end
 
 @implementation WKPDFView {
-    CGSize _overlaidAccessoryViewsInset;
     RetainPtr<NSData> _data;
-    RetainPtr<NSString> _suggestedFilename;
+    BlockPtr<void()> _findCompletion;
+    RetainPtr<NSString> _findString;
+    NSUInteger _findStringCount;
+    NSUInteger _findStringMaxCount;
+    RetainPtr<UIView> _fixedOverlayView;
+    std::optional<NSUInteger> _focusedSearchResultIndex;
+    NSInteger _focusedSearchResultPendingOffset;
     RetainPtr<PDFHostViewController> _hostViewController;
-    RetainPtr<UIView> _fixedOverlayView;
+    CGSize _overlaidAccessoryViewsInset;
     RetainPtr<UIView> _pageNumberIndicator;
+    RetainPtr<NSString> _suggestedFilename;
     WebKit::WeakObjCPtr<WKWebView> _webView;
 }
 
@@ -178,19 +186,121 @@
         [self _scrollToURLFragment:[_webView URL].fragment];
 }
 
+static NSStringCompareOptions stringCompareOptions(_WKFindOptions findOptions)
+{
+    NSStringCompareOptions compareOptions = 0;
+    if (findOptions & _WKFindOptionsBackwards)
+        compareOptions |= NSBackwardsSearch;
+    if (findOptions & _WKFindOptionsCaseInsensitive)
+        compareOptions |= NSCaseInsensitiveSearch;
+    return compareOptions;
+}
+
+- (void)_resetFind
+{
+    if (_findCompletion)
+        [_hostViewController cancelFindString];
+
+    _findCompletion = nil;
+    _findString = nil;
+    _findStringCount = 0;
+    _findStringMaxCount = 0;
+    _focusedSearchResultIndex = std::nullopt;
+    _focusedSearchResultPendingOffset = 0;
+}
+
+- (void)_findString:(NSString *)string withOptions:(_WKFindOptions)options maxCount:(NSUInteger)maxCount completion:(void(^)())completion
+{
+    [self _resetFind];
+
+    _findCompletion = completion;
+    _findString = adoptNS([string copy]);
+    _findStringMaxCount = maxCount;
+    [_hostViewController findString:_findString.get() withOptions:stringCompareOptions(options)];
+}
+
 - (void)web_countStringMatches:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
 {
-    // FIXME: Implement find-in-page.
+    [self _findString:string withOptions:options maxCount:maxCount completion:^{
+        ASSERT([_findString isEqualToString:string]);
+        if (auto page = [_webView _page])
+            page->findClient().didCountStringMatches(page, _findString.get(), _findStringCount);
+    }];
 }
 
+- (BOOL)_computeFocusedSearchResultIndexWithOptions:(_WKFindOptions)options didWrapAround:(BOOL *)didWrapAround
+{
+    BOOL isBackwards = options & _WKFindOptionsBackwards;
+    NSInteger singleOffset = isBackwards ? -1 : 1;
+
+    if (_findCompletion) {
+        ASSERT(!_focusedSearchResultIndex);
+        _focusedSearchResultPendingOffset += singleOffset;
+        return NO;
+    }
+
+    if (!_findStringCount)
+        return NO;
+
+    NSInteger newIndex;
+    if (_focusedSearchResultIndex) {
+        ASSERT(!_focusedSearchResultPendingOffset);
+        newIndex = *_focusedSearchResultIndex + singleOffset;
+    } else {
+        newIndex = isBackwards ? _findStringCount - 1 : 0;
+        newIndex += std::exchange(_focusedSearchResultPendingOffset, 0);
+    }
+
+    if (newIndex < 0 || static_cast<NSUInteger>(newIndex) >= _findStringCount) {
+        if (!(options & _WKFindOptionsWrapAround))
+            return NO;
+
+        NSUInteger wrappedIndex = std::abs(newIndex) % _findStringCount;
+        if (newIndex < 0)
+            wrappedIndex = _findStringCount - wrappedIndex;
+        newIndex = wrappedIndex;
+        *didWrapAround = YES;
+    }
+
+    _focusedSearchResultIndex = newIndex;
+    ASSERT(*_focusedSearchResultIndex < _findStringCount);
+    return YES;
+}
+
+- (void)_focusOnSearchResultWithOptions:(_WKFindOptions)options
+{
+    auto page = [_webView _page];
+    if (!page)
+        return;
+
+    BOOL didWrapAround = NO;
+    if (![self _computeFocusedSearchResultIndexWithOptions:options didWrapAround:&didWrapAround]) {
+        if (!_findCompletion)
+            page->findClient().didFailToFindString(page, _findString.get());
+        return;
+    }
+
+    auto focusedIndex = *_focusedSearchResultIndex;
+    [_hostViewController focusOnSearchResultAtIndex:focusedIndex];
+    page->findClient().didFindString(page, _findString.get(), { }, _findStringCount, focusedIndex, didWrapAround);
+}
+
 - (void)web_findString:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
 {
-    // FIXME: Implement find-in-page.
+    if ([_findString isEqualToString:string]) {
+        [self _focusOnSearchResultWithOptions:options];
+        return;
+    }
+
+    [self _findString:string withOptions:options maxCount:maxCount completion:^{
+        ASSERT([_findString isEqualToString:string]);
+        [self _focusOnSearchResultWithOptions:options];
+    }];
 }
 
 - (void)web_hideFindUI
 {
-    // FIXME: Implement find-in-page.
+    [self _resetFind];
 }
 
 - (UIView *)web_contentView
@@ -240,6 +350,18 @@
     [self _scrollToURLFragment:[_webView URL].fragment];
 }
 
+- (void)pdfHostViewController:(PDFHostViewController *)controller findStringUpdate:(NSUInteger)numFound done:(BOOL)done
+{
+    // FIXME: We should stop searching once numFound exceeds _findStringMaxCount, but PDFKit doesn't
+    // allow us to stop the search without also clearing the search highlights. See <rdar://problem/39546973>.
+    if (!done)
+        return;
+
+    _findStringCount = numFound;
+    if (auto findCompletion = std::exchange(_findCompletion, nil))
+        findCompletion();
+}
+
 - (void)pdfHostViewController:(PDFHostViewController *)controller goToURL:(NSURL *)url
 {
     WKWebView *webView = _webView.getAutoreleased();
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to