1/*
2 * Copyright (C) 2006-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 *
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 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include "AdClickAttribution.h"
32#include "BackForwardItemIdentifier.h"
33#include "FrameLoaderTypes.h"
34#include "LayoutPoint.h"
35#include "PageIdentifier.h"
36#include "ResourceRequest.h"
37#include "SecurityOrigin.h"
38#include "UserGestureIndicator.h"
39#include <wtf/Forward.h>
40#include <wtf/Optional.h>
41
42namespace WebCore {
43
44class Document;
45class Event;
46class HistoryItem;
47class MouseEvent;
48class UIEventWithKeyState;
49
50// NavigationAction should never hold a strong reference to the originating document either directly
51// or indirectly as doing so prevents its destruction even after navigating away from it because
52// DocumentLoader keeps around the NavigationAction for the last navigation.
53class NavigationAction {
54public:
55 NavigationAction();
56 WEBCORE_EXPORT NavigationAction(Document&, const ResourceRequest&, InitiatedByMainFrame, NavigationType = NavigationType::Other, ShouldOpenExternalURLsPolicy = ShouldOpenExternalURLsPolicy::ShouldNotAllow, Event* = nullptr, const AtomString& downloadAttribute = nullAtom());
57 NavigationAction(Document&, const ResourceRequest&, InitiatedByMainFrame, FrameLoadType, bool isFormSubmission, Event* = nullptr, ShouldOpenExternalURLsPolicy = ShouldOpenExternalURLsPolicy::ShouldNotAllow, const AtomString& downloadAttribute = nullAtom());
58
59 WEBCORE_EXPORT ~NavigationAction();
60
61 WEBCORE_EXPORT NavigationAction(const NavigationAction&);
62 NavigationAction& operator=(const NavigationAction&);
63
64 NavigationAction(NavigationAction&&);
65 NavigationAction& operator=(NavigationAction&&);
66
67 using PageIDAndFrameIDPair = std::pair<PageIdentifier, uint64_t /* frameID */>;
68 class Requester {
69 public:
70 Requester(const Document&);
71
72 const URL& url() const { return m_url; }
73 const SecurityOrigin& securityOrigin() const { return *m_origin; }
74 PageIdentifier pageID() const { return m_pageIDAndFrameIDPair.first; }
75 uint64_t frameID() const { return m_pageIDAndFrameIDPair.second; }
76 private:
77 URL m_url;
78 RefPtr<SecurityOrigin> m_origin;
79 PageIDAndFrameIDPair m_pageIDAndFrameIDPair;
80 };
81 const Optional<Requester>& requester() const { return m_requester; }
82
83 struct UIEventWithKeyStateData {
84 UIEventWithKeyStateData(const UIEventWithKeyState&);
85
86 bool isTrusted;
87 bool shiftKey;
88 bool ctrlKey;
89 bool altKey;
90 bool metaKey;
91 };
92 struct MouseEventData : UIEventWithKeyStateData {
93 MouseEventData(const MouseEvent&);
94
95 LayoutPoint absoluteLocation;
96 FloatPoint locationInRootViewCoordinates;
97 short button;
98 unsigned short syntheticClickType;
99 bool buttonDown;
100 };
101 const Optional<UIEventWithKeyStateData>& keyStateEventData() const { return m_keyStateEventData; }
102 const Optional<MouseEventData>& mouseEventData() const { return m_mouseEventData; }
103
104 NavigationAction copyWithShouldOpenExternalURLsPolicy(ShouldOpenExternalURLsPolicy) const;
105
106 bool isEmpty() const { return !m_requester || m_requester->url().isEmpty() || m_resourceRequest.url().isEmpty(); }
107
108 URL url() const { return m_resourceRequest.url(); }
109 const ResourceRequest& resourceRequest() const { return m_resourceRequest; }
110
111 NavigationType type() const { return m_type; }
112
113 bool processingUserGesture() const { return m_userGestureToken ? m_userGestureToken->processingUserGesture() : false; }
114 RefPtr<UserGestureToken> userGestureToken() const { return m_userGestureToken; }
115
116 ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy() const { return m_shouldOpenExternalURLsPolicy; }
117 void setShouldOpenExternalURLsPolicy(ShouldOpenExternalURLsPolicy policy) { m_shouldOpenExternalURLsPolicy = policy; }
118 InitiatedByMainFrame initiatedByMainFrame() const { return m_initiatedByMainFrame; }
119
120 const AtomString& downloadAttribute() const { return m_downloadAttribute; }
121
122 bool treatAsSameOriginNavigation() const { return m_treatAsSameOriginNavigation; }
123
124 bool hasOpenedFrames() const { return m_hasOpenedFrames; }
125 void setHasOpenedFrames(bool value) { m_hasOpenedFrames = value; }
126
127 bool openedByDOMWithOpener() const { return m_openedByDOMWithOpener; }
128 void setOpenedByDOMWithOpener() { m_openedByDOMWithOpener = true; }
129
130 void setTargetBackForwardItem(HistoryItem&);
131 const Optional<BackForwardItemIdentifier>& targetBackForwardItemIdentifier() const { return m_targetBackForwardItemIdentifier; }
132
133 void setSourceBackForwardItem(HistoryItem*);
134 const Optional<BackForwardItemIdentifier>& sourceBackForwardItemIdentifier() const { return m_sourceBackForwardItemIdentifier; }
135
136 LockHistory lockHistory() const { return m_lockHistory; }
137 void setLockHistory(LockHistory lockHistory) { m_lockHistory = lockHistory; }
138
139 LockBackForwardList lockBackForwardList() const { return m_lockBackForwardList; }
140 void setLockBackForwardList(LockBackForwardList lockBackForwardList) { m_lockBackForwardList = lockBackForwardList; }
141
142 const Optional<AdClickAttribution>& adClickAttribution() const { return m_adClickAttribution; };
143 void setAdClickAttribution(AdClickAttribution&& adClickAttribution) { m_adClickAttribution = adClickAttribution; };
144
145private:
146 // Do not add a strong reference to the originating document or a subobject that holds the
147 // originating document. See comment above the class for more details.
148 Optional<Requester> m_requester;
149 ResourceRequest m_resourceRequest;
150 NavigationType m_type;
151 ShouldOpenExternalURLsPolicy m_shouldOpenExternalURLsPolicy;
152 InitiatedByMainFrame m_initiatedByMainFrame;
153 Optional<UIEventWithKeyStateData> m_keyStateEventData;
154 Optional<MouseEventData> m_mouseEventData;
155 RefPtr<UserGestureToken> m_userGestureToken { UserGestureIndicator::currentUserGesture() };
156 AtomString m_downloadAttribute;
157 bool m_treatAsSameOriginNavigation;
158 bool m_hasOpenedFrames { false };
159 bool m_openedByDOMWithOpener { false };
160 Optional<BackForwardItemIdentifier> m_targetBackForwardItemIdentifier;
161 Optional<BackForwardItemIdentifier> m_sourceBackForwardItemIdentifier;
162 LockHistory m_lockHistory { LockHistory::No };
163 LockBackForwardList m_lockBackForwardList { LockBackForwardList::No };
164 Optional<AdClickAttribution> m_adClickAttribution;
165};
166
167} // namespace WebCore
168