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.
26 #include "WebBackForwardListProxy.h"
28 #include "DataReference.h"
29 #include "EncoderAdapter.h"
30 #include "WebCoreArgumentCoders.h"
32 #include "WebPageProxyMessages.h"
33 #include "WebProcess.h"
34 #include "WebProcessProxyMessages.h"
35 #include <WebCore/HistoryItem.h>
36 #include <wtf/HashMap.h>
38 using namespace WebCore;
42 static const unsigned DefaultCapacity = 100;
43 static const unsigned NoCurrentItemIndex = UINT_MAX;
45 // FIXME <rdar://problem/8819268>: This leaks all HistoryItems that go into these maps.
46 // We need to clear up the life time of these objects.
48 typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
49 typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
51 static IDToHistoryItemMap& idToHistoryItemMap()
53 static IDToHistoryItemMap map;
57 static HistoryItemToIDMap& historyItemToIDMap()
59 static HistoryItemToIDMap map;
63 static uint64_t generateHistoryItemID()
65 // These IDs exist in the WebProcess for items created by the WebProcess.
66 // The IDs generated here need to never collide with the IDs created in WebBackForwardList in the UIProcess.
67 // We accomplish this by starting from 3, and only ever using odd ids.
68 static uint64_t uniqueHistoryItemID = 1;
69 uniqueHistoryItemID += 2;
70 return uniqueHistoryItemID;
73 static uint64_t getIDForHistoryItem(HistoryItem* item)
77 std::pair<HistoryItemToIDMap::iterator, bool> result = historyItemToIDMap().add(item, 0);
79 itemID = generateHistoryItemID();
80 result.first->second = itemID;
81 idToHistoryItemMap().set(itemID, item);
83 itemID = result.first->second;
89 static void updateBackForwardItem(HistoryItem* item)
91 uint64_t itemID = getIDForHistoryItem(item);
92 const String& originalURLString = item->originalURLString();
93 const String& urlString = item->urlString();
94 const String& title = item->title();
96 // FIXME: We only want to do this work for top level back/forward items.
97 // The best way to do that is probably to arrange for this entire function to only be called by top leve back/forward items.
98 EncoderAdapter encoder;
99 item->encodeBackForwardTree(encoder);
101 WebProcess::shared().connection()->send(Messages::WebProcessProxy::AddBackForwardItem(itemID, originalURLString, urlString, title, encoder.data()), 0);
104 static void WK2NotifyHistoryItemChanged(HistoryItem* item)
106 updateBackForwardItem(item);
109 HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
111 return idToHistoryItemMap().get(itemID).get();
114 WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
117 WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
120 void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
125 RefPtr<HistoryItem> item = prpItem;
126 uint64_t itemID = historyItemToIDMap().get(item);
127 m_page->send(Messages::WebPageProxy::BackForwardAddItem(itemID));
130 void WebBackForwardListProxy::goToItem(HistoryItem* item)
135 uint64_t itemID = historyItemToIDMap().get(item);
136 m_page->send(Messages::WebPageProxy::BackForwardGoToItem(itemID));
139 HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
145 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID()))
151 RefPtr<HistoryItem> item = idToHistoryItemMap().get(itemID);
155 int WebBackForwardListProxy::backListCount()
160 int backListCount = 0;
161 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID()))
164 return backListCount;
167 int WebBackForwardListProxy::forwardListCount()
172 int forwardListCount = 0;
173 if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID()))
176 return forwardListCount;
179 void WebBackForwardListProxy::close()
184 bool WebBackForwardListProxy::isActive()
186 // FIXME: Should check the the list is enabled and has non-zero capacity.
190 void WebBackForwardListProxy::clear()
192 m_page->send(Messages::WebPageProxy::BackForwardClear());
196 void WebBackForwardListProxy::clearWMLPageHistory()
201 } // namespace WebKit