b56988f67e66f0de09e21d3d1f0fd5b67e71d446
[WebKit-https.git] / WebKit / win / WebHistoryItem.cpp
1 /*
2  * Copyright (C) 2006, 2007 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 #include "config.h"
27 #include "WebKitDLL.h"
28 #include "WebHistoryItem.h"
29
30 #include "COMPtr.h"
31 #include "MarshallingHelpers.h"
32 #include "WebKit.h"
33
34 #pragma warning(push, 0)
35 #include <WebCore/BString.h>
36 #include <WebCore/HistoryItem.h>
37 #pragma warning(pop)
38
39 #include <wtf/RetainPtr.h>
40
41 using namespace WebCore;
42
43 // WebHistoryItem ----------------------------------------------------------------
44
45 static HashMap<HistoryItem*, WebHistoryItem*>& historyItemWrappers()
46 {
47     static HashMap<HistoryItem*, WebHistoryItem*> staticHistoryItemWrappers;
48     return staticHistoryItemWrappers;
49 }
50
51 WebHistoryItem::WebHistoryItem(PassRefPtr<HistoryItem> historyItem)
52 : m_refCount(0)
53 , m_historyItem(historyItem)
54 {
55     ASSERT(!historyItemWrappers().contains(m_historyItem.get()));
56     historyItemWrappers().set(m_historyItem.get(), this);
57
58     gClassCount++;
59 }
60
61 WebHistoryItem::~WebHistoryItem()
62 {
63     ASSERT(historyItemWrappers().contains(m_historyItem.get()));
64     historyItemWrappers().remove(m_historyItem.get());
65
66     gClassCount--;
67 }
68
69 WebHistoryItem* WebHistoryItem::createInstance()
70 {
71     WebHistoryItem* instance = new WebHistoryItem(new HistoryItem);
72     instance->AddRef();
73     return instance;
74 }
75
76 WebHistoryItem* WebHistoryItem::createInstance(PassRefPtr<HistoryItem> historyItem)
77 {
78     WebHistoryItem* instance;
79
80     instance = historyItemWrappers().get(historyItem.get());
81
82     if (!instance)
83         instance = new WebHistoryItem(historyItem);
84
85     instance->AddRef();
86     return instance;
87 }
88
89 // IWebHistoryItemPrivate -----------------------------------------------------
90
91 static CFStringRef urlKey = CFSTR("");
92 static CFStringRef lastVisitedDateKey = CFSTR("lastVisitedDate");
93 static CFStringRef titleKey = CFSTR("title");
94 static CFStringRef visitCountKey = CFSTR("visitCount");
95
96 HRESULT STDMETHODCALLTYPE WebHistoryItem::initFromDictionaryRepresentation(void* dictionary)
97 {
98     CFDictionaryRef dictionaryRef = (CFDictionaryRef) dictionary;
99     HRESULT hr = S_OK;
100     int visitedCount = 0;
101     CFAbsoluteTime lastVisitedTime = 0.0;
102
103     CFStringRef urlStringRef = (CFStringRef) CFDictionaryGetValue(dictionaryRef, urlKey);
104     if (urlStringRef && CFGetTypeID(urlStringRef) != CFStringGetTypeID()) {
105         hr = E_FAIL;
106         goto exit;
107     }
108
109     CFStringRef lastVisitedRef = (CFStringRef) CFDictionaryGetValue(dictionaryRef, lastVisitedDateKey);
110     if (!lastVisitedRef || CFGetTypeID(lastVisitedRef) != CFStringGetTypeID()) {
111         hr = E_FAIL;
112         goto exit;
113     }
114     lastVisitedTime = CFStringGetDoubleValue(lastVisitedRef);
115
116     CFStringRef titleRef = (CFStringRef) CFDictionaryGetValue(dictionaryRef, titleKey);
117     if (titleRef && CFGetTypeID(titleRef) != CFStringGetTypeID()) {
118         hr = E_FAIL;
119         goto exit;
120     }
121
122     CFNumberRef visitCountRef = (CFNumberRef) CFDictionaryGetValue(dictionaryRef, visitCountKey);
123     if (!visitCountRef || CFGetTypeID(visitCountRef) != CFNumberGetTypeID()) {
124         hr = E_FAIL;
125         goto exit;
126     }
127
128     historyItemWrappers().remove(m_historyItem.get());
129     m_historyItem = new HistoryItem(urlStringRef, titleRef, lastVisitedTime);
130     historyItemWrappers().set(m_historyItem.get(), this);
131
132     if (!CFNumberGetValue(visitCountRef, kCFNumberIntType, &visitedCount)) {
133         hr = E_FAIL;
134         goto exit;
135     }
136
137     m_historyItem->setVisitCount(visitedCount);
138 exit:
139     return hr;
140 }
141
142 HRESULT STDMETHODCALLTYPE WebHistoryItem::dictionaryRepresentation(void** dictionary)
143 {
144     CFDictionaryRef* dictionaryRef = (CFDictionaryRef*) dictionary;
145     static CFStringRef lastVisitedFormat = CFSTR("%.1lf");
146     CFStringRef lastVisitedStringRef =
147         CFStringCreateWithFormat(0, 0, lastVisitedFormat, m_historyItem->lastVisitedTime());
148     if (!lastVisitedStringRef)
149         return E_FAIL;
150
151     int keyCount = 0;
152     CFTypeRef keys[4];
153     CFTypeRef values[4];
154     if (!m_historyItem->urlString().isEmpty()) {
155         keys[keyCount] = urlKey;
156         values[keyCount++] = m_historyItem->urlString().createCFString();
157     }
158
159     keys[keyCount] = lastVisitedDateKey;
160     values[keyCount++] = lastVisitedStringRef;
161     
162     if (!m_historyItem->title().isEmpty()) {
163         keys[keyCount] = titleKey;
164         values[keyCount++] = m_historyItem->title().createCFString();
165     }
166     
167     keys[keyCount]   = visitCountKey;
168     int visitCount = m_historyItem->visitCount();
169     values[keyCount++] = CFNumberCreate(0, kCFNumberIntType, &visitCount);
170     
171     *dictionaryRef = CFDictionaryCreate(0, keys, values, keyCount, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
172     
173     for (int i = 0; i < keyCount; ++i)
174         CFRelease(values[i]);
175
176     return S_OK;
177 }
178
179 HRESULT STDMETHODCALLTYPE WebHistoryItem::hasURLString(BOOL *hasURL)
180 {
181     *hasURL = m_historyItem->urlString().isEmpty() ? FALSE : TRUE;
182     return S_OK;
183 }
184
185 HRESULT STDMETHODCALLTYPE WebHistoryItem::visitCount(int *count)
186 {
187     *count = m_historyItem->visitCount();
188     return S_OK;
189 }
190
191 HRESULT STDMETHODCALLTYPE WebHistoryItem::setVisitCount(int count)
192 {
193     m_historyItem->setVisitCount(count);
194     return S_OK;
195 }
196
197 HRESULT STDMETHODCALLTYPE WebHistoryItem::mergeAutoCompleteHints(IWebHistoryItem* otherItem)
198 {
199     if (!otherItem)
200         return E_FAIL;
201
202     if (otherItem == this)
203         return S_OK;
204
205     IWebHistoryItemPrivate* otherItemPriv;
206     HRESULT hr = otherItem->QueryInterface(IID_IWebHistoryItemPrivate, (void**)&otherItemPriv);
207     if (FAILED(hr))
208         return hr;
209
210     int otherVisitCount;
211     hr = otherItemPriv->visitCount(&otherVisitCount);
212     otherItemPriv->Release();
213     if (FAILED(hr))
214         return hr;
215
216     m_historyItem->setVisitCount(otherVisitCount);
217
218     return S_OK;
219 }
220
221 HRESULT STDMETHODCALLTYPE WebHistoryItem::setLastVisitedTimeInterval(DATE time)
222 {
223     m_historyItem->setLastVisitedTime(MarshallingHelpers::DATEToCFAbsoluteTime(time));
224     return S_OK;
225 }
226
227 HRESULT STDMETHODCALLTYPE WebHistoryItem::setTitle(BSTR title)
228 {
229     m_historyItem->setTitle(String(title, SysStringLen(title)));
230
231     return S_OK;
232 }
233
234 HRESULT STDMETHODCALLTYPE WebHistoryItem::RSSFeedReferrer(BSTR* url)
235 {
236     BString str(m_historyItem->rssFeedReferrer());
237     *url = str.release();
238
239     return S_OK;
240 }
241
242 HRESULT STDMETHODCALLTYPE WebHistoryItem::setRSSFeedReferrer(BSTR url)
243 {
244     m_historyItem->setRSSFeedReferrer(String(url, SysStringLen(url)));
245
246     return S_OK;
247 }
248
249 HRESULT STDMETHODCALLTYPE WebHistoryItem::hasPageCache(BOOL* /*hasCache*/)
250 {
251     // FIXME - TODO
252     ASSERT_NOT_REACHED();
253     return E_NOTIMPL;
254 }
255
256 HRESULT STDMETHODCALLTYPE WebHistoryItem::setHasPageCache(BOOL /*hasCache*/)
257 {
258     // FIXME - TODO
259     return E_NOTIMPL;
260 }
261
262 HRESULT STDMETHODCALLTYPE WebHistoryItem::target(BSTR* target)
263 {
264     if (!target) {
265         ASSERT_NOT_REACHED();
266         return E_POINTER;
267     }
268
269     *target = BString(m_historyItem->target()).release();
270     return S_OK;
271 }
272
273 HRESULT STDMETHODCALLTYPE WebHistoryItem::isTargetItem(BOOL* result)
274 {
275     if (!result) {
276         ASSERT_NOT_REACHED();
277         return E_POINTER;
278     }
279
280     *result = m_historyItem->isTargetItem() ? TRUE : FALSE;
281     return S_OK;
282 }
283
284 HRESULT STDMETHODCALLTYPE WebHistoryItem::children(unsigned* outChildCount, SAFEARRAY** outChildren)
285 {
286     if (!outChildCount || !outChildren) {
287         ASSERT_NOT_REACHED();
288         return E_POINTER;
289     }
290
291     *outChildCount = 0;
292     *outChildren = 0;
293
294     const HistoryItemVector& coreChildren = m_historyItem->children();
295     if (coreChildren.isEmpty())
296         return S_OK;
297     size_t childCount = coreChildren.size();
298
299     SAFEARRAY* children = SafeArrayCreateVector(VT_UNKNOWN, 0, static_cast<ULONG>(childCount));
300     if (!children)
301         return E_OUTOFMEMORY;
302
303     for (unsigned i = 0; i < childCount; ++i) {
304         WebHistoryItem* item = WebHistoryItem::createInstance(coreChildren[i]);
305         if (!item) {
306             SafeArrayDestroy(children);
307             return E_OUTOFMEMORY;
308         }
309
310         COMPtr<IUnknown> unknown;
311         HRESULT hr = item->QueryInterface(IID_IUnknown, (void**)&unknown);
312         if (FAILED(hr)) {
313             SafeArrayDestroy(children);
314             return hr;
315         }
316
317         LONG longI = i;
318         hr = SafeArrayPutElement(children, &longI, unknown.get());
319         if (FAILED(hr)) {
320             SafeArrayDestroy(children);
321             return hr;
322         }
323     }
324
325     *outChildCount = static_cast<unsigned>(childCount);
326     *outChildren = children;
327     return S_OK;
328
329 }
330
331 // IUnknown -------------------------------------------------------------------
332
333 HRESULT STDMETHODCALLTYPE WebHistoryItem::QueryInterface(REFIID riid, void** ppvObject)
334 {
335     *ppvObject = 0;
336     if (IsEqualGUID(riid, CLSID_WebHistoryItem))
337         *ppvObject = this;
338     else if (IsEqualGUID(riid, IID_IUnknown))
339         *ppvObject = static_cast<IWebHistoryItem*>(this);
340     else if (IsEqualGUID(riid, IID_IWebHistoryItem))
341         *ppvObject = static_cast<IWebHistoryItem*>(this);
342     else if (IsEqualGUID(riid, IID_IWebHistoryItemPrivate))
343         *ppvObject = static_cast<IWebHistoryItemPrivate*>(this);
344     else
345         return E_NOINTERFACE;
346
347     AddRef();
348     return S_OK;
349 }
350
351 ULONG STDMETHODCALLTYPE WebHistoryItem::AddRef(void)
352 {
353     return ++m_refCount;
354 }
355
356 ULONG STDMETHODCALLTYPE WebHistoryItem::Release(void)
357 {
358     ULONG newRef = --m_refCount;
359     if (!newRef)
360         delete(this);
361
362     return newRef;
363 }
364
365 // IWebHistoryItem -------------------------------------------------------------
366
367 HRESULT STDMETHODCALLTYPE WebHistoryItem::initWithURLString(
368     /* [in] */ BSTR urlString,
369     /* [in] */ BSTR title,
370     /* [in] */ DATE lastVisited)
371 {
372     historyItemWrappers().remove(m_historyItem.get());
373     m_historyItem = new HistoryItem(String(urlString, SysStringLen(urlString)), String(title, SysStringLen(title)), MarshallingHelpers::DATEToCFAbsoluteTime(lastVisited));
374     historyItemWrappers().set(m_historyItem.get(), this);
375
376     return S_OK;
377 }
378
379 HRESULT STDMETHODCALLTYPE WebHistoryItem::originalURLString( 
380     /* [retval][out] */ BSTR* url)
381 {
382     if (!url)
383         return E_POINTER;
384
385     BString str = m_historyItem->originalURLString();
386     *url = str.release();
387     return S_OK;
388 }
389
390 HRESULT STDMETHODCALLTYPE WebHistoryItem::URLString( 
391     /* [retval][out] */ BSTR* url)
392 {
393     if (!url)
394         return E_POINTER;
395
396     BString str = m_historyItem->urlString();
397     *url = str.release();
398     return S_OK;
399 }
400
401 HRESULT STDMETHODCALLTYPE WebHistoryItem::title( 
402     /* [retval][out] */ BSTR* pageTitle)
403 {
404     if (!pageTitle)
405         return E_POINTER;
406
407     BString str(m_historyItem->title());
408     *pageTitle = str.release();
409     return S_OK;
410 }
411
412 HRESULT STDMETHODCALLTYPE WebHistoryItem::lastVisitedTimeInterval( 
413     /* [retval][out] */ DATE* lastVisited)
414 {
415     if (!lastVisited)
416         return E_POINTER;
417
418     *lastVisited = MarshallingHelpers::CFAbsoluteTimeToDATE(m_historyItem->lastVisitedTime());
419     return S_OK;
420 }
421
422 HRESULT STDMETHODCALLTYPE WebHistoryItem::setAlternateTitle( 
423     /* [in] */ BSTR title)
424 {
425     m_alternateTitle = title;
426     return S_OK;
427 }
428
429 HRESULT STDMETHODCALLTYPE WebHistoryItem::alternateTitle( 
430     /* [retval][out] */ BSTR* title)
431 {
432     if (!title) {
433         ASSERT_NOT_REACHED();
434         return E_POINTER;
435     }
436
437     *title = m_alternateTitle;
438     return S_OK;
439 }
440
441 HRESULT STDMETHODCALLTYPE WebHistoryItem::icon( 
442     /* [out, retval] */ OLE_HANDLE* /*hBitmap*/)
443 {
444     ASSERT_NOT_REACHED();
445     return E_NOTIMPL;
446 }
447
448 // WebHistoryItem -------------------------------------------------------------
449 HistoryItem* WebHistoryItem::historyItem() const
450 {
451     return m_historyItem.get();
452 }