WebKit2/WebProcess/WebPage/WebBackForwardListProxy.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 "WebBackForwardListProxy.h"
       
    27 
       
    28 #include "WebCoreArgumentCoders.h"
       
    29 #include "WebPage.h"
       
    30 #include "WebPageProxyMessageKinds.h"
       
    31 #include "WebProcess.h"
       
    32 #include "WebProcessProxyMessageKinds.h"
       
    33 #include <WebCore/HistoryItem.h>
       
    34 #include <wtf/HashMap.h>
       
    35 
       
    36 using namespace WebCore;
       
    37 
       
    38 namespace WebKit {
       
    39 
       
    40 static const unsigned DefaultCapacity = 100;
       
    41 static const unsigned NoCurrentItemIndex = UINT_MAX;
       
    42 
       
    43 // FIXME: This leaks all HistoryItems that go into these maps.  We need to clear
       
    44 // up the life time of these objects.
       
    45 
       
    46 typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
       
    47 typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
       
    48 
       
    49 static IDToHistoryItemMap& idToHistoryItemMap()
       
    50 {
       
    51     static IDToHistoryItemMap map;
       
    52     return map;
       
    53 } 
       
    54 
       
    55 static HistoryItemToIDMap& historyItemToIDMap()
       
    56 {
       
    57     static HistoryItemToIDMap map;
       
    58     return map;
       
    59 } 
       
    60 
       
    61 static uint64_t generateHistoryItemID()
       
    62 {
       
    63     static uint64_t uniqueHistoryItemID = 1;
       
    64     return uniqueHistoryItemID++;
       
    65 }
       
    66 
       
    67 static uint64_t getIDForHistoryItem(HistoryItem* item)
       
    68 {
       
    69     uint64_t itemID = 0;
       
    70 
       
    71     std::pair<HistoryItemToIDMap::iterator, bool> result = historyItemToIDMap().add(item, 0);
       
    72     if (result.second) {
       
    73         itemID = generateHistoryItemID();
       
    74         result.first->second = itemID;
       
    75         idToHistoryItemMap().set(itemID, item);
       
    76     } else
       
    77         itemID = result.first->second;
       
    78 
       
    79     ASSERT(itemID);
       
    80     return itemID;
       
    81 }
       
    82 
       
    83 static void updateBackForwardItem(HistoryItem* item)
       
    84 {
       
    85     uint64_t itemID = getIDForHistoryItem(item);
       
    86     const String& originalURLString = item->originalURLString();
       
    87     const String& urlString = item->urlString();
       
    88     const String& title = item->title();
       
    89     WebProcess::shared().connection()->send(WebProcessProxyMessage::AddBackForwardItem, 0, CoreIPC::In(itemID, originalURLString, urlString, title));
       
    90 }
       
    91 
       
    92 static void WK2NotifyHistoryItemChanged(HistoryItem* item)
       
    93 {
       
    94     updateBackForwardItem(item);
       
    95 }
       
    96 
       
    97 HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
       
    98 {
       
    99     return idToHistoryItemMap().get(itemID).get();
       
   100 }
       
   101 
       
   102 WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
       
   103     : m_page(page)
       
   104     , m_capacity(DefaultCapacity)
       
   105     , m_closed(true)
       
   106     , m_enabled(true)
       
   107 {
       
   108     WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
       
   109 }
       
   110 
       
   111 WebBackForwardListProxy::~WebBackForwardListProxy()
       
   112 {
       
   113 }
       
   114 
       
   115 void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
       
   116 {
       
   117     if (!m_capacity || !m_enabled)
       
   118         return;
       
   119 
       
   120     RefPtr<HistoryItem> item = prpItem;
       
   121     uint64_t itemID = historyItemToIDMap().get(item);
       
   122     WebProcess::shared().connection()->send(WebPageProxyMessage::BackForwardAddItem, m_page->pageID(), CoreIPC::In(itemID));
       
   123 }
       
   124 
       
   125 void WebBackForwardListProxy::goBack()
       
   126 {
       
   127     ASSERT_NOT_REACHED();
       
   128 }
       
   129 
       
   130 void WebBackForwardListProxy::goForward()
       
   131 {
       
   132     ASSERT_NOT_REACHED();
       
   133 }
       
   134 
       
   135 void WebBackForwardListProxy::goToItem(HistoryItem* item)
       
   136 {
       
   137     uint64_t itemID = historyItemToIDMap().get(item);
       
   138     WebProcess::shared().connection()->send(WebPageProxyMessage::BackForwardGoToItem, m_page->pageID(), CoreIPC::In(itemID));
       
   139 }
       
   140 
       
   141 HistoryItem* WebBackForwardListProxy::backItem()
       
   142 {
       
   143     return 0;
       
   144 }
       
   145 
       
   146 HistoryItem* WebBackForwardListProxy::currentItem()
       
   147 {
       
   148     uint64_t currentItemID = 0;
       
   149     if (!WebProcess::shared().connection()->sendSync(WebPageProxyMessage::BackForwardCurrentItem,
       
   150                                                      m_page->pageID(), CoreIPC::In(),
       
   151                                                      CoreIPC::Out(currentItemID),
       
   152                                                      CoreIPC::Connection::NoTimeout)) {
       
   153         return 0;
       
   154     }
       
   155 
       
   156     if (!currentItemID)
       
   157         return 0;
       
   158 
       
   159     RefPtr<HistoryItem> item = idToHistoryItemMap().get(currentItemID);
       
   160     return item.get();
       
   161 }
       
   162 
       
   163 HistoryItem* WebBackForwardListProxy::forwardItem()
       
   164 {
       
   165     return 0;
       
   166 }
       
   167 
       
   168 HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
       
   169 {
       
   170     uint64_t itemID = 0;
       
   171     if (!WebProcess::shared().connection()->sendSync(WebPageProxyMessage::BackForwardItemAtIndex,
       
   172                                                      m_page->pageID(), CoreIPC::In(itemIndex),
       
   173                                                      CoreIPC::Out(itemID),
       
   174                                                      CoreIPC::Connection::NoTimeout)) {
       
   175         return 0;
       
   176     }
       
   177 
       
   178     if (!itemID)
       
   179         return 0;
       
   180 
       
   181     RefPtr<HistoryItem> item = idToHistoryItemMap().get(itemID);
       
   182     return item.get();
       
   183 }
       
   184 
       
   185 void WebBackForwardListProxy::backListWithLimit(int, HistoryItemVector&)
       
   186 {
       
   187     ASSERT_NOT_REACHED();
       
   188 }
       
   189 
       
   190 void WebBackForwardListProxy::forwardListWithLimit(int, HistoryItemVector&)
       
   191 {
       
   192     ASSERT_NOT_REACHED();
       
   193 }
       
   194 
       
   195 int WebBackForwardListProxy::capacity()
       
   196 {
       
   197     return m_capacity;
       
   198 }
       
   199 
       
   200 void WebBackForwardListProxy::setCapacity(int capacity)
       
   201 {
       
   202     m_capacity = capacity;
       
   203 }
       
   204 
       
   205 bool WebBackForwardListProxy::enabled()
       
   206 {
       
   207     return m_enabled;
       
   208 }
       
   209 
       
   210 void WebBackForwardListProxy::setEnabled(bool enabled)
       
   211 {
       
   212     m_enabled = enabled;
       
   213 }
       
   214 
       
   215 int WebBackForwardListProxy::backListCount()
       
   216 {
       
   217     int backListCount = 0;
       
   218     if (!WebProcess::shared().connection()->sendSync(WebPageProxyMessage::BackForwardBackListCount,
       
   219                                                      m_page->pageID(), CoreIPC::In(),
       
   220                                                      CoreIPC::Out(backListCount),
       
   221                                                      CoreIPC::Connection::NoTimeout)) {
       
   222         return 0;
       
   223     }
       
   224 
       
   225     return backListCount;
       
   226 }
       
   227 
       
   228 int WebBackForwardListProxy::forwardListCount()
       
   229 {
       
   230     int forwardListCount = 0;
       
   231     if (!WebProcess::shared().connection()->sendSync(WebPageProxyMessage::BackForwardForwardListCount,
       
   232                                                      m_page->pageID(), CoreIPC::In(),
       
   233                                                      CoreIPC::Out(forwardListCount),
       
   234                                                      CoreIPC::Connection::NoTimeout)) {
       
   235         return 0;
       
   236     }
       
   237 
       
   238     return forwardListCount;
       
   239 }
       
   240 
       
   241 bool WebBackForwardListProxy::containsItem(HistoryItem*)
       
   242 {
       
   243     return false;
       
   244 }
       
   245 
       
   246 void WebBackForwardListProxy::close()
       
   247 {
       
   248     m_closed = true;
       
   249     m_page = 0;
       
   250 }
       
   251 
       
   252 bool WebBackForwardListProxy::closed()
       
   253 {
       
   254     return m_closed;
       
   255 }
       
   256 
       
   257 void WebBackForwardListProxy::removeItem(HistoryItem*)
       
   258 {
       
   259 }
       
   260 
       
   261 HistoryItemVector& WebBackForwardListProxy::entries()
       
   262 {
       
   263     static HistoryItemVector noEntries;
       
   264     return noEntries;
       
   265 }
       
   266 
       
   267 void WebBackForwardListProxy::pushStateItem(PassRefPtr<HistoryItem>)
       
   268 {
       
   269 }
       
   270 
       
   271 #if ENABLE(WML)
       
   272 void WebBackForwardListProxy::clearWMLPageHistory()
       
   273 {
       
   274 }
       
   275 #endif
       
   276 
       
   277 } // namespace WebKit