forked from qt/qtwebkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIconDatabase.cpp
More file actions
2140 lines (1696 loc) · 87.3 KB
/
Copy pathIconDatabase.cpp
File metadata and controls
2140 lines (1696 loc) · 87.3 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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
* Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
*
* 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 "config.h"
#include "IconDatabase.h"
#if ENABLE(ICONDATABASE)
#include "DocumentLoader.h"
#include "FileSystem.h"
#include "IconDatabaseClient.h"
#include "IconRecord.h"
#include "Image.h"
#include "Logging.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include "SuddenTermination.h"
#include <wtf/AutodrainedPool.h>
#include <wtf/MainThread.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
// For methods that are meant to support API from the main thread - should not be called internally
#define ASSERT_NOT_SYNC_THREAD() ASSERT(!m_syncThreadRunning || !IS_ICON_SYNC_THREAD())
// For methods that are meant to support the sync thread ONLY
#define IS_ICON_SYNC_THREAD() (m_syncThread == currentThread())
#define ASSERT_ICON_SYNC_THREAD() ASSERT(IS_ICON_SYNC_THREAD())
#if PLATFORM(QT) || PLATFORM(GTK)
#define CAN_THEME_URL_ICON
#endif
namespace WebCore {
static int databaseCleanupCounter = 0;
// This version number is in the DB and marks the current generation of the schema
// Currently, a mismatched schema causes the DB to be wiped and reset. This isn't
// so bad during development but in the future, we would need to write a conversion
// function to advance older released schemas to "current"
static const int currentDatabaseVersion = 6;
// Icons expire once every 4 days
static const int iconExpirationTime = 60*60*24*4;
static const int updateTimerDelay = 5;
static bool checkIntegrityOnOpen = false;
#if PLATFORM(GTK) || PLATFORM(EFL)
// We are not interested in icons that have been unused for more than
// 30 days, delete them even if they have not been explicitly released.
static const int notUsedIconExpirationTime = 60*60*24*30;
#endif
#if !LOG_DISABLED || !ERROR_DISABLED
static String urlForLogging(const String& url)
{
static const unsigned urlTruncationLength = 120;
if (url.length() < urlTruncationLength)
return url;
return url.substring(0, urlTruncationLength) + "...";
}
#endif
class DefaultIconDatabaseClient final : public IconDatabaseClient {
WTF_MAKE_FAST_ALLOCATED;
public:
virtual void didImportIconURLForPageURL(const String&) override { }
virtual void didImportIconDataForPageURL(const String&) override { }
virtual void didChangeIconForPageURL(const String&) override { }
virtual void didRemoveAllIcons() override { }
virtual void didFinishURLImport() override { }
};
static IconDatabaseClient* defaultClient()
{
static IconDatabaseClient* defaultClient = new DefaultIconDatabaseClient;
return defaultClient;
}
// ************************
// *** Main Thread Only ***
// ************************
void IconDatabase::setClient(IconDatabaseClient* client)
{
// We don't allow a null client, because we never null check it anywhere in this code
// Also don't allow a client change after the thread has already began
// (setting the client should occur before the database is opened)
ASSERT(client);
ASSERT(!m_syncThreadRunning);
if (!client || m_syncThreadRunning)
return;
m_client = client;
}
bool IconDatabase::open(const String& directory, const String& filename)
{
ASSERT_NOT_SYNC_THREAD();
if (!m_isEnabled)
return false;
if (isOpen()) {
LOG_ERROR("Attempt to reopen the IconDatabase which is already open. Must close it first.");
return false;
}
m_databaseDirectory = directory.isolatedCopy();
// Formulate the full path for the database file
m_completeDatabasePath = pathByAppendingComponent(m_databaseDirectory, filename);
// Lock here as well as first thing in the thread so the thread doesn't actually commence until the createThread() call
// completes and m_syncThreadRunning is properly set
m_syncLock.lock();
m_syncThread = createThread(IconDatabase::iconDatabaseSyncThreadStart, this, "WebCore: IconDatabase");
m_syncThreadRunning = m_syncThread;
m_syncLock.unlock();
if (!m_syncThread)
return false;
return true;
}
void IconDatabase::close()
{
ASSERT_NOT_SYNC_THREAD();
if (m_syncThreadRunning) {
// Set the flag to tell the sync thread to wrap it up
m_threadTerminationRequested = true;
// Wake up the sync thread if it's waiting
wakeSyncThread();
// Wait for the sync thread to terminate
waitForThreadCompletion(m_syncThread);
}
m_syncThreadRunning = false;
m_threadTerminationRequested = false;
m_removeIconsRequested = false;
m_syncDB.close();
// If there are still main thread callbacks in flight then the database might not actually be closed yet.
// But if it is closed, notify the client now.
if (!isOpen() && m_client)
m_client->didClose();
}
void IconDatabase::removeAllIcons()
{
ASSERT_NOT_SYNC_THREAD();
if (!isOpen())
return;
LOG(IconDatabase, "Requesting background thread to remove all icons");
// Clear the in-memory record of every IconRecord, anything waiting to be read from disk, and anything waiting to be written to disk
{
LockHolder locker(m_urlAndIconLock);
// Clear the IconRecords for every page URL - RefCounting will cause the IconRecords themselves to be deleted
// We don't delete the actual PageRecords because we have the "retain icon for url" count to keep track of
for (auto& pageURL : m_pageURLToRecordMap.values())
pageURL->setIconRecord(nullptr);
// Clear the iconURL -> IconRecord map
m_iconURLToRecordMap.clear();
// Clear all in-memory records of things that need to be synced out to disk
{
LockHolder locker(m_pendingSyncLock);
m_pageURLsPendingSync.clear();
m_iconsPendingSync.clear();
}
// Clear all in-memory records of things that need to be read in from disk
{
LockHolder locker(m_pendingReadingLock);
m_pageURLsPendingImport.clear();
m_pageURLsInterestedInIcons.clear();
m_iconsPendingReading.clear();
m_loadersPendingDecision.clear();
}
}
m_removeIconsRequested = true;
wakeSyncThread();
}
Image* IconDatabase::synchronousIconForPageURL(const String& pageURLOriginal, const IntSize& size)
{
ASSERT_NOT_SYNC_THREAD();
// pageURLOriginal cannot be stored without being deep copied first.
// We should go our of our way to only copy it if we have to store it
if (!isOpen() || !documentCanHaveIcon(pageURLOriginal))
return nullptr;
LockHolder locker(m_urlAndIconLock);
performPendingRetainAndReleaseOperations();
String pageURLCopy; // Creates a null string for easy testing
PageURLRecord* pageRecord = m_pageURLToRecordMap.get(pageURLOriginal);
if (!pageRecord) {
pageURLCopy = pageURLOriginal.isolatedCopy();
pageRecord = getOrCreatePageURLRecord(pageURLCopy);
}
// If pageRecord is NULL, one of two things is true -
// 1 - The initial url import is incomplete and this pageURL was marked to be notified once it is complete if an iconURL exists
// 2 - The initial url import IS complete and this pageURL has no icon
if (!pageRecord) {
LockHolder locker(m_pendingReadingLock);
// Import is ongoing, there might be an icon. In this case, register to be notified when the icon comes in
// If we ever reach this condition, we know we've already made the pageURL copy
if (!m_iconURLImportComplete)
m_pageURLsInterestedInIcons.add(pageURLCopy);
return nullptr;
}
IconRecord* iconRecord = pageRecord->iconRecord();
// If the initial URL import isn't complete, it's possible to have a PageURL record without an associated icon
// In this case, the pageURL is already in the set to alert the client when the iconURL mapping is complete so
// we can just bail now
if (!m_iconURLImportComplete && !iconRecord)
return nullptr;
// Assuming we're done initializing and cleanup is allowed,
// the only way we should *not* have an icon record is if this pageURL is retained but has no icon yet.
ASSERT(iconRecord || databaseCleanupCounter || m_retainedPageURLs.contains(pageURLOriginal));
if (!iconRecord)
return nullptr;
// If it's a new IconRecord object that doesn't have its imageData set yet,
// mark it to be read by the background thread
if (iconRecord->imageDataStatus() == ImageDataStatusUnknown) {
if (pageURLCopy.isNull())
pageURLCopy = pageURLOriginal.isolatedCopy();
LockHolder locker(m_pendingReadingLock);
m_pageURLsInterestedInIcons.add(pageURLCopy);
m_iconsPendingReading.add(iconRecord);
wakeSyncThread();
return nullptr;
}
// If the size parameter was (0, 0) that means the caller of this method just wanted the read from disk to be kicked off
// and isn't actually interested in the image return value
if (size == IntSize(0, 0))
return nullptr;
// PARANOID DISCUSSION: This method makes some assumptions. It returns a WebCore::image which the icon database might dispose of at anytime in the future,
// and Images aren't ref counted. So there is no way for the client to guarantee continued existence of the image.
// This has *always* been the case, but in practice clients would always create some other platform specific representation of the image
// and drop the raw Image*. On Mac an NSImage, and on windows drawing into an HBITMAP.
// The async aspect adds a huge question - what if the image is deleted before the platform specific API has a chance to create its own
// representation out of it?
// If an image is read in from the icondatabase, we do *not* overwrite any image data that exists in the in-memory cache.
// This is because we make the assumption that anything in memory is newer than whatever is in the database.
// So the only time the data will be set from the second thread is when it is INITIALLY being read in from the database, but we would never
// delete the image on the secondary thread if the image already exists.
return iconRecord->image(size);
}
PassNativeImagePtr IconDatabase::synchronousNativeIconForPageURL(const String& pageURLOriginal, const IntSize& size)
{
Image* icon = synchronousIconForPageURL(pageURLOriginal, size);
if (!icon)
return nullptr;
LockHolder locker(m_urlAndIconLock);
return icon->nativeImageForCurrentFrame();
}
void IconDatabase::readIconForPageURLFromDisk(const String& pageURL)
{
// The effect of asking for an Icon for a pageURL automatically queues it to be read from disk
// if it hasn't already been set in memory. The special IntSize (0, 0) is a special way of telling
// that method "I don't care about the actual Image, i just want you to make sure you're getting it from disk.
synchronousIconForPageURL(pageURL, IntSize(0, 0));
}
String IconDatabase::synchronousIconURLForPageURL(const String& pageURLOriginal)
{
ASSERT_NOT_SYNC_THREAD();
// Cannot do anything with pageURLOriginal that would end up storing it without deep copying first
// Also, in the case we have a real answer for the caller, we must deep copy that as well
if (!isOpen() || !documentCanHaveIcon(pageURLOriginal))
return String();
LockHolder locker(m_urlAndIconLock);
PageURLRecord* pageRecord = m_pageURLToRecordMap.get(pageURLOriginal);
if (!pageRecord)
pageRecord = getOrCreatePageURLRecord(pageURLOriginal.isolatedCopy());
// If pageRecord is NULL, one of two things is true -
// 1 - The initial url import is incomplete and this pageURL has already been marked to be notified once it is complete if an iconURL exists
// 2 - The initial url import IS complete and this pageURL has no icon
if (!pageRecord)
return String();
// Possible the pageRecord is around because it's a retained pageURL with no iconURL, so we have to check
return pageRecord->iconRecord() ? pageRecord->iconRecord()->iconURL().isolatedCopy() : String();
}
#ifdef CAN_THEME_URL_ICON
static inline void loadDefaultIconRecord(IconRecord* defaultIconRecord)
{
defaultIconRecord->loadImageFromResource("urlIcon");
}
#else
static inline void loadDefaultIconRecord(IconRecord* defaultIconRecord)
{
static const unsigned char defaultIconData[] = { 0x4D, 0x4D, 0x00, 0x2A, 0x00, 0x00, 0x03, 0x32, 0x80, 0x00, 0x20, 0x50, 0x38, 0x24, 0x16, 0x0D, 0x07, 0x84, 0x42, 0x61, 0x50, 0xB8,
0x64, 0x08, 0x18, 0x0D, 0x0A, 0x0B, 0x84, 0xA2, 0xA1, 0xE2, 0x08, 0x5E, 0x39, 0x28, 0xAF, 0x48, 0x24, 0xD3, 0x53, 0x9A, 0x37, 0x1D, 0x18, 0x0E, 0x8A, 0x4B, 0xD1, 0x38,
0xB0, 0x7C, 0x82, 0x07, 0x03, 0x82, 0xA2, 0xE8, 0x6C, 0x2C, 0x03, 0x2F, 0x02, 0x82, 0x41, 0xA1, 0xE2, 0xF8, 0xC8, 0x84, 0x68, 0x6D, 0x1C, 0x11, 0x0A, 0xB7, 0xFA, 0x91,
0x6E, 0xD1, 0x7F, 0xAF, 0x9A, 0x4E, 0x87, 0xFB, 0x19, 0xB0, 0xEA, 0x7F, 0xA4, 0x95, 0x8C, 0xB7, 0xF9, 0xA9, 0x0A, 0xA9, 0x7F, 0x8C, 0x88, 0x66, 0x96, 0xD4, 0xCA, 0x69,
0x2F, 0x00, 0x81, 0x65, 0xB0, 0x29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 0x66,
0x1E, 0x07, 0xB3, 0x3C, 0x63, 0x1D, 0x35, 0x49, 0x0E, 0x63, 0x2D, 0xA2, 0xF1, 0x12, 0x60, 0x1C, 0xE0, 0xE0, 0x52, 0x1B, 0x8B, 0xAC, 0x38, 0x0E, 0x07, 0x03, 0x60, 0x28,
0x1C, 0x0E, 0x87, 0x00, 0xF0, 0x66, 0x27, 0x11, 0xA2, 0xC1, 0x02, 0x5A, 0x1C, 0xE4, 0x21, 0x83, 0x1F, 0x13, 0x86, 0xFA, 0xD2, 0x55, 0x1D, 0xD6, 0x61, 0xBC, 0x77, 0xD3,
0xE6, 0x91, 0xCB, 0x4C, 0x90, 0xA6, 0x25, 0xB8, 0x2F, 0x90, 0xC5, 0xA9, 0xCE, 0x12, 0x07, 0x02, 0x91, 0x1B, 0x9F, 0x68, 0x00, 0x16, 0x76, 0x0D, 0xA1, 0x00, 0x08, 0x06,
0x03, 0x81, 0xA0, 0x20, 0x1A, 0x0D, 0x06, 0x80, 0x30, 0x24, 0x12, 0x89, 0x20, 0x98, 0x4A, 0x1F, 0x0F, 0x21, 0xA0, 0x9E, 0x36, 0x16, 0xC2, 0x88, 0xE6, 0x48, 0x9B, 0x83,
0x31, 0x1C, 0x55, 0x1E, 0x43, 0x59, 0x1A, 0x56, 0x1E, 0x42, 0xF0, 0xFA, 0x4D, 0x1B, 0x9B, 0x08, 0xDC, 0x5B, 0x02, 0xA1, 0x30, 0x7E, 0x3C, 0xEE, 0x5B, 0xA6, 0xDD, 0xB8,
0x6D, 0x5B, 0x62, 0xB7, 0xCD, 0xF3, 0x9C, 0xEA, 0x04, 0x80, 0x80, 0x00, 0x00, 0x0E, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x01, 0x01,
0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x00, 0x00, 0x01, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x03, 0xE0, 0x01, 0x03, 0x00, 0x03, 0x00, 0x00,
0x00, 0x01, 0x00, 0x05, 0x00, 0x00, 0x01, 0x06, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x01, 0x11, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x08, 0x01, 0x15, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x01, 0x16, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x17,
0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x29, 0x01, 0x1A, 0x00, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0xE8, 0x01, 0x1B, 0x00, 0x05, 0x00, 0x00,
0x00, 0x01, 0x00, 0x00, 0x03, 0xF0, 0x01, 0x1C, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x28, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02,
0x00, 0x00, 0x01, 0x52, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x0A,
0xFC, 0x80, 0x00, 0x00, 0x27, 0x10, 0x00, 0x0A, 0xFC, 0x80, 0x00, 0x00, 0x27, 0x10 };
static SharedBuffer* defaultIconBuffer = SharedBuffer::create(defaultIconData, sizeof(defaultIconData)).leakRef();
defaultIconRecord->setImageData(defaultIconBuffer);
}
#endif
Image* IconDatabase::defaultIcon(const IntSize& size)
{
ASSERT_NOT_SYNC_THREAD();
if (!m_defaultIconRecord) {
m_defaultIconRecord = IconRecord::create("urlIcon");
loadDefaultIconRecord(m_defaultIconRecord.get());
}
return m_defaultIconRecord->image(size);
}
void IconDatabase::retainIconForPageURL(const String& pageURL)
{
ASSERT_NOT_SYNC_THREAD();
if (!isEnabled() || !documentCanHaveIcon(pageURL))
return;
{
LockHolder locker(m_urlsToRetainOrReleaseLock);
m_urlsToRetain.add(pageURL.isolatedCopy());
m_retainOrReleaseIconRequested = true;
}
scheduleOrDeferSyncTimer();
}
void IconDatabase::performRetainIconForPageURL(const String& pageURLOriginal, int retainCount)
{
PageURLRecord* record = m_pageURLToRecordMap.get(pageURLOriginal);
String pageURL;
if (!record) {
pageURL = pageURLOriginal.isolatedCopy();
record = new PageURLRecord(pageURL);
m_pageURLToRecordMap.set(pageURL, record);
}
if (!record->retain(retainCount)) {
if (pageURL.isNull())
pageURL = pageURLOriginal.isolatedCopy();
// This page just had its retain count bumped from 0 to 1 - Record that fact
m_retainedPageURLs.add(pageURL);
// If we read the iconURLs yet, we want to avoid any pageURL->iconURL lookups and the pageURLsPendingDeletion is moot,
// so we bail here and skip those steps
if (!m_iconURLImportComplete)
return;
LockHolder locker(m_pendingSyncLock);
// If this pageURL waiting to be sync'ed, update the sync record
// This saves us in the case where a page was ready to be deleted from the database but was just retained - so theres no need to delete it!
if (!m_privateBrowsingEnabled && m_pageURLsPendingSync.contains(pageURL)) {
LOG(IconDatabase, "Bringing %s back from the brink", pageURL.ascii().data());
m_pageURLsPendingSync.set(pageURL, record->snapshot());
}
}
}
void IconDatabase::releaseIconForPageURL(const String& pageURL)
{
ASSERT_NOT_SYNC_THREAD();
// Cannot do anything with pageURLOriginal that would end up storing it without deep copying first
if (!isEnabled() || !documentCanHaveIcon(pageURL))
return;
{
LockHolder locker(m_urlsToRetainOrReleaseLock);
m_urlsToRelease.add(pageURL.isolatedCopy());
m_retainOrReleaseIconRequested = true;
}
scheduleOrDeferSyncTimer();
}
void IconDatabase::performReleaseIconForPageURL(const String& pageURLOriginal, int releaseCount)
{
// Check if this pageURL is actually retained
if (!m_retainedPageURLs.contains(pageURLOriginal)) {
LOG_ERROR("Attempting to release icon for URL %s which is not retained", urlForLogging(pageURLOriginal).ascii().data());
return;
}
// Get its retain count - if it's retained, we'd better have a PageURLRecord for it
PageURLRecord* pageRecord = m_pageURLToRecordMap.get(pageURLOriginal);
ASSERT(pageRecord);
LOG(IconDatabase, "Releasing pageURL %s to a retain count of %i", urlForLogging(pageURLOriginal).ascii().data(), pageRecord->retainCount() - 1);
ASSERT(pageRecord->retainCount() > 0);
// If it still has a positive retain count, store the new count and bail
if (pageRecord->release(releaseCount))
return;
// This pageRecord has now been fully released. Do the appropriate cleanup
LOG(IconDatabase, "No more retainers for PageURL %s", urlForLogging(pageURLOriginal).ascii().data());
m_pageURLToRecordMap.remove(pageURLOriginal);
m_retainedPageURLs.remove(pageURLOriginal);
// Grab the iconRecord for later use (and do a sanity check on it for kicks)
IconRecord* iconRecord = pageRecord->iconRecord();
ASSERT(!iconRecord || (iconRecord && m_iconURLToRecordMap.get(iconRecord->iconURL()) == iconRecord));
{
LockHolder locker(m_pendingReadingLock);
// Since this pageURL is going away, there's no reason anyone would ever be interested in its read results
if (!m_iconURLImportComplete)
m_pageURLsPendingImport.remove(pageURLOriginal);
m_pageURLsInterestedInIcons.remove(pageURLOriginal);
// If this icon is down to it's last retainer, we don't care about reading it in from disk anymore
if (iconRecord && iconRecord->hasOneRef()) {
m_iconURLToRecordMap.remove(iconRecord->iconURL());
m_iconsPendingReading.remove(iconRecord);
}
}
// Mark stuff for deletion from the database only if we're not in private browsing
if (!m_privateBrowsingEnabled) {
LockHolder locker(m_pendingSyncLock);
m_pageURLsPendingSync.set(pageURLOriginal.isolatedCopy(), pageRecord->snapshot(true));
// If this page is the last page to refer to a particular IconRecord, that IconRecord needs to
// be marked for deletion
if (iconRecord && iconRecord->hasOneRef())
m_iconsPendingSync.set(iconRecord->iconURL(), iconRecord->snapshot(true));
}
delete pageRecord;
}
void IconDatabase::setIconDataForIconURL(PassRefPtr<SharedBuffer> dataOriginal, const String& iconURLOriginal)
{
ASSERT_NOT_SYNC_THREAD();
// Cannot do anything with dataOriginal or iconURLOriginal that would end up storing them without deep copying first
if (!isOpen() || iconURLOriginal.isEmpty())
return;
RefPtr<SharedBuffer> data = dataOriginal ? dataOriginal->copy() : PassRefPtr<SharedBuffer>(nullptr);
String iconURL = iconURLOriginal.isolatedCopy();
Vector<String> pageURLs;
{
LockHolder locker(m_urlAndIconLock);
// If this icon was pending a read, remove it from that set because this new data should override what is on disk
RefPtr<IconRecord> icon = m_iconURLToRecordMap.get(iconURL);
if (icon) {
LockHolder locker(m_pendingReadingLock);
m_iconsPendingReading.remove(icon.get());
} else
icon = getOrCreateIconRecord(iconURL);
// Update the data and set the time stamp
icon->setImageData(data.release());
icon->setTimestamp((int)currentTime());
// Copy the current retaining pageURLs - if any - to notify them of the change
pageURLs.appendRange(icon->retainingPageURLs().begin(), icon->retainingPageURLs().end());
// Mark the IconRecord as requiring an update to the database only if private browsing is disabled
if (!m_privateBrowsingEnabled) {
LockHolder locker(m_pendingSyncLock);
m_iconsPendingSync.set(iconURL, icon->snapshot());
}
if (icon->hasOneRef()) {
ASSERT(icon->retainingPageURLs().isEmpty());
LOG(IconDatabase, "Icon for icon url %s is about to be destroyed - removing mapping for it", urlForLogging(icon->iconURL()).ascii().data());
m_iconURLToRecordMap.remove(icon->iconURL());
}
}
// Send notification out regarding all PageURLs that retain this icon
// But not if we're on the sync thread because that implies this mapping
// comes from the initial import which we don't want notifications for
if (!IS_ICON_SYNC_THREAD()) {
// Start the timer to commit this change - or further delay the timer if it was already started
scheduleOrDeferSyncTimer();
for (auto& pageURL : pageURLs) {
AutodrainedPool pool;
LOG(IconDatabase, "Dispatching notification that retaining pageURL %s has a new icon", urlForLogging(pageURL).ascii().data());
m_client->didChangeIconForPageURL(pageURL);
}
}
}
void IconDatabase::setIconURLForPageURL(const String& iconURLOriginal, const String& pageURLOriginal)
{
ASSERT_NOT_SYNC_THREAD();
// Cannot do anything with iconURLOriginal or pageURLOriginal that would end up storing them without deep copying first
ASSERT(!iconURLOriginal.isEmpty());
if (!isOpen() || !documentCanHaveIcon(pageURLOriginal))
return;
String iconURL, pageURL;
{
LockHolder locker(m_urlAndIconLock);
PageURLRecord* pageRecord = m_pageURLToRecordMap.get(pageURLOriginal);
// If the urls already map to each other, bail.
// This happens surprisingly often, and seems to cream iBench performance
if (pageRecord && pageRecord->iconRecord() && pageRecord->iconRecord()->iconURL() == iconURLOriginal)
return;
pageURL = pageURLOriginal.isolatedCopy();
iconURL = iconURLOriginal.isolatedCopy();
if (!pageRecord) {
pageRecord = new PageURLRecord(pageURL);
m_pageURLToRecordMap.set(pageURL, pageRecord);
}
RefPtr<IconRecord> iconRecord = pageRecord->iconRecord();
// Otherwise, set the new icon record for this page
pageRecord->setIconRecord(getOrCreateIconRecord(iconURL));
// If the current icon has only a single ref left, it is about to get wiped out.
// Remove it from the in-memory records and don't bother reading it in from disk anymore
if (iconRecord && iconRecord->hasOneRef()) {
ASSERT(iconRecord->retainingPageURLs().size() == 0);
LOG(IconDatabase, "Icon for icon url %s is about to be destroyed - removing mapping for it", urlForLogging(iconRecord->iconURL()).ascii().data());
m_iconURLToRecordMap.remove(iconRecord->iconURL());
LockHolder locker(m_pendingReadingLock);
m_iconsPendingReading.remove(iconRecord.get());
}
// And mark this mapping to be added to the database
if (!m_privateBrowsingEnabled) {
LockHolder locker(m_pendingSyncLock);
m_pageURLsPendingSync.set(pageURL, pageRecord->snapshot());
// If the icon is on its last ref, mark it for deletion
if (iconRecord && iconRecord->hasOneRef())
m_iconsPendingSync.set(iconRecord->iconURL(), iconRecord->snapshot(true));
}
}
// Since this mapping is new, send the notification out - but not if we're on the sync thread because that implies this mapping
// comes from the initial import which we don't want notifications for
if (!IS_ICON_SYNC_THREAD()) {
// Start the timer to commit this change - or further delay the timer if it was already started
scheduleOrDeferSyncTimer();
LOG(IconDatabase, "Dispatching notification that we changed an icon mapping for url %s", urlForLogging(pageURL).ascii().data());
AutodrainedPool pool;
m_client->didChangeIconForPageURL(pageURL);
}
}
IconLoadDecision IconDatabase::synchronousLoadDecisionForIconURL(const String& iconURL, DocumentLoader* notificationDocumentLoader)
{
ASSERT_NOT_SYNC_THREAD();
if (!isOpen() || iconURL.isEmpty())
return IconLoadNo;
// If we have a IconRecord, it should also have its timeStamp marked because there is only two times when we create the IconRecord:
// 1 - When we read the icon urls from disk, getting the timeStamp at the same time
// 2 - When we get a new icon from the loader, in which case the timestamp is set at that time
{
LockHolder locker(m_urlAndIconLock);
if (IconRecord* icon = m_iconURLToRecordMap.get(iconURL)) {
LOG(IconDatabase, "Found expiration time on a present icon based on existing IconRecord");
return static_cast<int>(currentTime()) - static_cast<int>(icon->getTimestamp()) > iconExpirationTime ? IconLoadYes : IconLoadNo;
}
}
// If we don't have a record for it, but we *have* imported all iconURLs from disk, then we should load it now
LockHolder readingLocker(m_pendingReadingLock);
if (m_iconURLImportComplete)
return IconLoadYes;
// Otherwise - since we refuse to perform I/O on the main thread to find out for sure - we return the answer that says
// "You might be asked to load this later, so flag that"
LOG(IconDatabase, "Don't know if we should load %s or not - adding %p to the set of document loaders waiting on a decision", iconURL.ascii().data(), notificationDocumentLoader);
if (notificationDocumentLoader)
m_loadersPendingDecision.add(notificationDocumentLoader);
return IconLoadUnknown;
}
bool IconDatabase::synchronousIconDataKnownForIconURL(const String& iconURL)
{
ASSERT_NOT_SYNC_THREAD();
LockHolder locker(m_urlAndIconLock);
if (IconRecord* icon = m_iconURLToRecordMap.get(iconURL))
return icon->imageDataStatus() != ImageDataStatusUnknown;
return false;
}
void IconDatabase::setEnabled(bool enabled)
{
ASSERT_NOT_SYNC_THREAD();
if (!enabled && isOpen())
close();
m_isEnabled = enabled;
}
bool IconDatabase::isEnabled() const
{
ASSERT_NOT_SYNC_THREAD();
return m_isEnabled;
}
void IconDatabase::setPrivateBrowsingEnabled(bool flag)
{
m_privateBrowsingEnabled = flag;
}
bool IconDatabase::isPrivateBrowsingEnabled() const
{
return m_privateBrowsingEnabled;
}
void IconDatabase::delayDatabaseCleanup()
{
++databaseCleanupCounter;
if (databaseCleanupCounter == 1)
LOG(IconDatabase, "Database cleanup is now DISABLED");
}
void IconDatabase::allowDatabaseCleanup()
{
if (--databaseCleanupCounter < 0)
databaseCleanupCounter = 0;
if (databaseCleanupCounter == 0)
LOG(IconDatabase, "Database cleanup is now ENABLED");
}
void IconDatabase::checkIntegrityBeforeOpening()
{
checkIntegrityOnOpen = true;
}
size_t IconDatabase::pageURLMappingCount()
{
LockHolder locker(m_urlAndIconLock);
return m_pageURLToRecordMap.size();
}
size_t IconDatabase::retainedPageURLCount()
{
LockHolder locker(m_urlAndIconLock);
performPendingRetainAndReleaseOperations();
return m_retainedPageURLs.size();
}
size_t IconDatabase::iconRecordCount()
{
LockHolder locker(m_urlAndIconLock);
return m_iconURLToRecordMap.size();
}
size_t IconDatabase::iconRecordCountWithData()
{
LockHolder locker(m_urlAndIconLock);
size_t result = 0;
for (auto& iconRecord : m_iconURLToRecordMap.values())
result += (iconRecord->imageDataStatus() == ImageDataStatusPresent);
return result;
}
IconDatabase::IconDatabase()
: m_syncTimer(*this, &IconDatabase::syncTimerFired)
, m_syncThreadRunning(false)
, m_scheduleOrDeferSyncTimerRequested(false)
, m_isEnabled(false)
, m_privateBrowsingEnabled(false)
, m_threadTerminationRequested(false)
, m_removeIconsRequested(false)
, m_iconURLImportComplete(false)
, m_syncThreadHasWorkToDo(false)
, m_retainOrReleaseIconRequested(false)
, m_initialPruningComplete(false)
, m_mainThreadCallbackCount(0)
, m_client(defaultClient())
{
LOG(IconDatabase, "Creating IconDatabase %p", this);
ASSERT(isMainThread());
}
IconDatabase::~IconDatabase()
{
ASSERT(!isOpen());
}
void IconDatabase::notifyPendingLoadDecisions()
{
ASSERT_NOT_SYNC_THREAD();
// This method should only be called upon completion of the initial url import from the database
ASSERT(m_iconURLImportComplete);
LOG(IconDatabase, "Notifying all DocumentLoaders that were waiting on a load decision for their icons");
for (auto& loader : m_loadersPendingDecision) {
if (loader->refCount() > 1)
loader->iconLoadDecisionAvailable();
}
m_loadersPendingDecision.clear();
}
void IconDatabase::wakeSyncThread()
{
LockHolder locker(m_syncLock);
if (!m_disableSuddenTerminationWhileSyncThreadHasWorkToDo)
m_disableSuddenTerminationWhileSyncThreadHasWorkToDo = std::make_unique<SuddenTerminationDisabler>();
m_syncThreadHasWorkToDo = true;
m_syncCondition.notifyOne();
}
void IconDatabase::scheduleOrDeferSyncTimer()
{
ASSERT_NOT_SYNC_THREAD();
if (m_scheduleOrDeferSyncTimerRequested)
return;
if (!m_disableSuddenTerminationWhileSyncTimerScheduled)
m_disableSuddenTerminationWhileSyncTimerScheduled = std::make_unique<SuddenTerminationDisabler>();
m_scheduleOrDeferSyncTimerRequested = true;
callOnMainThread([this] {
m_syncTimer.startOneShot(updateTimerDelay);
m_scheduleOrDeferSyncTimerRequested = false;
});
}
void IconDatabase::syncTimerFired()
{
ASSERT_NOT_SYNC_THREAD();
wakeSyncThread();
m_disableSuddenTerminationWhileSyncTimerScheduled.reset();
}
// ******************
// *** Any Thread ***
// ******************
bool IconDatabase::isOpen() const
{
return isOpenBesidesMainThreadCallbacks() || m_mainThreadCallbackCount;
}
bool IconDatabase::isOpenBesidesMainThreadCallbacks() const
{
LockHolder locker(m_syncLock);
return m_syncThreadRunning || m_syncDB.isOpen();
}
String IconDatabase::databasePath() const
{
LockHolder locker(m_syncLock);
return m_completeDatabasePath.isolatedCopy();
}
String IconDatabase::defaultDatabaseFilename()
{
static NeverDestroyed<String> defaultDatabaseFilename(ASCIILiteral("WebpageIcons.db"));
return defaultDatabaseFilename.get().isolatedCopy();
}
// Unlike getOrCreatePageURLRecord(), getOrCreateIconRecord() does not mark the icon as "interested in import"
PassRefPtr<IconRecord> IconDatabase::getOrCreateIconRecord(const String& iconURL)
{
// Clients of getOrCreateIconRecord() are required to acquire the m_urlAndIconLock before calling this method
ASSERT(!m_urlAndIconLock.tryLock());
if (IconRecord* icon = m_iconURLToRecordMap.get(iconURL))
return icon;
RefPtr<IconRecord> newIcon = IconRecord::create(iconURL);
m_iconURLToRecordMap.set(iconURL, newIcon.get());
return newIcon.release();
}
// This method retrieves the existing PageURLRecord, or creates a new one and marks it as "interested in the import" for later notification
PageURLRecord* IconDatabase::getOrCreatePageURLRecord(const String& pageURL)
{
// Clients of getOrCreatePageURLRecord() are required to acquire the m_urlAndIconLock before calling this method
ASSERT(!m_urlAndIconLock.tryLock());
if (!documentCanHaveIcon(pageURL))
return nullptr;
PageURLRecord* pageRecord = m_pageURLToRecordMap.get(pageURL);
LockHolder locker(m_pendingReadingLock);
if (!m_iconURLImportComplete) {
// If the initial import of all URLs hasn't completed and we have no page record, we assume we *might* know about this later and create a record for it
if (!pageRecord) {
LOG(IconDatabase, "Creating new PageURLRecord for pageURL %s", urlForLogging(pageURL).ascii().data());
pageRecord = new PageURLRecord(pageURL);
m_pageURLToRecordMap.set(pageURL, pageRecord);
}
// If the pageRecord for this page does not have an iconRecord attached to it, then it is a new pageRecord still awaiting the initial import
// Mark the URL as "interested in the result of the import" then bail
if (!pageRecord->iconRecord()) {
m_pageURLsPendingImport.add(pageURL);
return nullptr;
}
}
// We've done the initial import of all URLs known in the database. If this record doesn't exist now, it never will
return pageRecord;
}
// ************************
// *** Sync Thread Only ***
// ************************
bool IconDatabase::shouldStopThreadActivity() const
{
ASSERT_ICON_SYNC_THREAD();
return m_threadTerminationRequested || m_removeIconsRequested;
}
void IconDatabase::iconDatabaseSyncThreadStart(void* vIconDatabase)
{
IconDatabase* iconDB = static_cast<IconDatabase*>(vIconDatabase);
iconDB->iconDatabaseSyncThread();
}
void IconDatabase::iconDatabaseSyncThread()
{
// The call to create this thread might not complete before the thread actually starts, so we might fail this ASSERT_ICON_SYNC_THREAD() because the pointer
// to our thread structure hasn't been filled in yet.
// To fix this, the main thread acquires this lock before creating us, then releases the lock after creation is complete. A quick lock/unlock cycle here will
// prevent us from running before that call completes
m_syncLock.lock();
m_syncLock.unlock();
ASSERT_ICON_SYNC_THREAD();
LOG(IconDatabase, "(THREAD) IconDatabase sync thread started");
#if !LOG_DISABLED
double startTime = monotonicallyIncreasingTime();
#endif
// Need to create the database path if it doesn't already exist
makeAllDirectories(m_databaseDirectory);
// Existence of a journal file is evidence of a previous crash/force quit and automatically qualifies
// us to do an integrity check
String journalFilename = m_completeDatabasePath + "-journal";
if (!checkIntegrityOnOpen) {
AutodrainedPool pool;
checkIntegrityOnOpen = fileExists(journalFilename);
}
{
LockHolder locker(m_syncLock);
if (!m_syncDB.open(m_completeDatabasePath)) {
LOG_ERROR("Unable to open icon database at path %s - %s", m_completeDatabasePath.ascii().data(), m_syncDB.lastErrorMsg());
return;
}
}
if (shouldStopThreadActivity()) {
syncThreadMainLoop();
return;
}
#if !LOG_DISABLED
double timeStamp = monotonicallyIncreasingTime();
LOG(IconDatabase, "(THREAD) Open took %.4f seconds", timeStamp - startTime);
#endif
performOpenInitialization();
if (shouldStopThreadActivity()) {