1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Dirk Mueller (mueller@kde.org)
5 *           (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * Library General Public License for more details.
19 *
20 * You should have received a copy of the GNU Library General Public License
21 * along with this library; see the file COPYING.LIB.  If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 *
25 */
26
27#ifndef DocumentMarkerController_h
28#define DocumentMarkerController_h
29
30#include "core/dom/DocumentMarker.h"
31#include "platform/geometry/IntRect.h"
32#include "platform/heap/Handle.h"
33#include "wtf/HashMap.h"
34#include "wtf/Vector.h"
35
36namespace blink {
37
38class LayoutPoint;
39class LayoutRect;
40class Node;
41class Position;
42class Range;
43class RenderedDocumentMarker;
44class Text;
45class TextIterator;
46
47class MarkerRemoverPredicate FINAL {
48public:
49    explicit MarkerRemoverPredicate(const Vector<String>& words);
50    bool operator()(const DocumentMarker&, const Text&) const;
51
52private:
53    Vector<String> m_words;
54};
55
56class DocumentMarkerController FINAL : public NoBaseWillBeGarbageCollected<DocumentMarkerController> {
57    WTF_MAKE_NONCOPYABLE(DocumentMarkerController); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
58    DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(DocumentMarkerController);
59public:
60
61    DocumentMarkerController();
62
63    void clear();
64    void addMarker(Range*, DocumentMarker::MarkerType, const String& description = emptyString(), uint32_t hash = 0);
65    void addMarker(const Position& start, const Position& end, DocumentMarker::MarkerType, const String& description = emptyString(), uint32_t hash = 0);
66    void addTextMatchMarker(const Range*, bool activeMatch);
67
68    void copyMarkers(Node* srcNode, unsigned startOffset, int length, Node* dstNode, int delta);
69    bool hasMarkers(Range*, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers());
70
71    void prepareForDestruction();
72    // When a marker partially overlaps with range, if removePartiallyOverlappingMarkers is true, we completely
73    // remove the marker. If the argument is false, we will adjust the span of the marker so that it retains
74    // the portion that is outside of the range.
75    enum RemovePartiallyOverlappingMarkerOrNot { DoNotRemovePartiallyOverlappingMarker, RemovePartiallyOverlappingMarker };
76    void removeMarkers(Range*, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers(), RemovePartiallyOverlappingMarkerOrNot = DoNotRemovePartiallyOverlappingMarker);
77    void removeMarkers(const Position& start, const Position& end, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers(), RemovePartiallyOverlappingMarkerOrNot = DoNotRemovePartiallyOverlappingMarker);
78    void removeMarkers(Node*, unsigned startOffset, int length, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers(),  RemovePartiallyOverlappingMarkerOrNot = DoNotRemovePartiallyOverlappingMarker);
79
80    void removeMarkers(DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers());
81    void removeMarkers(Node*, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers());
82    void removeMarkers(const MarkerRemoverPredicate& shouldRemoveMarker);
83    void repaintMarkers(DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers());
84    void invalidateRenderedRectsForMarkersInRect(const LayoutRect&);
85    void shiftMarkers(Node*, unsigned startOffset, int delta);
86    void setMarkersActive(Range*, bool);
87    void setMarkersActive(Node*, unsigned startOffset, unsigned endOffset, bool);
88
89    DocumentMarker* markerContainingPoint(const LayoutPoint&, DocumentMarker::MarkerType);
90    DocumentMarkerVector markersFor(Node*, DocumentMarker::MarkerTypes = DocumentMarker::AllMarkers());
91    DocumentMarkerVector markersInRange(Range*, DocumentMarker::MarkerTypes);
92    DocumentMarkerVector markers();
93    Vector<IntRect> renderedRectsForMarkers(DocumentMarker::MarkerType);
94
95    void trace(Visitor*);
96
97#ifndef NDEBUG
98    void showMarkers() const;
99#endif
100
101private:
102    void addMarker(Node*, const DocumentMarker&);
103
104    typedef WillBeHeapVector<OwnPtrWillBeMember<RenderedDocumentMarker> > MarkerList;
105    typedef WillBeHeapVector<OwnPtrWillBeMember<MarkerList>, DocumentMarker::MarkerTypeIndexesCount> MarkerLists;
106    typedef WillBeHeapHashMap<RawPtrWillBeWeakMember<const Node>, OwnPtrWillBeMember<MarkerLists> > MarkerMap;
107    void mergeOverlapping(MarkerList*, DocumentMarker&);
108    bool possiblyHasMarkers(DocumentMarker::MarkerTypes);
109    void removeMarkersFromList(MarkerMap::iterator, DocumentMarker::MarkerTypes);
110    void removeMarkers(TextIterator&, DocumentMarker::MarkerTypes, RemovePartiallyOverlappingMarkerOrNot);
111
112    MarkerMap m_markers;
113    // Provide a quick way to determine whether a particular marker type is absent without going through the map.
114    DocumentMarker::MarkerTypes m_possiblyExistingMarkerTypes;
115};
116
117} // namespace blink
118
119#ifndef NDEBUG
120void showDocumentMarkers(const blink::DocumentMarkerController*);
121#endif
122
123#endif // DocumentMarkerController_h
124