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 <WebCore/PageCache.h>
38 #include <wtf/HashMap.h>
40 using namespace WebCore;
44 static const unsigned DefaultCapacity = 100;
45 static const unsigned NoCurrentItemIndex = UINT_MAX;
47 // FIXME <rdar://problem/8819268>: This leaks all HistoryItems that go into these maps.
48 // We need to clear up the life time of these objects.
50 typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
51 typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
53 static IDToHistoryItemMap& idToHistoryItemMap()
55 DEFINE_STATIC_LOCAL(IDToHistoryItemMap, map, ());
59 static HistoryItemToIDMap& historyItemToIDMap()
61 DEFINE_STATIC_LOCAL(HistoryItemToIDMap, map, ());
65 static uint64_t uniqueHistoryItemID = 1;
67 static uint64_t generateHistoryItemID()
69 // These IDs exist in the WebProcess for items created by the WebProcess.
70 // The IDs generated here need to never collide with the IDs created in WebBackForwardList in the UIProcess.
71 // We accomplish this by starting from 3, and only ever using odd ids.
72 uniqueHistoryItemID += 2;
73 return uniqueHistoryItemID;
76 void WebBackForwardListProxy::setHighestItemIDFromUIProcess(uint64_t itemID)
78 if (itemID <= uniqueHistoryItemID)
82 uniqueHistoryItemID = itemID;
84 uniqueHistoryItemID = itemID + 1;
87 static void updateBackForwardItem(uint64_t itemID, HistoryItem* item)
89 EncoderAdapter encoder;
90 item->encodeBackForwardTree(encoder);
92 WebProcess::shared().connection()->send(Messages::WebProcessProxy::AddBackForwardItem(itemID, item->originalURLString(), item->urlString(), item->title(), encoder.dataReference()), 0);
95 void WebBackForwardListProxy::addItemFromUIProcess(uint64_t itemID, PassRefPtr<WebCore::HistoryItem> prpItem)
97 RefPtr<HistoryItem> item = prpItem;
99 // This item/itemID pair should not already exist in our maps.
100 ASSERT(!historyItemToIDMap().contains(item.get()));
101 ASSERT(!idToHistoryItemMap().contains(itemID));
103 historyItemToIDMap().set(item, itemID);
104 idToHistoryItemMap().set(itemID, item);
107 static void WK2NotifyHistoryItemChanged(HistoryItem* item)
109 uint64_t itemID = historyItemToIDMap().get(item);
113 updateBackForwardItem(itemID, item);
116 HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
118 return idToHistoryItemMap().get(itemID).get();
121 uint64_t WebBackForwardListProxy::idForItem(HistoryItem* item)
124 return historyItemToIDMap().get(item);
127 void WebBackForwardListProxy::removeItem(uint64_t itemID)
129 IDToHistoryItemMap::iterator it = idToHistoryItemMap().find(itemID);
130 if (it == idToHistoryItemMap().end())
133 WebCore::pageCache()->remove(it->second.get());
135 historyItemToIDMap().remove(it->second);
136 idToHistoryItemMap().remove(it);
139 WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
142 WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
145 void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
147 RefPtr<HistoryItem> item = prpItem;
149 ASSERT(!historyItemToIDMap().contains(item));
154 uint64_t itemID = generateHistoryItemID();
156 ASSERT(!idToHistoryItemMap().contains(itemID));
158 m_associatedItemIDs.add(itemID);
160 historyItemToIDMap().set(item, itemID);
161 idToHistoryItemMap().set(itemID, item);
163 updateBackForwardItem(itemID, item.get());
164 m_page->send(Messages::WebPageProxy::BackForwardAddItem(itemID));
167 void WebBackForwardListProxy::goToItem(HistoryItem* item)
172 SandboxExtension::Handle sandboxExtensionHandle;
173 m_page->sendSync(Messages::WebPageProxy::BackForwardGoToItem(historyItemToIDMap().get(item)), Messages::WebPageProxy::BackForwardGoToItem::Reply(sandboxExtensionHandle));
174 m_page->sandboxExtensionTracker().beginLoad(m_page->mainWebFrame(), sandboxExtensionHandle);
177 HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
183 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID()))
189 return idToHistoryItemMap().get(itemID).get();
192 int WebBackForwardListProxy::backListCount()
197 int backListCount = 0;
198 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID()))
201 return backListCount;
204 int WebBackForwardListProxy::forwardListCount()
209 int forwardListCount = 0;
210 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID()))
213 return forwardListCount;
216 void WebBackForwardListProxy::close()
218 HashSet<uint64_t>::iterator end = m_associatedItemIDs.end();
219 for (HashSet<uint64_t>::iterator i = m_associatedItemIDs.begin(); i != end; ++i)
220 WebCore::pageCache()->remove(itemForID(*i));
222 m_associatedItemIDs.clear();
227 bool WebBackForwardListProxy::isActive()
229 // FIXME: Should check the the list is enabled and has non-zero capacity.
233 void WebBackForwardListProxy::clear()
235 m_page->send(Messages::WebPageProxy::BackForwardClear());
238 } // namespace WebKit