1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/*
* Copyright (C) 2005, 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "WebHistoryItemInternal.h"
#import "WebHistoryItemPrivate.h"
#import "WebFrameInternal.h"
#import "WebFrameView.h"
#import "WebHTMLViewInternal.h"
#import "WebIconDatabase.h"
#import "WebKitLogging.h"
#import "WebKitNSStringExtras.h"
#import "WebNSDictionaryExtras.h"
#import "WebNSObjectExtras.h"
#import "WebNSURLExtras.h"
#import "WebNSURLRequestExtras.h"
#import "WebNSViewExtras.h"
#import "WebPluginController.h"
#import "WebTypesInternal.h"
#import <WebCore/CachedPage.h>
#import <WebCore/HistoryItem.h>
#import <WebCore/Image.h>
#import <WebCore/KURL.h>
#import <WebCore/PageCache.h>
#import <WebCore/PlatformString.h>
#import <WebCore/ThreadCheck.h>
#import <WebCore/WebCoreObjCExtras.h>
#import <wtf/Assertions.h>
// Private keys used in the WebHistoryItem's dictionary representation.
// see 3245793 for explanation of "lastVisitedDate"
static NSString *WebLastVisitedTimeIntervalKey = @"lastVisitedDate";
static NSString *WebVisitCountKey = @"visitCount";
static NSString *WebTitleKey = @"title";
static NSString *WebChildrenKey = @"children";
static NSString *WebDisplayTitleKey = @"displayTitle";
// Notification strings.
NSString *WebHistoryItemChangedNotification = @"WebHistoryItemChangedNotification";
using namespace WebCore;
static inline WebHistoryItemPrivate* kitPrivate(WebCoreHistoryItem* list) { return (WebHistoryItemPrivate*)list; }
static inline WebCoreHistoryItem* core(WebHistoryItemPrivate* list) { return (WebCoreHistoryItem*)list; }
HashMap<HistoryItem*, WebHistoryItem*>& historyItemWrappers()
{
static HashMap<HistoryItem*, WebHistoryItem*> historyItemWrappers;
return historyItemWrappers;
}
void WKNotifyHistoryItemChanged()
{
[[NSNotificationCenter defaultCenter]
postNotificationName:WebHistoryItemChangedNotification object:nil userInfo:nil];
}
@implementation WebHistoryItem
#ifndef BUILDING_ON_TIGER
+ (void)initialize
{
WebCoreObjCFinalizeOnMainThread(self);
}
#endif
- (id)init
{
return [self initWithWebCoreHistoryItem:HistoryItem::create()];
}
- (id)initWithURLString:(NSString *)URLString title:(NSString *)title lastVisitedTimeInterval:(NSTimeInterval)time
{
WebCoreThreadViolationCheck();
return [self initWithWebCoreHistoryItem:HistoryItem::create(URLString, title, time)];
}
- (void)dealloc
{
if (WebCoreObjCScheduleDeallocateOnMainThread([WebHistoryItem class], self))
return;
if (_private) {
HistoryItem* coreItem = core(_private);
coreItem->deref();
historyItemWrappers().remove(coreItem);
}
[super dealloc];
}
- (void)finalize
{
WebCoreThreadViolationCheck();
// FIXME: ~HistoryItem is what releases the history item's icon from the icon database
// It's probably not good to release icons from the database only when the object is garbage-collected.
// Need to change design so this happens at a predictable time.
if (_private) {
HistoryItem* coreItem = core(_private);
coreItem->deref();
historyItemWrappers().remove(coreItem);
}
[super finalize];
}
- (id)copyWithZone:(NSZone *)zone
{
WebCoreThreadViolationCheck();
WebHistoryItem *copy = (WebHistoryItem *)NSCopyObject(self, 0, zone);
RefPtr<HistoryItem> item = core(_private)->copy();
copy->_private = kitPrivate(item.get());
historyItemWrappers().set(item.release().releaseRef(), copy);
return copy;
}
// FIXME: Need to decide if this class ever returns URLs and decide on the name of this method
- (NSString *)URLString
{
ASSERT_MAIN_THREAD();
return nsStringNilIfEmpty(core(_private)->urlString());
}
// The first URL we loaded to get to where this history item points. Includes both client
// and server redirects.
- (NSString *)originalURLString
{
ASSERT_MAIN_THREAD();
return nsStringNilIfEmpty(core(_private)->originalURLString());
}
- (NSString *)title
{
ASSERT_MAIN_THREAD();
return nsStringNilIfEmpty(core(_private)->title());
}
- (void)setAlternateTitle:(NSString *)alternateTitle
{
core(_private)->setAlternateTitle(alternateTitle);
}
- (NSString *)alternateTitle;
{
return nsStringNilIfEmpty(core(_private)->alternateTitle());
}
- (NSImage *)icon
{
return [[WebIconDatabase sharedIconDatabase] iconForURL:[self URLString] withSize:WebIconSmallSize];
// FIXME: Ideally, this code should simply be the following -
// return core(_private)->icon()->getNSImage();
// Once radar -
// <rdar://problem/4906567> - NSImage returned from WebCore::Image may be incorrect size
// is resolved
}
- (NSTimeInterval)lastVisitedTimeInterval
{
ASSERT_MAIN_THREAD();
return core(_private)->lastVisitedTime();
}
- (NSUInteger)hash
{
return [(NSString*)core(_private)->urlString() hash];
}
- (BOOL)isEqual:(id)anObject
{
ASSERT_MAIN_THREAD();
if (![anObject isMemberOfClass:[WebHistoryItem class]]) {
return NO;
}
return core(_private)->urlString() == core(((WebHistoryItem*)anObject)->_private)->urlString();
}
- (NSString *)description
{
ASSERT_MAIN_THREAD();
HistoryItem* coreItem = core(_private);
NSMutableString *result = [NSMutableString stringWithFormat:@"%@ %@", [super description], (NSString*)coreItem->urlString()];
if (coreItem->target()) {
[result appendFormat:@" in \"%@\"", (NSString*)coreItem->target()];
}
if (coreItem->isTargetItem()) {
[result appendString:@" *target*"];
}
if (coreItem->formData()) {
[result appendString:@" *POST*"];
}
if (coreItem->children().size()) {
const HistoryItemVector& children = coreItem->children();
int currPos = [result length];
unsigned size = children.size();
for (unsigned i = 0; i < size; ++i) {
WebHistoryItem *child = kit(children[i].get());
[result appendString:@"\n"];
[result appendString:[child description]];
}
// shift all the contents over. A bit slow, but hey, this is for debugging.
NSRange replRange = {currPos, [result length]-currPos};
[result replaceOccurrencesOfString:@"\n" withString:@"\n " options:0 range:replRange];
}
return result;
}
@end
@interface WebWindowWatcher : NSObject
@end
@implementation WebHistoryItem (WebInternal)
HistoryItem* core(WebHistoryItem *item)
{
if (!item)
return 0;
return core(item->_private);
}
WebHistoryItem *kit(HistoryItem* item)
{
if (!item)
return nil;
WebHistoryItem *kitItem = historyItemWrappers().get(item);
if (kitItem)
return kitItem;
return [[[WebHistoryItem alloc] initWithWebCoreHistoryItem:item] autorelease];
}
+ (WebHistoryItem *)entryWithURL:(NSURL *)URL
{
return [[[self alloc] initWithURL:URL title:nil] autorelease];
}
static WebWindowWatcher *_windowWatcher = nil;
+ (void)initWindowWatcherIfNecessary
{
if (_windowWatcher)
return;
_windowWatcher = [[WebWindowWatcher alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:_windowWatcher selector:@selector(windowWillClose:)
name:NSWindowWillCloseNotification object:nil];
}
- (id)initWithURL:(NSURL *)URL target:(NSString *)target parent:(NSString *)parent title:(NSString *)title
{
return [self initWithWebCoreHistoryItem:HistoryItem::create(URL, target, parent, title)];
}
- (id)initWithURLString:(NSString *)URLString title:(NSString *)title displayTitle:(NSString *)displayTitle lastVisitedTimeInterval:(NSTimeInterval)time
{
return [self initWithWebCoreHistoryItem:HistoryItem::create(URLString, title, displayTitle, time)];
}
- (id)initWithWebCoreHistoryItem:(PassRefPtr<HistoryItem>)item
{
WebCoreThreadViolationCheck();
// Need to tell WebCore what function to call for the
// "History Item has Changed" notification - no harm in doing this
// everytime a WebHistoryItem is created
// Note: We also do this in [WebFrameView initWithFrame:] where we do
// other "init before WebKit is used" type things
WebCore::notifyHistoryItemChanged = WKNotifyHistoryItemChanged;
self = [super init];
_private = kitPrivate(item.releaseRef());
ASSERT(!historyItemWrappers().get(core(_private)));
historyItemWrappers().set(core(_private), self);
return self;
}
- (void)setTitle:(NSString *)title
{
core(_private)->setTitle(title);
}
- (void)setVisitCount:(int)count
{
core(_private)->setVisitCount(count);
}
- (void)setViewState:(id)statePList;
{
core(_private)->setViewState(statePList);
}
- (void)_mergeAutoCompleteHints:(WebHistoryItem *)otherItem
{
ASSERT_ARG(otherItem, otherItem);
core(_private)->mergeAutoCompleteHints(core(otherItem->_private));
}
- (id)initFromDictionaryRepresentation:(NSDictionary *)dict
{
ASSERT_MAIN_THREAD();
NSString *URLString = [dict _webkit_stringForKey:@""];
NSString *title = [dict _webkit_stringForKey:WebTitleKey];
// Do an existence check to avoid calling doubleValue on a nil string. Leave
// time interval at 0 if there's no value in dict.
NSString *timeIntervalString = [dict _webkit_stringForKey:WebLastVisitedTimeIntervalKey];
NSTimeInterval lastVisited = timeIntervalString == nil ? 0 : [timeIntervalString doubleValue];
self = [self initWithURLString:URLString title:title displayTitle:[dict _webkit_stringForKey:WebDisplayTitleKey] lastVisitedTimeInterval:lastVisited];
// Check if we've read a broken URL from the file that has non-Latin1 chars. If so, try to convert
// as if it was from user typing.
if (![URLString canBeConvertedToEncoding:NSISOLatin1StringEncoding]) {
NSURL *tempURL = [NSURL _web_URLWithUserTypedString:URLString];
ASSERT(tempURL);
NSString *newURLString = [tempURL _web_originalDataAsString];
core(_private)->setURLString(newURLString);
core(_private)->setOriginalURLString(newURLString);
}
core(_private)->setVisitCount([dict _webkit_intForKey:WebVisitCountKey]);
NSArray *childDicts = [dict objectForKey:WebChildrenKey];
if (childDicts) {
for (int i = [childDicts count]; i >= 0; i--) {
WebHistoryItem *child = [[WebHistoryItem alloc] initFromDictionaryRepresentation: [childDicts objectAtIndex:i]];
core(_private)->addChildItem(core(child->_private));
[child release];
}
}
return self;
}
- (NSPoint)scrollPoint
{
ASSERT_MAIN_THREAD();
return core(_private)->scrollPoint();
}
@end
@implementation WebHistoryItem (WebPrivate)
- (id)initWithURL:(NSURL *)URL title:(NSString *)title
{
return [self initWithURLString:[URL _web_originalDataAsString] title:title lastVisitedTimeInterval:0];
}
- (NSDictionary *)dictionaryRepresentation
{
ASSERT_MAIN_THREAD();
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:6];
HistoryItem* coreItem = core(_private);
if (!coreItem->urlString().isEmpty()) {
[dict setObject:(NSString*)coreItem->urlString() forKey:@""];
}
if (!coreItem->title().isEmpty()) {
[dict setObject:(NSString*)coreItem->title() forKey:WebTitleKey];
}
if (!coreItem->alternateTitle().isEmpty()) {
[dict setObject:(NSString*)coreItem->alternateTitle() forKey:WebDisplayTitleKey];
}
if (coreItem->lastVisitedTime() != 0.0) {
// store as a string to maintain backward compatibility (see 3245793)
[dict setObject:[NSString stringWithFormat:@"%.1lf", coreItem->lastVisitedTime()]
forKey:WebLastVisitedTimeIntervalKey];
}
if (coreItem->visitCount()) {
[dict setObject:[NSNumber numberWithInt:coreItem->visitCount()] forKey:WebVisitCountKey];
}
if (coreItem->children().size()) {
const HistoryItemVector& children = coreItem->children();
NSMutableArray *childDicts = [NSMutableArray arrayWithCapacity:children.size()];
for (int i = children.size(); i >= 0; i--)
[childDicts addObject:[kit(children[i].get()) dictionaryRepresentation]];
[dict setObject: childDicts forKey:WebChildrenKey];
}
return dict;
}
- (NSString *)target
{
ASSERT_MAIN_THREAD();
return nsStringNilIfEmpty(core(_private)->target());
}
- (BOOL)isTargetItem
{
return core(_private)->isTargetItem();
}
- (int)visitCount
{
ASSERT_MAIN_THREAD();
return core(_private)->visitCount();
}
- (NSString *)RSSFeedReferrer
{
return nsStringNilIfEmpty(core(_private)->rssFeedReferrer());
}
- (void)setRSSFeedReferrer:(NSString *)referrer
{
core(_private)->setRSSFeedReferrer(referrer);
}
- (NSArray *)children
{
ASSERT_MAIN_THREAD();
const HistoryItemVector& children = core(_private)->children();
if (!children.size())
return nil;
unsigned size = children.size();
NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:size] autorelease];
for (unsigned i = 0; i < size; ++i)
[result addObject:kit(children[i].get())];
return result;
}
- (void)setAlwaysAttemptToUsePageCache:(BOOL)flag
{
// Safari 2.0 uses this for SnapBack, so we stub it out to avoid a crash.
}
- (NSURL *)URL
{
ASSERT_MAIN_THREAD();
const KURL& url = core(_private)->url();
if (url.isEmpty())
return nil;
return url;
}
// This should not be called directly for WebHistoryItems that are already included
// in WebHistory. Use -[WebHistory setLastVisitedTimeInterval:forItem:] instead.
- (void)_setLastVisitedTimeInterval:(NSTimeInterval)time
{
core(_private)->setLastVisitedTime(time);
}
// FIXME: <rdar://problem/4880065> - Push Global History into WebCore
// Once that task is complete, this accessor can go away
- (NSCalendarDate *)_lastVisitedDate
{
ASSERT_MAIN_THREAD();
return [[[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:core(_private)->lastVisitedTime()] autorelease];
}
- (WebHistoryItem *)targetItem
{
ASSERT_MAIN_THREAD();
HistoryItem* coreItem = core(_private);
if (coreItem->isTargetItem() || !coreItem->hasChildren())
return self;
return kit(coreItem->recurseToFindTargetItem());
}
+ (void)_releaseAllPendingPageCaches
{
pageCache()->releaseAutoreleasedPagesNow();
}
- (id)_transientPropertyForKey:(NSString *)key
{
return core(_private)->getTransientProperty(key);
}
- (void)_setTransientProperty:(id)property forKey:(NSString *)key
{
core(_private)->setTransientProperty(key, property);
}
@end
// FIXME: <rdar://problem/4886761>
// This is a bizarre policy - we flush the page caches ANY time ANY window is closed?
@implementation WebWindowWatcher
-(void)windowWillClose:(NSNotification *)notification
{
pageCache()->releaseAutoreleasedPagesNow();
}
@end
|