<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[230814] trunk/Source/WebKit</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/230814">230814</a></dd>
<dt>Author</dt> <dd>aestes@apple.com</dd>
<dt>Date</dt> <dd>2018-04-19 12:33:59 -0700 (Thu, 19 Apr 2018)</dd>
</dl>

<h3>Log Message</h3>
<pre>[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.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitUIProcessiosWKPDFViewmm">trunk/Source/WebKit/UIProcess/ios/WKPDFView.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (230813 => 230814)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,67 @@
</span><ins>+2018-04-19  Andy Estes  <aestes@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.
+
</ins><span class="cx"> 2018-04-17  Filip Pizlo  <fpizlo@apple.com>
</span><span class="cx"> 
</span><span class="cx">         The InternalFunction hierarchy should be in IsoSubspaces
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessiosWKPDFViewmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/ios/WKPDFView.mm (230813 => 230814)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -28,11 +28,13 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WKPDFVIEW)
</span><span class="cx"> 
</span><ins>+#import "FindClient.h"
</ins><span class="cx"> #import "WKWebViewInternal.h"
</span><span class="cx"> #import "WeakObjCPtr.h"
</span><span class="cx"> #import "WebPageProxy.h"
</span><span class="cx"> #import "_WKWebViewPrintFormatterInternal.h"
</span><span class="cx"> #import <PDFKit/PDFHostViewController.h>
</span><ins>+#import <wtf/BlockPtr.h>
</ins><span class="cx"> #import <wtf/MainThread.h>
</span><span class="cx"> #import <wtf/RetainPtr.h>
</span><span class="cx"> 
</span><span class="lines">@@ -40,12 +42,18 @@
</span><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> @implementation WKPDFView {
</span><del>-    CGSize _overlaidAccessoryViewsInset;
</del><span class="cx">     RetainPtr<NSData> _data;
</span><del>-    RetainPtr<NSString> _suggestedFilename;
</del><ins>+    BlockPtr<void()> _findCompletion;
+    RetainPtr<NSString> _findString;
+    NSUInteger _findStringCount;
+    NSUInteger _findStringMaxCount;
+    RetainPtr<UIView> _fixedOverlayView;
+    std::optional<NSUInteger> _focusedSearchResultIndex;
+    NSInteger _focusedSearchResultPendingOffset;
</ins><span class="cx">     RetainPtr<PDFHostViewController> _hostViewController;
</span><del>-    RetainPtr<UIView> _fixedOverlayView;
</del><ins>+    CGSize _overlaidAccessoryViewsInset;
</ins><span class="cx">     RetainPtr<UIView> _pageNumberIndicator;
</span><ins>+    RetainPtr<NSString> _suggestedFilename;
</ins><span class="cx">     WebKit::WeakObjCPtr<WKWebView> _webView;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -178,19 +186,121 @@
</span><span class="cx">         [self _scrollToURLFragment:[_webView URL].fragment];
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+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)];
+}
+
</ins><span class="cx"> - (void)web_countStringMatches:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
</span><span class="cx"> {
</span><del>-    // FIXME: Implement find-in-page.
</del><ins>+    [self _findString:string withOptions:options maxCount:maxCount completion:^{
+        ASSERT([_findString isEqualToString:string]);
+        if (auto page = [_webView _page])
+            page->findClient().didCountStringMatches(page, _findString.get(), _findStringCount);
+    }];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (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);
+}
+
</ins><span class="cx"> - (void)web_findString:(NSString *)string options:(_WKFindOptions)options maxCount:(NSUInteger)maxCount
</span><span class="cx"> {
</span><del>-    // FIXME: Implement find-in-page.
</del><ins>+    if ([_findString isEqualToString:string]) {
+        [self _focusOnSearchResultWithOptions:options];
+        return;
+    }
+
+    [self _findString:string withOptions:options maxCount:maxCount completion:^{
+        ASSERT([_findString isEqualToString:string]);
+        [self _focusOnSearchResultWithOptions:options];
+    }];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (void)web_hideFindUI
</span><span class="cx"> {
</span><del>-    // FIXME: Implement find-in-page.
</del><ins>+    [self _resetFind];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (UIView *)web_contentView
</span><span class="lines">@@ -240,6 +350,18 @@
</span><span class="cx">     [self _scrollToURLFragment:[_webView URL].fragment];
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (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();
+}
+
</ins><span class="cx"> - (void)pdfHostViewController:(PDFHostViewController *)controller goToURL:(NSURL *)url
</span><span class="cx"> {
</span><span class="cx">     WKWebView *webView = _webView.getAutoreleased();
</span></span></pre>
</div>
</div>

</body>
</html>