1/*
2 * Copyright (C) 2006-2018 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) 2008 Alp Toker <alp@atoker.com>
6 * Copyright (C) Research In Motion Limited 2009. All rights reserved.
7 * Copyright (C) 2011 Kris Jordan <krisjordan@gmail.com>
8 * Copyright (C) 2011 Google Inc. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
20 * its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include "config.h"
36#include "FrameLoader.h"
37
38#include "AXObjectCache.h"
39#include "ApplicationCacheHost.h"
40#include "BackForwardController.h"
41#include "BeforeUnloadEvent.h"
42#include "CachedPage.h"
43#include "CachedResourceLoader.h"
44#include "Chrome.h"
45#include "ChromeClient.h"
46#include "CommonVM.h"
47#include "ContentFilter.h"
48#include "ContentRuleListResults.h"
49#include "ContentSecurityPolicy.h"
50#include "DOMWindow.h"
51#include "DatabaseManager.h"
52#include "DiagnosticLoggingClient.h"
53#include "DiagnosticLoggingKeys.h"
54#include "Document.h"
55#include "DocumentLoader.h"
56#include "Editor.h"
57#include "EditorClient.h"
58#include "Element.h"
59#include "Event.h"
60#include "EventHandler.h"
61#include "EventNames.h"
62#include "FloatRect.h"
63#include "FormState.h"
64#include "FormSubmission.h"
65#include "Frame.h"
66#include "FrameLoadRequest.h"
67#include "FrameLoaderClient.h"
68#include "FrameNetworkingContext.h"
69#include "FrameTree.h"
70#include "FrameView.h"
71#include "GCController.h"
72#include "HTMLFormElement.h"
73#include "HTMLInputElement.h"
74#include "HTMLNames.h"
75#include "HTMLObjectElement.h"
76#include "HTMLParserIdioms.h"
77#include "HTTPHeaderNames.h"
78#include "HTTPParsers.h"
79#include "HistoryController.h"
80#include "HistoryItem.h"
81#include "IgnoreOpensDuringUnloadCountIncrementer.h"
82#include "InspectorController.h"
83#include "InspectorInstrumentation.h"
84#include "LinkLoader.h"
85#include "LoadTiming.h"
86#include "LoaderStrategy.h"
87#include "Logging.h"
88#include "MemoryCache.h"
89#include "MemoryRelease.h"
90#include "NavigationDisabler.h"
91#include "NavigationScheduler.h"
92#include "Node.h"
93#include "Page.h"
94#include "PageCache.h"
95#include "PageTransitionEvent.h"
96#include "PerformanceLogging.h"
97#include "PlatformStrategies.h"
98#include "PluginData.h"
99#include "PluginDocument.h"
100#include "PolicyChecker.h"
101#include "ProgressTracker.h"
102#include "ResourceHandle.h"
103#include "ResourceLoadInfo.h"
104#include "ResourceLoadObserver.h"
105#include "ResourceRequest.h"
106#include "SVGDocument.h"
107#include "SVGLocatable.h"
108#include "SVGNames.h"
109#include "SVGViewElement.h"
110#include "SVGViewSpec.h"
111#include "ScriptController.h"
112#include "ScriptSourceCode.h"
113#include "ScrollAnimator.h"
114#include "SecurityOrigin.h"
115#include "SecurityPolicy.h"
116#include "SegmentedString.h"
117#include "SerializedScriptValue.h"
118#include "Settings.h"
119#include "ShouldTreatAsContinuingLoad.h"
120#include "SubframeLoader.h"
121#include "SubresourceLoader.h"
122#include "TextResourceDecoder.h"
123#include "UserContentController.h"
124#include "UserGestureIndicator.h"
125#include "WindowFeatures.h"
126#include "XMLDocumentParser.h"
127#include <dom/ScriptDisallowedScope.h>
128#include <wtf/CompletionHandler.h>
129#include <wtf/URL.h>
130#include <wtf/Ref.h>
131#include <wtf/SetForScope.h>
132#include <wtf/StdLibExtras.h>
133#include <wtf/SystemTracing.h>
134#include <wtf/text/CString.h>
135#include <wtf/text/WTFString.h>
136
137#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
138#include "Archive.h"
139#endif
140
141#if ENABLE(DATA_DETECTION)
142#include "DataDetection.h"
143#endif
144
145#if PLATFORM(IOS_FAMILY)
146#include "DocumentType.h"
147#include "ResourceLoader.h"
148#include "RuntimeApplicationChecks.h"
149#endif
150
151#define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(isAlwaysOnLoggingAllowed(), Network, "%p - FrameLoader::" fmt, this, ##__VA_ARGS__)
152
153namespace WebCore {
154
155using namespace HTMLNames;
156using namespace SVGNames;
157
158static const char defaultAcceptHeader[] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
159
160bool isBackForwardLoadType(FrameLoadType type)
161{
162 switch (type) {
163 case FrameLoadType::Standard:
164 case FrameLoadType::Reload:
165 case FrameLoadType::ReloadFromOrigin:
166 case FrameLoadType::ReloadExpiredOnly:
167 case FrameLoadType::Same:
168 case FrameLoadType::RedirectWithLockedBackForwardList:
169 case FrameLoadType::Replace:
170 return false;
171 case FrameLoadType::Back:
172 case FrameLoadType::Forward:
173 case FrameLoadType::IndexedBackForward:
174 return true;
175 }
176 ASSERT_NOT_REACHED();
177 return false;
178}
179
180bool isReload(FrameLoadType type)
181{
182 switch (type) {
183 case FrameLoadType::Reload:
184 case FrameLoadType::ReloadFromOrigin:
185 case FrameLoadType::ReloadExpiredOnly:
186 return true;
187 case FrameLoadType::Standard:
188 case FrameLoadType::Same:
189 case FrameLoadType::RedirectWithLockedBackForwardList:
190 case FrameLoadType::Replace:
191 case FrameLoadType::Back:
192 case FrameLoadType::Forward:
193 case FrameLoadType::IndexedBackForward:
194 return false;
195 }
196 ASSERT_NOT_REACHED();
197 return false;
198}
199
200// This is not in the FrameLoader class to emphasize that it does not depend on
201// private FrameLoader data, and to avoid increasing the number of public functions
202// with access to private data. Since only this .cpp file needs it, making it
203// non-member lets us exclude it from the header file, thus keeping FrameLoader.h's
204// API simpler.
205//
206static bool isDocumentSandboxed(Frame& frame, SandboxFlags mask)
207{
208 return frame.document() && frame.document()->isSandboxed(mask);
209}
210
211struct ForbidPromptsScope {
212 ForbidPromptsScope(Page* page) : m_page(page)
213 {
214 if (!m_page)
215 return;
216 m_page->forbidPrompts();
217 }
218
219 ~ForbidPromptsScope()
220 {
221 if (!m_page)
222 return;
223 m_page->allowPrompts();
224 }
225
226 Page* m_page;
227};
228
229class FrameLoader::FrameProgressTracker {
230 WTF_MAKE_FAST_ALLOCATED;
231public:
232 explicit FrameProgressTracker(Frame& frame)
233 : m_frame(frame)
234 , m_inProgress(false)
235 {
236 }
237
238 ~FrameProgressTracker()
239 {
240 if (m_inProgress && m_frame.page())
241 m_frame.page()->progress().progressCompleted(m_frame);
242 }
243
244 void progressStarted()
245 {
246 ASSERT(m_frame.page());
247 if (!m_inProgress)
248 m_frame.page()->progress().progressStarted(m_frame);
249 m_inProgress = true;
250 }
251
252 void progressCompleted()
253 {
254 ASSERT(m_inProgress);
255 ASSERT(m_frame.page());
256 m_inProgress = false;
257 m_frame.page()->progress().progressCompleted(m_frame);
258
259 if (auto pageID = m_frame.loader().client().pageID())
260 platformStrategies()->loaderStrategy()->pageLoadCompleted(pageID.value());
261 }
262
263private:
264 Frame& m_frame;
265 bool m_inProgress;
266};
267
268FrameLoader::FrameLoader(Frame& frame, FrameLoaderClient& client)
269 : m_frame(frame)
270 , m_client(client)
271 , m_policyChecker(std::make_unique<PolicyChecker>(frame))
272 , m_history(std::make_unique<HistoryController>(frame))
273 , m_notifier(frame)
274 , m_subframeLoader(std::make_unique<SubframeLoader>(frame))
275 , m_mixedContentChecker(frame)
276 , m_state(FrameStateProvisional)
277 , m_loadType(FrameLoadType::Standard)
278 , m_quickRedirectComing(false)
279 , m_sentRedirectNotification(false)
280 , m_inStopAllLoaders(false)
281 , m_isExecutingJavaScriptFormAction(false)
282 , m_didCallImplicitClose(true)
283 , m_wasUnloadEventEmitted(false)
284 , m_isComplete(false)
285 , m_needsClear(false)
286 , m_checkTimer(*this, &FrameLoader::checkTimerFired)
287 , m_shouldCallCheckCompleted(false)
288 , m_shouldCallCheckLoadComplete(false)
289 , m_opener(nullptr)
290 , m_loadingFromCachedPage(false)
291 , m_currentNavigationHasShownBeforeUnloadConfirmPanel(false)
292 , m_loadsSynchronously(false)
293 , m_forcedSandboxFlags(SandboxNone)
294{
295}
296
297FrameLoader::~FrameLoader()
298{
299 setOpener(nullptr);
300
301 for (auto& frame : m_openedFrames)
302 frame->loader().m_opener = nullptr;
303
304 m_client.frameLoaderDestroyed();
305
306 if (m_networkingContext)
307 m_networkingContext->invalidate();
308}
309
310void FrameLoader::init()
311{
312 // This somewhat odd set of steps gives the frame an initial empty document.
313 setPolicyDocumentLoader(m_client.createDocumentLoader(ResourceRequest(URL({ }, emptyString())), SubstituteData()).ptr());
314 setProvisionalDocumentLoader(m_policyDocumentLoader.get());
315 m_provisionalDocumentLoader->startLoadingMainResource();
316
317 Ref<Frame> protect(m_frame);
318 m_frame.document()->cancelParsing();
319 m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocument);
320
321 m_networkingContext = m_client.createNetworkingContext();
322 m_progressTracker = std::make_unique<FrameProgressTracker>(m_frame);
323}
324
325void FrameLoader::initForSynthesizedDocument(const URL&)
326{
327 // FIXME: We need to initialize the document URL to the specified URL. Currently the URL is empty and hence
328 // FrameLoader::checkCompleted() will overwrite the URL of the document to be activeDocumentLoader()->documentURL().
329
330 auto loader = m_client.createDocumentLoader(ResourceRequest(URL({ }, emptyString())), SubstituteData());
331 loader->attachToFrame(m_frame);
332 loader->setResponse(ResourceResponse(URL(), "text/html"_s, 0, String()));
333 loader->setCommitted(true);
334 setDocumentLoader(loader.ptr());
335
336 m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocument);
337 m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocumentPostCommit);
338 m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedFirstRealLoad);
339 m_client.transitionToCommittedForNewPage();
340
341 m_didCallImplicitClose = true;
342 m_isComplete = true;
343 m_state = FrameStateComplete;
344 m_needsClear = true;
345
346 m_networkingContext = m_client.createNetworkingContext();
347 m_progressTracker = std::make_unique<FrameProgressTracker>(m_frame);
348}
349
350void FrameLoader::setDefersLoading(bool defers)
351{
352 if (m_documentLoader)
353 m_documentLoader->setDefersLoading(defers);
354 if (m_provisionalDocumentLoader)
355 m_provisionalDocumentLoader->setDefersLoading(defers);
356 if (m_policyDocumentLoader)
357 m_policyDocumentLoader->setDefersLoading(defers);
358 history().setDefersLoading(defers);
359
360 if (!defers) {
361 m_frame.navigationScheduler().startTimer();
362 startCheckCompleteTimer();
363 }
364}
365
366void FrameLoader::checkContentPolicy(const ResourceResponse& response, PolicyCheckIdentifier identifier, ContentPolicyDecisionFunction&& function)
367{
368 if (!activeDocumentLoader()) {
369 // Load was cancelled
370 function(PolicyAction::Ignore, identifier);
371 return;
372 }
373
374 // FIXME: Validate the policy check identifier.
375 client().dispatchDecidePolicyForResponse(response, activeDocumentLoader()->request(), identifier, activeDocumentLoader()->downloadAttribute(), WTFMove(function));
376}
377
378void FrameLoader::changeLocation(FrameLoadRequest&& request)
379{
380 urlSelected(WTFMove(request), nullptr);
381}
382
383void FrameLoader::urlSelected(const URL& url, const String& passedTarget, Event* triggeringEvent, LockHistory lockHistory, LockBackForwardList lockBackForwardList, ShouldSendReferrer shouldSendReferrer, ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy, Optional<NewFrameOpenerPolicy> openerPolicy, const AtomicString& downloadAttribute, const SystemPreviewInfo& systemPreviewInfo, Optional<AdClickAttribution>&& adClickAttribution)
384{
385 auto* frame = lexicalFrameFromCommonVM();
386 auto initiatedByMainFrame = frame && frame->isMainFrame() ? InitiatedByMainFrame::Yes : InitiatedByMainFrame::Unknown;
387
388 NewFrameOpenerPolicy newFrameOpenerPolicy = openerPolicy.valueOr(shouldSendReferrer == NeverSendReferrer ? NewFrameOpenerPolicy::Suppress : NewFrameOpenerPolicy::Allow);
389 urlSelected(FrameLoadRequest(*m_frame.document(), m_frame.document()->securityOrigin(), { url }, passedTarget, lockHistory, lockBackForwardList, shouldSendReferrer, AllowNavigationToInvalidURL::Yes, newFrameOpenerPolicy, shouldOpenExternalURLsPolicy, initiatedByMainFrame, DoNotReplaceDocumentIfJavaScriptURL, downloadAttribute, systemPreviewInfo), triggeringEvent, WTFMove(adClickAttribution));
390}
391
392void FrameLoader::urlSelected(FrameLoadRequest&& frameRequest, Event* triggeringEvent, Optional<AdClickAttribution>&& adClickAttribution)
393{
394 RELEASE_LOG_IF_ALLOWED("urlSelected: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
395
396 Ref<Frame> protect(m_frame);
397
398 if (m_frame.script().executeIfJavaScriptURL(frameRequest.resourceRequest().url(), frameRequest.shouldReplaceDocumentIfJavaScriptURL()))
399 return;
400
401 if (frameRequest.frameName().isEmpty())
402 frameRequest.setFrameName(m_frame.document()->baseTarget());
403
404 addHTTPOriginIfNeeded(frameRequest.resourceRequest(), outgoingOrigin());
405 m_frame.document()->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(frameRequest.resourceRequest(), ContentSecurityPolicy::InsecureRequestType::Navigation);
406
407 loadFrameRequest(WTFMove(frameRequest), triggeringEvent, { }, WTFMove(adClickAttribution));
408}
409
410void FrameLoader::submitForm(Ref<FormSubmission>&& submission)
411{
412 ASSERT(submission->method() == FormSubmission::Method::Post || submission->method() == FormSubmission::Method::Get);
413
414 // FIXME: Find a good spot for these.
415 ASSERT(!submission->state().sourceDocument().frame() || submission->state().sourceDocument().frame() == &m_frame);
416
417 if (!m_frame.page())
418 return;
419
420 if (submission->action().isEmpty())
421 return;
422
423 if (isDocumentSandboxed(m_frame, SandboxForms)) {
424 // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
425 m_frame.document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Blocked form submission to '" + submission->action().stringCenterEllipsizedToLength() + "' because the form's frame is sandboxed and the 'allow-forms' permission is not set.");
426 return;
427 }
428
429 if (WTF::protocolIsJavaScript(submission->action())) {
430 if (!m_frame.document()->contentSecurityPolicy()->allowFormAction(URL(submission->action())))
431 return;
432 m_isExecutingJavaScriptFormAction = true;
433 Ref<Frame> protect(m_frame);
434 m_frame.script().executeIfJavaScriptURL(submission->action(), DoNotReplaceDocumentIfJavaScriptURL);
435 m_isExecutingJavaScriptFormAction = false;
436 return;
437 }
438
439 Frame* targetFrame = findFrameForNavigation(submission->target(), &submission->state().sourceDocument());
440 if (!targetFrame) {
441 if (!DOMWindow::allowPopUp(m_frame) && !UserGestureIndicator::processingUserGesture())
442 return;
443
444 // FIXME: targetFrame can be null for two distinct reasons:
445 // 1. The frame was not found by name, so we should try opening a new window.
446 // 2. The frame was found, but navigating it was not allowed, e.g. by HTML5 sandbox or by origin checks.
447 // Continuing form submission makes no sense in the latter case.
448 // There is a repeat check after timer fires, so this is not a correctness issue.
449
450 targetFrame = &m_frame;
451 } else
452 submission->clearTarget();
453
454 if (!targetFrame->page())
455 return;
456
457 // FIXME: We'd like to remove this altogether and fix the multiple form submission issue another way.
458
459 // We do not want to submit more than one form from the same page, nor do we want to submit a single
460 // form more than once. This flag prevents these from happening; not sure how other browsers prevent this.
461 // The flag is reset in each time we start dispatching a new mouse or key down event, and
462 // also in setView since this part may get reused for a page from the back/forward cache.
463 // The form multi-submit logic here is only needed when we are submitting a form that affects this frame.
464
465 // FIXME: Frame targeting is only one of the ways the submission could end up doing something other
466 // than replacing this frame's content, so this check is flawed. On the other hand, the check is hardly
467 // needed any more now that we reset m_submittedFormURL on each mouse or key down event.
468
469 if (m_frame.tree().isDescendantOf(targetFrame)) {
470 if (m_submittedFormURL == submission->requestURL())
471 return;
472 m_submittedFormURL = submission->requestURL();
473 }
474
475 submission->data().generateFiles(m_frame.document());
476 submission->setReferrer(outgoingReferrer());
477 submission->setOrigin(outgoingOrigin());
478
479 targetFrame->navigationScheduler().scheduleFormSubmission(WTFMove(submission));
480}
481
482void FrameLoader::stopLoading(UnloadEventPolicy unloadEventPolicy)
483{
484 if (m_frame.document() && m_frame.document()->parser())
485 m_frame.document()->parser()->stopParsing();
486
487 if (unloadEventPolicy != UnloadEventPolicyNone)
488 dispatchUnloadEvents(unloadEventPolicy);
489
490 m_isComplete = true; // to avoid calling completed() in finishedParsing()
491 m_didCallImplicitClose = true; // don't want that one either
492
493 if (m_frame.document() && m_frame.document()->parsing()) {
494 finishedParsing();
495 m_frame.document()->setParsing(false);
496 }
497
498 if (auto* document = m_frame.document()) {
499 // FIXME: HTML5 doesn't tell us to set the state to complete when aborting, but we do anyway to match legacy behavior.
500 // http://www.w3.org/Bugs/Public/show_bug.cgi?id=10537
501 document->setReadyState(Document::Complete);
502
503 // FIXME: Should the DatabaseManager watch for something like ActiveDOMObject::stop() rather than being special-cased here?
504 DatabaseManager::singleton().stopDatabases(*document, nullptr);
505 }
506
507 policyChecker().stopCheck();
508
509 // FIXME: This will cancel redirection timer, which really needs to be restarted when restoring the frame from b/f cache.
510 m_frame.navigationScheduler().cancel();
511}
512
513void FrameLoader::stop()
514{
515 // http://bugs.webkit.org/show_bug.cgi?id=10854
516 // The frame's last ref may be removed and it will be deleted by checkCompleted().
517 Ref<Frame> protect(m_frame);
518
519 if (DocumentParser* parser = m_frame.document()->parser()) {
520 parser->stopParsing();
521 parser->finish();
522 }
523}
524
525void FrameLoader::willTransitionToCommitted()
526{
527 // This function is called when a frame is still fully in place (not cached, not detached), but will be replaced.
528
529 if (m_frame.editor().hasComposition()) {
530 // The text was already present in DOM, so it's better to confirm than to cancel the composition.
531 m_frame.editor().confirmComposition();
532 if (EditorClient* editorClient = m_frame.editor().client()) {
533 editorClient->respondToChangedSelection(&m_frame);
534 editorClient->discardedComposition(&m_frame);
535 }
536 }
537}
538
539bool FrameLoader::closeURL()
540{
541 history().saveDocumentState();
542
543 Document* currentDocument = m_frame.document();
544 UnloadEventPolicy unloadEventPolicy;
545 if (m_frame.page() && m_frame.page()->chrome().client().isSVGImageChromeClient()) {
546 // If this is the SVGDocument of an SVGImage, no need to dispatch events or recalcStyle.
547 unloadEventPolicy = UnloadEventPolicyNone;
548 } else {
549 // Should only send the pagehide event here if the current document exists and has not been placed in the page cache.
550 unloadEventPolicy = currentDocument && currentDocument->pageCacheState() == Document::NotInPageCache ? UnloadEventPolicyUnloadAndPageHide : UnloadEventPolicyUnloadOnly;
551 }
552
553 stopLoading(unloadEventPolicy);
554
555 m_frame.editor().clearUndoRedoOperations();
556 return true;
557}
558
559bool FrameLoader::didOpenURL()
560{
561 if (m_frame.navigationScheduler().redirectScheduledDuringLoad()) {
562 // A redirect was scheduled before the document was created.
563 // This can happen when one frame changes another frame's location.
564 return false;
565 }
566
567 m_frame.navigationScheduler().cancel();
568 m_frame.editor().clearLastEditCommand();
569
570 m_isComplete = false;
571 m_didCallImplicitClose = false;
572
573 // If we are still in the process of initializing an empty document then
574 // its frame is not in a consistent state for rendering, so avoid setJSStatusBarText
575 // since it may cause clients to attempt to render the frame.
576 if (!m_stateMachine.creatingInitialEmptyDocument()) {
577 DOMWindow* window = m_frame.document()->domWindow();
578 window->setStatus(String());
579 window->setDefaultStatus(String());
580 }
581
582 started();
583
584 return true;
585}
586
587void FrameLoader::didExplicitOpen()
588{
589 m_isComplete = false;
590 m_didCallImplicitClose = false;
591
592 // Calling document.open counts as committing the first real document load.
593 if (!m_stateMachine.committedFirstRealDocumentLoad())
594 m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocumentPostCommit);
595
596 m_client.dispatchDidExplicitOpen(m_frame.document() ? m_frame.document()->url() : URL());
597
598 // Prevent window.open(url) -- eg window.open("about:blank") -- from blowing away results
599 // from a subsequent window.document.open / window.document.write call.
600 // Canceling redirection here works for all cases because document.open
601 // implicitly precedes document.write.
602 m_frame.navigationScheduler().cancel();
603}
604
605
606void FrameLoader::cancelAndClear()
607{
608 m_frame.navigationScheduler().cancel();
609
610 if (!m_isComplete)
611 closeURL();
612
613 clear(m_frame.document(), false);
614 m_frame.script().updatePlatformScriptObjects();
615}
616
617static inline bool shouldClearWindowName(const Frame& frame, const Document& newDocument)
618{
619 if (!frame.isMainFrame())
620 return false;
621
622 if (frame.loader().opener())
623 return false;
624
625 return !newDocument.securityOrigin().isSameOriginAs(frame.document()->securityOrigin());
626}
627
628void FrameLoader::clear(Document* newDocument, bool clearWindowProperties, bool clearScriptObjects, bool clearFrameView)
629{
630 m_frame.editor().clear();
631
632 if (!m_needsClear)
633 return;
634 m_needsClear = false;
635
636 if (m_frame.document()->pageCacheState() != Document::InPageCache) {
637 m_frame.document()->cancelParsing();
638 m_frame.document()->stopActiveDOMObjects();
639 bool hadLivingRenderTree = m_frame.document()->hasLivingRenderTree();
640 m_frame.document()->prepareForDestruction();
641 if (hadLivingRenderTree)
642 m_frame.document()->adjustFocusedNodeOnNodeRemoval(*m_frame.document());
643 }
644
645 // Do this after detaching the document so that the unload event works.
646 if (clearWindowProperties) {
647 InspectorInstrumentation::frameWindowDiscarded(m_frame, m_frame.document()->domWindow());
648 m_frame.document()->domWindow()->resetUnlessSuspendedForDocumentSuspension();
649 m_frame.windowProxy().clearJSWindowProxiesNotMatchingDOMWindow(newDocument->domWindow(), m_frame.document()->pageCacheState() == Document::AboutToEnterPageCache);
650
651 if (shouldClearWindowName(m_frame, *newDocument))
652 m_frame.tree().setName(nullAtom());
653 }
654
655 m_frame.selection().prepareForDestruction();
656 m_frame.eventHandler().clear();
657
658 if (clearFrameView && m_frame.view())
659 m_frame.view()->clear();
660
661 // Do not drop the document before the ScriptController and view are cleared
662 // as some destructors might still try to access the document.
663 m_frame.setDocument(nullptr);
664
665 subframeLoader().clear();
666
667 if (clearWindowProperties)
668 m_frame.windowProxy().setDOMWindow(newDocument->domWindow());
669
670 if (clearScriptObjects)
671 m_frame.script().clearScriptObjects();
672
673 m_frame.script().enableEval();
674
675 m_frame.navigationScheduler().clear();
676
677 m_checkTimer.stop();
678 m_shouldCallCheckCompleted = false;
679 m_shouldCallCheckLoadComplete = false;
680
681 if (m_stateMachine.isDisplayingInitialEmptyDocument() && m_stateMachine.committedFirstRealDocumentLoad())
682 m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedFirstRealLoad);
683}
684
685void FrameLoader::receivedFirstData()
686{
687 dispatchDidCommitLoad(WTF::nullopt);
688 dispatchDidClearWindowObjectsInAllWorlds();
689 dispatchGlobalObjectAvailableInAllWorlds();
690
691 if (!m_documentLoader)
692 return;
693
694 auto& documentLoader = *m_documentLoader;
695 auto& title = documentLoader.title();
696 if (!title.string.isNull())
697 m_client.dispatchDidReceiveTitle(title);
698
699 ASSERT(m_frame.document());
700 auto& document = *m_frame.document();
701
702 LinkLoader::loadLinksFromHeader(documentLoader.response().httpHeaderField(HTTPHeaderName::Link), document.url(), document, LinkLoader::MediaAttributeCheck::MediaAttributeEmpty);
703
704 double delay;
705 String urlString;
706 if (!parseHTTPRefresh(documentLoader.response().httpHeaderField(HTTPHeaderName::Refresh), delay, urlString))
707 return;
708 auto completedURL = urlString.isEmpty() ? document.url() : document.completeURL(urlString);
709 if (!WTF::protocolIsJavaScript(completedURL))
710 m_frame.navigationScheduler().scheduleRedirect(document, delay, completedURL);
711 else {
712 auto message = "Refused to refresh " + document.url().stringCenterEllipsizedToLength() + " to a javascript: URL";
713 document.addConsoleMessage(MessageSource::Security, MessageLevel::Error, message);
714 }
715}
716
717void FrameLoader::setOutgoingReferrer(const URL& url)
718{
719 m_outgoingReferrer = url.strippedForUseAsReferrer();
720}
721
722void FrameLoader::didBeginDocument(bool dispatch)
723{
724 m_needsClear = true;
725 m_isComplete = false;
726 m_didCallImplicitClose = false;
727 m_frame.document()->setReadyState(Document::Loading);
728
729 if (m_pendingStateObject) {
730 m_frame.document()->statePopped(*m_pendingStateObject);
731 m_pendingStateObject = nullptr;
732 }
733
734 if (dispatch)
735 dispatchDidClearWindowObjectsInAllWorlds();
736
737 updateFirstPartyForCookies();
738 m_frame.document()->initContentSecurityPolicy();
739
740 const Settings& settings = m_frame.settings();
741 m_frame.document()->cachedResourceLoader().setImagesEnabled(settings.areImagesEnabled());
742 m_frame.document()->cachedResourceLoader().setAutoLoadImages(settings.loadsImagesAutomatically());
743
744 if (m_documentLoader) {
745 String dnsPrefetchControl = m_documentLoader->response().httpHeaderField(HTTPHeaderName::XDNSPrefetchControl);
746 if (!dnsPrefetchControl.isEmpty())
747 m_frame.document()->parseDNSPrefetchControlHeader(dnsPrefetchControl);
748
749 m_frame.document()->contentSecurityPolicy()->didReceiveHeaders(ContentSecurityPolicyResponseHeaders(m_documentLoader->response()), referrer(), ContentSecurityPolicy::ReportParsingErrors::No);
750
751 String referrerPolicy = m_documentLoader->response().httpHeaderField(HTTPHeaderName::ReferrerPolicy);
752 if (!referrerPolicy.isNull())
753 m_frame.document()->processReferrerPolicy(referrerPolicy, ReferrerPolicySource::HTTPHeader);
754
755 String headerContentLanguage = m_documentLoader->response().httpHeaderField(HTTPHeaderName::ContentLanguage);
756 if (!headerContentLanguage.isEmpty()) {
757 size_t commaIndex = headerContentLanguage.find(',');
758 headerContentLanguage.truncate(commaIndex); // notFound == -1 == don't truncate
759 headerContentLanguage = stripLeadingAndTrailingHTMLSpaces(headerContentLanguage);
760 if (!headerContentLanguage.isEmpty())
761 m_frame.document()->setContentLanguage(headerContentLanguage);
762 }
763 }
764
765 history().restoreDocumentState();
766}
767
768void FrameLoader::finishedParsing()
769{
770 LOG(Loading, "WebCoreLoading %s: Finished parsing", m_frame.tree().uniqueName().string().utf8().data());
771
772 m_frame.injectUserScripts(InjectAtDocumentEnd);
773
774 if (m_stateMachine.creatingInitialEmptyDocument())
775 return;
776
777 // This can be called from the Frame's destructor, in which case we shouldn't protect ourselves
778 // because doing so will cause us to re-enter the destructor when protector goes out of scope.
779 // Null-checking the FrameView indicates whether or not we're in the destructor.
780 RefPtr<Frame> protector = m_frame.view() ? &m_frame : 0;
781
782 m_client.dispatchDidFinishDocumentLoad();
783
784 scrollToFragmentWithParentBoundary(m_frame.document()->url());
785
786 checkCompleted();
787
788 if (!m_frame.view())
789 return; // We are being destroyed by something checkCompleted called.
790
791 // Check if the scrollbars are really needed for the content.
792 // If not, remove them, relayout, and repaint.
793 m_frame.view()->restoreScrollbar();
794}
795
796void FrameLoader::loadDone(LoadCompletionType type)
797{
798 if (type == LoadCompletionType::Finish)
799 checkCompleted();
800 else
801 scheduleCheckCompleted();
802}
803
804void FrameLoader::subresourceLoadDone(LoadCompletionType type)
805{
806 if (type == LoadCompletionType::Finish)
807 checkLoadComplete();
808 else
809 scheduleCheckLoadComplete();
810}
811
812bool FrameLoader::allChildrenAreComplete() const
813{
814 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling()) {
815 if (!child->loader().m_isComplete)
816 return false;
817 }
818 return true;
819}
820
821bool FrameLoader::allAncestorsAreComplete() const
822{
823 for (Frame* ancestor = &m_frame; ancestor; ancestor = ancestor->tree().parent()) {
824 if (!ancestor->loader().m_isComplete)
825 return false;
826 }
827 return true;
828}
829
830void FrameLoader::checkCompleted()
831{
832 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isScriptAllowed());
833 m_shouldCallCheckCompleted = false;
834
835 // Have we completed before?
836 if (m_isComplete)
837 return;
838
839#if ENABLE(VIDEO)
840 // FIXME: Remove this code once https://webkit.org/b/185284 is fixed.
841 if (HTMLMediaElement::isRunningDestructor()) {
842 ASSERT_NOT_REACHED();
843 scheduleCheckCompleted();
844 return;
845 }
846#endif
847
848 // FIXME: It would be better if resource loads were kicked off after render tree update (or didn't complete synchronously).
849 // https://bugs.webkit.org/show_bug.cgi?id=171729
850 if (m_frame.document()->inRenderTreeUpdate()) {
851 scheduleCheckCompleted();
852 return;
853 }
854
855 // Are we still parsing?
856 if (m_frame.document()->parsing())
857 return;
858
859 // Still waiting for images/scripts?
860 if (m_frame.document()->cachedResourceLoader().requestCount())
861 return;
862
863 // Still waiting for elements that don't go through a FrameLoader?
864 if (m_frame.document()->isDelayingLoadEvent())
865 return;
866
867 auto* scriptableParser = m_frame.document()->scriptableDocumentParser();
868 if (scriptableParser && scriptableParser->hasScriptsWaitingForStylesheets())
869 return;
870
871 // Any frame that hasn't completed yet?
872 if (!allChildrenAreComplete())
873 return;
874
875 // Important not to protect earlier in this function, because earlier parts
876 // of this function can be called in the frame's destructor, and it's not legal
877 // to ref an object while it's being destroyed.
878 Ref<Frame> protect(m_frame);
879
880 // OK, completed.
881 m_isComplete = true;
882 m_requestedHistoryItem = nullptr;
883 m_frame.document()->setReadyState(Document::Complete);
884
885#if PLATFORM(IOS_FAMILY)
886 if (m_frame.document()->url().isEmpty()) {
887 // We need to update the document URL of a PDF document to be non-empty so that both back/forward history navigation
888 // between PDF pages and fragment navigation works. See <rdar://problem/9544769> for more details.
889 // FIXME: Is there a better place for this code, say DocumentLoader? Also, we should explicitly only update the URL
890 // of the document when it's a PDFDocument object instead of assuming that a Document object with an empty URL is a PDFDocument.
891 // FIXME: This code is incorrect for a synthesized document (which also has an empty URL). The URL for a synthesized
892 // document should be the URL specified to FrameLoader::initForSynthesizedDocument().
893 m_frame.document()->setURL(activeDocumentLoader()->documentURL());
894 }
895#endif
896
897 checkCallImplicitClose(); // if we didn't do it before
898
899 m_frame.navigationScheduler().startTimer();
900
901 completed();
902 if (m_frame.page())
903 checkLoadComplete();
904}
905
906void FrameLoader::checkTimerFired()
907{
908 checkCompletenessNow();
909}
910
911void FrameLoader::checkCompletenessNow()
912{
913 Ref<Frame> protect(m_frame);
914
915 if (Page* page = m_frame.page()) {
916 if (page->defersLoading())
917 return;
918 }
919 if (m_shouldCallCheckCompleted)
920 checkCompleted();
921 if (m_shouldCallCheckLoadComplete)
922 checkLoadComplete();
923}
924
925void FrameLoader::startCheckCompleteTimer()
926{
927 if (!(m_shouldCallCheckCompleted || m_shouldCallCheckLoadComplete))
928 return;
929 if (m_checkTimer.isActive())
930 return;
931 m_checkTimer.startOneShot(0_s);
932}
933
934void FrameLoader::scheduleCheckCompleted()
935{
936 m_shouldCallCheckCompleted = true;
937 startCheckCompleteTimer();
938}
939
940void FrameLoader::scheduleCheckLoadComplete()
941{
942 m_shouldCallCheckLoadComplete = true;
943 startCheckCompleteTimer();
944}
945
946void FrameLoader::checkCallImplicitClose()
947{
948 if (m_didCallImplicitClose || m_frame.document()->parsing() || m_frame.document()->isDelayingLoadEvent())
949 return;
950
951 if (!allChildrenAreComplete())
952 return; // still got a frame running -> too early
953
954 m_didCallImplicitClose = true;
955 m_wasUnloadEventEmitted = false;
956 m_frame.document()->implicitClose();
957}
958
959void FrameLoader::loadURLIntoChildFrame(const URL& url, const String& referer, Frame* childFrame)
960{
961 RELEASE_LOG_IF_ALLOWED("loadURLIntoChildFrame: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
962
963 ASSERT(childFrame);
964
965#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
966 if (auto activeLoader = activeDocumentLoader()) {
967 if (auto subframeArchive = activeLoader->popArchiveForSubframe(childFrame->tree().uniqueName(), url)) {
968 childFrame->loader().loadArchive(RefPtr<Archive> { subframeArchive }.releaseNonNull());
969 return;
970 }
971 }
972#endif
973
974 // If we're moving in the back/forward list, we might want to replace the content
975 // of this child frame with whatever was there at that point.
976 auto* parentItem = history().currentItem();
977 if (parentItem && parentItem->children().size() && isBackForwardLoadType(loadType()) && !m_frame.document()->loadEventFinished()) {
978 if (auto* childItem = parentItem->childItemWithTarget(childFrame->tree().uniqueName())) {
979 childFrame->loader().m_requestedHistoryItem = childItem;
980 childFrame->loader().loadDifferentDocumentItem(*childItem, nullptr, loadType(), MayAttemptCacheOnlyLoadForFormSubmissionItem, ShouldTreatAsContinuingLoad::No);
981 return;
982 }
983 }
984
985 auto* lexicalFrame = lexicalFrameFromCommonVM();
986 auto initiatedByMainFrame = lexicalFrame && lexicalFrame->isMainFrame() ? InitiatedByMainFrame::Yes : InitiatedByMainFrame::Unknown;
987
988 FrameLoadRequest frameLoadRequest { *m_frame.document(), m_frame.document()->securityOrigin(), { url }, "_self"_s, LockHistory::No, LockBackForwardList::Yes, ShouldSendReferrer::MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Suppress, ShouldOpenExternalURLsPolicy::ShouldNotAllow, initiatedByMainFrame };
989 childFrame->loader().loadURL(WTFMove(frameLoadRequest), referer, FrameLoadType::RedirectWithLockedBackForwardList, nullptr, { }, WTF::nullopt, [] { });
990}
991
992#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
993
994void FrameLoader::loadArchive(Ref<Archive>&& archive)
995{
996 RELEASE_LOG_IF_ALLOWED("loadArchive: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
997
998 ArchiveResource* mainResource = archive->mainResource();
999 ASSERT(mainResource);
1000 if (!mainResource)
1001 return;
1002
1003 ResourceResponse response(URL(), mainResource->mimeType(), mainResource->data().size(), mainResource->textEncoding());
1004 SubstituteData substituteData(&mainResource->data(), URL(), response, SubstituteData::SessionHistoryVisibility::Hidden);
1005
1006 ResourceRequest request(mainResource->url());
1007
1008 auto documentLoader = m_client.createDocumentLoader(request, substituteData);
1009 documentLoader->setArchive(WTFMove(archive));
1010 load(documentLoader.get());
1011}
1012
1013#endif // ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
1014
1015String FrameLoader::outgoingReferrer() const
1016{
1017 // See http://www.whatwg.org/specs/web-apps/current-work/#fetching-resources
1018 // for why we walk the parent chain for srcdoc documents.
1019 Frame* frame = &m_frame;
1020 while (frame && frame->document()->isSrcdocDocument()) {
1021 frame = frame->tree().parent();
1022 // Srcdoc documents cannot be top-level documents, by definition,
1023 // because they need to be contained in iframes with the srcdoc.
1024 ASSERT(frame);
1025 }
1026 if (!frame)
1027 return emptyString();
1028 return frame->loader().m_outgoingReferrer;
1029}
1030
1031String FrameLoader::outgoingOrigin() const
1032{
1033 return m_frame.document()->securityOrigin().toString();
1034}
1035
1036bool FrameLoader::checkIfFormActionAllowedByCSP(const URL& url, bool didReceiveRedirectResponse) const
1037{
1038 if (m_submittedFormURL.isEmpty())
1039 return true;
1040
1041 auto redirectResponseReceived = didReceiveRedirectResponse ? ContentSecurityPolicy::RedirectResponseReceived::Yes : ContentSecurityPolicy::RedirectResponseReceived::No;
1042 return m_frame.document()->contentSecurityPolicy()->allowFormAction(url, redirectResponseReceived);
1043}
1044
1045Frame* FrameLoader::opener()
1046{
1047 return m_opener;
1048}
1049
1050void FrameLoader::setOpener(Frame* opener)
1051{
1052 if (m_opener && !opener)
1053 m_client.didDisownOpener();
1054
1055 if (m_opener) {
1056 // When setOpener is called in ~FrameLoader, opener's m_frameLoader is already cleared.
1057 auto& openerFrameLoader = m_opener == &m_frame ? *this : m_opener->loader();
1058 openerFrameLoader.m_openedFrames.remove(&m_frame);
1059 }
1060 if (opener) {
1061 opener->loader().m_openedFrames.add(&m_frame);
1062 if (auto* page = m_frame.page())
1063 page->setOpenedByDOMWithOpener();
1064 }
1065 m_opener = opener;
1066
1067 if (m_frame.document())
1068 m_frame.document()->initSecurityContext();
1069}
1070
1071// FIXME: This does not belong in FrameLoader!
1072void FrameLoader::handleFallbackContent()
1073{
1074 HTMLFrameOwnerElement* owner = m_frame.ownerElement();
1075 if (!is<HTMLObjectElement>(owner))
1076 return;
1077 downcast<HTMLObjectElement>(*owner).renderFallbackContent();
1078}
1079
1080void FrameLoader::provisionalLoadStarted()
1081{
1082 if (m_stateMachine.firstLayoutDone())
1083 m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedFirstRealLoad);
1084 m_frame.navigationScheduler().cancel(NewLoadInProgress::Yes);
1085 m_client.provisionalLoadStarted();
1086
1087 if (m_frame.isMainFrame()) {
1088 tracePoint(MainResourceLoadDidStartProvisional);
1089
1090 if (auto* page = m_frame.page())
1091 page->didStartProvisionalLoad();
1092 }
1093}
1094
1095void FrameLoader::resetMultipleFormSubmissionProtection()
1096{
1097 m_submittedFormURL = URL();
1098}
1099
1100void FrameLoader::updateFirstPartyForCookies()
1101{
1102 if (m_frame.tree().parent())
1103 setFirstPartyForCookies(m_frame.tree().parent()->document()->firstPartyForCookies());
1104 else
1105 setFirstPartyForCookies(m_frame.document()->url());
1106}
1107
1108void FrameLoader::setFirstPartyForCookies(const URL& url)
1109{
1110 for (Frame* frame = &m_frame; frame; frame = frame->tree().traverseNext(&m_frame))
1111 frame->document()->setFirstPartyForCookies(url);
1112
1113 RegistrableDomain registrableDomain(url);
1114 for (Frame* frame = &m_frame; frame; frame = frame->tree().traverseNext(&m_frame)) {
1115 if (SecurityPolicy::shouldInheritSecurityOriginFromOwner(frame->document()->url()) || registrableDomain.matches(frame->document()->url()))
1116 frame->document()->setSiteForCookies(url);
1117 }
1118}
1119
1120// This does the same kind of work that didOpenURL does, except it relies on the fact
1121// that a higher level already checked that the URLs match and the scrolling is the right thing to do.
1122void FrameLoader::loadInSameDocument(const URL& url, SerializedScriptValue* stateObject, bool isNewNavigation)
1123{
1124 RELEASE_LOG_IF_ALLOWED("loadInSameDocument: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1125
1126 // If we have a state object, we cannot also be a new navigation.
1127 ASSERT(!stateObject || (stateObject && !isNewNavigation));
1128
1129 // Update the data source's request with the new URL to fake the URL change
1130 URL oldURL = m_frame.document()->url();
1131 m_frame.document()->setURL(url);
1132 setOutgoingReferrer(url);
1133 documentLoader()->replaceRequestURLForSameDocumentNavigation(url);
1134 if (isNewNavigation && !shouldTreatURLAsSameAsCurrent(url) && !stateObject) {
1135 // NB: must happen after replaceRequestURLForSameDocumentNavigation(), since we add
1136 // based on the current request. Must also happen before we openURL and displace the
1137 // scroll position, since adding the BF item will save away scroll state.
1138
1139 // NB2: If we were loading a long, slow doc, and the user fragment navigated before
1140 // it was done, currItem is now set the that slow doc, and prevItem is whatever was
1141 // before it. Adding the b/f item will bump the slow doc down to prevItem, even
1142 // though its load is not yet done. I think this all works out OK, for one because
1143 // we have already saved away the scroll and doc state for the long slow load,
1144 // but it's not an obvious case.
1145
1146 history().updateBackForwardListForFragmentScroll();
1147 }
1148
1149 bool hashChange = equalIgnoringFragmentIdentifier(url, oldURL) && url.fragmentIdentifier() != oldURL.fragmentIdentifier();
1150
1151 history().updateForSameDocumentNavigation();
1152
1153 // If we were in the autoscroll/panScroll mode we want to stop it before following the link to the anchor
1154 if (hashChange)
1155 m_frame.eventHandler().stopAutoscrollTimer();
1156
1157 // It's important to model this as a load that starts and immediately finishes.
1158 // Otherwise, the parent frame may think we never finished loading.
1159 started();
1160
1161 if (auto* ownerElement = m_frame.ownerElement()) {
1162 auto* ownerRenderer = ownerElement->renderer();
1163 auto* view = m_frame.view();
1164 if (is<RenderWidget>(ownerRenderer) && view)
1165 downcast<RenderWidget>(*ownerRenderer).setWidget(view);
1166 }
1167
1168 // We need to scroll to the fragment whether or not a hash change occurred, since
1169 // the user might have scrolled since the previous navigation.
1170 scrollToFragmentWithParentBoundary(url, isNewNavigation);
1171
1172 m_isComplete = false;
1173 checkCompleted();
1174
1175 if (isNewNavigation) {
1176 // This will clear previousItem from the rest of the frame tree that didn't
1177 // doing any loading. We need to make a pass on this now, since for fragment
1178 // navigation we'll not go through a real load and reach Completed state.
1179 checkLoadComplete();
1180 }
1181
1182 m_client.dispatchDidNavigateWithinPage();
1183
1184 m_frame.document()->statePopped(stateObject ? Ref<SerializedScriptValue> { *stateObject } : SerializedScriptValue::nullValue());
1185 m_client.dispatchDidPopStateWithinPage();
1186
1187 if (hashChange) {
1188 m_frame.document()->enqueueHashchangeEvent(oldURL, url);
1189 m_client.dispatchDidChangeLocationWithinPage();
1190 }
1191
1192 // FrameLoaderClient::didFinishLoad() tells the internal load delegate the load finished with no error
1193 m_client.didFinishLoad();
1194}
1195
1196bool FrameLoader::isComplete() const
1197{
1198 return m_isComplete;
1199}
1200
1201void FrameLoader::completed()
1202{
1203 Ref<Frame> protect(m_frame);
1204
1205 for (Frame* descendant = m_frame.tree().traverseNext(&m_frame); descendant; descendant = descendant->tree().traverseNext(&m_frame))
1206 descendant->navigationScheduler().startTimer();
1207
1208 if (Frame* parent = m_frame.tree().parent())
1209 parent->loader().checkCompleted();
1210
1211 if (m_frame.view())
1212 m_frame.view()->maintainScrollPositionAtAnchor(nullptr);
1213}
1214
1215void FrameLoader::started()
1216{
1217 for (Frame* frame = &m_frame; frame; frame = frame->tree().parent())
1218 frame->loader().m_isComplete = false;
1219}
1220
1221void FrameLoader::prepareForLoadStart()
1222{
1223 RELEASE_LOG_IF_ALLOWED("prepareForLoadStart: Starting frame load (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1224
1225 m_progressTracker->progressStarted();
1226 m_client.dispatchDidStartProvisionalLoad();
1227
1228 if (AXObjectCache::accessibilityEnabled()) {
1229 if (AXObjectCache* cache = m_frame.document()->existingAXObjectCache()) {
1230 AXObjectCache::AXLoadingEvent loadingEvent = loadType() == FrameLoadType::Reload ? AXObjectCache::AXLoadingReloaded : AXObjectCache::AXLoadingStarted;
1231 cache->frameLoadingEventNotification(&m_frame, loadingEvent);
1232 }
1233 }
1234}
1235
1236void FrameLoader::setupForReplace()
1237{
1238 m_client.revertToProvisionalState(m_documentLoader.get());
1239 setState(FrameStateProvisional);
1240 m_provisionalDocumentLoader = m_documentLoader;
1241 m_documentLoader = nullptr;
1242 detachChildren();
1243}
1244
1245void FrameLoader::loadFrameRequest(FrameLoadRequest&& request, Event* event, RefPtr<FormState>&& formState, Optional<AdClickAttribution>&& adClickAttribution)
1246{
1247 RELEASE_LOG_IF_ALLOWED("loadFrameRequest: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1248
1249 // Protect frame from getting blown away inside dispatchBeforeLoadEvent in loadWithDocumentLoader.
1250 auto protectFrame = makeRef(m_frame);
1251
1252 URL url = request.resourceRequest().url();
1253
1254 ASSERT(m_frame.document());
1255 if (!request.requesterSecurityOrigin().canDisplay(url)) {
1256 reportLocalLoadFailed(&m_frame, url.stringCenterEllipsizedToLength());
1257 return;
1258 }
1259
1260 String argsReferrer = request.resourceRequest().httpReferrer();
1261 if (argsReferrer.isEmpty())
1262 argsReferrer = outgoingReferrer();
1263
1264 String referrer = SecurityPolicy::generateReferrerHeader(m_frame.document()->referrerPolicy(), url, argsReferrer);
1265 if (request.shouldSendReferrer() == NeverSendReferrer)
1266 referrer = String();
1267
1268 FrameLoadType loadType;
1269 if (request.resourceRequest().cachePolicy() == ResourceRequestCachePolicy::ReloadIgnoringCacheData)
1270 loadType = FrameLoadType::Reload;
1271 else if (request.lockBackForwardList() == LockBackForwardList::Yes)
1272 loadType = FrameLoadType::RedirectWithLockedBackForwardList;
1273 else
1274 loadType = FrameLoadType::Standard;
1275
1276 auto completionHandler = [this, protectedFrame = makeRef(m_frame), formState = makeWeakPtr(formState.get()), frameName = request.frameName()] {
1277 // FIXME: It's possible this targetFrame will not be the same frame that was targeted by the actual
1278 // load if frame names have changed.
1279 Frame* sourceFrame = formState ? formState->sourceDocument().frame() : &m_frame;
1280 if (!sourceFrame)
1281 sourceFrame = &m_frame;
1282 Frame* targetFrame = sourceFrame->loader().findFrameForNavigation(frameName);
1283 if (targetFrame && targetFrame != sourceFrame) {
1284 if (Page* page = targetFrame->page())
1285 page->chrome().focus();
1286 }
1287 };
1288
1289 if (request.resourceRequest().httpMethod() == "POST")
1290 loadPostRequest(WTFMove(request), referrer, loadType, event, WTFMove(formState), WTFMove(completionHandler));
1291 else
1292 loadURL(WTFMove(request), referrer, loadType, event, WTFMove(formState), WTFMove(adClickAttribution), WTFMove(completionHandler));
1293}
1294
1295static ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicyToApply(Frame& currentFrame, InitiatedByMainFrame initiatedByMainFrame, ShouldOpenExternalURLsPolicy propagatedPolicy)
1296{
1297 if (UserGestureIndicator::processingUserGesture())
1298 return ShouldOpenExternalURLsPolicy::ShouldAllow;
1299
1300 if (initiatedByMainFrame == InitiatedByMainFrame::Yes)
1301 return propagatedPolicy;
1302
1303 if (!currentFrame.isMainFrame())
1304 return ShouldOpenExternalURLsPolicy::ShouldNotAllow;
1305
1306 return propagatedPolicy;
1307}
1308
1309static ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicyToApply(Frame& currentFrame, const FrameLoadRequest& frameLoadRequest)
1310{
1311 return shouldOpenExternalURLsPolicyToApply(currentFrame, frameLoadRequest.initiatedByMainFrame(), frameLoadRequest.shouldOpenExternalURLsPolicy());
1312}
1313
1314static void applyShouldOpenExternalURLsPolicyToNewDocumentLoader(Frame& frame, DocumentLoader& documentLoader, InitiatedByMainFrame initiatedByMainFrame, ShouldOpenExternalURLsPolicy propagatedPolicy)
1315{
1316 documentLoader.setShouldOpenExternalURLsPolicy(shouldOpenExternalURLsPolicyToApply(frame, initiatedByMainFrame, propagatedPolicy));
1317}
1318
1319static void applyShouldOpenExternalURLsPolicyToNewDocumentLoader(Frame& frame, DocumentLoader& documentLoader, const FrameLoadRequest& frameLoadRequest)
1320{
1321 documentLoader.setShouldOpenExternalURLsPolicy(shouldOpenExternalURLsPolicyToApply(frame, frameLoadRequest));
1322}
1323
1324bool FrameLoader::isNavigationAllowed() const
1325{
1326 return m_pageDismissalEventBeingDispatched == PageDismissalType::None && NavigationDisabler::isNavigationAllowed(m_frame);
1327}
1328
1329bool FrameLoader::isStopLoadingAllowed() const
1330{
1331 return m_pageDismissalEventBeingDispatched == PageDismissalType::None;
1332}
1333
1334void FrameLoader::loadURL(FrameLoadRequest&& frameLoadRequest, const String& referrer, FrameLoadType newLoadType, Event* event, RefPtr<FormState>&& formState, Optional<AdClickAttribution>&& adClickAttribution, CompletionHandler<void()>&& completionHandler)
1335{
1336 RELEASE_LOG_IF_ALLOWED("loadURL: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1337
1338 CompletionHandlerCallingScope completionHandlerCaller(WTFMove(completionHandler));
1339 if (m_inStopAllLoaders || m_inClearProvisionalLoadForPolicyCheck)
1340 return;
1341
1342 Ref<Frame> protect(m_frame);
1343
1344 // Anchor target is ignored when the download attribute is set since it will download the hyperlink rather than follow it.
1345 String effectiveFrameName = frameLoadRequest.downloadAttribute().isNull() ? frameLoadRequest.frameName() : String();
1346 AllowNavigationToInvalidURL allowNavigationToInvalidURL = frameLoadRequest.allowNavigationToInvalidURL();
1347 NewFrameOpenerPolicy openerPolicy = frameLoadRequest.newFrameOpenerPolicy();
1348 LockHistory lockHistory = frameLoadRequest.lockHistory();
1349 bool isFormSubmission = formState;
1350
1351 const URL& newURL = frameLoadRequest.resourceRequest().url();
1352 ResourceRequest request(newURL);
1353 if (!referrer.isEmpty()) {
1354 request.setHTTPReferrer(referrer);
1355 auto referrerOrigin = SecurityOrigin::createFromString(referrer);
1356 addHTTPOriginIfNeeded(request, referrerOrigin->toString());
1357 }
1358 if (&m_frame.tree().top() != &m_frame)
1359 request.setDomainForCachePartition(m_frame.tree().top().document()->domainForCachePartition());
1360
1361 addExtraFieldsToRequest(request, newLoadType, true);
1362 if (isReload(newLoadType))
1363 request.setCachePolicy(ResourceRequestCachePolicy::ReloadIgnoringCacheData);
1364
1365 ASSERT(newLoadType != FrameLoadType::Same);
1366
1367 // The search for a target frame is done earlier in the case of form submission.
1368 Frame* targetFrame = isFormSubmission ? nullptr : findFrameForNavigation(effectiveFrameName);
1369 if (targetFrame && targetFrame != &m_frame) {
1370 frameLoadRequest.setFrameName("_self");
1371 targetFrame->loader().loadURL(WTFMove(frameLoadRequest), referrer, newLoadType, event, WTFMove(formState), WTFMove(adClickAttribution), completionHandlerCaller.release());
1372 return;
1373 }
1374
1375 if (!isNavigationAllowed())
1376 return;
1377
1378 NavigationAction action { frameLoadRequest.requester(), request, frameLoadRequest.initiatedByMainFrame(), newLoadType, isFormSubmission, event, frameLoadRequest.shouldOpenExternalURLsPolicy(), frameLoadRequest.downloadAttribute() };
1379 action.setLockHistory(lockHistory);
1380 action.setLockBackForwardList(frameLoadRequest.lockBackForwardList());
1381 if (adClickAttribution && m_frame.isMainFrame())
1382 action.setAdClickAttribution(WTFMove(*adClickAttribution));
1383
1384 if (!targetFrame && !effectiveFrameName.isEmpty()) {
1385 action = action.copyWithShouldOpenExternalURLsPolicy(shouldOpenExternalURLsPolicyToApply(m_frame, frameLoadRequest));
1386 policyChecker().checkNewWindowPolicy(WTFMove(action), WTFMove(request), WTFMove(formState), effectiveFrameName, [this, allowNavigationToInvalidURL, openerPolicy, completionHandler = completionHandlerCaller.release()] (const ResourceRequest& request, WeakPtr<FormState>&& formState, const String& frameName, const NavigationAction& action, ShouldContinue shouldContinue) mutable {
1387 continueLoadAfterNewWindowPolicy(request, formState.get(), frameName, action, shouldContinue, allowNavigationToInvalidURL, openerPolicy);
1388 completionHandler();
1389 });
1390 return;
1391 }
1392
1393 RefPtr<DocumentLoader> oldDocumentLoader = m_documentLoader;
1394
1395 bool sameURL = shouldTreatURLAsSameAsCurrent(newURL);
1396 const String& httpMethod = request.httpMethod();
1397
1398 // Make sure to do scroll to fragment processing even if the URL is
1399 // exactly the same so pages with '#' links and DHTML side effects
1400 // work properly.
1401 if (shouldPerformFragmentNavigation(isFormSubmission, httpMethod, newLoadType, newURL)) {
1402 oldDocumentLoader->setTriggeringAction(WTFMove(action));
1403 oldDocumentLoader->setLastCheckedRequest(ResourceRequest());
1404 policyChecker().stopCheck();
1405 policyChecker().setLoadType(newLoadType);
1406 RELEASE_ASSERT(!isBackForwardLoadType(newLoadType) || history().provisionalItem());
1407 policyChecker().checkNavigationPolicy(WTFMove(request), ResourceResponse { } /* redirectResponse */, oldDocumentLoader.get(), WTFMove(formState), [this, protectedFrame = makeRef(m_frame)] (const ResourceRequest& request, WeakPtr<FormState>&&, NavigationPolicyDecision navigationPolicyDecision) {
1408 continueFragmentScrollAfterNavigationPolicy(request, navigationPolicyDecision == NavigationPolicyDecision::ContinueLoad);
1409 }, PolicyDecisionMode::Synchronous);
1410 return;
1411 }
1412
1413 // Must grab this now, since this load may stop the previous load and clear this flag.
1414 bool isRedirect = m_quickRedirectComing;
1415#if USE(SYSTEM_PREVIEW)
1416 bool isSystemPreview = frameLoadRequest.isSystemPreview();
1417 request.setSystemPreview(isSystemPreview);
1418 if (isSystemPreview)
1419 request.setSystemPreviewRect(frameLoadRequest.systemPreviewRect());
1420#endif
1421 loadWithNavigationAction(request, WTFMove(action), lockHistory, newLoadType, WTFMove(formState), allowNavigationToInvalidURL, frameLoadRequest.downloadAttribute(), [this, isRedirect, sameURL, newLoadType, protectedFrame = makeRef(m_frame), completionHandler = completionHandlerCaller.release()] () mutable {
1422 if (isRedirect) {
1423 m_quickRedirectComing = false;
1424 if (m_provisionalDocumentLoader)
1425 m_provisionalDocumentLoader->setIsClientRedirect(true);
1426 else if (m_policyDocumentLoader)
1427 m_policyDocumentLoader->setIsClientRedirect(true);
1428 } else if (sameURL && !isReload(newLoadType)) {
1429 // Example of this case are sites that reload the same URL with a different cookie
1430 // driving the generated content, or a master frame with links that drive a target
1431 // frame, where the user has clicked on the same link repeatedly.
1432 m_loadType = FrameLoadType::Same;
1433 }
1434 completionHandler();
1435 });
1436}
1437
1438SubstituteData FrameLoader::defaultSubstituteDataForURL(const URL& url)
1439{
1440 if (!shouldTreatURLAsSrcdocDocument(url))
1441 return SubstituteData();
1442 auto& srcdoc = m_frame.ownerElement()->attributeWithoutSynchronization(srcdocAttr);
1443 ASSERT(!srcdoc.isNull());
1444 CString encodedSrcdoc = srcdoc.string().utf8();
1445
1446 ResourceResponse response(URL(), "text/html"_s, encodedSrcdoc.length(), "UTF-8"_s);
1447 return SubstituteData(SharedBuffer::create(encodedSrcdoc.data(), encodedSrcdoc.length()), URL(), response, SubstituteData::SessionHistoryVisibility::Hidden);
1448}
1449
1450void FrameLoader::load(FrameLoadRequest&& request)
1451{
1452 RELEASE_LOG_IF_ALLOWED("load (FrameLoadRequest): frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1453
1454 if (m_inStopAllLoaders || m_inClearProvisionalLoadForPolicyCheck)
1455 return;
1456
1457 if (!request.frameName().isEmpty()) {
1458 Frame* frame = findFrameForNavigation(request.frameName());
1459 if (frame) {
1460 request.setShouldCheckNewWindowPolicy(false);
1461 if (&frame->loader() != this) {
1462 frame->loader().load(WTFMove(request));
1463 return;
1464 }
1465 }
1466 }
1467
1468 if (request.shouldCheckNewWindowPolicy()) {
1469 NavigationAction action { request.requester(), request.resourceRequest(), InitiatedByMainFrame::Unknown, NavigationType::Other, request.shouldOpenExternalURLsPolicy() };
1470 policyChecker().checkNewWindowPolicy(WTFMove(action), WTFMove(request.resourceRequest()), { }, request.frameName(), [this] (const ResourceRequest& request, WeakPtr<FormState>&& formState, const String& frameName, const NavigationAction& action, ShouldContinue shouldContinue) {
1471 continueLoadAfterNewWindowPolicy(request, formState.get(), frameName, action, shouldContinue, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Suppress);
1472 });
1473
1474 return;
1475 }
1476
1477 if (!request.hasSubstituteData())
1478 request.setSubstituteData(defaultSubstituteDataForURL(request.resourceRequest().url()));
1479
1480 Ref<DocumentLoader> loader = m_client.createDocumentLoader(request.resourceRequest(), request.substituteData());
1481 loader->setAllowsWebArchiveForMainFrame(request.isRequestFromClientOrUserInput());
1482 addSameSiteInfoToRequestIfNeeded(loader->request());
1483 applyShouldOpenExternalURLsPolicyToNewDocumentLoader(m_frame, loader, request);
1484
1485 if (request.shouldTreatAsContinuingLoad()) {
1486 loader->setClientRedirectSourceForHistory(request.clientRedirectSourceForHistory());
1487 if (request.lockBackForwardList() == LockBackForwardList::Yes) {
1488 loader->setIsClientRedirect(true);
1489 m_loadType = FrameLoadType::RedirectWithLockedBackForwardList;
1490 }
1491 }
1492
1493 SetForScope<LoadContinuingState> continuingLoadGuard(m_currentLoadContinuingState, request.shouldTreatAsContinuingLoad() ? LoadContinuingState::ContinuingWithRequest : LoadContinuingState::NotContinuing);
1494 load(loader.get());
1495}
1496
1497void FrameLoader::loadWithNavigationAction(const ResourceRequest& request, NavigationAction&& action, LockHistory lockHistory, FrameLoadType type, RefPtr<FormState>&& formState, AllowNavigationToInvalidURL allowNavigationToInvalidURL, const String& downloadAttribute, CompletionHandler<void()>&& completionHandler)
1498{
1499 RELEASE_LOG_IF_ALLOWED("loadWithNavigationAction: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1500
1501 Ref<DocumentLoader> loader = m_client.createDocumentLoader(request, defaultSubstituteDataForURL(request.url()));
1502 loader->setDownloadAttribute(downloadAttribute);
1503 applyShouldOpenExternalURLsPolicyToNewDocumentLoader(m_frame, loader, action.initiatedByMainFrame(), action.shouldOpenExternalURLsPolicy());
1504
1505 if (lockHistory == LockHistory::Yes && m_documentLoader)
1506 loader->setClientRedirectSourceForHistory(m_documentLoader->didCreateGlobalHistoryEntry() ? m_documentLoader->urlForHistory().string() : m_documentLoader->clientRedirectSourceForHistory());
1507
1508 loader->setTriggeringAction(WTFMove(action));
1509 if (m_documentLoader)
1510 loader->setOverrideEncoding(m_documentLoader->overrideEncoding());
1511
1512 loadWithDocumentLoader(loader.ptr(), type, WTFMove(formState), allowNavigationToInvalidURL, ShouldTreatAsContinuingLoad::No, WTFMove(completionHandler));
1513}
1514
1515void FrameLoader::load(DocumentLoader& newDocumentLoader)
1516{
1517 RELEASE_LOG_IF_ALLOWED("load (DocumentLoader): frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1518
1519 ResourceRequest& r = newDocumentLoader.request();
1520 addExtraFieldsToMainResourceRequest(r);
1521 FrameLoadType type;
1522
1523 if (shouldTreatURLAsSameAsCurrent(newDocumentLoader.originalRequest().url())) {
1524 r.setCachePolicy(ResourceRequestCachePolicy::ReloadIgnoringCacheData);
1525 type = FrameLoadType::Same;
1526 } else if (shouldTreatURLAsSameAsCurrent(newDocumentLoader.unreachableURL()) && isReload(m_loadType))
1527 type = m_loadType;
1528 else if (m_loadType == FrameLoadType::RedirectWithLockedBackForwardList && ((!newDocumentLoader.unreachableURL().isEmpty() && newDocumentLoader.substituteData().isValid()) || shouldTreatCurrentLoadAsContinuingLoad()))
1529 type = FrameLoadType::RedirectWithLockedBackForwardList;
1530 else
1531 type = FrameLoadType::Standard;
1532
1533 if (m_documentLoader)
1534 newDocumentLoader.setOverrideEncoding(m_documentLoader->overrideEncoding());
1535
1536 // When we loading alternate content for an unreachable URL that we're
1537 // visiting in the history list, we treat it as a reload so the history list
1538 // is appropriately maintained.
1539 //
1540 // FIXME: This seems like a dangerous overloading of the meaning of "FrameLoadType::Reload" ...
1541 // shouldn't a more explicit type of reload be defined, that means roughly
1542 // "load without affecting history" ?
1543 if (shouldReloadToHandleUnreachableURL(newDocumentLoader)) {
1544 // shouldReloadToHandleUnreachableURL returns true only when the original load type is back-forward.
1545 // In this case we should save the document state now. Otherwise the state can be lost because load type is
1546 // changed and updateForBackForwardNavigation() will not be called when loading is committed.
1547 history().saveDocumentAndScrollState();
1548
1549 ASSERT(type == FrameLoadType::Standard);
1550 type = FrameLoadType::Reload;
1551 }
1552
1553 loadWithDocumentLoader(&newDocumentLoader, type, nullptr, AllowNavigationToInvalidURL::Yes, ShouldTreatAsContinuingLoad::No);
1554}
1555
1556void FrameLoader::loadWithDocumentLoader(DocumentLoader* loader, FrameLoadType type, RefPtr<FormState>&& formState, AllowNavigationToInvalidURL allowNavigationToInvalidURL, ShouldTreatAsContinuingLoad, CompletionHandler<void()>&& completionHandler)
1557{
1558 RELEASE_LOG_IF_ALLOWED("loadWithDocumentLoader: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1559
1560 // Retain because dispatchBeforeLoadEvent may release the last reference to it.
1561 Ref<Frame> protect(m_frame);
1562
1563 CompletionHandlerCallingScope completionHandlerCaller(WTFMove(completionHandler));
1564
1565 ASSERT(m_client.hasWebView());
1566
1567 // Unfortunately the view must be non-nil, this is ultimately due
1568 // to parser requiring a FrameView. We should fix this dependency.
1569
1570 ASSERT(m_frame.view());
1571
1572 if (!isNavigationAllowed())
1573 return;
1574
1575 if (m_frame.document())
1576 m_previousURL = m_frame.document()->url();
1577
1578 const URL& newURL = loader->request().url();
1579
1580 // Only the first iframe navigation or the first iframe navigation after about:blank should be reported.
1581 // https://www.w3.org/TR/resource-timing-2/#resources-included-in-the-performanceresourcetiming-interface
1582 if (m_shouldReportResourceTimingToParentFrame && !m_previousURL.isNull() && m_previousURL != WTF::blankURL())
1583 m_shouldReportResourceTimingToParentFrame = false;
1584
1585 // Log main frame navigation types.
1586 if (m_frame.isMainFrame()) {
1587 if (auto* page = m_frame.page()) {
1588 RELEASE_LOG_IF_ALLOWED("loadWithDocumentLoader: main frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1589 page->mainFrameLoadStarted(newURL, type);
1590 page->performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadStarted);
1591 }
1592 }
1593
1594 policyChecker().setLoadType(type);
1595 RELEASE_ASSERT(!isBackForwardLoadType(type) || history().provisionalItem());
1596 bool isFormSubmission = formState;
1597
1598 const String& httpMethod = loader->request().httpMethod();
1599
1600 if (shouldPerformFragmentNavigation(isFormSubmission, httpMethod, policyChecker().loadType(), newURL)) {
1601 RefPtr<DocumentLoader> oldDocumentLoader = m_documentLoader;
1602 NavigationAction action { *m_frame.document(), loader->request(), InitiatedByMainFrame::Unknown, policyChecker().loadType(), isFormSubmission };
1603
1604 oldDocumentLoader->setTriggeringAction(WTFMove(action));
1605 oldDocumentLoader->setLastCheckedRequest(ResourceRequest());
1606 policyChecker().stopCheck();
1607 RELEASE_ASSERT(!isBackForwardLoadType(policyChecker().loadType()) || history().provisionalItem());
1608 policyChecker().checkNavigationPolicy(ResourceRequest(loader->request()), ResourceResponse { } /* redirectResponse */, oldDocumentLoader.get(), WTFMove(formState), [this, protectedFrame = makeRef(m_frame)] (const ResourceRequest& request, WeakPtr<FormState>&&, NavigationPolicyDecision navigationPolicyDecision) {
1609 continueFragmentScrollAfterNavigationPolicy(request, navigationPolicyDecision == NavigationPolicyDecision::ContinueLoad);
1610 }, PolicyDecisionMode::Synchronous);
1611 return;
1612 }
1613
1614 if (Frame* parent = m_frame.tree().parent())
1615 loader->setOverrideEncoding(parent->loader().documentLoader()->overrideEncoding());
1616
1617 policyChecker().stopCheck();
1618 setPolicyDocumentLoader(loader);
1619 if (loader->triggeringAction().isEmpty())
1620 loader->setTriggeringAction({ *m_frame.document(), loader->request(), InitiatedByMainFrame::Unknown, policyChecker().loadType(), isFormSubmission });
1621
1622 if (Element* ownerElement = m_frame.ownerElement()) {
1623 // We skip dispatching the beforeload event if we've already
1624 // committed a real document load because the event would leak
1625 // subsequent activity by the frame which the parent frame isn't
1626 // supposed to learn. For example, if the child frame navigated to
1627 // a new URL, the parent frame shouldn't learn the URL.
1628 if (!m_stateMachine.committedFirstRealDocumentLoad()
1629 && !ownerElement->dispatchBeforeLoadEvent(loader->request().url().string())) {
1630 continueLoadAfterNavigationPolicy(loader->request(), formState.get(), NavigationPolicyDecision::IgnoreLoad, allowNavigationToInvalidURL);
1631 return;
1632 }
1633 }
1634
1635 m_frame.navigationScheduler().cancel(NewLoadInProgress::Yes);
1636
1637 if (shouldTreatCurrentLoadAsContinuingLoad()) {
1638 continueLoadAfterNavigationPolicy(loader->request(), formState.get(), NavigationPolicyDecision::ContinueLoad, allowNavigationToInvalidURL);
1639 return;
1640 }
1641
1642 RELEASE_ASSERT(!isBackForwardLoadType(policyChecker().loadType()) || history().provisionalItem());
1643 policyChecker().checkNavigationPolicy(ResourceRequest(loader->request()), ResourceResponse { } /* redirectResponse */, loader, WTFMove(formState), [this, protectedFrame = makeRef(m_frame), allowNavigationToInvalidURL, completionHandler = completionHandlerCaller.release()] (const ResourceRequest& request, WeakPtr<FormState>&& formState, NavigationPolicyDecision navigationPolicyDecision) mutable {
1644 continueLoadAfterNavigationPolicy(request, formState.get(), navigationPolicyDecision, allowNavigationToInvalidURL);
1645 completionHandler();
1646 }, PolicyDecisionMode::Asynchronous);
1647}
1648
1649void FrameLoader::clearProvisionalLoadForPolicyCheck()
1650{
1651 if (!m_policyDocumentLoader || !m_provisionalDocumentLoader || m_inClearProvisionalLoadForPolicyCheck)
1652 return;
1653
1654 SetForScope<bool> change(m_inClearProvisionalLoadForPolicyCheck, true);
1655 m_provisionalDocumentLoader->stopLoading();
1656 setProvisionalDocumentLoader(nullptr);
1657}
1658
1659void FrameLoader::reportLocalLoadFailed(Frame* frame, const String& url)
1660{
1661 ASSERT(!url.isEmpty());
1662 if (!frame)
1663 return;
1664
1665 frame->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Not allowed to load local resource: " + url);
1666}
1667
1668void FrameLoader::reportBlockedPortFailed(Frame* frame, const String& url)
1669{
1670 ASSERT(!url.isEmpty());
1671 if (!frame)
1672 return;
1673
1674 frame->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Not allowed to use restricted network port: " + url);
1675}
1676
1677void FrameLoader::reportAuthenticationChallengeBlocked(Frame* frame, const URL& url, const String& reason)
1678{
1679 if (!frame)
1680 return;
1681
1682 frame->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, makeString("Blocked ", url.stringCenterEllipsizedToLength(), " from asking for credentials because ", reason, '.'));
1683}
1684
1685const ResourceRequest& FrameLoader::initialRequest() const
1686{
1687 return activeDocumentLoader()->originalRequest();
1688}
1689
1690bool FrameLoader::willLoadMediaElementURL(URL& url, Node& initiatorNode)
1691{
1692#if PLATFORM(IOS_FAMILY)
1693 // MobileStore depends on the iOS 4.0 era client delegate method because webView:resource:willSendRequest:redirectResponse:fromDataSource
1694 // doesn't let them tell when a load request is coming from a media element. See <rdar://problem/8266916> for more details.
1695 if (IOSApplication::isMobileStore())
1696 return m_client.shouldLoadMediaElementURL(url);
1697#endif
1698
1699 ResourceRequest request(url);
1700 request.setInspectorInitiatorNodeIdentifier(InspectorInstrumentation::identifierForNode(initiatorNode));
1701
1702 unsigned long identifier;
1703 ResourceError error;
1704 requestFromDelegate(request, identifier, error);
1705 notifier().sendRemainingDelegateMessages(m_documentLoader.get(), identifier, request, ResourceResponse(url, String(), -1, String()), 0, -1, -1, error);
1706
1707 url = request.url();
1708
1709 return error.isNull();
1710}
1711
1712bool FrameLoader::shouldReloadToHandleUnreachableURL(DocumentLoader& docLoader)
1713{
1714 URL unreachableURL = docLoader.unreachableURL();
1715
1716 if (unreachableURL.isEmpty())
1717 return false;
1718
1719 if (!isBackForwardLoadType(policyChecker().loadType()))
1720 return false;
1721
1722 // We only treat unreachableURLs specially during the delegate callbacks
1723 // for provisional load errors and navigation policy decisions. The former
1724 // case handles well-formed URLs that can't be loaded, and the latter
1725 // case handles malformed URLs and unknown schemes. Loading alternate content
1726 // at other times behaves like a standard load.
1727 if (policyChecker().delegateIsDecidingNavigationPolicy() || policyChecker().delegateIsHandlingUnimplementablePolicy())
1728 return m_policyDocumentLoader && unreachableURL == m_policyDocumentLoader->request().url();
1729
1730 return unreachableURL == m_provisionalLoadErrorBeingHandledURL;
1731}
1732
1733void FrameLoader::reloadWithOverrideEncoding(const String& encoding)
1734{
1735 if (!m_documentLoader)
1736 return;
1737
1738 RELEASE_LOG_IF_ALLOWED("reloadWithOverrideEncoding: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1739
1740 ResourceRequest request = m_documentLoader->request();
1741 URL unreachableURL = m_documentLoader->unreachableURL();
1742 if (!unreachableURL.isEmpty())
1743 request.setURL(unreachableURL);
1744
1745 // FIXME: If the resource is a result of form submission and is not cached, the form will be silently resubmitted.
1746 // We should ask the user for confirmation in this case.
1747 request.setCachePolicy(ResourceRequestCachePolicy::ReturnCacheDataElseLoad);
1748
1749 Ref<DocumentLoader> loader = m_client.createDocumentLoader(request, defaultSubstituteDataForURL(request.url()));
1750 applyShouldOpenExternalURLsPolicyToNewDocumentLoader(m_frame, loader, InitiatedByMainFrame::Unknown, m_documentLoader->shouldOpenExternalURLsPolicyToPropagate());
1751
1752 setPolicyDocumentLoader(loader.ptr());
1753
1754 loader->setOverrideEncoding(encoding);
1755
1756 loadWithDocumentLoader(loader.ptr(), FrameLoadType::Reload, { }, AllowNavigationToInvalidURL::Yes, ShouldTreatAsContinuingLoad::No);
1757}
1758
1759void FrameLoader::reload(OptionSet<ReloadOption> options)
1760{
1761 if (!m_documentLoader)
1762 return;
1763
1764 // If a window is created by javascript, its main frame can have an empty but non-nil URL.
1765 // Reloading in this case will lose the current contents (see 4151001).
1766 if (m_documentLoader->request().url().isEmpty())
1767 return;
1768
1769 RELEASE_LOG_IF_ALLOWED("reload: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1770
1771 // Replace error-page URL with the URL we were trying to reach.
1772 ResourceRequest initialRequest = m_documentLoader->request();
1773 URL unreachableURL = m_documentLoader->unreachableURL();
1774 if (!unreachableURL.isEmpty())
1775 initialRequest.setURL(unreachableURL);
1776
1777 // Create a new document loader for the reload, this will become m_documentLoader eventually,
1778 // but first it has to be the "policy" document loader, and then the "provisional" document loader.
1779 Ref<DocumentLoader> loader = m_client.createDocumentLoader(initialRequest, defaultSubstituteDataForURL(initialRequest.url()));
1780 loader->setAllowsWebArchiveForMainFrame(m_documentLoader->allowsWebArchiveForMainFrame());
1781 applyShouldOpenExternalURLsPolicyToNewDocumentLoader(m_frame, loader, InitiatedByMainFrame::Unknown, m_documentLoader->shouldOpenExternalURLsPolicyToPropagate());
1782
1783 loader->setUserContentExtensionsEnabled(!options.contains(ReloadOption::DisableContentBlockers));
1784
1785 ResourceRequest& request = loader->request();
1786
1787 // FIXME: We don't have a mechanism to revalidate the main resource without reloading at the moment.
1788 request.setCachePolicy(ResourceRequestCachePolicy::ReloadIgnoringCacheData);
1789
1790 addSameSiteInfoToRequestIfNeeded(request);
1791
1792 // If we're about to re-post, set up action so the application can warn the user.
1793 if (request.httpMethod() == "POST")
1794 loader->setTriggeringAction({ *m_frame.document(), request, InitiatedByMainFrame::Unknown, NavigationType::FormResubmitted });
1795
1796 loader->setOverrideEncoding(m_documentLoader->overrideEncoding());
1797
1798 auto frameLoadTypeForReloadOptions = [] (auto options) {
1799 if (options & ReloadOption::FromOrigin)
1800 return FrameLoadType::ReloadFromOrigin;
1801 if (options & ReloadOption::ExpiredOnly)
1802 return FrameLoadType::ReloadExpiredOnly;
1803 return FrameLoadType::Reload;
1804 };
1805
1806 loadWithDocumentLoader(loader.ptr(), frameLoadTypeForReloadOptions(options), { }, AllowNavigationToInvalidURL::Yes, ShouldTreatAsContinuingLoad::No);
1807}
1808
1809void FrameLoader::stopAllLoaders(ClearProvisionalItemPolicy clearProvisionalItemPolicy)
1810{
1811 if (m_frame.document() && m_frame.document()->pageCacheState() == Document::InPageCache)
1812 return;
1813
1814 if (!isStopLoadingAllowed())
1815 return;
1816
1817 // If this method is called from within this method, infinite recursion can occur (3442218). Avoid this.
1818 if (m_inStopAllLoaders)
1819 return;
1820
1821 // This method might dispatch events.
1822 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isScriptAllowed());
1823
1824 // Calling stopLoading() on the provisional document loader can blow away
1825 // the frame from underneath.
1826 Ref<Frame> protect(m_frame);
1827
1828 m_inStopAllLoaders = true;
1829
1830 policyChecker().stopCheck();
1831
1832 // If no new load is in progress, we should clear the provisional item from history
1833 // before we call stopLoading.
1834 if (clearProvisionalItemPolicy == ShouldClearProvisionalItem)
1835 history().setProvisionalItem(nullptr);
1836
1837 for (RefPtr<Frame> child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
1838 child->loader().stopAllLoaders(clearProvisionalItemPolicy);
1839 if (m_provisionalDocumentLoader)
1840 m_provisionalDocumentLoader->stopLoading();
1841 if (m_documentLoader)
1842 m_documentLoader->stopLoading();
1843
1844 setProvisionalDocumentLoader(nullptr);
1845
1846 m_inStopAllLoaders = false;
1847}
1848
1849void FrameLoader::stopAllLoadersAndCheckCompleteness()
1850{
1851 stopAllLoaders();
1852
1853 if (!m_checkTimer.isActive())
1854 return;
1855
1856 m_checkTimer.stop();
1857 m_checkingLoadCompleteForDetachment = true;
1858 checkCompletenessNow();
1859 m_checkingLoadCompleteForDetachment = false;
1860}
1861
1862void FrameLoader::stopForUserCancel(bool deferCheckLoadComplete)
1863{
1864 // Calling stopAllLoaders can cause the frame to be deallocated, including the frame loader.
1865 Ref<Frame> protectedFrame(m_frame);
1866
1867 stopAllLoaders();
1868
1869#if PLATFORM(IOS_FAMILY)
1870 // Lay out immediately when stopping to immediately clear the old page if we just committed this one
1871 // but haven't laid out/painted yet.
1872 // FIXME: Is this behavior specific to iOS? Or should we expose a setting to toggle this behavior?
1873 if (m_frame.view() && !m_frame.view()->didFirstLayout())
1874 m_frame.view()->layoutContext().layout();
1875#endif
1876
1877 if (deferCheckLoadComplete)
1878 scheduleCheckLoadComplete();
1879 else if (m_frame.page())
1880 checkLoadComplete();
1881}
1882
1883DocumentLoader* FrameLoader::activeDocumentLoader() const
1884{
1885 if (m_state == FrameStateProvisional)
1886 return m_provisionalDocumentLoader.get();
1887 return m_documentLoader.get();
1888}
1889
1890bool FrameLoader::isLoading() const
1891{
1892 DocumentLoader* docLoader = activeDocumentLoader();
1893 if (!docLoader)
1894 return false;
1895 return docLoader->isLoading();
1896}
1897
1898bool FrameLoader::frameHasLoaded() const
1899{
1900 return m_stateMachine.committedFirstRealDocumentLoad() || (m_provisionalDocumentLoader && !m_stateMachine.creatingInitialEmptyDocument());
1901}
1902
1903void FrameLoader::setDocumentLoader(DocumentLoader* loader)
1904{
1905 if (!loader && !m_documentLoader)
1906 return;
1907
1908 if (loader == m_documentLoader)
1909 return;
1910
1911 ASSERT(loader != m_documentLoader);
1912 ASSERT(!loader || loader->frameLoader() == this);
1913
1914 m_client.prepareForDataSourceReplacement();
1915 detachChildren();
1916
1917 // detachChildren() can trigger this frame's unload event, and therefore
1918 // script can run and do just about anything. For example, an unload event that calls
1919 // document.write("") on its parent frame can lead to a recursive detachChildren()
1920 // invocation for this frame. In that case, we can end up at this point with a
1921 // loader that hasn't been deleted but has been detached from its frame. Such a
1922 // DocumentLoader has been sufficiently detached that we'll end up in an inconsistent
1923 // state if we try to use it.
1924 if (loader && !loader->frame())
1925 return;
1926
1927 if (m_documentLoader)
1928 m_documentLoader->detachFromFrame();
1929
1930 m_documentLoader = loader;
1931}
1932
1933void FrameLoader::setPolicyDocumentLoader(DocumentLoader* loader)
1934{
1935 if (m_policyDocumentLoader == loader)
1936 return;
1937
1938 if (loader)
1939 loader->attachToFrame(m_frame);
1940 if (m_policyDocumentLoader
1941 && m_policyDocumentLoader != m_provisionalDocumentLoader
1942 && m_policyDocumentLoader != m_documentLoader)
1943 m_policyDocumentLoader->detachFromFrame();
1944
1945 m_policyDocumentLoader = loader;
1946}
1947
1948void FrameLoader::setProvisionalDocumentLoader(DocumentLoader* loader)
1949{
1950 ASSERT(!loader || !m_provisionalDocumentLoader);
1951 ASSERT(!loader || loader->frameLoader() == this);
1952
1953 if (m_provisionalDocumentLoader && m_provisionalDocumentLoader != m_documentLoader)
1954 m_provisionalDocumentLoader->detachFromFrame();
1955
1956 m_provisionalDocumentLoader = loader;
1957}
1958
1959void FrameLoader::setState(FrameState newState)
1960{
1961 FrameState oldState = m_state;
1962 m_state = newState;
1963
1964 if (newState == FrameStateProvisional)
1965 provisionalLoadStarted();
1966 else if (newState == FrameStateComplete) {
1967 frameLoadCompleted();
1968 if (m_documentLoader)
1969 m_documentLoader->stopRecordingResponses();
1970 if (m_frame.isMainFrame() && oldState != newState) {
1971 RELEASE_LOG_IF_ALLOWED("setState: main frame load completed (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
1972 m_frame.page()->performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadCompleted);
1973 }
1974 }
1975}
1976
1977void FrameLoader::clearProvisionalLoad()
1978{
1979 setProvisionalDocumentLoader(nullptr);
1980 m_progressTracker->progressCompleted();
1981 setState(FrameStateComplete);
1982}
1983
1984void FrameLoader::commitProvisionalLoad()
1985{
1986 RefPtr<DocumentLoader> pdl = m_provisionalDocumentLoader;
1987 Ref<Frame> protect(m_frame);
1988
1989 std::unique_ptr<CachedPage> cachedPage;
1990 if (m_loadingFromCachedPage && history().provisionalItem())
1991 cachedPage = PageCache::singleton().take(*history().provisionalItem(), m_frame.page());
1992
1993 LOG(PageCache, "WebCoreLoading %s: About to commit provisional load from previous URL '%s' to new URL '%s' with cached page %p", m_frame.tree().uniqueName().string().utf8().data(),
1994 m_frame.document() ? m_frame.document()->url().stringCenterEllipsizedToLength().utf8().data() : "",
1995 pdl ? pdl->url().stringCenterEllipsizedToLength().utf8().data() : "<no provisional DocumentLoader>", cachedPage.get());
1996
1997 willTransitionToCommitted();
1998
1999 if (!m_frame.tree().parent() && history().currentItem() && history().currentItem() != history().provisionalItem()) {
2000 // Check to see if we need to cache the page we are navigating away from into the back/forward cache.
2001 // We are doing this here because we know for sure that a new page is about to be loaded.
2002 PageCache::singleton().addIfCacheable(*history().currentItem(), m_frame.page());
2003
2004 WebCore::jettisonExpensiveObjectsOnTopLevelNavigation();
2005 }
2006
2007 if (m_loadType != FrameLoadType::Replace)
2008 closeOldDataSources();
2009
2010 if (!cachedPage && !m_stateMachine.creatingInitialEmptyDocument())
2011 m_client.makeRepresentation(pdl.get());
2012
2013 transitionToCommitted(cachedPage.get());
2014
2015 if (pdl && m_documentLoader) {
2016 // Check if the destination page is allowed to access the previous page's timing information.
2017 Ref<SecurityOrigin> securityOrigin(SecurityOrigin::create(pdl->request().url()));
2018 m_documentLoader->timing().setHasSameOriginAsPreviousDocument(securityOrigin.get().canRequest(m_previousURL));
2019 }
2020
2021 // Call clientRedirectCancelledOrFinished() here so that the frame load delegate is notified that the redirect's
2022 // status has changed, if there was a redirect. The frame load delegate may have saved some state about
2023 // the redirect in its -webView:willPerformClientRedirectToURL:delay:fireDate:forFrame:. Since we are
2024 // just about to commit a new page, there cannot possibly be a pending redirect at this point.
2025 if (m_sentRedirectNotification)
2026 clientRedirectCancelledOrFinished(NewLoadInProgress::No);
2027
2028 if (cachedPage && cachedPage->document()) {
2029#if PLATFORM(IOS_FAMILY)
2030 // FIXME: CachedPage::restore() would dispatch viewport change notification. However UIKit expects load
2031 // commit to happen before any changes to viewport arguments and dealing with this there is difficult.
2032 m_frame.page()->chrome().setDispatchViewportDataDidChangeSuppressed(true);
2033#endif
2034 willRestoreFromCachedPage();
2035
2036 // Start request for the main resource and dispatch didReceiveResponse before the load is committed for
2037 // consistency with all other loads. See https://bugs.webkit.org/show_bug.cgi?id=150927.
2038 ResourceError mainResouceError;
2039 unsigned long mainResourceIdentifier;
2040 ResourceRequest mainResourceRequest(cachedPage->documentLoader()->request());
2041 requestFromDelegate(mainResourceRequest, mainResourceIdentifier, mainResouceError);
2042 notifier().dispatchDidReceiveResponse(cachedPage->documentLoader(), mainResourceIdentifier, cachedPage->documentLoader()->response());
2043
2044 Optional<HasInsecureContent> hasInsecureContent = cachedPage->cachedMainFrame()->hasInsecureContent();
2045
2046 dispatchDidCommitLoad(hasInsecureContent);
2047
2048 // FIXME: This API should be turned around so that we ground CachedPage into the Page.
2049 cachedPage->restore(*m_frame.page());
2050
2051#if PLATFORM(IOS_FAMILY)
2052 m_frame.page()->chrome().setDispatchViewportDataDidChangeSuppressed(false);
2053 m_frame.page()->chrome().dispatchViewportPropertiesDidChange(m_frame.page()->viewportArguments());
2054#endif
2055 m_frame.page()->chrome().dispatchDisabledAdaptationsDidChange(m_frame.page()->disabledAdaptations());
2056
2057 auto& title = m_documentLoader->title();
2058 if (!title.string.isNull())
2059 m_client.dispatchDidReceiveTitle(title);
2060
2061 // Send remaining notifications for the main resource.
2062 notifier().sendRemainingDelegateMessages(m_documentLoader.get(), mainResourceIdentifier, mainResourceRequest, ResourceResponse(),
2063 nullptr, static_cast<int>(m_documentLoader->response().expectedContentLength()), 0, mainResouceError);
2064
2065 checkCompleted();
2066 } else
2067 didOpenURL();
2068
2069 LOG(Loading, "WebCoreLoading %s: Finished committing provisional load to URL %s", m_frame.tree().uniqueName().string().utf8().data(),
2070 m_frame.document() ? m_frame.document()->url().stringCenterEllipsizedToLength().utf8().data() : "");
2071
2072 if (m_loadType == FrameLoadType::Standard && m_documentLoader && m_documentLoader->isClientRedirect())
2073 history().updateForClientRedirect();
2074
2075 if (m_loadingFromCachedPage) {
2076 // Note, didReceiveDocType is expected to be called for cached pages. See <rdar://problem/5906758> for more details.
2077 if (auto* page = m_frame.page())
2078 page->chrome().didReceiveDocType(m_frame);
2079 m_frame.document()->resume(ReasonForSuspension::PageCache);
2080
2081 // Force a layout to update view size and thereby update scrollbars.
2082#if PLATFORM(IOS_FAMILY)
2083 if (!m_client.forceLayoutOnRestoreFromPageCache())
2084 m_frame.view()->forceLayout();
2085#else
2086 m_frame.view()->forceLayout();
2087#endif
2088
2089 // Main resource delegates were already sent, so we skip the first response here.
2090 for (unsigned i = 1; i < m_documentLoader->responses().size(); ++i) {
2091 const auto& response = m_documentLoader->responses()[i];
2092 // FIXME: If the WebKit client changes or cancels the request, this is not respected.
2093 ResourceError error;
2094 unsigned long identifier;
2095 ResourceRequest request(response.url());
2096 requestFromDelegate(request, identifier, error);
2097 // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing.
2098 // However, with today's computers and networking speeds, this won't happen in practice.
2099 // Could be an issue with a giant local file.
2100 notifier().sendRemainingDelegateMessages(m_documentLoader.get(), identifier, request, response, 0, static_cast<int>(response.expectedContentLength()), 0, error);
2101 }
2102
2103 // FIXME: Why only this frame and not parent frames?
2104 checkLoadCompleteForThisFrame();
2105 }
2106}
2107
2108void FrameLoader::transitionToCommitted(CachedPage* cachedPage)
2109{
2110 ASSERT(m_client.hasWebView());
2111 ASSERT(m_state == FrameStateProvisional);
2112
2113 if (m_state != FrameStateProvisional)
2114 return;
2115
2116 if (FrameView* view = m_frame.view()) {
2117 if (ScrollAnimator* scrollAnimator = view->existingScrollAnimator())
2118 scrollAnimator->cancelAnimations();
2119 }
2120
2121 m_client.setCopiesOnScroll();
2122 history().updateForCommit();
2123
2124 // The call to closeURL() invokes the unload event handler, which can execute arbitrary
2125 // JavaScript. If the script initiates a new load, we need to abandon the current load,
2126 // or the two will stomp each other.
2127 DocumentLoader* pdl = m_provisionalDocumentLoader.get();
2128 if (m_documentLoader)
2129 closeURL();
2130 if (pdl != m_provisionalDocumentLoader)
2131 return;
2132
2133 if (m_documentLoader)
2134 m_documentLoader->stopLoadingSubresources();
2135 if (m_documentLoader)
2136 m_documentLoader->stopLoadingPlugIns();
2137
2138 // Setting our document loader invokes the unload event handler of our child frames.
2139 // Script can do anything. If the script initiates a new load, we need to abandon the
2140 // current load or the two will stomp each other.
2141 setDocumentLoader(m_provisionalDocumentLoader.get());
2142 if (pdl != m_provisionalDocumentLoader)
2143 return;
2144 setProvisionalDocumentLoader(nullptr);
2145
2146 // Nothing else can interrupt this commit - set the Provisional->Committed transition in stone
2147 setState(FrameStateCommittedPage);
2148
2149 // Handle adding the URL to the back/forward list.
2150 DocumentLoader* dl = m_documentLoader.get();
2151
2152 switch (m_loadType) {
2153 case FrameLoadType::Forward:
2154 case FrameLoadType::Back:
2155 case FrameLoadType::IndexedBackForward:
2156 if (m_frame.page()) {
2157 // If the first load within a frame is a navigation within a back/forward list that was attached
2158 // without any of the items being loaded then we need to update the history in a similar manner as
2159 // for a standard load with the exception of updating the back/forward list (<rdar://problem/8091103>).
2160 if (!m_stateMachine.committedFirstRealDocumentLoad() && m_frame.isMainFrame())
2161 history().updateForStandardLoad(HistoryController::UpdateAllExceptBackForwardList);
2162
2163 history().updateForBackForwardNavigation();
2164
2165 // For cached pages, CachedFrame::restore will take care of firing the popstate event with the history item's state object
2166 if (history().currentItem() && !cachedPage)
2167 m_pendingStateObject = history().currentItem()->stateObject();
2168
2169 // Create a document view for this document, or used the cached view.
2170 if (cachedPage) {
2171 DocumentLoader* cachedDocumentLoader = cachedPage->documentLoader();
2172 ASSERT(cachedDocumentLoader);
2173 cachedDocumentLoader->attachToFrame(m_frame);
2174 m_client.transitionToCommittedFromCachedFrame(cachedPage->cachedMainFrame());
2175 } else
2176 m_client.transitionToCommittedForNewPage();
2177 }
2178 break;
2179
2180 case FrameLoadType::Reload:
2181 case FrameLoadType::ReloadFromOrigin:
2182 case FrameLoadType::ReloadExpiredOnly:
2183 case FrameLoadType::Same:
2184 case FrameLoadType::Replace:
2185 history().updateForReload();
2186 m_client.transitionToCommittedForNewPage();
2187 break;
2188
2189 case FrameLoadType::Standard:
2190 history().updateForStandardLoad();
2191 if (m_frame.view())
2192 m_frame.view()->setScrollbarsSuppressed(true);
2193 m_client.transitionToCommittedForNewPage();
2194 break;
2195
2196 case FrameLoadType::RedirectWithLockedBackForwardList:
2197 history().updateForRedirectWithLockedBackForwardList();
2198 m_client.transitionToCommittedForNewPage();
2199 break;
2200 }
2201
2202 m_documentLoader->writer().setMIMEType(dl->responseMIMEType());
2203
2204 // Tell the client we've committed this URL.
2205 ASSERT(m_frame.view());
2206
2207 if (m_stateMachine.creatingInitialEmptyDocument())
2208 return;
2209
2210 if (!m_stateMachine.committedFirstRealDocumentLoad())
2211 m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocumentPostCommit);
2212}
2213
2214void FrameLoader::clientRedirectCancelledOrFinished(NewLoadInProgress newLoadInProgress)
2215{
2216 // Note that -webView:didCancelClientRedirectForFrame: is called on the frame load delegate even if
2217 // the redirect succeeded. We should either rename this API, or add a new method, like
2218 // -webView:didFinishClientRedirectForFrame:
2219 m_client.dispatchDidCancelClientRedirect();
2220
2221 if (newLoadInProgress == NewLoadInProgress::No)
2222 m_quickRedirectComing = false;
2223
2224 m_sentRedirectNotification = false;
2225}
2226
2227void FrameLoader::clientRedirected(const URL& url, double seconds, WallTime fireDate, LockBackForwardList lockBackForwardList)
2228{
2229 m_client.dispatchWillPerformClientRedirect(url, seconds, fireDate, lockBackForwardList);
2230
2231 // Remember that we sent a redirect notification to the frame load delegate so that when we commit
2232 // the next provisional load, we can send a corresponding -webView:didCancelClientRedirectForFrame:
2233 m_sentRedirectNotification = true;
2234
2235 // If a "quick" redirect comes in, we set a special mode so we treat the next
2236 // load as part of the original navigation. If we don't have a document loader, we have
2237 // no "original" load on which to base a redirect, so we treat the redirect as a normal load.
2238 // Loads triggered by JavaScript form submissions never count as quick redirects.
2239 m_quickRedirectComing = (lockBackForwardList == LockBackForwardList::Yes || history().currentItemShouldBeReplaced()) && m_documentLoader && !m_isExecutingJavaScriptFormAction;
2240}
2241
2242bool FrameLoader::shouldReload(const URL& currentURL, const URL& destinationURL)
2243{
2244 // This function implements the rule: "Don't reload if navigating by fragment within
2245 // the same URL, but do reload if going to a new URL or to the same URL with no
2246 // fragment identifier at all."
2247 if (!destinationURL.hasFragmentIdentifier())
2248 return true;
2249 return !equalIgnoringFragmentIdentifier(currentURL, destinationURL);
2250}
2251
2252void FrameLoader::closeOldDataSources()
2253{
2254 // FIXME: Is it important for this traversal to be postorder instead of preorder?
2255 // If so, add helpers for postorder traversal, and use them. If not, then lets not
2256 // use a recursive algorithm here.
2257 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
2258 child->loader().closeOldDataSources();
2259
2260 if (m_documentLoader)
2261 m_client.dispatchWillClose();
2262
2263 m_client.setMainFrameDocumentReady(false); // stop giving out the actual DOMDocument to observers
2264}
2265
2266void FrameLoader::willRestoreFromCachedPage()
2267{
2268 ASSERT(!m_frame.tree().parent());
2269 ASSERT(m_frame.page());
2270 ASSERT(m_frame.isMainFrame());
2271
2272 m_frame.navigationScheduler().cancel();
2273
2274 // We still have to close the previous part page.
2275 closeURL();
2276
2277 // Delete old status bar messages (if it _was_ activated on last URL).
2278 if (m_frame.script().canExecuteScripts(NotAboutToExecuteScript)) {
2279 DOMWindow* window = m_frame.document()->domWindow();
2280 window->setStatus(String());
2281 window->setDefaultStatus(String());
2282 }
2283}
2284
2285void FrameLoader::open(CachedFrameBase& cachedFrame)
2286{
2287 m_isComplete = false;
2288
2289 // Don't re-emit the load event.
2290 m_didCallImplicitClose = true;
2291
2292 URL url = cachedFrame.url();
2293
2294 // FIXME: I suspect this block of code doesn't do anything.
2295 if (url.protocolIsInHTTPFamily() && !url.host().isEmpty() && url.path().isEmpty())
2296 url.setPath("/");
2297
2298 started();
2299 Document* document = cachedFrame.document();
2300 ASSERT(document);
2301 ASSERT(document->domWindow());
2302
2303 clear(document, true, true, cachedFrame.isMainFrame());
2304
2305 document->setPageCacheState(Document::NotInPageCache);
2306
2307 m_needsClear = true;
2308 m_isComplete = false;
2309 m_didCallImplicitClose = false;
2310 m_outgoingReferrer = url.string();
2311
2312 FrameView* view = cachedFrame.view();
2313
2314 // When navigating to a CachedFrame its FrameView should never be null. If it is we'll crash in creative ways downstream.
2315 ASSERT(view);
2316 view->setWasScrolledByUser(false);
2317
2318 Optional<IntRect> previousViewFrameRect = m_frame.view() ? m_frame.view()->frameRect() : Optional<IntRect>(WTF::nullopt);
2319 m_frame.setView(view);
2320
2321 // Use the previous ScrollView's frame rect.
2322 if (previousViewFrameRect)
2323 view->setFrameRect(previousViewFrameRect.value());
2324
2325 {
2326 // Setting the document builds the render tree and runs post style resolution callbacks that can do anything,
2327 // including loading a child frame before its been re-attached to the frame tree as part of this restore.
2328 // For example, the HTML object element may load its content into a frame in a post style resolution callback.
2329 NavigationDisabler disableNavigation { &m_frame };
2330 m_frame.setDocument(document);
2331 }
2332
2333 document->domWindow()->resumeFromPageCache();
2334
2335 updateFirstPartyForCookies();
2336
2337 cachedFrame.restore();
2338}
2339
2340bool FrameLoader::isHostedByObjectElement() const
2341{
2342 HTMLFrameOwnerElement* owner = m_frame.ownerElement();
2343 return owner && owner->hasTagName(objectTag);
2344}
2345
2346bool FrameLoader::isReplacing() const
2347{
2348 return m_loadType == FrameLoadType::Replace;
2349}
2350
2351void FrameLoader::setReplacing()
2352{
2353 m_loadType = FrameLoadType::Replace;
2354}
2355
2356bool FrameLoader::subframeIsLoading() const
2357{
2358 // It's most likely that the last added frame is the last to load so we walk backwards.
2359 for (Frame* child = m_frame.tree().lastChild(); child; child = child->tree().previousSibling()) {
2360 FrameLoader& childLoader = child->loader();
2361 DocumentLoader* documentLoader = childLoader.documentLoader();
2362 if (documentLoader && documentLoader->isLoadingInAPISense())
2363 return true;
2364 documentLoader = childLoader.provisionalDocumentLoader();
2365 if (documentLoader && documentLoader->isLoadingInAPISense())
2366 return true;
2367 documentLoader = childLoader.policyDocumentLoader();
2368 if (documentLoader)
2369 return true;
2370 }
2371 return false;
2372}
2373
2374void FrameLoader::willChangeTitle(DocumentLoader* loader)
2375{
2376 m_client.willChangeTitle(loader);
2377}
2378
2379FrameLoadType FrameLoader::loadType() const
2380{
2381 return m_loadType;
2382}
2383
2384CachePolicy FrameLoader::subresourceCachePolicy(const URL& url) const
2385{
2386 if (Page* page = m_frame.page()) {
2387 if (page->isResourceCachingDisabled())
2388 return CachePolicyReload;
2389 }
2390
2391 if (m_isComplete)
2392 return CachePolicyVerify;
2393
2394 if (m_loadType == FrameLoadType::ReloadFromOrigin)
2395 return CachePolicyReload;
2396
2397 if (Frame* parentFrame = m_frame.tree().parent()) {
2398 CachePolicy parentCachePolicy = parentFrame->loader().subresourceCachePolicy(url);
2399 if (parentCachePolicy != CachePolicyVerify)
2400 return parentCachePolicy;
2401 }
2402
2403 switch (m_loadType) {
2404 case FrameLoadType::Reload:
2405 return CachePolicyRevalidate;
2406 case FrameLoadType::Back:
2407 case FrameLoadType::Forward:
2408 case FrameLoadType::IndexedBackForward:
2409 return CachePolicyHistoryBuffer;
2410 case FrameLoadType::ReloadFromOrigin:
2411 ASSERT_NOT_REACHED(); // Already handled above.
2412 return CachePolicyReload;
2413 case FrameLoadType::RedirectWithLockedBackForwardList:
2414 case FrameLoadType::Replace:
2415 case FrameLoadType::Same:
2416 case FrameLoadType::Standard:
2417 return CachePolicyVerify;
2418 case FrameLoadType::ReloadExpiredOnly:
2419 // We know about expiration for HTTP and data. Do a normal reload otherwise.
2420 if (!url.protocolIsInHTTPFamily() && !url.protocolIsData())
2421 return CachePolicyReload;
2422 return CachePolicyVerify;
2423 }
2424
2425 RELEASE_ASSERT_NOT_REACHED();
2426 return CachePolicyVerify;
2427}
2428
2429void FrameLoader::checkLoadCompleteForThisFrame()
2430{
2431 ASSERT(m_client.hasWebView());
2432
2433 // FIXME: Should this check be done in checkLoadComplete instead of here?
2434 // FIXME: Why does this one check need to be repeated here, and not the many others from checkCompleted?
2435 if (m_frame.document()->isDelayingLoadEvent())
2436 return;
2437
2438 switch (m_state) {
2439 case FrameStateProvisional: {
2440 // FIXME: Prohibiting any provisional load failures from being sent to clients
2441 // while handling provisional load failures is too heavy. For example, the current
2442 // load will fail to cancel another ongoing load. That might prevent clients' page
2443 // load state being handled properly.
2444 if (!m_provisionalLoadErrorBeingHandledURL.isEmpty())
2445 return;
2446
2447 RefPtr<DocumentLoader> pdl = m_provisionalDocumentLoader;
2448 if (!pdl)
2449 return;
2450
2451 // If we've received any errors we may be stuck in the provisional state and actually complete.
2452 const ResourceError& error = pdl->mainDocumentError();
2453 if (error.isNull())
2454 return;
2455
2456 // Check all children first.
2457 RefPtr<HistoryItem> item;
2458 if (Page* page = m_frame.page())
2459 if (isBackForwardLoadType(loadType()))
2460 // Reset the back forward list to the last committed history item at the top level.
2461 item = page->mainFrame().loader().history().currentItem();
2462
2463 // Only reset if we aren't already going to a new provisional item.
2464 bool shouldReset = !history().provisionalItem();
2465 if (!pdl->isLoadingInAPISense() || pdl->isStopping()) {
2466 RELEASE_LOG_IF_ALLOWED("checkLoadCompleteForThisFrame: Failed provisional load (frame = %p, main = %d, isTimeout = %d, isCancellation = %d, errorCode = %d)", &m_frame, m_frame.isMainFrame(), error.isTimeout(), error.isCancellation(), error.errorCode());
2467 m_provisionalLoadErrorBeingHandledURL = m_provisionalDocumentLoader->url();
2468
2469 m_client.dispatchDidFailProvisionalLoad(error, history().provisionalItem() ? WillContinueLoading::Yes : WillContinueLoading::No);
2470#if ENABLE(CONTENT_FILTERING)
2471 if (auto contentFilter = pdl->contentFilter())
2472 contentFilter->handleProvisionalLoadFailure(error);
2473#endif
2474 m_provisionalLoadErrorBeingHandledURL = { };
2475
2476 ASSERT(!pdl->isLoading());
2477
2478 // If we're in the middle of loading multipart data, we need to restore the document loader.
2479 if (isReplacing() && !m_documentLoader.get())
2480 setDocumentLoader(m_provisionalDocumentLoader.get());
2481
2482 // Finish resetting the load state, but only if another load hasn't been started by the
2483 // delegate callback.
2484 if (pdl == m_provisionalDocumentLoader)
2485 clearProvisionalLoad();
2486 else if (activeDocumentLoader()) {
2487 URL unreachableURL = activeDocumentLoader()->unreachableURL();
2488 if (!unreachableURL.isEmpty() && unreachableURL == pdl->request().url())
2489 shouldReset = false;
2490 }
2491 }
2492 if (shouldReset && item)
2493 if (Page* page = m_frame.page()) {
2494 page->backForward().setCurrentItem(*item);
2495 }
2496 return;
2497 }
2498
2499 case FrameStateCommittedPage: {
2500 if (!m_documentLoader)
2501 return;
2502 if (m_documentLoader->isLoadingInAPISense() && !m_documentLoader->isStopping() && !m_checkingLoadCompleteForDetachment)
2503 return;
2504
2505 setState(FrameStateComplete);
2506
2507 // FIXME: Is this subsequent work important if we already navigated away?
2508 // Maybe there are bugs because of that, or extra work we can skip because
2509 // the new page is ready.
2510
2511 m_client.forceLayoutForNonHTML();
2512
2513 // If the user had a scroll point, scroll to it, overriding the anchor point if any.
2514 if (m_frame.page()) {
2515 if (isBackForwardLoadType(m_loadType) || isReload(m_loadType))
2516 history().restoreScrollPositionAndViewState();
2517 }
2518
2519 if (m_stateMachine.creatingInitialEmptyDocument() || !m_stateMachine.committedFirstRealDocumentLoad())
2520 return;
2521
2522 m_progressTracker->progressCompleted();
2523 Page* page = m_frame.page();
2524 if (page) {
2525 if (m_frame.isMainFrame()) {
2526 tracePoint(MainResourceLoadDidEnd);
2527 page->didFinishLoad();
2528 }
2529 }
2530
2531 const ResourceError& error = m_documentLoader->mainDocumentError();
2532
2533 AXObjectCache::AXLoadingEvent loadingEvent;
2534 if (!error.isNull()) {
2535 RELEASE_LOG_IF_ALLOWED("checkLoadCompleteForThisFrame: Finished frame load with error (frame = %p, main = %d, isTimeout = %d, isCancellation = %d, errorCode = %d)", &m_frame, m_frame.isMainFrame(), error.isTimeout(), error.isCancellation(), error.errorCode());
2536 m_client.dispatchDidFailLoad(error);
2537 loadingEvent = AXObjectCache::AXLoadingFailed;
2538 } else {
2539 RELEASE_LOG_IF_ALLOWED("checkLoadCompleteForThisFrame: Finished frame load (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
2540#if ENABLE(DATA_DETECTION)
2541 auto* document = m_frame.document();
2542 if (m_frame.settings().dataDetectorTypes() != DataDetectorTypeNone && document) {
2543 if (auto* documentElement = document->documentElement()) {
2544 RefPtr<Range> documentRange = makeRange(firstPositionInNode(documentElement), lastPositionInNode(documentElement));
2545 m_frame.setDataDetectionResults(DataDetection::detectContentInRange(documentRange, m_frame.settings().dataDetectorTypes(), m_client.dataDetectionContext()));
2546 if (m_frame.isMainFrame())
2547 m_client.dispatchDidFinishDataDetection(m_frame.dataDetectionResults());
2548 }
2549 }
2550#endif
2551 m_client.dispatchDidFinishLoad();
2552 loadingEvent = AXObjectCache::AXLoadingFinished;
2553 }
2554
2555 // Notify accessibility.
2556 if (auto* document = m_frame.document()) {
2557 if (AXObjectCache* cache = document->existingAXObjectCache())
2558 cache->frameLoadingEventNotification(&m_frame, loadingEvent);
2559 }
2560
2561 // The above calls to dispatchDidFinishLoad() might have detached the Frame
2562 // from its Page and also might have caused Page to be deleted.
2563 // Don't assume 'page' is still available to use.
2564 if (m_frame.isMainFrame() && m_frame.page()) {
2565 ASSERT(&m_frame.page()->mainFrame() == &m_frame);
2566 m_frame.page()->diagnosticLoggingClient().logDiagnosticMessageWithResult(DiagnosticLoggingKeys::pageLoadedKey(), emptyString(), error.isNull() ? DiagnosticLoggingResultPass : DiagnosticLoggingResultFail, ShouldSample::Yes);
2567 }
2568
2569 return;
2570 }
2571
2572 case FrameStateComplete:
2573 m_loadType = FrameLoadType::Standard;
2574 frameLoadCompleted();
2575 return;
2576 }
2577
2578 ASSERT_NOT_REACHED();
2579}
2580
2581void FrameLoader::setOriginalURLForDownloadRequest(ResourceRequest& request)
2582{
2583 // FIXME: Rename firstPartyForCookies back to mainDocumentURL. It was a mistake to think that it was only used for cookies.
2584 // The originalURL is defined as the URL of the page where the download was initiated.
2585 URL originalURL;
2586 auto* initiator = m_frame.document();
2587 if (initiator) {
2588 originalURL = initiator->firstPartyForCookies();
2589 // If there is no main document URL, it means that this document is newly opened and just for download purpose.
2590 // In this case, we need to set the originalURL to this document's opener's main document URL.
2591 if (originalURL.isEmpty() && opener() && opener()->document()) {
2592 originalURL = opener()->document()->firstPartyForCookies();
2593 initiator = opener()->document();
2594 }
2595 }
2596 // If the originalURL is the same as the requested URL, we are processing a download
2597 // initiated directly without a page and do not need to specify the originalURL.
2598 if (originalURL == request.url())
2599 request.setFirstPartyForCookies(URL());
2600 else
2601 request.setFirstPartyForCookies(originalURL);
2602 addSameSiteInfoToRequestIfNeeded(request, initiator);
2603}
2604
2605void FrameLoader::didReachLayoutMilestone(OptionSet<LayoutMilestone> milestones)
2606{
2607 ASSERT(m_frame.isMainFrame());
2608
2609 m_client.dispatchDidReachLayoutMilestone(milestones);
2610}
2611
2612void FrameLoader::didFirstLayout()
2613{
2614#if PLATFORM(IOS_FAMILY)
2615 // Only send layout-related delegate callbacks synchronously for the main frame to
2616 // avoid reentering layout for the main frame while delivering a layout-related delegate
2617 // callback for a subframe.
2618 if (&m_frame != &m_frame.page()->mainFrame())
2619 return;
2620#endif
2621 if (m_frame.page() && isBackForwardLoadType(m_loadType))
2622 history().restoreScrollPositionAndViewState();
2623
2624 if (m_stateMachine.committedFirstRealDocumentLoad() && !m_stateMachine.isDisplayingInitialEmptyDocument() && !m_stateMachine.firstLayoutDone())
2625 m_stateMachine.advanceTo(FrameLoaderStateMachine::FirstLayoutDone);
2626}
2627
2628void FrameLoader::frameLoadCompleted()
2629{
2630 // Note: Can be called multiple times.
2631
2632 m_client.frameLoadCompleted();
2633
2634 history().updateForFrameLoadCompleted();
2635
2636 // After a canceled provisional load, firstLayoutDone is false.
2637 // Reset it to true if we're displaying a page.
2638 if (m_documentLoader && m_stateMachine.committedFirstRealDocumentLoad() && !m_stateMachine.isDisplayingInitialEmptyDocument() && !m_stateMachine.firstLayoutDone())
2639 m_stateMachine.advanceTo(FrameLoaderStateMachine::FirstLayoutDone);
2640}
2641
2642void FrameLoader::detachChildren()
2643{
2644 // detachChildren() will fire the unload event in each subframe and the
2645 // HTML specification states that the parent document's ignore-opens-during-unload counter while
2646 // this event is being fired in its subframes:
2647 // https://html.spec.whatwg.org/multipage/browsers.html#unload-a-document
2648 IgnoreOpensDuringUnloadCountIncrementer ignoreOpensDuringUnloadCountIncrementer(m_frame.document());
2649
2650 // Any subframe inserted by unload event handlers executed in the loop below will not get unloaded
2651 // because we create a copy of the subframes list before looping. Therefore, it would be unsafe to
2652 // allow loading of subframes at this point.
2653 SubframeLoadingDisabler subframeLoadingDisabler(m_frame.document());
2654
2655 Vector<Ref<Frame>, 16> childrenToDetach;
2656 childrenToDetach.reserveInitialCapacity(m_frame.tree().childCount());
2657 for (Frame* child = m_frame.tree().lastChild(); child; child = child->tree().previousSibling())
2658 childrenToDetach.uncheckedAppend(*child);
2659 for (auto& child : childrenToDetach)
2660 child->loader().detachFromParent();
2661}
2662
2663void FrameLoader::closeAndRemoveChild(Frame& child)
2664{
2665 child.tree().detachFromParent();
2666
2667 child.setView(nullptr);
2668 if (child.ownerElement() && child.page())
2669 child.page()->decrementSubframeCount();
2670 child.willDetachPage();
2671 child.detachFromPage();
2672
2673 m_frame.tree().removeChild(child);
2674}
2675
2676// Called every time a resource is completely loaded or an error is received.
2677void FrameLoader::checkLoadComplete()
2678{
2679 m_shouldCallCheckLoadComplete = false;
2680
2681 if (!m_frame.page())
2682 return;
2683
2684 ASSERT(m_client.hasWebView());
2685
2686 // FIXME: Always traversing the entire frame tree is a bit inefficient, but
2687 // is currently needed in order to null out the previous history item for all frames.
2688 Vector<Ref<Frame>, 16> frames;
2689 for (Frame* frame = &m_frame.mainFrame(); frame; frame = frame->tree().traverseNext())
2690 frames.append(*frame);
2691
2692 // To process children before their parents, iterate the vector backwards.
2693 for (auto frame = frames.rbegin(); frame != frames.rend(); ++frame) {
2694 if ((*frame)->page())
2695 (*frame)->loader().checkLoadCompleteForThisFrame();
2696 }
2697}
2698
2699int FrameLoader::numPendingOrLoadingRequests(bool recurse) const
2700{
2701 if (!recurse)
2702 return m_frame.document()->cachedResourceLoader().requestCount();
2703
2704 int count = 0;
2705 for (Frame* frame = &m_frame; frame; frame = frame->tree().traverseNext(&m_frame))
2706 count += frame->document()->cachedResourceLoader().requestCount();
2707 return count;
2708}
2709
2710String FrameLoader::userAgent(const URL& url) const
2711{
2712 String userAgent;
2713
2714 if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader())
2715 userAgent = documentLoader->customUserAgent();
2716
2717 InspectorInstrumentation::applyUserAgentOverride(m_frame, userAgent);
2718
2719 if (!userAgent.isEmpty())
2720 return userAgent;
2721
2722 return m_client.userAgent(url);
2723}
2724
2725String FrameLoader::userAgentForJavaScript(const URL& url) const
2726{
2727 String userAgent;
2728
2729 if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) {
2730 if (m_frame.settings().needsSiteSpecificQuirks())
2731 userAgent = documentLoader->customJavaScriptUserAgentAsSiteSpecificQuirks();
2732 if (userAgent.isEmpty())
2733 userAgent = documentLoader->customUserAgent();
2734 }
2735
2736 InspectorInstrumentation::applyUserAgentOverride(m_frame, userAgent);
2737
2738 if (!userAgent.isEmpty())
2739 return userAgent;
2740
2741 return m_client.userAgent(url);
2742}
2743
2744String FrameLoader::navigatorPlatform() const
2745{
2746 if (auto* documentLoader = m_frame.mainFrame().loader().activeDocumentLoader()) {
2747 auto& customNavigatorPlatform = documentLoader->customNavigatorPlatform();
2748 if (!customNavigatorPlatform.isEmpty())
2749 return customNavigatorPlatform;
2750 }
2751 return String();
2752}
2753
2754void FrameLoader::dispatchOnloadEvents()
2755{
2756 m_client.dispatchDidDispatchOnloadEvents();
2757
2758 if (documentLoader())
2759 documentLoader()->dispatchOnloadEvents();
2760}
2761
2762void FrameLoader::frameDetached()
2763{
2764 // Calling stopAllLoadersAndCheckCompleteness() can cause the frame to be deallocated, including the frame loader.
2765 Ref<Frame> protectedFrame(m_frame);
2766
2767 if (m_checkTimer.isActive()) {
2768 m_checkTimer.stop();
2769 checkCompletenessNow();
2770 }
2771
2772 if (m_frame.document()->pageCacheState() != Document::InPageCache) {
2773 stopAllLoadersAndCheckCompleteness();
2774 m_frame.document()->stopActiveDOMObjects();
2775 }
2776
2777 detachFromParent();
2778}
2779
2780void FrameLoader::detachFromParent()
2781{
2782 // Calling stopAllLoaders() can cause the frame to be deallocated, including the frame loader.
2783 Ref<Frame> protect(m_frame);
2784
2785 closeURL();
2786 history().saveScrollPositionAndViewStateToItem(history().currentItem());
2787 detachChildren();
2788 if (m_frame.document()->pageCacheState() != Document::InPageCache) {
2789 // stopAllLoaders() needs to be called after detachChildren() if the document is not in the page cache,
2790 // because detachedChildren() will trigger the unload event handlers of any child frames, and those event
2791 // handlers might start a new subresource load in this frame.
2792 stopAllLoaders();
2793 }
2794
2795 InspectorInstrumentation::frameDetachedFromParent(m_frame);
2796
2797 detachViewsAndDocumentLoader();
2798
2799 m_progressTracker = nullptr;
2800
2801 if (Frame* parent = m_frame.tree().parent()) {
2802 parent->loader().closeAndRemoveChild(m_frame);
2803 parent->loader().scheduleCheckCompleted();
2804 parent->loader().scheduleCheckLoadComplete();
2805 } else {
2806 m_frame.setView(nullptr);
2807 m_frame.willDetachPage();
2808 m_frame.detachFromPage();
2809 }
2810}
2811
2812void FrameLoader::detachViewsAndDocumentLoader()
2813{
2814 m_client.detachedFromParent2();
2815 setDocumentLoader(nullptr);
2816 m_client.detachedFromParent3();
2817}
2818
2819void FrameLoader::addExtraFieldsToSubresourceRequest(ResourceRequest& request)
2820{
2821 addExtraFieldsToRequest(request, m_loadType, false);
2822}
2823
2824void FrameLoader::addExtraFieldsToMainResourceRequest(ResourceRequest& request)
2825{
2826 // FIXME: Using m_loadType seems wrong for some callers.
2827 // If we are only preparing to load the main resource, that is previous load's load type!
2828 addExtraFieldsToRequest(request, m_loadType, true);
2829
2830 // Upgrade-Insecure-Requests should only be added to main resource requests
2831 addHTTPUpgradeInsecureRequestsIfNeeded(request);
2832}
2833
2834ResourceRequestCachePolicy FrameLoader::defaultRequestCachingPolicy(const ResourceRequest& request, FrameLoadType loadType, bool isMainResource)
2835{
2836 if (m_overrideCachePolicyForTesting)
2837 return m_overrideCachePolicyForTesting.value();
2838
2839 if (isMainResource) {
2840 if (isReload(loadType) || request.isConditional())
2841 return ResourceRequestCachePolicy::ReloadIgnoringCacheData;
2842
2843 return ResourceRequestCachePolicy::UseProtocolCachePolicy;
2844 }
2845
2846 if (request.isConditional())
2847 return ResourceRequestCachePolicy::ReloadIgnoringCacheData;
2848
2849 if (documentLoader()->isLoadingInAPISense()) {
2850 // If we inherit cache policy from a main resource, we use the DocumentLoader's
2851 // original request cache policy for two reasons:
2852 // 1. For POST requests, we mutate the cache policy for the main resource,
2853 // but we do not want this to apply to subresources
2854 // 2. Delegates that modify the cache policy using willSendRequest: should
2855 // not affect any other resources. Such changes need to be done
2856 // per request.
2857 ResourceRequestCachePolicy mainDocumentOriginalCachePolicy = documentLoader()->originalRequest().cachePolicy();
2858 // Back-forward navigations try to load main resource from cache only to avoid re-submitting form data, and start over (with a warning dialog) if that fails.
2859 // This policy is set on initial request too, but should not be inherited.
2860 return (mainDocumentOriginalCachePolicy == ResourceRequestCachePolicy::ReturnCacheDataDontLoad) ? ResourceRequestCachePolicy::ReturnCacheDataElseLoad : mainDocumentOriginalCachePolicy;
2861 }
2862
2863 return ResourceRequestCachePolicy::UseProtocolCachePolicy;
2864}
2865
2866void FrameLoader::addExtraFieldsToRequest(ResourceRequest& request, FrameLoadType loadType, bool isMainResource)
2867{
2868 // If the request came from a previous process due to process-swap-on-navigation then we should not modify the request.
2869 if (m_currentLoadContinuingState == LoadContinuingState::ContinuingWithRequest)
2870 return;
2871
2872 // Don't set the cookie policy URL if it's already been set.
2873 // But make sure to set it on all requests regardless of protocol, as it has significance beyond the cookie policy (<rdar://problem/6616664>).
2874 bool isMainFrameMainResource = isMainResource && m_frame.isMainFrame();
2875 if (request.firstPartyForCookies().isEmpty()) {
2876 if (isMainFrameMainResource)
2877 request.setFirstPartyForCookies(request.url());
2878 else if (Document* document = m_frame.document())
2879 request.setFirstPartyForCookies(document->firstPartyForCookies());
2880 }
2881
2882 if (request.isSameSiteUnspecified()) {
2883 auto* initiator = m_frame.document();
2884 if (isMainResource) {
2885 auto* ownerFrame = m_frame.tree().parent();
2886 if (!ownerFrame && m_stateMachine.isDisplayingInitialEmptyDocument())
2887 ownerFrame = m_opener;
2888 if (ownerFrame)
2889 initiator = ownerFrame->document();
2890 ASSERT(ownerFrame || m_frame.isMainFrame());
2891 }
2892 addSameSiteInfoToRequestIfNeeded(request, initiator);
2893 }
2894 request.setIsTopSite(isMainFrameMainResource);
2895
2896 Page* page = frame().page();
2897 bool hasSpecificCachePolicy = request.cachePolicy() != ResourceRequestCachePolicy::UseProtocolCachePolicy;
2898
2899 if (page && page->isResourceCachingDisabled()) {
2900 request.setCachePolicy(ResourceRequestCachePolicy::ReloadIgnoringCacheData);
2901 loadType = FrameLoadType::ReloadFromOrigin;
2902 } else if (!hasSpecificCachePolicy)
2903 request.setCachePolicy(defaultRequestCachingPolicy(request, loadType, isMainResource));
2904
2905 // The remaining modifications are only necessary for HTTP and HTTPS.
2906 if (!request.url().isEmpty() && !request.url().protocolIsInHTTPFamily())
2907 return;
2908
2909 if (!hasSpecificCachePolicy && request.cachePolicy() == ResourceRequestCachePolicy::ReloadIgnoringCacheData) {
2910 if (loadType == FrameLoadType::Reload)
2911 request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
2912 else if (loadType == FrameLoadType::ReloadFromOrigin) {
2913 request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "no-cache");
2914 request.setHTTPHeaderField(HTTPHeaderName::Pragma, "no-cache");
2915 }
2916 }
2917
2918 if (m_overrideResourceLoadPriorityForTesting)
2919 request.setPriority(m_overrideResourceLoadPriorityForTesting.value());
2920
2921 applyUserAgentIfNeeded(request);
2922
2923 if (isMainResource)
2924 request.setHTTPAccept(defaultAcceptHeader);
2925
2926 // Make sure we send the Origin header.
2927 addHTTPOriginIfNeeded(request, String());
2928
2929 // Only set fallback array if it's still empty (later attempts may be incorrect, see bug 117818).
2930 if (request.responseContentDispositionEncodingFallbackArray().isEmpty()) {
2931 // Always try UTF-8. If that fails, try frame encoding (if any) and then the default.
2932 request.setResponseContentDispositionEncodingFallbackArray("UTF-8", m_frame.document()->encoding(), m_frame.settings().defaultTextEncodingName());
2933 }
2934}
2935
2936void FrameLoader::addHTTPOriginIfNeeded(ResourceRequest& request, const String& origin)
2937{
2938 if (!request.httpOrigin().isEmpty())
2939 return; // Request already has an Origin header.
2940
2941 // Don't send an Origin header for GET or HEAD to avoid privacy issues.
2942 // For example, if an intranet page has a hyperlink to an external web
2943 // site, we don't want to include the Origin of the request because it
2944 // will leak the internal host name. Similar privacy concerns have lead
2945 // to the widespread suppression of the Referer header at the network
2946 // layer.
2947 if (request.httpMethod() == "GET" || request.httpMethod() == "HEAD")
2948 return;
2949
2950 // For non-GET and non-HEAD methods, always send an Origin header so the
2951 // server knows we support this feature.
2952
2953 if (origin.isEmpty()) {
2954 // If we don't know what origin header to attach, we attach the value
2955 // for an empty origin.
2956 request.setHTTPOrigin(SecurityOrigin::createUnique()->toString());
2957 return;
2958 }
2959
2960 request.setHTTPOrigin(origin);
2961}
2962
2963// Implements the "'Same-site' and 'cross-site' Requests" algorithm from <https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-2.1>.
2964// The algorithm is ammended to treat URLs that inherit their security origin from their owner (e.g. about:blank)
2965// as same-site. This matches the behavior of Chrome and Firefox.
2966void FrameLoader::addSameSiteInfoToRequestIfNeeded(ResourceRequest& request, const Document* initiator)
2967{
2968 if (!request.isSameSiteUnspecified())
2969 return;
2970 if (!initiator) {
2971 request.setIsSameSite(true);
2972 return;
2973 }
2974 if (SecurityPolicy::shouldInheritSecurityOriginFromOwner(request.url())) {
2975 request.setIsSameSite(true);
2976 return;
2977 }
2978 request.setIsSameSite(areRegistrableDomainsEqual(initiator->siteForCookies(), request.url()));
2979}
2980
2981void FrameLoader::addHTTPUpgradeInsecureRequestsIfNeeded(ResourceRequest& request)
2982{
2983 if (request.url().protocolIs("https")) {
2984 // FIXME: Identify HSTS cases and avoid adding the header. <https://bugs.webkit.org/show_bug.cgi?id=157885>
2985 return;
2986 }
2987
2988 request.setHTTPHeaderField(HTTPHeaderName::UpgradeInsecureRequests, "1"_s);
2989}
2990
2991void FrameLoader::loadPostRequest(FrameLoadRequest&& request, const String& referrer, FrameLoadType loadType, Event* event, RefPtr<FormState>&& formState, CompletionHandler<void()>&& completionHandler)
2992{
2993 RELEASE_LOG_IF_ALLOWED("loadPostRequest: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
2994
2995 String frameName = request.frameName();
2996 LockHistory lockHistory = request.lockHistory();
2997 AllowNavigationToInvalidURL allowNavigationToInvalidURL = request.allowNavigationToInvalidURL();
2998 NewFrameOpenerPolicy openerPolicy = request.newFrameOpenerPolicy();
2999
3000 const ResourceRequest& inRequest = request.resourceRequest();
3001 const URL& url = inRequest.url();
3002 const String& contentType = inRequest.httpContentType();
3003 String origin = inRequest.httpOrigin();
3004
3005 ResourceRequest workingResourceRequest(url);
3006
3007 if (!referrer.isEmpty())
3008 workingResourceRequest.setHTTPReferrer(referrer);
3009 workingResourceRequest.setHTTPOrigin(origin);
3010 workingResourceRequest.setHTTPMethod("POST");
3011 workingResourceRequest.setHTTPBody(inRequest.httpBody());
3012 workingResourceRequest.setHTTPContentType(contentType);
3013 addExtraFieldsToRequest(workingResourceRequest, loadType, true);
3014
3015 if (Document* document = m_frame.document())
3016 document->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(workingResourceRequest, ContentSecurityPolicy::InsecureRequestType::Load);
3017
3018 NavigationAction action { request.requester(), workingResourceRequest, request.initiatedByMainFrame(), loadType, true, event, request.shouldOpenExternalURLsPolicy(), request.downloadAttribute() };
3019
3020 if (!frameName.isEmpty()) {
3021 // The search for a target frame is done earlier in the case of form submission.
3022 if (auto* targetFrame = formState ? nullptr : findFrameForNavigation(frameName)) {
3023 targetFrame->loader().loadWithNavigationAction(workingResourceRequest, WTFMove(action), lockHistory, loadType, WTFMove(formState), allowNavigationToInvalidURL, { }, WTFMove(completionHandler));
3024 return;
3025 }
3026
3027 policyChecker().checkNewWindowPolicy(WTFMove(action), WTFMove(workingResourceRequest), WTFMove(formState), frameName, [this, allowNavigationToInvalidURL, openerPolicy, completionHandler = WTFMove(completionHandler)] (const ResourceRequest& request, WeakPtr<FormState>&& formState, const String& frameName, const NavigationAction& action, ShouldContinue shouldContinue) mutable {
3028 continueLoadAfterNewWindowPolicy(request, formState.get(), frameName, action, shouldContinue, allowNavigationToInvalidURL, openerPolicy);
3029 completionHandler();
3030 });
3031 return;
3032 }
3033
3034 // must grab this now, since this load may stop the previous load and clear this flag
3035 bool isRedirect = m_quickRedirectComing;
3036 loadWithNavigationAction(workingResourceRequest, WTFMove(action), lockHistory, loadType, WTFMove(formState), allowNavigationToInvalidURL, { }, [this, isRedirect, protectedFrame = makeRef(m_frame), completionHandler = WTFMove(completionHandler)] () mutable {
3037 if (isRedirect) {
3038 m_quickRedirectComing = false;
3039 if (m_provisionalDocumentLoader)
3040 m_provisionalDocumentLoader->setIsClientRedirect(true);
3041 else if (m_policyDocumentLoader)
3042 m_policyDocumentLoader->setIsClientRedirect(true);
3043 }
3044 completionHandler();
3045 });
3046}
3047
3048unsigned long FrameLoader::loadResourceSynchronously(const ResourceRequest& request, ClientCredentialPolicy clientCredentialPolicy, const FetchOptions& options, const HTTPHeaderMap& originalRequestHeaders, ResourceError& error, ResourceResponse& response, RefPtr<SharedBuffer>& data)
3049{
3050 ASSERT(m_frame.document());
3051 String referrer = SecurityPolicy::generateReferrerHeader(m_frame.document()->referrerPolicy(), request.url(), outgoingReferrer());
3052
3053 ResourceRequest initialRequest = request;
3054 initialRequest.setTimeoutInterval(10);
3055
3056 if (!referrer.isEmpty())
3057 initialRequest.setHTTPReferrer(referrer);
3058 addHTTPOriginIfNeeded(initialRequest, outgoingOrigin());
3059
3060 initialRequest.setFirstPartyForCookies(m_frame.mainFrame().loader().documentLoader()->request().url());
3061
3062 addExtraFieldsToSubresourceRequest(initialRequest);
3063
3064 unsigned long identifier = 0;
3065 ResourceRequest newRequest(initialRequest);
3066 requestFromDelegate(newRequest, identifier, error);
3067
3068#if ENABLE(CONTENT_EXTENSIONS)
3069 if (error.isNull()) {
3070 if (auto* page = m_frame.page()) {
3071 if (m_documentLoader) {
3072 auto results = page->userContentProvider().processContentRuleListsForLoad(newRequest.url(), ContentExtensions::ResourceType::Raw, *m_documentLoader);
3073 bool blockedLoad = results.summary.blockedLoad;
3074 ContentExtensions::applyResultsToRequest(WTFMove(results), page, newRequest);
3075 if (blockedLoad) {
3076 newRequest = { };
3077 error = ResourceError(errorDomainWebKitInternal, 0, initialRequest.url(), emptyString());
3078 response = { };
3079 data = nullptr;
3080 }
3081 }
3082 }
3083 }
3084#endif
3085
3086 m_frame.document()->contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(newRequest, ContentSecurityPolicy::InsecureRequestType::Load);
3087
3088 if (error.isNull()) {
3089 ASSERT(!newRequest.isNull());
3090
3091 if (!documentLoader()->applicationCacheHost().maybeLoadSynchronously(newRequest, error, response, data)) {
3092 Vector<char> buffer;
3093 platformStrategies()->loaderStrategy()->loadResourceSynchronously(*this, identifier, newRequest, clientCredentialPolicy, options, originalRequestHeaders, error, response, buffer);
3094 data = SharedBuffer::create(WTFMove(buffer));
3095 documentLoader()->applicationCacheHost().maybeLoadFallbackSynchronously(newRequest, error, response, data);
3096 ResourceLoadObserver::shared().logSubresourceLoading(&m_frame, newRequest, response);
3097 }
3098 }
3099 notifier().sendRemainingDelegateMessages(m_documentLoader.get(), identifier, request, response, data ? data->data() : nullptr, data ? data->size() : 0, -1, error);
3100 return identifier;
3101}
3102
3103const ResourceRequest& FrameLoader::originalRequest() const
3104{
3105 return activeDocumentLoader()->originalRequestCopy();
3106}
3107
3108void FrameLoader::receivedMainResourceError(const ResourceError& error)
3109{
3110 // Retain because the stop may release the last reference to it.
3111 Ref<Frame> protect(m_frame);
3112
3113 RefPtr<DocumentLoader> loader = activeDocumentLoader();
3114 // FIXME: Don't want to do this if an entirely new load is going, so should check
3115 // that both data sources on the frame are either this or nil.
3116 stop();
3117 if (m_client.shouldFallBack(error))
3118 handleFallbackContent();
3119
3120 if (m_state == FrameStateProvisional && m_provisionalDocumentLoader) {
3121 if (m_submittedFormURL == m_provisionalDocumentLoader->originalRequestCopy().url())
3122 m_submittedFormURL = URL();
3123
3124 // We might have made a page cache item, but now we're bailing out due to an error before we ever
3125 // transitioned to the new page (before WebFrameState == commit). The goal here is to restore any state
3126 // so that the existing view (that wenever got far enough to replace) can continue being used.
3127 history().invalidateCurrentItemCachedPage();
3128
3129 // Call clientRedirectCancelledOrFinished here so that the frame load delegate is notified that the redirect's
3130 // status has changed, if there was a redirect. The frame load delegate may have saved some state about
3131 // the redirect in its -webView:willPerformClientRedirectToURL:delay:fireDate:forFrame:. Since we are definitely
3132 // not going to use this provisional resource, as it was cancelled, notify the frame load delegate that the redirect
3133 // has ended.
3134 if (m_sentRedirectNotification)
3135 clientRedirectCancelledOrFinished(NewLoadInProgress::No);
3136 }
3137
3138 checkCompleted();
3139 if (m_frame.page())
3140 checkLoadComplete();
3141}
3142
3143void FrameLoader::continueFragmentScrollAfterNavigationPolicy(const ResourceRequest& request, bool shouldContinue)
3144{
3145 m_quickRedirectComing = false;
3146
3147 if (!shouldContinue)
3148 return;
3149
3150 // Calling stopLoading() on the provisional document loader can cause the underlying
3151 // frame to be deallocated.
3152 Ref<Frame> protectedFrame(m_frame);
3153
3154 // If we have a provisional request for a different document, a fragment scroll should cancel it.
3155 if (m_provisionalDocumentLoader && !equalIgnoringFragmentIdentifier(m_provisionalDocumentLoader->request().url(), request.url())) {
3156 m_provisionalDocumentLoader->stopLoading();
3157 setProvisionalDocumentLoader(nullptr);
3158 }
3159
3160 bool isRedirect = m_quickRedirectComing || policyChecker().loadType() == FrameLoadType::RedirectWithLockedBackForwardList;
3161 loadInSameDocument(request.url(), 0, !isRedirect);
3162}
3163
3164bool FrameLoader::shouldPerformFragmentNavigation(bool isFormSubmission, const String& httpMethod, FrameLoadType loadType, const URL& url)
3165{
3166 // We don't do this if we are submitting a form with method other than "GET", explicitly reloading,
3167 // currently displaying a frameset, or if the URL does not have a fragment.
3168 // These rules were originally based on what KHTML was doing in KHTMLPart::openURL.
3169
3170 // FIXME: What about load types other than Standard and Reload?
3171
3172 return (!isFormSubmission || equalLettersIgnoringASCIICase(httpMethod, "get"))
3173 && !isReload(loadType)
3174 && loadType != FrameLoadType::Same
3175 && m_frame.document()->pageCacheState() != Document::InPageCache
3176 && !shouldReload(m_frame.document()->url(), url)
3177 // We don't want to just scroll if a link from within a
3178 // frameset is trying to reload the frameset into _top.
3179 && !m_frame.document()->isFrameSet();
3180}
3181
3182static bool itemAllowsScrollRestoration(HistoryItem* historyItem)
3183{
3184 return !historyItem || historyItem->shouldRestoreScrollPosition();
3185}
3186
3187static bool isSameDocumentReload(bool isNewNavigation, FrameLoadType loadType)
3188{
3189 return !isNewNavigation && !isBackForwardLoadType(loadType);
3190}
3191
3192void FrameLoader::scrollToFragmentWithParentBoundary(const URL& url, bool isNewNavigation)
3193{
3194 FrameView* view = m_frame.view();
3195 if (!view)
3196 return;
3197
3198 if (isSameDocumentReload(isNewNavigation, m_loadType) || itemAllowsScrollRestoration(history().currentItem()))
3199 view->scrollToFragment(url);
3200}
3201
3202bool FrameLoader::shouldClose()
3203{
3204 Page* page = m_frame.page();
3205 if (!page)
3206 return true;
3207 if (!page->chrome().canRunBeforeUnloadConfirmPanel())
3208 return true;
3209
3210 // Store all references to each subframe in advance since beforeunload's event handler may modify frame
3211 Vector<Ref<Frame>, 16> targetFrames;
3212 targetFrames.append(m_frame);
3213 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().traverseNext(&m_frame))
3214 targetFrames.append(*child);
3215
3216 bool shouldClose = false;
3217 {
3218 NavigationDisabler navigationDisabler(&m_frame);
3219 IgnoreOpensDuringUnloadCountIncrementer ignoreOpensDuringUnloadCountIncrementer(m_frame.document());
3220 size_t i;
3221
3222 for (i = 0; i < targetFrames.size(); i++) {
3223 if (!targetFrames[i]->tree().isDescendantOf(&m_frame))
3224 continue;
3225 if (!targetFrames[i]->loader().dispatchBeforeUnloadEvent(page->chrome(), this))
3226 break;
3227 }
3228
3229 if (i == targetFrames.size())
3230 shouldClose = true;
3231 }
3232
3233 if (!shouldClose)
3234 m_submittedFormURL = URL();
3235
3236 m_currentNavigationHasShownBeforeUnloadConfirmPanel = false;
3237 return shouldClose;
3238}
3239
3240void FrameLoader::dispatchUnloadEvents(UnloadEventPolicy unloadEventPolicy)
3241{
3242 if (!m_frame.document())
3243 return;
3244
3245 // We store the frame's page in a local variable because the frame might get detached inside dispatchEvent.
3246 ForbidPromptsScope forbidPrompts(m_frame.page());
3247 IgnoreOpensDuringUnloadCountIncrementer ignoreOpensDuringUnloadCountIncrementer(m_frame.document());
3248
3249 if (m_didCallImplicitClose && !m_wasUnloadEventEmitted) {
3250 auto* currentFocusedElement = m_frame.document()->focusedElement();
3251 if (is<HTMLInputElement>(currentFocusedElement))
3252 downcast<HTMLInputElement>(*currentFocusedElement).endEditing();
3253 if (m_pageDismissalEventBeingDispatched == PageDismissalType::None) {
3254 if (unloadEventPolicy == UnloadEventPolicyUnloadAndPageHide) {
3255 m_pageDismissalEventBeingDispatched = PageDismissalType::PageHide;
3256 m_frame.document()->domWindow()->dispatchEvent(PageTransitionEvent::create(eventNames().pagehideEvent, m_frame.document()->pageCacheState() == Document::AboutToEnterPageCache), m_frame.document());
3257 }
3258
3259 // FIXME: update Page Visibility state here.
3260 // https://bugs.webkit.org/show_bug.cgi?id=116770
3261
3262 if (m_frame.document()->pageCacheState() == Document::NotInPageCache) {
3263 Ref<Event> unloadEvent(Event::create(eventNames().unloadEvent, Event::CanBubble::No, Event::IsCancelable::No));
3264 // The DocumentLoader (and thus its LoadTiming) might get destroyed
3265 // while dispatching the event, so protect it to prevent writing the end
3266 // time into freed memory.
3267 RefPtr<DocumentLoader> documentLoader = m_provisionalDocumentLoader;
3268 m_pageDismissalEventBeingDispatched = PageDismissalType::Unload;
3269 if (documentLoader && documentLoader->timing().startTime() && !documentLoader->timing().unloadEventStart() && !documentLoader->timing().unloadEventEnd()) {
3270 auto& timing = documentLoader->timing();
3271 timing.markUnloadEventStart();
3272 m_frame.document()->domWindow()->dispatchEvent(unloadEvent, m_frame.document());
3273 timing.markUnloadEventEnd();
3274 } else
3275 m_frame.document()->domWindow()->dispatchEvent(unloadEvent, m_frame.document());
3276 }
3277 }
3278 m_pageDismissalEventBeingDispatched = PageDismissalType::None;
3279 m_wasUnloadEventEmitted = true;
3280 }
3281
3282 // Dispatching the unload event could have made m_frame.document() null.
3283 if (!m_frame.document())
3284 return;
3285
3286 if (m_frame.document()->pageCacheState() != Document::NotInPageCache)
3287 return;
3288
3289 // Don't remove event listeners from a transitional empty document (see bug 28716 for more information).
3290 bool keepEventListeners = m_stateMachine.isDisplayingInitialEmptyDocument() && m_provisionalDocumentLoader
3291 && m_frame.document()->isSecureTransitionTo(m_provisionalDocumentLoader->url());
3292
3293 if (!keepEventListeners)
3294 m_frame.document()->removeAllEventListeners();
3295}
3296
3297static bool shouldAskForNavigationConfirmation(Document& document, const BeforeUnloadEvent& event)
3298{
3299 // Confirmation dialog should not be displayed when the allow-modals flag is not set.
3300 if (document.isSandboxed(SandboxModals))
3301 return false;
3302
3303 bool userDidInteractWithPage = document.topDocument().userDidInteractWithPage();
3304 // Web pages can request we ask for confirmation before navigating by:
3305 // - Cancelling the BeforeUnloadEvent (modern way)
3306 // - Setting the returnValue attribute on the BeforeUnloadEvent to a non-empty string.
3307 // - Returning a non-empty string from the event handler, which is then set as returnValue
3308 // attribute on the BeforeUnloadEvent.
3309 return userDidInteractWithPage && (event.defaultPrevented() || !event.returnValue().isEmpty());
3310}
3311
3312bool FrameLoader::dispatchBeforeUnloadEvent(Chrome& chrome, FrameLoader* frameLoaderBeingNavigated)
3313{
3314 DOMWindow* domWindow = m_frame.document()->domWindow();
3315 if (!domWindow)
3316 return true;
3317
3318 RefPtr<Document> document = m_frame.document();
3319 if (!document->bodyOrFrameset())
3320 return true;
3321
3322 Ref<BeforeUnloadEvent> beforeUnloadEvent = BeforeUnloadEvent::create();
3323 m_pageDismissalEventBeingDispatched = PageDismissalType::BeforeUnload;
3324
3325 {
3326 ForbidPromptsScope forbidPrompts(m_frame.page());
3327 domWindow->dispatchEvent(beforeUnloadEvent, domWindow->document());
3328 }
3329
3330 m_pageDismissalEventBeingDispatched = PageDismissalType::None;
3331
3332 if (!beforeUnloadEvent->defaultPrevented())
3333 document->defaultEventHandler(beforeUnloadEvent.get());
3334
3335 if (!shouldAskForNavigationConfirmation(*document, beforeUnloadEvent))
3336 return true;
3337
3338 // If the navigating FrameLoader has already shown a beforeunload confirmation panel for the current navigation attempt,
3339 // this frame is not allowed to cause another one to be shown.
3340 if (frameLoaderBeingNavigated->m_currentNavigationHasShownBeforeUnloadConfirmPanel) {
3341 document->addConsoleMessage(MessageSource::JS, MessageLevel::Error, "Blocked attempt to show multiple beforeunload confirmation dialogs for the same navigation."_s);
3342 return true;
3343 }
3344
3345 // We should only display the beforeunload dialog for an iframe if its SecurityOrigin matches all
3346 // ancestor frame SecurityOrigins up through the navigating FrameLoader.
3347 if (frameLoaderBeingNavigated != this) {
3348 Frame* parentFrame = m_frame.tree().parent();
3349 while (parentFrame) {
3350 Document* parentDocument = parentFrame->document();
3351 if (!parentDocument)
3352 return true;
3353 if (!m_frame.document() || !m_frame.document()->securityOrigin().canAccess(parentDocument->securityOrigin())) {
3354 document->addConsoleMessage(MessageSource::JS, MessageLevel::Error, "Blocked attempt to show beforeunload confirmation dialog on behalf of a frame with different security origin. Protocols, domains, and ports must match."_s);
3355 return true;
3356 }
3357
3358 if (&parentFrame->loader() == frameLoaderBeingNavigated)
3359 break;
3360
3361 parentFrame = parentFrame->tree().parent();
3362 }
3363
3364 // The navigatingFrameLoader should always be in our ancestory.
3365 ASSERT(parentFrame);
3366 ASSERT(&parentFrame->loader() == frameLoaderBeingNavigated);
3367 }
3368
3369 frameLoaderBeingNavigated->m_currentNavigationHasShownBeforeUnloadConfirmPanel = true;
3370
3371 String text = document->displayStringModifiedByEncoding(beforeUnloadEvent->returnValue());
3372 return chrome.runBeforeUnloadConfirmPanel(text, m_frame);
3373}
3374
3375void FrameLoader::continueLoadAfterNavigationPolicy(const ResourceRequest& request, FormState* formState, NavigationPolicyDecision navigationPolicyDecision, AllowNavigationToInvalidURL allowNavigationToInvalidURL)
3376{
3377 // If we loaded an alternate page to replace an unreachableURL, we'll get in here with a
3378 // nil policyDataSource because loading the alternate page will have passed
3379 // through this method already, nested; otherwise, policyDataSource should still be set.
3380 ASSERT(m_policyDocumentLoader || !m_provisionalDocumentLoader->unreachableURL().isEmpty());
3381
3382 bool isTargetItem = history().provisionalItem() ? history().provisionalItem()->isTargetItem() : false;
3383
3384 bool urlIsDisallowed = allowNavigationToInvalidURL == AllowNavigationToInvalidURL::No && !request.url().isValid();
3385 bool canContinue = navigationPolicyDecision == NavigationPolicyDecision::ContinueLoad && shouldClose() && !urlIsDisallowed;
3386
3387 if (!canContinue) {
3388 RELEASE_LOG_IF_ALLOWED("continueLoadAfterNavigationPolicy: can't continue loading frame due to the following reasons ("
3389 "frame = %p, "
3390 "main = %d, "
3391 "allowNavigationToInvalidURL = %d, "
3392 "requestURLIsValid = %d, "
3393 "navigationPolicyDecision = %d)",
3394 &m_frame,
3395 m_frame.isMainFrame(),
3396 static_cast<int>(allowNavigationToInvalidURL),
3397 request.url().isValid(),
3398 static_cast<int>(navigationPolicyDecision));
3399
3400 // If we were waiting for a quick redirect, but the policy delegate decided to ignore it, then we
3401 // need to report that the client redirect was cancelled.
3402 // FIXME: The client should be told about ignored non-quick redirects, too.
3403 if (m_quickRedirectComing)
3404 clientRedirectCancelledOrFinished(NewLoadInProgress::No);
3405
3406 if (navigationPolicyDecision == NavigationPolicyDecision::StopAllLoads) {
3407 stopAllLoaders();
3408 m_checkTimer.stop();
3409 }
3410
3411 setPolicyDocumentLoader(nullptr);
3412 checkCompleted();
3413
3414 if (navigationPolicyDecision != NavigationPolicyDecision::StopAllLoads)
3415 checkLoadComplete();
3416
3417 // If the navigation request came from the back/forward menu, and we punt on it, we have the
3418 // problem that we have optimistically moved the b/f cursor already, so move it back. For sanity,
3419 // we only do this when punting a navigation for the target frame or top-level frame.
3420 if ((isTargetItem || m_frame.isMainFrame()) && isBackForwardLoadType(policyChecker().loadType())) {
3421 if (Page* page = m_frame.page()) {
3422 if (HistoryItem* resetItem = m_frame.mainFrame().loader().history().currentItem())
3423 page->backForward().setCurrentItem(*resetItem);
3424 }
3425 }
3426 return;
3427 }
3428
3429 FrameLoadType type = policyChecker().loadType();
3430 // A new navigation is in progress, so don't clear the history's provisional item.
3431 stopAllLoaders(ShouldNotClearProvisionalItem);
3432
3433 // <rdar://problem/6250856> - In certain circumstances on pages with multiple frames, stopAllLoaders()
3434 // might detach the current FrameLoader, in which case we should bail on this newly defunct load.
3435 if (!m_frame.page()) {
3436 RELEASE_LOG_IF_ALLOWED("continueLoadAfterNavigationPolicy: can't continue loading frame because it became defunct (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3437 return;
3438 }
3439
3440 setProvisionalDocumentLoader(m_policyDocumentLoader.get());
3441 m_loadType = type;
3442 setState(FrameStateProvisional);
3443
3444 setPolicyDocumentLoader(nullptr);
3445
3446 if (isBackForwardLoadType(type)) {
3447 auto& diagnosticLoggingClient = m_frame.page()->diagnosticLoggingClient();
3448 if (history().provisionalItem()->isInPageCache()) {
3449 diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::pageCacheKey(), DiagnosticLoggingKeys::retrievalKey(), DiagnosticLoggingResultPass, ShouldSample::Yes);
3450 loadProvisionalItemFromCachedPage();
3451 RELEASE_LOG_IF_ALLOWED("continueLoadAfterNavigationPolicy: can't continue loading frame because it will be loaded from cache (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3452 return;
3453 }
3454 diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::pageCacheKey(), DiagnosticLoggingKeys::retrievalKey(), DiagnosticLoggingResultFail, ShouldSample::Yes);
3455 }
3456
3457 CompletionHandler<void()> completionHandler = [this, protectedFrame = makeRef(m_frame)] () mutable {
3458 if (!m_provisionalDocumentLoader) {
3459 RELEASE_LOG_IF_ALLOWED("continueLoadAfterNavigationPolicy completionHandler: Frame load canceled #1 (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3460 return;
3461 }
3462
3463 prepareForLoadStart();
3464
3465 // The load might be cancelled inside of prepareForLoadStart(), nulling out the m_provisionalDocumentLoader,
3466 // so we need to null check it again.
3467 if (!m_provisionalDocumentLoader) {
3468 RELEASE_LOG_IF_ALLOWED("prepareForLoadStart completionHandler: Frame load canceled #2 (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3469 return;
3470 }
3471
3472 DocumentLoader* activeDocLoader = activeDocumentLoader();
3473 if (activeDocLoader && activeDocLoader->isLoadingMainResource()) {
3474 RELEASE_LOG_IF_ALLOWED("prepareForLoadStart completionHandler: Main frame already being loaded (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3475 return;
3476 }
3477
3478 m_loadingFromCachedPage = false;
3479
3480 m_provisionalDocumentLoader->startLoadingMainResource();
3481 };
3482
3483 if (!formState) {
3484 completionHandler();
3485 return;
3486 }
3487
3488 m_client.dispatchWillSubmitForm(*formState, WTFMove(completionHandler));
3489}
3490
3491void FrameLoader::continueLoadAfterNewWindowPolicy(const ResourceRequest& request,
3492 FormState* formState, const String& frameName, const NavigationAction& action, ShouldContinue shouldContinue, AllowNavigationToInvalidURL allowNavigationToInvalidURL, NewFrameOpenerPolicy openerPolicy)
3493{
3494 if (shouldContinue != ShouldContinue::Yes)
3495 return;
3496
3497 Ref<Frame> frame(m_frame);
3498 RefPtr<Frame> mainFrame = m_client.dispatchCreatePage(action);
3499 if (!mainFrame)
3500 return;
3501
3502 SandboxFlags sandboxFlags = frame->loader().effectiveSandboxFlags();
3503 if (sandboxFlags & SandboxPropagatesToAuxiliaryBrowsingContexts)
3504 mainFrame->loader().forceSandboxFlags(sandboxFlags);
3505
3506 if (!equalIgnoringASCIICase(frameName, "_blank"))
3507 mainFrame->tree().setName(frameName);
3508
3509 mainFrame->page()->setOpenedByDOM();
3510 mainFrame->loader().m_client.dispatchShow();
3511 if (openerPolicy == NewFrameOpenerPolicy::Allow) {
3512 mainFrame->loader().setOpener(frame.ptr());
3513 mainFrame->document()->setReferrerPolicy(frame->document()->referrerPolicy());
3514 }
3515
3516 NavigationAction newAction { *frame->document(), request, InitiatedByMainFrame::Unknown, NavigationType::Other, action.shouldOpenExternalURLsPolicy(), nullptr, action.downloadAttribute() };
3517 mainFrame->loader().loadWithNavigationAction(request, WTFMove(newAction), LockHistory::No, FrameLoadType::Standard, formState, allowNavigationToInvalidURL);
3518}
3519
3520void FrameLoader::requestFromDelegate(ResourceRequest& request, unsigned long& identifier, ResourceError& error)
3521{
3522 ASSERT(!request.isNull());
3523
3524 identifier = 0;
3525 if (Page* page = m_frame.page()) {
3526 identifier = page->progress().createUniqueIdentifier();
3527 notifier().assignIdentifierToInitialRequest(identifier, m_documentLoader.get(), request);
3528 }
3529
3530 ResourceRequest newRequest(request);
3531 notifier().dispatchWillSendRequest(m_documentLoader.get(), identifier, newRequest, ResourceResponse());
3532
3533 if (newRequest.isNull())
3534 error = cancelledError(request);
3535 else
3536 error = ResourceError();
3537
3538 request = newRequest;
3539}
3540
3541void FrameLoader::loadedResourceFromMemoryCache(CachedResource& resource, ResourceRequest& newRequest, ResourceError& error)
3542{
3543 Page* page = m_frame.page();
3544 if (!page)
3545 return;
3546
3547 if (!resource.shouldSendResourceLoadCallbacks() || m_documentLoader->haveToldClientAboutLoad(resource.url()))
3548 return;
3549
3550 // Main resource delegate messages are synthesized in MainResourceLoader, so we must not send them here.
3551 if (resource.type() == CachedResource::Type::MainResource)
3552 return;
3553
3554 if (!page->areMemoryCacheClientCallsEnabled()) {
3555 InspectorInstrumentation::didLoadResourceFromMemoryCache(*page, m_documentLoader.get(), &resource);
3556 m_documentLoader->recordMemoryCacheLoadForFutureClientNotification(resource.resourceRequest());
3557 m_documentLoader->didTellClientAboutLoad(resource.url());
3558 return;
3559 }
3560
3561 if (m_client.dispatchDidLoadResourceFromMemoryCache(m_documentLoader.get(), newRequest, resource.response(), resource.encodedSize())) {
3562 InspectorInstrumentation::didLoadResourceFromMemoryCache(*page, m_documentLoader.get(), &resource);
3563 m_documentLoader->didTellClientAboutLoad(resource.url());
3564 return;
3565 }
3566
3567 unsigned long identifier;
3568 requestFromDelegate(newRequest, identifier, error);
3569
3570 ResourceResponse response = resource.response();
3571 response.setSource(ResourceResponse::Source::MemoryCache);
3572 notifier().sendRemainingDelegateMessages(m_documentLoader.get(), identifier, newRequest, response, 0, resource.encodedSize(), 0, error);
3573}
3574
3575void FrameLoader::applyUserAgentIfNeeded(ResourceRequest& request)
3576{
3577 if (!request.hasHTTPHeaderField(HTTPHeaderName::UserAgent)) {
3578 String userAgent = this->userAgent(request.url());
3579 ASSERT(!userAgent.isNull());
3580 request.setHTTPUserAgent(userAgent);
3581 }
3582}
3583
3584bool FrameLoader::shouldInterruptLoadForXFrameOptions(const String& content, const URL& url, unsigned long requestIdentifier)
3585{
3586 Frame& topFrame = m_frame.tree().top();
3587 if (&m_frame == &topFrame)
3588 return false;
3589
3590 XFrameOptionsDisposition disposition = parseXFrameOptionsHeader(content);
3591
3592 switch (disposition) {
3593 case XFrameOptionsSameOrigin: {
3594 auto origin = SecurityOrigin::create(url);
3595 if (!origin->isSameSchemeHostPort(topFrame.document()->securityOrigin()))
3596 return true;
3597 for (Frame* frame = m_frame.tree().parent(); frame; frame = frame->tree().parent()) {
3598 if (!origin->isSameSchemeHostPort(frame->document()->securityOrigin()))
3599 return true;
3600 }
3601 return false;
3602 }
3603 case XFrameOptionsDeny:
3604 return true;
3605 case XFrameOptionsAllowAll:
3606 return false;
3607 case XFrameOptionsConflict:
3608 m_frame.document()->addConsoleMessage(MessageSource::JS, MessageLevel::Error, "Multiple 'X-Frame-Options' headers with conflicting values ('" + content + "') encountered when loading '" + url.stringCenterEllipsizedToLength() + "'. Falling back to 'DENY'.", requestIdentifier);
3609 return true;
3610 case XFrameOptionsInvalid:
3611 m_frame.document()->addConsoleMessage(MessageSource::JS, MessageLevel::Error, "Invalid 'X-Frame-Options' header encountered when loading '" + url.stringCenterEllipsizedToLength() + "': '" + content + "' is not a recognized directive. The header will be ignored.", requestIdentifier);
3612 return false;
3613 case XFrameOptionsNone:
3614 return false;
3615 }
3616 ASSERT_NOT_REACHED();
3617 return false;
3618}
3619
3620void FrameLoader::loadProvisionalItemFromCachedPage()
3621{
3622 DocumentLoader* provisionalLoader = provisionalDocumentLoader();
3623 LOG(PageCache, "WebCorePageCache: Loading provisional DocumentLoader %p with URL '%s' from CachedPage", provisionalDocumentLoader(), provisionalDocumentLoader()->url().stringCenterEllipsizedToLength().utf8().data());
3624
3625 prepareForLoadStart();
3626
3627 m_loadingFromCachedPage = true;
3628
3629 // Should have timing data from previous time(s) the page was shown.
3630 ASSERT(provisionalLoader->timing().startTime());
3631 provisionalLoader->resetTiming();
3632 provisionalLoader->timing().markStartTime();
3633
3634 provisionalLoader->setCommitted(true);
3635 commitProvisionalLoad();
3636}
3637
3638bool FrameLoader::shouldTreatURLAsSameAsCurrent(const URL& url) const
3639{
3640 if (!history().currentItem())
3641 return false;
3642 return url == history().currentItem()->url() || url == history().currentItem()->originalURL();
3643}
3644
3645bool FrameLoader::shouldTreatURLAsSrcdocDocument(const URL& url) const
3646{
3647 if (!equalLettersIgnoringASCIICase(url.string(), "about:srcdoc"))
3648 return false;
3649 HTMLFrameOwnerElement* ownerElement = m_frame.ownerElement();
3650 if (!ownerElement)
3651 return false;
3652 if (!ownerElement->hasTagName(iframeTag))
3653 return false;
3654 return ownerElement->hasAttributeWithoutSynchronization(srcdocAttr);
3655}
3656
3657Frame* FrameLoader::findFrameForNavigation(const AtomicString& name, Document* activeDocument)
3658{
3659 // FIXME: Eventually all callers should supply the actual activeDocument so we can call canNavigate with the right document.
3660 if (!activeDocument)
3661 activeDocument = m_frame.document();
3662
3663 auto* frame = m_frame.tree().find(name, activeDocument->frame() ? *activeDocument->frame() : m_frame);
3664
3665 if (!activeDocument->canNavigate(frame))
3666 return nullptr;
3667
3668 return frame;
3669}
3670
3671void FrameLoader::loadSameDocumentItem(HistoryItem& item)
3672{
3673 ASSERT(item.documentSequenceNumber() == history().currentItem()->documentSequenceNumber());
3674
3675 Ref<Frame> protect(m_frame);
3676
3677 // Save user view state to the current history item here since we don't do a normal load.
3678 // FIXME: Does form state need to be saved here too?
3679 history().saveScrollPositionAndViewStateToItem(history().currentItem());
3680 if (FrameView* view = m_frame.view())
3681 view->setWasScrolledByUser(false);
3682
3683 history().setCurrentItem(item);
3684
3685 // loadInSameDocument() actually changes the URL and notifies load delegates of a "fake" load
3686 loadInSameDocument(item.url(), item.stateObject(), false);
3687
3688 // Restore user view state from the current history item here since we don't do a normal load.
3689 history().restoreScrollPositionAndViewState();
3690}
3691
3692// FIXME: This function should really be split into a couple pieces, some of
3693// which should be methods of HistoryController and some of which should be
3694// methods of FrameLoader.
3695void FrameLoader::loadDifferentDocumentItem(HistoryItem& item, HistoryItem* fromItem, FrameLoadType loadType, FormSubmissionCacheLoadPolicy cacheLoadPolicy, ShouldTreatAsContinuingLoad shouldTreatAsContinuingLoad)
3696{
3697 RELEASE_LOG_IF_ALLOWED("loadDifferentDocumentItem: frame load started (frame = %p, main = %d)", &m_frame, m_frame.isMainFrame());
3698
3699 Ref<Frame> protectedFrame(m_frame);
3700
3701 // History items should not be reported to the parent.
3702 m_shouldReportResourceTimingToParentFrame = false;
3703
3704 // Remember this item so we can traverse any child items as child frames load
3705 history().setProvisionalItem(&item);
3706
3707 auto initiatedByMainFrame = InitiatedByMainFrame::Unknown;
3708
3709 SetForScope<LoadContinuingState> continuingLoadGuard(m_currentLoadContinuingState, shouldTreatAsContinuingLoad == ShouldTreatAsContinuingLoad::Yes ? LoadContinuingState::ContinuingWithHistoryItem : LoadContinuingState::NotContinuing);
3710
3711 if (CachedPage* cachedPage = PageCache::singleton().get(item, m_frame.page())) {
3712 auto documentLoader = cachedPage->documentLoader();
3713 m_client.updateCachedDocumentLoader(*documentLoader);
3714
3715 auto action = NavigationAction { *m_frame.document(), documentLoader->request(), initiatedByMainFrame, loadType, false };
3716 action.setTargetBackForwardItem(item);
3717 action.setSourceBackForwardItem(fromItem);
3718 documentLoader->setTriggeringAction(WTFMove(action));
3719
3720 documentLoader->setLastCheckedRequest(ResourceRequest());
3721 loadWithDocumentLoader(documentLoader, loadType, { }, AllowNavigationToInvalidURL::Yes, shouldTreatAsContinuingLoad);
3722 return;
3723 }
3724
3725 URL itemURL = item.url();
3726 URL itemOriginalURL = item.originalURL();
3727 URL currentURL;
3728 if (documentLoader())
3729 currentURL = documentLoader()->url();
3730 RefPtr<FormData> formData = item.formData();
3731
3732 ResourceRequest request(itemURL);
3733
3734 if (!item.referrer().isNull())
3735 request.setHTTPReferrer(item.referrer());
3736
3737 ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy = shouldOpenExternalURLsPolicyToApply(m_frame, initiatedByMainFrame, item.shouldOpenExternalURLsPolicy());
3738 bool isFormSubmission = false;
3739 Event* event = nullptr;
3740
3741 // If this was a repost that failed the page cache, we might try to repost the form.
3742 NavigationAction action;
3743 if (formData) {
3744 formData->generateFiles(m_frame.document());
3745
3746 request.setHTTPMethod("POST");
3747 request.setHTTPBody(WTFMove(formData));
3748 request.setHTTPContentType(item.formContentType());
3749 auto securityOrigin = SecurityOrigin::createFromString(item.referrer());
3750 addHTTPOriginIfNeeded(request, securityOrigin->toString());
3751 addHTTPUpgradeInsecureRequestsIfNeeded(request);
3752
3753 // Make sure to add extra fields to the request after the Origin header is added for the FormData case.
3754 // See https://bugs.webkit.org/show_bug.cgi?id=22194 for more discussion.
3755 addExtraFieldsToRequest(request, loadType, true);
3756
3757 // FIXME: Slight hack to test if the NSURL cache contains the page we're going to.
3758 // We want to know this before talking to the policy delegate, since it affects whether
3759 // we show the DoYouReallyWantToRepost nag.
3760 //
3761 // This trick has a small bug (3123893) where we might find a cache hit, but then
3762 // have the item vanish when we try to use it in the ensuing nav. This should be
3763 // extremely rare, but in that case the user will get an error on the navigation.
3764
3765 if (cacheLoadPolicy == MayAttemptCacheOnlyLoadForFormSubmissionItem) {
3766 request.setCachePolicy(ResourceRequestCachePolicy::ReturnCacheDataDontLoad);
3767 action = { *m_frame.document(), request, initiatedByMainFrame, loadType, isFormSubmission, event, shouldOpenExternalURLsPolicy };
3768 } else {
3769 request.setCachePolicy(ResourceRequestCachePolicy::ReturnCacheDataElseLoad);
3770 action = { *m_frame.document(), request, initiatedByMainFrame, NavigationType::FormResubmitted, shouldOpenExternalURLsPolicy, event };
3771 }
3772 } else {
3773 switch (loadType) {
3774 case FrameLoadType::Reload:
3775 case FrameLoadType::ReloadFromOrigin:
3776 case FrameLoadType::ReloadExpiredOnly:
3777 request.setCachePolicy(ResourceRequestCachePolicy::ReloadIgnoringCacheData);
3778 break;
3779 case FrameLoadType::Back:
3780 case FrameLoadType::Forward:
3781 case FrameLoadType::IndexedBackForward: {
3782#if PLATFORM(COCOA)
3783 bool allowStaleData = true;
3784#else
3785 bool allowStaleData = !item.wasRestoredFromSession();
3786#endif
3787 if (allowStaleData)
3788 request.setCachePolicy(ResourceRequestCachePolicy::ReturnCacheDataElseLoad);
3789 item.setWasRestoredFromSession(false);
3790 break;
3791 }
3792 case FrameLoadType::Standard:
3793 case FrameLoadType::RedirectWithLockedBackForwardList:
3794 break;
3795 case FrameLoadType::Same:
3796 case FrameLoadType::Replace:
3797 ASSERT_NOT_REACHED();
3798 }
3799
3800 addExtraFieldsToRequest(request, loadType, true);
3801
3802 ResourceRequest requestForOriginalURL(request);
3803 requestForOriginalURL.setURL(itemOriginalURL);
3804 action = { *m_frame.document(), requestForOriginalURL, initiatedByMainFrame, loadType, isFormSubmission, event, shouldOpenExternalURLsPolicy };
3805 }
3806
3807 action.setTargetBackForwardItem(item);
3808 action.setSourceBackForwardItem(fromItem);
3809
3810 loadWithNavigationAction(request, WTFMove(action), LockHistory::No, loadType, { }, AllowNavigationToInvalidURL::Yes);
3811}
3812
3813// Loads content into this frame, as specified by history item
3814void FrameLoader::loadItem(HistoryItem& item, HistoryItem* fromItem, FrameLoadType loadType, ShouldTreatAsContinuingLoad shouldTreatAsContinuingLoad)
3815{
3816 m_requestedHistoryItem = &item;
3817 HistoryItem* currentItem = history().currentItem();
3818 bool sameDocumentNavigation = currentItem && item.shouldDoSameDocumentNavigationTo(*currentItem);
3819
3820 if (sameDocumentNavigation)
3821 loadSameDocumentItem(item);
3822 else
3823 loadDifferentDocumentItem(item, fromItem, loadType, MayAttemptCacheOnlyLoadForFormSubmissionItem, shouldTreatAsContinuingLoad);
3824}
3825
3826void FrameLoader::retryAfterFailedCacheOnlyMainResourceLoad()
3827{
3828 ASSERT(m_state == FrameStateProvisional);
3829 ASSERT(!m_loadingFromCachedPage);
3830 ASSERT(history().provisionalItem());
3831 ASSERT(history().provisionalItem()->formData());
3832 ASSERT(history().provisionalItem() == m_requestedHistoryItem.get());
3833
3834 FrameLoadType loadType = m_loadType;
3835 HistoryItem& item = *history().provisionalItem();
3836
3837 stopAllLoaders(ShouldNotClearProvisionalItem);
3838 loadDifferentDocumentItem(item, history().currentItem(), loadType, MayNotAttemptCacheOnlyLoadForFormSubmissionItem, ShouldTreatAsContinuingLoad::No);
3839}
3840
3841ResourceError FrameLoader::cancelledError(const ResourceRequest& request) const
3842{
3843 ResourceError error = m_client.cancelledError(request);
3844 error.setType(ResourceError::Type::Cancellation);
3845 return error;
3846}
3847
3848ResourceError FrameLoader::blockedByContentBlockerError(const ResourceRequest& request) const
3849{
3850 return m_client.blockedByContentBlockerError(request);
3851}
3852
3853ResourceError FrameLoader::blockedError(const ResourceRequest& request) const
3854{
3855 ResourceError error = m_client.blockedError(request);
3856 error.setType(ResourceError::Type::Cancellation);
3857 return error;
3858}
3859
3860#if ENABLE(CONTENT_FILTERING)
3861ResourceError FrameLoader::blockedByContentFilterError(const ResourceRequest& request) const
3862{
3863 ResourceError error = m_client.blockedByContentFilterError(request);
3864 error.setType(ResourceError::Type::General);
3865 return error;
3866}
3867#endif
3868
3869#if PLATFORM(IOS_FAMILY)
3870RetainPtr<CFDictionaryRef> FrameLoader::connectionProperties(ResourceLoader* loader)
3871{
3872 return m_client.connectionProperties(loader->documentLoader(), loader->identifier());
3873}
3874#endif
3875
3876ReferrerPolicy FrameLoader::effectiveReferrerPolicy() const
3877{
3878 if (auto* parentFrame = m_frame.tree().parent())
3879 return parentFrame->document()->referrerPolicy();
3880 if (m_opener)
3881 return m_opener->document()->referrerPolicy();
3882 return ReferrerPolicy::NoReferrerWhenDowngrade;
3883}
3884
3885String FrameLoader::referrer() const
3886{
3887 return m_documentLoader ? m_documentLoader->request().httpReferrer() : emptyString();
3888}
3889
3890void FrameLoader::dispatchDidClearWindowObjectsInAllWorlds()
3891{
3892 if (!m_frame.script().canExecuteScripts(NotAboutToExecuteScript))
3893 return;
3894
3895 Vector<Ref<DOMWrapperWorld>> worlds;
3896 ScriptController::getAllWorlds(worlds);
3897 for (auto& world : worlds)
3898 dispatchDidClearWindowObjectInWorld(world);
3899}
3900
3901void FrameLoader::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld& world)
3902{
3903 if (!m_frame.script().canExecuteScripts(NotAboutToExecuteScript) || !m_frame.windowProxy().existingJSWindowProxy(world))
3904 return;
3905
3906 m_client.dispatchDidClearWindowObjectInWorld(world);
3907
3908 if (Page* page = m_frame.page())
3909 page->inspectorController().didClearWindowObjectInWorld(m_frame, world);
3910
3911 InspectorInstrumentation::didClearWindowObjectInWorld(m_frame, world);
3912}
3913
3914void FrameLoader::dispatchGlobalObjectAvailableInAllWorlds()
3915{
3916 Vector<Ref<DOMWrapperWorld>> worlds;
3917 ScriptController::getAllWorlds(worlds);
3918 for (auto& world : worlds)
3919 m_client.dispatchGlobalObjectAvailable(world);
3920}
3921
3922SandboxFlags FrameLoader::effectiveSandboxFlags() const
3923{
3924 SandboxFlags flags = m_forcedSandboxFlags;
3925 if (Frame* parentFrame = m_frame.tree().parent())
3926 flags |= parentFrame->document()->sandboxFlags();
3927 if (HTMLFrameOwnerElement* ownerElement = m_frame.ownerElement())
3928 flags |= ownerElement->sandboxFlags();
3929 return flags;
3930}
3931
3932void FrameLoader::didChangeTitle(DocumentLoader* loader)
3933{
3934 m_client.didChangeTitle(loader);
3935
3936 if (loader == m_documentLoader) {
3937 // Must update the entries in the back-forward list too.
3938 history().setCurrentItemTitle(loader->title());
3939 // This must go through the WebFrame because it has the right notion of the current b/f item.
3940 m_client.setTitle(loader->title(), loader->urlForHistory());
3941 m_client.setMainFrameDocumentReady(true); // update observers with new DOMDocument
3942 m_client.dispatchDidReceiveTitle(loader->title());
3943 }
3944
3945#if ENABLE(REMOTE_INSPECTOR)
3946 if (m_frame.isMainFrame())
3947 m_frame.page()->remoteInspectorInformationDidChange();
3948#endif
3949}
3950
3951void FrameLoader::dispatchDidCommitLoad(Optional<HasInsecureContent> initialHasInsecureContent)
3952{
3953 if (m_stateMachine.creatingInitialEmptyDocument())
3954 return;
3955
3956 m_client.dispatchDidCommitLoad(initialHasInsecureContent);
3957
3958 if (m_frame.isMainFrame()) {
3959 m_frame.page()->resetSeenPlugins();
3960 m_frame.page()->resetSeenMediaEngines();
3961 }
3962
3963 InspectorInstrumentation::didCommitLoad(m_frame, m_documentLoader.get());
3964
3965#if ENABLE(REMOTE_INSPECTOR)
3966 if (m_frame.isMainFrame())
3967 m_frame.page()->remoteInspectorInformationDidChange();
3968#endif
3969}
3970
3971void FrameLoader::tellClientAboutPastMemoryCacheLoads()
3972{
3973 ASSERT(m_frame.page());
3974 ASSERT(m_frame.page()->areMemoryCacheClientCallsEnabled());
3975
3976 if (!m_documentLoader)
3977 return;
3978
3979 Vector<ResourceRequest> pastLoads;
3980 m_documentLoader->takeMemoryCacheLoadsForClientNotification(pastLoads);
3981
3982 for (auto& pastLoad : pastLoads) {
3983 CachedResource* resource = MemoryCache::singleton().resourceForRequest(pastLoad, m_frame.page()->sessionID());
3984
3985 // FIXME: These loads, loaded from cache, but now gone from the cache by the time
3986 // Page::setMemoryCacheClientCallsEnabled(true) is called, will not be seen by the client.
3987 // Consider if there's some efficient way of remembering enough to deliver this client call.
3988 // We have the URL, but not the rest of the response or the length.
3989 if (!resource)
3990 continue;
3991
3992 ResourceRequest request(resource->url());
3993 m_client.dispatchDidLoadResourceFromMemoryCache(m_documentLoader.get(), request, resource->response(), resource->encodedSize());
3994 }
3995}
3996
3997NetworkingContext* FrameLoader::networkingContext() const
3998{
3999 return m_networkingContext.get();
4000}
4001
4002void FrameLoader::loadProgressingStatusChanged()
4003{
4004 if (auto* view = m_frame.mainFrame().view())
4005 view->loadProgressingStatusChanged();
4006}
4007
4008void FrameLoader::forcePageTransitionIfNeeded()
4009{
4010 m_client.forcePageTransitionIfNeeded();
4011}
4012
4013void FrameLoader::clearTestingOverrides()
4014{
4015 m_overrideCachePolicyForTesting = WTF::nullopt;
4016 m_overrideResourceLoadPriorityForTesting = WTF::nullopt;
4017 m_isStrictRawResourceValidationPolicyDisabledForTesting = false;
4018}
4019
4020bool FrameLoader::isAlwaysOnLoggingAllowed() const
4021{
4022 return frame().isAlwaysOnLoggingAllowed();
4023}
4024
4025bool FrameLoaderClient::hasHTMLView() const
4026{
4027 return true;
4028}
4029
4030RefPtr<Frame> createWindow(Frame& openerFrame, Frame& lookupFrame, FrameLoadRequest&& request, const WindowFeatures& features, bool& created)
4031{
4032 ASSERT(!features.dialog || request.frameName().isEmpty());
4033
4034 created = false;
4035
4036 // FIXME: Provide line number information with respect to the opener's document.
4037 if (WTF::protocolIsJavaScript(request.resourceRequest().url()) && !openerFrame.document()->contentSecurityPolicy()->allowJavaScriptURLs(openerFrame.document()->url(), { }))
4038 return nullptr;
4039
4040 if (!request.frameName().isEmpty() && !equalIgnoringASCIICase(request.frameName(), "_blank")) {
4041 if (RefPtr<Frame> frame = lookupFrame.loader().findFrameForNavigation(request.frameName(), openerFrame.document())) {
4042 if (!equalIgnoringASCIICase(request.frameName(), "_self")) {
4043 if (Page* page = frame->page())
4044 page->chrome().focus();
4045 }
4046 return frame;
4047 }
4048 }
4049
4050 // Sandboxed frames cannot open new auxiliary browsing contexts.
4051 if (isDocumentSandboxed(openerFrame, SandboxPopups)) {
4052 // FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
4053 openerFrame.document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Blocked opening '" + request.resourceRequest().url().stringCenterEllipsizedToLength() + "' in a new window because the request was made in a sandboxed frame whose 'allow-popups' permission is not set.");
4054 return nullptr;
4055 }
4056
4057 // FIXME: Setting the referrer should be the caller's responsibility.
4058 String referrer = SecurityPolicy::generateReferrerHeader(openerFrame.document()->referrerPolicy(), request.resourceRequest().url(), openerFrame.loader().outgoingReferrer());
4059 if (!referrer.isEmpty())
4060 request.resourceRequest().setHTTPReferrer(referrer);
4061 FrameLoader::addHTTPOriginIfNeeded(request.resourceRequest(), openerFrame.loader().outgoingOrigin());
4062 FrameLoader::addHTTPUpgradeInsecureRequestsIfNeeded(request.resourceRequest());
4063 FrameLoader::addSameSiteInfoToRequestIfNeeded(request.resourceRequest(), openerFrame.document());
4064
4065 Page* oldPage = openerFrame.page();
4066 if (!oldPage)
4067 return nullptr;
4068
4069 ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy = shouldOpenExternalURLsPolicyToApply(openerFrame, request);
4070 NavigationAction action { request.requester(), request.resourceRequest(), request.initiatedByMainFrame(), NavigationType::Other, shouldOpenExternalURLsPolicy };
4071 Page* page = oldPage->chrome().createWindow(openerFrame, request, features, action);
4072 if (!page)
4073 return nullptr;
4074
4075 RefPtr<Frame> frame = &page->mainFrame();
4076
4077 if (isDocumentSandboxed(openerFrame, SandboxPropagatesToAuxiliaryBrowsingContexts))
4078 frame->loader().forceSandboxFlags(openerFrame.document()->sandboxFlags());
4079
4080 if (!equalIgnoringASCIICase(request.frameName(), "_blank"))
4081 frame->tree().setName(request.frameName());
4082
4083 page->chrome().setToolbarsVisible(features.toolBarVisible || features.locationBarVisible);
4084
4085 if (!frame->page())
4086 return nullptr;
4087 page->chrome().setStatusbarVisible(features.statusBarVisible);
4088
4089 if (!frame->page())
4090 return nullptr;
4091 page->chrome().setScrollbarsVisible(features.scrollbarsVisible);
4092
4093 if (!frame->page())
4094 return nullptr;
4095 page->chrome().setMenubarVisible(features.menuBarVisible);
4096
4097 if (!frame->page())
4098 return nullptr;
4099 page->chrome().setResizable(features.resizable);
4100
4101 // 'x' and 'y' specify the location of the window, while 'width' and 'height'
4102 // specify the size of the viewport. We can only resize the window, so adjust
4103 // for the difference between the window size and the viewport size.
4104
4105 // FIXME: We should reconcile the initialization of viewport arguments between iOS and non-IOS.
4106#if !PLATFORM(IOS_FAMILY)
4107 FloatSize viewportSize = page->chrome().pageRect().size();
4108 FloatRect windowRect = page->chrome().windowRect();
4109 if (features.x)
4110 windowRect.setX(*features.x);
4111 if (features.y)
4112 windowRect.setY(*features.y);
4113 // Zero width and height mean using default size, not minumum one.
4114 if (features.width && *features.width)
4115 windowRect.setWidth(*features.width + (windowRect.width() - viewportSize.width()));
4116 if (features.height && *features.height)
4117 windowRect.setHeight(*features.height + (windowRect.height() - viewportSize.height()));
4118
4119 // Ensure non-NaN values, minimum size as well as being within valid screen area.
4120 FloatRect newWindowRect = DOMWindow::adjustWindowRect(*page, windowRect);
4121
4122 if (!frame->page())
4123 return nullptr;
4124 page->chrome().setWindowRect(newWindowRect);
4125#else
4126 // On iOS, width and height refer to the viewport dimensions.
4127 ViewportArguments arguments;
4128 // Zero width and height mean using default size, not minimum one.
4129 if (features.width && *features.width)
4130 arguments.width = *features.width;
4131 if (features.height && *features.height)
4132 arguments.height = *features.height;
4133 frame->setViewportArguments(arguments);
4134#endif
4135
4136 if (!frame->page())
4137 return nullptr;
4138 page->chrome().show();
4139
4140 created = true;
4141 return frame;
4142}
4143
4144bool FrameLoader::shouldSuppressTextInputFromEditing() const
4145{
4146 return m_frame.settings().shouldSuppressTextInputFromEditingDuringProvisionalNavigation() && m_state == FrameStateProvisional;
4147}
4148
4149} // namespace WebCore
4150