1/*
2 * Copyright (C) 2015 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "APIObject.h"
29#include "DataReference.h"
30#include "FrameInfoData.h"
31#include "NavigationActionData.h"
32#include "WebBackForwardListItem.h"
33#include "WebContentMode.h"
34#include <WebCore/AdClickAttribution.h>
35#include <WebCore/ProcessIdentifier.h>
36#include <WebCore/ResourceRequest.h>
37#include <WebCore/SecurityOriginData.h>
38#include <wtf/Optional.h>
39#include <wtf/Ref.h>
40
41namespace WebCore {
42enum class FrameLoadType : uint8_t;
43}
44
45namespace WebKit {
46class WebNavigationState;
47}
48
49namespace API {
50
51struct SubstituteData {
52 WTF_MAKE_FAST_ALLOCATED;
53public:
54 SubstituteData(Vector<uint8_t>&& content, const WTF::String& MIMEType, const WTF::String& encoding, const WTF::String& baseURL, API::Object* userData)
55 : content(WTFMove(content))
56 , MIMEType(MIMEType)
57 , encoding(encoding)
58 , baseURL(baseURL)
59 , userData(userData)
60 { }
61
62 Vector<uint8_t> content;
63 WTF::String MIMEType;
64 WTF::String encoding;
65 WTF::String baseURL;
66 RefPtr<API::Object> userData;
67};
68
69class Navigation : public ObjectImpl<Object::Type::Navigation> {
70 WTF_MAKE_NONCOPYABLE(Navigation);
71public:
72 static Ref<Navigation> create(WebKit::WebNavigationState& state)
73 {
74 return adoptRef(*new Navigation(state));
75 }
76
77 static Ref<Navigation> create(WebKit::WebNavigationState& state, WebKit::WebBackForwardListItem& targetItem, WebKit::WebBackForwardListItem* fromItem, WebCore::FrameLoadType backForwardFrameLoadType)
78 {
79 return adoptRef(*new Navigation(state, targetItem, fromItem, backForwardFrameLoadType));
80 }
81
82 static Ref<Navigation> create(WebKit::WebNavigationState& state, WebCore::ResourceRequest&& request, WebKit::WebBackForwardListItem* fromItem)
83 {
84 return adoptRef(*new Navigation(state, WTFMove(request), fromItem));
85 }
86
87 static Ref<Navigation> create(WebKit::WebNavigationState& state, std::unique_ptr<SubstituteData>&& substituteData)
88 {
89 return adoptRef(*new Navigation(state, WTFMove(substituteData)));
90 }
91
92 virtual ~Navigation();
93
94 uint64_t navigationID() const { return m_navigationID; }
95
96 const WebCore::ResourceRequest& originalRequest() const { return m_originalRequest; }
97 void setCurrentRequest(WebCore::ResourceRequest&&, WebCore::ProcessIdentifier);
98 const WebCore::ResourceRequest& currentRequest() const { return m_currentRequest; }
99 Optional<WebCore::ProcessIdentifier> currentRequestProcessIdentifier() const { return m_currentRequestProcessIdentifier; }
100
101 bool currentRequestIsRedirect() const { return m_lastNavigationAction.isRedirect; }
102
103 WebKit::WebBackForwardListItem* targetItem() const { return m_targetItem.get(); }
104 WebKit::WebBackForwardListItem* fromItem() const { return m_fromItem.get(); }
105 Optional<WebCore::FrameLoadType> backForwardFrameLoadType() const { return m_backForwardFrameLoadType; }
106
107 void appendRedirectionURL(const WTF::URL&);
108 Vector<WTF::URL> takeRedirectChain() { return WTFMove(m_redirectChain); }
109
110 bool wasUserInitiated() const { return !!m_lastNavigationAction.userGestureTokenIdentifier; }
111
112 bool shouldPerformDownload() const { return !m_lastNavigationAction.downloadAttribute.isNull(); }
113
114 bool isSystemPreview() const
115 {
116#if USE(SYSTEM_PREVIEW)
117 return currentRequest().isSystemPreview();
118#else
119 return false;
120#endif
121 }
122
123 bool treatAsSameOriginNavigation() const { return m_lastNavigationAction.treatAsSameOriginNavigation; }
124 bool hasOpenedFrames() const { return m_lastNavigationAction.hasOpenedFrames; }
125 bool openedByDOMWithOpener() const { return m_lastNavigationAction.openedByDOMWithOpener; }
126 const WebCore::SecurityOriginData& requesterOrigin() const { return m_lastNavigationAction.requesterOrigin; }
127
128 void setUserContentExtensionsEnabled(bool enabled) { m_userContentExtensionsEnabled = enabled; }
129 bool userContentExtensionsEnabled() const { return m_userContentExtensionsEnabled; }
130
131 WebCore::LockHistory lockHistory() const { return m_lastNavigationAction.lockHistory; }
132 WebCore::LockBackForwardList lockBackForwardList() const { return m_lastNavigationAction.lockBackForwardList; }
133
134 WTF::String clientRedirectSourceForHistory() const { return m_lastNavigationAction.clientRedirectSourceForHistory; }
135
136 void setLastNavigationAction(const WebKit::NavigationActionData& navigationAction) { m_lastNavigationAction = navigationAction; }
137 const WebKit::NavigationActionData& lastNavigationAction() const { return m_lastNavigationAction; }
138
139 void setOriginatingFrameInfo(const WebKit::FrameInfoData& frameInfo) { m_originatingFrameInfo = frameInfo; }
140 const WebKit::FrameInfoData& originatingFrameInfo() const { return m_originatingFrameInfo; }
141
142 void setDestinationFrameSecurityOrigin(const WebCore::SecurityOriginData& origin) { m_destinationFrameSecurityOrigin = origin; }
143 const WebCore::SecurityOriginData& destinationFrameSecurityOrigin() const { return m_destinationFrameSecurityOrigin; }
144
145 void setEffectiveContentMode(WebKit::WebContentMode mode) { m_effectiveContentMode = mode; }
146 WebKit::WebContentMode effectiveContentMode() const { return m_effectiveContentMode; }
147
148#if !LOG_DISABLED
149 const char* loggingString() const;
150#endif
151
152 const std::unique_ptr<SubstituteData>& substituteData() const { return m_substituteData; }
153
154 const Optional<WebCore::AdClickAttribution>& adClickAttribution() const { return m_lastNavigationAction.adClickAttribution; }
155
156private:
157 explicit Navigation(WebKit::WebNavigationState&);
158 Navigation(WebKit::WebNavigationState&, WebCore::ResourceRequest&&, WebKit::WebBackForwardListItem* fromItem);
159 Navigation(WebKit::WebNavigationState&, WebKit::WebBackForwardListItem& targetItem, WebKit::WebBackForwardListItem* fromItem, WebCore::FrameLoadType);
160 Navigation(WebKit::WebNavigationState&, std::unique_ptr<SubstituteData>&&);
161
162 uint64_t m_navigationID;
163 WebCore::ResourceRequest m_originalRequest;
164 WebCore::ResourceRequest m_currentRequest;
165 Optional<WebCore::ProcessIdentifier> m_currentRequestProcessIdentifier;
166 Vector<WTF::URL> m_redirectChain;
167
168 RefPtr<WebKit::WebBackForwardListItem> m_targetItem;
169 RefPtr<WebKit::WebBackForwardListItem> m_fromItem;
170 Optional<WebCore::FrameLoadType> m_backForwardFrameLoadType;
171 std::unique_ptr<SubstituteData> m_substituteData;
172 WebKit::NavigationActionData m_lastNavigationAction;
173 WebKit::FrameInfoData m_originatingFrameInfo;
174 WebCore::SecurityOriginData m_destinationFrameSecurityOrigin;
175 bool m_userContentExtensionsEnabled { true };
176 WebKit::WebContentMode m_effectiveContentMode { WebKit::WebContentMode::Recommended };
177};
178
179} // namespace API
180