Page::backForward() should return a reference.
<https://webkit.org/b/121151>

Reviewed by Anders Carlsson.

There is always a BackForwardController, so make backForward() return a reference.
Also made it store a Page& internally since it's tied to the lifetime of Page.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@155529 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 8fcce6d..62b7711 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,13 @@
+2013-09-11  Andreas Kling  <akling@apple.com>
+
+        Page::backForward() should return a reference.
+        <https://webkit.org/b/121151>
+
+        Reviewed by Anders Carlsson.
+
+        There is always a BackForwardController, so make backForward() return a reference.
+        Also made it store a Page& internally since it's tied to the lifetime of Page.
+
 2013-09-11  Mikhail Pozdnyakov  <mikhail.pozdnyakov@intel.com>
 
         WTF::OwnPtr should behave similarly with the rest of WTF smart pointers
diff --git a/Source/WebCore/history/BackForwardController.cpp b/Source/WebCore/history/BackForwardController.cpp
index 3b7ee28..0419700 100644
--- a/Source/WebCore/history/BackForwardController.cpp
+++ b/Source/WebCore/history/BackForwardController.cpp
@@ -32,41 +32,36 @@
 
 namespace WebCore {
 
-BackForwardController::BackForwardController(Page* page, PassRefPtr<BackForwardClient> client)
+BackForwardController::BackForwardController(Page& page, PassRefPtr<BackForwardClient> client)
     : m_page(page)
     , m_client(client)
 {
     if (!m_client)
-        m_client = BackForwardList::create(page);
+        m_client = BackForwardList::create(&page);
 }
 
 BackForwardController::~BackForwardController()
 {
 }
 
-PassOwnPtr<BackForwardController> BackForwardController::create(Page* page, PassRefPtr<BackForwardClient> client)
-{
-    return adoptPtr(new BackForwardController(page, client));
-}
-
 bool BackForwardController::canGoBackOrForward(int distance) const
 {
-    return m_page->canGoBackOrForward(distance);
+    return m_page.canGoBackOrForward(distance);
 }
 
 void BackForwardController::goBackOrForward(int distance)
 {
-    m_page->goBackOrForward(distance);
+    m_page.goBackOrForward(distance);
 }
 
 bool BackForwardController::goBack()
 {
-    return m_page->goBack();
+    return m_page.goBack();
 }
 
 bool BackForwardController::goForward()
 {
-    return m_page->goForward();
+    return m_page.goForward();
 }
 
 void BackForwardController::addItem(PassRefPtr<HistoryItem> item)
@@ -81,7 +76,7 @@
 
 int BackForwardController::count() const
 {
-    return m_page->getHistoryLength();
+    return m_page.getHistoryLength();
 }
 
 int BackForwardController::backCount() const
diff --git a/Source/WebCore/history/BackForwardController.h b/Source/WebCore/history/BackForwardController.h
index 67fe698..fb05a38 100644
--- a/Source/WebCore/history/BackForwardController.h
+++ b/Source/WebCore/history/BackForwardController.h
@@ -39,10 +39,9 @@
 class BackForwardController {
     WTF_MAKE_NONCOPYABLE(BackForwardController); WTF_MAKE_FAST_ALLOCATED;
 public:
+    BackForwardController(Page&, PassRefPtr<BackForwardClient>);
     ~BackForwardController();
 
-    static PassOwnPtr<BackForwardController> create(Page*, PassRefPtr<BackForwardClient>);
-
     BackForwardClient* client() const { return m_client.get(); }
 
     bool canGoBackOrForward(int distance) const;
@@ -69,9 +68,7 @@
     HistoryItem* forwardItem() { return itemAtIndex(1); }
 
 private:
-    BackForwardController(Page*, PassRefPtr<BackForwardClient>);
-
-    Page* m_page;
+    Page& m_page;
     RefPtr<BackForwardClient> m_client;
 };
 
diff --git a/Source/WebCore/history/PageCache.cpp b/Source/WebCore/history/PageCache.cpp
index 148a606..c7164cf 100644
--- a/Source/WebCore/history/PageCache.cpp
+++ b/Source/WebCore/history/PageCache.cpp
@@ -217,7 +217,7 @@
     if (frameRejectReasons)
         rejectReasons |= 1 << FrameCannotBeInPageCache;
     
-    if (!page->backForward()->isActive()) {
+    if (!page->backForward().isActive()) {
         PCLOG("   -The back/forward list is disabled or has 0 capacity");
         rejectReasons |= 1 << DisabledBackForwardList;
     }
@@ -355,7 +355,7 @@
     
     return m_capacity > 0
         && canCachePageContainingThisFrame(&page->mainFrame())
-        && page->backForward()->isActive()
+        && page->backForward().isActive()
         && page->settings().usesPageCache()
 #if ENABLE(DEVICE_ORIENTATION)
         && !DeviceMotionController::isActiveAt(page)
diff --git a/Source/WebCore/loader/FrameLoader.cpp b/Source/WebCore/loader/FrameLoader.cpp
index cb712c9..4d7d899 100644
--- a/Source/WebCore/loader/FrameLoader.cpp
+++ b/Source/WebCore/loader/FrameLoader.cpp
@@ -1123,7 +1123,7 @@
         currentItem = HistoryItem::create();
         currentItem->setLastVisitWasFailure(true);
         history().setCurrentItem(currentItem.get());
-        m_frame.page()->backForward()->setCurrentItem(currentItem.get());
+        m_frame.page()->backForward().setCurrentItem(currentItem.get());
 
         ASSERT(stateMachine()->isDisplayingInitialEmptyDocument());
         stateMachine()->advanceTo(FrameLoaderStateMachine::DisplayingInitialEmptyDocumentPostCommit);
@@ -2149,7 +2149,7 @@
             }
             if (shouldReset && item)
                 if (Page* page = m_frame.page()) {
-                    page->backForward()->setCurrentItem(item.get());
+                    page->backForward().setCurrentItem(item.get());
                     m_frame.loader().client().updateGlobalHistoryItemForPage();
                 }
             return;
@@ -2236,11 +2236,11 @@
     // It has the same meaning of "page a user thinks is the current one".
 
     KURL originalURL;
-    int backCount = page->backForward()->backCount();
+    int backCount = page->backForward().backCount();
     for (int backIndex = 0; backIndex <= backCount; backIndex++) {
         // FIXME: At one point we had code here to check a "was user gesture" flag.
         // Do we need to restore that logic?
-        HistoryItem* historyItem = page->backForward()->itemAtIndex(-backIndex);
+        HistoryItem* historyItem = page->backForward().itemAtIndex(-backIndex);
         if (!historyItem)
             continue;
 
@@ -2843,7 +2843,7 @@
         if ((isTargetItem || isLoadingMainFrame()) && isBackForwardLoadType(policyChecker().loadType())) {
             if (Page* page = m_frame.page()) {
                 if (HistoryItem* resetItem = page->mainFrame().loader().history().currentItem()) {
-                    page->backForward()->setCurrentItem(resetItem);
+                    page->backForward().setCurrentItem(resetItem);
                     m_frame.loader().client().updateGlobalHistoryItemForPage();
                 }
             }
@@ -3060,7 +3060,7 @@
     if (!page)
         return;
 
-    if (!m_didPerformFirstNavigation && page->backForward()->currentItem() && !page->backForward()->backItem() && !page->backForward()->forwardItem()) {
+    if (!m_didPerformFirstNavigation && page->backForward().currentItem() && !page->backForward().backItem() && !page->backForward().forwardItem()) {
         m_didPerformFirstNavigation = true;
         m_client.didPerformFirstNavigation();
     }
diff --git a/Source/WebCore/loader/HistoryController.cpp b/Source/WebCore/loader/HistoryController.cpp
index 41683ac..f98c38c 100644
--- a/Source/WebCore/loader/HistoryController.cpp
+++ b/Source/WebCore/loader/HistoryController.cpp
@@ -277,8 +277,8 @@
     // Set the BF cursor before commit, which lets the user quickly click back/forward again.
     // - plus, it only makes sense for the top level of the operation through the frametree,
     // as opposed to happening for some/one of the page commits that might happen soon
-    RefPtr<HistoryItem> currentItem = page->backForward()->currentItem();
-    page->backForward()->setCurrentItem(targetItem);
+    RefPtr<HistoryItem> currentItem = page->backForward().currentItem();
+    page->backForward().setCurrentItem(targetItem);
     m_frame.loader().client().updateGlobalHistoryItemForPage();
 
     // First set the provisional item of any frames that are not actually navigating.
@@ -802,7 +802,7 @@
 
     RefPtr<HistoryItem> topItem = frameLoader.history().createItemTree(m_frame, doClip);
     LOG(BackForward, "WebCoreBackForward - Adding backforward item %p for frame %s", topItem.get(), m_frame.loader().documentLoader()->url().string().ascii().data());
-    page->backForward()->addItem(topItem.release());
+    page->backForward().addItem(topItem.release());
 }
 
 void HistoryController::updateCurrentItem()
@@ -847,7 +847,7 @@
     m_currentItem->setStateObject(stateObject);
     m_currentItem->setURLString(urlString);
 
-    page->backForward()->addItem(topItem.release());
+    page->backForward().addItem(topItem.release());
 
     if (m_frame.settings().privateBrowsingEnabled())
         return;
diff --git a/Source/WebCore/loader/NavigationScheduler.cpp b/Source/WebCore/loader/NavigationScheduler.cpp
index 80877c1..22d13fa 100644
--- a/Source/WebCore/loader/NavigationScheduler.cpp
+++ b/Source/WebCore/loader/NavigationScheduler.cpp
@@ -205,7 +205,7 @@
         }
         // go(i!=0) from a frame navigates into the history of the frame only,
         // in both IE and NS (but not in Mozilla). We can't easily do that.
-        frame->page()->backForward()->goBackOrForward(m_historySteps);
+        frame->page()->backForward().goBackOrForward(m_historySteps);
     }
 
 private:
@@ -402,8 +402,8 @@
 
     // Invalid history navigations (such as history.forward() during a new load) have the side effect of cancelling any scheduled
     // redirects. We also avoid the possibility of cancelling the current load by avoiding the scheduled redirection altogether.
-    BackForwardController* backForward = m_frame->page()->backForward();
-    if (steps > backForward->forwardCount() || -steps > backForward->backCount()) {
+    BackForwardController& backForward = m_frame->page()->backForward();
+    if (steps > backForward.forwardCount() || -steps > backForward.backCount()) {
         cancel();
         return;
     }
diff --git a/Source/WebCore/page/ContextMenuController.cpp b/Source/WebCore/page/ContextMenuController.cpp
index a02093f..9200f4b 100644
--- a/Source/WebCore/page/ContextMenuController.cpp
+++ b/Source/WebCore/page/ContextMenuController.cpp
@@ -292,11 +292,11 @@
         break;
     case ContextMenuItemTagGoBack:
         if (Page* page = frame->page())
-            page->backForward()->goBackOrForward(-1);
+            page->backForward().goBackOrForward(-1);
         break;
     case ContextMenuItemTagGoForward:
         if (Page* page = frame->page())
-            page->backForward()->goBackOrForward(1);
+            page->backForward().goBackOrForward(1);
         break;
     case ContextMenuItemTagStop:
         frame->loader().stop();
@@ -912,10 +912,10 @@
                 appendItem(StopItem, m_contextMenu.get());
                 appendItem(ReloadItem, m_contextMenu.get());
 #else
-                if (frame->page() && frame->page()->backForward()->canGoBackOrForward(-1))
+                if (frame->page() && frame->page()->backForward().canGoBackOrForward(-1))
                     appendItem(BackItem, m_contextMenu.get());
 
-                if (frame->page() && frame->page()->backForward()->canGoBackOrForward(1))
+                if (frame->page() && frame->page()->backForward().canGoBackOrForward(1))
                     appendItem(ForwardItem, m_contextMenu.get());
 
                 // use isLoadingInAPISense rather than isLoading because Stop/Reload are
@@ -1293,10 +1293,10 @@
 #endif
 #if PLATFORM(GTK)
         case ContextMenuItemTagGoBack:
-            shouldEnable = frame->page() && frame->page()->backForward()->canGoBackOrForward(-1);
+            shouldEnable = frame->page() && frame->page()->backForward().canGoBackOrForward(-1);
             break;
         case ContextMenuItemTagGoForward:
-            shouldEnable = frame->page() && frame->page()->backForward()->canGoBackOrForward(1);
+            shouldEnable = frame->page() && frame->page()->backForward().canGoBackOrForward(1);
             break;
         case ContextMenuItemTagStop:
             shouldEnable = frame->loader().documentLoader()->isLoadingInAPISense();
diff --git a/Source/WebCore/page/DOMWindow.cpp b/Source/WebCore/page/DOMWindow.cpp
index f25f2ad..b2374fe 100644
--- a/Source/WebCore/page/DOMWindow.cpp
+++ b/Source/WebCore/page/DOMWindow.cpp
@@ -972,7 +972,7 @@
 
     bool allowScriptsToCloseWindows = m_frame->settings().allowScriptsToCloseWindows();
 
-    if (!(page->openedByDOM() || page->backForward()->count() <= 1 || allowScriptsToCloseWindows)) {
+    if (!(page->openedByDOM() || page->backForward().count() <= 1 || allowScriptsToCloseWindows)) {
         pageConsole()->addMessage(JSMessageSource, WarningMessageLevel, ASCIILiteral("Can't close the window since it was not opened by JavaScript"));
         return;
     }
diff --git a/Source/WebCore/page/History.cpp b/Source/WebCore/page/History.cpp
index 273f7b7..25c1fe4 100644
--- a/Source/WebCore/page/History.cpp
+++ b/Source/WebCore/page/History.cpp
@@ -53,7 +53,7 @@
         return 0;
     if (!m_frame->page())
         return 0;
-    return m_frame->page()->backForward()->count();
+    return m_frame->page()->backForward().count();
 }
 
 PassRefPtr<SerializedScriptValue> History::state()
diff --git a/Source/WebCore/page/Page.cpp b/Source/WebCore/page/Page.cpp
index c7026c9..f004f8c 100644
--- a/Source/WebCore/page/Page.cpp
+++ b/Source/WebCore/page/Page.cpp
@@ -141,7 +141,7 @@
 #endif
     , m_settings(Settings::create(this))
     , m_progress(ProgressTracker::create())
-    , m_backForwardController(BackForwardController::create(this, pageClients.backForwardClient))
+    , m_backForwardController(createOwned<BackForwardController>(*this, pageClients.backForwardClient))
     , m_mainFrame(Frame::create(this, 0, pageClients.loaderClientForMainFrame))
     , m_theme(RenderTheme::themeForPage(this))
     , m_editorClient(pageClients.editorClient)
@@ -230,7 +230,7 @@
     if (m_scrollingCoordinator)
         m_scrollingCoordinator->pageDestroyed();
 
-    backForward()->close();
+    backForward().close();
 
 #ifndef NDEBUG
     pageCounter.decrement();
@@ -356,12 +356,12 @@
 
 BackForwardClient* Page::backForwardClient() const
 {
-    return m_backForwardController->client();
+    return backForward().client();
 }
 
 bool Page::goBack()
 {
-    HistoryItem* item = backForward()->backItem();
+    HistoryItem* item = backForward().backItem();
     
     if (item) {
         goToItem(item, FrameLoadTypeBack);
@@ -372,7 +372,7 @@
 
 bool Page::goForward()
 {
-    HistoryItem* item = backForward()->forwardItem();
+    HistoryItem* item = backForward().forwardItem();
     
     if (item) {
         goToItem(item, FrameLoadTypeForward);
@@ -385,9 +385,9 @@
 {
     if (distance == 0)
         return true;
-    if (distance > 0 && distance <= backForward()->forwardCount())
+    if (distance > 0 && distance <= backForward().forwardCount())
         return true;
-    if (distance < 0 && -distance <= backForward()->backCount())
+    if (distance < 0 && -distance <= backForward().backCount())
         return true;
     return false;
 }
@@ -397,14 +397,14 @@
     if (distance == 0)
         return;
 
-    HistoryItem* item = backForward()->itemAtIndex(distance);
+    HistoryItem* item = backForward().itemAtIndex(distance);
     if (!item) {
         if (distance > 0) {
-            if (int forwardCount = backForward()->forwardCount()) 
-                item = backForward()->itemAtIndex(forwardCount);
+            if (int forwardCount = backForward().forwardCount())
+                item = backForward().itemAtIndex(forwardCount);
         } else {
-            if (int backCount = backForward()->backCount())
-                item = backForward()->itemAtIndex(-backCount);
+            if (int backCount = backForward().backCount())
+                item = backForward().itemAtIndex(-backCount);
         }
     }
 
@@ -428,7 +428,7 @@
 
 int Page::getHistoryLength()
 {
-    return backForward()->backCount() + 1 + backForward()->forwardCount();
+    return backForward().backCount() + 1 + backForward().forwardCount();
 }
 
 void Page::setGroupName(const String& name)
diff --git a/Source/WebCore/page/Page.h b/Source/WebCore/page/Page.h
index 7c88efa..55c2562 100644
--- a/Source/WebCore/page/Page.h
+++ b/Source/WebCore/page/Page.h
@@ -212,7 +212,7 @@
 
     Settings& settings() const { return *m_settings; }
     ProgressTracker& progress() const { return *m_progress; }
-    BackForwardController* backForward() const { return m_backForwardController.get(); }
+    BackForwardController& backForward() const { return *m_backForwardController; }
 
     FeatureObserver* featureObserver() { return &m_featureObserver; }
 
@@ -463,7 +463,7 @@
     const RefPtr<Settings> m_settings;
     const OwnPtr<ProgressTracker> m_progress;
 
-    OwnPtr<BackForwardController> m_backForwardController;
+    const OwnPtr<BackForwardController> m_backForwardController;
     const RefPtr<Frame> m_mainFrame;
 
     mutable RefPtr<PluginData> m_pluginData;
diff --git a/Source/WebCore/page/Settings.cpp b/Source/WebCore/page/Settings.cpp
index 3c14fb3..2a2f03f 100644
--- a/Source/WebCore/page/Settings.cpp
+++ b/Source/WebCore/page/Settings.cpp
@@ -468,10 +468,10 @@
         
     m_usesPageCache = usesPageCache;
     if (!m_usesPageCache) {
-        int first = -m_page->backForward()->backCount();
-        int last = m_page->backForward()->forwardCount();
+        int first = -m_page->backForward().backCount();
+        int last = m_page->backForward().forwardCount();
         for (int i = first; i <= last; i++)
-            pageCache()->remove(m_page->backForward()->itemAtIndex(i));
+            pageCache()->remove(m_page->backForward().itemAtIndex(i));
     }
 }
 
diff --git a/Source/WebKit/mac/WebCoreSupport/WebFrameLoaderClient.mm b/Source/WebKit/mac/WebCoreSupport/WebFrameLoaderClient.mm
index b4c6f84..220b4d8 100644
--- a/Source/WebKit/mac/WebCoreSupport/WebFrameLoaderClient.mm
+++ b/Source/WebKit/mac/WebCoreSupport/WebFrameLoaderClient.mm
@@ -977,7 +977,7 @@
 
     if (Page* page = core(m_webFrame.get())->page()) {
         if (!page->settings().privateBrowsingEnabled())
-            historyItem = page->backForward()->currentItem();
+            historyItem = page->backForward().currentItem();
     }
 
     WebView *webView = getWebView(m_webFrame.get());
diff --git a/Source/WebKit/win/WebCoreSupport/WebFrameLoaderClient.cpp b/Source/WebKit/win/WebCoreSupport/WebFrameLoaderClient.cpp
index 7c02a17..83848c2 100644
--- a/Source/WebKit/win/WebCoreSupport/WebFrameLoaderClient.cpp
+++ b/Source/WebKit/win/WebCoreSupport/WebFrameLoaderClient.cpp
@@ -825,7 +825,7 @@
 
     if (Page* page = webView->page()) {
         if (!page->settings().privateBrowsingEnabled())
-            historyItem = page->backForward()->currentItem();
+            historyItem = page->backForward().currentItem();
     }
 
     webView->setGlobalHistoryItem(historyItem);
diff --git a/Source/WebKit2/WebProcess/InjectedBundle/InjectedBundleBackForwardList.cpp b/Source/WebKit2/WebProcess/InjectedBundle/InjectedBundleBackForwardList.cpp
index d7fb9ec..e87905b 100644
--- a/Source/WebKit2/WebProcess/InjectedBundle/InjectedBundleBackForwardList.cpp
+++ b/Source/WebKit2/WebProcess/InjectedBundle/InjectedBundleBackForwardList.cpp
@@ -43,7 +43,7 @@
     Page* page = m_page->corePage();
     if (!page)
         return 0;
-    return InjectedBundleBackForwardListItem::create(page->backForward()->itemAtIndex(index));
+    return InjectedBundleBackForwardListItem::create(page->backForward().itemAtIndex(index));
 }
 
 int InjectedBundleBackForwardList::backListCount() const
@@ -53,7 +53,7 @@
     Page* page = m_page->corePage();
     if (!page)
         return 0;
-    return page->backForward()->backCount();
+    return page->backForward().backCount();
 }
 
 int InjectedBundleBackForwardList::forwardListCount() const
@@ -63,7 +63,7 @@
     Page* page = m_page->corePage();
     if (!page)
         return 0;
-    return page->backForward()->forwardCount();
+    return page->backForward().forwardCount();
 }
 
 void InjectedBundleBackForwardList::clear()
@@ -73,7 +73,7 @@
     Page* page = m_page->corePage();
     if (!page)
         return;
-    static_cast<WebBackForwardListProxy*>(page->backForward()->client())->clear();
+    static_cast<WebBackForwardListProxy*>(page->backForward().client())->clear();
 }
 
 } // namespace WebKit