blob: 80ded7992686c0fbdcb19ae41439d0ac9c335eef [file] [log] [blame]
abarth@webkit.org450089a2009-10-02 08:03:35 +00001/*
dbates@webkit.org174ac712017-06-30 21:35:23 +00002 * Copyright (C) 2006-2017 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"
beidson@apple.com7034ec72017-07-06 00:01:53 +000036#include "CommonVM.h"
inferno@chromium.org1930aab2010-09-17 16:36:53 +000037#include "DOMWindow.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000038#include "DocumentLoader.h"
39#include "Event.h"
40#include "FormState.h"
dglazkov@chromium.org996fc222010-06-21 16:36:40 +000041#include "FormSubmission.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000042#include "Frame.h"
43#include "FrameLoadRequest.h"
44#include "FrameLoader.h"
japhet@chromium.org61d4b9c2010-06-21 20:06:02 +000045#include "FrameLoaderStateMachine.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000046#include "HTMLFormElement.h"
beidson@apple.com1117ef42009-12-12 01:34:17 +000047#include "HTMLFrameOwnerElement.h"
inferno@chromium.org1930aab2010-09-17 16:36:53 +000048#include "HistoryItem.h"
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +000049#include "InspectorInstrumentation.h"
simon.fraser@apple.com78287862017-02-13 22:53:46 +000050#include "Logging.h"
rniwa@webkit.orgf694bd62017-06-30 19:32:41 +000051#include "NavigationDisabler.h"
beidson@apple.com992d69a2009-11-03 21:06:09 +000052#include "Page.h"
cdumez@apple.comf0af1232018-02-13 20:46:47 +000053#include "PolicyChecker.h"
weinig@apple.com123e4aa2013-04-14 00:54:27 +000054#include "ScriptController.h"
abarth@webkit.orgc72185c2010-04-09 06:43:19 +000055#include "UserGestureIndicator.h"
akling@apple.comf8515982013-09-02 18:50:01 +000056#include <wtf/Ref.h>
abarth@webkit.org450089a2009-10-02 08:03:35 +000057
58namespace WebCore {
59
rniwa@webkit.orgf694bd62017-06-30 19:32:41 +000060unsigned NavigationDisabler::s_globalNavigationDisableCount = 0;
rniwa@webkit.orgcf460072011-01-08 02:15:10 +000061
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000062class ScheduledNavigation {
63 WTF_MAKE_NONCOPYABLE(ScheduledNavigation); WTF_MAKE_FAST_ALLOCATED;
eric@webkit.org9b31e482010-03-31 23:22:23 +000064public:
mrowe@apple.combf5eaa62014-04-03 00:51:04 +000065 ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange)
eric@webkit.org9b31e482010-03-31 23:22:23 +000066 : m_delay(delay)
67 , m_lockHistory(lockHistory)
68 , m_lockBackForwardList(lockBackForwardList)
69 , m_wasDuringLoad(wasDuringLoad)
weinig@apple.come2b28762010-05-12 23:21:59 +000070 , m_isLocationChange(isLocationChange)
weinig@apple.com2e10e102016-07-18 01:07:11 +000071 , m_userGestureToForward(UserGestureIndicator::currentUserGesture())
abarth@webkit.org450089a2009-10-02 08:03:35 +000072 {
abarth@webkit.org450089a2009-10-02 08:03:35 +000073 }
darin@apple.comc04189e2017-01-18 05:17:17 +000074 ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange, ShouldOpenExternalURLsPolicy externalURLPolicy)
75 : m_delay(delay)
76 , m_lockHistory(lockHistory)
77 , m_lockBackForwardList(lockBackForwardList)
78 , m_wasDuringLoad(wasDuringLoad)
79 , m_isLocationChange(isLocationChange)
80 , m_userGestureToForward(UserGestureIndicator::currentUserGesture())
81 , m_shouldOpenExternalURLsPolicy(externalURLPolicy)
82 {
beidson@apple.com7034ec72017-07-06 00:01:53 +000083 if (auto* frame = lexicalFrameFromCommonVM()) {
84 if (frame->isMainFrame())
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +000085 m_initiatedByMainFrame = InitiatedByMainFrame::Yes;
beidson@apple.com7034ec72017-07-06 00:01:53 +000086 }
darin@apple.comc04189e2017-01-18 05:17:17 +000087 }
dbates@webkit.orgf21f3ae2017-10-19 23:48:45 +000088 virtual ~ScheduledNavigation() = default;
abarth@webkit.org450089a2009-10-02 08:03:35 +000089
andersca@apple.com8a458862014-01-11 00:38:16 +000090 virtual void fire(Frame&) = 0;
abarth@webkit.org450089a2009-10-02 08:03:35 +000091
andersca@apple.com8a458862014-01-11 00:38:16 +000092 virtual bool shouldStartTimer(Frame&) { return true; }
jer.noble@apple.com50862fe2014-11-06 23:06:03 +000093 virtual void didStartTimer(Frame&, Timer&) { }
achristensen@apple.com6fddc912018-10-18 21:16:52 +000094 virtual void didStopTimer(Frame&, NewLoadInProgress) { }
eric@webkit.org9b31e482010-03-31 23:22:23 +000095
96 double delay() const { return m_delay; }
mrowe@apple.combf5eaa62014-04-03 00:51:04 +000097 LockHistory lockHistory() const { return m_lockHistory; }
98 LockBackForwardList lockBackForwardList() const { return m_lockBackForwardList; }
eric@webkit.org9b31e482010-03-31 23:22:23 +000099 bool wasDuringLoad() const { return m_wasDuringLoad; }
weinig@apple.come2b28762010-05-12 23:21:59 +0000100 bool isLocationChange() const { return m_isLocationChange; }
darin@apple.comc04189e2017-01-18 05:17:17 +0000101 UserGestureToken* userGestureToForward() const { return m_userGestureToForward.get(); }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000102
jnd@chromium.orgae563162010-12-02 20:17:06 +0000103protected:
weinig@apple.com2e10e102016-07-18 01:07:11 +0000104 void clearUserGesture() { m_userGestureToForward = nullptr; }
darin@apple.comc04189e2017-01-18 05:17:17 +0000105 ShouldOpenExternalURLsPolicy shouldOpenExternalURLs() const { return m_shouldOpenExternalURLsPolicy; }
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000106 InitiatedByMainFrame initiatedByMainFrame() const { return m_initiatedByMainFrame; };
beidson@apple.com87dc6312015-06-02 18:43:13 +0000107
eric@webkit.org9b31e482010-03-31 23:22:23 +0000108private:
109 double m_delay;
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000110 LockHistory m_lockHistory;
111 LockBackForwardList m_lockBackForwardList;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000112 bool m_wasDuringLoad;
weinig@apple.come2b28762010-05-12 23:21:59 +0000113 bool m_isLocationChange;
weinig@apple.com2e10e102016-07-18 01:07:11 +0000114 RefPtr<UserGestureToken> m_userGestureToForward;
darin@apple.comc04189e2017-01-18 05:17:17 +0000115 ShouldOpenExternalURLsPolicy m_shouldOpenExternalURLsPolicy { ShouldOpenExternalURLsPolicy::ShouldNotAllow };
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000116 InitiatedByMainFrame m_initiatedByMainFrame { InitiatedByMainFrame::Unknown };
eric@webkit.org9b31e482010-03-31 23:22:23 +0000117};
118
119class ScheduledURLNavigation : public ScheduledNavigation {
darin@apple.com1cf3d562010-12-07 16:23:03 +0000120protected:
darin@apple.comc04189e2017-01-18 05:17:17 +0000121 ScheduledURLNavigation(Document& initiatingDocument, double delay, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad, bool isLocationChange)
122 : ScheduledNavigation(delay, lockHistory, lockBackForwardList, duringLoad, isLocationChange, initiatingDocument.shouldOpenExternalURLsPolicyToPropagate())
dbates@webkit.org174ac712017-06-30 21:35:23 +0000123 , m_initiatingDocument { makeRef(initiatingDocument) }
124 , m_securityOrigin{ makeRefPtr(securityOrigin) }
125 , m_url { url }
126 , m_referrer { referrer }
abarth@webkit.org450089a2009-10-02 08:03:35 +0000127 {
128 }
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());
utatane.tea@gmail.comeb3a8092018-02-23 04:18:17 +0000137 frame.loader().clientRedirected(m_url, delay(), WallTime::now() + timer.nextFireInterval(), lockBackForwardList());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000138 }
139
achristensen@apple.com6fddc912018-10-18 21:16:52 +0000140 void didStopTimer(Frame& frame, NewLoadInProgress 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
dbates@webkit.org174ac712017-06-30 21:35:23 +0000154 Document& initiatingDocument() { return m_initiatingDocument.get(); }
darin@apple.com1cf3d562010-12-07 16:23:03 +0000155 SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
darin@apple.com47a20cb2014-04-27 16:06:27 +0000156 const URL& url() const { return m_url; }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000157 String referrer() const { return m_referrer; }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000158
159private:
dbates@webkit.org174ac712017-06-30 21:35:23 +0000160 Ref<Document> m_initiatingDocument;
darin@apple.com1cf3d562010-12-07 16:23:03 +0000161 RefPtr<SecurityOrigin> m_securityOrigin;
darin@apple.com47a20cb2014-04-27 16:06:27 +0000162 URL m_url;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000163 String m_referrer;
darin@apple.comc04189e2017-01-18 05:17:17 +0000164 bool m_haveToldClient { false };
eric@webkit.org9b31e482010-03-31 23:22:23 +0000165};
166
167class ScheduledRedirect : public ScheduledURLNavigation {
168public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000169 ScheduledRedirect(Document& initiatingDocument, double delay, SecurityOrigin* securityOrigin, const URL& url, LockHistory lockHistory, LockBackForwardList lockBackForwardList)
beidson@apple.com87dc6312015-06-02 18:43:13 +0000170 : ScheduledURLNavigation(initiatingDocument, delay, securityOrigin, url, String(), lockHistory, lockBackForwardList, false, false)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000171 {
jnd@chromium.orgae563162010-12-02 20:17:06 +0000172 clearUserGesture();
abarth@webkit.org41c93442010-10-18 02:59:32 +0000173 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000174
darin@apple.com11ff47c2016-03-04 16:47:55 +0000175 bool shouldStartTimer(Frame& frame) override
andersca@apple.com8a458862014-01-11 00:38:16 +0000176 {
177 return frame.loader().allAncestorsAreComplete();
178 }
robert@webkit.org52019832011-07-03 20:34:14 +0000179
darin@apple.com11ff47c2016-03-04 16:47:55 +0000180 void fire(Frame& frame) override
robert@webkit.org52019832011-07-03 20:34:14 +0000181 {
dbates@webkit.org174ac712017-06-30 21:35:23 +0000182 UserGestureIndicator gestureIndicator { userGestureToForward() };
beidson@apple.comee119d42015-04-28 19:38:13 +0000183
dbates@webkit.org174ac712017-06-30 21:35:23 +0000184 bool refresh = equalIgnoringFragmentIdentifier(frame.document()->url(), url());
simon.fraser@apple.com90e96252018-07-09 23:54:18 +0000185 ResourceRequest resourceRequest { url(), referrer(), refresh ? ResourceRequestCachePolicy::ReloadIgnoringCacheData : ResourceRequestCachePolicy::UseProtocolCachePolicy };
bfulgham@apple.come89a3202018-04-25 19:16:00 +0000186 if (initiatedByMainFrame() == InitiatedByMainFrame::Yes)
187 resourceRequest.setRequester(ResourceRequest::Requester::Main);
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000188 FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() };
dbates@webkit.org174ac712017-06-30 21:35:23 +0000189
commit-queue@webkit.org79b7c912017-08-31 21:26:24 +0000190 frame.loader().changeLocation(WTFMove(frameLoadRequest));
robert@webkit.org52019832011-07-03 20:34:14 +0000191 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000192};
193
194class ScheduledLocationChange : public ScheduledURLNavigation {
195public:
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000196 ScheduledLocationChange(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad, CompletionHandler<void()>&& completionHandler)
197 : ScheduledURLNavigation(initiatingDocument, 0.0, securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, true)
198 , m_completionHandler(WTFMove(completionHandler))
199 {
200 }
201
202 ~ScheduledLocationChange()
203 {
204 if (m_completionHandler)
205 m_completionHandler();
206 }
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000207
darin@apple.com11ff47c2016-03-04 16:47:55 +0000208 void fire(Frame& frame) override
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000209 {
dbates@webkit.org174ac712017-06-30 21:35:23 +0000210 UserGestureIndicator gestureIndicator { userGestureToForward() };
beidson@apple.comee119d42015-04-28 19:38:13 +0000211
simon.fraser@apple.com90e96252018-07-09 23:54:18 +0000212 ResourceRequest resourceRequest { url(), referrer(), ResourceRequestCachePolicy::UseProtocolCachePolicy };
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000213 FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() };
beidson@apple.com7034ec72017-07-06 00:01:53 +0000214
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000215 auto completionHandler = WTFMove(m_completionHandler);
commit-queue@webkit.org79b7c912017-08-31 21:26:24 +0000216 frame.loader().changeLocation(WTFMove(frameLoadRequest));
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000217 completionHandler();
aestes@apple.com47ac5e12014-08-18 17:48:48 +0000218 }
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000219
220private:
221 CompletionHandler<void()> m_completionHandler;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000222};
223
224class ScheduledRefresh : public ScheduledURLNavigation {
225public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000226 ScheduledRefresh(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer)
beidson@apple.com87dc6312015-06-02 18:43:13 +0000227 : ScheduledURLNavigation(initiatingDocument, 0.0, securityOrigin, url, referrer, LockHistory::Yes, LockBackForwardList::Yes, false, true)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000228 {
229 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000230
darin@apple.com11ff47c2016-03-04 16:47:55 +0000231 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000232 {
dbates@webkit.org174ac712017-06-30 21:35:23 +0000233 UserGestureIndicator gestureIndicator { userGestureToForward() };
beidson@apple.comee119d42015-04-28 19:38:13 +0000234
simon.fraser@apple.com90e96252018-07-09 23:54:18 +0000235 ResourceRequest resourceRequest { url(), referrer(), ResourceRequestCachePolicy::ReloadIgnoringCacheData };
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000236 FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self", lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() };
beidson@apple.com7034ec72017-07-06 00:01:53 +0000237
dbates@webkit.org174ac712017-06-30 21:35:23 +0000238 frame.loader().changeLocation(WTFMove(frameLoadRequest));
eric@webkit.org9b31e482010-03-31 23:22:23 +0000239 }
240};
241
242class ScheduledHistoryNavigation : public ScheduledNavigation {
243public:
abarth@webkit.org41c93442010-10-18 02:59:32 +0000244 explicit ScheduledHistoryNavigation(int historySteps)
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000245 : ScheduledNavigation(0, LockHistory::No, LockBackForwardList::No, false, true)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000246 , m_historySteps(historySteps)
247 {
248 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000249
darin@apple.com11ff47c2016-03-04 16:47:55 +0000250 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000251 {
weinig@apple.com2e10e102016-07-18 01:07:11 +0000252 UserGestureIndicator gestureIndicator(userGestureToForward());
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000253
eric@webkit.org9b31e482010-03-31 23:22:23 +0000254 if (!m_historySteps) {
255 // Special case for go(0) from a frame -> reload only the frame
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000256 // To follow Firefox and IE's behavior, history reload can only navigate the self frame.
darin@apple.comc04189e2017-01-18 05:17:17 +0000257 frame.loader().urlSelected(frame.document()->url(), "_self", 0, lockHistory(), lockBackForwardList(), MaybeSendReferrer, shouldOpenExternalURLs());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000258 return;
259 }
andersca@apple.com8a458862014-01-11 00:38:16 +0000260
eric@webkit.org9b31e482010-03-31 23:22:23 +0000261 // go(i!=0) from a frame navigates into the history of the frame only,
262 // in both IE and NS (but not in Mozilla). We can't easily do that.
andersca@apple.com8a458862014-01-11 00:38:16 +0000263 frame.page()->backForward().goBackOrForward(m_historySteps);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000264 }
265
266private:
267 int m_historySteps;
268};
269
270class ScheduledFormSubmission : public ScheduledNavigation {
271public:
darin@apple.comc04189e2017-01-18 05:17:17 +0000272 ScheduledFormSubmission(Ref<FormSubmission>&& submission, LockBackForwardList lockBackForwardList, bool duringLoad)
273 : ScheduledNavigation(0, submission->lockHistory(), lockBackForwardList, duringLoad, true, submission->state().sourceDocument().shouldOpenExternalURLsPolicyToPropagate())
274 , m_submission(WTFMove(submission))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000275 {
abarth@webkit.org450089a2009-10-02 08:03:35 +0000276 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000277
darin@apple.com11ff47c2016-03-04 16:47:55 +0000278 void fire(Frame& frame) override
eric@webkit.org9b31e482010-03-31 23:22:23 +0000279 {
cdumez@apple.comede5bd32019-12-20 00:28:57 +0000280 if (m_submission->wasCancelled())
281 return;
282
weinig@apple.com2e10e102016-07-18 01:07:11 +0000283 UserGestureIndicator gestureIndicator(userGestureToForward());
commit-queue@webkit.org572a9b62010-09-03 18:21:29 +0000284
eric@webkit.org9b31e482010-03-31 23:22:23 +0000285 // The submitForm function will find a target frame before using the redirection timer.
286 // Now that the timer has fired, we need to repeat the security check which normally is done when
287 // selecting a target, in case conditions have changed. Other code paths avoid this by targeting
288 // without leaving a time window. If we fail the check just silently drop the form submission.
darin@apple.comc04189e2017-01-18 05:17:17 +0000289 auto& requestingDocument = m_submission->state().sourceDocument();
290 if (!requestingDocument.canNavigate(&frame))
eric@webkit.org9b31e482010-03-31 23:22:23 +0000291 return;
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000292 FrameLoadRequest frameLoadRequest { requestingDocument, requestingDocument.securityOrigin(), { }, { }, lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() };
dbates@webkit.orgb8a43fd2017-06-21 21:19:05 +0000293 m_submission->populateFrameLoadRequest(frameLoadRequest);
bfulgham@apple.com657c5c72018-05-24 05:23:00 +0000294 frame.loader().loadFrameRequest(WTFMove(frameLoadRequest), m_submission->event(), m_submission->takeState());
eric@webkit.org9b31e482010-03-31 23:22:23 +0000295 }
darin@apple.comc04189e2017-01-18 05:17:17 +0000296
darin@apple.com11ff47c2016-03-04 16:47:55 +0000297 void didStartTimer(Frame& frame, Timer& timer) override
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000298 {
299 if (m_haveToldClient)
300 return;
301 m_haveToldClient = true;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000302
weinig@apple.com2e10e102016-07-18 01:07:11 +0000303 UserGestureIndicator gestureIndicator(userGestureToForward());
utatane.tea@gmail.comeb3a8092018-02-23 04:18:17 +0000304 frame.loader().clientRedirected(m_submission->requestURL(), delay(), WallTime::now() + timer.nextFireInterval(), lockBackForwardList());
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000305 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000306
achristensen@apple.com6fddc912018-10-18 21:16:52 +0000307 void didStopTimer(Frame& frame, NewLoadInProgress newLoadInProgress) override
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000308 {
309 if (!m_haveToldClient)
310 return;
commit-queue@webkit.org88c74ba2011-05-07 23:37:35 +0000311
312 // Do not set a UserGestureIndicator because
313 // clientRedirectCancelledOrFinished() is also called from many places
314 // inside FrameLoader, where the gesture state is not set and is in
315 // fact unavailable. We need to be consistent with them, otherwise the
316 // gesture state will sometimes be set and sometimes not within
317 // dispatchDidCancelClientRedirect().
andersca@apple.com8a458862014-01-11 00:38:16 +0000318 frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress);
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000319 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000320
321private:
darin@apple.comc04189e2017-01-18 05:17:17 +0000322 Ref<FormSubmission> m_submission;
323 bool m_haveToldClient { false };
abarth@webkit.org450089a2009-10-02 08:03:35 +0000324};
325
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000326class ScheduledPageBlock final : public ScheduledNavigation {
327public:
328 ScheduledPageBlock(Document& originDocument)
329 : ScheduledNavigation(0, LockHistory::Yes, LockBackForwardList::Yes, false, false)
330 , m_originDocument(originDocument)
331 {
332 }
333
334 void fire(Frame& frame) override
335 {
dbates@webkit.org174ac712017-06-30 21:35:23 +0000336 UserGestureIndicator gestureIndicator { userGestureToForward() };
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000337
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000338 ResourceResponse replacementResponse { m_originDocument.url(), "text/plain"_s, 0, "UTF-8"_s };
dbates@webkit.org174ac712017-06-30 21:35:23 +0000339 SubstituteData replacementData { SharedBuffer::create(), m_originDocument.url(), replacementResponse, SubstituteData::SessionHistoryVisibility::Hidden };
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000340
simon.fraser@apple.com90e96252018-07-09 23:54:18 +0000341 ResourceRequest resourceRequest { m_originDocument.url(), emptyString(), ResourceRequestCachePolicy::ReloadIgnoringCacheData };
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000342 FrameLoadRequest frameLoadRequest { m_originDocument, m_originDocument.securityOrigin(), resourceRequest, { }, lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() };
dbates@webkit.orgb8a43fd2017-06-21 21:19:05 +0000343 frameLoadRequest.setSubstituteData(replacementData);
dbates@webkit.org7f3a9822017-06-22 19:45:10 +0000344 frame.loader().load(WTFMove(frameLoadRequest));
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000345 }
346
347private:
348 Document& m_originDocument;
349};
350
andersca@apple.com8a458862014-01-11 00:38:16 +0000351NavigationScheduler::NavigationScheduler(Frame& frame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000352 : m_frame(frame)
andersca@apple.com574a7452014-11-21 20:10:13 +0000353 , m_timer(*this, &NavigationScheduler::timerFired)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000354{
355}
356
dbates@webkit.orgf21f3ae2017-10-19 23:48:45 +0000357NavigationScheduler::~NavigationScheduler() = default;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000358
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000359bool NavigationScheduler::redirectScheduledDuringLoad()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000360{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000361 return m_redirect && m_redirect->wasDuringLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000362}
363
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000364bool NavigationScheduler::locationChangePending()
weinig@apple.come2b28762010-05-12 23:21:59 +0000365{
366 return m_redirect && m_redirect->isLocationChange();
367}
368
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000369void NavigationScheduler::clear()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000370{
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000371 if (m_timer.isActive())
372 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000373 m_timer.stop();
andersca@apple.com8a458862014-01-11 00:38:16 +0000374 m_redirect = nullptr;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000375}
376
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000377inline bool NavigationScheduler::shouldScheduleNavigation() const
378{
andersca@apple.com8a458862014-01-11 00:38:16 +0000379 return m_frame.page();
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000380}
381
darin@apple.com47a20cb2014-04-27 16:06:27 +0000382inline bool NavigationScheduler::shouldScheduleNavigation(const URL& url) const
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000383{
darin@apple.com47a20cb2014-04-27 16:06:27 +0000384 if (!shouldScheduleNavigation())
385 return false;
keith_miller@apple.combb2f61c2018-12-01 03:28:36 +0000386 if (WTF::protocolIsJavaScript(url))
darin@apple.com47a20cb2014-04-27 16:06:27 +0000387 return true;
rniwa@webkit.orgf694bd62017-06-30 19:32:41 +0000388 return NavigationDisabler::isNavigationAllowed(m_frame);
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000389}
390
darin@apple.comc04189e2017-01-18 05:17:17 +0000391void NavigationScheduler::scheduleRedirect(Document& initiatingDocument, double delay, const URL& url)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000392{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000393 if (!shouldScheduleNavigation(url))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000394 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000395 if (delay < 0 || delay > INT_MAX / 1000)
396 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000397 if (url.isEmpty())
398 return;
399
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000400 // We want a new back/forward list item if the refresh timeout is > 1 second.
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000401 if (!m_redirect || delay <= m_redirect->delay()) {
darin@apple.comc04189e2017-01-18 05:17:17 +0000402 auto lockBackForwardList = delay <= 1 ? LockBackForwardList::Yes : LockBackForwardList::No;
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000403 schedule(makeUnique<ScheduledRedirect>(initiatingDocument, delay, &m_frame.document()->securityOrigin(), url, LockHistory::Yes, lockBackForwardList));
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000404 }
abarth@webkit.org450089a2009-10-02 08:03:35 +0000405}
406
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000407LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000408{
commit-queue@webkit.org2ca2f772010-08-17 23:40:00 +0000409 // 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 +0000410 // See https://webkit.org/b/42861 for the original motivation for this.
weinig@apple.com2e10e102016-07-18 01:07:11 +0000411 if (!UserGestureIndicator::processingUserGesture() && targetFrame.loader().documentLoader() && !targetFrame.loader().documentLoader()->wasOnloadDispatched())
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000412 return LockBackForwardList::Yes;
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000413
abarth@webkit.org450089a2009-10-02 08:03:35 +0000414 // Navigation of a subframe during loading of an ancestor frame does not create a new back/forward item.
415 // The definition of "during load" is any time before all handlers for the load event have been run.
416 // See https://bugs.webkit.org/show_bug.cgi?id=14957 for the original motivation for this.
andersca@apple.com8a458862014-01-11 00:38:16 +0000417 for (Frame* ancestor = targetFrame.tree().parent(); ancestor; ancestor = ancestor->tree().parent()) {
abarth@webkit.org450089a2009-10-02 08:03:35 +0000418 Document* document = ancestor->document();
andersca@apple.comdf550b92013-08-15 22:17:17 +0000419 if (!ancestor->loader().isComplete() || (document && document->processingLoadEvent()))
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000420 return LockBackForwardList::Yes;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000421 }
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000422 return LockBackForwardList::No;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000423}
424
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000425void NavigationScheduler::scheduleLocationChange(Document& initiatingDocument, SecurityOrigin& securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, CompletionHandler<void()>&& completionHandler)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000426{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000427 if (!shouldScheduleNavigation(url))
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000428 return completionHandler();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000429
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000430 if (lockBackForwardList == LockBackForwardList::No)
431 lockBackForwardList = mustLockBackForwardList(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000432
andersca@apple.com8a458862014-01-11 00:38:16 +0000433 FrameLoader& loader = m_frame.loader();
eric@webkit.org818318b2011-06-16 23:15:10 +0000434
abarth@webkit.org450089a2009-10-02 08:03:35 +0000435 // If the URL we're going to navigate to is the same as the current one, except for the
436 // fragment part, we don't need to schedule the location change.
darin@apple.com610f27d2015-04-13 15:40:42 +0000437 if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(m_frame.document()->url(), url)) {
simon.fraser@apple.com90e96252018-07-09 23:54:18 +0000438 ResourceRequest resourceRequest { m_frame.document()->completeURL(url), referrer, ResourceRequestCachePolicy::UseProtocolCachePolicy };
beidson@apple.com7034ec72017-07-06 00:01:53 +0000439 auto* frame = lexicalFrameFromCommonVM();
dbates@webkit.orgdc3a7cf2017-07-12 17:12:07 +0000440 auto initiatedByMainFrame = frame && frame->isMainFrame() ? InitiatedByMainFrame::Yes : InitiatedByMainFrame::Unknown;
beidson@apple.com7034ec72017-07-06 00:01:53 +0000441
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000442 FrameLoadRequest frameLoadRequest { initiatingDocument, securityOrigin, resourceRequest, "_self"_s, lockHistory, lockBackForwardList, MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, initiatingDocument.shouldOpenExternalURLsPolicyToPropagate(), initiatedByMainFrame };
dbates@webkit.org7f3a9822017-06-22 19:45:10 +0000443 loader.changeLocation(WTFMove(frameLoadRequest));
cdumez@apple.come3df2df2019-05-02 22:24:27 +0000444 return completionHandler();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000445 }
446
447 // Handle a location change of a page with no document as a special case.
448 // This may happen when a frame changes the location of another frame.
akling@apple.com63e53cb2014-02-05 20:42:37 +0000449 bool duringLoad = !loader.stateMachine().committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000450
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000451 schedule(makeUnique<ScheduledLocationChange>(initiatingDocument, &securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, WTFMove(completionHandler)));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000452}
453
darin@apple.comc04189e2017-01-18 05:17:17 +0000454void NavigationScheduler::scheduleFormSubmission(Ref<FormSubmission>&& submission)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000455{
andersca@apple.com8a458862014-01-11 00:38:16 +0000456 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000457
458 // FIXME: Do we need special handling for form submissions where the URL is the same
459 // as the current one except for the fragment part? See scheduleLocationChange above.
460
461 // Handle a location change of a page with no document as a special case.
462 // This may happen when a frame changes the location of another frame.
akling@apple.com63e53cb2014-02-05 20:42:37 +0000463 bool duringLoad = !m_frame.loader().stateMachine().committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000464
beidson@apple.com1117ef42009-12-12 01:34:17 +0000465 // If this is a child frame and the form submission was triggered by a script, lock the back/forward list
466 // to match IE and Opera.
467 // See https://bugs.webkit.org/show_bug.cgi?id=32383 for the original motivation for this.
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000468 LockBackForwardList lockBackForwardList = mustLockBackForwardList(m_frame);
469 if (lockBackForwardList == LockBackForwardList::No
darin@apple.comc04189e2017-01-18 05:17:17 +0000470 && (submission->state().formSubmissionTrigger() == SubmittedByJavaScript && m_frame.tree().parent() && !UserGestureIndicator::processingUserGesture())) {
mrowe@apple.combf5eaa62014-04-03 00:51:04 +0000471 lockBackForwardList = LockBackForwardList::Yes;
472 }
473
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000474 schedule(makeUnique<ScheduledFormSubmission>(WTFMove(submission), lockBackForwardList, duringLoad));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000475}
476
darin@apple.comc04189e2017-01-18 05:17:17 +0000477void NavigationScheduler::scheduleRefresh(Document& initiatingDocument)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000478{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000479 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000480 return;
andersca@apple.com8a458862014-01-11 00:38:16 +0000481 const URL& url = m_frame.document()->url();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000482 if (url.isEmpty())
483 return;
484
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000485 schedule(makeUnique<ScheduledRefresh>(initiatingDocument, &m_frame.document()->securityOrigin(), url, m_frame.loader().outgoingReferrer()));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000486}
487
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000488void NavigationScheduler::scheduleHistoryNavigation(int steps)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000489{
simon.fraser@apple.com78287862017-02-13 22:53:46 +0000490 LOG(History, "NavigationScheduler %p scheduleHistoryNavigation(%d) - shouldSchedule %d", this, steps, shouldScheduleNavigation());
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000491 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000492 return;
493
beidson@apple.comaca909e2009-10-26 23:20:25 +0000494 // Invalid history navigations (such as history.forward() during a new load) have the side effect of cancelling any scheduled
495 // 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 +0000496 BackForwardController& backForward = m_frame.page()->backForward();
achristensen@apple.com83efad72018-10-17 20:37:36 +0000497 if ((steps > 0 && static_cast<unsigned>(steps) > backForward.forwardCount())
498 || (steps < 0 && static_cast<unsigned>(-steps) > backForward.backCount())) {
beidson@apple.combf1584a2010-01-22 04:22:02 +0000499 cancel();
500 return;
501 }
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000502
beidson@apple.combf1584a2010-01-22 04:22:02 +0000503 // In all other cases, schedule the history traversal to occur asynchronously.
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000504 schedule(makeUnique<ScheduledHistoryNavigation>(steps));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000505}
506
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000507void NavigationScheduler::schedulePageBlock(Document& originDocument)
508{
509 if (shouldScheduleNavigation())
ysuzuki@apple.com1d8e24d2019-08-19 06:59:40 +0000510 schedule(makeUnique<ScheduledPageBlock>(originDocument));
dbates@webkit.orgdb7c48d2016-01-12 22:28:59 +0000511}
512
andersca@apple.com574a7452014-11-21 20:10:13 +0000513void NavigationScheduler::timerFired()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000514{
andersca@apple.com8a458862014-01-11 00:38:16 +0000515 if (!m_frame.page())
eric@webkit.org3c521022009-10-29 00:22:14 +0000516 return;
andersca@apple.com8a458862014-01-11 00:38:16 +0000517 if (m_frame.page()->defersLoading()) {
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000518 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000519 return;
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000520 }
521
andersca@apple.com8a458862014-01-11 00:38:16 +0000522 Ref<Frame> protect(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000523
aestes@apple.com13aae082016-01-02 08:03:08 +0000524 std::unique_ptr<ScheduledNavigation> redirect = WTFMove(m_redirect);
simon.fraser@apple.com78287862017-02-13 22:53:46 +0000525 LOG(History, "NavigationScheduler %p timerFired - firing redirect %p", this, redirect.get());
526
eric@webkit.org9b31e482010-03-31 23:22:23 +0000527 redirect->fire(m_frame);
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000528 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000529}
530
andersca@apple.com8a458862014-01-11 00:38:16 +0000531void NavigationScheduler::schedule(std::unique_ptr<ScheduledNavigation> redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000532{
andersca@apple.com8a458862014-01-11 00:38:16 +0000533 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000534
andersca@apple.com8a458862014-01-11 00:38:16 +0000535 Ref<Frame> protect(m_frame);
inferno@chromium.org01ee2a22012-02-14 21:02:36 +0000536
abarth@webkit.org450089a2009-10-02 08:03:35 +0000537 // If a redirect was scheduled during a load, then stop the current load.
538 // Otherwise when the current load transitions from a provisional to a
539 // committed state, pending redirects may be cancelled.
eric@webkit.org9b31e482010-03-31 23:22:23 +0000540 if (redirect->wasDuringLoad()) {
andersca@apple.com8a458862014-01-11 00:38:16 +0000541 if (DocumentLoader* provisionalDocumentLoader = m_frame.loader().provisionalDocumentLoader())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000542 provisionalDocumentLoader->stopLoading();
andersca@apple.com8a458862014-01-11 00:38:16 +0000543 m_frame.loader().stopLoading(UnloadEventPolicyUnloadAndPageHide);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000544 }
545
546 cancel();
aestes@apple.com13aae082016-01-02 08:03:08 +0000547 m_redirect = WTFMove(redirect);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000548
andersca@apple.com8a458862014-01-11 00:38:16 +0000549 if (!m_frame.loader().isComplete() && m_redirect->isLocationChange())
550 m_frame.loader().completed();
eric@webkit.org9b31e482010-03-31 23:22:23 +0000551
andersca@apple.com8a458862014-01-11 00:38:16 +0000552 if (!m_frame.page())
inferno@chromium.org01ee2a22012-02-14 21:02:36 +0000553 return;
554
abarth@webkit.org450089a2009-10-02 08:03:35 +0000555 startTimer();
556}
557
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000558void NavigationScheduler::startTimer()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000559{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000560 if (!m_redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000561 return;
562
andersca@apple.com8a458862014-01-11 00:38:16 +0000563 ASSERT(m_frame.page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000564 if (m_timer.isActive())
565 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000566 if (!m_redirect->shouldStartTimer(m_frame))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000567 return;
568
cdumez@apple.com7d7fe74a2017-04-10 08:02:21 +0000569 Seconds delay = 1_s * m_redirect->delay();
dbates@webkit.orgdbb69522013-10-24 21:40:55 +0000570 m_timer.startOneShot(delay);
571 InspectorInstrumentation::frameScheduledNavigation(m_frame, delay);
andersca@apple.com8a458862014-01-11 00:38:16 +0000572 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 +0000573}
574
achristensen@apple.com6fddc912018-10-18 21:16:52 +0000575void NavigationScheduler::cancel(NewLoadInProgress newLoadInProgress)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000576{
achristensen@apple.com6fddc912018-10-18 21:16:52 +0000577 LOG(History, "NavigationScheduler %p cancel(newLoadInProgress=%d)", this, newLoadInProgress == NewLoadInProgress::Yes);
simon.fraser@apple.com78287862017-02-13 22:53:46 +0000578
commit-queue@webkit.orgdaffe2f2013-01-24 05:42:33 +0000579 if (m_timer.isActive())
580 InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000581 m_timer.stop();
582
achristensen@apple.com6fddc912018-10-18 21:16:52 +0000583 if (auto redirect = WTFMove(m_redirect))
eric@webkit.org9b31e482010-03-31 23:22:23 +0000584 redirect->didStopTimer(m_frame, newLoadInProgress);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000585}
586
587} // namespace WebCore