1 /* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "core/history/BackForwardController.h" 28 29 #include "core/history/BackForwardClient.h" 30 #include "core/history/HistoryItem.h" 31 #include "core/page/Page.h" 32 33 namespace WebCore { 34 35 BackForwardController::BackForwardController(Page* page, BackForwardClient* client) 36 : m_page(page) 37 , m_client(client) 38 { 39 ASSERT(m_client); 40 } 41 42 BackForwardController::~BackForwardController() 43 { 44 } 45 46 PassOwnPtr<BackForwardController> BackForwardController::create(Page* page, BackForwardClient* client) 47 { 48 return adoptPtr(new BackForwardController(page, client)); 49 } 50 51 void BackForwardController::goBackOrForward(int distance) 52 { 53 if (distance == 0) 54 return; 55 56 HistoryItem* item = itemAtIndex(distance); 57 if (!item) { 58 if (distance > 0) { 59 if (forwardCount()) 60 item = itemAtIndex(forwardCount()); 61 } else { 62 if (backCount()) 63 item = itemAtIndex(-backCount()); 64 } 65 } 66 67 if (!item) 68 return; 69 70 m_page->goToItem(item); 71 } 72 73 bool BackForwardController::goBack() 74 { 75 HistoryItem* item = backItem(); 76 77 if (item) { 78 m_page->goToItem(item); 79 return true; 80 } 81 return false; 82 } 83 84 bool BackForwardController::goForward() 85 { 86 HistoryItem* item = forwardItem(); 87 88 if (item) { 89 m_page->goToItem(item); 90 return true; 91 } 92 return false; 93 } 94 95 void BackForwardController::addItem(PassRefPtr<HistoryItem> item) 96 { 97 m_client->addItem(item); 98 } 99 100 void BackForwardController::setCurrentItem(HistoryItem* item) 101 { 102 m_client->goToItem(item); 103 } 104 105 int BackForwardController::count() const 106 { 107 return backCount() + 1 + forwardCount(); 108 } 109 110 int BackForwardController::backCount() const 111 { 112 return m_client->backListCount(); 113 } 114 115 int BackForwardController::forwardCount() const 116 { 117 return m_client->forwardListCount(); 118 } 119 120 HistoryItem* BackForwardController::itemAtIndex(int i) 121 { 122 return m_client->itemAtIndex(i); 123 } 124 125 bool BackForwardController::isActive() 126 { 127 return m_client->isActive(); 128 } 129 130 void BackForwardController::close() 131 { 132 m_client->close(); 133 } 134 135 } // namespace WebCore 136