📄 webhistoryitem.mm
字号:
/* * 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 "WebNSArrayExtras.h"#import "WebNSDictionaryExtras.h"#import "WebNSObjectExtras.h"#import "WebNSURLExtras.h"#import "WebNSURLRequestExtras.h"#import "WebNSViewExtras.h"#import "WebPluginController.h"#import "WebTypesInternal.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 <runtime/InitializeThreading.h>#import <wtf/Assertions.h>#import <wtf/StdLibExtras.h>// Private keys used in the WebHistoryItem's dictionary representation.// see 3245793 for explanation of "lastVisitedDate"static NSString *lastVisitedTimeIntervalKey = @"lastVisitedDate";static NSString *visitCountKey = @"visitCount";static NSString *titleKey = @"title";static NSString *childrenKey = @"children";static NSString *displayTitleKey = @"displayTitle";static NSString *lastVisitWasFailureKey = @"lastVisitWasFailure";static NSString *lastVisitWasHTTPNonGetKey = @"lastVisitWasHTTPNonGet";static NSString *redirectURLsKey = @"redirectURLs";static NSString *dailyVisitCountKey = @"D"; // short key to save spacestatic NSString *weeklyVisitCountKey = @"W"; // short key to save space// Notification strings.NSString *WebHistoryItemChangedNotification = @"WebHistoryItemChangedNotification";using namespace WebCore;using namespace std;typedef HashMap<HistoryItem*, WebHistoryItem*> HistoryItemMap;static inline WebHistoryItemPrivate* kitPrivate(WebCoreHistoryItem* list) { return (WebHistoryItemPrivate*)list; }static inline WebCoreHistoryItem* core(WebHistoryItemPrivate* list) { return (WebCoreHistoryItem*)list; }static HistoryItemMap& historyItemWrappers(){ DEFINE_STATIC_LOCAL(HistoryItemMap, historyItemWrappers, ()); return historyItemWrappers;}void WKNotifyHistoryItemChanged(){ [[NSNotificationCenter defaultCenter] postNotificationName:WebHistoryItemChangedNotification object:nil userInfo:nil];}@implementation WebHistoryItem+ (void)initialize{ JSC::initializeThreading();#ifndef BUILDING_ON_TIGER 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -