Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / loader / FrameLoader.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 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 Computer, 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 "core/loader/FrameLoader.h"
37
38 #include "HTMLNames.h"
39 #include "bindings/v8/DOMWrapperWorld.h"
40 #include "bindings/v8/ScriptController.h"
41 #include "bindings/v8/SerializedScriptValue.h"
42 #include "core/dom/Document.h"
43 #include "core/dom/Element.h"
44 #include "core/dom/ViewportDescription.h"
45 #include "core/editing/Editor.h"
46 #include "core/editing/UndoStack.h"
47 #include "core/events/Event.h"
48 #include "core/events/PageTransitionEvent.h"
49 #include "core/fetch/FetchContext.h"
50 #include "core/fetch/ResourceFetcher.h"
51 #include "core/fetch/ResourceLoader.h"
52 #include "core/frame/DOMWindow.h"
53 #include "core/frame/FrameHost.h"
54 #include "core/frame/FrameView.h"
55 #include "core/frame/LocalFrame.h"
56 #include "core/frame/PinchViewport.h"
57 #include "core/frame/csp/ContentSecurityPolicy.h"
58 #include "core/html/HTMLFormElement.h"
59 #include "core/html/HTMLFrameOwnerElement.h"
60 #include "core/html/parser/HTMLParserIdioms.h"
61 #include "core/inspector/InspectorController.h"
62 #include "core/inspector/InspectorInstrumentation.h"
63 #include "core/loader/DocumentLoadTiming.h"
64 #include "core/loader/DocumentLoader.h"
65 #include "core/loader/FormState.h"
66 #include "core/loader/FormSubmission.h"
67 #include "core/loader/FrameFetchContext.h"
68 #include "core/loader/FrameLoadRequest.h"
69 #include "core/loader/FrameLoaderClient.h"
70 #include "core/loader/ProgressTracker.h"
71 #include "core/loader/UniqueIdentifier.h"
72 #include "core/loader/appcache/ApplicationCacheHost.h"
73 #include "core/page/BackForwardClient.h"
74 #include "core/page/Chrome.h"
75 #include "core/page/ChromeClient.h"
76 #include "core/page/CreateWindow.h"
77 #include "core/page/EventHandler.h"
78 #include "core/page/FrameTree.h"
79 #include "core/page/Page.h"
80 #include "core/frame/Settings.h"
81 #include "core/page/WindowFeatures.h"
82 #include "core/page/scrolling/ScrollingCoordinator.h"
83 #include "core/xml/parser/XMLDocumentParser.h"
84 #include "platform/Logging.h"
85 #include "platform/UserGestureIndicator.h"
86 #include "platform/geometry/FloatRect.h"
87 #include "platform/network/ContentSecurityPolicyResponseHeaders.h"
88 #include "platform/network/HTTPParsers.h"
89 #include "platform/network/ResourceRequest.h"
90 #include "platform/scroll/ScrollAnimator.h"
91 #include "platform/weborigin/SecurityOrigin.h"
92 #include "platform/weborigin/SecurityPolicy.h"
93 #include "wtf/TemporaryChange.h"
94 #include "wtf/text/CString.h"
95 #include "wtf/text/WTFString.h"
96
97 namespace WebCore {
98
99 using namespace HTMLNames;
100
101 bool isBackForwardLoadType(FrameLoadType type)
102 {
103     return type == FrameLoadTypeBackForward;
104 }
105
106 static bool needsHistoryItemRestore(FrameLoadType type)
107 {
108     return type == FrameLoadTypeBackForward || type == FrameLoadTypeReload || type == FrameLoadTypeReloadFromOrigin;
109 }
110
111 FrameLoader::FrameLoader(LocalFrame* frame, FrameLoaderClient* client)
112     : m_frame(frame)
113     , m_client(client)
114     , m_mixedContentChecker(frame)
115     , m_progressTracker(ProgressTracker::create(frame))
116     , m_state(FrameStateProvisional)
117     , m_loadType(FrameLoadTypeStandard)
118     , m_fetchContext(FrameFetchContext::create(frame))
119     , m_inStopAllLoaders(false)
120     , m_isComplete(false)
121     , m_checkTimer(this, &FrameLoader::checkTimerFired)
122     , m_shouldCallCheckCompleted(false)
123     , m_didAccessInitialDocument(false)
124     , m_didAccessInitialDocumentTimer(this, &FrameLoader::didAccessInitialDocumentTimerFired)
125     , m_forcedSandboxFlags(SandboxNone)
126 {
127 }
128
129 FrameLoader::~FrameLoader()
130 {
131 }
132
133 void FrameLoader::init()
134 {
135     m_provisionalDocumentLoader = m_client->createDocumentLoader(m_frame, ResourceRequest(KURL(ParsedURLString, emptyString())), SubstituteData());
136     m_provisionalDocumentLoader->startLoadingMainResource();
137     m_frame->document()->cancelParsing();
138     m_stateMachine.advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocument);
139 }
140
141 void FrameLoader::setDefersLoading(bool defers)
142 {
143     if (m_documentLoader)
144         m_documentLoader->setDefersLoading(defers);
145     if (m_provisionalDocumentLoader)
146         m_provisionalDocumentLoader->setDefersLoading(defers);
147     if (m_policyDocumentLoader)
148         m_policyDocumentLoader->setDefersLoading(defers);
149
150     if (!defers) {
151         if (m_deferredHistoryLoad.isValid()) {
152             loadHistoryItem(m_deferredHistoryLoad.m_item.get(), m_deferredHistoryLoad.m_type, m_deferredHistoryLoad.m_cachePolicy);
153             m_deferredHistoryLoad = DeferredHistoryLoad();
154         }
155         m_frame->navigationScheduler().startTimer();
156         startCheckCompleteTimer();
157     }
158 }
159
160 void FrameLoader::stopLoading()
161 {
162     m_isComplete = true; // to avoid calling completed() in finishedParsing()
163
164     if (m_frame->document() && m_frame->document()->parsing()) {
165         finishedParsing();
166         m_frame->document()->setParsing(false);
167     }
168
169     if (Document* doc = m_frame->document()) {
170         // FIXME: HTML5 doesn't tell us to set the state to complete when aborting, but we do anyway to match legacy behavior.
171         // http://www.w3.org/Bugs/Public/show_bug.cgi?id=10537
172         doc->setReadyState(Document::Complete);
173     }
174
175     // FIXME: This will cancel redirection timer, which really needs to be restarted when restoring the frame from b/f cache.
176     m_frame->navigationScheduler().cancel();
177 }
178
179 void FrameLoader::saveScrollState()
180 {
181     if (!m_currentItem || !m_frame->view())
182         return;
183
184     // Shouldn't clobber anything if we might still restore later.
185     if (needsHistoryItemRestore(m_loadType) && !m_frame->view()->wasScrolledByUser())
186         return;
187
188     m_currentItem->setScrollPoint(m_frame->view()->scrollPosition());
189
190     if (m_frame->settings()->pinchVirtualViewportEnabled())
191         m_currentItem->setPinchViewportScrollPoint(m_frame->host()->pinchViewport().visibleRect().location());
192     else
193         m_currentItem->setPinchViewportScrollPoint(FloatPoint(-1, -1));
194
195     if (m_frame->isMainFrame() && !m_frame->page()->inspectorController().deviceEmulationEnabled())
196         m_currentItem->setPageScaleFactor(m_frame->page()->pageScaleFactor());
197
198     m_client->didUpdateCurrentHistoryItem();
199 }
200
201 void FrameLoader::clearScrollPositionAndViewState()
202 {
203     ASSERT(m_frame->isMainFrame());
204     if (!m_currentItem)
205         return;
206     m_currentItem->clearScrollPoint();
207     m_currentItem->setPageScaleFactor(0);
208 }
209
210 bool FrameLoader::closeURL()
211 {
212     saveScrollState();
213
214     // Should only send the pagehide event here if the current document exists.
215     if (m_frame->document())
216         m_frame->document()->dispatchUnloadEvents();
217     stopLoading();
218
219     if (Page* page = m_frame->page())
220         page->undoStack().didUnloadFrame(*m_frame);
221     return true;
222 }
223
224 void FrameLoader::didExplicitOpen()
225 {
226     m_isComplete = false;
227
228     // Calling document.open counts as committing the first real document load.
229     if (!m_stateMachine.committedFirstRealDocumentLoad())
230         m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedFirstRealLoad);
231
232     // Prevent window.open(url) -- eg window.open("about:blank") -- from blowing away results
233     // from a subsequent window.document.open / window.document.write call.
234     // Canceling redirection here works for all cases because document.open
235     // implicitly precedes document.write.
236     m_frame->navigationScheduler().cancel();
237 }
238
239 void FrameLoader::clear()
240 {
241     if (m_stateMachine.creatingInitialEmptyDocument())
242         return;
243
244     m_frame->editor().clear();
245     m_frame->document()->cancelParsing();
246     m_frame->document()->prepareForDestruction();
247     m_frame->document()->removeFocusedElementOfSubtree(m_frame->document());
248
249     m_frame->selection().prepareForDestruction();
250     m_frame->eventHandler().clear();
251     if (m_frame->view())
252         m_frame->view()->clear();
253
254     m_frame->script().enableEval();
255
256     m_frame->navigationScheduler().clear();
257
258     m_checkTimer.stop();
259     m_shouldCallCheckCompleted = false;
260
261     if (m_stateMachine.isDisplayingInitialEmptyDocument())
262         m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedFirstRealLoad);
263 }
264
265 void FrameLoader::setHistoryItemStateForCommit(HistoryCommitType historyCommitType, bool isPushOrReplaceState, PassRefPtr<SerializedScriptValue> stateObject)
266 {
267     if (m_provisionalItem)
268         m_currentItem = m_provisionalItem.release();
269
270     if (!m_currentItem || historyCommitType == StandardCommit) {
271         m_currentItem = HistoryItem::create();
272     } else if (!isPushOrReplaceState && m_documentLoader->url() != m_currentItem->url()) {
273         m_currentItem->generateNewItemSequenceNumber();
274         if (!equalIgnoringFragmentIdentifier(m_documentLoader->url(), m_currentItem->url()))
275             m_currentItem->generateNewDocumentSequenceNumber();
276     }
277
278     m_currentItem->setURL(m_documentLoader->urlForHistory());
279     m_currentItem->setDocumentState(m_frame->document()->formElementsState());
280     m_currentItem->setTarget(m_frame->tree().uniqueName());
281     if (isPushOrReplaceState)
282         m_currentItem->setStateObject(stateObject);
283     m_currentItem->setReferrer(Referrer(m_documentLoader->request().httpReferrer(), m_documentLoader->request().referrerPolicy()));
284     m_currentItem->setFormInfoFromRequest(isPushOrReplaceState ? ResourceRequest() : m_documentLoader->request());
285 }
286
287 static HistoryCommitType loadTypeToCommitType(FrameLoadType type, bool isValidHistoryURL)
288 {
289     switch (type) {
290     case FrameLoadTypeStandard:
291         return isValidHistoryURL ? StandardCommit : HistoryInertCommit;
292     case FrameLoadTypeInitialInChildFrame:
293         return InitialCommitInChildFrame;
294     case FrameLoadTypeBackForward:
295         return BackForwardCommit;
296     default:
297         break;
298     }
299     return HistoryInertCommit;
300 }
301
302 void FrameLoader::receivedFirstData()
303 {
304     if (m_stateMachine.creatingInitialEmptyDocument())
305         return;
306
307     bool isValidHistoryURL = !m_documentLoader->urlForHistory().isEmpty() && (!opener() || m_currentItem || !m_documentLoader->originalRequest().url().isEmpty());
308     HistoryCommitType historyCommitType = loadTypeToCommitType(m_loadType, isValidHistoryURL);
309     setHistoryItemStateForCommit(historyCommitType);
310
311     if (!m_stateMachine.committedMultipleRealLoads() && m_loadType == FrameLoadTypeStandard)
312         m_stateMachine.advanceTo(FrameLoaderStateMachine::CommittedMultipleRealLoads);
313
314     m_client->dispatchDidCommitLoad(m_frame, m_currentItem.get(), historyCommitType);
315
316     InspectorInstrumentation::didCommitLoad(m_frame, m_documentLoader.get());
317     m_frame->page()->didCommitLoad(m_frame);
318     dispatchDidClearDocumentOfWindowObject();
319 }
320
321 static void didFailContentSecurityPolicyCheck(FrameLoader* loader)
322 {
323     // load event and stopAllLoaders can detach the LocalFrame, so protect it.
324     RefPtr<LocalFrame> frame(loader->frame());
325
326     // Move the page to a unique origin, and cancel the load.
327     frame->document()->enforceSandboxFlags(SandboxOrigin);
328     loader->stopAllLoaders();
329
330     // Fire a load event, as timing attacks would otherwise reveal that the
331     // frame was blocked. This way, it looks like every other cross-origin
332     // page.
333     if (HTMLFrameOwnerElement* ownerElement = frame->ownerElement())
334         ownerElement->dispatchEvent(Event::create(EventTypeNames::load));
335 }
336
337 void FrameLoader::didBeginDocument(bool dispatch)
338 {
339     m_isComplete = false;
340     m_frame->document()->setReadyState(Document::Loading);
341
342     if (m_provisionalItem && m_loadType == FrameLoadTypeBackForward)
343         m_frame->domWindow()->statePopped(m_provisionalItem->stateObject());
344
345     if (dispatch)
346         dispatchDidClearDocumentOfWindowObject();
347
348     m_frame->document()->initContentSecurityPolicy(m_documentLoader ? ContentSecurityPolicyResponseHeaders(m_documentLoader->response()) : ContentSecurityPolicyResponseHeaders());
349
350     if (!m_frame->document()->contentSecurityPolicy()->allowAncestors(m_frame)) {
351         didFailContentSecurityPolicyCheck(this);
352         return;
353     }
354
355     Settings* settings = m_frame->document()->settings();
356     if (settings) {
357         m_frame->document()->fetcher()->setImagesEnabled(settings->imagesEnabled());
358         m_frame->document()->fetcher()->setAutoLoadImages(settings->loadsImagesAutomatically());
359     }
360
361     if (m_documentLoader) {
362         const AtomicString& dnsPrefetchControl = m_documentLoader->response().httpHeaderField("X-DNS-Prefetch-Control");
363         if (!dnsPrefetchControl.isEmpty())
364             m_frame->document()->parseDNSPrefetchControlHeader(dnsPrefetchControl);
365
366         String headerContentLanguage = m_documentLoader->response().httpHeaderField("Content-Language");
367         if (!headerContentLanguage.isEmpty()) {
368             size_t commaIndex = headerContentLanguage.find(',');
369             headerContentLanguage.truncate(commaIndex); // kNotFound == -1 == don't truncate
370             headerContentLanguage = headerContentLanguage.stripWhiteSpace(isHTMLSpace<UChar>);
371             if (!headerContentLanguage.isEmpty())
372                 m_frame->document()->setContentLanguage(AtomicString(headerContentLanguage));
373         }
374     }
375
376     if (m_provisionalItem && m_loadType == FrameLoadTypeBackForward)
377         m_frame->document()->setStateForNewFormElements(m_provisionalItem->documentState());
378 }
379
380 void FrameLoader::finishedParsing()
381 {
382     if (m_stateMachine.creatingInitialEmptyDocument())
383         return;
384
385     // This can be called from the LocalFrame's destructor, in which case we shouldn't protect ourselves
386     // because doing so will cause us to re-enter the destructor when protector goes out of scope.
387     // Null-checking the FrameView indicates whether or not we're in the destructor.
388     RefPtr<LocalFrame> protector = m_frame->view() ? m_frame : 0;
389
390     if (m_client)
391         m_client->dispatchDidFinishDocumentLoad();
392
393     checkCompleted();
394
395     if (!m_frame->view())
396         return; // We are being destroyed by something checkCompleted called.
397
398     // Check if the scrollbars are really needed for the content.
399     // If not, remove them, relayout, and repaint.
400     m_frame->view()->restoreScrollbar();
401     scrollToFragmentWithParentBoundary(m_frame->document()->url());
402 }
403
404 void FrameLoader::loadDone()
405 {
406     checkCompleted();
407 }
408
409 bool FrameLoader::allChildrenAreComplete() const
410 {
411     for (LocalFrame* child = m_frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
412         if (!child->loader().m_isComplete)
413             return false;
414     }
415     return true;
416 }
417
418 bool FrameLoader::allAncestorsAreComplete() const
419 {
420     for (LocalFrame* ancestor = m_frame; ancestor; ancestor = ancestor->tree().parent()) {
421         if (!ancestor->document()->loadEventFinished())
422             return false;
423     }
424     return true;
425 }
426
427 void FrameLoader::checkCompleted()
428 {
429     RefPtr<LocalFrame> protect(m_frame);
430     m_shouldCallCheckCompleted = false;
431
432     if (m_frame->view())
433         m_frame->view()->handleLoadCompleted();
434
435     // Have we completed before?
436     if (m_isComplete)
437         return;
438
439     // Are we still parsing?
440     if (m_frame->document()->parsing())
441         return;
442
443     // Still waiting imports?
444     if (!m_frame->document()->haveImportsLoaded())
445         return;
446
447     // Still waiting for images/scripts?
448     if (m_frame->document()->fetcher()->requestCount())
449         return;
450
451     // Still waiting for elements that don't go through a FrameLoader?
452     if (m_frame->document()->isDelayingLoadEvent())
453         return;
454
455     // Any frame that hasn't completed yet?
456     if (!allChildrenAreComplete())
457         return;
458
459     // OK, completed.
460     m_isComplete = true;
461     m_frame->document()->setReadyState(Document::Complete);
462     if (m_frame->document()->loadEventStillNeeded())
463         m_frame->document()->implicitClose();
464
465     m_frame->navigationScheduler().startTimer();
466
467     completed();
468     if (m_frame->page())
469         checkLoadComplete();
470
471     if (m_frame->view())
472         m_frame->view()->handleLoadCompleted();
473 }
474
475 void FrameLoader::checkTimerFired(Timer<FrameLoader>*)
476 {
477     RefPtr<LocalFrame> protect(m_frame);
478
479     if (Page* page = m_frame->page()) {
480         if (page->defersLoading())
481             return;
482     }
483     if (m_shouldCallCheckCompleted)
484         checkCompleted();
485 }
486
487 void FrameLoader::startCheckCompleteTimer()
488 {
489     if (!m_shouldCallCheckCompleted)
490         return;
491     if (m_checkTimer.isActive())
492         return;
493     m_checkTimer.startOneShot(0, FROM_HERE);
494 }
495
496 void FrameLoader::scheduleCheckCompleted()
497 {
498     m_shouldCallCheckCompleted = true;
499     startCheckCompleteTimer();
500 }
501
502 LocalFrame* FrameLoader::opener()
503 {
504     // FIXME: Temporary hack to stage converting locations that really should be Frame.
505     return m_client ? toLocalFrame(m_client->opener()) : 0;
506 }
507
508 void FrameLoader::setOpener(LocalFrame* opener)
509 {
510     // If the frame is already detached, the opener has already been cleared.
511     if (m_client)
512         m_client->setOpener(opener);
513 }
514
515 bool FrameLoader::allowPlugins(ReasonForCallingAllowPlugins reason)
516 {
517     Settings* settings = m_frame->settings();
518     bool allowed = m_client->allowPlugins(settings && settings->pluginsEnabled());
519     if (!allowed && reason == AboutToInstantiatePlugin)
520         m_client->didNotAllowPlugins();
521     return allowed;
522 }
523
524 void FrameLoader::updateForSameDocumentNavigation(const KURL& newURL, SameDocumentNavigationSource sameDocumentNavigationSource, PassRefPtr<SerializedScriptValue> data, UpdateBackForwardListPolicy updateBackForwardList)
525 {
526     // Update the data source's request with the new URL to fake the URL change
527     m_frame->document()->setURL(newURL);
528     documentLoader()->updateForSameDocumentNavigation(newURL);
529
530     // Generate start and stop notifications only when loader is completed so that we
531     // don't fire them for fragment redirection that happens in window.onload handler.
532     // See https://bugs.webkit.org/show_bug.cgi?id=31838
533     if (m_frame->document()->loadEventFinished())
534         m_client->didStartLoading(NavigationWithinSameDocument);
535
536     HistoryCommitType historyCommitType = updateBackForwardList == UpdateBackForwardList && m_currentItem ? StandardCommit : HistoryInertCommit;
537     setHistoryItemStateForCommit(historyCommitType, sameDocumentNavigationSource == SameDocumentNavigationHistoryApi, data);
538     m_client->dispatchDidNavigateWithinPage(m_currentItem.get(), historyCommitType);
539     m_client->dispatchDidReceiveTitle(m_frame->document()->title());
540     if (m_frame->document()->loadEventFinished())
541         m_client->didStopLoading();
542 }
543
544 void FrameLoader::loadInSameDocument(const KURL& url, PassRefPtr<SerializedScriptValue> stateObject, UpdateBackForwardListPolicy updateBackForwardList, ClientRedirectPolicy clientRedirect)
545 {
546     // If we have a state object, we cannot also be a new navigation.
547     ASSERT(!stateObject || updateBackForwardList == DoNotUpdateBackForwardList);
548
549     // If we have a provisional request for a different document, a fragment scroll should cancel it.
550     if (m_provisionalDocumentLoader) {
551         m_provisionalDocumentLoader->stopLoading();
552         if (m_provisionalDocumentLoader)
553             m_provisionalDocumentLoader->detachFromFrame();
554         m_provisionalDocumentLoader = nullptr;
555     }
556     saveScrollState();
557
558     KURL oldURL = m_frame->document()->url();
559     // If we were in the autoscroll/panScroll mode we want to stop it before following the link to the anchor
560     bool hashChange = equalIgnoringFragmentIdentifier(url, oldURL) && url.fragmentIdentifier() != oldURL.fragmentIdentifier();
561     if (hashChange) {
562         m_frame->eventHandler().stopAutoscroll();
563         m_frame->domWindow()->enqueueHashchangeEvent(oldURL, url);
564     }
565     m_documentLoader->setIsClientRedirect(clientRedirect == ClientRedirect);
566     bool replacesCurrentHistoryItem = updateBackForwardList == DoNotUpdateBackForwardList;
567     m_documentLoader->setReplacesCurrentHistoryItem(replacesCurrentHistoryItem);
568     updateForSameDocumentNavigation(url, SameDocumentNavigationDefault, nullptr, updateBackForwardList);
569
570     m_frame->view()->setWasScrolledByUser(false);
571
572     // It's important to model this as a load that starts and immediately finishes.
573     // Otherwise, the parent frame may think we never finished loading.
574     started();
575
576     // We need to scroll to the fragment whether or not a hash change occurred, since
577     // the user might have scrolled since the previous navigation.
578     scrollToFragmentWithParentBoundary(url);
579
580     m_isComplete = false;
581     checkCompleted();
582
583     m_frame->domWindow()->statePopped(stateObject ? stateObject : SerializedScriptValue::nullValue());
584 }
585
586 void FrameLoader::completed()
587 {
588     RefPtr<LocalFrame> protect(m_frame);
589
590     for (LocalFrame* descendant = m_frame->tree().traverseNext(m_frame); descendant; descendant = descendant->tree().traverseNext(m_frame))
591         descendant->navigationScheduler().startTimer();
592
593     if (LocalFrame* parent = m_frame->tree().parent())
594         parent->loader().checkCompleted();
595
596     if (m_frame->view())
597         m_frame->view()->maintainScrollPositionAtAnchor(0);
598 }
599
600 void FrameLoader::started()
601 {
602     for (LocalFrame* frame = m_frame; frame; frame = frame->tree().parent())
603         frame->loader().m_isComplete = false;
604 }
605
606 void FrameLoader::setReferrerForFrameRequest(ResourceRequest& request, ShouldSendReferrer shouldSendReferrer, Document* originDocument)
607 {
608     if (shouldSendReferrer == NeverSendReferrer) {
609         request.clearHTTPReferrer();
610         return;
611     }
612
613     // Always use the initiating document to generate the referrer.
614     // We need to generateReferrerHeader(), because we might not have enforced ReferrerPolicy or https->http
615     // referrer suppression yet.
616     String argsReferrer(request.httpReferrer());
617     if (argsReferrer.isEmpty())
618         argsReferrer = originDocument->outgoingReferrer();
619     String referrer = SecurityPolicy::generateReferrerHeader(originDocument->referrerPolicy(), request.url(), argsReferrer);
620
621     request.setHTTPReferrer(Referrer(referrer, originDocument->referrerPolicy()));
622     RefPtr<SecurityOrigin> referrerOrigin = SecurityOrigin::createFromString(referrer);
623     addHTTPOriginIfNeeded(request, referrerOrigin->toAtomicString());
624 }
625
626 bool FrameLoader::isScriptTriggeredFormSubmissionInChildFrame(const FrameLoadRequest& request) const
627 {
628     // If this is a child frame and the form submission was triggered by a script, lock the back/forward list
629     // to match IE and Opera.
630     // See https://bugs.webkit.org/show_bug.cgi?id=32383 for the original motivation for this.
631     if (!m_frame->tree().parent() || UserGestureIndicator::processingUserGesture())
632         return false;
633     return request.formState() && request.formState()->formSubmissionTrigger() == SubmittedByJavaScript;
634 }
635
636 FrameLoadType FrameLoader::determineFrameLoadType(const FrameLoadRequest& request)
637 {
638     if (m_frame->tree().parent() && !m_stateMachine.committedFirstRealDocumentLoad())
639         return FrameLoadTypeInitialInChildFrame;
640     if (!m_frame->tree().parent() && !m_frame->page()->backForward().backForwardListCount())
641         return FrameLoadTypeStandard;
642     if (m_provisionalDocumentLoader && request.substituteData().failingURL() == m_provisionalDocumentLoader->url() && m_loadType == FrameLoadTypeBackForward)
643         return FrameLoadTypeBackForward;
644     if (request.resourceRequest().cachePolicy() == ReloadIgnoringCacheData)
645         return FrameLoadTypeReload;
646     if (request.lockBackForwardList() || isScriptTriggeredFormSubmissionInChildFrame(request))
647         return FrameLoadTypeRedirectWithLockedBackForwardList;
648     if (!request.originDocument() && request.resourceRequest().url() == m_documentLoader->urlForHistory())
649         return FrameLoadTypeSame;
650     if (request.substituteData().failingURL() == m_documentLoader->urlForHistory() && m_loadType == FrameLoadTypeReload)
651         return FrameLoadTypeReload;
652     return FrameLoadTypeStandard;
653 }
654
655 bool FrameLoader::prepareRequestForThisFrame(FrameLoadRequest& request)
656 {
657     // If no origin Document* was specified, skip security checks and assume the caller has fully initialized the FrameLoadRequest.
658     if (!request.originDocument())
659         return true;
660
661     KURL url = request.resourceRequest().url();
662     if (m_frame->script().executeScriptIfJavaScriptURL(url))
663         return false;
664
665     if (!request.originDocument()->securityOrigin()->canDisplay(url)) {
666         reportLocalLoadFailed(m_frame, url.elidedString());
667         return false;
668     }
669
670     if (!request.formState() && request.frameName().isEmpty())
671         request.setFrameName(m_frame->document()->baseTarget());
672
673     setReferrerForFrameRequest(request.resourceRequest(), request.shouldSendReferrer(), request.originDocument());
674     return true;
675 }
676
677 static bool shouldOpenInNewWindow(LocalFrame* targetFrame, const FrameLoadRequest& request, const NavigationAction& action)
678 {
679     if (!targetFrame && !request.frameName().isEmpty())
680         return true;
681     // FIXME: This case is a workaround for the fact that ctrl+clicking a form submission incorrectly
682     // sends as a GET rather than a POST if it creates a new window in a different process.
683     return request.formState() && action.shouldOpenInNewWindow();
684 }
685
686 void FrameLoader::load(const FrameLoadRequest& passedRequest)
687 {
688     ASSERT(m_frame->document());
689
690     RefPtr<LocalFrame> protect(m_frame);
691
692     if (m_inStopAllLoaders)
693         return;
694
695     FrameLoadRequest request(passedRequest);
696     if (!prepareRequestForThisFrame(request))
697         return;
698
699     RefPtr<LocalFrame> targetFrame = request.formState() ? 0 : findFrameForNavigation(AtomicString(request.frameName()), request.formState() ? request.formState()->sourceDocument() : m_frame->document());
700     if (targetFrame && targetFrame != m_frame) {
701         request.setFrameName("_self");
702         targetFrame->loader().load(request);
703         if (Page* page = targetFrame->page())
704             page->chrome().focus();
705         return;
706     }
707
708     FrameLoadType newLoadType = determineFrameLoadType(request);
709     NavigationAction action(request.resourceRequest(), newLoadType, request.formState(), request.triggeringEvent());
710     if (shouldOpenInNewWindow(targetFrame.get(), request, action)) {
711         if (action.policy() == NavigationPolicyDownload)
712             m_client->loadURLExternally(action.resourceRequest(), NavigationPolicyDownload);
713         else
714             createWindowForRequest(request, *m_frame, action.policy(), request.shouldSendReferrer());
715         return;
716     }
717
718     const KURL& url = request.resourceRequest().url();
719     if (!action.shouldOpenInNewWindow() && shouldPerformFragmentNavigation(request.formState(), request.resourceRequest().httpMethod(), newLoadType, url)) {
720         m_documentLoader->setTriggeringAction(action);
721         loadInSameDocument(url, nullptr, newLoadType == FrameLoadTypeStandard && !shouldTreatURLAsSameAsCurrent(url) ? UpdateBackForwardList : DoNotUpdateBackForwardList, request.clientRedirect());
722         return;
723     }
724     bool sameURL = url == m_documentLoader->urlForHistory();
725     loadWithNavigationAction(action, newLoadType, request.formState(), request.substituteData(), request.clientRedirect());
726     // Example of this case are sites that reload the same URL with a different cookie
727     // driving the generated content, or a master frame with links that drive a target
728     // frame, where the user has clicked on the same link repeatedly.
729     if (sameURL && newLoadType != FrameLoadTypeReload && newLoadType != FrameLoadTypeReloadFromOrigin && request.resourceRequest().httpMethod() != "POST")
730         m_loadType = FrameLoadTypeSame;
731 }
732
733 SubstituteData FrameLoader::defaultSubstituteDataForURL(const KURL& url)
734 {
735     if (!shouldTreatURLAsSrcdocDocument(url))
736         return SubstituteData();
737     String srcdoc = m_frame->ownerElement()->fastGetAttribute(srcdocAttr);
738     ASSERT(!srcdoc.isNull());
739     CString encodedSrcdoc = srcdoc.utf8();
740     return SubstituteData(SharedBuffer::create(encodedSrcdoc.data(), encodedSrcdoc.length()), "text/html", "UTF-8", KURL());
741 }
742
743 void FrameLoader::reportLocalLoadFailed(LocalFrame* frame, const String& url)
744 {
745     ASSERT(!url.isEmpty());
746     if (!frame)
747         return;
748
749     frame->document()->addConsoleMessage(SecurityMessageSource, ErrorMessageLevel, "Not allowed to load local resource: " + url);
750 }
751
752 static ResourceRequest requestFromHistoryItem(HistoryItem* item, ResourceRequestCachePolicy cachePolicy)
753 {
754     RefPtr<FormData> formData = item->formData();
755     ResourceRequest request(item->url(), item->referrer());
756     request.setCachePolicy(cachePolicy);
757     if (formData) {
758         request.setHTTPMethod("POST");
759         request.setHTTPBody(formData);
760         request.setHTTPContentType(item->formContentType());
761         RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::createFromString(item->referrer().referrer);
762         FrameLoader::addHTTPOriginIfNeeded(request, securityOrigin->toAtomicString());
763     }
764     return request;
765 }
766
767 void FrameLoader::reload(ReloadPolicy reloadPolicy, const KURL& overrideURL, const AtomicString& overrideEncoding)
768 {
769     if (!m_currentItem)
770         return;
771
772     ResourceRequest request = requestFromHistoryItem(m_currentItem.get(), ReloadIgnoringCacheData);
773     if (!overrideURL.isEmpty()) {
774         request.setURL(overrideURL);
775         request.clearHTTPReferrer();
776     }
777
778     FrameLoadType type = reloadPolicy == EndToEndReload ? FrameLoadTypeReloadFromOrigin : FrameLoadTypeReload;
779     loadWithNavigationAction(NavigationAction(request, type), type, nullptr, SubstituteData(), NotClientRedirect, overrideEncoding);
780 }
781
782 void FrameLoader::stopAllLoaders()
783 {
784     if (m_frame->document()->pageDismissalEventBeingDispatched() != Document::NoDismissal)
785         return;
786
787     // If this method is called from within this method, infinite recursion can occur (3442218). Avoid this.
788     if (m_inStopAllLoaders)
789         return;
790
791     // Calling stopLoading() on the provisional document loader can blow away
792     // the frame from underneath.
793     RefPtr<LocalFrame> protect(m_frame);
794
795     m_inStopAllLoaders = true;
796
797     for (RefPtr<LocalFrame> child = m_frame->tree().firstChild(); child; child = child->tree().nextSibling())
798         child->loader().stopAllLoaders();
799     if (m_provisionalDocumentLoader)
800         m_provisionalDocumentLoader->stopLoading();
801     if (m_documentLoader)
802         m_documentLoader->stopLoading();
803
804     if (m_provisionalDocumentLoader)
805         m_provisionalDocumentLoader->detachFromFrame();
806     m_provisionalDocumentLoader = nullptr;
807
808     m_checkTimer.stop();
809
810     m_inStopAllLoaders = false;
811
812     // detachFromParent() can be called multiple times on same LocalFrame, which
813     // means we may no longer have a FrameLoaderClient to talk to.
814     if (m_client)
815         m_client->didStopAllLoaders();
816 }
817
818 void FrameLoader::didAccessInitialDocument()
819 {
820     // We only need to notify the client once, and only for the main frame.
821     if (isLoadingMainFrame() && !m_didAccessInitialDocument) {
822         m_didAccessInitialDocument = true;
823         // Notify asynchronously, since this is called within a JavaScript security check.
824         m_didAccessInitialDocumentTimer.startOneShot(0, FROM_HERE);
825     }
826 }
827
828 void FrameLoader::didAccessInitialDocumentTimerFired(Timer<FrameLoader>*)
829 {
830     m_client->didAccessInitialDocument();
831 }
832
833 void FrameLoader::notifyIfInitialDocumentAccessed()
834 {
835     if (m_didAccessInitialDocumentTimer.isActive()) {
836         m_didAccessInitialDocumentTimer.stop();
837         didAccessInitialDocumentTimerFired(0);
838     }
839 }
840
841 bool FrameLoader::isLoading() const
842 {
843     if (m_provisionalDocumentLoader)
844         return true;
845     return m_documentLoader && m_documentLoader->isLoading();
846 }
847
848 void FrameLoader::commitProvisionalLoad()
849 {
850     ASSERT(m_client->hasWebView());
851     ASSERT(m_state == FrameStateProvisional);
852     RefPtr<DocumentLoader> pdl = m_provisionalDocumentLoader;
853     RefPtr<LocalFrame> protect(m_frame);
854
855     // Check if the destination page is allowed to access the previous page's timing information.
856     if (m_frame->document()) {
857         RefPtr<SecurityOrigin> securityOrigin = SecurityOrigin::create(pdl->request().url());
858         pdl->timing()->setHasSameOriginAsPreviousDocument(securityOrigin->canRequest(m_frame->document()->url()));
859     }
860
861     // The call to closeURL() invokes the unload event handler, which can execute arbitrary
862     // JavaScript. If the script initiates a new load, we need to abandon the current load,
863     // or the two will stomp each other.
864     // detachChildren will similarly trigger child frame unload event handlers.
865     if (m_documentLoader) {
866         m_client->dispatchWillClose();
867         closeURL();
868     }
869     detachChildren();
870     if (pdl != m_provisionalDocumentLoader)
871         return;
872     if (m_documentLoader)
873         m_documentLoader->detachFromFrame();
874     m_documentLoader = m_provisionalDocumentLoader.release();
875     m_state = FrameStateCommittedPage;
876
877     if (isLoadingMainFrame())
878         m_frame->page()->chrome().client().needTouchEvents(false);
879
880     m_client->transitionToCommittedForNewPage();
881     m_frame->navigationScheduler().cancel();
882     m_frame->editor().clearLastEditCommand();
883
884     // If we are still in the process of initializing an empty document then
885     // its frame is not in a consistent state for rendering, so avoid setJSStatusBarText
886     // since it may cause clients to attempt to render the frame.
887     if (!m_stateMachine.creatingInitialEmptyDocument()) {
888         DOMWindow* window = m_frame->domWindow();
889         window->setStatus(String());
890         window->setDefaultStatus(String());
891     }
892     started();
893 }
894
895 bool FrameLoader::isLoadingMainFrame() const
896 {
897     return m_frame->isMainFrame();
898 }
899
900 FrameLoadType FrameLoader::loadType() const
901 {
902     return m_loadType;
903 }
904
905 // This function is an incomprehensible mess and is only used in checkLoadCompleteForThisFrame.
906 // If you're thinking of using it elsewhere, stop right now and reconsider your life.
907 static bool isDocumentDoneLoading(Document* document)
908 {
909     if (!document->loader())
910         return true;
911     if (document->loader()->isLoadingMainResource())
912         return false;
913     if (!document->loadEventFinished()) {
914         if (document->loader()->isLoading() || document->isDelayingLoadEvent())
915             return false;
916     }
917     if (document->fetcher()->requestCount())
918         return false;
919     if (document->processingLoadEvent())
920         return false;
921     if (document->hasActiveParser())
922         return false;
923     return true;
924 }
925
926 bool FrameLoader::checkLoadCompleteForThisFrame()
927 {
928     ASSERT(m_client->hasWebView());
929     RefPtr<LocalFrame> protect(m_frame);
930
931     if (m_state == FrameStateProvisional && m_provisionalDocumentLoader) {
932         const ResourceError& error = m_provisionalDocumentLoader->mainDocumentError();
933         if (error.isNull())
934             return false;
935         RefPtr<DocumentLoader> loader = m_provisionalDocumentLoader;
936         m_client->dispatchDidFailProvisionalLoad(error);
937         if (loader != m_provisionalDocumentLoader)
938             return false;
939         m_provisionalDocumentLoader->detachFromFrame();
940         m_provisionalDocumentLoader = nullptr;
941         m_progressTracker->progressCompleted();
942         m_state = FrameStateComplete;
943         return true;
944     }
945
946     bool allChildrenAreDoneLoading = true;
947     for (LocalFrame* child = m_frame->tree().firstChild(); child; child = child->tree().nextSibling())
948         allChildrenAreDoneLoading &= child->loader().checkLoadCompleteForThisFrame();
949     if (!allChildrenAreDoneLoading)
950         return false;
951
952     if (m_state == FrameStateComplete)
953         return true;
954     if (m_provisionalDocumentLoader || !m_documentLoader)
955         return false;
956     if (!isDocumentDoneLoading(m_frame->document()) && !m_inStopAllLoaders)
957         return false;
958
959     m_state = FrameStateComplete;
960
961     // FIXME: Is this subsequent work important if we already navigated away?
962     // Maybe there are bugs because of that, or extra work we can skip because
963     // the new page is ready.
964
965     // Retry restoring scroll offset since FrameStateComplete disables content
966     // size clamping.
967     restoreScrollPositionAndViewState();
968
969     if (!m_stateMachine.committedFirstRealDocumentLoad())
970         return true;
971
972     m_progressTracker->progressCompleted();
973     m_frame->domWindow()->finishedLoading();
974
975     const ResourceError& error = m_documentLoader->mainDocumentError();
976     if (!error.isNull()) {
977         m_client->dispatchDidFailLoad(error);
978     } else {
979         // Report mobile vs. desktop page statistics. This will only report on Android.
980         if (m_frame->isMainFrame())
981             m_frame->document()->viewportDescription().reportMobilePageStats(m_frame);
982
983         m_client->dispatchDidFinishLoad();
984     }
985     m_loadType = FrameLoadTypeStandard;
986     return true;
987 }
988
989 void FrameLoader::restoreScrollPositionAndViewState()
990 {
991     FrameView* view = m_frame->view();
992     if (!m_frame->page() || !view || !m_currentItem || !m_stateMachine.committedFirstRealDocumentLoad())
993         return;
994
995     if (!needsHistoryItemRestore(m_loadType))
996         return;
997
998     // This tries to balance 1. restoring as soon as possible, 2. detecting
999     // clamping to avoid repeatedly popping the scroll position down as the
1000     // page height increases, 3. ignore clamp detection after load completes
1001     // because that may be because the page will never reach its previous
1002     // height.
1003     float mainFrameScale = m_frame->settings()->pinchVirtualViewportEnabled() ? 1 : m_currentItem->pageScaleFactor();
1004     bool canRestoreWithoutClamping = view->clampOffsetAtScale(m_currentItem->scrollPoint(), mainFrameScale) == m_currentItem->scrollPoint();
1005     bool canRestoreWithoutAnnoyingUser = !view->wasScrolledByUser() && (canRestoreWithoutClamping || m_state == FrameStateComplete);
1006     if (!canRestoreWithoutAnnoyingUser)
1007         return;
1008
1009     if (m_frame->isMainFrame() && m_currentItem->pageScaleFactor()) {
1010         FloatPoint pinchViewportOffset(m_currentItem->pinchViewportScrollPoint());
1011         IntPoint frameScrollOffset(m_currentItem->scrollPoint());
1012
1013         m_frame->page()->setPageScaleFactor(m_currentItem->pageScaleFactor(), frameScrollOffset);
1014
1015         if (m_frame->document()->settings()->pinchVirtualViewportEnabled()) {
1016             // If the pinch viewport's offset is (-1, -1) it means the history item
1017             // is an old version of HistoryItem so distribute the scroll between
1018             // the main frame and the pinch viewport as best as we can.
1019             // FIXME(bokan): This legacy distribution can be removed once the virtual viewport
1020             // pinch path is enabled on all platforms for at least one release.
1021             if (pinchViewportOffset.x() == -1 && pinchViewportOffset.y() == -1)
1022                 pinchViewportOffset = FloatPoint(frameScrollOffset - view->scrollPosition());
1023
1024             m_frame->host()->pinchViewport().setLocation(pinchViewportOffset);
1025         }
1026     } else {
1027         view->setScrollPositionNonProgrammatically(m_currentItem->scrollPoint());
1028     }
1029
1030     if (m_frame->isMainFrame()) {
1031         if (ScrollingCoordinator* scrollingCoordinator = m_frame->page()->scrollingCoordinator())
1032             scrollingCoordinator->frameViewRootLayerDidChange(view);
1033     }
1034 }
1035
1036 void FrameLoader::detachChildren()
1037 {
1038     typedef Vector<RefPtr<LocalFrame> > FrameVector;
1039     FrameVector childrenToDetach;
1040     childrenToDetach.reserveCapacity(m_frame->tree().childCount());
1041     for (LocalFrame* child = m_frame->tree().lastChild(); child; child = child->tree().previousSibling())
1042         childrenToDetach.append(child);
1043     FrameVector::iterator end = childrenToDetach.end();
1044     for (FrameVector::iterator it = childrenToDetach.begin(); it != end; ++it)
1045         (*it)->loader().detachFromParent();
1046 }
1047
1048 void FrameLoader::closeAndRemoveChild(LocalFrame* child)
1049 {
1050     child->setView(nullptr);
1051     if (child->ownerElement() && child->page())
1052         child->page()->decrementSubframeCount();
1053     child->willDetachFrameHost();
1054     child->loader().detachClient();
1055 }
1056
1057 // Called every time a resource is completely loaded or an error is received.
1058 void FrameLoader::checkLoadComplete()
1059 {
1060     ASSERT(m_client->hasWebView());
1061     if (Page* page = m_frame->page())
1062         page->mainFrame()->loader().checkLoadCompleteForThisFrame();
1063 }
1064
1065 String FrameLoader::userAgent(const KURL& url) const
1066 {
1067     String userAgent = m_client->userAgent(url);
1068     InspectorInstrumentation::applyUserAgentOverride(m_frame, &userAgent);
1069     return userAgent;
1070 }
1071
1072 void FrameLoader::frameDetached()
1073 {
1074     // stopAllLoaders can detach the LocalFrame, so protect it.
1075     RefPtr<LocalFrame> protect(m_frame);
1076     stopAllLoaders();
1077     detachFromParent();
1078 }
1079
1080 void FrameLoader::detachFromParent()
1081 {
1082     // stopAllLoaders can detach the LocalFrame, so protect it.
1083     RefPtr<LocalFrame> protect(m_frame);
1084
1085     closeURL();
1086     detachChildren();
1087     // stopAllLoaders() needs to be called after detachChildren(), because detachedChildren()
1088     // will trigger the unload event handlers of any child frames, and those event
1089     // handlers might start a new subresource load in this frame.
1090     stopAllLoaders();
1091
1092     InspectorInstrumentation::frameDetachedFromParent(m_frame);
1093
1094     if (m_documentLoader)
1095         m_documentLoader->detachFromFrame();
1096     m_documentLoader = nullptr;
1097
1098     if (!m_client)
1099         return;
1100
1101     // FIXME: All this code belongs up in Page.
1102     if (LocalFrame* parent = m_frame->tree().parent()) {
1103         parent->loader().closeAndRemoveChild(m_frame);
1104         parent->loader().scheduleCheckCompleted();
1105     } else {
1106         m_frame->setView(nullptr);
1107         m_frame->willDetachFrameHost();
1108         detachClient();
1109     }
1110     m_frame->detachFromFrameHost();
1111
1112 }
1113
1114 void FrameLoader::detachClient()
1115 {
1116     ASSERT(m_client);
1117
1118     // Finish all cleanup work that might require talking to the embedder.
1119     m_progressTracker.clear();
1120     setOpener(0);
1121     // Notify ScriptController that the frame is closing, since its cleanup ends up calling
1122     // back to FrameLoaderClient via V8WindowShell.
1123     m_frame->script().clearForClose();
1124
1125     // m_client should never be null because that means we somehow re-entered
1126     // the frame detach code... but it is sometimes.
1127     // FIXME: Understand why this is happening so we can document this insanity.
1128     if (m_client) {
1129         // After this, we must no longer talk to the client since this clears
1130         // its owning reference back to our owning LocalFrame.
1131         m_client->detachedFromParent();
1132         m_client = 0;
1133     }
1134 }
1135
1136 void FrameLoader::addHTTPOriginIfNeeded(ResourceRequest& request, const AtomicString& origin)
1137 {
1138     if (!request.httpOrigin().isEmpty())
1139         return;  // Request already has an Origin header.
1140
1141     // Don't send an Origin header for GET or HEAD to avoid privacy issues.
1142     // For example, if an intranet page has a hyperlink to an external web
1143     // site, we don't want to include the Origin of the request because it
1144     // will leak the internal host name. Similar privacy concerns have lead
1145     // to the widespread suppression of the Referer header at the network
1146     // layer.
1147     if (request.httpMethod() == "GET" || request.httpMethod() == "HEAD")
1148         return;
1149
1150     // For non-GET and non-HEAD methods, always send an Origin header so the
1151     // server knows we support this feature.
1152
1153     if (origin.isEmpty()) {
1154         // If we don't know what origin header to attach, we attach the value
1155         // for an empty origin.
1156         request.setHTTPOrigin(SecurityOrigin::createUnique()->toAtomicString());
1157         return;
1158     }
1159
1160     request.setHTTPOrigin(origin);
1161 }
1162
1163 void FrameLoader::receivedMainResourceError(const ResourceError& error)
1164 {
1165     // Retain because the stop may release the last reference to it.
1166     RefPtr<LocalFrame> protect(m_frame);
1167
1168     if (m_frame->document()->parser())
1169         m_frame->document()->parser()->stopParsing();
1170
1171     // FIXME: We really ought to be able to just check for isCancellation() here, but there are some
1172     // ResourceErrors that setIsCancellation() but aren't created by ResourceError::cancelledError().
1173     ResourceError c(ResourceError::cancelledError(KURL()));
1174     if ((error.errorCode() != c.errorCode() || error.domain() != c.domain()) && m_frame->ownerElement())
1175         m_frame->ownerElement()->renderFallbackContent();
1176
1177     checkCompleted();
1178     if (m_frame->page())
1179         checkLoadComplete();
1180 }
1181
1182 bool FrameLoader::shouldPerformFragmentNavigation(bool isFormSubmission, const String& httpMethod, FrameLoadType loadType, const KURL& url)
1183 {
1184     ASSERT(loadType != FrameLoadTypeReloadFromOrigin);
1185     // We don't do this if we are submitting a form with method other than "GET", explicitly reloading,
1186     // currently displaying a frameset, or if the URL does not have a fragment.
1187     return (!isFormSubmission || equalIgnoringCase(httpMethod, "GET"))
1188         && loadType != FrameLoadTypeReload
1189         && loadType != FrameLoadTypeSame
1190         && loadType != FrameLoadTypeBackForward
1191         && url.hasFragmentIdentifier()
1192         && equalIgnoringFragmentIdentifier(m_frame->document()->url(), url)
1193         // We don't want to just scroll if a link from within a
1194         // frameset is trying to reload the frameset into _top.
1195         && !m_frame->document()->isFrameSet();
1196 }
1197
1198 void FrameLoader::scrollToFragmentWithParentBoundary(const KURL& url)
1199 {
1200     FrameView* view = m_frame->view();
1201     if (!view)
1202         return;
1203
1204     // Leaking scroll position to a cross-origin ancestor would permit the so-called "framesniffing" attack.
1205     RefPtr<LocalFrame> boundaryFrame(url.hasFragmentIdentifier() ? m_frame->document()->findUnsafeParentScrollPropagationBoundary() : 0);
1206
1207     if (boundaryFrame)
1208         boundaryFrame->view()->setSafeToPropagateScrollToParent(false);
1209
1210     view->scrollToFragment(url);
1211
1212     if (boundaryFrame)
1213         boundaryFrame->view()->setSafeToPropagateScrollToParent(true);
1214 }
1215
1216 bool FrameLoader::shouldClose()
1217 {
1218     Page* page = m_frame->page();
1219     if (!page || !page->chrome().canRunBeforeUnloadConfirmPanel())
1220         return true;
1221
1222     // Store all references to each subframe in advance since beforeunload's event handler may modify frame
1223     Vector<RefPtr<LocalFrame> > targetFrames;
1224     targetFrames.append(m_frame);
1225     for (LocalFrame* child = m_frame->tree().firstChild(); child; child = child->tree().traverseNext(m_frame))
1226         targetFrames.append(child);
1227
1228     bool shouldClose = false;
1229     {
1230         NavigationDisablerForBeforeUnload navigationDisabler;
1231         size_t i;
1232
1233         bool didAllowNavigation = false;
1234         for (i = 0; i < targetFrames.size(); i++) {
1235             if (!targetFrames[i]->tree().isDescendantOf(m_frame))
1236                 continue;
1237             if (!targetFrames[i]->document()->dispatchBeforeUnloadEvent(page->chrome(), didAllowNavigation))
1238                 break;
1239         }
1240
1241         if (i == targetFrames.size())
1242             shouldClose = true;
1243     }
1244     return shouldClose;
1245 }
1246
1247 void FrameLoader::loadWithNavigationAction(const NavigationAction& action, FrameLoadType type, PassRefPtrWillBeRawPtr<FormState> formState, const SubstituteData& substituteData, ClientRedirectPolicy clientRedirect, const AtomicString& overrideEncoding)
1248 {
1249     ASSERT(m_client->hasWebView());
1250     if (m_frame->document()->pageDismissalEventBeingDispatched() != Document::NoDismissal)
1251         return;
1252
1253     const ResourceRequest& request = action.resourceRequest();
1254
1255     // The current load should replace the history item if it is the first real
1256     // load of the frame.
1257     bool replacesCurrentHistoryItem = false;
1258     if (type == FrameLoadTypeRedirectWithLockedBackForwardList
1259         || !m_stateMachine.committedFirstRealDocumentLoad()) {
1260         replacesCurrentHistoryItem = true;
1261     }
1262
1263     m_policyDocumentLoader = m_client->createDocumentLoader(m_frame, request, substituteData.isValid() ? substituteData : defaultSubstituteDataForURL(request.url()));
1264     m_policyDocumentLoader->setTriggeringAction(action);
1265     m_policyDocumentLoader->setReplacesCurrentHistoryItem(replacesCurrentHistoryItem);
1266     m_policyDocumentLoader->setIsClientRedirect(clientRedirect == ClientRedirect);
1267
1268     if (LocalFrame* parent = m_frame->tree().parent())
1269         m_policyDocumentLoader->setOverrideEncoding(parent->loader().documentLoader()->overrideEncoding());
1270     else if (!overrideEncoding.isEmpty())
1271         m_policyDocumentLoader->setOverrideEncoding(overrideEncoding);
1272     else if (m_documentLoader)
1273         m_policyDocumentLoader->setOverrideEncoding(m_documentLoader->overrideEncoding());
1274
1275     // stopAllLoaders can detach the LocalFrame, so protect it.
1276     RefPtr<LocalFrame> protect(m_frame);
1277     if ((!m_policyDocumentLoader->shouldContinueForNavigationPolicy(request) || !shouldClose()) && m_policyDocumentLoader) {
1278         m_policyDocumentLoader->detachFromFrame();
1279         m_policyDocumentLoader = nullptr;
1280         return;
1281     }
1282
1283     // A new navigation is in progress, so don't clear the history's provisional item.
1284     stopAllLoaders();
1285
1286     // <rdar://problem/6250856> - In certain circumstances on pages with multiple frames, stopAllLoaders()
1287     // might detach the current FrameLoader, in which case we should bail on this newly defunct load.
1288     if (!m_frame->page() || !m_policyDocumentLoader)
1289         return;
1290
1291     if (isLoadingMainFrame())
1292         m_frame->page()->inspectorController().resume();
1293     m_frame->navigationScheduler().cancel();
1294
1295     m_provisionalDocumentLoader = m_policyDocumentLoader.release();
1296     m_loadType = type;
1297     m_state = FrameStateProvisional;
1298
1299     if (formState)
1300         m_client->dispatchWillSubmitForm(formState->form());
1301
1302     m_progressTracker->progressStarted();
1303     if (m_provisionalDocumentLoader->isClientRedirect())
1304         m_provisionalDocumentLoader->appendRedirect(m_frame->document()->url());
1305     m_provisionalDocumentLoader->appendRedirect(m_provisionalDocumentLoader->request().url());
1306     m_client->dispatchDidStartProvisionalLoad();
1307     ASSERT(m_provisionalDocumentLoader);
1308     m_provisionalDocumentLoader->startLoadingMainResource();
1309 }
1310
1311 void FrameLoader::applyUserAgent(ResourceRequest& request)
1312 {
1313     String userAgent = this->userAgent(request.url());
1314     ASSERT(!userAgent.isNull());
1315     request.setHTTPUserAgent(AtomicString(userAgent));
1316 }
1317
1318 bool FrameLoader::shouldInterruptLoadForXFrameOptions(const String& content, const KURL& url, unsigned long requestIdentifier)
1319 {
1320     UseCounter::count(m_frame->domWindow()->document(), UseCounter::XFrameOptions);
1321
1322     LocalFrame* topFrame = m_frame->tree().top();
1323     if (m_frame == topFrame)
1324         return false;
1325
1326     XFrameOptionsDisposition disposition = parseXFrameOptionsHeader(content);
1327
1328     switch (disposition) {
1329     case XFrameOptionsSameOrigin: {
1330         UseCounter::count(m_frame->domWindow()->document(), UseCounter::XFrameOptionsSameOrigin);
1331         RefPtr<SecurityOrigin> origin = SecurityOrigin::create(url);
1332         if (!origin->isSameSchemeHostPort(topFrame->document()->securityOrigin()))
1333             return true;
1334         for (LocalFrame* frame = m_frame->tree().parent(); frame; frame = frame->tree().parent()) {
1335             if (!origin->isSameSchemeHostPort(frame->document()->securityOrigin())) {
1336                 UseCounter::count(m_frame->domWindow()->document(), UseCounter::XFrameOptionsSameOriginWithBadAncestorChain);
1337                 break;
1338             }
1339         }
1340         return false;
1341     }
1342     case XFrameOptionsDeny:
1343         return true;
1344     case XFrameOptionsAllowAll:
1345         return false;
1346     case XFrameOptionsConflict:
1347         m_frame->document()->addConsoleMessageWithRequestIdentifier(JSMessageSource, ErrorMessageLevel, "Multiple 'X-Frame-Options' headers with conflicting values ('" + content + "') encountered when loading '" + url.elidedString() + "'. Falling back to 'DENY'.", requestIdentifier);
1348         return true;
1349     case XFrameOptionsInvalid:
1350         m_frame->document()->addConsoleMessageWithRequestIdentifier(JSMessageSource, ErrorMessageLevel, "Invalid 'X-Frame-Options' header encountered when loading '" + url.elidedString() + "': '" + content + "' is not a recognized directive. The header will be ignored.", requestIdentifier);
1351         return false;
1352     default:
1353         ASSERT_NOT_REACHED();
1354         return false;
1355     }
1356 }
1357
1358 bool FrameLoader::shouldTreatURLAsSameAsCurrent(const KURL& url) const
1359 {
1360     return m_currentItem && url == m_currentItem->url();
1361 }
1362
1363 bool FrameLoader::shouldTreatURLAsSrcdocDocument(const KURL& url) const
1364 {
1365     if (!equalIgnoringCase(url.string(), "about:srcdoc"))
1366         return false;
1367     HTMLFrameOwnerElement* ownerElement = m_frame->ownerElement();
1368     if (!isHTMLIFrameElement(ownerElement))
1369         return false;
1370     return ownerElement->fastHasAttribute(srcdocAttr);
1371 }
1372
1373 LocalFrame* FrameLoader::findFrameForNavigation(const AtomicString& name, Document* activeDocument)
1374 {
1375     ASSERT(activeDocument);
1376     LocalFrame* frame = m_frame->tree().find(name);
1377     if (!activeDocument->canNavigate(frame))
1378         return 0;
1379     return frame;
1380 }
1381
1382 void FrameLoader::loadHistoryItem(HistoryItem* item, HistoryLoadType historyLoadType, ResourceRequestCachePolicy cachePolicy)
1383 {
1384     RefPtr<LocalFrame> protect(m_frame);
1385     if (m_frame->page()->defersLoading()) {
1386         m_deferredHistoryLoad = DeferredHistoryLoad(item, historyLoadType, cachePolicy);
1387         return;
1388     }
1389
1390     m_provisionalItem = item;
1391     if (historyLoadType == HistorySameDocumentLoad) {
1392         m_loadType = FrameLoadTypeBackForward;
1393         loadInSameDocument(item->url(), item->stateObject(), DoNotUpdateBackForwardList, NotClientRedirect);
1394         restoreScrollPositionAndViewState();
1395         return;
1396     }
1397     loadWithNavigationAction(NavigationAction(requestFromHistoryItem(item, cachePolicy), FrameLoadTypeBackForward), FrameLoadTypeBackForward, nullptr, SubstituteData());
1398 }
1399
1400 void FrameLoader::dispatchDocumentElementAvailable()
1401 {
1402     m_client->documentElementAvailable();
1403 }
1404
1405 void FrameLoader::dispatchDidClearDocumentOfWindowObject()
1406 {
1407     if (!m_frame->script().canExecuteScripts(NotAboutToExecuteScript))
1408         return;
1409
1410     if (Page* page = m_frame->page())
1411         page->inspectorController().didClearDocumentOfWindowObject(m_frame);
1412     InspectorInstrumentation::didClearDocumentOfWindowObject(m_frame);
1413
1414     // We just cleared the document, not the entire window object, but for the
1415     // embedder that's close enough.
1416     m_client->dispatchDidClearWindowObjectInMainWorld();
1417 }
1418
1419 void FrameLoader::dispatchDidClearWindowObjectInMainWorld()
1420 {
1421     if (!m_frame->script().canExecuteScripts(NotAboutToExecuteScript))
1422         return;
1423
1424     // FIXME: Why isn't the inspector notified of this?
1425
1426     m_client->dispatchDidClearWindowObjectInMainWorld();
1427 }
1428
1429 SandboxFlags FrameLoader::effectiveSandboxFlags() const
1430 {
1431     SandboxFlags flags = m_forcedSandboxFlags;
1432     if (LocalFrame* parentFrame = m_frame->tree().parent())
1433         flags |= parentFrame->document()->sandboxFlags();
1434     if (HTMLFrameOwnerElement* ownerElement = m_frame->ownerElement())
1435         flags |= ownerElement->sandboxFlags();
1436     return flags;
1437 }
1438
1439 } // namespace WebCore