1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "config.h"
  27 
  28 #include "VisitedLinkStoreJava.h"
  29 
  30 #include <WebCore/PageCache.h>
  31 #include <wtf/NeverDestroyed.h>
  32 
  33 
  34 using namespace WebCore;
  35 
  36 static bool s_shouldTrackVisitedLinks;
  37 
  38 static HashSet<VisitedLinkStoreJava*>& visitedLinkStores()
  39 {
  40     static NeverDestroyed<HashSet<VisitedLinkStoreJava*>> visitedLinkStores;
  41 
  42     return visitedLinkStores;
  43 }
  44 
  45 Ref<VisitedLinkStoreJava> VisitedLinkStoreJava::create()
  46 {
  47     return adoptRef(*new VisitedLinkStoreJava);
  48 }
  49 
  50 VisitedLinkStoreJava::VisitedLinkStoreJava()
  51     : m_visitedLinksPopulated(false)
  52 {
  53     visitedLinkStores().add(this);
  54 }
  55 
  56 VisitedLinkStoreJava::~VisitedLinkStoreJava()
  57 {
  58     visitedLinkStores().remove(this);
  59 }
  60 
  61 void VisitedLinkStoreJava::setShouldTrackVisitedLinks(bool shouldTrackVisitedLinks)
  62 {
  63     if (s_shouldTrackVisitedLinks == shouldTrackVisitedLinks)
  64         return;
  65 
  66     s_shouldTrackVisitedLinks = shouldTrackVisitedLinks;
  67     if (!s_shouldTrackVisitedLinks)
  68         removeAllVisitedLinks();
  69 }
  70 
  71 void VisitedLinkStoreJava::removeAllVisitedLinks()
  72 {
  73     for (auto& visitedLinkStore : visitedLinkStores())
  74         visitedLinkStore->removeVisitedLinkHashes();
  75 }
  76 
  77 void VisitedLinkStoreJava::addVisitedLink(const String& urlString)
  78 {
  79     addVisitedLinkHash(computeSharedStringHash(urlString));
  80 }
  81 
  82 bool VisitedLinkStoreJava::isLinkVisited(Page& page, SharedStringHash linkHash, const URL&, const AtomicString&)
  83 {
  84     populateVisitedLinksIfNeeded(page);
  85 
  86     return m_visitedLinkHashes.contains(linkHash);
  87 }
  88 
  89 void VisitedLinkStoreJava::addVisitedLink(Page&, SharedStringHash linkHash)
  90 {
  91     if (!s_shouldTrackVisitedLinks)
  92         return;
  93 
  94     addVisitedLinkHash(linkHash);
  95 }
  96 
  97 void VisitedLinkStoreJava::populateVisitedLinksIfNeeded(Page&)
  98 {
  99     if (m_visitedLinksPopulated)
 100         return;
 101 
 102     m_visitedLinksPopulated = true;
 103 }
 104 
 105 void VisitedLinkStoreJava::addVisitedLinkHash(SharedStringHash linkHash)
 106 {
 107     ASSERT(s_shouldTrackVisitedLinks);
 108     m_visitedLinkHashes.add(linkHash);
 109 
 110     invalidateStylesForLink(linkHash);
 111 }
 112 
 113 void VisitedLinkStoreJava::removeVisitedLinkHashes()
 114 {
 115     m_visitedLinksPopulated = false;
 116     if (m_visitedLinkHashes.isEmpty())
 117         return;
 118     m_visitedLinkHashes.clear();
 119 
 120     invalidateStylesForAllLinks();
 121 }
 122 
 123