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 "Logging.h" |
30 | #include "SessionState.h" |
31 | #include "SessionStateConversion.h" |
32 | #include "WebCoreArgumentCoders.h" |
33 | #include "WebPage.h" |
34 | #include "WebPageProxyMessages.h" |
35 | #include "WebProcess.h" |
36 | #include "WebProcessProxyMessages.h" |
37 | #include <WebCore/Frame.h> |
38 | #include <WebCore/HistoryController.h> |
39 | #include <WebCore/HistoryItem.h> |
40 | #include <WebCore/PageCache.h> |
41 | #include <wtf/HashMap.h> |
42 | #include <wtf/NeverDestroyed.h> |
43 | #include <wtf/ProcessID.h> |
44 | |
45 | namespace WebKit { |
46 | using namespace WebCore; |
47 | |
48 | // FIXME <rdar://problem/8819268>: This leaks all HistoryItems that go into these maps. |
49 | // We need to clear up the life time of these objects. |
50 | |
51 | typedef HashMap<BackForwardItemIdentifier, RefPtr<HistoryItem>> IDToHistoryItemMap; // "ID" here is the item ID. |
52 | static IDToHistoryItemMap& idToHistoryItemMap() |
53 | { |
54 | static NeverDestroyed<IDToHistoryItemMap> map; |
55 | return map; |
56 | } |
57 | |
58 | void WebBackForwardListProxy::addItemFromUIProcess(const BackForwardItemIdentifier& itemID, Ref<HistoryItem>&& item, PageIdentifier pageID, OverwriteExistingItem overwriteExistingItem) |
59 | { |
60 | // This item/itemID pair should not already exist in our map. |
61 | ASSERT_UNUSED(overwriteExistingItem, overwriteExistingItem == OverwriteExistingItem::Yes || !idToHistoryItemMap().contains(itemID)); |
62 | idToHistoryItemMap().set(itemID, item.ptr()); |
63 | } |
64 | |
65 | static void WK2NotifyHistoryItemChanged(HistoryItem& item) |
66 | { |
67 | WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::UpdateBackForwardItem(toBackForwardListItemState(item)), 0); |
68 | } |
69 | |
70 | HistoryItem* WebBackForwardListProxy::itemForID(const BackForwardItemIdentifier& itemID) |
71 | { |
72 | return idToHistoryItemMap().get(itemID); |
73 | } |
74 | |
75 | void WebBackForwardListProxy::removeItem(const BackForwardItemIdentifier& itemID) |
76 | { |
77 | RefPtr<HistoryItem> item = idToHistoryItemMap().take(itemID); |
78 | if (!item) |
79 | return; |
80 | |
81 | PageCache::singleton().remove(*item); |
82 | WebCore::Page::clearPreviousItemFromAllPages(item.get()); |
83 | } |
84 | |
85 | WebBackForwardListProxy::WebBackForwardListProxy(WebPage& page) |
86 | : m_page(&page) |
87 | { |
88 | WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged; |
89 | } |
90 | |
91 | void WebBackForwardListProxy::addItem(Ref<HistoryItem>&& item) |
92 | { |
93 | if (!m_page) |
94 | return; |
95 | |
96 | auto result = idToHistoryItemMap().add(item->identifier(), item.ptr()); |
97 | ASSERT_UNUSED(result, result.isNewEntry); |
98 | |
99 | LOG(BackForward, "(Back/Forward) WebProcess pid %i setting item %p for id %s with url %s" , getCurrentProcessID(), item.ptr(), item->identifier().logString(), item->urlString().utf8().data()); |
100 | m_page->send(Messages::WebPageProxy::BackForwardAddItem(toBackForwardListItemState(item.get()))); |
101 | } |
102 | |
103 | void WebBackForwardListProxy::goToItem(HistoryItem& item) |
104 | { |
105 | if (!m_page) |
106 | return; |
107 | |
108 | SandboxExtension::Handle sandboxExtensionHandle; |
109 | m_page->sendSync(Messages::WebPageProxy::BackForwardGoToItem(item.identifier()), Messages::WebPageProxy::BackForwardGoToItem::Reply(sandboxExtensionHandle)); |
110 | m_page->sandboxExtensionTracker().beginLoad(m_page->mainWebFrame(), WTFMove(sandboxExtensionHandle)); |
111 | } |
112 | |
113 | RefPtr<HistoryItem> WebBackForwardListProxy::itemAtIndex(int itemIndex) |
114 | { |
115 | if (!m_page) |
116 | return nullptr; |
117 | |
118 | Optional<BackForwardItemIdentifier> itemID; |
119 | if (!WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->pageID())) |
120 | return nullptr; |
121 | |
122 | if (!itemID) |
123 | return nullptr; |
124 | |
125 | return idToHistoryItemMap().get(*itemID); |
126 | } |
127 | |
128 | unsigned WebBackForwardListProxy::backListCount() const |
129 | { |
130 | if (!m_page) |
131 | return 0; |
132 | |
133 | unsigned backListCount = 0; |
134 | if (!WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardBackListCount(), Messages::WebPageProxy::BackForwardBackListCount::Reply(backListCount), m_page->pageID())) |
135 | return 0; |
136 | |
137 | return backListCount; |
138 | } |
139 | |
140 | unsigned WebBackForwardListProxy::forwardListCount() const |
141 | { |
142 | if (!m_page) |
143 | return 0; |
144 | |
145 | unsigned forwardListCount = 0; |
146 | if (!WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardForwardListCount(), Messages::WebPageProxy::BackForwardForwardListCount::Reply(forwardListCount), m_page->pageID())) |
147 | return 0; |
148 | |
149 | return forwardListCount; |
150 | } |
151 | |
152 | void WebBackForwardListProxy::close() |
153 | { |
154 | ASSERT(m_page); |
155 | m_page = nullptr; |
156 | } |
157 | |
158 | void WebBackForwardListProxy::clear() |
159 | { |
160 | m_page->send(Messages::WebPageProxy::BackForwardClear()); |
161 | } |
162 | |
163 | } // namespace WebKit |
164 | |