WebHistory.mm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * Copyright (C) 2005, 2008, 2009 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. #import "WebHistoryInternal.h"
  29. #import "HistoryPropertyList.h"
  30. #import "WebHistoryItemInternal.h"
  31. #import "WebKitLogging.h"
  32. #import "WebNSURLExtras.h"
  33. #import "WebTypesInternal.h"
  34. #import <WebCore/HistoryItem.h>
  35. #import <WebCore/PageGroup.h>
  36. using namespace WebCore;
  37. typedef int64_t WebHistoryDateKey;
  38. typedef HashMap<WebHistoryDateKey, RetainPtr<NSMutableArray> > DateToEntriesMap;
  39. NSString *WebHistoryItemsAddedNotification = @"WebHistoryItemsAddedNotification";
  40. NSString *WebHistoryItemsRemovedNotification = @"WebHistoryItemsRemovedNotification";
  41. NSString *WebHistoryAllItemsRemovedNotification = @"WebHistoryAllItemsRemovedNotification";
  42. NSString *WebHistoryLoadedNotification = @"WebHistoryLoadedNotification";
  43. NSString *WebHistoryItemsDiscardedWhileLoadingNotification = @"WebHistoryItemsDiscardedWhileLoadingNotification";
  44. NSString *WebHistorySavedNotification = @"WebHistorySavedNotification";
  45. NSString *WebHistoryItemsKey = @"WebHistoryItems";
  46. static WebHistory *_sharedHistory = nil;
  47. NSString *FileVersionKey = @"WebHistoryFileVersion";
  48. NSString *DatesArrayKey = @"WebHistoryDates";
  49. #define currentFileVersion 1
  50. class WebHistoryWriter : public HistoryPropertyListWriter {
  51. public:
  52. WebHistoryWriter(DateToEntriesMap*);
  53. private:
  54. virtual void writeHistoryItems(BinaryPropertyListObjectStream&);
  55. DateToEntriesMap* m_entriesByDate;
  56. Vector<int> m_dateKeys;
  57. };
  58. @interface WebHistory ()
  59. - (void)_sendNotification:(NSString *)name entries:(NSArray *)entries;
  60. @end
  61. @interface WebHistoryPrivate : NSObject {
  62. @private
  63. NSMutableDictionary *_entriesByURL;
  64. DateToEntriesMap* _entriesByDate;
  65. NSMutableArray *_orderedLastVisitedDays;
  66. BOOL itemLimitSet;
  67. int itemLimit;
  68. BOOL ageInDaysLimitSet;
  69. int ageInDaysLimit;
  70. }
  71. - (WebHistoryItem *)visitedURL:(NSURL *)url withTitle:(NSString *)title increaseVisitCount:(BOOL)increaseVisitCount;
  72. - (BOOL)addItem:(WebHistoryItem *)entry discardDuplicate:(BOOL)discardDuplicate;
  73. - (void)addItems:(NSArray *)newEntries;
  74. - (BOOL)removeItem:(WebHistoryItem *)entry;
  75. - (BOOL)removeItems:(NSArray *)entries;
  76. - (BOOL)removeAllItems;
  77. - (void)rebuildHistoryByDayIfNeeded:(WebHistory *)webHistory;
  78. - (NSArray *)orderedLastVisitedDays;
  79. - (NSArray *)orderedItemsLastVisitedOnDay:(NSCalendarDate *)calendarDate;
  80. - (BOOL)containsURL:(NSURL *)URL;
  81. - (WebHistoryItem *)itemForURL:(NSURL *)URL;
  82. - (WebHistoryItem *)itemForURLString:(NSString *)URLString;
  83. - (NSArray *)allItems;
  84. - (BOOL)loadFromURL:(NSURL *)URL collectDiscardedItemsInto:(NSMutableArray *)discardedItems error:(NSError **)error;
  85. - (BOOL)saveToURL:(NSURL *)URL error:(NSError **)error;
  86. - (NSCalendarDate *)ageLimitDate;
  87. - (void)setHistoryItemLimit:(int)limit;
  88. - (int)historyItemLimit;
  89. - (void)setHistoryAgeInDaysLimit:(int)limit;
  90. - (int)historyAgeInDaysLimit;
  91. - (void)addVisitedLinksToPageGroup:(PageGroup&)group;
  92. @end
  93. @implementation WebHistoryPrivate
  94. // MARK: OBJECT FRAMEWORK
  95. + (void)initialize
  96. {
  97. [[NSUserDefaults standardUserDefaults] registerDefaults:
  98. [NSDictionary dictionaryWithObjectsAndKeys:
  99. @"1000", @"WebKitHistoryItemLimit",
  100. @"7", @"WebKitHistoryAgeInDaysLimit",
  101. nil]];
  102. }
  103. - (id)init
  104. {
  105. self = [super init];
  106. if (!self)
  107. return nil;
  108. _entriesByURL = [[NSMutableDictionary alloc] init];
  109. _entriesByDate = new DateToEntriesMap;
  110. return self;
  111. }
  112. - (void)dealloc
  113. {
  114. [_entriesByURL release];
  115. [_orderedLastVisitedDays release];
  116. delete _entriesByDate;
  117. [super dealloc];
  118. }
  119. - (void)finalize
  120. {
  121. delete _entriesByDate;
  122. [super finalize];
  123. }
  124. // MARK: MODIFYING CONTENTS
  125. static void getDayBoundaries(NSTimeInterval interval, NSTimeInterval& beginningOfDay, NSTimeInterval& beginningOfNextDay)
  126. {
  127. CFTimeZoneRef timeZone = CFTimeZoneCopyDefault();
  128. CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(interval, timeZone);
  129. date.hour = 0;
  130. date.minute = 0;
  131. date.second = 0;
  132. beginningOfDay = CFGregorianDateGetAbsoluteTime(date, timeZone);
  133. date.day += 1;
  134. beginningOfNextDay = CFGregorianDateGetAbsoluteTime(date, timeZone);
  135. CFRelease(timeZone);
  136. }
  137. static inline NSTimeInterval beginningOfDay(NSTimeInterval date)
  138. {
  139. static NSTimeInterval cachedBeginningOfDay = NAN;
  140. static NSTimeInterval cachedBeginningOfNextDay;
  141. if (!(date >= cachedBeginningOfDay && date < cachedBeginningOfNextDay))
  142. getDayBoundaries(date, cachedBeginningOfDay, cachedBeginningOfNextDay);
  143. return cachedBeginningOfDay;
  144. }
  145. static inline WebHistoryDateKey dateKey(NSTimeInterval date)
  146. {
  147. // Converting from double (NSTimeInterval) to int64_t (WebHistoryDateKey) is
  148. // safe here because all sensible dates are in the range -2**48 .. 2**47 which
  149. // safely fits in an int64_t.
  150. return beginningOfDay(date);
  151. }
  152. // Returns whether the day is already in the list of days,
  153. // and fills in *key with the key used to access its location
  154. - (BOOL)findKey:(WebHistoryDateKey*)key forDay:(NSTimeInterval)date
  155. {
  156. ASSERT_ARG(key, key);
  157. *key = dateKey(date);
  158. return _entriesByDate->contains(*key);
  159. }
  160. - (void)insertItem:(WebHistoryItem *)entry forDateKey:(WebHistoryDateKey)dateKey
  161. {
  162. ASSERT_ARG(entry, entry != nil);
  163. ASSERT(_entriesByDate->contains(dateKey));
  164. NSMutableArray *entriesForDate = _entriesByDate->get(dateKey).get();
  165. NSTimeInterval entryDate = [entry lastVisitedTimeInterval];
  166. unsigned count = [entriesForDate count];
  167. // The entries for each day are stored in a sorted array with the most recent entry first
  168. // Check for the common cases of the entry being newer than all existing entries or the first entry of the day
  169. if (!count || [[entriesForDate objectAtIndex:0] lastVisitedTimeInterval] < entryDate) {
  170. [entriesForDate insertObject:entry atIndex:0];
  171. return;
  172. }
  173. // .. or older than all existing entries
  174. if (count > 0 && [[entriesForDate objectAtIndex:count - 1] lastVisitedTimeInterval] >= entryDate) {
  175. [entriesForDate insertObject:entry atIndex:count];
  176. return;
  177. }
  178. unsigned low = 0;
  179. unsigned high = count;
  180. while (low < high) {
  181. unsigned mid = low + (high - low) / 2;
  182. if ([[entriesForDate objectAtIndex:mid] lastVisitedTimeInterval] >= entryDate)
  183. low = mid + 1;
  184. else
  185. high = mid;
  186. }
  187. // low is now the index of the first entry that is older than entryDate
  188. [entriesForDate insertObject:entry atIndex:low];
  189. }
  190. - (BOOL)removeItemFromDateCaches:(WebHistoryItem *)entry
  191. {
  192. WebHistoryDateKey dateKey;
  193. BOOL foundDate = [self findKey:&dateKey forDay:[entry lastVisitedTimeInterval]];
  194. if (!foundDate)
  195. return NO;
  196. DateToEntriesMap::iterator it = _entriesByDate->find(dateKey);
  197. NSMutableArray *entriesForDate = it->value.get();
  198. [entriesForDate removeObjectIdenticalTo:entry];
  199. // remove this date entirely if there are no other entries on it
  200. if ([entriesForDate count] == 0) {
  201. _entriesByDate->remove(it);
  202. // Clear _orderedLastVisitedDays so it will be regenerated when next requested.
  203. [_orderedLastVisitedDays release];
  204. _orderedLastVisitedDays = nil;
  205. }
  206. return YES;
  207. }
  208. - (BOOL)removeItemForURLString:(NSString *)URLString
  209. {
  210. WebHistoryItem *entry = [_entriesByURL objectForKey:URLString];
  211. if (!entry)
  212. return NO;
  213. [_entriesByURL removeObjectForKey:URLString];
  214. #if ASSERT_DISABLED
  215. [self removeItemFromDateCaches:entry];
  216. #else
  217. BOOL itemWasInDateCaches = [self removeItemFromDateCaches:entry];
  218. ASSERT(itemWasInDateCaches);
  219. #endif
  220. if (![_entriesByURL count])
  221. PageGroup::removeAllVisitedLinks();
  222. return YES;
  223. }
  224. - (void)addItemToDateCaches:(WebHistoryItem *)entry
  225. {
  226. WebHistoryDateKey dateKey;
  227. if ([self findKey:&dateKey forDay:[entry lastVisitedTimeInterval]])
  228. // other entries already exist for this date
  229. [self insertItem:entry forDateKey:dateKey];
  230. else {
  231. // no other entries exist for this date
  232. NSMutableArray *entries = [[NSMutableArray alloc] initWithObjects:&entry count:1];
  233. _entriesByDate->set(dateKey, entries);
  234. [entries release];
  235. // Clear _orderedLastVisitedDays so it will be regenerated when next requested.
  236. [_orderedLastVisitedDays release];
  237. _orderedLastVisitedDays = nil;
  238. }
  239. }
  240. - (WebHistoryItem *)visitedURL:(NSURL *)url withTitle:(NSString *)title increaseVisitCount:(BOOL)increaseVisitCount
  241. {
  242. ASSERT(url);
  243. ASSERT(title);
  244. NSString *URLString = [url _web_originalDataAsString];
  245. if (!URLString)
  246. URLString = @"";
  247. WebHistoryItem *entry = [_entriesByURL objectForKey:URLString];
  248. if (entry) {
  249. LOG(History, "Updating global history entry %@", entry);
  250. // Remove the item from date caches before changing its last visited date. Otherwise we might get duplicate entries
  251. // as seen in <rdar://problem/6570573>.
  252. BOOL itemWasInDateCaches = [self removeItemFromDateCaches:entry];
  253. ASSERT_UNUSED(itemWasInDateCaches, itemWasInDateCaches);
  254. [entry _visitedWithTitle:title increaseVisitCount:increaseVisitCount];
  255. } else {
  256. LOG(History, "Adding new global history entry for %@", url);
  257. entry = [[WebHistoryItem alloc] initWithURLString:URLString title:title lastVisitedTimeInterval:[NSDate timeIntervalSinceReferenceDate]];
  258. [entry _recordInitialVisit];
  259. [_entriesByURL setObject:entry forKey:URLString];
  260. [entry release];
  261. }
  262. [self addItemToDateCaches:entry];
  263. return entry;
  264. }
  265. - (BOOL)addItem:(WebHistoryItem *)entry discardDuplicate:(BOOL)discardDuplicate
  266. {
  267. ASSERT_ARG(entry, entry);
  268. ASSERT_ARG(entry, [entry lastVisitedTimeInterval] != 0);
  269. NSString *URLString = [entry URLString];
  270. WebHistoryItem *oldEntry = [_entriesByURL objectForKey:URLString];
  271. if (oldEntry) {
  272. if (discardDuplicate)
  273. return NO;
  274. // The last reference to oldEntry might be this dictionary, so we hold onto a reference
  275. // until we're done with oldEntry.
  276. [oldEntry retain];
  277. [self removeItemForURLString:URLString];
  278. // If we already have an item with this URL, we need to merge info that drives the
  279. // URL autocomplete heuristics from that item into the new one.
  280. [entry _mergeAutoCompleteHints:oldEntry];
  281. [oldEntry release];
  282. }
  283. [self addItemToDateCaches:entry];
  284. [_entriesByURL setObject:entry forKey:URLString];
  285. return YES;
  286. }
  287. - (void)rebuildHistoryByDayIfNeeded:(WebHistory *)webHistory
  288. {
  289. // We clear all the values to present a consistent state when sending the notifications.
  290. // We keep a reference to the entries for rebuilding the history after the notification.
  291. Vector <RetainPtr<NSMutableArray> > entryArrays;
  292. copyValuesToVector(*_entriesByDate, entryArrays);
  293. _entriesByDate->clear();
  294. NSMutableDictionary *entriesByURL = _entriesByURL;
  295. _entriesByURL = nil;
  296. [_orderedLastVisitedDays release];
  297. _orderedLastVisitedDays = nil;
  298. NSArray *allEntries = [entriesByURL allValues];
  299. [webHistory _sendNotification:WebHistoryAllItemsRemovedNotification entries:allEntries];
  300. // Next, we rebuild the history, restore the states, and notify the clients.
  301. _entriesByURL = entriesByURL;
  302. for (size_t dayIndex = 0; dayIndex < entryArrays.size(); ++dayIndex) {
  303. for (WebHistoryItem *entry in (entryArrays[dayIndex]).get())
  304. [self addItemToDateCaches:entry];
  305. }
  306. [webHistory _sendNotification:WebHistoryItemsAddedNotification entries:allEntries];
  307. }
  308. - (BOOL)removeItem:(WebHistoryItem *)entry
  309. {
  310. NSString *URLString = [entry URLString];
  311. // If this exact object isn't stored, then make no change.
  312. // FIXME: Is this the right behavior if this entry isn't present, but another entry for the same URL is?
  313. // Maybe need to change the API to make something like removeEntryForURLString public instead.
  314. WebHistoryItem *matchingEntry = [_entriesByURL objectForKey:URLString];
  315. if (matchingEntry != entry)
  316. return NO;
  317. [self removeItemForURLString:URLString];
  318. return YES;
  319. }
  320. - (BOOL)removeItems:(NSArray *)entries
  321. {
  322. NSUInteger count = [entries count];
  323. if (!count)
  324. return NO;
  325. for (NSUInteger index = 0; index < count; ++index)
  326. [self removeItem:[entries objectAtIndex:index]];
  327. return YES;
  328. }
  329. - (BOOL)removeAllItems
  330. {
  331. if (_entriesByDate->isEmpty())
  332. return NO;
  333. _entriesByDate->clear();
  334. [_entriesByURL removeAllObjects];
  335. // Clear _orderedLastVisitedDays so it will be regenerated when next requested.
  336. [_orderedLastVisitedDays release];
  337. _orderedLastVisitedDays = nil;
  338. PageGroup::removeAllVisitedLinks();
  339. return YES;
  340. }
  341. - (void)addItems:(NSArray *)newEntries
  342. {
  343. // There is no guarantee that the incoming entries are in any particular
  344. // order, but if this is called with a set of entries that were created by
  345. // iterating through the results of orderedLastVisitedDays and orderedItemsLastVisitedOnDayy
  346. // then they will be ordered chronologically from newest to oldest. We can make adding them
  347. // faster (fewer compares) by inserting them from oldest to newest.
  348. NSEnumerator *enumerator = [newEntries reverseObjectEnumerator];
  349. while (WebHistoryItem *entry = [enumerator nextObject])
  350. [self addItem:entry discardDuplicate:NO];
  351. }
  352. // MARK: DATE-BASED RETRIEVAL
  353. - (NSArray *)orderedLastVisitedDays
  354. {
  355. if (!_orderedLastVisitedDays) {
  356. Vector<int> daysAsTimeIntervals;
  357. daysAsTimeIntervals.reserveCapacity(_entriesByDate->size());
  358. DateToEntriesMap::const_iterator end = _entriesByDate->end();
  359. for (DateToEntriesMap::const_iterator it = _entriesByDate->begin(); it != end; ++it)
  360. daysAsTimeIntervals.append(it->key);
  361. std::sort(daysAsTimeIntervals.begin(), daysAsTimeIntervals.end());
  362. size_t count = daysAsTimeIntervals.size();
  363. _orderedLastVisitedDays = [[NSMutableArray alloc] initWithCapacity:count];
  364. for (int i = count - 1; i >= 0; i--) {
  365. NSTimeInterval interval = daysAsTimeIntervals[i];
  366. NSCalendarDate *date = [[NSCalendarDate alloc] initWithTimeIntervalSinceReferenceDate:interval];
  367. [_orderedLastVisitedDays addObject:date];
  368. [date release];
  369. }
  370. }
  371. return _orderedLastVisitedDays;
  372. }
  373. - (NSArray *)orderedItemsLastVisitedOnDay:(NSCalendarDate *)date
  374. {
  375. WebHistoryDateKey dateKey;
  376. if (![self findKey:&dateKey forDay:[date timeIntervalSinceReferenceDate]])
  377. return nil;
  378. return _entriesByDate->get(dateKey).get();
  379. }
  380. // MARK: URL MATCHING
  381. - (WebHistoryItem *)itemForURLString:(NSString *)URLString
  382. {
  383. return [_entriesByURL objectForKey:URLString];
  384. }
  385. - (BOOL)containsURL:(NSURL *)URL
  386. {
  387. return [self itemForURLString:[URL _web_originalDataAsString]] != nil;
  388. }
  389. - (WebHistoryItem *)itemForURL:(NSURL *)URL
  390. {
  391. return [self itemForURLString:[URL _web_originalDataAsString]];
  392. }
  393. - (NSArray *)allItems
  394. {
  395. return [_entriesByURL allValues];
  396. }
  397. // MARK: ARCHIVING/UNARCHIVING
  398. - (void)setHistoryAgeInDaysLimit:(int)limit
  399. {
  400. ageInDaysLimitSet = YES;
  401. ageInDaysLimit = limit;
  402. }
  403. - (int)historyAgeInDaysLimit
  404. {
  405. if (ageInDaysLimitSet)
  406. return ageInDaysLimit;
  407. return [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitHistoryAgeInDaysLimit"];
  408. }
  409. - (void)setHistoryItemLimit:(int)limit
  410. {
  411. itemLimitSet = YES;
  412. itemLimit = limit;
  413. }
  414. - (int)historyItemLimit
  415. {
  416. if (itemLimitSet)
  417. return itemLimit;
  418. return [[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitHistoryItemLimit"];
  419. }
  420. // Return a date that marks the age limit for history entries saved to or
  421. // loaded from disk. Any entry older than this item should be rejected.
  422. - (NSCalendarDate *)ageLimitDate
  423. {
  424. return [[NSCalendarDate calendarDate] dateByAddingYears:0 months:0 days:-[self historyAgeInDaysLimit]
  425. hours:0 minutes:0 seconds:0];
  426. }
  427. - (BOOL)loadHistoryGutsFromURL:(NSURL *)URL savedItemsCount:(int *)numberOfItemsLoaded collectDiscardedItemsInto:(NSMutableArray *)discardedItems error:(NSError **)error
  428. {
  429. *numberOfItemsLoaded = 0;
  430. NSDictionary *dictionary = nil;
  431. // Optimize loading from local file, which is faster than using the general URL loading mechanism
  432. if ([URL isFileURL]) {
  433. dictionary = [NSDictionary dictionaryWithContentsOfFile:[URL path]];
  434. if (!dictionary) {
  435. #if !LOG_DISABLED
  436. if ([[NSFileManager defaultManager] fileExistsAtPath:[URL path]])
  437. LOG_ERROR("unable to read history from file %@; perhaps contents are corrupted", [URL path]);
  438. #endif
  439. // else file doesn't exist, which is normal the first time
  440. return NO;
  441. }
  442. } else {
  443. NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:error];
  444. if (data && [data length] > 0) {
  445. dictionary = [NSPropertyListSerialization propertyListFromData:data
  446. mutabilityOption:NSPropertyListImmutable
  447. format:nil
  448. errorDescription:nil];
  449. }
  450. }
  451. // We used to support NSArrays here, but that was before Safari 1.0 shipped. We will no longer support
  452. // that ancient format, so anything that isn't an NSDictionary is bogus.
  453. if (![dictionary isKindOfClass:[NSDictionary class]])
  454. return NO;
  455. NSNumber *fileVersionObject = [dictionary objectForKey:FileVersionKey];
  456. int fileVersion;
  457. // we don't trust data obtained from elsewhere, so double-check
  458. if (!fileVersionObject || ![fileVersionObject isKindOfClass:[NSNumber class]]) {
  459. LOG_ERROR("history file version can't be determined, therefore not loading");
  460. return NO;
  461. }
  462. fileVersion = [fileVersionObject intValue];
  463. if (fileVersion > currentFileVersion) {
  464. LOG_ERROR("history file version is %d, newer than newest known version %d, therefore not loading", fileVersion, currentFileVersion);
  465. return NO;
  466. }
  467. NSArray *array = [dictionary objectForKey:DatesArrayKey];
  468. int itemCountLimit = [self historyItemLimit];
  469. NSTimeInterval ageLimitDate = [[self ageLimitDate] timeIntervalSinceReferenceDate];
  470. NSEnumerator *enumerator = [array objectEnumerator];
  471. BOOL ageLimitPassed = NO;
  472. BOOL itemLimitPassed = NO;
  473. ASSERT(*numberOfItemsLoaded == 0);
  474. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  475. NSDictionary *itemAsDictionary;
  476. while ((itemAsDictionary = [enumerator nextObject]) != nil) {
  477. WebHistoryItem *item = [[WebHistoryItem alloc] initFromDictionaryRepresentation:itemAsDictionary];
  478. // item without URL is useless; data on disk must have been bad; ignore
  479. if ([item URLString]) {
  480. // Test against date limit. Since the items are ordered newest to oldest, we can stop comparing
  481. // once we've found the first item that's too old.
  482. if (!ageLimitPassed && [item lastVisitedTimeInterval] <= ageLimitDate)
  483. ageLimitPassed = YES;
  484. if (ageLimitPassed || itemLimitPassed)
  485. [discardedItems addObject:item];
  486. else {
  487. if ([self addItem:item discardDuplicate:YES])
  488. ++(*numberOfItemsLoaded);
  489. if (*numberOfItemsLoaded == itemCountLimit)
  490. itemLimitPassed = YES;
  491. // Draining the autorelease pool every 50 iterations was found by experimentation to be optimal
  492. if (*numberOfItemsLoaded % 50 == 0) {
  493. [pool drain];
  494. pool = [[NSAutoreleasePool alloc] init];
  495. }
  496. }
  497. }
  498. [item release];
  499. }
  500. [pool drain];
  501. return YES;
  502. }
  503. - (BOOL)loadFromURL:(NSURL *)URL collectDiscardedItemsInto:(NSMutableArray *)discardedItems error:(NSError **)error
  504. {
  505. #if !LOG_DISABLED
  506. double start = CFAbsoluteTimeGetCurrent();
  507. #endif
  508. int numberOfItems;
  509. if (![self loadHistoryGutsFromURL:URL savedItemsCount:&numberOfItems collectDiscardedItemsInto:discardedItems error:error])
  510. return NO;
  511. #if !LOG_DISABLED
  512. double duration = CFAbsoluteTimeGetCurrent() - start;
  513. LOG(Timing, "loading %d history entries from %@ took %f seconds", numberOfItems, URL, duration);
  514. #endif
  515. return YES;
  516. }
  517. - (NSData *)data
  518. {
  519. if (_entriesByDate->isEmpty()) {
  520. static NSData *emptyHistoryData = (NSData *)CFDataCreate(0, 0, 0);
  521. return emptyHistoryData;
  522. }
  523. // Ignores the date and item count limits; these are respected when loading instead of when saving, so
  524. // that clients can learn of discarded items by listening to WebHistoryItemsDiscardedWhileLoadingNotification.
  525. WebHistoryWriter writer(_entriesByDate);
  526. writer.writePropertyList();
  527. return [[(NSData *)writer.releaseData().get() retain] autorelease];
  528. }
  529. - (BOOL)saveToURL:(NSURL *)URL error:(NSError **)error
  530. {
  531. #if !LOG_DISABLED
  532. double start = CFAbsoluteTimeGetCurrent();
  533. #endif
  534. BOOL result = [[self data] writeToURL:URL options:0 error:error];
  535. #if !LOG_DISABLED
  536. double duration = CFAbsoluteTimeGetCurrent() - start;
  537. LOG(Timing, "saving history to %@ took %f seconds", URL, duration);
  538. #endif
  539. return result;
  540. }
  541. - (void)addVisitedLinksToPageGroup:(PageGroup&)group
  542. {
  543. NSEnumerator *enumerator = [_entriesByURL keyEnumerator];
  544. while (NSString *url = [enumerator nextObject]) {
  545. size_t length = [url length];
  546. const UChar* characters = CFStringGetCharactersPtr(reinterpret_cast<CFStringRef>(url));
  547. if (characters)
  548. group.addVisitedLink(characters, length);
  549. else {
  550. Vector<UChar, 512> buffer(length);
  551. [url getCharacters:buffer.data()];
  552. group.addVisitedLink(buffer.data(), length);
  553. }
  554. }
  555. }
  556. @end
  557. @implementation WebHistory
  558. + (WebHistory *)optionalSharedHistory
  559. {
  560. return _sharedHistory;
  561. }
  562. + (void)setOptionalSharedHistory:(WebHistory *)history
  563. {
  564. if (_sharedHistory == history)
  565. return;
  566. // FIXME: Need to think about multiple instances of WebHistory per application
  567. // and correct synchronization of history file between applications.
  568. [_sharedHistory release];
  569. _sharedHistory = [history retain];
  570. PageGroup::setShouldTrackVisitedLinks(history);
  571. PageGroup::removeAllVisitedLinks();
  572. }
  573. - (void)timeZoneChanged:(NSNotification *)notification
  574. {
  575. [_historyPrivate rebuildHistoryByDayIfNeeded:self];
  576. }
  577. - (id)init
  578. {
  579. self = [super init];
  580. if (!self)
  581. return nil;
  582. _historyPrivate = [[WebHistoryPrivate alloc] init];
  583. [[NSNotificationCenter defaultCenter] addObserver:self
  584. selector:@selector(timeZoneChanged:)
  585. name:NSSystemTimeZoneDidChangeNotification
  586. object:nil];
  587. return self;
  588. }
  589. - (void)dealloc
  590. {
  591. [[NSNotificationCenter defaultCenter] removeObserver:self
  592. name:NSSystemTimeZoneDidChangeNotification
  593. object:nil];
  594. [_historyPrivate release];
  595. [super dealloc];
  596. }
  597. - (void)finalize
  598. {
  599. [[NSNotificationCenter defaultCenter] removeObserver:self
  600. name:NSSystemTimeZoneDidChangeNotification
  601. object:nil];
  602. [super finalize];
  603. }
  604. // MARK: MODIFYING CONTENTS
  605. - (void)_sendNotification:(NSString *)name entries:(NSArray *)entries
  606. {
  607. NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:entries, WebHistoryItemsKey, nil];
  608. [[NSNotificationCenter defaultCenter]
  609. postNotificationName:name object:self userInfo:userInfo];
  610. }
  611. - (void)removeItems:(NSArray *)entries
  612. {
  613. if ([_historyPrivate removeItems:entries]) {
  614. [self _sendNotification:WebHistoryItemsRemovedNotification
  615. entries:entries];
  616. }
  617. }
  618. - (void)removeAllItems
  619. {
  620. NSArray *entries = [_historyPrivate allItems];
  621. if ([_historyPrivate removeAllItems])
  622. [self _sendNotification:WebHistoryAllItemsRemovedNotification entries:entries];
  623. }
  624. - (void)addItems:(NSArray *)newEntries
  625. {
  626. [_historyPrivate addItems:newEntries];
  627. [self _sendNotification:WebHistoryItemsAddedNotification
  628. entries:newEntries];
  629. }
  630. // MARK: DATE-BASED RETRIEVAL
  631. - (NSArray *)orderedLastVisitedDays
  632. {
  633. return [_historyPrivate orderedLastVisitedDays];
  634. }
  635. - (NSArray *)orderedItemsLastVisitedOnDay:(NSCalendarDate *)date
  636. {
  637. return [_historyPrivate orderedItemsLastVisitedOnDay:date];
  638. }
  639. // MARK: URL MATCHING
  640. - (BOOL)containsURL:(NSURL *)URL
  641. {
  642. return [_historyPrivate containsURL:URL];
  643. }
  644. - (WebHistoryItem *)itemForURL:(NSURL *)URL
  645. {
  646. return [_historyPrivate itemForURL:URL];
  647. }
  648. // MARK: SAVING TO DISK
  649. - (BOOL)loadFromURL:(NSURL *)URL error:(NSError **)error
  650. {
  651. NSMutableArray *discardedItems = [[NSMutableArray alloc] init];
  652. if (![_historyPrivate loadFromURL:URL collectDiscardedItemsInto:discardedItems error:error]) {
  653. [discardedItems release];
  654. return NO;
  655. }
  656. [[NSNotificationCenter defaultCenter]
  657. postNotificationName:WebHistoryLoadedNotification
  658. object:self];
  659. if ([discardedItems count])
  660. [self _sendNotification:WebHistoryItemsDiscardedWhileLoadingNotification entries:discardedItems];
  661. [discardedItems release];
  662. return YES;
  663. }
  664. - (BOOL)saveToURL:(NSURL *)URL error:(NSError **)error
  665. {
  666. if (![_historyPrivate saveToURL:URL error:error])
  667. return NO;
  668. [[NSNotificationCenter defaultCenter]
  669. postNotificationName:WebHistorySavedNotification
  670. object:self];
  671. return YES;
  672. }
  673. - (void)setHistoryItemLimit:(int)limit
  674. {
  675. [_historyPrivate setHistoryItemLimit:limit];
  676. }
  677. - (int)historyItemLimit
  678. {
  679. return [_historyPrivate historyItemLimit];
  680. }
  681. - (void)setHistoryAgeInDaysLimit:(int)limit
  682. {
  683. [_historyPrivate setHistoryAgeInDaysLimit:limit];
  684. }
  685. - (int)historyAgeInDaysLimit
  686. {
  687. return [_historyPrivate historyAgeInDaysLimit];
  688. }
  689. @end
  690. @implementation WebHistory (WebPrivate)
  691. - (WebHistoryItem *)_itemForURLString:(NSString *)URLString
  692. {
  693. return [_historyPrivate itemForURLString:URLString];
  694. }
  695. - (NSArray *)allItems
  696. {
  697. return [_historyPrivate allItems];
  698. }
  699. - (NSData *)_data
  700. {
  701. return [_historyPrivate data];
  702. }
  703. + (void)_setVisitedLinkTrackingEnabled:(BOOL)visitedLinkTrackingEnabled
  704. {
  705. PageGroup::setShouldTrackVisitedLinks(visitedLinkTrackingEnabled);
  706. }
  707. + (void)_removeAllVisitedLinks
  708. {
  709. PageGroup::removeAllVisitedLinks();
  710. }
  711. @end
  712. @implementation WebHistory (WebInternal)
  713. - (void)_visitedURL:(NSURL *)url withTitle:(NSString *)title method:(NSString *)method wasFailure:(BOOL)wasFailure increaseVisitCount:(BOOL)increaseVisitCount
  714. {
  715. WebHistoryItem *entry = [_historyPrivate visitedURL:url withTitle:title increaseVisitCount:increaseVisitCount];
  716. HistoryItem* item = core(entry);
  717. item->setLastVisitWasFailure(wasFailure);
  718. if ([method length])
  719. item->setLastVisitWasHTTPNonGet([method caseInsensitiveCompare:@"GET"] && (![[url scheme] caseInsensitiveCompare:@"http"] || ![[url scheme] caseInsensitiveCompare:@"https"]));
  720. item->setRedirectURLs(nullptr);
  721. NSArray *entries = [[NSArray alloc] initWithObjects:entry, nil];
  722. [self _sendNotification:WebHistoryItemsAddedNotification entries:entries];
  723. [entries release];
  724. }
  725. - (void)_addVisitedLinksToPageGroup:(WebCore::PageGroup&)group
  726. {
  727. [_historyPrivate addVisitedLinksToPageGroup:group];
  728. }
  729. @end
  730. WebHistoryWriter::WebHistoryWriter(DateToEntriesMap* entriesByDate)
  731. : m_entriesByDate(entriesByDate)
  732. {
  733. m_dateKeys.reserveCapacity(m_entriesByDate->size());
  734. DateToEntriesMap::const_iterator end = m_entriesByDate->end();
  735. for (DateToEntriesMap::const_iterator it = m_entriesByDate->begin(); it != end; ++it)
  736. m_dateKeys.append(it->key);
  737. std::sort(m_dateKeys.begin(), m_dateKeys.end());
  738. }
  739. void WebHistoryWriter::writeHistoryItems(BinaryPropertyListObjectStream& stream)
  740. {
  741. for (int dateIndex = m_dateKeys.size() - 1; dateIndex >= 0; dateIndex--) {
  742. NSArray *entries = m_entriesByDate->get(m_dateKeys[dateIndex]).get();
  743. NSUInteger entryCount = [entries count];
  744. for (NSUInteger entryIndex = 0; entryIndex < entryCount; ++entryIndex)
  745. writeHistoryItem(stream, core([entries objectAtIndex:entryIndex]));
  746. }
  747. }