Made -[WebHistoryItem copyWithZone:] allocate an object of the same class as the...
[WebKit-https.git] / Source / WebKit / mac / History / WebHistoryItem.mm
1 /*
2  * Copyright (C) 2005, 2007, 2008 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 "WebHistoryItemInternal.h"
30 #import "WebHistoryItemPrivate.h"
31
32 #import "WebFrameInternal.h"
33 #import "WebFrameView.h"
34 #import "WebHTMLViewInternal.h"
35 #import "WebIconDatabase.h"
36 #import "WebKitLogging.h"
37 #import "WebKitNSStringExtras.h"
38 #import "WebNSArrayExtras.h"
39 #import "WebNSDictionaryExtras.h"
40 #import "WebNSObjectExtras.h"
41 #import "WebNSURLExtras.h"
42 #import "WebNSURLRequestExtras.h"
43 #import "WebNSViewExtras.h"
44 #import "WebPluginController.h"
45 #import "WebTypesInternal.h"
46 #import <WebCore/HistoryItem.h>
47 #import <WebCore/Image.h>
48 #import <WebCore/KURL.h>
49 #import <WebCore/PageCache.h>
50 #import <WebCore/PlatformString.h>
51 #import <WebCore/ThreadCheck.h>
52 #import <WebCore/WebCoreObjCExtras.h>
53 #import <runtime/InitializeThreading.h>
54 #import <wtf/Assertions.h>
55 #import <wtf/MainThread.h>
56 #import <wtf/StdLibExtras.h>
57
58 // Private keys used in the WebHistoryItem's dictionary representation.
59 // see 3245793 for explanation of "lastVisitedDate"
60 static NSString *lastVisitedTimeIntervalKey = @"lastVisitedDate";
61 static NSString *visitCountKey = @"visitCount";
62 static NSString *titleKey = @"title";
63 static NSString *childrenKey = @"children";
64 static NSString *displayTitleKey = @"displayTitle";
65 static NSString *lastVisitWasFailureKey = @"lastVisitWasFailure";
66 static NSString *lastVisitWasHTTPNonGetKey = @"lastVisitWasHTTPNonGet";
67 static NSString *redirectURLsKey = @"redirectURLs";
68 static NSString *dailyVisitCountKey = @"D"; // short key to save space
69 static NSString *weeklyVisitCountKey = @"W"; // short key to save space
70
71 // Notification strings.
72 NSString *WebHistoryItemChangedNotification = @"WebHistoryItemChangedNotification";
73
74 using namespace WebCore;
75 using namespace std;
76
77 typedef HashMap<HistoryItem*, WebHistoryItem*> HistoryItemMap;
78
79 static inline WebHistoryItemPrivate* kitPrivate(WebCoreHistoryItem* list) { return (WebHistoryItemPrivate*)list; }
80 static inline WebCoreHistoryItem* core(WebHistoryItemPrivate* list) { return (WebCoreHistoryItem*)list; }
81
82 static HistoryItemMap& historyItemWrappers()
83 {
84     DEFINE_STATIC_LOCAL(HistoryItemMap, historyItemWrappers, ());
85     return historyItemWrappers;
86 }
87
88 void WKNotifyHistoryItemChanged(HistoryItem*)
89 {
90     [[NSNotificationCenter defaultCenter]
91         postNotificationName:WebHistoryItemChangedNotification object:nil userInfo:nil];
92 }
93
94 @implementation WebHistoryItem
95
96 + (void)initialize
97 {
98     JSC::initializeThreading();
99     WTF::initializeMainThreadToProcessMainThread();
100     WebCoreObjCFinalizeOnMainThread(self);
101 }
102
103 - (id)init
104 {
105     return [self initWithWebCoreHistoryItem:HistoryItem::create()];
106 }
107
108 - (id)initWithURLString:(NSString *)URLString title:(NSString *)title lastVisitedTimeInterval:(NSTimeInterval)time
109 {
110     WebCoreThreadViolationCheckRoundOne();
111     return [self initWithWebCoreHistoryItem:HistoryItem::create(URLString, title, time)];
112 }
113
114 - (void)dealloc
115 {
116     if (WebCoreObjCScheduleDeallocateOnMainThread([WebHistoryItem class], self))
117         return;
118
119     if (_private) {
120         HistoryItem* coreItem = core(_private);
121         coreItem->deref();
122         historyItemWrappers().remove(coreItem);
123     }
124     [super dealloc];
125 }
126
127 - (void)finalize
128 {
129     WebCoreThreadViolationCheckRoundOne();
130     // FIXME: ~HistoryItem is what releases the history item's icon from the icon database
131     // It's probably not good to release icons from the database only when the object is garbage-collected. 
132     // Need to change design so this happens at a predictable time.
133     if (_private) {
134         HistoryItem* coreItem = core(_private);
135         coreItem->deref();
136         historyItemWrappers().remove(coreItem);
137     }
138     [super finalize];
139 }
140
141 - (id)copyWithZone:(NSZone *)zone
142 {
143     WebCoreThreadViolationCheckRoundOne();
144     WebHistoryItem *copy = [[[self class] alloc] initWithWebCoreHistoryItem:core(_private)->copy()];
145     historyItemWrappers().set(core(copy->_private), copy);
146
147     return copy;
148 }
149
150 // FIXME: Need to decide if this class ever returns URLs and decide on the name of this method
151 - (NSString *)URLString
152 {
153     ASSERT_MAIN_THREAD();
154     return nsStringNilIfEmpty(core(_private)->urlString());
155 }
156
157 // The first URL we loaded to get to where this history item points.  Includes both client
158 // and server redirects.
159 - (NSString *)originalURLString
160 {
161     ASSERT_MAIN_THREAD();
162     return nsStringNilIfEmpty(core(_private)->originalURLString());
163 }
164
165 - (NSString *)title
166 {
167     ASSERT_MAIN_THREAD();
168     return nsStringNilIfEmpty(core(_private)->title());
169 }
170
171 - (void)setAlternateTitle:(NSString *)alternateTitle
172 {
173     core(_private)->setAlternateTitle(alternateTitle);
174 }
175
176 - (NSString *)alternateTitle
177 {
178     return nsStringNilIfEmpty(core(_private)->alternateTitle());
179 }
180
181 - (NSImage *)icon
182 {
183     return [[WebIconDatabase sharedIconDatabase] iconForURL:[self URLString] withSize:WebIconSmallSize];
184 }
185
186 - (NSTimeInterval)lastVisitedTimeInterval
187 {
188     ASSERT_MAIN_THREAD();
189     return core(_private)->lastVisitedTime();
190 }
191
192 - (NSUInteger)hash
193 {
194     return [(NSString*)core(_private)->urlString() hash];
195 }
196
197 - (BOOL)isEqual:(id)anObject
198 {
199     ASSERT_MAIN_THREAD();
200     if (![anObject isMemberOfClass:[WebHistoryItem class]]) {
201         return NO;
202     }
203     
204     return core(_private)->urlString() == core(((WebHistoryItem*)anObject)->_private)->urlString();
205 }
206
207 - (NSString *)description
208 {
209     ASSERT_MAIN_THREAD();
210     HistoryItem* coreItem = core(_private);
211     NSMutableString *result = [NSMutableString stringWithFormat:@"%@ %@", [super description], (NSString*)coreItem->urlString()];
212     if (!coreItem->target().isEmpty()) {
213         NSString *target = coreItem->target();
214         [result appendFormat:@" in \"%@\"", target];
215     }
216     if (coreItem->isTargetItem()) {
217         [result appendString:@" *target*"];
218     }
219     if (coreItem->formData()) {
220         [result appendString:@" *POST*"];
221     }
222     
223     if (coreItem->children().size()) {
224         const HistoryItemVector& children = coreItem->children();
225         int currPos = [result length];
226         unsigned size = children.size();        
227         for (unsigned i = 0; i < size; ++i) {
228             WebHistoryItem *child = kit(children[i].get());
229             [result appendString:@"\n"];
230             [result appendString:[child description]];
231         }
232         // shift all the contents over.  A bit slow, but hey, this is for debugging.
233         NSRange replRange = {currPos, [result length]-currPos};
234         [result replaceOccurrencesOfString:@"\n" withString:@"\n    " options:0 range:replRange];
235     }
236     
237     return result;
238 }
239
240 @end
241
242 @interface WebWindowWatcher : NSObject
243 @end
244
245
246 @implementation WebHistoryItem (WebInternal)
247
248 HistoryItem* core(WebHistoryItem *item)
249 {
250     if (!item)
251         return 0;
252     
253     ASSERT(historyItemWrappers().get(core(item->_private)) == item);
254
255     return core(item->_private);
256 }
257
258 WebHistoryItem *kit(HistoryItem* item)
259 {
260     if (!item)
261         return nil;
262         
263     WebHistoryItem *kitItem = historyItemWrappers().get(item);
264     if (kitItem)
265         return kitItem;
266     
267     return [[[WebHistoryItem alloc] initWithWebCoreHistoryItem:item] autorelease];
268 }
269
270 + (WebHistoryItem *)entryWithURL:(NSURL *)URL
271 {
272     return [[[self alloc] initWithURL:URL title:nil] autorelease];
273 }
274
275 static WebWindowWatcher *_windowWatcher = nil;
276
277 + (void)initWindowWatcherIfNecessary
278 {
279     if (_windowWatcher)
280         return;
281     _windowWatcher = [[WebWindowWatcher alloc] init];
282     [[NSNotificationCenter defaultCenter] addObserver:_windowWatcher selector:@selector(windowWillClose:)
283         name:NSWindowWillCloseNotification object:nil];
284 }
285
286 - (id)initWithURL:(NSURL *)URL target:(NSString *)target parent:(NSString *)parent title:(NSString *)title
287 {
288     return [self initWithWebCoreHistoryItem:HistoryItem::create(URL, target, parent, title)];
289 }
290
291 - (id)initWithURLString:(NSString *)URLString title:(NSString *)title displayTitle:(NSString *)displayTitle lastVisitedTimeInterval:(NSTimeInterval)time
292 {
293     return [self initWithWebCoreHistoryItem:HistoryItem::create(URLString, title, displayTitle, time)];
294 }
295
296 - (id)initWithWebCoreHistoryItem:(PassRefPtr<HistoryItem>)item
297 {   
298     WebCoreThreadViolationCheckRoundOne();
299     // Need to tell WebCore what function to call for the 
300     // "History Item has Changed" notification - no harm in doing this
301     // everytime a WebHistoryItem is created
302     // Note: We also do this in [WebFrameView initWithFrame:] where we do
303     // other "init before WebKit is used" type things
304     WebCore::notifyHistoryItemChanged = WKNotifyHistoryItemChanged;
305     
306     self = [super init];
307     
308     _private = kitPrivate(item.leakRef());
309     ASSERT(!historyItemWrappers().get(core(_private)));
310     historyItemWrappers().set(core(_private), self);
311     return self;
312 }
313
314 - (void)setTitle:(NSString *)title
315 {
316     core(_private)->setTitle(title);
317 }
318
319 - (void)setVisitCount:(int)count
320 {
321     core(_private)->setVisitCount(count);
322 }
323
324 - (void)setViewState:(id)statePList
325 {
326     core(_private)->setViewState(statePList);
327 }
328
329 - (void)_mergeAutoCompleteHints:(WebHistoryItem *)otherItem
330 {
331     ASSERT_ARG(otherItem, otherItem);
332     core(_private)->mergeAutoCompleteHints(core(otherItem->_private));
333 }
334
335 - (id)initFromDictionaryRepresentation:(NSDictionary *)dict
336 {
337     ASSERT_MAIN_THREAD();
338     NSString *URLString = [dict _webkit_stringForKey:@""];
339     NSString *title = [dict _webkit_stringForKey:titleKey];
340
341     // Do an existence check to avoid calling doubleValue on a nil string. Leave
342     // time interval at 0 if there's no value in dict.
343     NSString *timeIntervalString = [dict _webkit_stringForKey:lastVisitedTimeIntervalKey];
344     NSTimeInterval lastVisited = timeIntervalString == nil ? 0 : [timeIntervalString doubleValue];
345
346     self = [self initWithURLString:URLString title:title displayTitle:[dict _webkit_stringForKey:displayTitleKey] lastVisitedTimeInterval:lastVisited];
347     
348     // Check if we've read a broken URL from the file that has non-Latin1 chars.  If so, try to convert
349     // as if it was from user typing.
350     if (![URLString canBeConvertedToEncoding:NSISOLatin1StringEncoding]) {
351         NSURL *tempURL = [NSURL _web_URLWithUserTypedString:URLString];
352         ASSERT(tempURL);
353         NSString *newURLString = [tempURL _web_originalDataAsString];
354         core(_private)->setURLString(newURLString);
355         core(_private)->setOriginalURLString(newURLString);
356     } 
357
358     int visitCount = [dict _webkit_intForKey:visitCountKey];
359     
360     // Can't trust data on disk, and we've had at least one report of this (<rdar://6572300>).
361     if (visitCount < 0) {
362         LOG_ERROR("visit count for history item \"%@\" is negative (%d), will be reset to 1", URLString, visitCount);
363         visitCount = 1;
364     }
365     core(_private)->setVisitCount(visitCount);
366
367     if ([dict _webkit_boolForKey:lastVisitWasFailureKey])
368         core(_private)->setLastVisitWasFailure(true);
369     
370     BOOL lastVisitWasHTTPNonGet = [dict _webkit_boolForKey:lastVisitWasHTTPNonGetKey];
371     NSString *tempURLString = [URLString lowercaseString];
372     if (lastVisitWasHTTPNonGet && ([tempURLString hasPrefix:@"http:"] || [tempURLString hasPrefix:@"https:"]))
373         core(_private)->setLastVisitWasHTTPNonGet(lastVisitWasHTTPNonGet);
374
375     if (NSArray *redirectURLs = [dict _webkit_arrayForKey:redirectURLsKey]) {
376         NSUInteger size = [redirectURLs count];
377         OwnPtr<Vector<String> > redirectURLsVector = adoptPtr(new Vector<String>(size));
378         for (NSUInteger i = 0; i < size; ++i)
379             (*redirectURLsVector)[i] = String([redirectURLs _webkit_stringAtIndex:i]);
380         core(_private)->setRedirectURLs(redirectURLsVector.release());
381     }
382
383     NSArray *dailyCounts = [dict _webkit_arrayForKey:dailyVisitCountKey];
384     NSArray *weeklyCounts = [dict _webkit_arrayForKey:weeklyVisitCountKey];
385     if (dailyCounts || weeklyCounts) {
386         Vector<int> coreDailyCounts([dailyCounts count]);
387         Vector<int> coreWeeklyCounts([weeklyCounts count]);
388
389         // Daily and weekly counts < 0 are errors in the data read from disk, so reset to 0.
390         for (size_t i = 0; i < coreDailyCounts.size(); ++i)
391             coreDailyCounts[i] = max([[dailyCounts _webkit_numberAtIndex:i] intValue], 0);
392         for (size_t i = 0; i < coreWeeklyCounts.size(); ++i)
393             coreWeeklyCounts[i] = max([[weeklyCounts _webkit_numberAtIndex:i] intValue], 0);
394     
395         core(_private)->adoptVisitCounts(coreDailyCounts, coreWeeklyCounts);
396     }
397
398     NSArray *childDicts = [dict objectForKey:childrenKey];
399     if (childDicts) {
400         for (int i = [childDicts count] - 1; i >= 0; i--) {
401             WebHistoryItem *child = [[WebHistoryItem alloc] initFromDictionaryRepresentation:[childDicts objectAtIndex:i]];
402             core(_private)->addChildItem(core(child->_private));
403             [child release];
404         }
405     }
406
407     return self;
408 }
409
410 - (NSPoint)scrollPoint
411 {
412     ASSERT_MAIN_THREAD();
413     return core(_private)->scrollPoint();
414 }
415
416 - (void)_visitedWithTitle:(NSString *)title increaseVisitCount:(BOOL)increaseVisitCount
417 {
418     core(_private)->visited(title, [NSDate timeIntervalSinceReferenceDate], increaseVisitCount ? IncreaseVisitCount : DoNotIncreaseVisitCount);
419 }
420
421 - (void)_recordInitialVisit
422 {
423     core(_private)->recordInitialVisit();
424 }
425
426 @end
427
428 @implementation WebHistoryItem (WebPrivate)
429
430 - (id)initWithURL:(NSURL *)URL title:(NSString *)title
431 {
432     return [self initWithURLString:[URL _web_originalDataAsString] title:title lastVisitedTimeInterval:0];
433 }
434
435 - (NSDictionary *)dictionaryRepresentation
436 {
437     ASSERT_MAIN_THREAD();
438     NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:8];
439
440     HistoryItem* coreItem = core(_private);
441     
442     if (!coreItem->urlString().isEmpty())
443         [dict setObject:(NSString*)coreItem->urlString() forKey:@""];
444     if (!coreItem->title().isEmpty())
445         [dict setObject:(NSString*)coreItem->title() forKey:titleKey];
446     if (!coreItem->alternateTitle().isEmpty())
447         [dict setObject:(NSString*)coreItem->alternateTitle() forKey:displayTitleKey];
448     if (coreItem->lastVisitedTime() != 0.0) {
449         // Store as a string to maintain backward compatibility. (See 3245793)
450         [dict setObject:[NSString stringWithFormat:@"%.1lf", coreItem->lastVisitedTime()]
451                  forKey:lastVisitedTimeIntervalKey];
452     }
453     if (coreItem->visitCount())
454         [dict setObject:[NSNumber numberWithInt:coreItem->visitCount()] forKey:visitCountKey];
455     if (coreItem->lastVisitWasFailure())
456         [dict setObject:[NSNumber numberWithBool:YES] forKey:lastVisitWasFailureKey];
457     if (coreItem->lastVisitWasHTTPNonGet()) {
458         ASSERT(coreItem->urlString().startsWith("http:", false) || coreItem->urlString().startsWith("https:", false));
459         [dict setObject:[NSNumber numberWithBool:YES] forKey:lastVisitWasHTTPNonGetKey];
460     }
461     if (Vector<String>* redirectURLs = coreItem->redirectURLs()) {
462         size_t size = redirectURLs->size();
463         ASSERT(size);
464         NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:size];
465         for (size_t i = 0; i < size; ++i)
466             [result addObject:(NSString*)redirectURLs->at(i)];
467         [dict setObject:result forKey:redirectURLsKey];
468         [result release];
469     }
470     
471     const Vector<int>& dailyVisitCounts = coreItem->dailyVisitCounts();
472     if (dailyVisitCounts.size()) {
473         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:13];
474         for (size_t i = 0; i < dailyVisitCounts.size(); ++i)
475             [array addObject:[NSNumber numberWithInt:dailyVisitCounts[i]]];
476         [dict setObject:array forKey:dailyVisitCountKey];
477         [array release];
478     }
479     
480     const Vector<int>& weeklyVisitCounts = coreItem->weeklyVisitCounts();
481     if (weeklyVisitCounts.size()) {
482         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
483         for (size_t i = 0; i < weeklyVisitCounts.size(); ++i)
484             [array addObject:[NSNumber numberWithInt:weeklyVisitCounts[i]]];
485         [dict setObject:array forKey:weeklyVisitCountKey];
486         [array release];
487     }    
488     
489     if (coreItem->children().size()) {
490         const HistoryItemVector& children = coreItem->children();
491         NSMutableArray *childDicts = [NSMutableArray arrayWithCapacity:children.size()];
492         
493         for (int i = children.size() - 1; i >= 0; i--)
494             [childDicts addObject:[kit(children[i].get()) dictionaryRepresentation]];
495         [dict setObject: childDicts forKey:childrenKey];
496     }
497
498     return dict;
499 }
500
501 - (NSString *)target
502 {
503     ASSERT_MAIN_THREAD();
504     return nsStringNilIfEmpty(core(_private)->target());
505 }
506
507 - (BOOL)isTargetItem
508 {
509     return core(_private)->isTargetItem();
510 }
511
512 - (int)visitCount
513 {
514     ASSERT_MAIN_THREAD();
515     return core(_private)->visitCount();
516 }
517
518 - (NSString *)RSSFeedReferrer
519 {
520     return nsStringNilIfEmpty(core(_private)->referrer());
521 }
522
523 - (void)setRSSFeedReferrer:(NSString *)referrer
524 {
525     core(_private)->setReferrer(referrer);
526 }
527
528 - (NSArray *)children
529 {
530     ASSERT_MAIN_THREAD();
531     const HistoryItemVector& children = core(_private)->children();
532     if (!children.size())
533         return nil;
534
535     unsigned size = children.size();
536     NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:size] autorelease];
537     
538     for (unsigned i = 0; i < size; ++i)
539         [result addObject:kit(children[i].get())];
540     
541     return result;
542 }
543
544 - (void)setAlwaysAttemptToUsePageCache:(BOOL)flag
545 {
546     // Safari 2.0 uses this for SnapBack, so we stub it out to avoid a crash.
547 }
548
549 - (NSURL *)URL
550 {
551     ASSERT_MAIN_THREAD();
552     const KURL& url = core(_private)->url();
553     if (url.isEmpty())
554         return nil;
555     return url;
556 }
557
558 // This should not be called directly for WebHistoryItems that are already included
559 // in WebHistory. Use -[WebHistory setLastVisitedTimeInterval:forItem:] instead.
560 - (void)_setLastVisitedTimeInterval:(NSTimeInterval)time
561 {
562     core(_private)->setLastVisitedTime(time);
563 }
564
565 // FIXME: <rdar://problem/4880065> - Push Global History into WebCore
566 // Once that task is complete, this accessor can go away
567 - (NSCalendarDate *)_lastVisitedDate
568 {
569     ASSERT_MAIN_THREAD();
570     return [[[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:core(_private)->lastVisitedTime()] autorelease];
571 }
572
573 - (WebHistoryItem *)targetItem
574 {    
575     ASSERT_MAIN_THREAD();
576     return kit(core(_private)->targetItem());
577 }
578
579 + (void)_releaseAllPendingPageCaches
580 {
581     pageCache()->releaseAutoreleasedPagesNow();
582 }
583
584 - (id)_transientPropertyForKey:(NSString *)key
585 {
586     return core(_private)->getTransientProperty(key);
587 }
588
589 - (void)_setTransientProperty:(id)property forKey:(NSString *)key
590 {
591     core(_private)->setTransientProperty(key, property);
592 }
593
594 - (BOOL)lastVisitWasFailure
595 {
596     return core(_private)->lastVisitWasFailure();
597 }
598
599 - (void)_setLastVisitWasFailure:(BOOL)failure
600 {
601     core(_private)->setLastVisitWasFailure(failure);
602 }
603
604 - (BOOL)_lastVisitWasHTTPNonGet
605 {
606     return core(_private)->lastVisitWasHTTPNonGet();
607 }
608
609 - (NSArray *)_redirectURLs
610 {
611     Vector<String>* redirectURLs = core(_private)->redirectURLs();
612     if (!redirectURLs)
613         return nil;
614
615     size_t size = redirectURLs->size();
616     ASSERT(size);
617     NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:size];
618     for (size_t i = 0; i < size; ++i)
619         [result addObject:(NSString*)redirectURLs->at(i)];
620     return [result autorelease];
621 }
622
623 - (size_t)_getDailyVisitCounts:(const int**)counts
624 {
625     HistoryItem* coreItem = core(_private);
626     *counts = coreItem->dailyVisitCounts().data();
627     return coreItem->dailyVisitCounts().size();
628 }
629
630 - (size_t)_getWeeklyVisitCounts:(const int**)counts
631 {
632     HistoryItem* coreItem = core(_private);
633     *counts = coreItem->weeklyVisitCounts().data();
634     return coreItem->weeklyVisitCounts().size();
635 }
636
637 @end
638
639
640 // FIXME: <rdar://problem/4886761>.
641 // This is a bizarre policy. We flush the page caches ANY time ANY window is closed?
642
643 @implementation WebWindowWatcher
644
645 - (void)windowWillClose:(NSNotification *)notification
646 {
647     if (!pthread_main_np()) {
648         [self performSelectorOnMainThread:_cmd withObject:notification waitUntilDone:NO];
649         return;
650     }
651
652     pageCache()->releaseAutoreleasedPagesNow();
653 }
654
655 @end