1/*
2 * Copyright (C) 2014 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "VisitedLinkTableController.h"
28
29#include "VisitedLinkStoreMessages.h"
30#include "VisitedLinkTableControllerMessages.h"
31#include "WebPage.h"
32#include "WebProcess.h"
33#include <WebCore/PageCache.h>
34#include <wtf/NeverDestroyed.h>
35
36namespace WebKit {
37using namespace WebCore;
38
39static HashMap<uint64_t, VisitedLinkTableController*>& visitedLinkTableControllers()
40{
41 static NeverDestroyed<HashMap<uint64_t, VisitedLinkTableController*>> visitedLinkTableControllers;
42
43 return visitedLinkTableControllers;
44}
45
46Ref<VisitedLinkTableController> VisitedLinkTableController::getOrCreate(uint64_t identifier)
47{
48 auto& visitedLinkTableControllerPtr = visitedLinkTableControllers().add(identifier, nullptr).iterator->value;
49 if (visitedLinkTableControllerPtr)
50 return *visitedLinkTableControllerPtr;
51
52 auto visitedLinkTableController = adoptRef(*new VisitedLinkTableController(identifier));
53 visitedLinkTableControllerPtr = visitedLinkTableController.ptr();
54
55 return visitedLinkTableController;
56}
57
58VisitedLinkTableController::VisitedLinkTableController(uint64_t identifier)
59 : m_identifier(identifier)
60{
61 WebProcess::singleton().addMessageReceiver(Messages::VisitedLinkTableController::messageReceiverName(), m_identifier, *this);
62}
63
64VisitedLinkTableController::~VisitedLinkTableController()
65{
66 ASSERT(visitedLinkTableControllers().contains(m_identifier));
67
68 WebProcess::singleton().removeMessageReceiver(Messages::VisitedLinkTableController::messageReceiverName(), m_identifier);
69
70 visitedLinkTableControllers().remove(m_identifier);
71}
72
73bool VisitedLinkTableController::isLinkVisited(Page&, SharedStringHash linkHash, const URL&, const AtomString&)
74{
75 return m_visitedLinkTable.contains(linkHash);
76}
77
78void VisitedLinkTableController::addVisitedLink(Page& page, SharedStringHash linkHash)
79{
80 if (m_visitedLinkTable.contains(linkHash))
81 return;
82
83 WebPage* webPage = WebPage::fromCorePage(&page);
84 if (!webPage)
85 return;
86
87 WebProcess::singleton().parentProcessConnection()->send(Messages::VisitedLinkStore::AddVisitedLinkHashFromPage(webPage->pageID(), linkHash), m_identifier);
88}
89
90void VisitedLinkTableController::setVisitedLinkTable(const SharedMemory::Handle& handle)
91{
92 auto sharedMemory = SharedMemory::map(handle, SharedMemory::Protection::ReadOnly);
93 if (!sharedMemory)
94 return;
95
96 m_visitedLinkTable.setSharedMemory(sharedMemory.releaseNonNull());
97
98 invalidateStylesForAllLinks();
99}
100
101void VisitedLinkTableController::visitedLinkStateChanged(const Vector<WebCore::SharedStringHash>& linkHashes)
102{
103 for (auto linkHash : linkHashes)
104 invalidateStylesForLink(linkHash);
105}
106
107void VisitedLinkTableController::allVisitedLinkStateChanged()
108{
109 invalidateStylesForAllLinks();
110}
111
112void VisitedLinkTableController::removeAllVisitedLinks()
113{
114 m_visitedLinkTable.setSharedMemory(nullptr);
115
116 invalidateStylesForAllLinks();
117}
118
119} // namespace WebKit
120