WebBackForwardListCF.cpp [plain text]
#include "config.h"
#include "WebBackForwardList.h"
#include "Logging.h"
#include <wtf/RetainPtr.h>
#include <CoreFoundation/CoreFoundation.h>
using namespace WebCore;
namespace WebKit {
static uint64_t generateWebBackForwardItemID()
{
static uint64_t uniqueHistoryItemID = 0;
uniqueHistoryItemID += 2;
return uniqueHistoryItemID;
}
static CFIndex currentVersion = 1;
DEFINE_STATIC_GETTER(CFNumberRef, SessionHistoryCurrentVersion, (CFNumberCreate(0, kCFNumberCFIndexType, ¤tVersion)));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryVersionKey, (CFSTR("SessionHistoryVersion")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryCurrentIndexKey, (CFSTR("SessionHistoryCurrentIndex")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntriesKey, (CFSTR("SessionHistoryEntries")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryTitleKey, (CFSTR("SessionHistoryEntryTitle")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryURLKey, (CFSTR("SessionHistoryEntryURL")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryOriginalURLKey, (CFSTR("SessionHistoryEntryOriginalURL")));
DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryDataKey, (CFSTR("SessionHistoryEntryData")));
static bool extractBackForwardListEntriesFromArray(CFArrayRef, BackForwardListItemVector&);
static CFDictionaryRef createEmptySessionHistoryDictionary()
{
static const void* keys[1] = { SessionHistoryVersionKey() };
static const void* values[1] = { SessionHistoryCurrentVersion() };
return CFDictionaryCreate(0, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
CFDictionaryRef WebBackForwardList::createCFDictionaryRepresentation(WebPageProxy::WebPageProxySessionStateFilterCallback filter, void* context) const
{
ASSERT(!m_hasCurrentIndex || m_currentIndex < m_entries.size());
if (!m_hasCurrentIndex) {
return createEmptySessionHistoryDictionary();
}
RetainPtr<CFMutableArrayRef> entries = adoptCF(CFArrayCreateMutable(0, m_entries.size(), &kCFTypeArrayCallBacks));
CFIndex currentIndex = m_currentIndex;
bool hasCurrentIndex = true;
for (size_t i = 0; i < m_entries.size(); ++i) {
ASSERT(m_entries[i]);
if (!m_entries[i]) {
LOG(SessionState, "WebBackForwardList contained a null entry at index %lu", i);
return 0;
}
if (filter) {
if (!filter(toAPI(m_page), WKPageGetSessionBackForwardListItemValueType(), toAPI(m_entries[i].get()), context)
|| !filter(toAPI(m_page), WKPageGetSessionHistoryURLValueType(), toURLRef(m_entries[i]->originalURL().impl()), context)) {
if (i <= m_currentIndex)
currentIndex--;
continue;
}
}
RetainPtr<CFStringRef> url = m_entries[i]->url().createCFString();
RetainPtr<CFStringRef> title = m_entries[i]->title().createCFString();
RetainPtr<CFStringRef> originalURL = m_entries[i]->originalURL().createCFString();
RetainPtr<CFDataRef> entryData = adoptCF(CFDataCreate(kCFAllocatorDefault, m_entries[i]->backForwardData().data(), m_entries[i]->backForwardData().size()));
const void* keys[4] = { SessionHistoryEntryURLKey(), SessionHistoryEntryTitleKey(), SessionHistoryEntryOriginalURLKey(), SessionHistoryEntryDataKey() };
const void* values[4] = { url.get(), title.get(), originalURL.get(), entryData.get() };
RetainPtr<CFDictionaryRef> entryDictionary = adoptCF(CFDictionaryCreate(0, keys, values, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
CFArrayAppendValue(entries.get(), entryDictionary.get());
}
ASSERT(currentIndex == -1 || (currentIndex > -1 && currentIndex < CFArrayGetCount(entries.get())));
if (currentIndex < -1 || currentIndex >= CFArrayGetCount(entries.get())) {
LOG(SessionState, "Filtering entries to be saved resulted in an inconsistent state that we cannot represent");
return 0;
}
if (currentIndex == -1) {
if (CFArrayGetCount(entries.get()))
currentIndex = 0;
else
hasCurrentIndex = false;
}
if (hasCurrentIndex) {
RetainPtr<CFNumberRef> currentIndexNumber = adoptCF(CFNumberCreate(0, kCFNumberCFIndexType, ¤tIndex));
const void* keys[3] = { SessionHistoryVersionKey(), SessionHistoryCurrentIndexKey(), SessionHistoryEntriesKey() };
const void* values[3] = { SessionHistoryCurrentVersion(), currentIndexNumber.get(), entries.get() };
return CFDictionaryCreate(0, keys, values, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
return createEmptySessionHistoryDictionary();
}
bool WebBackForwardList::restoreFromCFDictionaryRepresentation(CFDictionaryRef dictionary)
{
CFNumberRef cfVersion = (CFNumberRef)CFDictionaryGetValue(dictionary, SessionHistoryVersionKey());
if (!cfVersion) {
return restoreFromV0CFDictionaryRepresentation(dictionary);
}
if (CFGetTypeID(cfVersion) != CFNumberGetTypeID()) {
LOG(SessionState, "WebBackForwardList dictionary representation contains a version that is not a number");
return false;
}
CFIndex version;
if (!CFNumberGetValue(cfVersion, kCFNumberCFIndexType, &version)) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a correctly typed current version");
return false;
}
if (version == 1)
return restoreFromV1CFDictionaryRepresentation(dictionary);
LOG(SessionState, "WebBackForwardList dictionary representation has an invalid current version (%ld)", version);
return false;
}
bool WebBackForwardList::restoreFromV0CFDictionaryRepresentation(CFDictionaryRef dictionary)
{
CFNumberRef cfIndex = (CFNumberRef)CFDictionaryGetValue(dictionary, SessionHistoryCurrentIndexKey());
if (!cfIndex || CFGetTypeID(cfIndex) != CFNumberGetTypeID()) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid current index");
return false;
}
CFIndex currentCFIndex;
if (!CFNumberGetValue(cfIndex, kCFNumberCFIndexType, ¤tCFIndex)) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a correctly typed current index");
return false;
}
if (currentCFIndex < -1) {
LOG(SessionState, "WebBackForwardList dictionary representation contains an unexpected negative current index (%ld)", currentCFIndex);
return false;
}
CFArrayRef cfEntries = (CFArrayRef)CFDictionaryGetValue(dictionary, SessionHistoryEntriesKey());
if (!cfEntries || CFGetTypeID(cfEntries) != CFArrayGetTypeID()) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid list of entries");
return false;
}
CFIndex size = CFArrayGetCount(cfEntries);
if (size < 0 || currentCFIndex >= size) {
LOG(SessionState, "WebBackForwardList dictionary representation contains an invalid current index (%ld) for the number of entries (%ld)", currentCFIndex, size);
return false;
}
bool hasCurrentIndex = currentCFIndex != -1;
if (!hasCurrentIndex && size) {
LOG(SessionState, "WebBackForwardList dictionary representation says there is no current index, but there is a list of %ld entries", size);
return false;
}
BackForwardListItemVector entries;
if (!extractBackForwardListEntriesFromArray(cfEntries, entries)) {
return false;
}
ASSERT(entries.size() == static_cast<unsigned>(size));
m_hasCurrentIndex = hasCurrentIndex;
m_currentIndex = m_hasCurrentIndex ? static_cast<uint32_t>(currentCFIndex) : 0;
m_entries = entries;
return true;
}
bool WebBackForwardList::restoreFromV1CFDictionaryRepresentation(CFDictionaryRef dictionary)
{
CFNumberRef cfIndex = (CFNumberRef)CFDictionaryGetValue(dictionary, SessionHistoryCurrentIndexKey());
if (!cfIndex) {
m_hasCurrentIndex = false;
m_currentIndex = 0;
m_entries.clear();
return true;
}
if (CFGetTypeID(cfIndex) != CFNumberGetTypeID()) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid current index");
return false;
}
CFIndex currentCFIndex;
if (!CFNumberGetValue(cfIndex, kCFNumberCFIndexType, ¤tCFIndex)) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a correctly typed current index");
return false;
}
if (currentCFIndex < 0) {
LOG(SessionState, "WebBackForwardList dictionary representation contains an unexpected negative current index (%ld)", currentCFIndex);
return false;
}
CFArrayRef cfEntries = (CFArrayRef)CFDictionaryGetValue(dictionary, SessionHistoryEntriesKey());
if (!cfEntries || CFGetTypeID(cfEntries) != CFArrayGetTypeID()) {
LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid list of entries");
return false;
}
CFIndex size = CFArrayGetCount(cfEntries);
if (currentCFIndex >= size) {
LOG(SessionState, "WebBackForwardList dictionary representation contains an invalid current index (%ld) for the number of entries (%ld)", currentCFIndex, size);
return false;
}
BackForwardListItemVector entries;
if (!extractBackForwardListEntriesFromArray(cfEntries, entries)) {
return false;
}
ASSERT(entries.size() == static_cast<unsigned>(size));
m_hasCurrentIndex = true;
m_currentIndex = static_cast<uint32_t>(currentCFIndex);
m_entries = entries;
return true;
}
static bool extractBackForwardListEntriesFromArray(CFArrayRef cfEntries, BackForwardListItemVector& entries)
{
CFIndex size = CFArrayGetCount(cfEntries);
entries.reserveCapacity(size);
for (CFIndex i = 0; i < size; ++i) {
CFDictionaryRef entryDictionary = (CFDictionaryRef)CFArrayGetValueAtIndex(cfEntries, i);
if (!entryDictionary || CFGetTypeID(entryDictionary) != CFDictionaryGetTypeID()) {
LOG(SessionState, "WebBackForwardList entry array does not have a valid entry at index %i", (int)i);
return false;
}
CFStringRef entryURL = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryURLKey());
if (!entryURL || CFGetTypeID(entryURL) != CFStringGetTypeID()) {
LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid URL", (int)i);
return false;
}
CFStringRef entryTitle = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryTitleKey());
if (!entryTitle || CFGetTypeID(entryTitle) != CFStringGetTypeID()) {
LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid title", (int)i);
return false;
}
CFStringRef originalURL = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryOriginalURLKey());
if (!originalURL || CFGetTypeID(originalURL) != CFStringGetTypeID()) {
LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid original URL", (int)i);
return false;
}
CFDataRef backForwardData = (CFDataRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryDataKey());
if (!backForwardData || CFGetTypeID(backForwardData) != CFDataGetTypeID()) {
LOG(SessionState, "WebBackForwardList entry at index %i does not have back/forward data", (int)i);
return false;
}
entries.append(WebBackForwardListItem::create(originalURL, entryURL, entryTitle, CFDataGetBytePtr(backForwardData), CFDataGetLength(backForwardData), generateWebBackForwardItemID()));
}
return true;
}
}