[iOS] Upstream PLATFORM(IOS) changes to Source/WebKit/
[WebKit-https.git] / Source / WebKit / mac / History / WebBackForwardList.mm
1 /*
2  * Copyright (C) 2005, 2007 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  *
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. 
16  *
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.
27  */
28
29 #import "WebBackForwardList.h"
30 #import "WebBackForwardListInternal.h"
31
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>
54
55 using namespace WebCore;
56
57 typedef HashMap<BackForwardList*, WebBackForwardList*> BackForwardListMap;
58
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()
62 {
63     DEFINE_STATIC_LOCAL(BackForwardListMap, staticBackForwardLists, ());
64     return staticBackForwardLists;
65 }
66
67 @implementation WebBackForwardList (WebBackForwardListInternal)
68
69 BackForwardList* core(WebBackForwardList *webBackForwardList)
70 {
71     if (!webBackForwardList)
72         return 0;
73
74     return reinterpret_cast<BackForwardList*>(webBackForwardList->_private);
75 }
76
77 WebBackForwardList *kit(BackForwardList* backForwardList)
78 {
79     if (!backForwardList)
80         return nil;
81
82     if (WebBackForwardList *webBackForwardList = backForwardLists().get(backForwardList))
83         return webBackForwardList;
84
85     return [[[WebBackForwardList alloc] initWithBackForwardList:backForwardList] autorelease];
86 }
87
88 - (id)initWithBackForwardList:(PassRefPtr<BackForwardList>)backForwardList
89 {   
90     WebCoreThreadViolationCheckRoundOne();
91     self = [super init];
92     if (!self)
93         return nil;
94
95     _private = reinterpret_cast<WebBackForwardListPrivate*>(backForwardList.leakRef());
96     backForwardLists().set(core(self), self);
97     return self;
98 }
99
100 @end
101
102 @implementation WebBackForwardList
103
104 + (void)initialize
105 {
106 #if !PLATFORM(IOS)
107     JSC::initializeThreading();
108     WTF::initializeMainThreadToProcessMainThread();
109     RunLoop::initializeMainRunLoop();
110 #endif
111     WebCoreObjCFinalizeOnMainThread(self);
112 }
113
114 - (id)init
115 {
116     return [self initWithBackForwardList:BackForwardList::create(0)];
117 }
118
119 - (void)dealloc
120 {
121     if (WebCoreObjCScheduleDeallocateOnMainThread([WebBackForwardList class], self))
122         return;
123
124     BackForwardList* backForwardList = core(self);
125     ASSERT(backForwardList);
126     if (backForwardList) {
127         ASSERT(backForwardList->closed());
128         backForwardLists().remove(backForwardList);
129         backForwardList->deref();
130     }
131
132     [super dealloc];
133 }
134
135 - (void)finalize
136 {
137     WebCoreThreadViolationCheckRoundOne();
138     BackForwardList* backForwardList = core(self);
139     ASSERT(backForwardList);
140     if (backForwardList) {
141         ASSERT(backForwardList->closed());
142         backForwardLists().remove(backForwardList);
143         backForwardList->deref();
144     }
145         
146     [super finalize];
147 }
148
149 - (void)_close
150 {
151     core(self)->close();
152 }
153
154 - (void)addItem:(WebHistoryItem *)entry
155 {
156     core(self)->addItem(core(entry));
157     
158     // Since the assumed contract with WebBackForwardList is that it retains its WebHistoryItems,
159     // the following line prevents a whole class of problems where a history item will be created in
160     // a function, added to the BFlist, then used in the rest of that function.
161     [[entry retain] autorelease];
162 }
163
164 - (void)removeItem:(WebHistoryItem *)item
165 {
166     core(self)->removeItem(core(item));
167 }
168
169 #if PLATFORM(IOS)
170
171 // FIXME: Move into WebCore the code that deals directly with WebCore::BackForwardList.
172
173 #define WebBackForwardListDictionaryEntriesKey @"entries"
174 #define WebBackForwardListDictionaryCapacityKey @"capacity"
175 #define WebBackForwardListDictionaryCurrentKey @"current"
176
177 - (NSDictionary *)dictionaryRepresentation
178 {
179     BackForwardList *coreBFList = core(self);
180     
181     HistoryItemVector historyItems = coreBFList->entries();
182     NSMutableArray *entriesArray = [[NSMutableArray alloc] init];
183     for (auto it : historyItems)
184         [entriesArray addObject:[(WebHistoryItem *)kit((*it).get()) dictionaryRepresentationIncludingChildren:NO]];
185     
186     NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
187         entriesArray, WebBackForwardListDictionaryEntriesKey,
188         [NSNumber numberWithUnsignedInt:coreBFList->current()], WebBackForwardListDictionaryCurrentKey,
189         [NSNumber numberWithInt:coreBFList->capacity()], WebBackForwardListDictionaryCapacityKey,
190         nil];
191         
192     [entriesArray release];
193     
194     return dictionary;
195 }
196
197 - (void)setToMatchDictionaryRepresentation:(NSDictionary *)dictionary
198 {
199     BackForwardList *coreBFList = core(self);
200     
201     coreBFList->setCapacity([[dictionary objectForKey:WebBackForwardListDictionaryCapacityKey] intValue]);
202     
203     for (NSDictionary *itemDictionary in [dictionary objectForKey:WebBackForwardListDictionaryEntriesKey]) {
204         WebHistoryItem *item = [[WebHistoryItem alloc] initFromDictionaryRepresentation:itemDictionary];
205         coreBFList->addItem(core(item));
206         [item release];
207     }
208
209     unsigned currentIndex = [[dictionary objectForKey:WebBackForwardListDictionaryCurrentKey] unsignedIntValue];
210     size_t listSize = coreBFList->entries().size();
211     if (currentIndex >= listSize)
212         currentIndex = listSize - 1;
213     coreBFList->setCurrent(currentIndex);
214 }
215 #endif // PLATFORM(IOS)
216
217 - (BOOL)containsItem:(WebHistoryItem *)item
218 {
219     return core(self)->containsItem(core(item));
220 }
221
222 - (void)goBack
223 {
224     core(self)->goBack();
225 }
226
227 - (void)goForward
228 {
229     core(self)->goForward();
230 }
231
232 - (void)goToItem:(WebHistoryItem *)item
233 {
234     core(self)->goToItem(core(item));
235 }
236
237 - (WebHistoryItem *)backItem
238 {
239     return [[kit(core(self)->backItem()) retain] autorelease];
240 }
241
242 - (WebHistoryItem *)currentItem
243 {
244     return [[kit(core(self)->currentItem()) retain] autorelease];
245 }
246
247 - (WebHistoryItem *)forwardItem
248 {
249     return [[kit(core(self)->forwardItem()) retain] autorelease];
250 }
251
252 static NSArray* vectorToNSArray(HistoryItemVector& list)
253 {
254     unsigned size = list.size();
255     NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:size] autorelease];
256     for (unsigned i = 0; i < size; ++i)
257         [result addObject:kit(list[i].get())];
258
259     return result;
260 }
261
262 static bool bumperCarBackForwardHackNeeded() 
263 {
264 #if !PLATFORM(IOS)
265     static bool hackNeeded = [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:@"com.freeverse.bumpercar"] && 
266         !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITHOUT_BUMPERCAR_BACK_FORWARD_QUIRK);
267
268     return hackNeeded;
269 #else
270     return false;
271 #endif
272 }
273
274 - (NSArray *)backListWithLimit:(int)limit
275 {
276     HistoryItemVector list;
277     core(self)->backListWithLimit(limit, list);
278     NSArray *result = vectorToNSArray(list);
279     
280     if (bumperCarBackForwardHackNeeded()) {
281         static NSArray *lastBackListArray = nil;
282         [lastBackListArray release];
283         lastBackListArray = [result retain];
284     }
285     
286     return result;
287 }
288
289 - (NSArray *)forwardListWithLimit:(int)limit
290 {
291     HistoryItemVector list;
292     core(self)->forwardListWithLimit(limit, list);
293     NSArray *result = vectorToNSArray(list);
294     
295     if (bumperCarBackForwardHackNeeded()) {
296         static NSArray *lastForwardListArray = nil;
297         [lastForwardListArray release];
298         lastForwardListArray = [result retain];
299     }
300     
301     return result;
302 }
303
304 - (int)capacity
305 {
306     return core(self)->capacity();
307 }
308
309 - (void)setCapacity:(int)size
310 {
311     core(self)->setCapacity(size);
312 }
313
314
315 -(NSString *)description
316 {
317     NSMutableString *result;
318     
319     result = [NSMutableString stringWithCapacity:512];
320     
321     [result appendString:@"\n--------------------------------------------\n"];    
322     [result appendString:@"WebBackForwardList:\n"];
323     
324     BackForwardList* backForwardList = core(self);
325     HistoryItemVector& entries = backForwardList->entries();
326     
327     unsigned size = entries.size();
328     for (unsigned i = 0; i < size; ++i) {
329         if (entries[i] == backForwardList->currentItem()) {
330             [result appendString:@" >>>"]; 
331         } else {
332             [result appendString:@"    "]; 
333         }   
334         [result appendFormat:@"%2d) ", i];
335         int currPos = [result length];
336         [result appendString:[kit(entries[i].get()) description]];
337
338         // shift all the contents over.  a bit slow, but this is for debugging
339         NSRange replRange = { static_cast<NSUInteger>(currPos), [result length] - currPos };
340         [result replaceOccurrencesOfString:@"\n" withString:@"\n        " options:0 range:replRange];
341         
342         [result appendString:@"\n"];
343     }
344
345     [result appendString:@"\n--------------------------------------------\n"];    
346
347     return result;
348 }
349
350 - (void)setPageCacheSize:(NSUInteger)size
351 {
352     [kit(core(self)->page()) setUsesPageCache:size != 0];
353 }
354
355 - (NSUInteger)pageCacheSize
356 {
357     return [kit(core(self)->page()) usesPageCache] ? pageCache()->capacity() : 0;
358 }
359
360 - (int)backListCount
361 {
362     return core(self)->backListCount();
363 }
364
365 - (int)forwardListCount
366 {
367     return core(self)->forwardListCount();
368 }
369
370 - (WebHistoryItem *)itemAtIndex:(int)index
371 {
372     return [[kit(core(self)->itemAtIndex(index)) retain] autorelease];
373 }
374
375 @end