forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebHistory.cpp
More file actions
508 lines (415 loc) · 14.8 KB
/
WebHistory.cpp
File metadata and controls
508 lines (415 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright (C) 2006-2007, 2014-2015 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 INC. OR
* 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.
*/
#include "WebKitDLL.h"
#include "WebHistory.h"
#include "MemoryStream.h"
#include "WebKit.h"
#include "MarshallingHelpers.h"
#include "WebHistoryItem.h"
#include "WebKit.h"
#include "WebNotificationCenter.h"
#include "WebPreferences.h"
#include "WebVisitedLinkStore.h"
#include <WebCore/BString.h>
#include <WebCore/HistoryItem.h>
#include <WebCore/SharedBuffer.h>
#include <functional>
#include <wtf/DateMath.h>
#include <wtf/StdLibExtras.h>
#include <wtf/URL.h>
#include <wtf/Vector.h>
#if USE(CF)
#include "CFDictionaryPropertyBag.h"
#include <CoreFoundation/CoreFoundation.h>
#else
#include "COMPropertyBag.h"
#endif
using namespace WebCore;
static COMPtr<IPropertyBag> createUserInfoFromArray(BSTR notificationStr, IWebHistoryItem** data, size_t size)
{
#if USE(CF)
RetainPtr<CFArrayRef> arrayItem = adoptCF(CFArrayCreate(kCFAllocatorDefault, (const void**)data, size, &MarshallingHelpers::kIUnknownArrayCallBacks));
RetainPtr<CFMutableDictionaryRef> dictionary = adoptCF(
CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
auto key = MarshallingHelpers::BSTRToCFStringRef(notificationStr);
CFDictionaryAddValue(dictionary.get(), key.get(), arrayItem.get());
COMPtr<CFDictionaryPropertyBag> result = CFDictionaryPropertyBag::createInstance();
result->setDictionary(dictionary.get());
return COMPtr<IPropertyBag>(AdoptCOM, result.leakRef());
#else
Vector<COMPtr<IWebHistoryItem>, 1> arrayItem;
arrayItem.reserveInitialCapacity(size);
for (size_t i = 0; i < size; ++i)
arrayItem.uncheckedAppend(data[i]);
HashMap<String, Vector<COMPtr<IWebHistoryItem>>> dictionary;
String key(notificationStr, ::SysStringLen(notificationStr));
dictionary.set(key, WTFMove(arrayItem));
return COMPtr<IPropertyBag>(AdoptCOM, COMPropertyBag<Vector<COMPtr<IWebHistoryItem>>>::adopt(dictionary));
#endif
}
static inline COMPtr<IPropertyBag> createUserInfoFromHistoryItem(BSTR notificationStr, IWebHistoryItem* item)
{
return createUserInfoFromArray(notificationStr, &item, 1);
}
// WebHistory -----------------------------------------------------------------
WebHistory::WebHistory()
{
gClassCount++;
gClassNameCount().add("WebHistory");
m_preferences = WebPreferences::sharedStandardPreferences();
}
WebHistory::~WebHistory()
{
gClassCount--;
gClassNameCount().remove("WebHistory");
}
WebHistory* WebHistory::createInstance()
{
WebHistory* instance = new WebHistory();
instance->AddRef();
return instance;
}
HRESULT WebHistory::postNotification(NotificationType notifyType, IPropertyBag* userInfo /*=0*/)
{
IWebNotificationCenter* nc = WebNotificationCenter::defaultCenterInternal();
HRESULT hr = nc->postNotificationName(getNotificationString(notifyType), static_cast<IWebHistory*>(this), userInfo);
if (FAILED(hr))
return hr;
return S_OK;
}
BSTR WebHistory::getNotificationString(NotificationType notifyType)
{
static BSTR keys[6] = {0};
if (!keys[0]) {
keys[0] = SysAllocString(WebHistoryItemsAddedNotification);
keys[1] = SysAllocString(WebHistoryItemsRemovedNotification);
keys[2] = SysAllocString(WebHistoryAllItemsRemovedNotification);
keys[3] = SysAllocString(WebHistoryLoadedNotification);
keys[4] = SysAllocString(WebHistoryItemsDiscardedWhileLoadingNotification);
keys[5] = SysAllocString(WebHistorySavedNotification);
}
return keys[notifyType];
}
// IUnknown -------------------------------------------------------------------
HRESULT WebHistory::QueryInterface(_In_ REFIID riid, _COM_Outptr_ void** ppvObject)
{
if (!ppvObject)
return E_POINTER;
*ppvObject = nullptr;
if (IsEqualGUID(riid, CLSID_WebHistory))
*ppvObject = this;
else if (IsEqualGUID(riid, IID_IUnknown))
*ppvObject = static_cast<IWebHistory*>(this);
else if (IsEqualGUID(riid, IID_IWebHistory))
*ppvObject = static_cast<IWebHistory*>(this);
else if (IsEqualGUID(riid, IID_IWebHistoryPrivate))
*ppvObject = static_cast<IWebHistoryPrivate*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
ULONG WebHistory::AddRef()
{
return ++m_refCount;
}
ULONG WebHistory::Release()
{
ULONG newRef = --m_refCount;
if (!newRef)
delete(this);
return newRef;
}
// IWebHistory ----------------------------------------------------------------
static COMPtr<WebHistory>& sharedHistoryStorage()
{
static NeverDestroyed<COMPtr<WebHistory>> sharedHistory;
return sharedHistory;
}
WebHistory* WebHistory::sharedHistory()
{
return sharedHistoryStorage().get();
}
HRESULT WebHistory::optionalSharedHistory(_COM_Outptr_opt_ IWebHistory** history)
{
if (!history)
return E_POINTER;
*history = sharedHistory();
if (*history)
(*history)->AddRef();
return S_OK;
}
HRESULT WebHistory::setOptionalSharedHistory(_In_opt_ IWebHistory* history)
{
if (sharedHistoryStorage() == history)
return S_OK;
sharedHistoryStorage().query(history);
WebVisitedLinkStore::setShouldTrackVisitedLinks(sharedHistoryStorage());
WebVisitedLinkStore::removeAllVisitedLinks();
return S_OK;
}
HRESULT WebHistory::unused1()
{
ASSERT_NOT_REACHED();
return E_FAIL;
}
HRESULT WebHistory::unused2()
{
ASSERT_NOT_REACHED();
return E_FAIL;
}
HRESULT WebHistory::addItems(int itemCount, __deref_in_ecount_opt(itemCount) IWebHistoryItem** items)
{
// There is no guarantee that the incoming entries are in any particular
// order, but if this is called with a set of entries that were created by
// iterating through the results of orderedLastVisitedDays and orderedItemsLastVisitedOnDay
// then they will be ordered chronologically from newest to oldest. We can make adding them
// faster (fewer compares) by inserting them from oldest to newest.
HRESULT hr;
for (int i = itemCount - 1; i >= 0; --i) {
hr = addItem(items[i], false, 0);
if (FAILED(hr))
return hr;
}
return S_OK;
}
HRESULT WebHistory::removeItems(int itemCount, __deref_in_ecount_opt(itemCount) IWebHistoryItem** items)
{
HRESULT hr;
for (int i = 0; i < itemCount; ++i) {
hr = removeItem(items[i]);
if (FAILED(hr))
return hr;
}
return S_OK;
}
HRESULT WebHistory::removeAllItems()
{
Vector<IWebHistoryItem*> itemsVector;
itemsVector.reserveInitialCapacity(m_entriesByURL.size());
for (auto it = m_entriesByURL.begin(); it != m_entriesByURL.end(); ++it)
itemsVector.append(it->value.get());
COMPtr<IPropertyBag> userInfo = createUserInfoFromArray(getNotificationString(kWebHistoryAllItemsRemovedNotification), itemsVector.data(), itemsVector.size());
m_entriesByURL.clear();
WebVisitedLinkStore::removeAllVisitedLinks();
return postNotification(kWebHistoryAllItemsRemovedNotification, userInfo.get());
}
// FIXME: This function should be removed from the IWebHistory interface.
HRESULT WebHistory::orderedLastVisitedDays(_Inout_ int* count, _In_ DATE* calendarDates)
{
return E_NOTIMPL;
}
// FIXME: This function should be removed from the IWebHistory interface.
HRESULT WebHistory::orderedItemsLastVisitedOnDay(_Inout_ int* count, __deref_in_opt IWebHistoryItem** items, DATE calendarDate)
{
return E_NOTIMPL;
}
HRESULT WebHistory::allItems(_Inout_ int* count, __deref_opt_out IWebHistoryItem** items)
{
int entriesByURLCount = m_entriesByURL.size();
if (!items) {
*count = entriesByURLCount;
return S_OK;
}
if (*count < entriesByURLCount) {
*count = entriesByURLCount;
return E_NOT_SUFFICIENT_BUFFER;
}
*count = entriesByURLCount;
int i = 0;
for (auto it = m_entriesByURL.begin(); it != m_entriesByURL.end(); ++i, ++it) {
items[i] = it->value.get();
items[i]->AddRef();
}
return S_OK;
}
HRESULT WebHistory::setVisitedLinkTrackingEnabled(BOOL visitedLinkTrackingEnabled)
{
WebVisitedLinkStore::setShouldTrackVisitedLinks(visitedLinkTrackingEnabled);
return S_OK;
}
HRESULT WebHistory::removeAllVisitedLinks()
{
WebVisitedLinkStore::removeAllVisitedLinks();
return S_OK;
}
HRESULT WebHistory::setHistoryItemLimit(int limit)
{
if (!m_preferences)
return E_FAIL;
return m_preferences->setHistoryItemLimit(limit);
}
HRESULT WebHistory::historyItemLimit(_Out_ int* limit)
{
if (!limit)
return E_POINTER;
*limit = 0;
if (!m_preferences)
return E_FAIL;
return m_preferences->historyItemLimit(limit);
}
HRESULT WebHistory::setHistoryAgeInDaysLimit(int limit)
{
if (!m_preferences)
return E_FAIL;
return m_preferences->setHistoryAgeInDaysLimit(limit);
}
HRESULT WebHistory::historyAgeInDaysLimit(_Out_ int* limit)
{
if (!limit)
return E_POINTER;
*limit = 0;
if (!m_preferences)
return E_FAIL;
return m_preferences->historyAgeInDaysLimit(limit);
}
HRESULT WebHistory::removeItem(IWebHistoryItem* entry)
{
HRESULT hr = S_OK;
BString urlBStr;
hr = entry->URLString(&urlBStr);
if (FAILED(hr))
return hr;
String urlString(urlBStr, SysStringLen(urlBStr));
if (urlString.isEmpty())
return E_FAIL;
auto it = m_entriesByURL.find(urlString);
if (it == m_entriesByURL.end())
return E_FAIL;
// If this exact object isn't stored, then make no change.
// FIXME: Is this the right behavior if this entry isn't present, but another entry for the same URL is?
// Maybe need to change the API to make something like removeEntryForURLString public instead.
if (it->value.get() != entry)
return E_FAIL;
hr = removeItemForURLString(urlString);
if (FAILED(hr))
return hr;
COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
getNotificationString(kWebHistoryItemsRemovedNotification), entry);
hr = postNotification(kWebHistoryItemsRemovedNotification, userInfo.get());
return hr;
}
HRESULT WebHistory::addItem(IWebHistoryItem* entry, bool discardDuplicate, bool* added)
{
HRESULT hr = S_OK;
if (!entry)
return E_FAIL;
BString urlBStr;
hr = entry->URLString(&urlBStr);
if (FAILED(hr))
return hr;
String urlString(urlBStr, SysStringLen(urlBStr));
if (urlString.isEmpty())
return E_FAIL;
COMPtr<IWebHistoryItem> oldEntry(m_entriesByURL.get(urlString));
if (oldEntry) {
if (discardDuplicate) {
if (added)
*added = false;
return S_OK;
}
removeItemForURLString(urlString);
// If we already have an item with this URL, we need to merge info that drives the
// URL autocomplete heuristics from that item into the new one.
IWebHistoryItemPrivate* entryPriv;
hr = entry->QueryInterface(IID_IWebHistoryItemPrivate, (void**)&entryPriv);
if (SUCCEEDED(hr)) {
entryPriv->mergeAutoCompleteHints(oldEntry.get());
entryPriv->Release();
}
}
m_entriesByURL.set(urlString, entry);
COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
getNotificationString(kWebHistoryItemsAddedNotification), entry);
hr = postNotification(kWebHistoryItemsAddedNotification, userInfo.get());
if (added)
*added = true;
return hr;
}
void WebHistory::visitedURL(const URL& url, const String& title, const String& httpMethod, bool wasFailure, bool increaseVisitCount)
{
const String& urlString = url.string();
if (urlString.isEmpty())
return;
IWebHistoryItem* entry = m_entriesByURL.get(urlString);
if (!entry) {
COMPtr<WebHistoryItem> item(AdoptCOM, WebHistoryItem::createInstance());
if (!item)
return;
entry = item.get();
SYSTEMTIME currentTime;
GetSystemTime(¤tTime);
DATE lastVisited;
if (!SystemTimeToVariantTime(¤tTime, &lastVisited))
return;
if (FAILED(entry->initWithURLString(BString(urlString), BString(title), lastVisited)))
return;
m_entriesByURL.set(urlString, entry);
}
COMPtr<IWebHistoryItemPrivate> entryPrivate(Query, entry);
if (!entryPrivate)
return;
entryPrivate->setLastVisitWasFailure(wasFailure);
COMPtr<WebHistoryItem> item(Query, entry);
COMPtr<IPropertyBag> userInfo = createUserInfoFromHistoryItem(
getNotificationString(kWebHistoryItemsAddedNotification), entry);
postNotification(kWebHistoryItemsAddedNotification, userInfo.get());
}
HRESULT WebHistory::itemForURL(_In_ BSTR urlBStr, _COM_Outptr_opt_ IWebHistoryItem** item)
{
if (!item)
return E_POINTER;
*item = nullptr;
String urlString(urlBStr, SysStringLen(urlBStr));
if (urlString.isEmpty())
return E_FAIL;
auto it = m_entriesByURL.find(urlString);
if (it == m_entriesByURL.end())
return E_FAIL;
it->value.copyRefTo(item);
return S_OK;
}
HRESULT WebHistory::removeItemForURLString(const WTF::String& urlString)
{
if (urlString.isEmpty())
return E_FAIL;
auto it = m_entriesByURL.find(urlString);
if (it == m_entriesByURL.end())
return E_FAIL;
if (!m_entriesByURL.size())
WebVisitedLinkStore::removeAllVisitedLinks();
return S_OK;
}
COMPtr<IWebHistoryItem> WebHistory::itemForURLString(const String& urlString) const
{
if (urlString.isEmpty())
return nullptr;
return m_entriesByURL.get(urlString);
}
void WebHistory::addVisitedLinksToVisitedLinkStore(WebVisitedLinkStore& visitedLinkStore)
{
for (auto& url : m_entriesByURL.keys())
visitedLinkStore.addVisitedLink(url);
}