1/*
2 * Copyright (C) 2006, 2008, 2011, 2014 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef HistoryItem_h
28#define HistoryItem_h
29
30#include "FloatRect.h"
31#include "IntPoint.h"
32#include "IntRect.h"
33#include "SerializedScriptValue.h"
34#include <memory>
35#include <wtf/RefCounted.h>
36#include <wtf/text/WTFString.h>
37
38#if PLATFORM(IOS)
39#include "ViewportArguments.h"
40#endif
41
42#if PLATFORM(COCOA)
43#import <wtf/RetainPtr.h>
44typedef struct objc_object* id;
45#endif
46
47namespace WebCore {
48
49class CachedPage;
50class Document;
51class FormData;
52class HistoryItem;
53class Image;
54class ResourceRequest;
55class URL;
56
57typedef Vector<RefPtr<HistoryItem>> HistoryItemVector;
58
59extern void (*notifyHistoryItemChanged)(HistoryItem*);
60
61class HistoryItem : public RefCounted<HistoryItem> {
62    friend class PageCache;
63
64public:
65    static PassRefPtr<HistoryItem> create() { return adoptRef(new HistoryItem); }
66    static PassRefPtr<HistoryItem> create(const String& urlString, const String& title)
67    {
68        return adoptRef(new HistoryItem(urlString, title));
69    }
70    static PassRefPtr<HistoryItem> create(const String& urlString, const String& title, const String& alternateTitle)
71    {
72        return adoptRef(new HistoryItem(urlString, title, alternateTitle));
73    }
74    static PassRefPtr<HistoryItem> create(const URL& url, const String& target, const String& parent, const String& title)
75    {
76        return adoptRef(new HistoryItem(url, target, parent, title));
77    }
78
79    ~HistoryItem();
80
81    PassRefPtr<HistoryItem> copy() const;
82
83    // Resets the HistoryItem to its initial state, as returned by create().
84    void reset();
85
86    const String& originalURLString() const;
87    const String& urlString() const;
88    const String& title() const;
89
90    bool isInPageCache() const { return m_cachedPage.get(); }
91    bool hasCachedPageExpired() const;
92
93    void setAlternateTitle(const String& alternateTitle);
94    const String& alternateTitle() const;
95
96    const String& parent() const;
97    URL url() const;
98    URL originalURL() const;
99    const String& referrer() const;
100    const String& target() const;
101    bool isTargetItem() const;
102
103    FormData* formData();
104    String formContentType() const;
105
106    bool lastVisitWasFailure() const { return m_lastVisitWasFailure; }
107
108    const IntPoint& scrollPoint() const;
109    void setScrollPoint(const IntPoint&);
110    void clearScrollPoint();
111
112    float pageScaleFactor() const;
113    void setPageScaleFactor(float);
114
115    const Vector<String>& documentState() const;
116    void setDocumentState(const Vector<String>&);
117    void clearDocumentState();
118
119    void setURL(const URL&);
120    void setURLString(const String&);
121    void setOriginalURLString(const String&);
122    void setReferrer(const String&);
123    void setTarget(const String&);
124    void setParent(const String&);
125    void setTitle(const String&);
126    void setIsTargetItem(bool);
127
128    void setStateObject(PassRefPtr<SerializedScriptValue> object);
129    PassRefPtr<SerializedScriptValue> stateObject() const { return m_stateObject; }
130
131    void setItemSequenceNumber(long long number) { m_itemSequenceNumber = number; }
132    long long itemSequenceNumber() const { return m_itemSequenceNumber; }
133
134    void setDocumentSequenceNumber(long long number) { m_documentSequenceNumber = number; }
135    long long documentSequenceNumber() const { return m_documentSequenceNumber; }
136
137    void setFormInfoFromRequest(const ResourceRequest&);
138    void setFormData(PassRefPtr<FormData>);
139    void setFormContentType(const String&);
140
141    void setLastVisitWasFailure(bool wasFailure) { m_lastVisitWasFailure = wasFailure; }
142
143    void addChildItem(PassRefPtr<HistoryItem>);
144    void setChildItem(PassRefPtr<HistoryItem>);
145    HistoryItem* childItemWithTarget(const String&) const;
146    HistoryItem* childItemWithDocumentSequenceNumber(long long number) const;
147    HistoryItem* targetItem();
148    const HistoryItemVector& children() const;
149    bool hasChildren() const;
150    void clearChildren();
151    bool isAncestorOf(const HistoryItem*) const;
152
153    bool shouldDoSameDocumentNavigationTo(HistoryItem* otherItem) const;
154    bool hasSameFrames(HistoryItem* otherItem) const;
155
156    void addRedirectURL(const String&);
157    Vector<String>* redirectURLs() const;
158    void setRedirectURLs(std::unique_ptr<Vector<String>>);
159
160    bool isCurrentDocument(Document*) const;
161
162#if PLATFORM(COCOA)
163    id viewState() const;
164    void setViewState(id);
165
166    // Transient properties may be of any ObjC type.  They are intended to be used to store state per back/forward list entry.
167    // The properties will not be persisted; when the history item is removed, the properties will be lost.
168    id getTransientProperty(const String&) const;
169    void setTransientProperty(const String&, id);
170#endif
171
172#ifndef NDEBUG
173    int showTree() const;
174    int showTreeWithIndent(unsigned indentLevel) const;
175#endif
176
177#if PLATFORM(IOS)
178    FloatRect exposedContentRect() const { return m_exposedContentRect; }
179    void setExposedContentRect(FloatRect exposedContentRect) { m_exposedContentRect = exposedContentRect; }
180
181    IntRect unobscuredContentRect() const { return m_unobscuredContentRect; }
182    void setUnobscuredContentRect(IntRect unobscuredContentRect) { m_unobscuredContentRect = unobscuredContentRect; }
183
184    FloatSize minimumLayoutSizeInScrollViewCoordinates() const { return m_minimumLayoutSizeInScrollViewCoordinates; }
185    void setMinimumLayoutSizeInScrollViewCoordinates(FloatSize minimumLayoutSizeInScrollViewCoordinates) { m_minimumLayoutSizeInScrollViewCoordinates = minimumLayoutSizeInScrollViewCoordinates; }
186
187    IntSize contentSize() const { return m_contentSize; }
188    void setContentSize(IntSize contentSize) { m_contentSize = contentSize; }
189
190    float scale() const { return m_scale; }
191    bool scaleIsInitial() const { return m_scaleIsInitial; }
192    void setScaleIsInitial(bool scaleIsInitial) { m_scaleIsInitial = scaleIsInitial; }
193    void setScale(float newScale, bool isInitial)
194    {
195        m_scale = newScale;
196        m_scaleIsInitial = isInitial;
197    }
198
199    const ViewportArguments& viewportArguments() const { return m_viewportArguments; }
200    void setViewportArguments(const ViewportArguments& viewportArguments) { m_viewportArguments = viewportArguments; }
201
202    uint32_t bookmarkID() const { return m_bookmarkID; }
203    void setBookmarkID(uint32_t bookmarkID) { m_bookmarkID = bookmarkID; }
204    String sharedLinkUniqueIdentifier() const { return m_sharedLinkUniqueIdentifier; }
205    void setSharedLinkUniqueIdentifier(const String& sharedLinkUniqueidentifier) { m_sharedLinkUniqueIdentifier = sharedLinkUniqueidentifier; }
206#endif
207
208private:
209    HistoryItem();
210    HistoryItem(const String& urlString, const String& title);
211    HistoryItem(const String& urlString, const String& title, const String& alternateTitle);
212    HistoryItem(const URL& url, const String& frameName, const String& parent, const String& title);
213
214    explicit HistoryItem(const HistoryItem&);
215
216    bool hasSameDocumentTree(HistoryItem* otherItem) const;
217
218    HistoryItem* findTargetItem();
219
220    String m_urlString;
221    String m_originalURLString;
222    String m_referrer;
223    String m_target;
224    String m_parent;
225    String m_title;
226    String m_displayTitle;
227
228    IntPoint m_scrollPoint;
229    float m_pageScaleFactor;
230    Vector<String> m_documentState;
231
232    HistoryItemVector m_children;
233
234    bool m_lastVisitWasFailure;
235    bool m_isTargetItem;
236
237    std::unique_ptr<Vector<String>> m_redirectURLs;
238
239    // If two HistoryItems have the same item sequence number, then they are
240    // clones of one another.  Traversing history from one such HistoryItem to
241    // another is a no-op.  HistoryItem clones are created for parent and
242    // sibling frames when only a subframe navigates.
243    int64_t m_itemSequenceNumber;
244
245    // If two HistoryItems have the same document sequence number, then they
246    // refer to the same instance of a document.  Traversing history from one
247    // such HistoryItem to another preserves the document.
248    int64_t m_documentSequenceNumber;
249
250    // Support for HTML5 History
251    RefPtr<SerializedScriptValue> m_stateObject;
252
253    // info used to repost form data
254    RefPtr<FormData> m_formData;
255    String m_formContentType;
256
257    // PageCache controls these fields.
258    HistoryItem* m_next;
259    HistoryItem* m_prev;
260    std::unique_ptr<CachedPage> m_cachedPage;
261
262#if PLATFORM(IOS)
263    FloatRect m_exposedContentRect;
264    IntRect m_unobscuredContentRect;
265    FloatSize m_minimumLayoutSizeInScrollViewCoordinates;
266    IntSize m_contentSize;
267    float m_scale;
268    bool m_scaleIsInitial;
269    ViewportArguments m_viewportArguments;
270
271    uint32_t m_bookmarkID;
272    String m_sharedLinkUniqueIdentifier;
273#endif
274
275#if PLATFORM(COCOA)
276    RetainPtr<id> m_viewState;
277    std::unique_ptr<HashMap<String, RetainPtr<id>>> m_transientProperties;
278#endif
279}; //class HistoryItem
280
281} //namespace WebCore
282
283#ifndef NDEBUG
284// Outside the WebCore namespace for ease of invocation from gdb.
285extern "C" int showTree(const WebCore::HistoryItem*);
286#endif
287
288#endif // HISTORYITEM_H
289