1/*
2 * Copyright (C) 2010-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "ShareableBitmap.h"
29#include "WebFindOptions.h"
30#include <WebCore/FindOptions.h>
31#include <WebCore/IntRect.h>
32#include <WebCore/PageOverlay.h>
33#include <wtf/Forward.h>
34#include <wtf/Noncopyable.h>
35#include <wtf/Vector.h>
36
37#if PLATFORM(IOS_FAMILY)
38#include "FindIndicatorOverlayClientIOS.h"
39#endif
40
41namespace WebCore {
42class Frame;
43class Range;
44enum class DidWrap : bool;
45}
46
47namespace WebKit {
48
49class WebPage;
50
51class FindController : private WebCore::PageOverlay::Client {
52 WTF_MAKE_NONCOPYABLE(FindController);
53
54public:
55 explicit FindController(WebPage*);
56 virtual ~FindController();
57
58 void findString(const String&, FindOptions, unsigned maxMatchCount);
59 void findStringMatches(const String&, FindOptions, unsigned maxMatchCount);
60 void getImageForFindMatch(uint32_t matchIndex);
61 void selectFindMatch(uint32_t matchIndex);
62 void hideFindUI();
63 void countStringMatches(const String&, FindOptions, unsigned maxMatchCount);
64 uint32_t replaceMatches(const Vector<uint32_t>& matchIndices, const String& replacementText, bool selectionOnly);
65
66 void hideFindIndicator();
67 void showFindIndicatorInSelection();
68
69 bool isShowingOverlay() const { return m_isShowingFindIndicator && m_findPageOverlay; }
70
71 void deviceScaleFactorDidChange();
72 void didInvalidateDocumentMarkerRects();
73
74 void redraw();
75
76private:
77 // PageOverlay::Client.
78 void willMoveToPage(WebCore::PageOverlay&, WebCore::Page*) override;
79 void didMoveToPage(WebCore::PageOverlay&, WebCore::Page*) override;
80 bool mouseEvent(WebCore::PageOverlay&, const WebCore::PlatformMouseEvent&) override;
81 void drawRect(WebCore::PageOverlay&, WebCore::GraphicsContext&, const WebCore::IntRect& dirtyRect) override;
82
83 Vector<WebCore::FloatRect> rectsForTextMatchesInRect(WebCore::IntRect clipRect);
84 bool updateFindIndicator(WebCore::Frame& selectedFrame, bool isShowingOverlay, bool shouldAnimate = true);
85
86 void updateFindUIAfterPageScroll(bool found, const String&, FindOptions, unsigned maxMatchCount, WebCore::DidWrap);
87
88 void willFindString();
89 void didFindString();
90 void didFailToFindString();
91 void didHideFindIndicator();
92
93 unsigned findIndicatorRadius() const;
94 bool shouldHideFindIndicatorOnScroll() const;
95
96 WebPage* m_webPage;
97 WebCore::PageOverlay* m_findPageOverlay { nullptr };
98
99 // Whether the UI process is showing the find indicator. Note that this can be true even if
100 // the find indicator isn't showing, but it will never be false when it is showing.
101 bool m_isShowingFindIndicator { false };
102 WebCore::IntRect m_findIndicatorRect;
103 Vector<RefPtr<WebCore::Range>> m_findMatches;
104 // Index value is -1 if not found or if number of matches exceeds provided maximum.
105 int m_foundStringMatchIndex { -1 };
106
107#if PLATFORM(IOS_FAMILY)
108 RefPtr<WebCore::PageOverlay> m_findIndicatorOverlay;
109 std::unique_ptr<FindIndicatorOverlayClientIOS> m_findIndicatorOverlayClient;
110#endif
111};
112
113WebCore::FindOptions core(FindOptions);
114
115} // namespace WebKit
116