| 1 | /* |
| 2 | * Copyright (C) 2006-2017 Apple Inc. All rights reserved. |
| 3 | * 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 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" |
| 33 | #include "NavigationScheduler.h" |
| 34 | |
| 35 | #include "BackForwardController.h" |
| 36 | #include "CommonVM.h" |
| 37 | #include "DOMWindow.h" |
| 38 | #include "DocumentLoader.h" |
| 39 | #include "Event.h" |
| 40 | #include "FormState.h" |
| 41 | #include "FormSubmission.h" |
| 42 | #include "Frame.h" |
| 43 | #include "FrameLoadRequest.h" |
| 44 | #include "FrameLoader.h" |
| 45 | #include "FrameLoaderStateMachine.h" |
| 46 | #include "HTMLFormElement.h" |
| 47 | #include "HTMLFrameOwnerElement.h" |
| 48 | #include "HistoryItem.h" |
| 49 | #include "InspectorInstrumentation.h" |
| 50 | #include "Logging.h" |
| 51 | #include "NavigationDisabler.h" |
| 52 | #include "Page.h" |
| 53 | #include "PolicyChecker.h" |
| 54 | #include "ScriptController.h" |
| 55 | #include "UserGestureIndicator.h" |
| 56 | #include <wtf/Ref.h> |
| 57 | |
| 58 | namespace WebCore { |
| 59 | |
| 60 | unsigned NavigationDisabler::s_globalNavigationDisableCount = 0; |
| 61 | |
| 62 | class ScheduledNavigation { |
| 63 | WTF_MAKE_NONCOPYABLE(ScheduledNavigation); WTF_MAKE_FAST_ALLOCATED; |
| 64 | public: |
| 65 | ScheduledNavigation(double delay, LockHistory lockHistory, LockBackForwardList lockBackForwardList, bool wasDuringLoad, bool isLocationChange) |
| 66 | : m_delay(delay) |
| 67 | , m_lockHistory(lockHistory) |
| 68 | , m_lockBackForwardList(lockBackForwardList) |
| 69 | , m_wasDuringLoad(wasDuringLoad) |
| 70 | , m_isLocationChange(isLocationChange) |
| 71 | , m_userGestureToForward(UserGestureIndicator::currentUserGesture()) |
| 72 | { |
| 73 | } |
| 74 | 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 | { |
| 83 | if (auto* frame = lexicalFrameFromCommonVM()) { |
| 84 | if (frame->isMainFrame()) |
| 85 | m_initiatedByMainFrame = InitiatedByMainFrame::Yes; |
| 86 | } |
| 87 | } |
| 88 | virtual ~ScheduledNavigation() = default; |
| 89 | |
| 90 | virtual void fire(Frame&) = 0; |
| 91 | |
| 92 | virtual bool shouldStartTimer(Frame&) { return true; } |
| 93 | virtual void didStartTimer(Frame&, Timer&) { } |
| 94 | virtual void didStopTimer(Frame&, NewLoadInProgress) { } |
| 95 | |
| 96 | double delay() const { return m_delay; } |
| 97 | LockHistory lockHistory() const { return m_lockHistory; } |
| 98 | LockBackForwardList lockBackForwardList() const { return m_lockBackForwardList; } |
| 99 | bool wasDuringLoad() const { return m_wasDuringLoad; } |
| 100 | bool isLocationChange() const { return m_isLocationChange; } |
| 101 | UserGestureToken* userGestureToForward() const { return m_userGestureToForward.get(); } |
| 102 | |
| 103 | protected: |
| 104 | void clearUserGesture() { m_userGestureToForward = nullptr; } |
| 105 | ShouldOpenExternalURLsPolicy shouldOpenExternalURLs() const { return m_shouldOpenExternalURLsPolicy; } |
| 106 | InitiatedByMainFrame initiatedByMainFrame() const { return m_initiatedByMainFrame; }; |
| 107 | |
| 108 | private: |
| 109 | double m_delay; |
| 110 | LockHistory m_lockHistory; |
| 111 | LockBackForwardList m_lockBackForwardList; |
| 112 | bool m_wasDuringLoad; |
| 113 | bool m_isLocationChange; |
| 114 | RefPtr<UserGestureToken> m_userGestureToForward; |
| 115 | ShouldOpenExternalURLsPolicy m_shouldOpenExternalURLsPolicy { ShouldOpenExternalURLsPolicy::ShouldNotAllow }; |
| 116 | InitiatedByMainFrame m_initiatedByMainFrame { InitiatedByMainFrame::Unknown }; |
| 117 | }; |
| 118 | |
| 119 | class ScheduledURLNavigation : public ScheduledNavigation { |
| 120 | protected: |
| 121 | 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()) |
| 123 | , m_initiatingDocument { makeRef(initiatingDocument) } |
| 124 | , m_securityOrigin{ makeRefPtr(securityOrigin) } |
| 125 | , m_url { url } |
| 126 | , m_referrer { referrer } |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | void didStartTimer(Frame& frame, Timer& timer) override |
| 131 | { |
| 132 | if (m_haveToldClient) |
| 133 | return; |
| 134 | m_haveToldClient = true; |
| 135 | |
| 136 | UserGestureIndicator gestureIndicator(userGestureToForward()); |
| 137 | frame.loader().clientRedirected(m_url, delay(), WallTime::now() + timer.nextFireInterval(), lockBackForwardList()); |
| 138 | } |
| 139 | |
| 140 | void didStopTimer(Frame& frame, NewLoadInProgress newLoadInProgress) override |
| 141 | { |
| 142 | if (!m_haveToldClient) |
| 143 | return; |
| 144 | |
| 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(). |
| 151 | frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress); |
| 152 | } |
| 153 | |
| 154 | Document& initiatingDocument() { return m_initiatingDocument.get(); } |
| 155 | SecurityOrigin* securityOrigin() const { return m_securityOrigin.get(); } |
| 156 | const URL& url() const { return m_url; } |
| 157 | String referrer() const { return m_referrer; } |
| 158 | |
| 159 | private: |
| 160 | Ref<Document> m_initiatingDocument; |
| 161 | RefPtr<SecurityOrigin> m_securityOrigin; |
| 162 | URL m_url; |
| 163 | String m_referrer; |
| 164 | bool m_haveToldClient { false }; |
| 165 | }; |
| 166 | |
| 167 | class ScheduledRedirect : public ScheduledURLNavigation { |
| 168 | public: |
| 169 | ScheduledRedirect(Document& initiatingDocument, double delay, SecurityOrigin* securityOrigin, const URL& url, LockHistory lockHistory, LockBackForwardList lockBackForwardList) |
| 170 | : ScheduledURLNavigation(initiatingDocument, delay, securityOrigin, url, String(), lockHistory, lockBackForwardList, false, false) |
| 171 | { |
| 172 | clearUserGesture(); |
| 173 | } |
| 174 | |
| 175 | bool shouldStartTimer(Frame& frame) override |
| 176 | { |
| 177 | return frame.loader().allAncestorsAreComplete(); |
| 178 | } |
| 179 | |
| 180 | void fire(Frame& frame) override |
| 181 | { |
| 182 | UserGestureIndicator gestureIndicator { userGestureToForward() }; |
| 183 | |
| 184 | bool refresh = equalIgnoringFragmentIdentifier(frame.document()->url(), url()); |
| 185 | ResourceRequest resourceRequest { url(), referrer(), refresh ? ResourceRequestCachePolicy::ReloadIgnoringCacheData : ResourceRequestCachePolicy::UseProtocolCachePolicy }; |
| 186 | if (initiatedByMainFrame() == InitiatedByMainFrame::Yes) |
| 187 | resourceRequest.setRequester(ResourceRequest::Requester::Main); |
| 188 | FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self" , lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() }; |
| 189 | |
| 190 | frame.loader().changeLocation(WTFMove(frameLoadRequest)); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | class ScheduledLocationChange : public ScheduledURLNavigation { |
| 195 | public: |
| 196 | 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 | } |
| 207 | |
| 208 | void fire(Frame& frame) override |
| 209 | { |
| 210 | UserGestureIndicator gestureIndicator { userGestureToForward() }; |
| 211 | |
| 212 | ResourceRequest resourceRequest { url(), referrer(), ResourceRequestCachePolicy::UseProtocolCachePolicy }; |
| 213 | FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self" , lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() }; |
| 214 | |
| 215 | auto completionHandler = WTFMove(m_completionHandler); |
| 216 | frame.loader().changeLocation(WTFMove(frameLoadRequest)); |
| 217 | completionHandler(); |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | CompletionHandler<void()> m_completionHandler; |
| 222 | }; |
| 223 | |
| 224 | class ScheduledRefresh : public ScheduledURLNavigation { |
| 225 | public: |
| 226 | ScheduledRefresh(Document& initiatingDocument, SecurityOrigin* securityOrigin, const URL& url, const String& referrer) |
| 227 | : ScheduledURLNavigation(initiatingDocument, 0.0, securityOrigin, url, referrer, LockHistory::Yes, LockBackForwardList::Yes, false, true) |
| 228 | { |
| 229 | } |
| 230 | |
| 231 | void fire(Frame& frame) override |
| 232 | { |
| 233 | UserGestureIndicator gestureIndicator { userGestureToForward() }; |
| 234 | |
| 235 | ResourceRequest resourceRequest { url(), referrer(), ResourceRequestCachePolicy::ReloadIgnoringCacheData }; |
| 236 | FrameLoadRequest frameLoadRequest { initiatingDocument(), *securityOrigin(), resourceRequest, "_self" , lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() }; |
| 237 | |
| 238 | frame.loader().changeLocation(WTFMove(frameLoadRequest)); |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | class ScheduledHistoryNavigation : public ScheduledNavigation { |
| 243 | public: |
| 244 | explicit ScheduledHistoryNavigation(int historySteps) |
| 245 | : ScheduledNavigation(0, LockHistory::No, LockBackForwardList::No, false, true) |
| 246 | , m_historySteps(historySteps) |
| 247 | { |
| 248 | } |
| 249 | |
| 250 | void fire(Frame& frame) override |
| 251 | { |
| 252 | UserGestureIndicator gestureIndicator(userGestureToForward()); |
| 253 | |
| 254 | if (!m_historySteps) { |
| 255 | // Special case for go(0) from a frame -> reload only the frame |
| 256 | // To follow Firefox and IE's behavior, history reload can only navigate the self frame. |
| 257 | frame.loader().urlSelected(frame.document()->url(), "_self" , 0, lockHistory(), lockBackForwardList(), MaybeSendReferrer, shouldOpenExternalURLs()); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // 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. |
| 263 | frame.page()->backForward().goBackOrForward(m_historySteps); |
| 264 | } |
| 265 | |
| 266 | private: |
| 267 | int m_historySteps; |
| 268 | }; |
| 269 | |
| 270 | class ScheduledFormSubmission : public ScheduledNavigation { |
| 271 | public: |
| 272 | 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)) |
| 275 | { |
| 276 | } |
| 277 | |
| 278 | void fire(Frame& frame) override |
| 279 | { |
| 280 | UserGestureIndicator gestureIndicator(userGestureToForward()); |
| 281 | |
| 282 | // The submitForm function will find a target frame before using the redirection timer. |
| 283 | // Now that the timer has fired, we need to repeat the security check which normally is done when |
| 284 | // selecting a target, in case conditions have changed. Other code paths avoid this by targeting |
| 285 | // without leaving a time window. If we fail the check just silently drop the form submission. |
| 286 | auto& requestingDocument = m_submission->state().sourceDocument(); |
| 287 | if (!requestingDocument.canNavigate(&frame)) |
| 288 | return; |
| 289 | FrameLoadRequest frameLoadRequest { requestingDocument, requestingDocument.securityOrigin(), { }, { }, lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() }; |
| 290 | m_submission->populateFrameLoadRequest(frameLoadRequest); |
| 291 | frame.loader().loadFrameRequest(WTFMove(frameLoadRequest), m_submission->event(), m_submission->takeState()); |
| 292 | } |
| 293 | |
| 294 | void didStartTimer(Frame& frame, Timer& timer) override |
| 295 | { |
| 296 | if (m_haveToldClient) |
| 297 | return; |
| 298 | m_haveToldClient = true; |
| 299 | |
| 300 | UserGestureIndicator gestureIndicator(userGestureToForward()); |
| 301 | frame.loader().clientRedirected(m_submission->requestURL(), delay(), WallTime::now() + timer.nextFireInterval(), lockBackForwardList()); |
| 302 | } |
| 303 | |
| 304 | void didStopTimer(Frame& frame, NewLoadInProgress newLoadInProgress) override |
| 305 | { |
| 306 | if (!m_haveToldClient) |
| 307 | return; |
| 308 | |
| 309 | // Do not set a UserGestureIndicator because |
| 310 | // clientRedirectCancelledOrFinished() is also called from many places |
| 311 | // inside FrameLoader, where the gesture state is not set and is in |
| 312 | // fact unavailable. We need to be consistent with them, otherwise the |
| 313 | // gesture state will sometimes be set and sometimes not within |
| 314 | // dispatchDidCancelClientRedirect(). |
| 315 | frame.loader().clientRedirectCancelledOrFinished(newLoadInProgress); |
| 316 | } |
| 317 | |
| 318 | private: |
| 319 | Ref<FormSubmission> m_submission; |
| 320 | bool m_haveToldClient { false }; |
| 321 | }; |
| 322 | |
| 323 | class ScheduledPageBlock final : public ScheduledNavigation { |
| 324 | public: |
| 325 | ScheduledPageBlock(Document& originDocument) |
| 326 | : ScheduledNavigation(0, LockHistory::Yes, LockBackForwardList::Yes, false, false) |
| 327 | , m_originDocument(originDocument) |
| 328 | { |
| 329 | } |
| 330 | |
| 331 | void fire(Frame& frame) override |
| 332 | { |
| 333 | UserGestureIndicator gestureIndicator { userGestureToForward() }; |
| 334 | |
| 335 | ResourceResponse replacementResponse { m_originDocument.url(), "text/plain"_s , 0, "UTF-8"_s }; |
| 336 | SubstituteData replacementData { SharedBuffer::create(), m_originDocument.url(), replacementResponse, SubstituteData::SessionHistoryVisibility::Hidden }; |
| 337 | |
| 338 | ResourceRequest resourceRequest { m_originDocument.url(), emptyString(), ResourceRequestCachePolicy::ReloadIgnoringCacheData }; |
| 339 | FrameLoadRequest frameLoadRequest { m_originDocument, m_originDocument.securityOrigin(), resourceRequest, { }, lockHistory(), lockBackForwardList(), MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, shouldOpenExternalURLs(), initiatedByMainFrame() }; |
| 340 | frameLoadRequest.setSubstituteData(replacementData); |
| 341 | frame.loader().load(WTFMove(frameLoadRequest)); |
| 342 | } |
| 343 | |
| 344 | private: |
| 345 | Document& m_originDocument; |
| 346 | }; |
| 347 | |
| 348 | NavigationScheduler::NavigationScheduler(Frame& frame) |
| 349 | : m_frame(frame) |
| 350 | , m_timer(*this, &NavigationScheduler::timerFired) |
| 351 | { |
| 352 | } |
| 353 | |
| 354 | NavigationScheduler::~NavigationScheduler() = default; |
| 355 | |
| 356 | bool NavigationScheduler::redirectScheduledDuringLoad() |
| 357 | { |
| 358 | return m_redirect && m_redirect->wasDuringLoad(); |
| 359 | } |
| 360 | |
| 361 | bool NavigationScheduler::locationChangePending() |
| 362 | { |
| 363 | return m_redirect && m_redirect->isLocationChange(); |
| 364 | } |
| 365 | |
| 366 | void NavigationScheduler::clear() |
| 367 | { |
| 368 | if (m_timer.isActive()) |
| 369 | InspectorInstrumentation::frameClearedScheduledNavigation(m_frame); |
| 370 | m_timer.stop(); |
| 371 | m_redirect = nullptr; |
| 372 | } |
| 373 | |
| 374 | inline bool NavigationScheduler::shouldScheduleNavigation() const |
| 375 | { |
| 376 | return m_frame.page(); |
| 377 | } |
| 378 | |
| 379 | inline bool NavigationScheduler::shouldScheduleNavigation(const URL& url) const |
| 380 | { |
| 381 | if (!shouldScheduleNavigation()) |
| 382 | return false; |
| 383 | if (WTF::protocolIsJavaScript(url)) |
| 384 | return true; |
| 385 | return NavigationDisabler::isNavigationAllowed(m_frame); |
| 386 | } |
| 387 | |
| 388 | void NavigationScheduler::scheduleRedirect(Document& initiatingDocument, double delay, const URL& url) |
| 389 | { |
| 390 | if (!shouldScheduleNavigation(url)) |
| 391 | return; |
| 392 | if (delay < 0 || delay > INT_MAX / 1000) |
| 393 | return; |
| 394 | if (url.isEmpty()) |
| 395 | return; |
| 396 | |
| 397 | // We want a new back/forward list item if the refresh timeout is > 1 second. |
| 398 | if (!m_redirect || delay <= m_redirect->delay()) { |
| 399 | auto lockBackForwardList = delay <= 1 ? LockBackForwardList::Yes : LockBackForwardList::No; |
| 400 | schedule(std::make_unique<ScheduledRedirect>(initiatingDocument, delay, &m_frame.document()->securityOrigin(), url, LockHistory::Yes, lockBackForwardList)); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | LockBackForwardList NavigationScheduler::mustLockBackForwardList(Frame& targetFrame) |
| 405 | { |
| 406 | // Non-user navigation before the page has finished firing onload should not create a new back/forward item. |
| 407 | // See https://webkit.org/b/42861 for the original motivation for this. |
| 408 | if (!UserGestureIndicator::processingUserGesture() && targetFrame.loader().documentLoader() && !targetFrame.loader().documentLoader()->wasOnloadDispatched()) |
| 409 | return LockBackForwardList::Yes; |
| 410 | |
| 411 | // Navigation of a subframe during loading of an ancestor frame does not create a new back/forward item. |
| 412 | // The definition of "during load" is any time before all handlers for the load event have been run. |
| 413 | // See https://bugs.webkit.org/show_bug.cgi?id=14957 for the original motivation for this. |
| 414 | for (Frame* ancestor = targetFrame.tree().parent(); ancestor; ancestor = ancestor->tree().parent()) { |
| 415 | Document* document = ancestor->document(); |
| 416 | if (!ancestor->loader().isComplete() || (document && document->processingLoadEvent())) |
| 417 | return LockBackForwardList::Yes; |
| 418 | } |
| 419 | return LockBackForwardList::No; |
| 420 | } |
| 421 | |
| 422 | void NavigationScheduler::scheduleLocationChange(Document& initiatingDocument, SecurityOrigin& securityOrigin, const URL& url, const String& referrer, LockHistory lockHistory, LockBackForwardList lockBackForwardList, CompletionHandler<void()>&& completionHandler) |
| 423 | { |
| 424 | if (!shouldScheduleNavigation(url)) |
| 425 | return completionHandler(); |
| 426 | |
| 427 | if (lockBackForwardList == LockBackForwardList::No) |
| 428 | lockBackForwardList = mustLockBackForwardList(m_frame); |
| 429 | |
| 430 | FrameLoader& loader = m_frame.loader(); |
| 431 | |
| 432 | // If the URL we're going to navigate to is the same as the current one, except for the |
| 433 | // fragment part, we don't need to schedule the location change. |
| 434 | if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(m_frame.document()->url(), url)) { |
| 435 | ResourceRequest resourceRequest { m_frame.document()->completeURL(url), referrer, ResourceRequestCachePolicy::UseProtocolCachePolicy }; |
| 436 | auto* frame = lexicalFrameFromCommonVM(); |
| 437 | auto initiatedByMainFrame = frame && frame->isMainFrame() ? InitiatedByMainFrame::Yes : InitiatedByMainFrame::Unknown; |
| 438 | |
| 439 | FrameLoadRequest frameLoadRequest { initiatingDocument, securityOrigin, resourceRequest, "_self"_s , lockHistory, lockBackForwardList, MaybeSendReferrer, AllowNavigationToInvalidURL::No, NewFrameOpenerPolicy::Allow, initiatingDocument.shouldOpenExternalURLsPolicyToPropagate(), initiatedByMainFrame }; |
| 440 | loader.changeLocation(WTFMove(frameLoadRequest)); |
| 441 | return completionHandler(); |
| 442 | } |
| 443 | |
| 444 | // Handle a location change of a page with no document as a special case. |
| 445 | // This may happen when a frame changes the location of another frame. |
| 446 | bool duringLoad = !loader.stateMachine().committedFirstRealDocumentLoad(); |
| 447 | |
| 448 | schedule(std::make_unique<ScheduledLocationChange>(initiatingDocument, &securityOrigin, url, referrer, lockHistory, lockBackForwardList, duringLoad, WTFMove(completionHandler))); |
| 449 | } |
| 450 | |
| 451 | void NavigationScheduler::scheduleFormSubmission(Ref<FormSubmission>&& submission) |
| 452 | { |
| 453 | ASSERT(m_frame.page()); |
| 454 | |
| 455 | // FIXME: Do we need special handling for form submissions where the URL is the same |
| 456 | // as the current one except for the fragment part? See scheduleLocationChange above. |
| 457 | |
| 458 | // Handle a location change of a page with no document as a special case. |
| 459 | // This may happen when a frame changes the location of another frame. |
| 460 | bool duringLoad = !m_frame.loader().stateMachine().committedFirstRealDocumentLoad(); |
| 461 | |
| 462 | // If this is a child frame and the form submission was triggered by a script, lock the back/forward list |
| 463 | // to match IE and Opera. |
| 464 | // See https://bugs.webkit.org/show_bug.cgi?id=32383 for the original motivation for this. |
| 465 | LockBackForwardList lockBackForwardList = mustLockBackForwardList(m_frame); |
| 466 | if (lockBackForwardList == LockBackForwardList::No |
| 467 | && (submission->state().formSubmissionTrigger() == SubmittedByJavaScript && m_frame.tree().parent() && !UserGestureIndicator::processingUserGesture())) { |
| 468 | lockBackForwardList = LockBackForwardList::Yes; |
| 469 | } |
| 470 | |
| 471 | schedule(std::make_unique<ScheduledFormSubmission>(WTFMove(submission), lockBackForwardList, duringLoad)); |
| 472 | } |
| 473 | |
| 474 | void NavigationScheduler::scheduleRefresh(Document& initiatingDocument) |
| 475 | { |
| 476 | if (!shouldScheduleNavigation()) |
| 477 | return; |
| 478 | const URL& url = m_frame.document()->url(); |
| 479 | if (url.isEmpty()) |
| 480 | return; |
| 481 | |
| 482 | schedule(std::make_unique<ScheduledRefresh>(initiatingDocument, &m_frame.document()->securityOrigin(), url, m_frame.loader().outgoingReferrer())); |
| 483 | } |
| 484 | |
| 485 | void NavigationScheduler::scheduleHistoryNavigation(int steps) |
| 486 | { |
| 487 | LOG(History, "NavigationScheduler %p scheduleHistoryNavigation(%d) - shouldSchedule %d" , this, steps, shouldScheduleNavigation()); |
| 488 | if (!shouldScheduleNavigation()) |
| 489 | return; |
| 490 | |
| 491 | // Invalid history navigations (such as history.forward() during a new load) have the side effect of cancelling any scheduled |
| 492 | // redirects. We also avoid the possibility of cancelling the current load by avoiding the scheduled redirection altogether. |
| 493 | BackForwardController& backForward = m_frame.page()->backForward(); |
| 494 | if ((steps > 0 && static_cast<unsigned>(steps) > backForward.forwardCount()) |
| 495 | || (steps < 0 && static_cast<unsigned>(-steps) > backForward.backCount())) { |
| 496 | cancel(); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | // In all other cases, schedule the history traversal to occur asynchronously. |
| 501 | schedule(std::make_unique<ScheduledHistoryNavigation>(steps)); |
| 502 | } |
| 503 | |
| 504 | void NavigationScheduler::schedulePageBlock(Document& originDocument) |
| 505 | { |
| 506 | if (shouldScheduleNavigation()) |
| 507 | schedule(std::make_unique<ScheduledPageBlock>(originDocument)); |
| 508 | } |
| 509 | |
| 510 | void NavigationScheduler::timerFired() |
| 511 | { |
| 512 | if (!m_frame.page()) |
| 513 | return; |
| 514 | if (m_frame.page()->defersLoading()) { |
| 515 | InspectorInstrumentation::frameClearedScheduledNavigation(m_frame); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | Ref<Frame> protect(m_frame); |
| 520 | |
| 521 | std::unique_ptr<ScheduledNavigation> redirect = WTFMove(m_redirect); |
| 522 | LOG(History, "NavigationScheduler %p timerFired - firing redirect %p" , this, redirect.get()); |
| 523 | |
| 524 | redirect->fire(m_frame); |
| 525 | InspectorInstrumentation::frameClearedScheduledNavigation(m_frame); |
| 526 | } |
| 527 | |
| 528 | void NavigationScheduler::schedule(std::unique_ptr<ScheduledNavigation> redirect) |
| 529 | { |
| 530 | ASSERT(m_frame.page()); |
| 531 | |
| 532 | Ref<Frame> protect(m_frame); |
| 533 | |
| 534 | // If a redirect was scheduled during a load, then stop the current load. |
| 535 | // Otherwise when the current load transitions from a provisional to a |
| 536 | // committed state, pending redirects may be cancelled. |
| 537 | if (redirect->wasDuringLoad()) { |
| 538 | if (DocumentLoader* provisionalDocumentLoader = m_frame.loader().provisionalDocumentLoader()) |
| 539 | provisionalDocumentLoader->stopLoading(); |
| 540 | m_frame.loader().stopLoading(UnloadEventPolicyUnloadAndPageHide); |
| 541 | } |
| 542 | |
| 543 | cancel(); |
| 544 | m_redirect = WTFMove(redirect); |
| 545 | |
| 546 | if (!m_frame.loader().isComplete() && m_redirect->isLocationChange()) |
| 547 | m_frame.loader().completed(); |
| 548 | |
| 549 | if (!m_frame.page()) |
| 550 | return; |
| 551 | |
| 552 | startTimer(); |
| 553 | } |
| 554 | |
| 555 | void NavigationScheduler::startTimer() |
| 556 | { |
| 557 | if (!m_redirect) |
| 558 | return; |
| 559 | |
| 560 | ASSERT(m_frame.page()); |
| 561 | if (m_timer.isActive()) |
| 562 | return; |
| 563 | if (!m_redirect->shouldStartTimer(m_frame)) |
| 564 | return; |
| 565 | |
| 566 | Seconds delay = 1_s * m_redirect->delay(); |
| 567 | m_timer.startOneShot(delay); |
| 568 | InspectorInstrumentation::frameScheduledNavigation(m_frame, delay); |
| 569 | m_redirect->didStartTimer(m_frame, m_timer); // m_redirect may be null on return (e.g. the client canceled the load) |
| 570 | } |
| 571 | |
| 572 | void NavigationScheduler::cancel(NewLoadInProgress newLoadInProgress) |
| 573 | { |
| 574 | LOG(History, "NavigationScheduler %p cancel(newLoadInProgress=%d)" , this, newLoadInProgress == NewLoadInProgress::Yes); |
| 575 | |
| 576 | if (m_timer.isActive()) |
| 577 | InspectorInstrumentation::frameClearedScheduledNavigation(m_frame); |
| 578 | m_timer.stop(); |
| 579 | |
| 580 | if (auto redirect = WTFMove(m_redirect)) |
| 581 | redirect->didStopTimer(m_frame, newLoadInProgress); |
| 582 | } |
| 583 | |
| 584 | } // namespace WebCore |
| 585 | |