initial import
[vuplus_webkit] / Source / WebKit2 / WebProcess / WebPage / WebBackForwardListProxy.cpp
1 /*
2  * Copyright (C) 2010, 2011 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 "WebBackForwardListProxy.h"
28
29 #include "DataReference.h"
30 #include "EncoderAdapter.h"
31 #include "WebCoreArgumentCoders.h"
32 #include "WebPage.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #include "WebProcessProxyMessages.h"
36 #include <WebCore/HistoryItem.h>
37 #include <wtf/HashMap.h>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 static const unsigned DefaultCapacity = 100;
44 static const unsigned NoCurrentItemIndex = UINT_MAX;
45
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.
48
49 typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
50 typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
51
52 static IDToHistoryItemMap& idToHistoryItemMap()
53 {
54     DEFINE_STATIC_LOCAL(IDToHistoryItemMap, map, ());
55     return map;
56
57
58 static HistoryItemToIDMap& historyItemToIDMap()
59 {
60     DEFINE_STATIC_LOCAL(HistoryItemToIDMap, map, ());
61     return map;
62
63
64 static uint64_t uniqueHistoryItemID = 1;
65
66 static uint64_t generateHistoryItemID()
67 {
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;
73 }
74
75 void WebBackForwardListProxy::setHighestItemIDFromUIProcess(uint64_t itemID)
76 {
77     if (itemID <= uniqueHistoryItemID)
78         return;
79     
80      if (itemID % 2)
81          uniqueHistoryItemID = itemID;
82      else
83          uniqueHistoryItemID = itemID + 1;
84 }
85
86 static void updateBackForwardItem(uint64_t itemID, HistoryItem* item)
87 {
88     EncoderAdapter encoder;
89     item->encodeBackForwardTree(encoder);
90
91     WebProcess::shared().connection()->send(Messages::WebProcessProxy::AddBackForwardItem(itemID, item->originalURLString(), item->urlString(), item->title(), encoder.dataReference()), 0);
92 }
93
94 void WebBackForwardListProxy::addItemFromUIProcess(uint64_t itemID, PassRefPtr<WebCore::HistoryItem> prpItem)
95 {
96     RefPtr<HistoryItem> item = prpItem;
97     
98     // This item/itemID pair should not already exist in our maps.
99     ASSERT(!historyItemToIDMap().contains(item.get()));
100     ASSERT(!idToHistoryItemMap().contains(itemID));
101         
102     historyItemToIDMap().set(item, itemID);
103     idToHistoryItemMap().set(itemID, item);
104 }
105
106 static void WK2NotifyHistoryItemChanged(HistoryItem* item)
107 {
108     uint64_t itemID = historyItemToIDMap().get(item);
109     if (!itemID)
110         return;
111
112     updateBackForwardItem(itemID, item);
113 }
114
115 HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
116 {
117     return idToHistoryItemMap().get(itemID).get();
118 }
119
120 uint64_t WebBackForwardListProxy::idForItem(HistoryItem* item)
121 {
122     ASSERT(item);
123     return historyItemToIDMap().get(item);
124 }
125
126 void WebBackForwardListProxy::removeItem(uint64_t itemID)
127 {
128     IDToHistoryItemMap::iterator it = idToHistoryItemMap().find(itemID);
129     if (it == idToHistoryItemMap().end())
130         return;
131     historyItemToIDMap().remove(it->second);
132     idToHistoryItemMap().remove(it);
133 }
134
135 WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
136     : m_page(page)
137 {
138     WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
139 }
140
141 void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
142 {
143     RefPtr<HistoryItem> item = prpItem;
144
145     ASSERT(!historyItemToIDMap().contains(item));
146
147     if (!m_page)
148         return;
149
150     uint64_t itemID = generateHistoryItemID();
151
152     ASSERT(!idToHistoryItemMap().contains(itemID));
153
154     historyItemToIDMap().set(item, itemID);
155     idToHistoryItemMap().set(itemID, item);
156
157     updateBackForwardItem(itemID, item.get());
158     m_page->send(Messages::WebPageProxy::BackForwardAddItem(itemID));
159 }
160
161 void WebBackForwardListProxy::goToItem(HistoryItem* item)
162 {
163     if (!m_page)
164         return;
165
166     m_page->send(Messages::WebPageProxy::BackForwardGoToItem(historyItemToIDMap().get(item)));
167 }
168
169 HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
170 {
171     if (!m_page)
172         return 0;
173
174     uint64_t itemID = 0;
175     if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID()))
176         return 0;
177
178     if (!itemID)
179         return 0;
180
181     return idToHistoryItemMap().get(itemID).get();
182 }
183
184 int WebBackForwardListProxy::backListCount()
185 {
186     if (!m_page)
187         return 0;
188
189     int backListCount = 0;
190     if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID()))
191         return 0;
192
193     return backListCount;
194 }
195
196 int WebBackForwardListProxy::forwardListCount()
197 {
198     if (!m_page)
199         return 0;
200
201     int forwardListCount = 0;
202     if (!WebProcess::shared().connection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID()))
203         return 0;
204
205     return forwardListCount;
206 }
207
208 void WebBackForwardListProxy::close()
209 {
210     m_page = 0;
211 }
212
213 bool WebBackForwardListProxy::isActive()
214 {
215     // FIXME: Should check the the list is enabled and has non-zero capacity.
216     return true;
217 }
218
219 void WebBackForwardListProxy::clear()
220 {
221     m_page->send(Messages::WebPageProxy::BackForwardClear());
222 }
223
224 } // namespace WebKit