Home | History | Annotate | Download | only in loader
      1 /*
      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/)
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     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 Computer, 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.
     18  *
     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.
     29  */
     30 
     31 #include "config.h"
     32 #include "HistoryController.h"
     33 
     34 #include "BackForwardController.h"
     35 #include "CachedPage.h"
     36 #include "DocumentLoader.h"
     37 #include "Frame.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"
     44 #include "Logging.h"
     45 #include "Page.h"
     46 #include "PageCache.h"
     47 #include "PageGroup.h"
     48 #include "Settings.h"
     49 #include <wtf/text/CString.h>
     50 
     51 #if USE(PLATFORM_STRATEGIES)
     52 #include "PlatformStrategies.h"
     53 #include "VisitedLinkStrategy.h"
     54 #endif
     55 
     56 namespace WebCore {
     57 
     58 static inline void addVisitedLink(Page* page, const KURL& url)
     59 {
     60 #if USE(PLATFORM_STRATEGIES)
     61     platformStrategies()->visitedLinkStrategy()->addVisitedLink(page, visitedLinkHash(url.string().characters(), url.string().length()));
     62 #else
     63     page->group().addVisitedLink(url);
     64 #endif
     65 }
     66 
     67 HistoryController::HistoryController(Frame* frame)
     68     : m_frame(frame)
     69     , m_frameLoadComplete(true)
     70 {
     71 }
     72 
     73 HistoryController::~HistoryController()
     74 {
     75 }
     76 
     77 void HistoryController::saveScrollPositionAndViewStateToItem(HistoryItem* item)
     78 {
     79     if (!item || !m_frame->view())
     80         return;
     81 
     82     if (m_frame->document()->inPageCache())
     83         item->setScrollPoint(m_frame->view()->cachedScrollPosition());
     84     else
     85         item->setScrollPoint(m_frame->view()->scrollPosition());
     86 
     87     item->setPageScaleFactor(m_frame->pageScaleFactor());
     88 
     89     // FIXME: It would be great to work out a way to put this code in WebCore instead of calling through to the client.
     90     m_frame->loader()->client()->saveViewStateToItem(item);
     91 }
     92 
     93 /*
     94  There is a race condition between the layout and load completion that affects restoring the scroll position.
     95  We try to restore the scroll position at both the first layout and upon load completion.
     96 
     97  1) If first layout happens before the load completes, we want to restore the scroll position then so that the
     98  first time we draw the page is already scrolled to the right place, instead of starting at the top and later
     99  jumping down.  It is possible that the old scroll position is past the part of the doc laid out so far, in
    100  which case the restore silent fails and we will fix it in when we try to restore on doc completion.
    101  2) If the layout happens after the load completes, the attempt to restore at load completion time silently
    102  fails.  We then successfully restore it when the layout happens.
    103 */
    104 void HistoryController::restoreScrollPositionAndViewState()
    105 {
    106     if (!m_frame->loader()->stateMachine()->committedFirstRealDocumentLoad())
    107         return;
    108 
    109     ASSERT(m_currentItem);
    110 
    111     // FIXME: As the ASSERT attests, it seems we should always have a currentItem here.
    112     // One counterexample is <rdar://problem/4917290>
    113     // For now, to cover this issue in release builds, there is no technical harm to returning
    114     // early and from a user standpoint - as in the above radar - the previous page load failed
    115     // so there *is* no scroll or view state to restore!
    116     if (!m_currentItem)
    117         return;
    118 
    119     // FIXME: It would be great to work out a way to put this code in WebCore instead of calling
    120     // through to the client. It's currently used only for the PDF view on Mac.
    121     m_frame->loader()->client()->restoreViewState();
    122 
    123     if (FrameView* view = m_frame->view()) {
    124         if (!view->wasScrolledByUser()) {
    125             view->setScrollPosition(m_currentItem->scrollPoint());
    126             m_frame->scalePage(m_currentItem->pageScaleFactor(), m_currentItem->scrollPoint());
    127         }
    128     }
    129 }
    130 
    131 void HistoryController::updateBackForwardListForFragmentScroll()
    132 {
    133     updateBackForwardListClippedAtTarget(false);
    134 }
    135 
    136 void HistoryController::saveDocumentState()
    137 {
    138     // FIXME: Reading this bit of FrameLoader state here is unfortunate.  I need to study
    139     // this more to see if we can remove this dependency.
    140     if (m_frame->loader()->stateMachine()->creatingInitialEmptyDocument())
    141         return;
    142 
    143     // For a standard page load, we will have a previous item set, which will be used to
    144     // store the form state.  However, in some cases we will have no previous item, and
    145     // the current item is the right place to save the state.  One example is when we
    146     // detach a bunch of frames because we are navigating from a site with frames to
    147     // another site.  Another is when saving the frame state of a frame that is not the
    148     // target of the current navigation (if we even decide to save with that granularity).
    149 
    150     // Because of previousItem's "masking" of currentItem for this purpose, it's important
    151     // that we keep track of the end of a page transition with m_frameLoadComplete.  We
    152     // leverage the checkLoadComplete recursion to achieve this goal.
    153 
    154     HistoryItem* item = m_frameLoadComplete ? m_currentItem.get() : m_previousItem.get();
    155     if (!item)
    156         return;
    157 
    158     Document* document = m_frame->document();
    159     ASSERT(document);
    160 
    161     if (item->isCurrentDocument(document)) {
    162         LOG(Loading, "WebCoreLoading %s: saving form state to %p", m_frame->tree()->uniqueName().string().utf8().data(), item);
    163         item->setDocumentState(document->formElementsState());
    164     }
    165 }
    166 
    167 // Walk the frame tree, telling all frames to save their form state into their current
    168 // history item.
    169 void HistoryController::saveDocumentAndScrollState()
    170 {
    171     for (Frame* frame = m_frame; frame; frame = frame->tree()->traverseNext(m_frame)) {
    172         frame->loader()->history()->saveDocumentState();
    173         frame->loader()->history()->saveScrollPositionAndViewStateToItem(frame->loader()->history()->currentItem());
    174     }
    175 }
    176 
    177 void HistoryController::restoreDocumentState()
    178 {
    179     Document* doc = m_frame->document();
    180 
    181     HistoryItem* itemToRestore = 0;
    182 
    183     switch (m_frame->loader()->loadType()) {
    184         case FrameLoadTypeReload:
    185         case FrameLoadTypeReloadFromOrigin:
    186         case FrameLoadTypeSame:
    187         case FrameLoadTypeReplace:
    188             break;
    189         case FrameLoadTypeBack:
    190         case FrameLoadTypeBackWMLDeckNotAccessible:
    191         case FrameLoadTypeForward:
    192         case FrameLoadTypeIndexedBackForward:
    193         case FrameLoadTypeRedirectWithLockedBackForwardList:
    194         case FrameLoadTypeStandard:
    195             itemToRestore = m_currentItem.get();
    196     }
    197 
    198     if (!itemToRestore)
    199         return;
    200 
    201     LOG(Loading, "WebCoreLoading %s: restoring form state from %p", m_frame->tree()->uniqueName().string().utf8().data(), itemToRestore);
    202     doc->setStateForNewFormElements(itemToRestore->documentState());
    203 }
    204 
    205 void HistoryController::invalidateCurrentItemCachedPage()
    206 {
    207     // When we are pre-commit, the currentItem is where the pageCache data resides
    208     CachedPage* cachedPage = pageCache()->get(currentItem());
    209 
    210     // FIXME: This is a grotesque hack to fix <rdar://problem/4059059> Crash in RenderFlow::detach
    211     // Somehow the PageState object is not properly updated, and is holding onto a stale document.
    212     // Both Xcode and FileMaker see this crash, Safari does not.
    213 
    214     ASSERT(!cachedPage || cachedPage->document() == m_frame->document());
    215     if (cachedPage && cachedPage->document() == m_frame->document()) {
    216         cachedPage->document()->setInPageCache(false);
    217         cachedPage->clear();
    218     }
    219 
    220     if (cachedPage)
    221         pageCache()->remove(currentItem());
    222 }
    223 
    224 bool HistoryController::shouldStopLoadingForHistoryItem(HistoryItem* targetItem) const
    225 {
    226     if (!m_currentItem)
    227         return false;
    228 
    229     // Don't abort the current load if we're navigating within the current document.
    230     if (m_currentItem->shouldDoSameDocumentNavigationTo(targetItem))
    231         return false;
    232 
    233     return m_frame->loader()->client()->shouldStopLoadingForHistoryItem(targetItem);
    234 }
    235 
    236 // Main funnel for navigating to a previous location (back/forward, non-search snap-back)
    237 // This includes recursion to handle loading into framesets properly
    238 void HistoryController::goToItem(HistoryItem* targetItem, FrameLoadType type)
    239 {
    240     ASSERT(!m_frame->tree()->parent());
    241 
    242     // shouldGoToHistoryItem is a private delegate method. This is needed to fix:
    243     // <rdar://problem/3951283> can view pages from the back/forward cache that should be disallowed by Parental Controls
    244     // Ultimately, history item navigations should go through the policy delegate. That's covered in:
    245     // <rdar://problem/3979539> back/forward cache navigations should consult policy delegate
    246     Page* page = m_frame->page();
    247     if (!page)
    248         return;
    249     if (!m_frame->loader()->client()->shouldGoToHistoryItem(targetItem))
    250         return;
    251 
    252     // Set the BF cursor before commit, which lets the user quickly click back/forward again.
    253     // - plus, it only makes sense for the top level of the operation through the frametree,
    254     // as opposed to happening for some/one of the page commits that might happen soon
    255     RefPtr<HistoryItem> currentItem = page->backForward()->currentItem();
    256     page->backForward()->setCurrentItem(targetItem);
    257     m_frame->loader()->client()->updateGlobalHistoryItemForPage();
    258 
    259     // First set the provisional item of any frames that are not actually navigating.
    260     // This must be done before trying to navigate the desired frame, because some
    261     // navigations can commit immediately (such as about:blank).  We must be sure that
    262     // all frames have provisional items set before the commit.
    263     recursiveSetProvisionalItem(targetItem, currentItem.get(), type);
    264     // Now that all other frames have provisional items, do the actual navigation.
    265     recursiveGoToItem(targetItem, currentItem.get(), type);
    266 }
    267 
    268 void HistoryController::updateForBackForwardNavigation()
    269 {
    270 #if !LOG_DISABLED
    271     if (m_frame->loader()->documentLoader())
    272         LOG(History, "WebCoreHistory: Updating History for back/forward navigation in frame %s", m_frame->loader()->documentLoader()->title().string().utf8().data());
    273 #endif
    274 
    275     // Must grab the current scroll position before disturbing it
    276     if (!m_frameLoadComplete)
    277         saveScrollPositionAndViewStateToItem(m_previousItem.get());
    278 
    279     // When traversing history, we may end up redirecting to a different URL
    280     // this time (e.g., due to cookies).  See http://webkit.org/b/49654.
    281     updateCurrentItem();
    282 }
    283 
    284 void HistoryController::updateForReload()
    285 {
    286 #if !LOG_DISABLED
    287     if (m_frame->loader()->documentLoader())
    288         LOG(History, "WebCoreHistory: Updating History for reload in frame %s", m_frame->loader()->documentLoader()->title().string().utf8().data());
    289 #endif
    290 
    291     if (m_currentItem) {
    292         pageCache()->remove(m_currentItem.get());
    293 
    294         if (m_frame->loader()->loadType() == FrameLoadTypeReload || m_frame->loader()->loadType() == FrameLoadTypeReloadFromOrigin)
    295             saveScrollPositionAndViewStateToItem(m_currentItem.get());
    296     }
    297 
    298     // When reloading the page, we may end up redirecting to a different URL
    299     // this time (e.g., due to cookies).  See http://webkit.org/b/4072.
    300     updateCurrentItem();
    301 }
    302 
    303 // There are 3 things you might think of as "history", all of which are handled by these functions.
    304 //
    305 //     1) Back/forward: The m_currentItem is part of this mechanism.
    306 //     2) Global history: Handled by the client.
    307 //     3) Visited links: Handled by the PageGroup.
    308 
    309 void HistoryController::updateForStandardLoad(HistoryUpdateType updateType)
    310 {
    311     LOG(History, "WebCoreHistory: Updating History for Standard Load in frame %s", m_frame->loader()->documentLoader()->url().string().ascii().data());
    312 
    313     FrameLoader* frameLoader = m_frame->loader();
    314 
    315     Settings* settings = m_frame->settings();
    316     bool needPrivacy = !settings || settings->privateBrowsingEnabled();
    317     const KURL& historyURL = frameLoader->documentLoader()->urlForHistory();
    318 
    319     if (!frameLoader->documentLoader()->isClientRedirect()) {
    320         if (!historyURL.isEmpty()) {
    321             if (updateType != UpdateAllExceptBackForwardList)
    322                 updateBackForwardListClippedAtTarget(true);
    323             if (!needPrivacy) {
    324                 frameLoader->client()->updateGlobalHistory();
    325                 frameLoader->documentLoader()->setDidCreateGlobalHistoryEntry(true);
    326                 if (frameLoader->documentLoader()->unreachableURL().isEmpty())
    327                     frameLoader->client()->updateGlobalHistoryRedirectLinks();
    328             }
    329 
    330             m_frame->loader()->client()->updateGlobalHistoryItemForPage();
    331         }
    332     } else {
    333         // The client redirect replaces the current history item.
    334         updateCurrentItem();
    335     }
    336 
    337     if (!historyURL.isEmpty() && !needPrivacy) {
    338         if (Page* page = m_frame->page())
    339             addVisitedLink(page, historyURL);
    340 
    341         if (!frameLoader->documentLoader()->didCreateGlobalHistoryEntry() && frameLoader->documentLoader()->unreachableURL().isEmpty() && !m_frame->document()->url().isEmpty())
    342             frameLoader->client()->updateGlobalHistoryRedirectLinks();
    343     }
    344 }
    345 
    346 void HistoryController::updateForRedirectWithLockedBackForwardList()
    347 {
    348 #if !LOG_DISABLED
    349     if (m_frame->loader()->documentLoader())
    350         LOG(History, "WebCoreHistory: Updating History for redirect load in frame %s", m_frame->loader()->documentLoader()->title().string().utf8().data());
    351 #endif
    352 
    353     Settings* settings = m_frame->settings();
    354     bool needPrivacy = !settings || settings->privateBrowsingEnabled();
    355     const KURL& historyURL = m_frame->loader()->documentLoader()->urlForHistory();
    356 
    357     if (m_frame->loader()->documentLoader()->isClientRedirect()) {
    358         if (!m_currentItem && !m_frame->tree()->parent()) {
    359             if (!historyURL.isEmpty()) {
    360                 updateBackForwardListClippedAtTarget(true);
    361                 if (!needPrivacy) {
    362                     m_frame->loader()->client()->updateGlobalHistory();
    363                     m_frame->loader()->documentLoader()->setDidCreateGlobalHistoryEntry(true);
    364                     if (m_frame->loader()->documentLoader()->unreachableURL().isEmpty())
    365                         m_frame->loader()->client()->updateGlobalHistoryRedirectLinks();
    366                 }
    367 
    368                 m_frame->loader()->client()->updateGlobalHistoryItemForPage();
    369             }
    370         }
    371         // The client redirect replaces the current history item.
    372         updateCurrentItem();
    373     } else {
    374         Frame* parentFrame = m_frame->tree()->parent();
    375         if (parentFrame && parentFrame->loader()->history()->m_currentItem)
    376             parentFrame->loader()->history()->m_currentItem->setChildItem(createItem());
    377     }
    378 
    379     if (!historyURL.isEmpty() && !needPrivacy) {
    380         if (Page* page = m_frame->page())
    381             addVisitedLink(page, historyURL);
    382 
    383         if (!m_frame->loader()->documentLoader()->didCreateGlobalHistoryEntry() && m_frame->loader()->documentLoader()->unreachableURL().isEmpty() && !m_frame->document()->url().isEmpty())
    384             m_frame->loader()->client()->updateGlobalHistoryRedirectLinks();
    385     }
    386 }
    387 
    388 void HistoryController::updateForClientRedirect()
    389 {
    390 #if !LOG_DISABLED
    391     if (m_frame->loader()->documentLoader())
    392         LOG(History, "WebCoreHistory: Updating History for client redirect in frame %s", m_frame->loader()->documentLoader()->title().string().utf8().data());
    393 #endif
    394 
    395     // Clear out form data so we don't try to restore it into the incoming page.  Must happen after
    396     // webcore has closed the URL and saved away the form state.
    397     if (m_currentItem) {
    398         m_currentItem->clearDocumentState();
    399         m_currentItem->clearScrollPoint();
    400     }
    401 
    402     Settings* settings = m_frame->settings();
    403     bool needPrivacy = !settings || settings->privateBrowsingEnabled();
    404     const KURL& historyURL = m_frame->loader()->documentLoader()->urlForHistory();
    405 
    406     if (!historyURL.isEmpty() && !needPrivacy) {
    407         if (Page* page = m_frame->page())
    408             addVisitedLink(page, historyURL);
    409     }
    410 }
    411 
    412 void HistoryController::updateForCommit()
    413 {
    414     FrameLoader* frameLoader = m_frame->loader();
    415 #if !LOG_DISABLED
    416     if (frameLoader->documentLoader())
    417         LOG(History, "WebCoreHistory: Updating History for commit in frame %s", frameLoader->documentLoader()->title().string().utf8().data());
    418 #endif
    419     FrameLoadType type = frameLoader->loadType();
    420     if (isBackForwardLoadType(type)
    421         || isReplaceLoadTypeWithProvisionalItem(type)
    422         || ((type == FrameLoadTypeReload || type == FrameLoadTypeReloadFromOrigin) && !frameLoader->provisionalDocumentLoader()->unreachableURL().isEmpty())) {
    423         // Once committed, we want to use current item for saving DocState, and
    424         // the provisional item for restoring state.
    425         // Note previousItem must be set before we close the URL, which will
    426         // happen when the data source is made non-provisional below
    427         m_frameLoadComplete = false;
    428         m_previousItem = m_currentItem;
    429         ASSERT(m_provisionalItem);
    430         m_currentItem = m_provisionalItem;
    431         m_provisionalItem = 0;
    432 
    433         // Tell all other frames in the tree to commit their provisional items and
    434         // restore their scroll position.  We'll avoid this frame (which has already
    435         // committed) and its children (which will be replaced).
    436         Page* page = m_frame->page();
    437         ASSERT(page);
    438         page->mainFrame()->loader()->history()->recursiveUpdateForCommit();
    439     }
    440 }
    441 
    442 bool HistoryController::isReplaceLoadTypeWithProvisionalItem(FrameLoadType type)
    443 {
    444     // Going back to an error page in a subframe can trigger a FrameLoadTypeReplace
    445     // while m_provisionalItem is set, so we need to commit it.
    446     return type == FrameLoadTypeReplace && m_provisionalItem;
    447 }
    448 
    449 void HistoryController::recursiveUpdateForCommit()
    450 {
    451     // The frame that navigated will now have a null provisional item.
    452     // Ignore it and its children.
    453     if (!m_provisionalItem)
    454         return;
    455 
    456     // For each frame that already had the content the item requested (based on
    457     // (a matching URL and frame tree snapshot), just restore the scroll position.
    458     // Save form state (works from currentItem, since m_frameLoadComplete is true)
    459     ASSERT(m_frameLoadComplete);
    460     saveDocumentState();
    461     saveScrollPositionAndViewStateToItem(m_currentItem.get());
    462 
    463     if (FrameView* view = m_frame->view())
    464         view->setWasScrolledByUser(false);
    465 
    466     // Now commit the provisional item
    467     m_frameLoadComplete = false;
    468     m_previousItem = m_currentItem;
    469     m_currentItem = m_provisionalItem;
    470     m_provisionalItem = 0;
    471 
    472     // Restore form state (works from currentItem)
    473     restoreDocumentState();
    474 
    475     // Restore the scroll position (we choose to do this rather than going back to the anchor point)
    476     restoreScrollPositionAndViewState();
    477 
    478     // Iterate over the rest of the tree
    479     for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
    480         child->loader()->history()->recursiveUpdateForCommit();
    481 }
    482 
    483 void HistoryController::updateForSameDocumentNavigation()
    484 {
    485     if (m_frame->document()->url().isEmpty())
    486         return;
    487 
    488     Settings* settings = m_frame->settings();
    489     if (!settings || settings->privateBrowsingEnabled())
    490         return;
    491 
    492     Page* page = m_frame->page();
    493     if (!page)
    494         return;
    495 
    496     addVisitedLink(page, m_frame->document()->url());
    497     page->mainFrame()->loader()->history()->recursiveUpdateForSameDocumentNavigation();
    498 }
    499 
    500 void HistoryController::recursiveUpdateForSameDocumentNavigation()
    501 {
    502     // The frame that navigated will now have a null provisional item.
    503     // Ignore it and its children.
    504     if (!m_provisionalItem)
    505         return;
    506 
    507     // Commit the provisional item.
    508     m_frameLoadComplete = false;
    509     m_previousItem = m_currentItem;
    510     m_currentItem = m_provisionalItem;
    511     m_provisionalItem = 0;
    512 
    513     // Iterate over the rest of the tree.
    514     for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
    515         child->loader()->history()->recursiveUpdateForSameDocumentNavigation();
    516 }
    517 
    518 void HistoryController::updateForFrameLoadCompleted()
    519 {
    520     // Even if already complete, we might have set a previous item on a frame that
    521     // didn't do any data loading on the past transaction. Make sure to track that
    522     // the load is complete so that we use the current item instead.
    523     m_frameLoadComplete = true;
    524 }
    525 
    526 void HistoryController::setCurrentItem(HistoryItem* item)
    527 {
    528     m_frameLoadComplete = false;
    529     m_previousItem = m_currentItem;
    530     m_currentItem = item;
    531 }
    532 
    533 void HistoryController::setCurrentItemTitle(const StringWithDirection& title)
    534 {
    535     if (m_currentItem)
    536         // FIXME: make use of title.direction() as well.
    537         m_currentItem->setTitle(title.string());
    538 }
    539 
    540 bool HistoryController::currentItemShouldBeReplaced() const
    541 {
    542     // From the HTML5 spec for location.assign():
    543     //  "If the browsing context's session history contains only one Document,
    544     //   and that was the about:blank Document created when the browsing context
    545     //   was created, then the navigation must be done with replacement enabled."
    546     return m_currentItem && !m_previousItem && equalIgnoringCase(m_currentItem->urlString(), blankURL());
    547 }
    548 
    549 void HistoryController::setProvisionalItem(HistoryItem* item)
    550 {
    551     m_provisionalItem = item;
    552 }
    553 
    554 void HistoryController::initializeItem(HistoryItem* item)
    555 {
    556     DocumentLoader* documentLoader = m_frame->loader()->documentLoader();
    557     ASSERT(documentLoader);
    558 
    559     KURL unreachableURL = documentLoader->unreachableURL();
    560 
    561     KURL url;
    562     KURL originalURL;
    563 
    564     if (!unreachableURL.isEmpty()) {
    565         url = unreachableURL;
    566         originalURL = unreachableURL;
    567     } else {
    568         url = documentLoader->url();
    569         originalURL = documentLoader->originalURL();
    570     }
    571 
    572     // Frames that have never successfully loaded any content
    573     // may have no URL at all. Currently our history code can't
    574     // deal with such things, so we nip that in the bud here.
    575     // Later we may want to learn to live with nil for URL.
    576     // See bug 3368236 and related bugs for more information.
    577     if (url.isEmpty())
    578         url = blankURL();
    579     if (originalURL.isEmpty())
    580         originalURL = blankURL();
    581 
    582     Frame* parentFrame = m_frame->tree()->parent();
    583     String parent = parentFrame ? parentFrame->tree()->uniqueName() : "";
    584     StringWithDirection title = documentLoader->title();
    585 
    586     item->setURL(url);
    587     item->setTarget(m_frame->tree()->uniqueName());
    588     item->setParent(parent);
    589     // FIXME: should store title directionality in history as well.
    590     item->setTitle(title.string());
    591     item->setOriginalURLString(originalURL.string());
    592 
    593     if (!unreachableURL.isEmpty() || documentLoader->response().httpStatusCode() >= 400)
    594         item->setLastVisitWasFailure(true);
    595 
    596     // Save form state if this is a POST
    597     item->setFormInfoFromRequest(documentLoader->request());
    598 }
    599 
    600 PassRefPtr<HistoryItem> HistoryController::createItem()
    601 {
    602     RefPtr<HistoryItem> item = HistoryItem::create();
    603     initializeItem(item.get());
    604 
    605     // Set the item for which we will save document state
    606     m_frameLoadComplete = false;
    607     m_previousItem = m_currentItem;
    608     m_currentItem = item;
    609 
    610     return item.release();
    611 }
    612 
    613 PassRefPtr<HistoryItem> HistoryController::createItemTree(Frame* targetFrame, bool clipAtTarget)
    614 {
    615     RefPtr<HistoryItem> bfItem = createItem();
    616     if (!m_frameLoadComplete)
    617         saveScrollPositionAndViewStateToItem(m_previousItem.get());
    618 
    619     if (!clipAtTarget || m_frame != targetFrame) {
    620         // save frame state for items that aren't loading (khtml doesn't save those)
    621         saveDocumentState();
    622 
    623         // clipAtTarget is false for navigations within the same document, so
    624         // we should copy the documentSequenceNumber over to the newly create
    625         // item.  Non-target items are just clones, and they should therefore
    626         // preserve the same itemSequenceNumber.
    627         if (m_previousItem) {
    628             if (m_frame != targetFrame)
    629                 bfItem->setItemSequenceNumber(m_previousItem->itemSequenceNumber());
    630             bfItem->setDocumentSequenceNumber(m_previousItem->documentSequenceNumber());
    631         }
    632 
    633         for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling()) {
    634             FrameLoader* childLoader = child->loader();
    635             bool hasChildLoaded = childLoader->frameHasLoaded();
    636 
    637             // If the child is a frame corresponding to an <object> element that never loaded,
    638             // we don't want to create a history item, because that causes fallback content
    639             // to be ignored on reload.
    640 
    641             if (!(!hasChildLoaded && childLoader->isHostedByObjectElement()))
    642                 bfItem->addChildItem(childLoader->history()->createItemTree(targetFrame, clipAtTarget));
    643         }
    644     }
    645     // FIXME: Eliminate the isTargetItem flag in favor of itemSequenceNumber.
    646     if (m_frame == targetFrame)
    647         bfItem->setIsTargetItem(true);
    648     return bfItem;
    649 }
    650 
    651 // The general idea here is to traverse the frame tree and the item tree in parallel,
    652 // tracking whether each frame already has the content the item requests.  If there is
    653 // a match, we set the provisional item and recurse.  Otherwise we will reload that
    654 // frame and all its kids in recursiveGoToItem.
    655 void HistoryController::recursiveSetProvisionalItem(HistoryItem* item, HistoryItem* fromItem, FrameLoadType type)
    656 {
    657     ASSERT(item);
    658     ASSERT(fromItem);
    659 
    660     if (itemsAreClones(item, fromItem)) {
    661         // Set provisional item, which will be committed in recursiveUpdateForCommit.
    662         m_provisionalItem = item;
    663 
    664         const HistoryItemVector& childItems = item->children();
    665 
    666         int size = childItems.size();
    667 
    668         for (int i = 0; i < size; ++i) {
    669             String childFrameName = childItems[i]->target();
    670             HistoryItem* fromChildItem = fromItem->childItemWithTarget(childFrameName);
    671             ASSERT(fromChildItem);
    672             Frame* childFrame = m_frame->tree()->child(childFrameName);
    673             ASSERT(childFrame);
    674             childFrame->loader()->history()->recursiveSetProvisionalItem(childItems[i].get(), fromChildItem, type);
    675         }
    676     }
    677 }
    678 
    679 // We now traverse the frame tree and item tree a second time, loading frames that
    680 // do have the content the item requests.
    681 void HistoryController::recursiveGoToItem(HistoryItem* item, HistoryItem* fromItem, FrameLoadType type)
    682 {
    683     ASSERT(item);
    684     ASSERT(fromItem);
    685 
    686     if (itemsAreClones(item, fromItem)) {
    687         // Just iterate over the rest, looking for frames to navigate.
    688         const HistoryItemVector& childItems = item->children();
    689 
    690         int size = childItems.size();
    691         for (int i = 0; i < size; ++i) {
    692             String childFrameName = childItems[i]->target();
    693             HistoryItem* fromChildItem = fromItem->childItemWithTarget(childFrameName);
    694             ASSERT(fromChildItem);
    695             Frame* childFrame = m_frame->tree()->child(childFrameName);
    696             ASSERT(childFrame);
    697             childFrame->loader()->history()->recursiveGoToItem(childItems[i].get(), fromChildItem, type);
    698         }
    699     } else {
    700         m_frame->loader()->loadItem(item, type);
    701     }
    702 }
    703 
    704 bool HistoryController::itemsAreClones(HistoryItem* item1, HistoryItem* item2) const
    705 {
    706     // If the item we're going to is a clone of the item we're at, then we do
    707     // not need to load it again.  The current frame tree and the frame tree
    708     // snapshot in the item have to match.
    709     // Note: Some clients treat a navigation to the current history item as
    710     // a reload.  Thus, if item1 and item2 are the same, we need to create a
    711     // new document and should not consider them clones.
    712     // (See http://webkit.org/b/35532 for details.)
    713     return item1 != item2
    714         && item1->itemSequenceNumber() == item2->itemSequenceNumber()
    715         && currentFramesMatchItem(item1)
    716         && item2->hasSameFrames(item1);
    717 }
    718 
    719 // Helper method that determines whether the current frame tree matches given history item's.
    720 bool HistoryController::currentFramesMatchItem(HistoryItem* item) const
    721 {
    722     if ((!m_frame->tree()->uniqueName().isEmpty() || !item->target().isEmpty()) && m_frame->tree()->uniqueName() != item->target())
    723         return false;
    724 
    725     const HistoryItemVector& childItems = item->children();
    726     if (childItems.size() != m_frame->tree()->childCount())
    727         return false;
    728 
    729     unsigned size = childItems.size();
    730     for (unsigned i = 0; i < size; ++i) {
    731         if (!m_frame->tree()->child(childItems[i]->target()))
    732             return false;
    733     }
    734 
    735     return true;
    736 }
    737 
    738 void HistoryController::updateBackForwardListClippedAtTarget(bool doClip)
    739 {
    740     // In the case of saving state about a page with frames, we store a tree of items that mirrors the frame tree.
    741     // The item that was the target of the user's navigation is designated as the "targetItem".
    742     // When this function is called with doClip=true we're able to create the whole tree except for the target's children,
    743     // which will be loaded in the future. That part of the tree will be filled out as the child loads are committed.
    744 
    745     Page* page = m_frame->page();
    746     if (!page)
    747         return;
    748 
    749     if (m_frame->loader()->documentLoader()->urlForHistory().isEmpty())
    750         return;
    751 
    752     Frame* mainFrame = page->mainFrame();
    753     ASSERT(mainFrame);
    754     FrameLoader* frameLoader = mainFrame->loader();
    755 
    756     frameLoader->checkDidPerformFirstNavigation();
    757 
    758     RefPtr<HistoryItem> topItem = frameLoader->history()->createItemTree(m_frame, doClip);
    759     LOG(BackForward, "WebCoreBackForward - Adding backforward item %p for frame %s", topItem.get(), m_frame->loader()->documentLoader()->url().string().ascii().data());
    760     page->backForward()->addItem(topItem.release());
    761 }
    762 
    763 void HistoryController::updateCurrentItem()
    764 {
    765     if (!m_currentItem)
    766         return;
    767 
    768     DocumentLoader* documentLoader = m_frame->loader()->documentLoader();
    769 
    770     if (!documentLoader->unreachableURL().isEmpty())
    771         return;
    772 
    773     if (m_currentItem->url() != documentLoader->url()) {
    774         // We ended up on a completely different URL this time, so the HistoryItem
    775         // needs to be re-initialized.  Preserve the isTargetItem flag as it is a
    776         // property of how this HistoryItem was originally created and is not
    777         // dependent on the document.
    778         bool isTargetItem = m_currentItem->isTargetItem();
    779         m_currentItem->reset();
    780         initializeItem(m_currentItem.get());
    781         m_currentItem->setIsTargetItem(isTargetItem);
    782     } else {
    783         // Even if the final URL didn't change, the form data may have changed.
    784         m_currentItem->setFormInfoFromRequest(documentLoader->request());
    785     }
    786 }
    787 
    788 void HistoryController::pushState(PassRefPtr<SerializedScriptValue> stateObject, const String& title, const String& urlString)
    789 {
    790     if (!m_currentItem)
    791         return;
    792 
    793     Page* page = m_frame->page();
    794     ASSERT(page);
    795 
    796     // Get a HistoryItem tree for the current frame tree.
    797     RefPtr<HistoryItem> topItem = page->mainFrame()->loader()->history()->createItemTree(m_frame, false);
    798 
    799     // Override data in the current item (created by createItemTree) to reflect
    800     // the pushState() arguments.
    801     m_currentItem->setTitle(title);
    802     m_currentItem->setStateObject(stateObject);
    803     m_currentItem->setURLString(urlString);
    804 
    805     page->backForward()->addItem(topItem.release());
    806 
    807     addVisitedLink(page, KURL(ParsedURLString, urlString));
    808     m_frame->loader()->client()->updateGlobalHistory();
    809 
    810 }
    811 
    812 void HistoryController::replaceState(PassRefPtr<SerializedScriptValue> stateObject, const String& title, const String& urlString)
    813 {
    814     if (!m_currentItem)
    815         return;
    816 
    817     if (!urlString.isEmpty())
    818         m_currentItem->setURLString(urlString);
    819     m_currentItem->setTitle(title);
    820     m_currentItem->setStateObject(stateObject);
    821 
    822     ASSERT(m_frame->page());
    823     addVisitedLink(m_frame->page(), KURL(ParsedURLString, urlString));
    824     m_frame->loader()->client()->updateGlobalHistory();
    825 }
    826 
    827 } // namespace WebCore
    828