2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "WebBackForwardListProxy.h"
29 #include "DataReference.h"
30 #include "EncoderAdapter.h"
31 #include "WebCoreArgumentCoders.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #include "WebProcessProxyMessages.h"
36 #include <WebCore/HistoryItem.h>
37 #include <wtf/HashMap.h>
39 using namespace WebCore;
43 static const unsigned DefaultCapacity = 100;
44 static const unsigned NoCurrentItemIndex = UINT_MAX;
46 // FIXME <rdar://problem/8819268>: This leaks all HistoryItems that go into these maps.
47 // We need to clear up the life time of these objects.
49 typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
50 typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
52 static IDToHistoryItemMap& idToHistoryItemMap()
54 DEFINE_STATIC_LOCAL(IDToHistoryItemMap, map, ());
58 static HistoryItemToIDMap& historyItemToIDMap()
60 DEFINE_STATIC_LOCAL(HistoryItemToIDMap, map, ());
64 static uint64_t uniqueHistoryItemID = 1;
66 static uint64_t generateHistoryItemID()
68 // These IDs exist in the WebProcess for items created by the WebProcess.
69 // The IDs generated here need to never collide with the IDs created in WebBackForwardList in the UIProcess.
70 // We accomplish this by starting from 3, and only ever using odd ids.
71 uniqueHistoryItemID += 2;
72 return uniqueHistoryItemID;
75 void WebBackForwardListProxy::setHighestItemIDFromUIProcess(uint64_t itemID)
77 if (itemID <= uniqueHistoryItemID)
81 uniqueHistoryItemID = itemID;
83 uniqueHistoryItemID = itemID + 1;
86 static void updateBackForwardItem(uint64_t itemID, HistoryItem* item)
88 EncoderAdapter encoder;
89 item->encodeBackForwardTree(encoder);
91 WebProcess::shared().connection()->send(Messages::WebProcessProxy::AddBackForwardItem(itemID, item->originalURLString(), item->urlString(), item->title(), encoder.dataReference()), 0);
94 void WebBackForwardListProxy::addItemFromUIProcess(uint64_t itemID, PassRefPtr<WebCore::HistoryItem> prpItem)
96 RefPtr<HistoryItem> item = prpItem;
98 // This item/itemID pair should not already exist in our maps.
99 ASSERT(!historyItemToIDMap().contains(item.get()));
100 ASSERT(!idToHistoryItemMap().contains(itemID));
102 historyItemToIDMap().set(item, itemID);
103 idToHistoryItemMap().set(itemID, item);
106 static void WK2NotifyHistoryItemChanged(HistoryItem* item)
108 uint64_t itemID = historyItemToIDMap().get(item);
112 updateBackForwardItem(itemID, item);
115 HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
117 return idToHistoryItemMap().get(itemID).get();
120 uint64_t WebBackForwardListProxy::idForItem(HistoryItem* item)
123 return historyItemToIDMap().get(item);
126 void WebBackForwardListProxy::removeItem(uint64_t itemID)
128 IDToHistoryItemMap::iterator it = idToHistoryItemMap().find(itemID);
129 if (it == idToHistoryItemMap().end())
131 historyItemToIDMap().remove(it->second);
132 idToHistoryItemMap().remove(it);
135 WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
138 WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
141 void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
143 RefPtr<HistoryItem> item = prpItem;
145 ASSERT(!historyItemToIDMap().contains(item));
150 uint64_t itemID = generateHistoryItemID();
152 ASSERT(!idToHistoryItemMap().contains(itemID));
154 historyItemToIDMap().set(item, itemID);
155 idToHistoryItemMap().set(itemID, item);
157 updateBackForwardItem(itemID, item.get());
158 m_page->send(Messages::WebPageProxy::BackForwardAddItem(itemID));
161 void WebBackForwardListProxy::goToItem(HistoryItem* item)
166 m_page->send(Messages::WebPageProxy::BackForwardGoToItem(historyItemToIDMap().get(item)));
169 HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
175 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID()))
181 return idToHistoryItemMap().get(itemID).get();
184 int WebBackForwardListProxy::backListCount()
189 int backListCount = 0;
190 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID()))
193 return backListCount;
196 int WebBackForwardListProxy::forwardListCount()
201 int forwardListCount = 0;
202 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID()))
205 return forwardListCount;
208 void WebBackForwardListProxy::close()
213 bool WebBackForwardListProxy::isActive()
215 // FIXME: Should check the the list is enabled and has non-zero capacity.
219 void WebBackForwardListProxy::clear()
221 m_page->send(Messages::WebPageProxy::BackForwardClear());
224 } // namespace WebKit