Home | History | Annotate | Download | only in web
      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 
     34 #include "core/editing/FindOptions.h"
     35 #include "platform/geometry/FloatRect.h"
     36 #include "platform/heap/Handle.h"
     37 #include "public/platform/WebFloatPoint.h"
     38 #include "public/platform/WebFloatRect.h"
     39 #include "public/platform/WebRect.h"
     40 #include "public/web/WebFindOptions.h"
     41 #include "wtf/PassOwnPtr.h"
     42 #include "wtf/PassRefPtr.h"
     43 #include "wtf/Vector.h"
     44 #include "wtf/text/WTFString.h"
     45 
     46 namespace blink {
     47 
     48 class Range;
     49 class WebLocalFrameImpl;
     50 
     51 template <typename T> class WebVector;
     52 
     53 class TextFinder {
     54 public:
     55     static PassOwnPtr<TextFinder> create(WebLocalFrameImpl& ownerFrame);
     56 
     57     bool find(
     58         int identifier, const WebString& searchText, const WebFindOptions&,
     59         bool wrapWithinFrame, WebRect* selectionRect);
     60     void stopFindingAndClearSelection();
     61     void scopeStringMatches(
     62         int identifier, const WebString& searchText, const WebFindOptions&,
     63         bool reset);
     64     void cancelPendingScopingEffort();
     65     void increaseMatchCount(int identifier, int count);
     66     void resetMatchCount();
     67     int findMatchMarkersVersion() const { return m_findMatchMarkersVersion; }
     68     WebFloatRect activeFindMatchRect();
     69     void findMatchRects(WebVector<WebFloatRect>&);
     70     int selectNearestFindMatch(const WebFloatPoint&, WebRect* selectionRect);
     71 
     72     // Returns which frame has an active match. This function should only be
     73     // called on the main frame, as it is the only frame keeping track. Returned
     74     // value can be 0 if no frame has an active match.
     75     WebLocalFrameImpl* activeMatchFrame() const { return m_currentActiveMatchFrame; }
     76 
     77     // Returns the active match in the current frame. Could be a null range if
     78     // the local frame has no active match.
     79     Range* activeMatch() const { return m_activeMatch.get(); }
     80 
     81     void flushCurrentScoping();
     82 
     83     void resetActiveMatch() { m_activeMatch = nullptr; }
     84 
     85     int totalMatchCount() const { return m_totalMatchCount; }
     86     bool scopingInProgress() const { return m_scopingInProgress; }
     87     void increaseMarkerVersion() { ++m_findMatchMarkersVersion; }
     88 
     89     ~TextFinder();
     90 
     91     class FindMatch {
     92         ALLOW_ONLY_INLINE_ALLOCATION();
     93     public:
     94         RefPtrWillBeMember<Range> m_range;
     95 
     96         // 1-based index within this frame.
     97         int m_ordinal;
     98 
     99         // In find-in-page coordinates.
    100         // Lazily calculated by updateFindMatchRects.
    101         FloatRect m_rect;
    102 
    103         FindMatch(PassRefPtrWillBeRawPtr<Range>, int ordinal);
    104 
    105         void trace(Visitor*);
    106     };
    107 
    108 private:
    109     class DeferredScopeStringMatches;
    110     friend class DeferredScopeStringMatches;
    111 
    112     explicit TextFinder(WebLocalFrameImpl& ownerFrame);
    113 
    114     // Notifies the delegate about a new selection rect.
    115     void reportFindInPageSelection(
    116         const WebRect& selectionRect, int activeMatchOrdinal, int identifier);
    117 
    118     // Clear the find-in-page matches cache forcing rects to be fully
    119     // calculated again next time updateFindMatchRects is called.
    120     void clearFindMatchesCache();
    121 
    122     // Check if the activeMatchFrame still exists in the frame tree.
    123     bool isActiveMatchFrameValid() const;
    124 
    125     // Return the index in the find-in-page cache of the match closest to the
    126     // provided point in find-in-page coordinates, or -1 in case of error.
    127     // The squared distance to the closest match is returned in the distanceSquared parameter.
    128     int nearestFindMatch(const FloatPoint&, float& distanceSquared);
    129 
    130     // Select a find-in-page match marker in the current frame using a cache
    131     // match index returned by nearestFindMatch. Returns the ordinal of the new
    132     // selected match or -1 in case of error. Also provides the bounding box of
    133     // the marker in window coordinates if selectionRect is not null.
    134     int selectFindMatch(unsigned index, WebRect* selectionRect);
    135 
    136     // Compute and cache the rects for FindMatches if required.
    137     // Rects are automatically invalidated in case of content size changes,
    138     // propagating the invalidation to child frames.
    139     void updateFindMatchRects();
    140 
    141     // Append the find-in-page match rects of the current frame to the provided vector.
    142     void appendFindMatchRects(Vector<WebFloatRect>& frameRects);
    143 
    144     // Add a WebKit TextMatch-highlight marker to nodes in a range.
    145     void addMarker(Range*, bool activeMatch);
    146 
    147     // Sets the markers within a range as active or inactive.
    148     void setMarkerActive(Range*, bool active);
    149 
    150     // Returns the ordinal of the first match in the frame specified. This
    151     // function enumerates the frames, starting with the main frame and up to (but
    152     // not including) the frame passed in as a parameter and counts how many
    153     // matches have been found.
    154     int ordinalOfFirstMatchForFrame(WebLocalFrameImpl*) const;
    155 
    156     // Determines whether the scoping effort is required for a particular frame.
    157     // It is not necessary if the frame is invisible, for example, or if this
    158     // is a repeat search that already returned nothing last time the same prefix
    159     // was searched.
    160     bool shouldScopeMatches(const WTF::String& searchText);
    161 
    162     // Removes the current frame from the global scoping effort and triggers any
    163     // updates if appropriate. This method does not mark the scoping operation
    164     // as finished.
    165     void flushCurrentScopingEffort(int identifier);
    166 
    167     // Finishes the current scoping effort and triggers any updates if appropriate.
    168     void finishCurrentScopingEffort(int identifier);
    169 
    170     // Queue up a deferred call to scopeStringMatches.
    171     void scopeStringMatchesSoon(
    172         int identifier, const WebString& searchText, const WebFindOptions&,
    173         bool reset);
    174 
    175     // Called by a DeferredScopeStringMatches instance.
    176     void callScopeStringMatches(
    177         DeferredScopeStringMatches*, int identifier, const WebString& searchText,
    178         const WebFindOptions&, bool reset);
    179 
    180     // Determines whether to invalidate the content area and scrollbar.
    181     void invalidateIfNecessary();
    182 
    183     // Sets the markers within a current match range as active or inactive.
    184     void setMatchMarkerActive(bool);
    185 
    186     void decrementFramesScopingCount(int identifier);
    187 
    188     // Returns the ordinal of the first match in the owner frame.
    189     int ordinalOfFirstMatch() const;
    190 
    191     WebLocalFrameImpl& m_ownerFrame;
    192 
    193     // A way for the main frame to keep track of which frame has an active
    194     // match. Should be 0 for all other frames.
    195     WebLocalFrameImpl* m_currentActiveMatchFrame;
    196 
    197     // The range of the active match for the current frame.
    198     RefPtrWillBePersistent<Range> m_activeMatch;
    199 
    200     // The index of the active match for the current frame.
    201     int m_activeMatchIndexInCurrentFrame;
    202 
    203     // The scoping effort can time out and we need to keep track of where we
    204     // ended our last search so we can continue from where we left of.
    205     //
    206     // This range is collapsed to the end position of the last successful
    207     // search; the new search should start from this position.
    208     RefPtrWillBePersistent<Range> m_resumeScopingFromRange;
    209 
    210     // Keeps track of the last string this frame searched for. This is used for
    211     // short-circuiting searches in the following scenarios: When a frame has
    212     // been searched and returned 0 results, we don't need to search that frame
    213     // again if the user is just adding to the search (making it more specific).
    214     WTF::String m_lastSearchString;
    215 
    216     // Keeps track of how many matches this frame has found so far, so that we
    217     // don't loose count between scoping efforts, and is also used (in conjunction
    218     // with m_lastSearchString) to figure out if we need to search the frame again.
    219     int m_lastMatchCount;
    220 
    221     // This variable keeps a cumulative total of matches found so far for ALL the
    222     // frames on the page, and is only incremented by calling IncreaseMatchCount
    223     // (on the main frame only). It should be -1 for all other frames.
    224     int m_totalMatchCount;
    225 
    226     // This variable keeps a cumulative total of how many frames are currently
    227     // scoping, and is incremented/decremented on the main frame only.
    228     // It should be -1 for all other frames.
    229     int m_framesScopingCount;
    230 
    231     // Identifier of the latest find-in-page request. Required to be stored in
    232     // the frame in order to reply if required in case the frame is detached.
    233     int m_findRequestIdentifier;
    234 
    235     // Keeps track of when the scoping effort should next invalidate the scrollbar
    236     // and the frame area.
    237     int m_nextInvalidateAfter;
    238 
    239     // A list of all of the pending calls to scopeStringMatches.
    240     Vector<DeferredScopeStringMatches*> m_deferredScopingWork;
    241 
    242     // Version number incremented on the main frame only whenever the document
    243     // find-in-page match markers change. It should be 0 for all other frames.
    244     int m_findMatchMarkersVersion;
    245 
    246     // Local cache of the find match markers currently displayed for this frame.
    247     WillBePersistentHeapVector<FindMatch> m_findMatchesCache;
    248 
    249     // Contents size when find-in-page match rects were last computed for this
    250     // frame's cache.
    251     IntSize m_contentsSizeForCurrentFindMatchRects;
    252 
    253     // This flag is used by the scoping effort to determine if we need to figure
    254     // out which rectangle is the active match. Once we find the active
    255     // rectangle we clear this flag.
    256     bool m_locatingActiveRect;
    257 
    258     // Keeps track of whether there is an scoping effort ongoing in the frame.
    259     bool m_scopingInProgress;
    260 
    261     // Keeps track of whether the last find request completed its scoping effort
    262     // without finding any matches in this frame.
    263     bool m_lastFindRequestCompletedWithNoMatches;
    264 
    265     // Determines if the rects in the find-in-page matches cache of this frame
    266     // are invalid and should be recomputed.
    267     bool m_findMatchRectsAreValid;
    268 };
    269 
    270 } // namespace blink
    271 
    272 WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(blink::TextFinder::FindMatch);
    273 
    274 #endif
    275