1/*
2 * Copyright (C) 2007-2018 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "DOMWindowProperty.h"
29#include "ExceptionOr.h"
30#include "JSValueInWrappedObject.h"
31#include "ScriptWrappable.h"
32#include "SerializedScriptValue.h"
33#include <wtf/WallTime.h>
34
35namespace WebCore {
36
37class Document;
38
39class History final : public ScriptWrappable, public RefCounted<History>, public DOMWindowProperty {
40 WTF_MAKE_ISO_ALLOCATED(History);
41public:
42 static Ref<History> create(DOMWindow& window) { return adoptRef(*new History(window)); }
43
44 unsigned length() const;
45
46 enum class ScrollRestoration {
47 Auto,
48 Manual
49 };
50
51 ExceptionOr<ScrollRestoration> scrollRestoration() const;
52 ExceptionOr<void> setScrollRestoration(ScrollRestoration);
53
54 SerializedScriptValue* state();
55 JSValueInWrappedObject& cachedState();
56
57 void back();
58 void forward();
59 void go(int);
60
61 void back(Document&);
62 void forward(Document&);
63 void go(Document&, int);
64
65 bool isSameAsCurrentState(SerializedScriptValue*) const;
66
67 ExceptionOr<void> pushState(RefPtr<SerializedScriptValue>&& data, const String& title, const String& urlString);
68 ExceptionOr<void> replaceState(RefPtr<SerializedScriptValue>&& data, const String& title, const String& urlString);
69
70private:
71 explicit History(DOMWindow&);
72
73 enum class StateObjectType { Push, Replace };
74 ExceptionOr<void> stateObjectAdded(RefPtr<SerializedScriptValue>&&, const String& title, const String& url, StateObjectType);
75 bool stateChanged() const;
76
77 URL urlForState(const String& url);
78
79 SerializedScriptValue* stateInternal() const;
80
81 RefPtr<SerializedScriptValue> m_lastStateObjectRequested;
82 JSValueInWrappedObject m_cachedState;
83
84 unsigned m_currentStateObjectTimeSpanObjectsAdded { 0 };
85 WallTime m_currentStateObjectTimeSpanStart;
86
87 // For the main frame's History object to keep track of all state object usage.
88 uint64_t m_totalStateObjectUsage { 0 };
89
90 // For each individual History object to keep track of the most recent state object added.
91 uint64_t m_mostRecentStateObjectUsage { 0 };
92};
93
94inline ExceptionOr<void> History::pushState(RefPtr<SerializedScriptValue>&& data, const String& title, const String& urlString)
95{
96 return stateObjectAdded(WTFMove(data), title, urlString, StateObjectType::Push);
97}
98
99inline ExceptionOr<void> History::replaceState(RefPtr<SerializedScriptValue>&& data, const String& title, const String& urlString)
100{
101 return stateObjectAdded(WTFMove(data), title, urlString, StateObjectType::Replace);
102}
103
104} // namespace WebCore
105