1/*
2 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
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 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#pragma once
31
32#include "FrameLoaderTypes.h"
33#include <wtf/Noncopyable.h>
34#include <wtf/RefPtr.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebCore {
38
39class Frame;
40class HistoryItem;
41class SerializedScriptValue;
42
43enum class ShouldTreatAsContinuingLoad : bool;
44
45struct StringWithDirection;
46
47class HistoryController {
48 WTF_MAKE_NONCOPYABLE(HistoryController);
49 WTF_MAKE_FAST_ALLOCATED;
50public:
51 enum HistoryUpdateType { UpdateAll, UpdateAllExceptBackForwardList };
52
53 explicit HistoryController(Frame&);
54 ~HistoryController();
55
56 WEBCORE_EXPORT void saveScrollPositionAndViewStateToItem(HistoryItem*);
57 void clearScrollPositionAndViewState();
58 WEBCORE_EXPORT void restoreScrollPositionAndViewState();
59
60 void updateBackForwardListForFragmentScroll();
61
62 void saveDocumentState();
63 WEBCORE_EXPORT void saveDocumentAndScrollState();
64 void restoreDocumentState();
65
66 void invalidateCurrentItemCachedPage();
67
68 void updateForBackForwardNavigation();
69 void updateForReload();
70 void updateForStandardLoad(HistoryUpdateType updateType = UpdateAll);
71 void updateForRedirectWithLockedBackForwardList();
72 void updateForClientRedirect();
73 void updateForCommit();
74 void updateForSameDocumentNavigation();
75 void updateForFrameLoadCompleted();
76
77 HistoryItem* currentItem() const { return m_currentItem.get(); }
78 WEBCORE_EXPORT void setCurrentItem(HistoryItem&);
79 void setCurrentItemTitle(const StringWithDirection&);
80 bool currentItemShouldBeReplaced() const;
81 WEBCORE_EXPORT void replaceCurrentItem(HistoryItem*);
82
83 HistoryItem* previousItem() const { return m_previousItem.get(); }
84 void clearPreviousItem();
85
86 HistoryItem* provisionalItem() const { return m_provisionalItem.get(); }
87 void setProvisionalItem(HistoryItem*);
88
89 void pushState(RefPtr<SerializedScriptValue>&&, const String& title, const String& url);
90 void replaceState(RefPtr<SerializedScriptValue>&&, const String& title, const String& url);
91
92 void setDefersLoading(bool);
93
94private:
95 friend class Page;
96 bool shouldStopLoadingForHistoryItem(HistoryItem&) const;
97 void goToItem(HistoryItem&, FrameLoadType, ShouldTreatAsContinuingLoad);
98
99 void initializeItem(HistoryItem&);
100 Ref<HistoryItem> createItem();
101 Ref<HistoryItem> createItemTree(Frame& targetFrame, bool clipAtTarget);
102
103 void recursiveSetProvisionalItem(HistoryItem&, HistoryItem*);
104 void recursiveGoToItem(HistoryItem&, HistoryItem*, FrameLoadType, ShouldTreatAsContinuingLoad);
105 bool isReplaceLoadTypeWithProvisionalItem(FrameLoadType);
106 bool isReloadTypeWithProvisionalItem(FrameLoadType);
107 void recursiveUpdateForCommit();
108 void recursiveUpdateForSameDocumentNavigation();
109 bool itemsAreClones(HistoryItem&, HistoryItem*) const;
110 bool currentFramesMatchItem(HistoryItem&) const;
111 void updateBackForwardListClippedAtTarget(bool doClip);
112 void updateCurrentItem();
113
114 Frame& m_frame;
115
116 RefPtr<HistoryItem> m_currentItem;
117 RefPtr<HistoryItem> m_previousItem;
118 RefPtr<HistoryItem> m_provisionalItem;
119
120 bool m_frameLoadComplete;
121
122 bool m_defersLoading;
123 RefPtr<HistoryItem> m_deferredItem;
124 FrameLoadType m_deferredFrameLoadType;
125};
126
127} // namespace WebCore
128