blob: 65a4551751f0eb60a524f266c87c8e86e50baa83 [file] [log] [blame]
abarth@webkit.org450089a2009-10-02 08:03:35 +00001/*
weinig@apple.come2b28762010-05-12 23:21:59 +00002 * Copyright (C) 2006, 2007, 2008, 2009, 2010 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.
16 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 * 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"
beidson@apple.com992d69a2009-11-03 21:06:09 +000048#include "Page.h"
abarth@webkit.orgc72185c2010-04-09 06:43:19 +000049#include "UserGestureIndicator.h"
abarth@webkit.org450089a2009-10-02 08:03:35 +000050#include <wtf/CurrentTime.h>
51
52namespace WebCore {
53
rniwa@webkit.orgcf460072011-01-08 02:15:10 +000054unsigned NavigationDisablerForBeforeUnload::s_navigationDisableCount = 0;
55
ossy@webkit.org95c1bc42011-01-20 16:30:54 +000056class ScheduledNavigation {
57 WTF_MAKE_NONCOPYABLE(ScheduledNavigation); WTF_MAKE_FAST_ALLOCATED;
eric@webkit.org9b31e482010-03-31 23:22:23 +000058public:
abarth@webkit.org41c93442010-10-18 02:59:32 +000059 ScheduledNavigation(double delay, bool lockHistory, bool lockBackForwardList, bool wasDuringLoad, bool isLocationChange)
eric@webkit.org9b31e482010-03-31 23:22:23 +000060 : m_delay(delay)
61 , m_lockHistory(lockHistory)
62 , m_lockBackForwardList(lockBackForwardList)
63 , m_wasDuringLoad(wasDuringLoad)
weinig@apple.come2b28762010-05-12 23:21:59 +000064 , m_isLocationChange(isLocationChange)
abarth@webkit.org41c93442010-10-18 02:59:32 +000065 , m_wasUserGesture(ScriptController::processingUserGesture())
abarth@webkit.org450089a2009-10-02 08:03:35 +000066 {
abarth@webkit.org450089a2009-10-02 08:03:35 +000067 }
eric@webkit.org9b31e482010-03-31 23:22:23 +000068 virtual ~ScheduledNavigation() { }
abarth@webkit.org450089a2009-10-02 08:03:35 +000069
eric@webkit.org9b31e482010-03-31 23:22:23 +000070 virtual void fire(Frame*) = 0;
abarth@webkit.org450089a2009-10-02 08:03:35 +000071
eric@webkit.org9b31e482010-03-31 23:22:23 +000072 virtual bool shouldStartTimer(Frame*) { return true; }
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +000073 virtual void didStartTimer(Frame*, Timer<NavigationScheduler>*) { }
eric@webkit.org9b31e482010-03-31 23:22:23 +000074 virtual void didStopTimer(Frame*, bool /* newLoadInProgress */) { }
75
76 double delay() const { return m_delay; }
77 bool lockHistory() const { return m_lockHistory; }
78 bool lockBackForwardList() const { return m_lockBackForwardList; }
79 bool wasDuringLoad() const { return m_wasDuringLoad; }
weinig@apple.come2b28762010-05-12 23:21:59 +000080 bool isLocationChange() const { return m_isLocationChange; }
inferno@chromium.org1930aab2010-09-17 16:36:53 +000081 bool wasUserGesture() const { return m_wasUserGesture; }
eric@webkit.org9b31e482010-03-31 23:22:23 +000082
jnd@chromium.orgae563162010-12-02 20:17:06 +000083protected:
84 void clearUserGesture() { m_wasUserGesture = false; }
85
eric@webkit.org9b31e482010-03-31 23:22:23 +000086private:
87 double m_delay;
88 bool m_lockHistory;
89 bool m_lockBackForwardList;
90 bool m_wasDuringLoad;
weinig@apple.come2b28762010-05-12 23:21:59 +000091 bool m_isLocationChange;
inferno@chromium.org1930aab2010-09-17 16:36:53 +000092 bool m_wasUserGesture;
eric@webkit.org9b31e482010-03-31 23:22:23 +000093};
94
95class ScheduledURLNavigation : public ScheduledNavigation {
darin@apple.com1cf3d562010-12-07 16:23:03 +000096protected:
97 ScheduledURLNavigation(double delay, PassRefPtr<SecurityOrigin> securityOrigin, const String& url, const String& referrer, bool lockHistory, bool lockBackForwardList, bool duringLoad, bool isLocationChange)
abarth@webkit.org41c93442010-10-18 02:59:32 +000098 : ScheduledNavigation(delay, lockHistory, lockBackForwardList, duringLoad, isLocationChange)
darin@apple.com1cf3d562010-12-07 16:23:03 +000099 , m_securityOrigin(securityOrigin)
eric@webkit.org9b31e482010-03-31 23:22:23 +0000100 , m_url(url)
101 , m_referrer(referrer)
eric@webkit.org9b31e482010-03-31 23:22:23 +0000102 , m_haveToldClient(false)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000103 {
104 }
105
eric@webkit.org9b31e482010-03-31 23:22:23 +0000106 virtual void fire(Frame* frame)
107 {
abarth@webkit.org0dbfba22010-10-17 16:02:51 +0000108 UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
darin@apple.com1cf3d562010-12-07 16:23:03 +0000109 frame->loader()->changeLocation(m_securityOrigin, KURL(ParsedURLString, m_url), m_referrer, lockHistory(), lockBackForwardList(), false);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000110 }
111
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000112 virtual void didStartTimer(Frame* frame, Timer<NavigationScheduler>* timer)
eric@webkit.org9b31e482010-03-31 23:22:23 +0000113 {
114 if (m_haveToldClient)
115 return;
116 m_haveToldClient = true;
117 frame->loader()->clientRedirected(KURL(ParsedURLString, m_url), delay(), currentTime() + timer->nextFireInterval(), lockBackForwardList());
118 }
119
120 virtual void didStopTimer(Frame* frame, bool newLoadInProgress)
121 {
122 if (!m_haveToldClient)
123 return;
124 frame->loader()->clientRedirectCancelledOrFinished(newLoadInProgress);
125 }
126
darin@apple.com1cf3d562010-12-07 16:23:03 +0000127 SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000128 String url() const { return m_url; }
129 String referrer() const { return m_referrer; }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000130
131private:
darin@apple.com1cf3d562010-12-07 16:23:03 +0000132 RefPtr<SecurityOrigin> m_securityOrigin;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000133 String m_url;
134 String m_referrer;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000135 bool m_haveToldClient;
136};
137
138class ScheduledRedirect : public ScheduledURLNavigation {
139public:
darin@apple.com1cf3d562010-12-07 16:23:03 +0000140 ScheduledRedirect(double delay, PassRefPtr<SecurityOrigin> securityOrigin, const String& url, bool lockHistory, bool lockBackForwardList)
141 : ScheduledURLNavigation(delay, securityOrigin, url, String(), lockHistory, lockBackForwardList, false, false)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000142 {
jnd@chromium.orgae563162010-12-02 20:17:06 +0000143 clearUserGesture();
abarth@webkit.org41c93442010-10-18 02:59:32 +0000144 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000145
eric@webkit.org9b31e482010-03-31 23:22:23 +0000146 virtual bool shouldStartTimer(Frame* frame) { return frame->loader()->allAncestorsAreComplete(); }
147};
148
149class ScheduledLocationChange : public ScheduledURLNavigation {
150public:
darin@apple.com1cf3d562010-12-07 16:23:03 +0000151 ScheduledLocationChange(PassRefPtr<SecurityOrigin> securityOrigin, const String& url, const String& referrer, bool lockHistory, bool lockBackForwardList, bool duringLoad)
152 : ScheduledURLNavigation(0.0, securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, true) { }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000153};
154
155class ScheduledRefresh : public ScheduledURLNavigation {
156public:
darin@apple.com1cf3d562010-12-07 16:23:03 +0000157 ScheduledRefresh(PassRefPtr<SecurityOrigin> securityOrigin, const String& url, const String& referrer)
158 : ScheduledURLNavigation(0.0, securityOrigin, url, referrer, true, true, false, true)
abarth@webkit.org41c93442010-10-18 02:59:32 +0000159 {
160 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000161
162 virtual void fire(Frame* frame)
163 {
abarth@webkit.org0dbfba22010-10-17 16:02:51 +0000164 UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
darin@apple.com1cf3d562010-12-07 16:23:03 +0000165 frame->loader()->changeLocation(securityOrigin(), KURL(ParsedURLString, url()), referrer(), lockHistory(), lockBackForwardList(), true);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000166 }
167};
168
169class ScheduledHistoryNavigation : public ScheduledNavigation {
170public:
abarth@webkit.org41c93442010-10-18 02:59:32 +0000171 explicit ScheduledHistoryNavigation(int historySteps)
172 : ScheduledNavigation(0, false, false, false, true)
173 , m_historySteps(historySteps)
174 {
175 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000176
177 virtual void fire(Frame* frame)
178 {
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000179 UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
180
eric@webkit.org9b31e482010-03-31 23:22:23 +0000181 if (!m_historySteps) {
182 // Special case for go(0) from a frame -> reload only the frame
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000183 // To follow Firefox and IE's behavior, history reload can only navigate the self frame.
japhet@chromium.org5a152742011-01-28 01:22:16 +0000184 frame->loader()->urlSelected(frame->document()->url(), "_self", 0, lockHistory(), lockBackForwardList(), SendReferrer);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000185 return;
186 }
187 // go(i!=0) from a frame navigates into the history of the frame only,
188 // in both IE and NS (but not in Mozilla). We can't easily do that.
darin@apple.come10e1282010-10-30 00:44:33 +0000189 frame->page()->backForward()->goBackOrForward(m_historySteps);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000190 }
191
192private:
193 int m_historySteps;
194};
195
196class ScheduledFormSubmission : public ScheduledNavigation {
197public:
abarth@webkit.org41c93442010-10-18 02:59:32 +0000198 ScheduledFormSubmission(PassRefPtr<FormSubmission> submission, bool lockBackForwardList, bool duringLoad)
199 : ScheduledNavigation(0, submission->lockHistory(), lockBackForwardList, duringLoad, true)
dglazkov@chromium.org996fc222010-06-21 16:36:40 +0000200 , m_submission(submission)
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000201 , m_haveToldClient(false)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000202 {
dglazkov@chromium.org996fc222010-06-21 16:36:40 +0000203 ASSERT(m_submission->state());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000204 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000205
206 virtual void fire(Frame* frame)
207 {
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000208 UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
commit-queue@webkit.org572a9b62010-09-03 18:21:29 +0000209
eric@webkit.org9b31e482010-03-31 23:22:23 +0000210 // The submitForm function will find a target frame before using the redirection timer.
211 // Now that the timer has fired, we need to repeat the security check which normally is done when
212 // selecting a target, in case conditions have changed. Other code paths avoid this by targeting
213 // without leaving a time window. If we fail the check just silently drop the form submission.
darin@apple.com1cf3d562010-12-07 16:23:03 +0000214 Frame* requestingFrame = m_submission->state()->sourceFrame();
215 if (!requestingFrame->loader()->shouldAllowNavigation(frame))
eric@webkit.org9b31e482010-03-31 23:22:23 +0000216 return;
darin@apple.com1cf3d562010-12-07 16:23:03 +0000217 FrameLoadRequest frameRequest(requestingFrame->document()->securityOrigin());
dglazkov@chromium.org996fc222010-06-21 16:36:40 +0000218 m_submission->populateFrameLoadRequest(frameRequest);
219 frame->loader()->loadFrameRequest(frameRequest, lockHistory(), lockBackForwardList(), m_submission->event(), m_submission->state(), SendReferrer);
eric@webkit.org9b31e482010-03-31 23:22:23 +0000220 }
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000221
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000222 virtual void didStartTimer(Frame* frame, Timer<NavigationScheduler>* timer)
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000223 {
224 if (m_haveToldClient)
225 return;
226 m_haveToldClient = true;
227 frame->loader()->clientRedirected(m_submission->requestURL(), delay(), currentTime() + timer->nextFireInterval(), lockBackForwardList());
228 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000229
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000230 virtual void didStopTimer(Frame* frame, bool newLoadInProgress)
231 {
232 if (!m_haveToldClient)
233 return;
234 frame->loader()->clientRedirectCancelledOrFinished(newLoadInProgress);
235 }
eric@webkit.org9b31e482010-03-31 23:22:23 +0000236
237private:
dglazkov@chromium.org996fc222010-06-21 16:36:40 +0000238 RefPtr<FormSubmission> m_submission;
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000239 bool m_haveToldClient;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000240};
241
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000242NavigationScheduler::NavigationScheduler(Frame* frame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000243 : m_frame(frame)
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000244 , m_timer(this, &NavigationScheduler::timerFired)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000245{
246}
247
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000248NavigationScheduler::~NavigationScheduler()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000249{
250}
251
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000252bool NavigationScheduler::redirectScheduledDuringLoad()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000253{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000254 return m_redirect && m_redirect->wasDuringLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000255}
256
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000257bool NavigationScheduler::locationChangePending()
weinig@apple.come2b28762010-05-12 23:21:59 +0000258{
259 return m_redirect && m_redirect->isLocationChange();
260}
261
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000262void NavigationScheduler::clear()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000263{
264 m_timer.stop();
eric@webkit.org9b31e482010-03-31 23:22:23 +0000265 m_redirect.clear();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000266}
267
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000268inline bool NavigationScheduler::shouldScheduleNavigation() const
269{
270 return m_frame->page();
271}
272
273inline bool NavigationScheduler::shouldScheduleNavigation(const String& url) const
274{
275 return shouldScheduleNavigation() && (protocolIsJavaScript(url) || NavigationDisablerForBeforeUnload::isNavigationAllowed());
276}
277
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000278void NavigationScheduler::scheduleRedirect(double delay, const String& url)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000279{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000280 if (!shouldScheduleNavigation(url))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000281 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000282 if (delay < 0 || delay > INT_MAX / 1000)
283 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000284 if (url.isEmpty())
285 return;
286
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000287 // We want a new back/forward list item if the refresh timeout is > 1 second.
eric@webkit.org9b31e482010-03-31 23:22:23 +0000288 if (!m_redirect || delay <= m_redirect->delay())
darin@apple.com1cf3d562010-12-07 16:23:03 +0000289 schedule(adoptPtr(new ScheduledRedirect(delay, m_frame->document()->securityOrigin(), url, true, delay <= 1)));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000290}
291
abarth@webkit.org41c93442010-10-18 02:59:32 +0000292bool NavigationScheduler::mustLockBackForwardList(Frame* targetFrame)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000293{
commit-queue@webkit.org2ca2f772010-08-17 23:40:00 +0000294 // 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 +0000295 // See https://webkit.org/b/42861 for the original motivation for this.
abarth@webkit.org41c93442010-10-18 02:59:32 +0000296 if (!ScriptController::processingUserGesture() && targetFrame->loader()->documentLoader() && !targetFrame->loader()->documentLoader()->wasOnloadHandled())
commit-queue@webkit.org4cbfdfd2010-08-13 23:35:34 +0000297 return true;
298
abarth@webkit.org450089a2009-10-02 08:03:35 +0000299 // Navigation of a subframe during loading of an ancestor frame does not create a new back/forward item.
300 // The definition of "during load" is any time before all handlers for the load event have been run.
301 // See https://bugs.webkit.org/show_bug.cgi?id=14957 for the original motivation for this.
abarth@webkit.org450089a2009-10-02 08:03:35 +0000302 for (Frame* ancestor = targetFrame->tree()->parent(); ancestor; ancestor = ancestor->tree()->parent()) {
303 Document* document = ancestor->document();
eric@webkit.org6cc17692009-11-09 20:04:49 +0000304 if (!ancestor->loader()->isComplete() || (document && document->processingLoadEvent()))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000305 return true;
306 }
307 return false;
308}
309
darin@apple.com1cf3d562010-12-07 16:23:03 +0000310void NavigationScheduler::scheduleLocationChange(PassRefPtr<SecurityOrigin> securityOrigin, const String& url, const String& referrer, bool lockHistory, bool lockBackForwardList)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000311{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000312 if (!shouldScheduleNavigation(url))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000313 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000314 if (url.isEmpty())
315 return;
316
abarth@webkit.org41c93442010-10-18 02:59:32 +0000317 lockBackForwardList = lockBackForwardList || mustLockBackForwardList(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000318
319 FrameLoader* loader = m_frame->loader();
320
321 // If the URL we're going to navigate to is the same as the current one, except for the
322 // fragment part, we don't need to schedule the location change.
323 KURL parsedURL(ParsedURLString, url);
japhet@chromium.org5a152742011-01-28 01:22:16 +0000324 if (parsedURL.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(m_frame->document()->url(), parsedURL)) {
darin@apple.com1cf3d562010-12-07 16:23:03 +0000325 loader->changeLocation(securityOrigin, loader->completeURL(url), referrer, lockHistory, lockBackForwardList);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000326 return;
327 }
328
329 // Handle a location change of a page with no document as a special case.
330 // This may happen when a frame changes the location of another frame.
japhet@chromium.org61d4b9c2010-06-21 20:06:02 +0000331 bool duringLoad = !loader->stateMachine()->committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000332
darin@apple.com1cf3d562010-12-07 16:23:03 +0000333 schedule(adoptPtr(new ScheduledLocationChange(securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad)));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000334}
335
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000336void NavigationScheduler::scheduleFormSubmission(PassRefPtr<FormSubmission> submission)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000337{
338 ASSERT(m_frame->page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000339
340 // FIXME: Do we need special handling for form submissions where the URL is the same
341 // as the current one except for the fragment part? See scheduleLocationChange above.
342
343 // Handle a location change of a page with no document as a special case.
344 // This may happen when a frame changes the location of another frame.
japhet@chromium.org61d4b9c2010-06-21 20:06:02 +0000345 bool duringLoad = !m_frame->loader()->stateMachine()->committedFirstRealDocumentLoad();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000346
beidson@apple.com1117ef42009-12-12 01:34:17 +0000347 // If this is a child frame and the form submission was triggered by a script, lock the back/forward list
348 // to match IE and Opera.
349 // See https://bugs.webkit.org/show_bug.cgi?id=32383 for the original motivation for this.
abarth@webkit.org41c93442010-10-18 02:59:32 +0000350 bool lockBackForwardList = mustLockBackForwardList(m_frame)
351 || (submission->state()->formSubmissionTrigger() == SubmittedByJavaScript
352 && m_frame->tree()->parent() && !ScriptController::processingUserGesture());
beidson@apple.com1117ef42009-12-12 01:34:17 +0000353
abarth@webkit.org41c93442010-10-18 02:59:32 +0000354 schedule(adoptPtr(new ScheduledFormSubmission(submission, lockBackForwardList, duringLoad)));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000355}
356
abarth@webkit.org41c93442010-10-18 02:59:32 +0000357void NavigationScheduler::scheduleRefresh()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000358{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000359 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000360 return;
japhet@chromium.org5a152742011-01-28 01:22:16 +0000361 const KURL& url = m_frame->document()->url();
abarth@webkit.org450089a2009-10-02 08:03:35 +0000362 if (url.isEmpty())
363 return;
364
darin@apple.com1cf3d562010-12-07 16:23:03 +0000365 schedule(adoptPtr(new ScheduledRefresh(m_frame->document()->securityOrigin(), url.string(), m_frame->loader()->outgoingReferrer())));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000366}
367
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000368void NavigationScheduler::scheduleHistoryNavigation(int steps)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000369{
rniwa@webkit.orgcf460072011-01-08 02:15:10 +0000370 if (!shouldScheduleNavigation())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000371 return;
372
beidson@apple.comaca909e2009-10-26 23:20:25 +0000373 // Invalid history navigations (such as history.forward() during a new load) have the side effect of cancelling any scheduled
374 // redirects. We also avoid the possibility of cancelling the current load by avoiding the scheduled redirection altogether.
mihaip@chromium.org5f3cb782010-11-02 21:23:43 +0000375 BackForwardController* backForward = m_frame->page()->backForward();
376 if (steps > backForward->forwardCount() || -steps > backForward->backCount()) {
beidson@apple.combf1584a2010-01-22 04:22:02 +0000377 cancel();
378 return;
379 }
inferno@chromium.org1930aab2010-09-17 16:36:53 +0000380
beidson@apple.combf1584a2010-01-22 04:22:02 +0000381 // In all other cases, schedule the history traversal to occur asynchronously.
abarth@webkit.org41c93442010-10-18 02:59:32 +0000382 schedule(adoptPtr(new ScheduledHistoryNavigation(steps)));
abarth@webkit.org450089a2009-10-02 08:03:35 +0000383}
384
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000385void NavigationScheduler::timerFired(Timer<NavigationScheduler>*)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000386{
eric@webkit.org3c521022009-10-29 00:22:14 +0000387 if (!m_frame->page())
388 return;
abarth@webkit.org450089a2009-10-02 08:03:35 +0000389 if (m_frame->page()->defersLoading())
390 return;
391
eric@webkit.org9b31e482010-03-31 23:22:23 +0000392 OwnPtr<ScheduledNavigation> redirect(m_redirect.release());
393 redirect->fire(m_frame);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000394}
395
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000396void NavigationScheduler::schedule(PassOwnPtr<ScheduledNavigation> redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000397{
398 ASSERT(m_frame->page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000399
400 // If a redirect was scheduled during a load, then stop the current load.
401 // Otherwise when the current load transitions from a provisional to a
402 // committed state, pending redirects may be cancelled.
eric@webkit.org9b31e482010-03-31 23:22:23 +0000403 if (redirect->wasDuringLoad()) {
404 if (DocumentLoader* provisionalDocumentLoader = m_frame->loader()->provisionalDocumentLoader())
abarth@webkit.org450089a2009-10-02 08:03:35 +0000405 provisionalDocumentLoader->stopLoading();
eric@webkit.org9b31e482010-03-31 23:22:23 +0000406 m_frame->loader()->stopLoading(UnloadEventPolicyUnloadAndPageHide);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000407 }
408
409 cancel();
eric@webkit.org9b31e482010-03-31 23:22:23 +0000410 m_redirect = redirect;
411
412 if (!m_frame->loader()->isComplete() && m_redirect->isLocationChange())
413 m_frame->loader()->completed();
414
abarth@webkit.org450089a2009-10-02 08:03:35 +0000415 startTimer();
416}
417
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000418void NavigationScheduler::startTimer()
abarth@webkit.org450089a2009-10-02 08:03:35 +0000419{
eric@webkit.org9b31e482010-03-31 23:22:23 +0000420 if (!m_redirect)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000421 return;
422
423 ASSERT(m_frame->page());
abarth@webkit.org450089a2009-10-02 08:03:35 +0000424 if (m_timer.isActive())
425 return;
eric@webkit.org9b31e482010-03-31 23:22:23 +0000426 if (!m_redirect->shouldStartTimer(m_frame))
abarth@webkit.org450089a2009-10-02 08:03:35 +0000427 return;
428
eric@webkit.org9b31e482010-03-31 23:22:23 +0000429 m_timer.startOneShot(m_redirect->delay());
430 m_redirect->didStartTimer(m_frame, &m_timer);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000431}
432
abarth@webkit.org88b4e6b2010-10-04 22:09:57 +0000433void NavigationScheduler::cancel(bool newLoadInProgress)
abarth@webkit.org450089a2009-10-02 08:03:35 +0000434{
435 m_timer.stop();
436
eric@webkit.org9b31e482010-03-31 23:22:23 +0000437 OwnPtr<ScheduledNavigation> redirect(m_redirect.release());
438 if (redirect)
439 redirect->didStopTimer(m_frame, newLoadInProgress);
abarth@webkit.org450089a2009-10-02 08:03:35 +0000440}
441
442} // namespace WebCore