WebBackForwardListProxy.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "config.h"
  26. #include "WebBackForwardListProxy.h"
  27. #include "DataReference.h"
  28. #include "EncoderAdapter.h"
  29. #include "WebCoreArgumentCoders.h"
  30. #include "WebPage.h"
  31. #include "WebPageProxyMessages.h"
  32. #include "WebProcess.h"
  33. #include "WebProcessProxyMessages.h"
  34. #include <WebCore/HistoryItem.h>
  35. #include <WebCore/PageCache.h>
  36. #include <wtf/HashMap.h>
  37. using namespace WebCore;
  38. namespace WebKit {
  39. // FIXME <rdar://problem/8819268>: This leaks all HistoryItems that go into these maps.
  40. // We need to clear up the life time of these objects.
  41. typedef HashMap<uint64_t, RefPtr<HistoryItem> > IDToHistoryItemMap;
  42. typedef HashMap<RefPtr<HistoryItem>, uint64_t> HistoryItemToIDMap;
  43. static IDToHistoryItemMap& idToHistoryItemMap()
  44. {
  45. DEFINE_STATIC_LOCAL(IDToHistoryItemMap, map, ());
  46. return map;
  47. }
  48. static HistoryItemToIDMap& historyItemToIDMap()
  49. {
  50. DEFINE_STATIC_LOCAL(HistoryItemToIDMap, map, ());
  51. return map;
  52. }
  53. static uint64_t uniqueHistoryItemID = 1;
  54. static uint64_t generateHistoryItemID()
  55. {
  56. // These IDs exist in the WebProcess for items created by the WebProcess.
  57. // The IDs generated here need to never collide with the IDs created in WebBackForwardList in the UIProcess.
  58. // We accomplish this by starting from 3, and only ever using odd ids.
  59. uniqueHistoryItemID += 2;
  60. return uniqueHistoryItemID;
  61. }
  62. void WebBackForwardListProxy::setHighestItemIDFromUIProcess(uint64_t itemID)
  63. {
  64. if (itemID <= uniqueHistoryItemID)
  65. return;
  66. if (itemID % 2)
  67. uniqueHistoryItemID = itemID;
  68. else
  69. uniqueHistoryItemID = itemID + 1;
  70. }
  71. static void updateBackForwardItem(uint64_t itemID, HistoryItem* item)
  72. {
  73. EncoderAdapter encoder;
  74. item->encodeBackForwardTree(encoder);
  75. WebProcess::shared().parentProcessConnection()->send(Messages::WebProcessProxy::AddBackForwardItem(itemID, item->originalURLString(), item->urlString(), item->title(), encoder.dataReference()), 0);
  76. }
  77. void WebBackForwardListProxy::addItemFromUIProcess(uint64_t itemID, PassRefPtr<WebCore::HistoryItem> prpItem)
  78. {
  79. RefPtr<HistoryItem> item = prpItem;
  80. // This item/itemID pair should not already exist in our maps.
  81. ASSERT(!historyItemToIDMap().contains(item.get()));
  82. ASSERT(!idToHistoryItemMap().contains(itemID));
  83. historyItemToIDMap().set(item, itemID);
  84. idToHistoryItemMap().set(itemID, item);
  85. }
  86. static void WK2NotifyHistoryItemChanged(HistoryItem* item)
  87. {
  88. uint64_t itemID = historyItemToIDMap().get(item);
  89. if (!itemID)
  90. return;
  91. updateBackForwardItem(itemID, item);
  92. }
  93. HistoryItem* WebBackForwardListProxy::itemForID(uint64_t itemID)
  94. {
  95. return idToHistoryItemMap().get(itemID);
  96. }
  97. uint64_t WebBackForwardListProxy::idForItem(HistoryItem* item)
  98. {
  99. ASSERT(item);
  100. return historyItemToIDMap().get(item);
  101. }
  102. void WebBackForwardListProxy::removeItem(uint64_t itemID)
  103. {
  104. IDToHistoryItemMap::iterator it = idToHistoryItemMap().find(itemID);
  105. if (it == idToHistoryItemMap().end())
  106. return;
  107. WebCore::pageCache()->remove(it->value.get());
  108. historyItemToIDMap().remove(it->value);
  109. idToHistoryItemMap().remove(it);
  110. }
  111. WebBackForwardListProxy::WebBackForwardListProxy(WebPage* page)
  112. : m_page(page)
  113. {
  114. WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
  115. }
  116. void WebBackForwardListProxy::addItem(PassRefPtr<HistoryItem> prpItem)
  117. {
  118. RefPtr<HistoryItem> item = prpItem;
  119. ASSERT(!historyItemToIDMap().contains(item));
  120. if (!m_page)
  121. return;
  122. uint64_t itemID = generateHistoryItemID();
  123. ASSERT(!idToHistoryItemMap().contains(itemID));
  124. m_associatedItemIDs.add(itemID);
  125. historyItemToIDMap().set(item, itemID);
  126. idToHistoryItemMap().set(itemID, item);
  127. updateBackForwardItem(itemID, item.get());
  128. m_page->send(Messages::WebPageProxy::BackForwardAddItem(itemID));
  129. }
  130. void WebBackForwardListProxy::goToItem(HistoryItem* item)
  131. {
  132. if (!m_page)
  133. return;
  134. SandboxExtension::Handle sandboxExtensionHandle;
  135. m_page->sendSync(Messages::WebPageProxy::BackForwardGoToItem(historyItemToIDMap().get(item)), Messages::WebPageProxy::BackForwardGoToItem::Reply(sandboxExtensionHandle));
  136. m_page->sandboxExtensionTracker().beginLoad(m_page->mainWebFrame(), sandboxExtensionHandle);
  137. }
  138. HistoryItem* WebBackForwardListProxy::itemAtIndex(int itemIndex)
  139. {
  140. if (!m_page)
  141. return 0;
  142. uint64_t itemID = 0;
  143. if (!WebProcess::shared().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID()))
  144. return 0;
  145. if (!itemID)
  146. return 0;
  147. return idToHistoryItemMap().get(itemID);
  148. }
  149. int WebBackForwardListProxy::backListCount()
  150. {
  151. if (!m_page)
  152. return 0;
  153. int backListCount = 0;
  154. if (!WebProcess::shared().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID()))
  155. return 0;
  156. return backListCount;
  157. }
  158. int WebBackForwardListProxy::forwardListCount()
  159. {
  160. if (!m_page)
  161. return 0;
  162. int forwardListCount = 0;
  163. if (!WebProcess::shared().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID()))
  164. return 0;
  165. return forwardListCount;
  166. }
  167. void WebBackForwardListProxy::close()
  168. {
  169. HashSet<uint64_t>::iterator end = m_associatedItemIDs.end();
  170. for (HashSet<uint64_t>::iterator i = m_associatedItemIDs.begin(); i != end; ++i)
  171. WebCore::pageCache()->remove(itemForID(*i));
  172. m_associatedItemIDs.clear();
  173. m_page = 0;
  174. }
  175. bool WebBackForwardListProxy::isActive()
  176. {
  177. // FIXME: Should check the the list is enabled and has non-zero capacity.
  178. return true;
  179. }
  180. void WebBackForwardListProxy::clear()
  181. {
  182. m_page->send(Messages::WebPageProxy::BackForwardClear());
  183. }
  184. } // namespace WebKit