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#include <CoreFoundation/CoreFoundation.h>
31#include <WebCore/COMPtr.h>
32#include <wtf/Forward.h>
33#include <wtf/OwnArrayPtr.h>
34#include <wtf/RetainPtr.h>
35
36namespace WebCore {
37    class KURL;
38    class PageGroup;
39}
40
41//-----------------------------------------------------------------------------
42
43class WebPreferences;
44
45class WebHistory : public IWebHistory, public IWebHistoryPrivate {
46public:
47    static WebHistory* createInstance();
48private:
49    WebHistory();
50    ~WebHistory();
51
52public:
53    // IUnknown
54    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
55    virtual ULONG STDMETHODCALLTYPE AddRef(void);
56    virtual ULONG STDMETHODCALLTYPE Release(void);
57
58    // IWebHistory
59    virtual HRESULT STDMETHODCALLTYPE optionalSharedHistory(
60        /* [retval][out] */ IWebHistory** history);
61
62    virtual HRESULT STDMETHODCALLTYPE setOptionalSharedHistory(
63        /* [in] */ IWebHistory* history);
64
65    virtual HRESULT STDMETHODCALLTYPE loadFromURL(
66        /* [in] */ BSTR url,
67        /* [out] */ IWebError** error,
68        /* [retval][out] */ BOOL* succeeded);
69
70    virtual HRESULT STDMETHODCALLTYPE saveToURL(
71        /* [in] */ BSTR url,
72        /* [out] */ IWebError** error,
73        /* [retval][out] */ BOOL* succeeded);
74
75    virtual HRESULT STDMETHODCALLTYPE addItems(
76        /* [in] */ int itemCount,
77        /* [in] */ IWebHistoryItem** items);
78
79    virtual HRESULT STDMETHODCALLTYPE removeItems(
80        /* [in] */ int itemCount,
81        /* [in] */ IWebHistoryItem** items);
82
83    virtual HRESULT STDMETHODCALLTYPE removeAllItems( void);
84
85    virtual HRESULT STDMETHODCALLTYPE orderedLastVisitedDays(
86        /* [out][in] */ int* count,
87        /* [in] */ DATE* calendarDates);
88
89    virtual HRESULT STDMETHODCALLTYPE orderedItemsLastVisitedOnDay(
90        /* [out][in] */ int* count,
91        /* [in] */ IWebHistoryItem** items,
92        /* [in] */ DATE calendarDate);
93
94    virtual HRESULT STDMETHODCALLTYPE itemForURL(
95        /* [in] */ BSTR url,
96        /* [retval][out] */ IWebHistoryItem** item);
97
98    virtual HRESULT STDMETHODCALLTYPE setHistoryItemLimit(
99        /* [in] */ int limit);
100
101    virtual HRESULT STDMETHODCALLTYPE historyItemLimit(
102        /* [retval][out] */ int* limit);
103
104    virtual HRESULT STDMETHODCALLTYPE setHistoryAgeInDaysLimit(
105        /* [in] */ int limit);
106
107    virtual HRESULT STDMETHODCALLTYPE historyAgeInDaysLimit(
108        /* [retval][out] */ int* limit);
109
110    // IWebHistoryPrivate
111
112    virtual HRESULT STDMETHODCALLTYPE allItems(
113        /* [out][in] */ int* count,
114        /* [retval][out] */ IWebHistoryItem** items);
115
116    virtual HRESULT STDMETHODCALLTYPE data(IStream**);
117
118    virtual HRESULT STDMETHODCALLTYPE setVisitedLinkTrackingEnabled(BOOL visitedLinkTrackingEnable);
119    virtual HRESULT STDMETHODCALLTYPE removeAllVisitedLinks();
120
121    // WebHistory
122    static WebHistory* sharedHistory();
123    void visitedURL(const WebCore::KURL&, const WTF::String& title, const WTF::String& httpMethod, bool wasFailure, bool increaseVisitCount);
124    void addVisitedLinksToPageGroup(WebCore::PageGroup&);
125
126    COMPtr<IWebHistoryItem> itemForURLString(const WTF::String&) const;
127
128    typedef int64_t DateKey;
129    typedef HashMap<DateKey, RetainPtr<CFMutableArrayRef> > DateToEntriesMap;
130
131private:
132
133    enum NotificationType
134    {
135        kWebHistoryItemsAddedNotification = 0,
136        kWebHistoryItemsRemovedNotification = 1,
137        kWebHistoryAllItemsRemovedNotification = 2,
138        kWebHistoryLoadedNotification = 3,
139        kWebHistoryItemsDiscardedWhileLoadingNotification = 4,
140        kWebHistorySavedNotification = 5
141    };
142
143    HRESULT loadHistoryGutsFromURL(CFURLRef url, CFMutableArrayRef discardedItems, IWebError** error);
144    HRESULT saveHistoryGuts(CFURLRef url, IWebError** error);
145    HRESULT postNotification(NotificationType notifyType, IPropertyBag* userInfo = 0);
146    HRESULT removeItem(IWebHistoryItem* entry);
147    HRESULT addItem(IWebHistoryItem* entry, bool discardDuplicate, bool* added);
148    HRESULT removeItemForURLString(CFStringRef urlString);
149    HRESULT addItemToDateCaches(IWebHistoryItem* entry);
150    HRESULT removeItemFromDateCaches(IWebHistoryItem* entry);
151    HRESULT insertItem(IWebHistoryItem* entry, DateKey);
152    HRESULT ageLimitDate(CFAbsoluteTime* time);
153    bool findKey(DateKey*, CFAbsoluteTime forDay);
154    static CFAbsoluteTime timeToDate(CFAbsoluteTime time);
155    BSTR getNotificationString(NotificationType notifyType);
156    HRESULT itemForURLString(CFStringRef urlString, IWebHistoryItem** item) const;
157    RetainPtr<CFDataRef> data() const;
158
159    ULONG m_refCount;
160    RetainPtr<CFMutableDictionaryRef> m_entriesByURL;
161    DateToEntriesMap m_entriesByDate;
162    OwnArrayPtr<DATE> m_orderedLastVisitedDays;
163    COMPtr<WebPreferences> m_preferences;
164};
165
166#endif
167