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 "WebBackForwardList.h" 28 #include "Logging.h" 29 #include <wtf/RetainPtr.h> 30 #include <CoreFoundation/CoreFoundation.h> 31 32 using namespace WebCore; 33 34 namespace WebKit { 35 36 static uint64_t generateWebBackForwardItemID() 37 { 38 // These IDs exist in the UIProcess for items created by the UIProcess. 39 // The IDs generated here need to never collide with the IDs created in WebBackForwardListProxy in the WebProcess. 40 // We accomplish this by starting from 2, and only ever using even ids. 41 static uint64_t uniqueHistoryItemID = 0; 42 uniqueHistoryItemID += 2; 43 return uniqueHistoryItemID; 44 } 45 46 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryCurrentIndexKey, (CFSTR("SessionHistoryCurrentIndex"))); 47 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntriesKey, (CFSTR("SessionHistoryEntries"))); 48 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryTitleKey, (CFSTR("SessionHistoryEntryTitle"))); 49 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryURLKey, (CFSTR("SessionHistoryEntryURL"))); 50 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryOriginalURLKey, (CFSTR("SessionHistoryEntryOriginalURL"))); 51 DEFINE_STATIC_GETTER(CFStringRef, SessionHistoryEntryDataKey, (CFSTR("SessionHistoryEntryData"))); 52 53 CFDictionaryRef WebBackForwardList::createCFDictionaryRepresentation(WebPageProxy::WebPageProxySessionStateFilterCallback filter, void* context) const 54 { 55 ASSERT(m_current == NoCurrentItemIndex || m_current < m_entries.size()); 56 57 RetainPtr<CFMutableArrayRef> entries(AdoptCF, CFArrayCreateMutable(0, m_entries.size(), &kCFTypeArrayCallBacks)); 58 59 // We may need to update the current index to account for entries that are filtered by the callback. 60 int currentIndex = m_current; 61 62 for (size_t i = 0; i < m_entries.size(); ++i) { 63 RefPtr<WebURL> webURL = WebURL::create(m_entries[i]->url()); 64 if (filter && !filter(toAPI(m_page), WKPageGetSessionHistoryURLValueType(), toURLRef(m_entries[i]->originalURL().impl()), context)) { 65 if (i <= static_cast<size_t>(currentIndex)) 66 currentIndex--; 67 continue; 68 } 69 70 RetainPtr<CFStringRef> url(AdoptCF, m_entries[i]->url().createCFString()); 71 RetainPtr<CFStringRef> title(AdoptCF, m_entries[i]->title().createCFString()); 72 RetainPtr<CFStringRef> originalURL(AdoptCF, m_entries[i]->originalURL().createCFString()); 73 RetainPtr<CFDataRef> entryData(AdoptCF, CFDataCreate(kCFAllocatorDefault, m_entries[i]->backForwardData().data(), m_entries[i]->backForwardData().size())); 74 75 const void* keys[4] = { SessionHistoryEntryURLKey(), SessionHistoryEntryTitleKey(), SessionHistoryEntryOriginalURLKey(), SessionHistoryEntryDataKey() }; 76 const void* values[4] = { url.get(), title.get(), originalURL.get(), entryData.get() }; 77 78 RetainPtr<CFDictionaryRef> entryDictionary(AdoptCF, CFDictionaryCreate(0, keys, values, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); 79 CFArrayAppendValue(entries.get(), entryDictionary.get()); 80 } 81 RetainPtr<CFNumberRef> currentIndexNumber(AdoptCF, CFNumberCreate(0, kCFNumberIntType, ¤tIndex)); 82 83 const void* keys[2] = { SessionHistoryCurrentIndexKey(), SessionHistoryEntriesKey() }; 84 const void* values[2] = { currentIndexNumber.get(), entries.get() }; 85 86 return CFDictionaryCreate(0, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 87 } 88 89 bool WebBackForwardList::restoreFromCFDictionaryRepresentation(CFDictionaryRef dictionary) 90 { 91 CFNumberRef cfIndex = (CFNumberRef)CFDictionaryGetValue(dictionary, SessionHistoryCurrentIndexKey()); 92 if (!cfIndex || CFGetTypeID(cfIndex) != CFNumberGetTypeID()) { 93 LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid current index"); 94 return false; 95 } 96 97 CFIndex currentIndex; 98 if (!CFNumberGetValue(cfIndex, kCFNumberCFIndexType, ¤tIndex)) { 99 LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid integer current index"); 100 return false; 101 } 102 103 CFArrayRef cfEntries = (CFArrayRef)CFDictionaryGetValue(dictionary, SessionHistoryEntriesKey()); 104 if (!cfEntries || CFGetTypeID(cfEntries) != CFArrayGetTypeID()) { 105 LOG(SessionState, "WebBackForwardList dictionary representation does not have a valid list of entries"); 106 return false; 107 } 108 109 CFIndex size = CFArrayGetCount(cfEntries); 110 if (currentIndex != static_cast<CFIndex>(NoCurrentItemIndex) && currentIndex >= size) { 111 LOG(SessionState, "WebBackForwardList dictionary representation contains an invalid current index (%ld) for the number of entries (%ld)", currentIndex, size); 112 return false; 113 } 114 115 if (currentIndex == static_cast<CFIndex>(NoCurrentItemIndex) && size) { 116 LOG(SessionState, "WebBackForwardList dictionary representation says there is no current item index, but there is a list of %ld entries - this is bogus", size); 117 return false; 118 } 119 120 BackForwardListItemVector newEntries; 121 newEntries.reserveCapacity(size); 122 for (CFIndex i = 0; i < size; ++i) { 123 CFDictionaryRef entryDictionary = (CFDictionaryRef)CFArrayGetValueAtIndex(cfEntries, i); 124 if (!entryDictionary || CFGetTypeID(entryDictionary) != CFDictionaryGetTypeID()) { 125 LOG(SessionState, "WebBackForwardList entry array does not have a valid entry at index %i", (int)i); 126 return false; 127 } 128 129 CFStringRef entryURL = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryURLKey()); 130 if (!entryURL || CFGetTypeID(entryURL) != CFStringGetTypeID()) { 131 LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid URL", (int)i); 132 return false; 133 } 134 135 CFStringRef entryTitle = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryTitleKey()); 136 if (!entryTitle || CFGetTypeID(entryTitle) != CFStringGetTypeID()) { 137 LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid title", (int)i); 138 return false; 139 } 140 141 CFStringRef originalURL = (CFStringRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryOriginalURLKey()); 142 if (!originalURL || CFGetTypeID(originalURL) != CFStringGetTypeID()) { 143 LOG(SessionState, "WebBackForwardList entry at index %i does not have a valid original URL", (int)i); 144 return false; 145 } 146 147 CFDataRef backForwardData = (CFDataRef)CFDictionaryGetValue(entryDictionary, SessionHistoryEntryDataKey()); 148 if (!backForwardData || CFGetTypeID(backForwardData) != CFDataGetTypeID()) { 149 LOG(SessionState, "WebBackForwardList entry at index %i does not have back/forward data", (int)i); 150 return false; 151 } 152 153 newEntries.append(WebBackForwardListItem::create(originalURL, entryURL, entryTitle, CFDataGetBytePtr(backForwardData), CFDataGetLength(backForwardData), generateWebBackForwardItemID())); 154 } 155 156 m_current = currentIndex; 157 m_entries = newEntries; 158 159 return true; 160 } 161 162 } // namespace WebKit 163