WebBackForwardListProxy.cpp [plain text]
#include "config.h"
#include "WebBackForwardListProxy.h"
#include "Logging.h"
#include "SessionState.h"
#include "SessionStateConversion.h"
#include "WebCoreArgumentCoders.h"
#include "WebPage.h"
#include "WebPageProxyMessages.h"
#include "WebProcess.h"
#include "WebProcessProxyMessages.h"
#include <WebCore/BackForwardCache.h>
#include <WebCore/Frame.h>
#include <WebCore/HistoryController.h>
#include <WebCore/HistoryItem.h>
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/ProcessID.h>
namespace WebKit {
using namespace WebCore;
typedef HashMap<BackForwardItemIdentifier, RefPtr<HistoryItem>> IDToHistoryItemMap; static IDToHistoryItemMap& idToHistoryItemMap()
{
static NeverDestroyed<IDToHistoryItemMap> map;
return map;
}
void WebBackForwardListProxy::addItemFromUIProcess(const BackForwardItemIdentifier& itemID, Ref<HistoryItem>&& item, PageIdentifier pageID, OverwriteExistingItem overwriteExistingItem)
{
ASSERT_UNUSED(overwriteExistingItem, overwriteExistingItem == OverwriteExistingItem::Yes || !idToHistoryItemMap().contains(itemID));
idToHistoryItemMap().set(itemID, item.ptr());
clearCachedListCounts();
}
static void WK2NotifyHistoryItemChanged(HistoryItem& item)
{
WebProcess::singleton().parentProcessConnection()->send(Messages::WebProcessProxy::UpdateBackForwardItem(toBackForwardListItemState(item)), 0);
}
HistoryItem* WebBackForwardListProxy::itemForID(const BackForwardItemIdentifier& itemID)
{
return idToHistoryItemMap().get(itemID);
}
void WebBackForwardListProxy::removeItem(const BackForwardItemIdentifier& itemID)
{
RefPtr<HistoryItem> item = idToHistoryItemMap().take(itemID);
if (!item)
return;
BackForwardCache::singleton().remove(*item);
WebCore::Page::clearPreviousItemFromAllPages(item.get());
}
WebBackForwardListProxy::WebBackForwardListProxy(WebPage& page)
: m_page(&page)
{
WebCore::notifyHistoryItemChanged = WK2NotifyHistoryItemChanged;
}
void WebBackForwardListProxy::addItem(Ref<HistoryItem>&& item)
{
if (!m_page)
return;
auto result = idToHistoryItemMap().add(item->identifier(), item.ptr());
ASSERT_UNUSED(result, result.isNewEntry);
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());
clearCachedListCounts();
m_page->send(Messages::WebPageProxy::BackForwardAddItem(toBackForwardListItemState(item.get())));
}
void WebBackForwardListProxy::goToItem(HistoryItem& item)
{
if (!m_page)
return;
WebBackForwardListCounts backForwardListCounts;
m_page->sendSync(Messages::WebPageProxy::BackForwardGoToItem(item.identifier()), Messages::WebPageProxy::BackForwardGoToItem::Reply(backForwardListCounts));
m_cachedBackForwardListCounts = backForwardListCounts;
}
RefPtr<HistoryItem> WebBackForwardListProxy::itemAtIndex(int itemIndex)
{
if (!m_page)
return nullptr;
Optional<BackForwardItemIdentifier> itemID;
if (!WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardItemAtIndex(itemIndex), Messages::WebPageProxy::BackForwardItemAtIndex::Reply(itemID), m_page->identifier()))
return nullptr;
if (!itemID)
return nullptr;
return idToHistoryItemMap().get(*itemID);
}
unsigned WebBackForwardListProxy::backListCount() const
{
return cacheListCountsIfNecessary().backCount;
}
unsigned WebBackForwardListProxy::forwardListCount() const
{
return cacheListCountsIfNecessary().forwardCount;
}
const WebBackForwardListCounts& WebBackForwardListProxy::cacheListCountsIfNecessary() const
{
if (!m_cachedBackForwardListCounts) {
WebBackForwardListCounts backForwardListCounts;
if (m_page)
WebProcess::singleton().parentProcessConnection()->sendSync(Messages::WebPageProxy::BackForwardListCounts(), Messages::WebPageProxy::BackForwardListCounts::Reply(backForwardListCounts), m_page->identifier());
m_cachedBackForwardListCounts = backForwardListCounts;
}
return *m_cachedBackForwardListCounts;
}
void WebBackForwardListProxy::clearCachedListCounts()
{
m_cachedBackForwardListCounts = WTF::nullopt;
}
void WebBackForwardListProxy::close()
{
ASSERT(m_page);
m_page = nullptr;
m_cachedBackForwardListCounts = WebBackForwardListCounts { };
}
void WebBackForwardListProxy::clear()
{
m_cachedBackForwardListCounts = WebBackForwardListCounts { }; m_page->send(Messages::WebPageProxy::BackForwardClear());
}
}