blob: 56977532b8b12be08a619cd3fb1c1875d3b76eb1 [file] [log] [blame]
abarth@webkit.org450089a2009-10-02 08:03:35 +00001/*
aestes@apple.com176a9aa2016-07-08 20:14:34 +00002 * Copyright (C) 2006-2016 Apple Inc. All rights reserved.
abarth@webkit.org450089a2009-10-02 08:03:35 +00003 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
5 * Copyright (C) 2009 Adam Barth. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
mjs@apple.com92047332014-03-15 04:08:27 +000016 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
abarth@webkit.org450089a2009-10-02 08:03:35 +000017 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +000033#include "NavigationScheduler.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000034
darin@apple.come10e1282010-10-30 00:44:33 +000035#include "BackForwardController.h"
inferno@chromium.org1930aab2010-09-17 16:36:53 +000036#include "DOMWindow.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000037#include "DocumentLoader.h"
38#include "Event.h"
39#include "FormState.h"
dglazkov@chromium.org996fc222010-06-21 16:36:40 +000040#include "FormSubmission.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000041#include "Frame.h"
42#include "FrameLoadRequest.h"
43#include "FrameLoader.h"
japhet@chromium.org61d4b9c2010-06-21 20:06:02 +000044#include "FrameLoaderStateMachine.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000045#include "HTMLFormElement.h"
beidson@apple.com1117ef42009-12-12 01:34:17 +000046#include "HTMLFrameOwnerElement.h"
inferno@chromium.org1930aab2010-09-17 16:36:53 +000047#include "HistoryItem.h"
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +000048#include "InspectorInstrumentation.h"
beidson@apple.com992d69a2009-11-03 21:06:09 +000049#include "Page.h"
weinig@apple.com123e4aa2013-04-14 00:54:27 +000050#include "ScriptController.h"
abarth@webkit.orgc72185c2010-04-09 06:43:19 +000051#include "UserGestureIndicator.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000052#include <wtf/CurrentTime.h>
akling@apple.comf8515982013-09-02 18:50:01 +000053#include <wtf/Ref.h>
abarth@webkit.org450089a2009-10-02 08:03:35 +000054
55namespace WebCore {
56
rniwa@webkit.orgcf460072011-01-08 02:15:10 +000057unsigned NavigationDisablerForBeforeUnload::s_navigationDisableCount = 0;
58
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000059class ScheduledNavigation {
60 WTF_MAKE_NONCOPYABLE(ScheduledNavigation); WTF_MAKE_FAST_ALLOCATED;
eric@webkit.org9b31e482010-03-31 23:22:23 +000061public:
mrowe@apple.combf5eaa62014-04-03 00:51:04 +000062 ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange)
eric@webkit.org9b31e482010-03-31 23:22:23 +000063 : m_delay(delay)
64 , m_lockHistory(lockHistory)
65 , m_lockBackForwardList(lockBackForwardList)
66 , m_wasDuringLoad(wasDuringLoad)
weinig@apple.come2b28762010-05-12 23:21:59 +000067 , m_isLocationChange(isLocationChange)
weinig@apple.com2e10e102016-07-18 01:07:11 +000068 , m_userGestureToForward(UserGestureIndicator::currentUserGesture())
abarth@webkit.org450089a2009-10-02 08:03:35 +000069 {
abarth@webkit.org450089a2009-10-02 08:03:35 +000070 }
darin@apple.comc04189e2017-01-18 05:17:17 +000071 ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange, ShouldOpenExternalURLsPolicy externalURLPolicy)
72 : m_delay(delay)
73 , m_lockHistory(lockHistory)
74 , m_lockBackForwardList(lockBackForwardList)
75 , m_wasDuringLoad(wasDuringLoad)
76 , m_isLocationChange(isLocationChange)
77 , m_userGestureToForward(UserGestureIndicator::currentUserGesture())
78 , m_shouldOpenExternalURLsPolicy(externalURLPolicy)
79 {
80 }
eric@webkit.org9b31e482010-03-31 23:22:23 +000081 virtual ~ScheduledNavigation() { }
abarth@webkit.org450089a2009-10-02 08:03:35 +000082
andersca@apple.com8a458862014-01-11 00:38:16 +000083 virtual void fire(Frame&) = 0;
abarth@webkit.org450089a2009-10-02 08:03:35 +000084
andersca@apple.com8a458862014-01-11 00:38:16 +000085 virtual bool shouldStartTimer(Frame&) { return true; }
jer.noble@apple.com50862fe2014-11-06 23:06:03 +000086 virtual void didStartTimer(Frame&, Timer&) { }
andersca@apple.com8a458862014-01-11 00:38:16 +000087 virtual void didStopTimer(Frame&, bool /* newLoadInProgress */) { }
eric@webkit.org9b31e482010-03-31 23:22:23 +000088
89 double delay() const { return m_delay; }
mrowe@apple.combf5eaa62014-04-03 00:51:04 +000090 LockHistory lockHistory() const { return m_lockHistory; }
91 LockBackForwardList lockBackForwardList() const { return m_lockBackForwardList; }
eric@webkit.org9b31e482010-03-31 23:22:23 +000092 bool wasDuringLoad() const { return m_wasDuringLoad; }
weinig@apple.come2b28762010-05-12 23:21:59 +000093 bool isLocationChange() const { return m_isLocationChange; }
darin@apple.comc04189e2017-01-18 05:17:17 +000094 UserGestureToken* userGestureToForward() const { return m_userGestureToForward.get(); }
eric@webkit.org9b31e482010-03-31 23:22:23 +000095
jnd@chromium.orgae563162010-12-02 20:17:06 +000096protected:
weinig@apple.com2e10e102016-07-18 01:07:11 +000097 void clearUserGesture() { m_userGestureToForward = nullptr; }
darin@apple.comc04189e2017-01-18 05:17:17 +000098 ShouldOpenExternalURLsPolicy shouldOpenExternalURLs() const { return m_shouldOpenExternalURLsPolicy; }
beidson@apple.com87dc6312015-06-02 18:43:13 +000099
eric@webkit.org9b31e482010-03-31 23:22:23 +0000100private:
101 double m_delay;
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000102 LockHistory m_lockHistory;
103 LockBackForwardList m_lockBackForwardList;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000104 bool m_wasDuringLoad;
weinig@apple.come2b28762010-05-12 23:21:59 +0000105 bool m_isLocationChange;
weinig@apple.com2e10e102016-07-18 01:07:11 +0000106 RefPtr<UserGestureToken> m_userGestureToForward;
darin@apple.comc04189e2017-01-18 05:17:17 +0000107 ShouldOpenExternalURLsPolicy m_shouldOpenExternalURLsPolicy { ShouldOpenExternalURLsPolicy::ShouldNotAllow };
eric@webkit.org9b31e482010-03-31 23:22:23 +0000108};
109
110class ScheduledURLNavigation : public ScheduledNavigation {
darin@apple.com1cf3d562010-12-07 16:23:03 +0000111protected:
darin@apple.comc04189e2017-01-18 05:17:17 +0000112 ScheduledURLNavigation(Document& initiatingDocument, double delay, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad, bool isLocationChange)
113 : ScheduledNavigation(delay, lockHistory, lockBackForwardList, duringLoad, isLocationChange, initiatingDocument.shouldOpenExternalURLsPolicyToPropagate())
darin@apple.com1cf3d562010-12-07 16:23:03 +0000114 , m_securityOrigin(securityOrigin)
eric@webkit.org9b31e482010-03-31 23:22:23 +0000115 , m_url(url)
116 , m_referrer(referrer)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000117 {
118 }
119
darin@apple.com11ff47c2016-03-04 16:47:55 +0000120 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000121 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000122 UserGestureIndicator gestureIndicator(userGestureToForward());
beidson@apple.comee119d42015-04-28 19:38:13 +0000123
124 ResourceRequest resourceRequest(m_url, m_referrer, UseProtocolCachePolicy);
darin@apple.comc04189e2017-01-18 05:17:17 +0000125 FrameLoadRequest frameRequest(m_securityOrigin.get(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
beidson@apple.comee119d42015-04-28 19:38:13 +0000126
127 frame.loader().changeLocation(frameRequest);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000128 }
129
darin@apple.com11ff47c2016-03-04 16:47:55 +0000130 void didStartTimer(Frame& frame, Timer& timer) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000131 {
132 if (m_haveToldClient)
133 return;
134 m_haveToldClient = true;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000135
weinig@apple.com2e10e102016-07-18 01:07:11 +0000136 UserGestureIndicator gestureIndicator(userGestureToForward());
darin@apple.com47a20cb2014-04-27 16:06:27 +0000137 frame.loader().clientRedirected(m_url, delay(), currentTime() + timer.nextFireInterval(), lockBackForwardList());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000138 }
139
darin@apple.com11ff47c2016-03-04 16:47:55 +0000140 void didStopTimer(Frame& frame, bool newLoadInProgress) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000141 {
142 if (!m_haveToldClient)
143 return;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000144
145 // Do not set a UserGestureIndicator because
146 // clientRedirectCancelledOrFinished() is also called from many places
147 // inside FrameLoader, where the gesture state is not set and is in
148 // fact unavailable. We need to be consistent with them, otherwise the
149 // gesture state will sometimes be set and sometimes not within
150 // dispatchDidCancelClientRedirect().
andersca@apple.com8a458862014-01-11 00:38:16 +0000151 frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000152 }
153
darin@apple.com1cf3d562010-12-07 16:23:03 +0000154 SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
darin@apple.com47a20cb2014-04-27 16:06:27 +0000155 const URL& url() const { return m_url; }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000156 String referrer() const { return m_referrer; }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000157
158private:
darin@apple.com1cf3d562010-12-07 16:23:03 +0000159 RefPtr<SecurityOrigin> m_securityOrigin;
darin@apple.com47a20cb2014-04-27 16:06:27 +0000160 URL m_url;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000161 String m_referrer;
darin@apple.comc04189e2017-01-18 05:17:17 +0000162 bool m_haveToldClient { false };
eric@webkit.org9b31e482010-03-31 23:22:23 +0000163};
164
165class ScheduledRedirect : public ScheduledURLNavigation {
166public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000167 ScheduledRedirect(Document& initiatingDocument, double delay, SecurityOrigin* securityOrigin, const URL& url, LockHistory lockHistory, LockBackForwardList lockBackForwardList)
beidson@apple.com87dc6312015-06-02 18:43:13 +0000168 : ScheduledURLNavigation(initiatingDocument, delay, securityOrigin, url, String(), lockHistory, lockBackForwardList, false, false)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000169 {
jnd@chromium.orgae563162010-12-02 20:17:06 +0000170 clearUserGesture();
abarth@webkit.org41c93442010-10-18 02:59:32 +0000171 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000172
darin@apple.com11ff47c2016-03-04 16:47:55 +0000173 bool shouldStartTimer(Frame& frame) override
andersca@apple.com8a458862014-01-11 00:38:16 +0000174 {
175 return frame.loader().allAncestorsAreComplete();
176 }
robert@webkit.org52019832011-07-03 20:34:14 +0000177
darin@apple.com11ff47c2016-03-04 16:47:55 +0000178 void fire(Frame& frame) override
robert@webkit.org52019832011-07-03 20:34:14 +0000179 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000180 UserGestureIndicator gestureIndicator(userGestureToForward());
darin@apple.com47a20cb2014-04-27 16:06:27 +0000181 bool refresh = equalIgnoringFragmentIdentifier(frame.document()->url(), url());
beidson@apple.comee119d42015-04-28 19:38:13 +0000182 ResourceRequest resourceRequest(url(), referrer(), refresh ? ReloadIgnoringCacheData : UseProtocolCachePolicy);
darin@apple.comc04189e2017-01-18 05:17:17 +0000183 FrameLoadRequest frameRequest(securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
beidson@apple.comee119d42015-04-28 19:38:13 +0000184
185 frame.loader().changeLocation(frameRequest);
robert@webkit.org52019832011-07-03 20:34:14 +0000186 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000187};
188
189class ScheduledLocationChange : public ScheduledURLNavigation {
190public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000191 ScheduledLocationChange(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad)
beidson@apple.com87dc6312015-06-02 18:43:13 +0000192 : ScheduledURLNavigation(initiatingDocument, 0.0, securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, true) { }
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000193
darin@apple.com11ff47c2016-03-04 16:47:55 +0000194 void fire(Frame& frame) override
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000195 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000196 UserGestureIndicator gestureIndicator(userGestureToForward());
beidson@apple.comee119d42015-04-28 19:38:13 +0000197
198 ResourceRequest resourceRequest(url(), referrer(), UseProtocolCachePolicy);
darin@apple.comc04189e2017-01-18 05:17:17 +0000199 FrameLoadRequest frameRequest(securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
beidson@apple.comee119d42015-04-28 19:38:13 +0000200 frame.loader().changeLocation(frameRequest);
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000201 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000202};
203
204class ScheduledRefresh : public ScheduledURLNavigation {
205public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000206 ScheduledRefresh(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer)
beidson@apple.com87dc6312015-06-02 18:43:13 +0000207 : ScheduledURLNavigation(initiatingDocument, 0.0, securityOrigin, url, referrer, LockHistory::Yes, LockBackForwardList::Yes, false, true)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000208 {
209 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000210
darin@apple.com11ff47c2016-03-04 16:47:55 +0000211 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000212 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000213 UserGestureIndicator gestureIndicator(userGestureToForward());
beidson@apple.comee119d42015-04-28 19:38:13 +0000214
215 ResourceRequest resourceRequest(url(), referrer(), ReloadIgnoringCacheData);
darin@apple.comc04189e2017-01-18 05:17:17 +0000216 FrameLoadRequest frameRequest(securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
beidson@apple.comee119d42015-04-28 19:38:13 +0000217 frame.loader().changeLocation(frameRequest);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000218 }
219};
220
221class ScheduledHistoryNavigation : public ScheduledNavigation {
222public:
abarth@webkit.org41c93442010-10-18 02:59:32 +0000223 explicit ScheduledHistoryNavigation(int historySteps)
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000224 : ScheduledNavigation(0, LockHistory::No, LockBackForwardList::No, false, true)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000225 , m_historySteps(historySteps)
226 {
227 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000228
darin@apple.com11ff47c2016-03-04 16:47:55 +0000229 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000230 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000231 UserGestureIndicator gestureIndicator(userGestureToForward());
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000232
eric@webkit.org9b31e482010-03-31 23:22:23 +0000233 if (!m_historySteps) {
234 // Special case for go(0) from a frame -> reload only the frame
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000235 // To follow Firefox and IE's behavior, history reload can only navigate the self frame.
darin@apple.comc04189e2017-01-18 05:17:17 +0000236 frame.loader().urlSelected(frame.document()->url(), "_self", 0, lockHistory(), lockBackForwardList(), MaybeSendReferrer, shouldOpenExternalURLs());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000237 return;
238 }
andersca@apple.com8a458862014-01-11 00:38:16 +0000239
eric@webkit.org9b31e482010-03-31 23:22:23 +0000240 // go(i!=0) from a frame navigates into the history of the frame only,
241 // in both IE and NS (but not in Mozilla). We can't easily do that.
andersca@apple.com8a458862014-01-11 00:38:16 +0000242 frame.page()->backForward().goBackOrForward(m_historySteps);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000243 }
244
245private:
246 int m_historySteps;
247};
248
249class ScheduledFormSubmission : public ScheduledNavigation {
250public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000251 ScheduledFormSubmission(Ref<FormSubmission>&& submission, LockBackForwardList lockBackForwardList, bool duringLoad)
252 : ScheduledNavigation(0, submission->lockHistory(), lockBackForwardList, duringLoad, true, submission->state().sourceDocument().shouldOpenExternalURLsPolicyToPropagate())
253 , m_submission(WTFMove(submission))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000254 {
abarth@webkit.org450089a2009-10-02 08:03:35 +0000255 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000256
darin@apple.com11ff47c2016-03-04 16:47:55 +0000257 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000258 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000259 UserGestureIndicator gestureIndicator(userGestureToForward());
commit-queue@webkit.org572a9b62010-09-03 18:21:29 +0000260
eric@webkit.org9b31e482010-03-31 23:22:23 +0000261 // The submitForm function will find a target frame before using the redirection timer.
262 // Now that the timer has fired, we need to repeat the security check which normally is done when
263 // selecting a target, in case conditions have changed. Other code paths avoid this by targeting
264 // without leaving a time window. If we fail the check just silently drop the form submission.
darin@apple.comc04189e2017-01-18 05:17:17 +0000265 auto& requestingDocument = m_submission->state().sourceDocument();
266 if (!requestingDocument.canNavigate(&frame))
eric@webkit.org9b31e482010-03-31 23:22:23 +0000267 return;
darin@apple.comc04189e2017-01-18 05:17:17 +0000268 FrameLoadRequest frameRequest(requestingDocument.securityOrigin(), lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
dglazkov@chromium.org996fc222010-06-21 16:36:40 +0000269 m_submission->populateFrameLoadRequest(frameRequest);
darin@apple.comc04189e2017-01-18 05:17:17 +0000270 frame.loader().loadFrameRequest(frameRequest, m_submission->event(), &m_submission->state());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000271 }
darin@apple.comc04189e2017-01-18 05:17:17 +0000272
darin@apple.com11ff47c2016-03-04 16:47:55 +0000273 void didStartTimer(Frame& frame, Timer& timer) override
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000274 {
275 if (m_haveToldClient)
276 return;
277 m_haveToldClient = true;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000278
weinig@apple.com2e10e102016-07-18 01:07:11 +0000279 UserGestureIndicator gestureIndicator(userGestureToForward());
andersca@apple.com8a458862014-01-11 00:38:16 +0000280 frame.loader().clientRedirected(m_submission->requestURL(), delay(), currentTime() + timer.nextFireInterval(), lockBackForwardList());
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000281 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000282
darin@apple.com11ff47c2016-03-04 16:47:55 +0000283 void didStopTimer(Frame& frame, bool newLoadInProgress) override
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000284 {
285 if (!m_haveToldClient)
286 return;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000287
288 // Do not set a UserGestureIndicator because
289 // clientRedirectCancelledOrFinished() is also called from many places
290 // inside FrameLoader, where the gesture state is not set and is in
291 // fact unavailable. We need to be consistent with them, otherwise the
292 // gesture state will sometimes be set and sometimes not within
293 // dispatchDidCancelClientRedirect().
andersca@apple.com8a458862014-01-11 00:38:16 +0000294 frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress);
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000295 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000296
297private:
darin@apple.comc04189e2017-01-18 05:17:17 +0000298 Ref<FormSubmission> m_submission;
299 bool m_haveToldClient { false };
abarth@webkit.org450089a2009-10-02 08:03:35 +0000300};
301
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000302class ScheduledPageBlock final : public ScheduledNavigation {
303public:
304 ScheduledPageBlock(Document& originDocument)
305 : ScheduledNavigation(0, LockHistory::Yes, LockBackForwardList::Yes, false, false)
306 , m_originDocument(originDocument)
307 {
308 }
309
310 void fire(Frame& frame) override
311 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000312 UserGestureIndicator gestureIndicator(userGestureToForward());
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000313
314 ResourceResponse replacementResponse(m_originDocument.url(), ASCIILiteral("text/plain"), 0, ASCIILiteral("UTF-8"));
315 SubstituteData replacementData(SharedBuffer::create(), m_originDocument.url(), replacementResponse, SubstituteData::SessionHistoryVisibility::Hidden);
316
317 ResourceRequest resourceRequest(m_originDocument.url(), emptyString(), ReloadIgnoringCacheData);
darin@apple.comc04189e2017-01-18 05:17:17 +0000318 FrameLoadRequest frameRequest(m_originDocument.securityOrigin(), resourceRequest, lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs());
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000319 frameRequest.setSubstituteData(replacementData);
320 frame.loader().load(frameRequest);
321 }
322
323private:
324 Document& m_originDocument;
325};
326
andersca@apple.com8a458862014-01-11 00:38:16 +0000327NavigationScheduler::NavigationScheduler(Frame& frame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000328 : m_frame(frame)
andersca@apple.com574a7452014-11-21 20:10:13 +0000329 , m_timer(*this, &NavigationScheduler::timerFired)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000330{
331}
332
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000333NavigationScheduler::~NavigationScheduler()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000334{
335}
336
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000337bool NavigationScheduler::redirectScheduledDuringLoad()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000338{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000339 return m_redirect && m_redirect->wasDuringLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000340}
341
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000342bool NavigationScheduler::locationChangePending()
weinig@apple.come2b28762010-05-12 23:21:59 +0000343{
344 return m_redirect && m_redirect->isLocationChange();
345}
346
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000347void NavigationScheduler::clear()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000348{
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000349 if (m_timer.isActive())
350 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000351 m_timer.stop();
andersca@apple.com8a458862014-01-11 00:38:16 +0000352 m_redirect = nullptr;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000353}
354
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000355inline bool NavigationScheduler::shouldScheduleNavigation() const
356{
andersca@apple.com8a458862014-01-11 00:38:16 +0000357 return m_frame.page();
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000358}
359
darin@apple.com47a20cb2014-04-27 16:06:27 +0000360inline bool NavigationScheduler::shouldScheduleNavigation(const URL& url) const
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000361{
darin@apple.com47a20cb2014-04-27 16:06:27 +0000362 if (!shouldScheduleNavigation())
363 return false;
364 if (protocolIsJavaScript(url))
365 return true;
darin@apple.com47a20cb2014-04-27 16:06:27 +0000366 return NavigationDisablerForBeforeUnload::isNavigationAllowed();
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000367}
368
darin@apple.comc04189e2017-01-18 05:17:17 +0000369void NavigationScheduler::scheduleRedirect(Document& initiatingDocument, double delay, const URL& url)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000370{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000371 if (!shouldScheduleNavigation(url))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000372 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000373 if (delay < 0 || delay > INT_MAX / 1000)
374 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000375 if (url.isEmpty())
376 return;
377
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000378 // We want a new back/forward list item if the refresh timeout is > 1 second.
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000379 if (!m_redirect || delay <= m_redirect->delay()) {
darin@apple.comc04189e2017-01-18 05:17:17 +0000380 auto lockBackForwardList = delay <= 1 ? LockBackForwardList::Yes : LockBackForwardList::No;
beidson@apple.com87dc6312015-06-02 18:43:13 +0000381 schedule(std::make_unique<ScheduledRedirect>(initiatingDocument, delay, m_frame.document()->securityOrigin(), url, LockHistory::Yes, lockBackForwardList));
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000382 }
abarth@webkit.org450089a2009-10-02 08:03:35 +0000383}
384
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000385LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000386{
commit-queue@webkit.org2ca2f772010-08-17 23:40:00 +0000387 // Non-user navigation before the page has finished firing onload should not create a new back/forward item.
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000388 // See https://webkit.org/b/42861 for the original motivation for this.
weinig@apple.com2e10e102016-07-18 01:07:11 +0000389 if (!UserGestureIndicator::processingUserGesture() && targetFrame.loader().documentLoader() && !targetFrame.loader().documentLoader()->wasOnloadDispatched())
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000390 return LockBackForwardList::Yes;
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000391
abarth@webkit.org450089a2009-10-02 08:03:35 +0000392 // Navigation of a subframe during loading of an ancestor frame does not create a new back/forward item.
393 // The definition of "during load" is any time before all handlers for the load event have been run.
394 // See https://bugs.webkit.org/show_bug.cgi?id=14957 for the original motivation for this.
andersca@apple.com8a458862014-01-11 00:38:16 +0000395 for (Frame* ancestor = targetFrame.tree().parent(); ancestor; ancestor = ancestor->tree().parent()) {
abarth@webkit.org450089a2009-10-02 08:03:35 +0000396 Document* document = ancestor->document();
andersca@apple.comdf550b92013-08-15 22:17:17 +0000397 if (!ancestor->loader().isComplete() || (document && document->processingLoadEvent()))
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000398 return LockBackForwardList::Yes;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000399 }
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000400 return LockBackForwardList::No;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000401}
402
darin@apple.comc04189e2017-01-18 05:17:17 +0000403void NavigationScheduler::scheduleLocationChange(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000404{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000405 if (!shouldScheduleNavigation(url))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000406 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000407
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000408 if (lockBackForwardList == LockBackForwardList::No)
409 lockBackForwardList = mustLockBackForwardList(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000410
andersca@apple.com8a458862014-01-11 00:38:16 +0000411 FrameLoader& loader = m_frame.loader();
eric@webkit.org818318b2011-06-16 23:15:10 +0000412
abarth@webkit.org450089a2009-10-02 08:03:35 +0000413 // If the URL we're going to navigate to is the same as the current one, except for the
414 // fragment part, we don't need to schedule the location change.
darin@apple.com610f27d2015-04-13 15:40:42 +0000415 if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(m_frame.document()->url(), url)) {
beidson@apple.comee119d42015-04-28 19:38:13 +0000416 ResourceRequest resourceRequest(m_frame.document()->completeURL(url), referrer, UseProtocolCachePolicy);
darin@apple.comc04189e2017-01-18 05:17:17 +0000417 FrameLoadRequest frameRequest(securityOrigin, resourceRequest, "_self", lockHistory, lockBackForwardList, MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, ReplaceDocumentIfJavaScriptURL, initiatingDocument.shouldOpenExternalURLsPolicyToPropagate());
beidson@apple.comee119d42015-04-28 19:38:13 +0000418 loader.changeLocation(frameRequest);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000419 return;
420 }
421
422 // Handle a location change of a page with no document as a special case.
423 // This may happen when a frame changes the location of another frame.
akling@apple.com63e53cb2014-02-05 20:42:37 +0000424 bool duringLoad = !loader.stateMachine().committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000425
beidson@apple.com87dc6312015-06-02 18:43:13 +0000426 schedule(std::make_unique<ScheduledLocationChange>(initiatingDocument, securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000427}
428
darin@apple.comc04189e2017-01-18 05:17:17 +0000429void NavigationScheduler::scheduleFormSubmission(Ref<FormSubmission>&& submission)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000430{
andersca@apple.com8a458862014-01-11 00:38:16 +0000431 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000432
433 // FIXME: Do we need special handling for form submissions where the URL is the same
434 // as the current one except for the fragment part? See scheduleLocationChange above.
435
436 // Handle a location change of a page with no document as a special case.
437 // This may happen when a frame changes the location of another frame.
akling@apple.com63e53cb2014-02-05 20:42:37 +0000438 bool duringLoad = !m_frame.loader().stateMachine().committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000439
beidson@apple.com1117ef42009-12-12 01:34:17 +0000440 // If this is a child frame and the form submission was triggered by a script, lock the back/forward list
441 // to match IE and Opera.
442 // See https://bugs.webkit.org/show_bug.cgi?id=32383 for the original motivation for this.
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000443 LockBackForwardList lockBackForwardList = mustLockBackForwardList(m_frame);
444 if (lockBackForwardList == LockBackForwardList::No
darin@apple.comc04189e2017-01-18 05:17:17 +0000445 && (submission->state().formSubmissionTrigger() == SubmittedByJavaScript && m_frame.tree().parent() && !UserGestureIndicator::processingUserGesture())) {
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000446 lockBackForwardList = LockBackForwardList::Yes;
447 }
448
darin@apple.comc04189e2017-01-18 05:17:17 +0000449 schedule(std::make_unique<ScheduledFormSubmission>(WTFMove(submission), lockBackForwardList, duringLoad));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000450}
451
darin@apple.comc04189e2017-01-18 05:17:17 +0000452void NavigationScheduler::scheduleRefresh(Document& initiatingDocument)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000453{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000454 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000455 return;
andersca@apple.com8a458862014-01-11 00:38:16 +0000456 const URL& url = m_frame.document()->url();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000457 if (url.isEmpty())
458 return;
459
beidson@apple.com87dc6312015-06-02 18:43:13 +0000460 schedule(std::make_unique<ScheduledRefresh>(initiatingDocument, m_frame.document()->securityOrigin(), url, m_frame.loader().outgoingReferrer()));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000461}
462
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000463void NavigationScheduler::scheduleHistoryNavigation(int steps)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000464{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000465 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000466 return;
467
beidson@apple.comaca909e2009-10-26 23:20:25 +0000468 // Invalid history navigations (such as history.forward() during a new load) have the side effect of cancelling any scheduled
469 // redirects. We also avoid the possibility of cancelling the current load by avoiding the scheduled redirection altogether.
andersca@apple.com8a458862014-01-11 00:38:16 +0000470 BackForwardController& backForward = m_frame.page()->backForward();
akling@apple.com31cf0092013-09-11 15:24:45 +0000471 if (steps > backForward.forwardCount() || -steps > backForward.backCount()) {
beidson@apple.combf1584a2010-01-22 04:22:02 +0000472 cancel();
473 return;
474 }
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000475
beidson@apple.combf1584a2010-01-22 04:22:02 +0000476 // In all other cases, schedule the history traversal to occur asynchronously.
andersca@apple.com8a458862014-01-11 00:38:16 +0000477 schedule(std::make_unique<ScheduledHistoryNavigation>(steps));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000478}
479
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000480void NavigationScheduler::schedulePageBlock(Document& originDocument)
481{
482 if (shouldScheduleNavigation())
483 schedule(std::make_unique<ScheduledPageBlock>(originDocument));
484}
485
andersca@apple.com574a7452014-11-21 20:10:13 +0000486void NavigationScheduler::timerFired()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000487{
andersca@apple.com8a458862014-01-11 00:38:16 +0000488 if (!m_frame.page())
eric@webkit.org3c521022009-10-29 00:22:14 +0000489 return;
andersca@apple.com8a458862014-01-11 00:38:16 +0000490 if (m_frame.page()->defersLoading()) {
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000491 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000492 return;
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000493 }
494
andersca@apple.com8a458862014-01-11 00:38:16 +0000495 Ref<Frame> protect(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000496
aestes@apple.com13aae082016-01-02 08:03:08 +0000497 std::unique_ptr<ScheduledNavigation> redirect = WTFMove(m_redirect);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000498 redirect->fire(m_frame);
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000499 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000500}
501
andersca@apple.com8a458862014-01-11 00:38:16 +0000502void NavigationScheduler::schedule(std::unique_ptr<ScheduledNavigation> redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000503{
andersca@apple.com8a458862014-01-11 00:38:16 +0000504 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000505
andersca@apple.com8a458862014-01-11 00:38:16 +0000506 Ref<Frame> protect(m_frame);
inferno@chromium.org01ee2a22012-02-14 21:02:36 +0000507
abarth@webkit.org450089a2009-10-02 08:03:35 +0000508 // If a redirect was scheduled during a load, then stop the current load.
509 // Otherwise when the current load transitions from a provisional to a
510 // committed state, pending redirects may be cancelled.
eric@webkit.org9b31e482010-03-31 23:22:23 +0000511 if (redirect->wasDuringLoad()) {
andersca@apple.com8a458862014-01-11 00:38:16 +0000512 if (DocumentLoader* provisionalDocumentLoader = m_frame.loader().provisionalDocumentLoader())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000513 provisionalDocumentLoader->stopLoading();
andersca@apple.com8a458862014-01-11 00:38:16 +0000514 m_frame.loader().stopLoading(UnloadEventPolicyUnloadAndPageHide);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000515 }
516
517 cancel();
aestes@apple.com13aae082016-01-02 08:03:08 +0000518 m_redirect = WTFMove(redirect);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000519
andersca@apple.com8a458862014-01-11 00:38:16 +0000520 if (!m_frame.loader().isComplete() && m_redirect->isLocationChange())
521 m_frame.loader().completed();
eric@webkit.org9b31e482010-03-31 23:22:23 +0000522
andersca@apple.com8a458862014-01-11 00:38:16 +0000523 if (!m_frame.page())
inferno@chromium.org01ee2a22012-02-14 21:02:36 +0000524 return;
525
abarth@webkit.org450089a2009-10-02 08:03:35 +0000526 startTimer();
527}
528
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000529void NavigationScheduler::startTimer()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000530{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000531 if (!m_redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000532 return;
533
andersca@apple.com8a458862014-01-11 00:38:16 +0000534 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000535 if (m_timer.isActive())
536 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000537 if (!m_redirect->shouldStartTimer(m_frame))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000538 return;
539
dbates@webkit.orgdbb69522013-10-24 21:40:55 +0000540 double delay = m_redirect->delay();
541 m_timer.startOneShot(delay);
542 InspectorInstrumentation::frameScheduledNavigation(m_frame, delay);
andersca@apple.com8a458862014-01-11 00:38:16 +0000543 m_redirect->didStartTimer(m_frame, m_timer); // m_redirect may be null on return (e.g. the client canceled the load)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000544}
545
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000546void NavigationScheduler::cancel(bool newLoadInProgress)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000547{
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000548 if (m_timer.isActive())
549 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000550 m_timer.stop();
551
aestes@apple.com13aae082016-01-02 08:03:08 +0000552 if (std::unique_ptr<ScheduledNavigation> redirect = WTFMove(m_redirect))
eric@webkit.org9b31e482010-03-31 23:22:23 +0000553 redirect->didStopTimer(m_frame, newLoadInProgress);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000554}
555
556} // namespace WebCore