2 * Copyright (C) 2006, 2007, 2008, 2009 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/)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "HistoryController.h"
34 #include "BackForwardController.h"
35 #include "CachedPage.h"
37 #include "DocumentLoader.h"
38 #include "FrameLoader.h"
39 #include "FrameLoaderClient.h"
40 #include "FrameLoaderStateMachine.h"
41 #include "FrameTree.h"
42 #include "FrameView.h"
43 #include "HistoryItem.h"
46 #include "MainFrame.h"
48 #include "PageCache.h"
49 #include "PageGroup.h"
50 #include "ScrollingCoordinator.h"
51 #include "VisitedLinkStore.h"
52 #include <wtf/text/CString.h>
56 static inline void addVisitedLink(Page& page, const URL& url)
58 page.visitedLinkStore().addVisitedLink(page, visitedLinkHash(url.string()));
61 HistoryController::HistoryController(Frame& frame)
63 , m_frameLoadComplete(true)
64 , m_defersLoading(false)
68 HistoryController::~HistoryController()
72 void HistoryController::saveScrollPositionAndViewStateToItem(HistoryItem* item)
74 FrameView* frameView = m_frame.view();
75 if (!item || !frameView)
78 if (m_frame.document()->inPageCache())
79 item->setScrollPosition(frameView->cachedScrollPosition());
81 item->setScrollPosition(frameView->scrollPosition());
83 item->setExposedContentRect(frameView->exposedContentRect());
84 item->setUnobscuredContentRect(frameView->unobscuredContentRect());
87 Page* page = m_frame.page();
88 if (page && m_frame.isMainFrame())
89 item->setPageScaleFactor(page->pageScaleFactor() / page->viewScaleFactor());
91 // FIXME: It would be great to work out a way to put this code in WebCore instead of calling through to the client.
92 m_frame.loader().client().saveViewStateToItem(item);
94 // Notify clients that the HistoryItem has changed.
95 item->notifyChanged();
98 void HistoryController::clearScrollPositionAndViewState()
103 m_currentItem->clearScrollPosition();
104 m_currentItem->setPageScaleFactor(0);
108 There is a race condition between the layout and load completion that affects restoring the scroll position.
109 We try to restore the scroll position at both the first layout and upon load completion.
111 1) If first layout happens before the load completes, we want to restore the scroll position then so that the
112 first time we draw the page is already scrolled to the right place, instead of starting at the top and later
113 jumping down. It is possible that the old scroll position is past the part of the doc laid out so far, in
114 which case the restore silent fails and we will fix it in when we try to restore on doc completion.
115 2) If the layout happens after the load completes, the attempt to restore at load completion time silently
116 fails. We then successfully restore it when the layout happens.
118 void HistoryController::restoreScrollPositionAndViewState()
120 if (!m_frame.loader().stateMachine().committedFirstRealDocumentLoad())
123 ASSERT(m_currentItem);
125 // FIXME: As the ASSERT attests, it seems we should always have a currentItem here.
126 // One counterexample is <rdar://problem/4917290>
127 // For now, to cover this issue in release builds, there is no technical harm to returning
128 // early and from a user standpoint - as in the above radar - the previous page load failed
129 // so there *is* no scroll or view state to restore!
133 FrameView* view = m_frame.view();
135 // FIXME: There is some scrolling related work that needs to happen whenever a page goes into the
136 // page cache and similar work that needs to occur when it comes out. This is where we do the work
137 // that needs to happen when we exit, and the work that needs to happen when we enter is in
138 // Document::setIsInPageCache(bool). It would be nice if there was more symmetry in these spots.
139 // https://bugs.webkit.org/show_bug.cgi?id=98698
141 Page* page = m_frame.page();
142 if (page && m_frame.isMainFrame()) {
143 if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
144 scrollingCoordinator->frameViewRootLayerDidChange(*view);
148 // FIXME: It would be great to work out a way to put this code in WebCore instead of calling
149 // through to the client.
150 m_frame.loader().client().restoreViewState();
152 #if !PLATFORM(IOS) && !PLATFORM(EFL)
153 // Don't restore scroll point on iOS as FrameLoaderClient::restoreViewState() does that.
154 if (view && !view->wasScrolledByUser()) {
155 Page* page = m_frame.page();
156 auto desiredScrollPosition = m_currentItem->scrollPosition();
158 if (page && m_frame.isMainFrame() && m_currentItem->pageScaleFactor())
159 page->setPageScaleFactor(m_currentItem->pageScaleFactor() * page->viewScaleFactor(), desiredScrollPosition);
161 view->setScrollPosition(desiredScrollPosition);
163 // If the scroll position doesn't have to be clamped, consider it successfully restored.
164 if (m_frame.isMainFrame()) {
165 auto adjustedDesiredScrollPosition = view->adjustScrollPositionWithinRange(desiredScrollPosition);
166 if (desiredScrollPosition == adjustedDesiredScrollPosition)
167 m_frame.loader().client().didRestoreScrollPosition();
174 void HistoryController::updateBackForwardListForFragmentScroll()
176 updateBackForwardListClippedAtTarget(false);
179 void HistoryController::saveDocumentState()
181 // FIXME: Reading this bit of FrameLoader state here is unfortunate. I need to study
182 // this more to see if we can remove this dependency.
183 if (m_frame.loader().stateMachine().creatingInitialEmptyDocument())
186 // For a standard page load, we will have a previous item set, which will be used to
187 // store the form state. However, in some cases we will have no previous item, and
188 // the current item is the right place to save the state. One example is when we
189 // detach a bunch of frames because we are navigating from a site with frames to
190 // another site. Another is when saving the frame state of a frame that is not the
191 // target of the current navigation (if we even decide to save with that granularity).
193 // Because of previousItem's "masking" of currentItem for this purpose, it's important
194 // that we keep track of the end of a page transition with m_frameLoadComplete. We
195 // leverage the checkLoadComplete recursion to achieve this goal.
197 HistoryItem* item = m_frameLoadComplete ? m_currentItem.get() : m_previousItem.get();
201 ASSERT(m_frame.document());
202 Document& document = *m_frame.document();
203 if (item->isCurrentDocument(document) && document.hasLivingRenderTree()) {
204 if (DocumentLoader* documentLoader = document.loader())
205 item->setShouldOpenExternalURLsPolicy(documentLoader->shouldOpenExternalURLsPolicyToPropagate());
207 LOG(Loading, "WebCoreLoading %s: saving form state to %p", m_frame.tree().uniqueName().string().utf8().data(), item);
208 item->setDocumentState(document.formElementsState());
212 // Walk the frame tree, telling all frames to save their form state into their current
214 void HistoryController::saveDocumentAndScrollState()
216 for (Frame* frame = &m_frame; frame; frame = frame->tree().traverseNext(&m_frame)) {
217 frame->loader().history().saveDocumentState();
218 frame->loader().history().saveScrollPositionAndViewStateToItem(frame->loader().history().currentItem());
222 void HistoryController::restoreDocumentState()
224 switch (m_frame.loader().loadType()) {
225 case FrameLoadType::Reload:
226 case FrameLoadType::ReloadFromOrigin:
227 case FrameLoadType::Same:
228 case FrameLoadType::Replace:
229 // Not restoring the document state.
231 case FrameLoadType::Back:
232 case FrameLoadType::Forward:
233 case FrameLoadType::IndexedBackForward:
234 case FrameLoadType::RedirectWithLockedBackForwardList:
235 case FrameLoadType::Standard:
241 if (m_frame.loader().requestedHistoryItem() != m_currentItem.get())
243 if (m_frame.loader().documentLoader()->isClientRedirect())
246 m_frame.loader().documentLoader()->setShouldOpenExternalURLsPolicy(m_currentItem->shouldOpenExternalURLsPolicy());
248 LOG(Loading, "WebCoreLoading %s: restoring form state from %p", m_frame.tree().uniqueName().string().utf8().data(), m_currentItem.get());
249 m_frame.document()->setStateForNewFormElements(m_currentItem->documentState());
252 void HistoryController::invalidateCurrentItemCachedPage()
257 // When we are pre-commit, the currentItem is where any page cache data resides.
258 std::unique_ptr<CachedPage> cachedPage = PageCache::singleton().take(*currentItem(), m_frame.page());
262 // FIXME: This is a grotesque hack to fix <rdar://problem/4059059> Crash in RenderFlow::detach
263 // Somehow the PageState object is not properly updated, and is holding onto a stale document.
264 // Both Xcode and FileMaker see this crash, Safari does not.
266 ASSERT(cachedPage->document() == m_frame.document());
267 if (cachedPage->document() == m_frame.document()) {
268 cachedPage->document()->setInPageCache(false);
273 bool HistoryController::shouldStopLoadingForHistoryItem(HistoryItem& targetItem) const
278 // Don't abort the current load if we're navigating within the current document.
279 if (m_currentItem->shouldDoSameDocumentNavigationTo(targetItem))
285 // Main funnel for navigating to a previous location (back/forward, non-search snap-back)
286 // This includes recursion to handle loading into framesets properly
287 void HistoryController::goToItem(HistoryItem& targetItem, FrameLoadType type)
289 ASSERT(!m_frame.tree().parent());
291 // shouldGoToHistoryItem is a private delegate method. This is needed to fix:
292 // <rdar://problem/3951283> can view pages from the back/forward cache that should be disallowed by Parental Controls
293 // Ultimately, history item navigations should go through the policy delegate. That's covered in:
294 // <rdar://problem/3979539> back/forward cache navigations should consult policy delegate
295 Page* page = m_frame.page();
298 if (!m_frame.loader().client().shouldGoToHistoryItem(&targetItem))
300 if (m_defersLoading) {
301 m_deferredItem = &targetItem;
302 m_deferredFrameLoadType = type;
306 // Set the BF cursor before commit, which lets the user quickly click back/forward again.
307 // - plus, it only makes sense for the top level of the operation through the frame tree,
308 // as opposed to happening for some/one of the page commits that might happen soon
309 RefPtr<HistoryItem> currentItem = page->backForward().currentItem();
310 page->backForward().setCurrentItem(&targetItem);
311 m_frame.loader().client().updateGlobalHistoryItemForPage();
313 // First set the provisional item of any frames that are not actually navigating.
314 // This must be done before trying to navigate the desired frame, because some
315 // navigations can commit immediately (such as about:blank). We must be sure that
316 // all frames have provisional items set before the commit.
317 recursiveSetProvisionalItem(targetItem, currentItem.get());
319 // Now that all other frames have provisional items, do the actual navigation.
320 recursiveGoToItem(targetItem, currentItem.get(), type);
323 void HistoryController::setDefersLoading(bool defer)
325 m_defersLoading = defer;
326 if (!defer && m_deferredItem) {
327 goToItem(*m_deferredItem, m_deferredFrameLoadType);
328 m_deferredItem = nullptr;
332 void HistoryController::updateForBackForwardNavigation()
334 LOG(History, "HistoryController %p updateForBackForwardNavigation: Updating History for back/forward navigation in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader() ? m_frame.loader().documentLoader()->url().string().utf8().data() : "");
336 // Must grab the current scroll position before disturbing it
337 if (!m_frameLoadComplete)
338 saveScrollPositionAndViewStateToItem(m_previousItem.get());
340 // When traversing history, we may end up redirecting to a different URL
341 // this time (e.g., due to cookies). See http://webkit.org/b/49654.
345 void HistoryController::updateForReload()
347 LOG(History, "HistoryController %p updateForBackForwardNavigation: Updating History for reload in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader() ? m_frame.loader().documentLoader()->url().string().utf8().data() : "");
350 PageCache::singleton().remove(*m_currentItem);
352 if (m_frame.loader().loadType() == FrameLoadType::Reload || m_frame.loader().loadType() == FrameLoadType::ReloadFromOrigin)
353 saveScrollPositionAndViewStateToItem(m_currentItem.get());
355 // Rebuild the history item tree when reloading as trying to re-associate everything is too error-prone.
356 m_currentItem->clearChildren();
359 // When reloading the page, we may end up redirecting to a different URL
360 // this time (e.g., due to cookies). See http://webkit.org/b/4072.
364 // There are 3 things you might think of as "history", all of which are handled by these functions.
366 // 1) Back/forward: The m_currentItem is part of this mechanism.
367 // 2) Global history: Handled by the client.
368 // 3) Visited links: Handled by the PageGroup.
370 void HistoryController::updateForStandardLoad(HistoryUpdateType updateType)
372 LOG(History, "HistoryController %p updateForStandardLoad: Updating History for standard load in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader()->url().string().ascii().data());
374 FrameLoader& frameLoader = m_frame.loader();
376 bool needPrivacy = m_frame.page()->usesEphemeralSession();
377 const URL& historyURL = frameLoader.documentLoader()->urlForHistory();
379 if (!frameLoader.documentLoader()->isClientRedirect()) {
380 if (!historyURL.isEmpty()) {
381 if (updateType != UpdateAllExceptBackForwardList)
382 updateBackForwardListClippedAtTarget(true);
384 frameLoader.client().updateGlobalHistory();
385 frameLoader.documentLoader()->setDidCreateGlobalHistoryEntry(true);
386 if (frameLoader.documentLoader()->unreachableURL().isEmpty())
387 frameLoader.client().updateGlobalHistoryRedirectLinks();
390 m_frame.loader().client().updateGlobalHistoryItemForPage();
393 // The client redirect replaces the current history item.
397 if (!historyURL.isEmpty() && !needPrivacy) {
398 if (Page* page = m_frame.page())
399 addVisitedLink(*page, historyURL);
401 if (!frameLoader.documentLoader()->didCreateGlobalHistoryEntry() && frameLoader.documentLoader()->unreachableURL().isEmpty() && !m_frame.document()->url().isEmpty())
402 frameLoader.client().updateGlobalHistoryRedirectLinks();
406 void HistoryController::updateForRedirectWithLockedBackForwardList()
408 LOG(History, "HistoryController %p updateForRedirectWithLockedBackForwardList: Updating History for redirect load in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader() ? m_frame.loader().documentLoader()->url().string().utf8().data() : "");
410 bool needPrivacy = m_frame.page()->usesEphemeralSession();
411 const URL& historyURL = m_frame.loader().documentLoader()->urlForHistory();
413 if (m_frame.loader().documentLoader()->isClientRedirect()) {
414 if (!m_currentItem && !m_frame.tree().parent()) {
415 if (!historyURL.isEmpty()) {
416 updateBackForwardListClippedAtTarget(true);
418 m_frame.loader().client().updateGlobalHistory();
419 m_frame.loader().documentLoader()->setDidCreateGlobalHistoryEntry(true);
420 if (m_frame.loader().documentLoader()->unreachableURL().isEmpty())
421 m_frame.loader().client().updateGlobalHistoryRedirectLinks();
424 m_frame.loader().client().updateGlobalHistoryItemForPage();
427 // The client redirect replaces the current history item.
430 Frame* parentFrame = m_frame.tree().parent();
431 if (parentFrame && parentFrame->loader().history().currentItem())
432 parentFrame->loader().history().currentItem()->setChildItem(createItem());
435 if (!historyURL.isEmpty() && !needPrivacy) {
436 if (Page* page = m_frame.page())
437 addVisitedLink(*page, historyURL);
439 if (!m_frame.loader().documentLoader()->didCreateGlobalHistoryEntry() && m_frame.loader().documentLoader()->unreachableURL().isEmpty() && !m_frame.document()->url().isEmpty())
440 m_frame.loader().client().updateGlobalHistoryRedirectLinks();
444 void HistoryController::updateForClientRedirect()
446 LOG(History, "HistoryController %p updateForClientRedirect: Updating History for client redirect in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader() ? m_frame.loader().documentLoader()->url().string().utf8().data() : "");
448 // Clear out form data so we don't try to restore it into the incoming page. Must happen after
449 // webcore has closed the URL and saved away the form state.
451 m_currentItem->clearDocumentState();
452 m_currentItem->clearScrollPosition();
455 bool needPrivacy = m_frame.page()->usesEphemeralSession();
456 const URL& historyURL = m_frame.loader().documentLoader()->urlForHistory();
458 if (!historyURL.isEmpty() && !needPrivacy) {
459 if (Page* page = m_frame.page())
460 addVisitedLink(*page, historyURL);
464 void HistoryController::updateForCommit()
466 FrameLoader& frameLoader = m_frame.loader();
467 LOG(History, "HistoryController %p updateForCommit: Updating History for commit in frame %p (main frame %d) %s", this, &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader() ? m_frame.loader().documentLoader()->url().string().utf8().data() : "");
469 FrameLoadType type = frameLoader.loadType();
470 if (isBackForwardLoadType(type)
471 || isReplaceLoadTypeWithProvisionalItem(type)
472 || (isReloadTypeWithProvisionalItem(type) && !frameLoader.provisionalDocumentLoader()->unreachableURL().isEmpty())) {
473 // Once committed, we want to use current item for saving DocState, and
474 // the provisional item for restoring state.
475 // Note previousItem must be set before we close the URL, which will
476 // happen when the data source is made non-provisional below
478 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=146842
479 // We should always have a provisional item when committing, but we sometimes don't.
480 // Not having one leads to us not having a m_currentItem later, which is also a terrible known issue.
481 // We should get to the bottom of this.
482 ASSERT(m_provisionalItem);
483 setCurrentItem(m_provisionalItem.get());
484 m_provisionalItem = nullptr;
486 // Tell all other frames in the tree to commit their provisional items and
487 // restore their scroll position. We'll avoid this frame (which has already
488 // committed) and its children (which will be replaced).
489 m_frame.mainFrame().loader().history().recursiveUpdateForCommit();
493 bool HistoryController::isReplaceLoadTypeWithProvisionalItem(FrameLoadType type)
495 // Going back to an error page in a subframe can trigger a FrameLoadType::Replace
496 // while m_provisionalItem is set, so we need to commit it.
497 return type == FrameLoadType::Replace && m_provisionalItem;
500 bool HistoryController::isReloadTypeWithProvisionalItem(FrameLoadType type)
502 return (type == FrameLoadType::Reload || type == FrameLoadType::ReloadFromOrigin) && m_provisionalItem;
505 void HistoryController::recursiveUpdateForCommit()
507 // The frame that navigated will now have a null provisional item.
508 // Ignore it and its children.
509 if (!m_provisionalItem)
512 // For each frame that already had the content the item requested (based on
513 // (a matching URL and frame tree snapshot), just restore the scroll position.
514 // Save form state (works from currentItem, since m_frameLoadComplete is true)
515 if (m_currentItem && itemsAreClones(*m_currentItem, m_provisionalItem.get())) {
516 ASSERT(m_frameLoadComplete);
518 saveScrollPositionAndViewStateToItem(m_currentItem.get());
520 if (FrameView* view = m_frame.view())
521 view->setWasScrolledByUser(false);
523 // Now commit the provisional item
524 setCurrentItem(m_provisionalItem.get());
525 m_provisionalItem = nullptr;
527 // Restore form state (works from currentItem)
528 restoreDocumentState();
530 // Restore the scroll position (we choose to do this rather than going back to the anchor point)
531 restoreScrollPositionAndViewState();
534 // Iterate over the rest of the tree
535 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
536 child->loader().history().recursiveUpdateForCommit();
539 void HistoryController::updateForSameDocumentNavigation()
541 if (m_frame.document()->url().isEmpty())
544 if (m_frame.page()->usesEphemeralSession())
547 Page* page = m_frame.page();
551 addVisitedLink(*page, m_frame.document()->url());
552 m_frame.mainFrame().loader().history().recursiveUpdateForSameDocumentNavigation();
555 m_currentItem->setURL(m_frame.document()->url());
556 m_frame.loader().client().updateGlobalHistory();
560 void HistoryController::recursiveUpdateForSameDocumentNavigation()
562 // The frame that navigated will now have a null provisional item.
563 // Ignore it and its children.
564 if (!m_provisionalItem)
567 // The provisional item may represent a different pending navigation.
568 // Don't commit it if it isn't a same document navigation.
569 if (m_currentItem && !m_currentItem->shouldDoSameDocumentNavigationTo(*m_provisionalItem))
572 // Commit the provisional item.
573 setCurrentItem(m_provisionalItem.get());
574 m_provisionalItem = nullptr;
576 // Iterate over the rest of the tree.
577 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
578 child->loader().history().recursiveUpdateForSameDocumentNavigation();
581 void HistoryController::updateForFrameLoadCompleted()
583 // Even if already complete, we might have set a previous item on a frame that
584 // didn't do any data loading on the past transaction. Make sure to track that
585 // the load is complete so that we use the current item instead.
586 m_frameLoadComplete = true;
589 void HistoryController::setCurrentItem(HistoryItem* item)
591 m_frameLoadComplete = false;
592 m_previousItem = m_currentItem;
593 m_currentItem = item;
596 void HistoryController::setCurrentItemTitle(const StringWithDirection& title)
599 // FIXME: make use of title.direction() as well.
600 m_currentItem->setTitle(title.string());
603 bool HistoryController::currentItemShouldBeReplaced() const
605 // From the HTML5 spec for location.assign():
606 // "If the browsing context's session history contains only one Document,
607 // and that was the about:blank Document created when the browsing context
608 // was created, then the navigation must be done with replacement enabled."
609 return m_currentItem && !m_previousItem && equalIgnoringASCIICase(m_currentItem->urlString(), blankURL());
612 void HistoryController::clearPreviousItem()
614 m_previousItem = nullptr;
615 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
616 child->loader().history().clearPreviousItem();
619 void HistoryController::setProvisionalItem(HistoryItem* item)
621 m_provisionalItem = item;
624 void HistoryController::initializeItem(HistoryItem& item)
626 DocumentLoader* documentLoader = m_frame.loader().documentLoader();
627 ASSERT(documentLoader);
629 URL unreachableURL = documentLoader->unreachableURL();
634 if (!unreachableURL.isEmpty()) {
635 url = unreachableURL;
636 originalURL = unreachableURL;
638 url = documentLoader->url();
639 originalURL = documentLoader->originalURL();
642 // Frames that have never successfully loaded any content
643 // may have no URL at all. Currently our history code can't
644 // deal with such things, so we nip that in the bud here.
645 // Later we may want to learn to live with nil for URL.
646 // See bug 3368236 and related bugs for more information.
649 if (originalURL.isEmpty())
650 originalURL = blankURL();
652 StringWithDirection title = documentLoader->title();
655 item.setTarget(m_frame.tree().uniqueName());
656 // FIXME: should store title directionality in history as well.
657 item.setTitle(title.string());
658 item.setOriginalURLString(originalURL.string());
660 if (!unreachableURL.isEmpty() || documentLoader->response().httpStatusCode() >= 400)
661 item.setLastVisitWasFailure(true);
663 item.setShouldOpenExternalURLsPolicy(documentLoader->shouldOpenExternalURLsPolicyToPropagate());
665 // Save form state if this is a POST
666 item.setFormInfoFromRequest(documentLoader->request());
669 Ref<HistoryItem> HistoryController::createItem()
671 Ref<HistoryItem> item = HistoryItem::create();
672 initializeItem(item);
674 // Set the item for which we will save document state
675 setCurrentItem(item.ptr());
680 Ref<HistoryItem> HistoryController::createItemTree(Frame& targetFrame, bool clipAtTarget)
682 Ref<HistoryItem> bfItem = createItem();
683 if (!m_frameLoadComplete)
684 saveScrollPositionAndViewStateToItem(m_previousItem.get());
686 if (!clipAtTarget || &m_frame != &targetFrame) {
687 // save frame state for items that aren't loading (khtml doesn't save those)
690 // clipAtTarget is false for navigations within the same document, so
691 // we should copy the documentSequenceNumber over to the newly create
692 // item. Non-target items are just clones, and they should therefore
693 // preserve the same itemSequenceNumber.
694 if (m_previousItem) {
695 if (&m_frame != &targetFrame)
696 bfItem->setItemSequenceNumber(m_previousItem->itemSequenceNumber());
697 bfItem->setDocumentSequenceNumber(m_previousItem->documentSequenceNumber());
700 for (Frame* child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling()) {
701 FrameLoader& childLoader = child->loader();
702 bool hasChildLoaded = childLoader.frameHasLoaded();
704 // If the child is a frame corresponding to an <object> element that never loaded,
705 // we don't want to create a history item, because that causes fallback content
706 // to be ignored on reload.
708 if (!(!hasChildLoaded && childLoader.isHostedByObjectElement()))
709 bfItem->addChildItem(childLoader.history().createItemTree(targetFrame, clipAtTarget));
712 // FIXME: Eliminate the isTargetItem flag in favor of itemSequenceNumber.
713 if (&m_frame == &targetFrame)
714 bfItem->setIsTargetItem(true);
718 // The general idea here is to traverse the frame tree and the item tree in parallel,
719 // tracking whether each frame already has the content the item requests. If there is
720 // a match, we set the provisional item and recurse. Otherwise we will reload that
721 // frame and all its kids in recursiveGoToItem.
722 void HistoryController::recursiveSetProvisionalItem(HistoryItem& item, HistoryItem* fromItem)
724 if (!itemsAreClones(item, fromItem))
727 // Set provisional item, which will be committed in recursiveUpdateForCommit.
728 m_provisionalItem = &item;
730 for (auto& childItem : item.children()) {
731 const String& childFrameName = childItem->target();
733 HistoryItem* fromChildItem = fromItem->childItemWithTarget(childFrameName);
734 ASSERT(fromChildItem);
735 Frame* childFrame = m_frame.tree().child(childFrameName);
738 childFrame->loader().history().recursiveSetProvisionalItem(const_cast<HistoryItem&>(childItem.get()), fromChildItem);
742 // We now traverse the frame tree and item tree a second time, loading frames that
743 // do have the content the item requests.
744 void HistoryController::recursiveGoToItem(HistoryItem& item, HistoryItem* fromItem, FrameLoadType type)
746 if (!itemsAreClones(item, fromItem)) {
747 m_frame.loader().loadItem(item, type);
751 // Just iterate over the rest, looking for frames to navigate.
752 for (auto& childItem : item.children()) {
753 const String& childFrameName = childItem->target();
755 HistoryItem* fromChildItem = fromItem->childItemWithTarget(childFrameName);
756 ASSERT(fromChildItem);
757 if (Frame* childFrame = m_frame.tree().child(childFrameName))
758 childFrame->loader().history().recursiveGoToItem(const_cast<HistoryItem&>(childItem.get()), fromChildItem, type);
762 bool HistoryController::itemsAreClones(HistoryItem& item1, HistoryItem* item2) const
764 // If the item we're going to is a clone of the item we're at, then we do
765 // not need to load it again. The current frame tree and the frame tree
766 // snapshot in the item have to match.
767 // Note: Some clients treat a navigation to the current history item as
768 // a reload. Thus, if item1 and item2 are the same, we need to create a
769 // new document and should not consider them clones.
770 // (See http://webkit.org/b/35532 for details.)
773 && item1.itemSequenceNumber() == item2->itemSequenceNumber()
774 && currentFramesMatchItem(&item1)
775 && item2->hasSameFrames(item1);
778 // Helper method that determines whether the current frame tree matches given history item's.
779 bool HistoryController::currentFramesMatchItem(HistoryItem* item) const
781 if ((!m_frame.tree().uniqueName().isEmpty() || !item->target().isEmpty()) && m_frame.tree().uniqueName() != item->target())
784 const HistoryItemVector& childItems = item->children();
785 if (childItems.size() != m_frame.tree().childCount())
788 for (auto& item : childItems) {
789 if (!m_frame.tree().child(item->target()))
796 void HistoryController::updateBackForwardListClippedAtTarget(bool doClip)
798 // In the case of saving state about a page with frames, we store a tree of items that mirrors the frame tree.
799 // The item that was the target of the user's navigation is designated as the "targetItem".
800 // When this function is called with doClip=true we're able to create the whole tree except for the target's children,
801 // which will be loaded in the future. That part of the tree will be filled out as the child loads are committed.
803 Page* page = m_frame.page();
807 if (m_frame.loader().documentLoader()->urlForHistory().isEmpty())
810 FrameLoader& frameLoader = m_frame.mainFrame().loader();
812 Ref<HistoryItem> topItem = frameLoader.history().createItemTree(m_frame, doClip);
813 LOG(History, "HistoryController %p updateBackForwardListClippedAtTarget: Adding backforward item %p in frame %p (main frame %d) %s", this, topItem.ptr(), &m_frame, m_frame.isMainFrame(), m_frame.loader().documentLoader()->url().string().utf8().data());
815 page->backForward().addItem(WTFMove(topItem));
818 void HistoryController::updateCurrentItem()
823 DocumentLoader* documentLoader = m_frame.loader().documentLoader();
825 if (!documentLoader->unreachableURL().isEmpty())
828 if (m_currentItem->url() != documentLoader->url()) {
829 // We ended up on a completely different URL this time, so the HistoryItem
830 // needs to be re-initialized. Preserve the isTargetItem flag as it is a
831 // property of how this HistoryItem was originally created and is not
832 // dependent on the document.
833 bool isTargetItem = m_currentItem->isTargetItem();
834 m_currentItem->reset();
835 initializeItem(*m_currentItem);
836 m_currentItem->setIsTargetItem(isTargetItem);
838 // Even if the final URL didn't change, the form data may have changed.
839 m_currentItem->setFormInfoFromRequest(documentLoader->request());
843 void HistoryController::pushState(PassRefPtr<SerializedScriptValue> stateObject, const String& title, const String& urlString)
848 Page* page = m_frame.page();
851 // Get a HistoryItem tree for the current frame tree.
852 Ref<HistoryItem> topItem = m_frame.mainFrame().loader().history().createItemTree(m_frame, false);
854 // Override data in the current item (created by createItemTree) to reflect
855 // the pushState() arguments.
856 m_currentItem->setTitle(title);
857 m_currentItem->setStateObject(stateObject);
858 m_currentItem->setURLString(urlString);
860 LOG(History, "HistoryController %p pushState: Adding top item %p, setting url of current item %p to %s", this, topItem.ptr(), m_currentItem.get(), urlString.ascii().data());
862 page->backForward().addItem(WTFMove(topItem));
864 if (m_frame.page()->usesEphemeralSession())
867 addVisitedLink(*page, URL(ParsedURLString, urlString));
868 m_frame.loader().client().updateGlobalHistory();
871 void HistoryController::replaceState(PassRefPtr<SerializedScriptValue> stateObject, const String& title, const String& urlString)
876 LOG(History, "HistoryController %p replaceState: Setting url of current item %p to %s", this, m_currentItem.get(), urlString.ascii().data());
878 if (!urlString.isEmpty())
879 m_currentItem->setURLString(urlString);
880 m_currentItem->setTitle(title);
881 m_currentItem->setStateObject(stateObject);
882 m_currentItem->setFormData(nullptr);
883 m_currentItem->setFormContentType(String());
885 if (m_frame.page()->usesEphemeralSession())
888 ASSERT(m_frame.page());
889 addVisitedLink(*m_frame.page(), URL(ParsedURLString, urlString));
890 m_frame.loader().client().updateGlobalHistory();
893 void HistoryController::replaceCurrentItem(HistoryItem* item)
898 m_previousItem = nullptr;
899 if (m_provisionalItem)
900 m_provisionalItem = item;
902 m_currentItem = item;
905 } // namespace WebCore