2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "VisitedLinkProvider.h"
29 #include "SharedMemory.h"
30 #include "VisitedLinkTable.h"
31 #include "WebContext.h"
32 #include "WebProcessMessages.h"
34 using namespace WebCore;
38 static const int VisitedLinkTableMaxLoad = 2;
40 VisitedLinkProvider::VisitedLinkProvider(WebContext* context)
42 , m_visitedLinksPopulated(false)
43 , m_webProcessHasVisitedLinkState(false)
46 , m_pendingVisitedLinksTimer(RunLoop::main(), this, &VisitedLinkProvider::pendingVisitedLinksTimerFired)
50 void VisitedLinkProvider::processDidFinishLaunching()
52 m_webProcessHasVisitedLinkState = false;
55 m_pendingVisitedLinksTimer.startOneShot(0);
57 if (m_visitedLinksPopulated)
60 m_context->populateVisitedLinks();
62 m_visitedLinksPopulated = true;
65 void VisitedLinkProvider::addVisitedLink(LinkHash linkHash)
67 m_pendingVisitedLinks.add(linkHash);
69 if (!m_pendingVisitedLinksTimer.isActive())
70 m_pendingVisitedLinksTimer.startOneShot(0);
73 void VisitedLinkProvider::processDidClose()
75 m_pendingVisitedLinksTimer.stop();
78 static unsigned nextPowerOf2(unsigned v)
80 // Taken from http://www.cs.utk.edu/~vose/c-stuff/bithacks.html
81 // Devised by Sean Anderson, Sepember 14, 2001
94 static unsigned tableSizeForKeyCount(unsigned keyCount)
96 // We want the table to be at least half empty.
97 unsigned tableSize = nextPowerOf2(keyCount * VisitedLinkTableMaxLoad);
99 // Ensure that the table size is at least the size of a page.
100 size_t minimumTableSize = SharedMemory::systemPageSize() / sizeof(LinkHash);
101 if (tableSize < minimumTableSize)
102 return minimumTableSize;
107 void VisitedLinkProvider::pendingVisitedLinksTimerFired()
109 Vector<WebCore::LinkHash> pendingVisitedLinks;
110 copyToVector(m_pendingVisitedLinks, pendingVisitedLinks);
111 m_pendingVisitedLinks.clear();
113 unsigned currentTableSize = m_tableSize;
114 unsigned newTableSize = tableSizeForKeyCount(m_keyCount + pendingVisitedLinks.size());
116 // Links that were added.
117 Vector<WebCore::LinkHash> addedVisitedLinks;
119 if (currentTableSize != newTableSize) {
120 // Create a new table.
121 RefPtr<SharedMemory> newTableMemory = SharedMemory::create(newTableSize * sizeof(LinkHash));
123 // We failed to create the shared memory.
127 memset(newTableMemory->data(), 0, newTableMemory->size());
129 RefPtr<SharedMemory> currentTableMemory = m_table.sharedMemory();
131 m_table.setSharedMemory(newTableMemory);
132 m_tableSize = newTableSize;
134 if (currentTableMemory) {
135 ASSERT(currentTableMemory->size() == currentTableSize * sizeof(LinkHash));
137 // Go through the current hash table and re-add all entries to the new hash table.
138 const LinkHash* currentLinkHashes = static_cast<const LinkHash*>(currentTableMemory->data());
139 for (unsigned i = 0; i < currentTableSize; ++i) {
140 LinkHash linkHash = currentLinkHashes[i];
145 // It should always be possible to add the link hash to a new table.
146 if (!m_table.addLinkHash(linkHash))
147 ASSERT_NOT_REACHED();
152 for (size_t i = 0; i < pendingVisitedLinks.size(); ++i) {
153 if (m_table.addLinkHash(pendingVisitedLinks[i]))
154 addedVisitedLinks.append(pendingVisitedLinks[i]);
157 m_keyCount += pendingVisitedLinks.size();
159 if (!m_webProcessHasVisitedLinkState || currentTableSize != newTableSize) {
160 // Send the new visited link table.
162 SharedMemory::Handle handle;
163 if (!m_table.sharedMemory()->createHandle(handle, SharedMemory::ReadOnly))
166 // FIXME (Multi-WebProcess): Encoding a handle will null it out so we need to create a new
167 // handle for every process. Maybe the ArgumentEncoder should handle this.
168 m_context->sendToAllProcesses(Messages::WebProcess::SetVisitedLinkTable(handle));
171 // We now need to let the web process know that we've added links.
172 if (m_webProcessHasVisitedLinkState && addedVisitedLinks.size() <= 20) {
173 m_context->sendToAllProcesses(Messages::WebProcess::VisitedLinkStateChanged(addedVisitedLinks));
177 // Just recalculate all the visited links.
178 m_context->sendToAllProcesses(Messages::WebProcess::AllVisitedLinkStateChanged());
179 m_webProcessHasVisitedLinkState = true;
182 } // namespace WebKit