1/*
2 * Copyright (C) 2006, 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WebHistory_H
27#define WebHistory_H
28
29#include "WebKit.h"
30
31#include "COMPtr.h"
32#include <CoreFoundation/CoreFoundation.h>
33#include <wtf/OwnArrayPtr.h>
34#include <wtf/RetainPtr.h>
35
36namespace WebCore {
37    class KURL;
38    class PageGroup;
39    class String;
40}
41
42//-----------------------------------------------------------------------------
43
44class WebPreferences;
45
46class WebHistory : public IWebHistory, public IWebHistoryPrivate {
47public:
48    static WebHistory* createInstance();
49private:
50    WebHistory();
51    ~WebHistory();
52
53public:
54    // IUnknown
55    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
56    virtual ULONG STDMETHODCALLTYPE AddRef(void);
57    virtual ULONG STDMETHODCALLTYPE Release(void);
58
59    // IWebHistory
60    virtual HRESULT STDMETHODCALLTYPE optionalSharedHistory(
61        /* [retval][out] */ IWebHistory** history);
62
63    virtual HRESULT STDMETHODCALLTYPE setOptionalSharedHistory(
64        /* [in] */ IWebHistory* history);
65
66    virtual HRESULT STDMETHODCALLTYPE loadFromURL(
67        /* [in] */ BSTR url,
68        /* [out] */ IWebError** error,
69        /* [retval][out] */ BOOL* succeeded);
70
71    virtual HRESULT STDMETHODCALLTYPE saveToURL(
72        /* [in] */ BSTR url,
73        /* [out] */ IWebError** error,
74        /* [retval][out] */ BOOL* succeeded);
75
76    virtual HRESULT STDMETHODCALLTYPE addItems(
77        /* [in] */ int itemCount,
78        /* [in] */ IWebHistoryItem** items);
79
80    virtual HRESULT STDMETHODCALLTYPE removeItems(
81        /* [in] */ int itemCount,
82        /* [in] */ IWebHistoryItem** items);
83
84    virtual HRESULT STDMETHODCALLTYPE removeAllItems( void);
85
86    virtual HRESULT STDMETHODCALLTYPE orderedLastVisitedDays(
87        /* [out][in] */ int* count,
88        /* [in] */ DATE* calendarDates);
89
90    virtual HRESULT STDMETHODCALLTYPE orderedItemsLastVisitedOnDay(
91        /* [out][in] */ int* count,
92        /* [in] */ IWebHistoryItem** items,
93        /* [in] */ DATE calendarDate);
94
95    virtual HRESULT STDMETHODCALLTYPE itemForURL(
96        /* [in] */ BSTR url,
97        /* [retval][out] */ IWebHistoryItem** item);
98
99    virtual HRESULT STDMETHODCALLTYPE setHistoryItemLimit(
100        /* [in] */ int limit);
101
102    virtual HRESULT STDMETHODCALLTYPE historyItemLimit(
103        /* [retval][out] */ int* limit);
104
105    virtual HRESULT STDMETHODCALLTYPE setHistoryAgeInDaysLimit(
106        /* [in] */ int limit);
107
108    virtual HRESULT STDMETHODCALLTYPE historyAgeInDaysLimit(
109        /* [retval][out] */ int* limit);
110
111    // IWebHistoryPrivate
112
113    virtual HRESULT STDMETHODCALLTYPE allItems(
114        /* [out][in] */ int* count,
115        /* [retval][out] */ IWebHistoryItem** items);
116
117    virtual HRESULT STDMETHODCALLTYPE data(IStream**);
118
119    virtual HRESULT STDMETHODCALLTYPE setVisitedLinkTrackingEnabled(BOOL visitedLinkTrackingEnable);
120    virtual HRESULT STDMETHODCALLTYPE removeAllVisitedLinks();
121
122    // WebHistory
123    static WebHistory* sharedHistory();
124    void visitedURL(const WebCore::KURL&, const WebCore::String& title, const WebCore::String& httpMethod, bool wasFailure, bool increaseVisitCount);
125    void addVisitedLinksToPageGroup(WebCore::PageGroup&);
126
127    COMPtr<IWebHistoryItem> itemForURLString(const WebCore::String&) const;
128
129    typedef int64_t DateKey;
130    typedef HashMap<DateKey, RetainPtr<CFMutableArrayRef> > DateToEntriesMap;
131
132private:
133
134    enum NotificationType
135    {
136        kWebHistoryItemsAddedNotification = 0,
137        kWebHistoryItemsRemovedNotification = 1,
138        kWebHistoryAllItemsRemovedNotification = 2,
139        kWebHistoryLoadedNotification = 3,
140        kWebHistoryItemsDiscardedWhileLoadingNotification = 4,
141        kWebHistorySavedNotification = 5
142    };
143
144    HRESULT loadHistoryGutsFromURL(CFURLRef url, CFMutableArrayRef discardedItems, IWebError** error);
145    HRESULT saveHistoryGuts(CFURLRef url, IWebError** error);
146    HRESULT postNotification(NotificationType notifyType, IPropertyBag* userInfo = 0);
147    HRESULT removeItem(IWebHistoryItem* entry);
148    HRESULT addItem(IWebHistoryItem* entry, bool discardDuplicate, bool* added);
149    HRESULT removeItemForURLString(CFStringRef urlString);
150    HRESULT addItemToDateCaches(IWebHistoryItem* entry);
151    HRESULT removeItemFromDateCaches(IWebHistoryItem* entry);
152    HRESULT insertItem(IWebHistoryItem* entry, DateKey);
153    HRESULT ageLimitDate(CFAbsoluteTime* time);
154    bool findKey(DateKey*, CFAbsoluteTime forDay);
155    static CFAbsoluteTime timeToDate(CFAbsoluteTime time);
156    BSTR getNotificationString(NotificationType notifyType);
157    HRESULT itemForURLString(CFStringRef urlString, IWebHistoryItem** item) const;
158    RetainPtr<CFDataRef> data() const;
159
160    ULONG m_refCount;
161    RetainPtr<CFMutableDictionaryRef> m_entriesByURL;
162    DateToEntriesMap m_entriesByDate;
163    OwnArrayPtr<DATE> m_orderedLastVisitedDays;
164    COMPtr<WebPreferences> m_preferences;
165};
166
167#endif
168