2 * Copyright (C) 2005, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #import "WebBackForwardList.h"
30 #import "WebBackForwardListInternal.h"
32 #import "WebFrameInternal.h"
33 #import "WebHistoryItemInternal.h"
34 #import "WebHistoryItemPrivate.h"
35 #import "WebKitLogging.h"
36 #import "WebKitVersionChecks.h"
37 #import "WebNSObjectExtras.h"
38 #import "WebPreferencesPrivate.h"
39 #import "WebTypesInternal.h"
40 #import "WebViewPrivate.h"
41 #import <WebCore/BackForwardList.h>
42 #import <WebCore/HistoryItem.h>
43 #import <WebCore/Page.h>
44 #import <WebCore/PageCache.h>
45 #import <WebCore/Settings.h>
46 #import <WebCore/ThreadCheck.h>
47 #import <WebCore/WebCoreObjCExtras.h>
48 #import <runtime/InitializeThreading.h>
49 #import <wtf/Assertions.h>
50 #import <wtf/MainThread.h>
51 #import <wtf/RetainPtr.h>
52 #import <wtf/RunLoop.h>
53 #import <wtf/StdLibExtras.h>
55 using namespace WebCore;
57 typedef HashMap<BackForwardList*, WebBackForwardList*> BackForwardListMap;
59 // FIXME: Instead of this we could just create a class derived from BackForwardList
60 // with a pointer to a WebBackForwardList in it.
61 static BackForwardListMap& backForwardLists()
63 DEFINE_STATIC_LOCAL(BackForwardListMap, staticBackForwardLists, ());
64 return staticBackForwardLists;
67 @implementation WebBackForwardList (WebBackForwardListInternal)
69 BackForwardList* core(WebBackForwardList *webBackForwardList)
71 if (!webBackForwardList)
74 return reinterpret_cast<BackForwardList*>(webBackForwardList->_private);
77 WebBackForwardList *kit(BackForwardList* backForwardList)
82 if (WebBackForwardList *webBackForwardList = backForwardLists().get(backForwardList))
83 return webBackForwardList;
85 return [[[WebBackForwardList alloc] initWithBackForwardList:backForwardList] autorelease];
88 - (id)initWithBackForwardList:(PassRefPtr<BackForwardList>)backForwardList
90 WebCoreThreadViolationCheckRoundOne();
95 _private = reinterpret_cast<WebBackForwardListPrivate*>(backForwardList.leakRef());
96 backForwardLists().set(core(self), self);
102 @implementation WebBackForwardList
106 JSC::initializeThreading();
107 WTF::initializeMainThreadToProcessMainThread();
108 RunLoop::initializeMainRunLoop();
109 WebCoreObjCFinalizeOnMainThread(self);
114 return [self initWithBackForwardList:BackForwardList::create(0)];
119 if (WebCoreObjCScheduleDeallocateOnMainThread([WebBackForwardList class], self))
122 BackForwardList* backForwardList = core(self);
123 ASSERT(backForwardList);
124 if (backForwardList) {
125 ASSERT(backForwardList->closed());
126 backForwardLists().remove(backForwardList);
127 backForwardList->deref();
135 WebCoreThreadViolationCheckRoundOne();
136 BackForwardList* backForwardList = core(self);
137 ASSERT(backForwardList);
138 if (backForwardList) {
139 ASSERT(backForwardList->closed());
140 backForwardLists().remove(backForwardList);
141 backForwardList->deref();
152 - (void)addItem:(WebHistoryItem *)entry
154 core(self)->addItem(core(entry));
156 // Since the assumed contract with WebBackForwardList is that it retains its WebHistoryItems,
157 // the following line prevents a whole class of problems where a history item will be created in
158 // a function, added to the BFlist, then used in the rest of that function.
159 [[entry retain] autorelease];
162 - (void)removeItem:(WebHistoryItem *)item
164 core(self)->removeItem(core(item));
167 - (BOOL)containsItem:(WebHistoryItem *)item
169 return core(self)->containsItem(core(item));
174 core(self)->goBack();
179 core(self)->goForward();
182 - (void)goToItem:(WebHistoryItem *)item
184 core(self)->goToItem(core(item));
187 - (WebHistoryItem *)backItem
189 return [[kit(core(self)->backItem()) retain] autorelease];
192 - (WebHistoryItem *)currentItem
194 return [[kit(core(self)->currentItem()) retain] autorelease];
197 - (WebHistoryItem *)forwardItem
199 return [[kit(core(self)->forwardItem()) retain] autorelease];
202 static NSArray* vectorToNSArray(HistoryItemVector& list)
204 unsigned size = list.size();
205 NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:size] autorelease];
206 for (unsigned i = 0; i < size; ++i)
207 [result addObject:kit(list[i].get())];
212 static bool bumperCarBackForwardHackNeeded()
214 static bool hackNeeded = [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.freeverse.bumpercar"] &&
215 !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITHOUT_BUMPERCAR_BACK_FORWARD_QUIRK);
220 - (NSArray *)backListWithLimit:(int)limit
222 HistoryItemVector list;
223 core(self)->backListWithLimit(limit, list);
224 NSArray *result = vectorToNSArray(list);
226 if (bumperCarBackForwardHackNeeded()) {
227 static NSArray *lastBackListArray = nil;
228 [lastBackListArray release];
229 lastBackListArray = [result retain];
235 - (NSArray *)forwardListWithLimit:(int)limit
237 HistoryItemVector list;
238 core(self)->forwardListWithLimit(limit, list);
239 NSArray *result = vectorToNSArray(list);
241 if (bumperCarBackForwardHackNeeded()) {
242 static NSArray *lastForwardListArray = nil;
243 [lastForwardListArray release];
244 lastForwardListArray = [result retain];
252 return core(self)->capacity();
255 - (void)setCapacity:(int)size
257 core(self)->setCapacity(size);
261 -(NSString *)description
263 NSMutableString *result;
265 result = [NSMutableString stringWithCapacity:512];
267 [result appendString:@"\n--------------------------------------------\n"];
268 [result appendString:@"WebBackForwardList:\n"];
270 BackForwardList* backForwardList = core(self);
271 HistoryItemVector& entries = backForwardList->entries();
273 unsigned size = entries.size();
274 for (unsigned i = 0; i < size; ++i) {
275 if (entries[i] == backForwardList->currentItem()) {
276 [result appendString:@" >>>"];
278 [result appendString:@" "];
280 [result appendFormat:@"%2d) ", i];
281 int currPos = [result length];
282 [result appendString:[kit(entries[i].get()) description]];
284 // shift all the contents over. a bit slow, but this is for debugging
285 NSRange replRange = { static_cast<NSUInteger>(currPos), [result length] - currPos };
286 [result replaceOccurrencesOfString:@"\n" withString:@"\n " options:0 range:replRange];
288 [result appendString:@"\n"];
291 [result appendString:@"\n--------------------------------------------\n"];
296 - (void)setPageCacheSize:(NSUInteger)size
298 [kit(core(self)->page()) setUsesPageCache:size != 0];
301 - (NSUInteger)pageCacheSize
303 return [kit(core(self)->page()) usesPageCache] ? pageCache()->capacity() : 0;
308 return core(self)->backListCount();
311 - (int)forwardListCount
313 return core(self)->forwardListCount();
316 - (WebHistoryItem *)itemAtIndex:(int)index
318 return [[kit(core(self)->itemAtIndex(index)) retain] autorelease];