Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Google 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 are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef TextFinder_h |
| 32 | #define TextFinder_h |
| 33 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 34 | #include "WebFindOptions.h" |
| 35 | #include "core/editing/FindOptions.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 36 | #include "platform/geometry/FloatRect.h" |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 37 | #include "public/platform/WebFloatPoint.h" |
| 38 | #include "public/platform/WebFloatRect.h" |
| 39 | #include "public/platform/WebRect.h" |
| 40 | #include "wtf/PassOwnPtr.h" |
| 41 | #include "wtf/PassRefPtr.h" |
| 42 | #include "wtf/Vector.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 43 | #include "wtf/text/WTFString.h" |
| 44 | |
| 45 | namespace WebCore { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 46 | class Range; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | namespace blink { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 50 | class WebFrameImpl; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 51 | |
| 52 | template <typename T> class WebVector; |
| 53 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 54 | class TextFinder { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 55 | public: |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 56 | static PassOwnPtr<TextFinder> create(WebFrameImpl& ownerFrame); |
| 57 | |
| 58 | bool find( |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 59 | int identifier, const WebString& searchText, const WebFindOptions&, |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 60 | bool wrapWithinFrame, WebRect* selectionRect); |
| 61 | void stopFindingAndClearSelection(); |
| 62 | void scopeStringMatches( |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 63 | int identifier, const WebString& searchText, const WebFindOptions&, |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 64 | bool reset); |
| 65 | void cancelPendingScopingEffort(); |
| 66 | void increaseMatchCount(int identifier, int count); |
| 67 | void resetMatchCount(); |
| 68 | int findMatchMarkersVersion() const { return m_findMatchMarkersVersion; } |
| 69 | WebFloatRect activeFindMatchRect(); |
| 70 | void findMatchRects(WebVector<WebFloatRect>&); |
| 71 | int selectNearestFindMatch(const WebFloatPoint&, WebRect* selectionRect); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 72 | |
| 73 | // Returns which frame has an active match. This function should only be |
| 74 | // called on the main frame, as it is the only frame keeping track. Returned |
| 75 | // value can be 0 if no frame has an active match. |
| 76 | WebFrameImpl* activeMatchFrame() const { return m_currentActiveMatchFrame; } |
| 77 | |
| 78 | // Returns the active match in the current frame. Could be a null range if |
| 79 | // the local frame has no active match. |
| 80 | WebCore::Range* activeMatch() const { return m_activeMatch.get(); } |
| 81 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 82 | void flushCurrentScoping(); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 83 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 84 | void resetActiveMatch() { m_activeMatch = nullptr; } |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 85 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 86 | int totalMatchCount() const { return m_totalMatchCount; } |
| 87 | bool scopingInProgress() const { return m_scopingInProgress; } |
| 88 | void increaseMarkerVersion() { ++m_findMatchMarkersVersion; } |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 89 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 90 | ~TextFinder(); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | class DeferredScopeStringMatches; |
| 94 | friend class DeferredScopeStringMatches; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 95 | |
| 96 | struct FindMatch { |
| 97 | RefPtr<WebCore::Range> m_range; |
| 98 | |
| 99 | // 1-based index within this frame. |
| 100 | int m_ordinal; |
| 101 | |
| 102 | // In find-in-page coordinates. |
| 103 | // Lazily calculated by updateFindMatchRects. |
| 104 | WebCore::FloatRect m_rect; |
| 105 | |
| 106 | FindMatch(PassRefPtr<WebCore::Range>, int ordinal); |
| 107 | }; |
| 108 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 109 | explicit TextFinder(WebFrameImpl& ownerFrame); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 110 | |
| 111 | // Notifies the delegate about a new selection rect. |
| 112 | void reportFindInPageSelection( |
| 113 | const WebRect& selectionRect, int activeMatchOrdinal, int identifier); |
| 114 | |
| 115 | // Clear the find-in-page matches cache forcing rects to be fully |
| 116 | // calculated again next time updateFindMatchRects is called. |
| 117 | void clearFindMatchesCache(); |
| 118 | |
| 119 | // Check if the activeMatchFrame still exists in the frame tree. |
| 120 | bool isActiveMatchFrameValid() const; |
| 121 | |
| 122 | // Return the index in the find-in-page cache of the match closest to the |
| 123 | // provided point in find-in-page coordinates, or -1 in case of error. |
| 124 | // The squared distance to the closest match is returned in the distanceSquared parameter. |
| 125 | int nearestFindMatch(const WebCore::FloatPoint&, float& distanceSquared); |
| 126 | |
| 127 | // Select a find-in-page match marker in the current frame using a cache |
| 128 | // match index returned by nearestFindMatch. Returns the ordinal of the new |
| 129 | // selected match or -1 in case of error. Also provides the bounding box of |
| 130 | // the marker in window coordinates if selectionRect is not null. |
| 131 | int selectFindMatch(unsigned index, WebRect* selectionRect); |
| 132 | |
| 133 | // Compute and cache the rects for FindMatches if required. |
| 134 | // Rects are automatically invalidated in case of content size changes, |
| 135 | // propagating the invalidation to child frames. |
| 136 | void updateFindMatchRects(); |
| 137 | |
| 138 | // Append the find-in-page match rects of the current frame to the provided vector. |
| 139 | void appendFindMatchRects(Vector<WebFloatRect>& frameRects); |
| 140 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 141 | // Add a WebKit TextMatch-highlight marker to nodes in a range. |
| 142 | void addMarker(WebCore::Range*, bool activeMatch); |
| 143 | |
| 144 | // Sets the markers within a range as active or inactive. |
| 145 | void setMarkerActive(WebCore::Range*, bool active); |
| 146 | |
| 147 | // Returns the ordinal of the first match in the frame specified. This |
| 148 | // function enumerates the frames, starting with the main frame and up to (but |
| 149 | // not including) the frame passed in as a parameter and counts how many |
| 150 | // matches have been found. |
| 151 | int ordinalOfFirstMatchForFrame(WebFrameImpl*) const; |
| 152 | |
| 153 | // Determines whether the scoping effort is required for a particular frame. |
| 154 | // It is not necessary if the frame is invisible, for example, or if this |
| 155 | // is a repeat search that already returned nothing last time the same prefix |
| 156 | // was searched. |
| 157 | bool shouldScopeMatches(const WTF::String& searchText); |
| 158 | |
| 159 | // Removes the current frame from the global scoping effort and triggers any |
| 160 | // updates if appropriate. This method does not mark the scoping operation |
| 161 | // as finished. |
| 162 | void flushCurrentScopingEffort(int identifier); |
| 163 | |
| 164 | // Finishes the current scoping effort and triggers any updates if appropriate. |
| 165 | void finishCurrentScopingEffort(int identifier); |
| 166 | |
| 167 | // Queue up a deferred call to scopeStringMatches. |
| 168 | void scopeStringMatchesSoon( |
| 169 | int identifier, const WebString& searchText, const WebFindOptions&, |
| 170 | bool reset); |
| 171 | |
| 172 | // Called by a DeferredScopeStringMatches instance. |
| 173 | void callScopeStringMatches( |
| 174 | DeferredScopeStringMatches*, int identifier, const WebString& searchText, |
| 175 | const WebFindOptions&, bool reset); |
| 176 | |
| 177 | // Determines whether to invalidate the content area and scrollbar. |
| 178 | void invalidateIfNecessary(); |
| 179 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 180 | // Sets the markers within a current match range as active or inactive. |
| 181 | void setMatchMarkerActive(bool); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 182 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 183 | void decrementFramesScopingCount(int identifier); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 184 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 185 | // Returns the ordinal of the first match in the owner frame. |
| 186 | int ordinalOfFirstMatch() const; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 187 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 188 | WebFrameImpl& m_ownerFrame; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 189 | |
| 190 | // A way for the main frame to keep track of which frame has an active |
| 191 | // match. Should be 0 for all other frames. |
| 192 | WebFrameImpl* m_currentActiveMatchFrame; |
| 193 | |
| 194 | // The range of the active match for the current frame. |
| 195 | RefPtr<WebCore::Range> m_activeMatch; |
| 196 | |
| 197 | // The index of the active match for the current frame. |
| 198 | int m_activeMatchIndexInCurrentFrame; |
| 199 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 200 | // The scoping effort can time out and we need to keep track of where we |
| 201 | // ended our last search so we can continue from where we left of. |
| 202 | RefPtr<WebCore::Range> m_resumeScopingFromRange; |
| 203 | |
| 204 | // Keeps track of the last string this frame searched for. This is used for |
| 205 | // short-circuiting searches in the following scenarios: When a frame has |
| 206 | // been searched and returned 0 results, we don't need to search that frame |
| 207 | // again if the user is just adding to the search (making it more specific). |
| 208 | WTF::String m_lastSearchString; |
| 209 | |
| 210 | // Keeps track of how many matches this frame has found so far, so that we |
| 211 | // don't loose count between scoping efforts, and is also used (in conjunction |
| 212 | // with m_lastSearchString) to figure out if we need to search the frame again. |
| 213 | int m_lastMatchCount; |
| 214 | |
| 215 | // This variable keeps a cumulative total of matches found so far for ALL the |
| 216 | // frames on the page, and is only incremented by calling IncreaseMatchCount |
| 217 | // (on the main frame only). It should be -1 for all other frames. |
| 218 | int m_totalMatchCount; |
| 219 | |
| 220 | // This variable keeps a cumulative total of how many frames are currently |
| 221 | // scoping, and is incremented/decremented on the main frame only. |
| 222 | // It should be -1 for all other frames. |
| 223 | int m_framesScopingCount; |
| 224 | |
| 225 | // Identifier of the latest find-in-page request. Required to be stored in |
| 226 | // the frame in order to reply if required in case the frame is detached. |
| 227 | int m_findRequestIdentifier; |
| 228 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 229 | // Keeps track of when the scoping effort should next invalidate the scrollbar |
| 230 | // and the frame area. |
| 231 | int m_nextInvalidateAfter; |
| 232 | |
| 233 | // A list of all of the pending calls to scopeStringMatches. |
| 234 | Vector<DeferredScopeStringMatches*> m_deferredScopingWork; |
| 235 | |
| 236 | // Version number incremented on the main frame only whenever the document |
| 237 | // find-in-page match markers change. It should be 0 for all other frames. |
| 238 | int m_findMatchMarkersVersion; |
| 239 | |
| 240 | // Local cache of the find match markers currently displayed for this frame. |
| 241 | Vector<FindMatch> m_findMatchesCache; |
| 242 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 243 | // Contents size when find-in-page match rects were last computed for this |
| 244 | // frame's cache. |
| 245 | WebCore::IntSize m_contentsSizeForCurrentFindMatchRects; |
| 246 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 247 | // This flag is used by the scoping effort to determine if we need to figure |
| 248 | // out which rectangle is the active match. Once we find the active |
| 249 | // rectangle we clear this flag. |
| 250 | bool m_locatingActiveRect; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 251 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 252 | // Keeps track of whether there is an scoping effort ongoing in the frame. |
| 253 | bool m_scopingInProgress; |
| 254 | |
| 255 | // Keeps track of whether the last find request completed its scoping effort |
| 256 | // without finding any matches in this frame. |
| 257 | bool m_lastFindRequestCompletedWithNoMatches; |
| 258 | |
| 259 | // Determines if the rects in the find-in-page matches cache of this frame |
| 260 | // are invalid and should be recomputed. |
| 261 | bool m_findMatchRectsAreValid; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 264 | } // namespace blink |
| 265 | |
| 266 | #endif |