NavigationScheduler.cpp [plain text]
#include "config.h"
#include "NavigationScheduler.h"
#include "BackForwardController.h"
#include "DOMWindow.h"
#include "DocumentLoader.h"
#include "Event.h"
#include "FormState.h"
#include "FormSubmission.h"
#include "Frame.h"
#include "FrameLoadRequest.h"
#include "FrameLoader.h"
#include "FrameLoaderStateMachine.h"
#include "HTMLFormElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HistoryItem.h"
#include "InspectorInstrumentation.h"
#include "Page.h"
#include "ScriptController.h"
#include "Settings.h"
#include "UserGestureIndicator.h"
#include <wtf/CurrentTime.h>
#include <wtf/Ref.h>
namespace WebCore {
unsigned NavigationDisablerForBeforeUnload::s_navigationDisableCount = 0;
class ScheduledNavigation {
WTF_MAKE_NONCOPYABLE(ScheduledNavigation); WTF_MAKE_FAST_ALLOCATED;
public:
ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange)
: m_delay(delay)
, m_lockHistory(lockHistory)
, m_lockBackForwardList(lockBackForwardList)
, m_wasDuringLoad(wasDuringLoad)
, m_isLocationChange(isLocationChange)
, m_wasUserGesture(ScriptController::processingUserGesture())
{
}
virtual ~ScheduledNavigation() { }
virtual void fire(Frame&) = 0;
virtual bool shouldStartTimer(Frame&) { return true; }
virtual void didStartTimer(Frame&, Timer<NavigationScheduler>&) { }
virtual void didStopTimer(Frame&, bool ) { }
double delay() const { return m_delay; }
LockHistory lockHistory() const { return m_lockHistory; }
LockBackForwardList lockBackForwardList() const { return m_lockBackForwardList; }
bool wasDuringLoad() const { return m_wasDuringLoad; }
bool isLocationChange() const { return m_isLocationChange; }
bool wasUserGesture() const { return m_wasUserGesture; }
protected:
void clearUserGesture() { m_wasUserGesture = false; }
private:
double m_delay;
LockHistory m_lockHistory;
LockBackForwardList m_lockBackForwardList;
bool m_wasDuringLoad;
bool m_isLocationChange;
bool m_wasUserGesture;
};
class ScheduledURLNavigation : public ScheduledNavigation {
protected:
ScheduledURLNavigation(double delay, SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad, bool isLocationChange)
: ScheduledNavigation(delay, lockHistory, lockBackForwardList, duringLoad, isLocationChange)
, m_securityOrigin(securityOrigin)
, m_url(url)
, m_referrer(referrer)
, m_haveToldClient(false)
{
}
virtual void fire(Frame& frame) override
{
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
frame.loader().changeLocation(m_securityOrigin.get(), m_url, m_referrer, lockHistory(), lockBackForwardList(), false);
}
virtual void didStartTimer(Frame& frame, Timer<NavigationScheduler>& timer) override
{
if (m_haveToldClient)
return;
m_haveToldClient = true;
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
frame.loader().clientRedirected(m_url, delay(), currentTime() + timer.nextFireInterval(), lockBackForwardList());
}
virtual void didStopTimer(Frame& frame, bool newLoadInProgress) override
{
if (!m_haveToldClient)
return;
frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress);
}
SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); }
const URL& url() const { return m_url; }
String referrer() const { return m_referrer; }
private:
RefPtr<SecurityOrigin> m_securityOrigin;
URL m_url;
String m_referrer;
bool m_haveToldClient;
};
class ScheduledRedirect : public ScheduledURLNavigation {
public:
ScheduledRedirect(double delay, SecurityOrigin* securityOrigin, const URL& url, LockHistory lockHistory, LockBackForwardList lockBackForwardList)
: ScheduledURLNavigation(delay, securityOrigin, url, String(), lockHistory, lockBackForwardList, false, false)
{
clearUserGesture();
}
virtual bool shouldStartTimer(Frame& frame) override
{
return frame.loader().allAncestorsAreComplete();
}
virtual void fire(Frame& frame) override
{
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
bool refresh = equalIgnoringFragmentIdentifier(frame.document()->url(), url());
frame.loader().changeLocation(securityOrigin(), url(), referrer(), lockHistory(), lockBackForwardList(), refresh);
}
};
class ScheduledLocationChange : public ScheduledURLNavigation {
public:
ScheduledLocationChange(SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool duringLoad)
: ScheduledURLNavigation(0.0, securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, true) { }
};
class ScheduledRefresh : public ScheduledURLNavigation {
public:
ScheduledRefresh(SecurityOrigin* securityOrigin, const URL& url, const String& referrer)
: ScheduledURLNavigation(0.0, securityOrigin, url, referrer, LockHistory::Yes, LockBackForwardList::Yes, false, true)
{
}
virtual void fire(Frame& frame) override
{
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
frame.loader().changeLocation(securityOrigin(), url(), referrer(), lockHistory(), lockBackForwardList(), true);
}
};
class ScheduledHistoryNavigation : public ScheduledNavigation {
public:
explicit ScheduledHistoryNavigation(int historySteps)
: ScheduledNavigation(0, LockHistory::No, LockBackForwardList::No, false, true)
, m_historySteps(historySteps)
{
}
virtual void fire(Frame& frame) override
{
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
if (!m_historySteps) {
frame.loader().urlSelected(frame.document()->url(), "_self", 0, lockHistory(), lockBackForwardList(), MaybeSendReferrer);
return;
}
frame.page()->backForward().goBackOrForward(m_historySteps);
}
private:
int m_historySteps;
};
class ScheduledFormSubmission : public ScheduledNavigation {
public:
ScheduledFormSubmission(PassRefPtr<FormSubmission> submission, LockBackForwardList lockBackForwardList, bool duringLoad)
: ScheduledNavigation(0, submission->lockHistory(), lockBackForwardList, duringLoad, true)
, m_submission(submission)
, m_haveToldClient(false)
{
ASSERT(m_submission->state());
}
virtual void fire(Frame& frame) override
{
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
Document* requestingDocument = m_submission->state()->sourceDocument();
if (!requestingDocument->canNavigate(&frame))
return;
FrameLoadRequest frameRequest(requestingDocument->securityOrigin());
m_submission->populateFrameLoadRequest(frameRequest);
frame.loader().loadFrameRequest(frameRequest, lockHistory(), lockBackForwardList(), m_submission->event(), m_submission->state(), MaybeSendReferrer);
}
virtual void didStartTimer(Frame& frame, Timer<NavigationScheduler>& timer) override
{
if (m_haveToldClient)
return;
m_haveToldClient = true;
UserGestureIndicator gestureIndicator(wasUserGesture() ? DefinitelyProcessingUserGesture : DefinitelyNotProcessingUserGesture);
frame.loader().clientRedirected(m_submission->requestURL(), delay(), currentTime() + timer.nextFireInterval(), lockBackForwardList());
}
virtual void didStopTimer(Frame& frame, bool newLoadInProgress) override
{
if (!m_haveToldClient)
return;
frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress);
}
private:
RefPtr<FormSubmission> m_submission;
bool m_haveToldClient;
};
NavigationScheduler::NavigationScheduler(Frame& frame)
: m_frame(frame)
, m_timer(this, &NavigationScheduler::timerFired)
{
}
NavigationScheduler::~NavigationScheduler()
{
}
bool NavigationScheduler::redirectScheduledDuringLoad()
{
return m_redirect && m_redirect->wasDuringLoad();
}
bool NavigationScheduler::locationChangePending()
{
return m_redirect && m_redirect->isLocationChange();
}
void NavigationScheduler::clear()
{
if (m_timer.isActive())
InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
m_timer.stop();
m_redirect = nullptr;
}
inline bool NavigationScheduler::shouldScheduleNavigation() const
{
return m_frame.page();
}
inline bool NavigationScheduler::shouldScheduleNavigation(const URL& url) const
{
if (!shouldScheduleNavigation())
return false;
if (protocolIsJavaScript(url))
return true;
if (!url.isValid() && !m_frame.settings().allowNavigationToInvalidURL())
return false;
return NavigationDisablerForBeforeUnload::isNavigationAllowed();
}
void NavigationScheduler::scheduleRedirect(double delay, const URL& url)
{
if (!shouldScheduleNavigation(url))
return;
if (delay < 0 || delay > INT_MAX / 1000)
return;
if (url.isEmpty())
return;
if (!m_redirect || delay <= m_redirect->delay()) {
LockBackForwardList lockBackForwardList = delay <= 1 ? LockBackForwardList::Yes : LockBackForwardList::No;
schedule(std::make_unique<ScheduledRedirect>(delay, m_frame.document()->securityOrigin(), url, LockHistory::Yes, lockBackForwardList));
}
}
LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame)
{
if (!ScriptController::processingUserGesture() && targetFrame.loader().documentLoader() && !targetFrame.loader().documentLoader()->wasOnloadHandled())
return LockBackForwardList::Yes;
for (Frame* ancestor = targetFrame.tree().parent(); ancestor; ancestor = ancestor->tree().parent()) {
Document* document = ancestor->document();
if (!ancestor->loader().isComplete() || (document && document->processingLoadEvent()))
return LockBackForwardList::Yes;
}
return LockBackForwardList::No;
}
void NavigationScheduler::scheduleLocationChange(SecurityOrigin* securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList)
{
if (!shouldScheduleNavigation(url))
return;
if (lockBackForwardList == LockBackForwardList::No)
lockBackForwardList = mustLockBackForwardList(m_frame);
FrameLoader& loader = m_frame.loader();
URL parsedURL(ParsedURLString, url);
if (parsedURL.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(m_frame.document()->url(), parsedURL)) {
loader.changeLocation(securityOrigin, m_frame.document()->completeURL(url), referrer, lockHistory, lockBackForwardList);
return;
}
bool duringLoad = !loader.stateMachine().committedFirstRealDocumentLoad();
schedule(std::make_unique<ScheduledLocationChange>(securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad));
}
void NavigationScheduler::scheduleFormSubmission(PassRefPtr<FormSubmission> submission)
{
ASSERT(m_frame.page());
bool duringLoad = !m_frame.loader().stateMachine().committedFirstRealDocumentLoad();
LockBackForwardList lockBackForwardList = mustLockBackForwardList(m_frame);
if (lockBackForwardList == LockBackForwardList::No
&& (submission->state()->formSubmissionTrigger() == SubmittedByJavaScript && m_frame.tree().parent() && !ScriptController::processingUserGesture())) {
lockBackForwardList = LockBackForwardList::Yes;
}
schedule(std::make_unique<ScheduledFormSubmission>(submission, lockBackForwardList, duringLoad));
}
void NavigationScheduler::scheduleRefresh()
{
if (!shouldScheduleNavigation())
return;
const URL& url = m_frame.document()->url();
if (url.isEmpty())
return;
schedule(std::make_unique<ScheduledRefresh>(m_frame.document()->securityOrigin(), url, m_frame.loader().outgoingReferrer()));
}
void NavigationScheduler::scheduleHistoryNavigation(int steps)
{
if (!shouldScheduleNavigation())
return;
BackForwardController& backForward = m_frame.page()->backForward();
if (steps > backForward.forwardCount() || -steps > backForward.backCount()) {
cancel();
return;
}
schedule(std::make_unique<ScheduledHistoryNavigation>(steps));
}
void NavigationScheduler::timerFired(Timer<NavigationScheduler>&)
{
if (!m_frame.page())
return;
if (m_frame.page()->defersLoading()) {
InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
return;
}
Ref<Frame> protect(m_frame);
std::unique_ptr<ScheduledNavigation> redirect = WTF::move(m_redirect);
redirect->fire(m_frame);
InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
}
void NavigationScheduler::schedule(std::unique_ptr<ScheduledNavigation> redirect)
{
ASSERT(m_frame.page());
Ref<Frame> protect(m_frame);
if (redirect->wasDuringLoad()) {
if (DocumentLoader* provisionalDocumentLoader = m_frame.loader().provisionalDocumentLoader())
provisionalDocumentLoader->stopLoading();
m_frame.loader().stopLoading(UnloadEventPolicyUnloadAndPageHide);
}
cancel();
m_redirect = WTF::move(redirect);
if (!m_frame.loader().isComplete() && m_redirect->isLocationChange())
m_frame.loader().completed();
if (!m_frame.page())
return;
startTimer();
}
void NavigationScheduler::startTimer()
{
if (!m_redirect)
return;
ASSERT(m_frame.page());
if (m_timer.isActive())
return;
if (!m_redirect->shouldStartTimer(m_frame))
return;
double delay = m_redirect->delay();
m_timer.startOneShot(delay);
InspectorInstrumentation::frameScheduledNavigation(m_frame, delay);
m_redirect->didStartTimer(m_frame, m_timer); }
void NavigationScheduler::cancel(bool newLoadInProgress)
{
if (m_timer.isActive())
InspectorInstrumentation::frameClearedScheduledNavigation(m_frame);
m_timer.stop();
if (std::unique_ptr<ScheduledNavigation> redirect = WTF::move(m_redirect))
redirect->didStopTimer(m_frame, newLoadInProgress);
}
}