2 * Copyright (C) 2011 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 "WebIconDatabase.h"
29 #include "DataReference.h"
31 #include "WebContext.h"
32 #include "WebIconDatabaseProxyMessages.h"
33 #include <WebCore/FileSystem.h>
34 #include <WebCore/IconDatabase.h>
35 #include <WebCore/IconDatabaseBase.h>
36 #include <wtf/text/CString.h>
37 #include <wtf/text/WTFString.h>
39 using namespace WebCore;
43 PassRefPtr<WebIconDatabase> WebIconDatabase::create(WebContext* context)
45 return adoptRef(new WebIconDatabase(context));
48 WebIconDatabase::~WebIconDatabase()
52 WebIconDatabase::WebIconDatabase(WebContext* context)
53 : m_webContext(context)
54 , m_urlImportCompleted(false)
55 , m_databaseCleanupDisabled(false)
59 void WebIconDatabase::invalidate()
63 void WebIconDatabase::setDatabasePath(const String& path)
66 LOG_ERROR("Icon database already has a path and is already open. We don't currently support changing its path and reopening.");
70 m_iconDatabaseImpl = IconDatabase::create();
71 m_iconDatabaseImpl->setClient(this);
72 IconDatabase::delayDatabaseCleanup();
73 m_databaseCleanupDisabled = true;
74 m_iconDatabaseImpl->setEnabled(true);
75 if (!m_iconDatabaseImpl->open(directoryName(path), pathGetFileName(path))) {
76 LOG_ERROR("Unable to open WebKit2 icon database on disk");
77 m_iconDatabaseImpl.clear();
78 setGlobalIconDatabase(0);
79 IconDatabase::allowDatabaseCleanup();
80 m_databaseCleanupDisabled = false;
82 setGlobalIconDatabase(m_iconDatabaseImpl.get());
85 void WebIconDatabase::enableDatabaseCleanup()
87 if (!m_iconDatabaseImpl) {
88 LOG_ERROR("Cannot enabled Icon Database cleanup - it hasn't been opened yet.");
92 if (!m_databaseCleanupDisabled) {
93 LOG_ERROR("Attempt to enable database cleanup, but it's already enabled.");
98 IconDatabase::allowDatabaseCleanup();
99 m_databaseCleanupDisabled = false;
102 void WebIconDatabase::retainIconForPageURL(const String& pageURL)
104 if (m_iconDatabaseImpl)
105 m_iconDatabaseImpl->retainIconForPageURL(pageURL);
108 void WebIconDatabase::releaseIconForPageURL(const String& pageURL)
110 if (m_iconDatabaseImpl)
111 m_iconDatabaseImpl->releaseIconForPageURL(pageURL);
114 void WebIconDatabase::setIconURLForPageURL(const String& iconURL, const String& pageURL)
116 LOG(IconDatabase, "WK2 UIProcess setting icon URL %s for page URL %s", iconURL.ascii().data(), pageURL.ascii().data());
117 if (m_iconDatabaseImpl)
118 m_iconDatabaseImpl->setIconURLForPageURL(iconURL, pageURL);
121 void WebIconDatabase::setIconDataForIconURL(const CoreIPC::DataReference& iconData, const String& iconURL)
123 LOG(IconDatabase, "WK2 UIProcess setting icon data (%i bytes) for page URL %s", (int)iconData.size(), iconURL.ascii().data());
124 if (!m_iconDatabaseImpl)
127 RefPtr<SharedBuffer> buffer = SharedBuffer::create(iconData.data(), iconData.size());
128 m_iconDatabaseImpl->setIconDataForIconURL(buffer.release(), iconURL);
131 void WebIconDatabase::synchronousIconDataForPageURL(const String& pageURL, CoreIPC::DataReference& iconData)
133 iconData = CoreIPC::DataReference();
135 #if ENABLE(TIZEN_ICON_DATABASE) && !ENABLE(TIZEN_WEBKIT2_EFL_WTR)
136 m_iconDatabaseImpl->synchronousIconForPageURL(pageURL, IntSize(0, 0));
140 void WebIconDatabase::synchronousIconURLForPageURL(const String& pageURL, String& iconURL)
142 if (!m_iconDatabaseImpl) {
147 iconURL = m_iconDatabaseImpl->synchronousIconURLForPageURL(pageURL);
150 void WebIconDatabase::synchronousIconDataKnownForIconURL(const String&, bool& iconDataKnown) const
152 iconDataKnown = false;
155 void WebIconDatabase::synchronousLoadDecisionForIconURL(const String&, int& loadDecision) const
157 loadDecision = static_cast<int>(IconLoadNo);
160 void WebIconDatabase::getLoadDecisionForIconURL(const String& iconURL, uint64_t callbackID)
162 LOG(IconDatabase, "WK2 UIProcess getting load decision for icon URL %s with callback ID %lli", iconURL.ascii().data(), static_cast<long long>(callbackID));
167 if (!m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || iconURL.isEmpty()) {
168 // FIXME (Multi-WebProcess): We need to know which connection to send this message to.
169 m_webContext->sendToAllProcesses(Messages::WebIconDatabaseProxy::ReceivedIconLoadDecision(static_cast<int>(IconLoadNo), callbackID));
173 // If the decision hasn't been read from disk yet, set this url and callback ID aside to be notifed later
174 IconLoadDecision decision = m_iconDatabaseImpl->synchronousLoadDecisionForIconURL(iconURL, 0);
175 if (decision == IconLoadUnknown) {
176 // We should never get an unknown load decision after the URL import has completed.
177 ASSERT(!m_urlImportCompleted);
179 m_pendingLoadDecisionURLMap.set(callbackID, iconURL);
183 // FIXME (Multi-WebProcess): We need to know which connection to send this message to.
184 m_webContext->sendToAllProcesses(Messages::WebIconDatabaseProxy::ReceivedIconLoadDecision((int)decision, callbackID));
187 void WebIconDatabase::didReceiveIconForPageURL(const String& pageURL)
189 notifyIconDataReadyForPageURL(pageURL);
192 Image* WebIconDatabase::imageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
194 if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
197 // The WebCore IconDatabase ignores the passed in size parameter.
198 // If that changes we'll need to rethink how this API is exposed.
199 return m_iconDatabaseImpl->synchronousIconForPageURL(pageURL, iconSize);
202 WebCore::NativeImagePtr WebIconDatabase::nativeImageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
204 if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
207 return m_iconDatabaseImpl->synchronousNativeIconForPageURL(pageURL, iconSize);
210 bool WebIconDatabase::isOpen()
212 return m_iconDatabaseImpl && m_iconDatabaseImpl->isOpen();
215 bool WebIconDatabase::isUrlImportCompleted()
217 return m_urlImportCompleted;
220 void WebIconDatabase::removeAllIcons()
222 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
223 if (!m_iconDatabaseImpl)
226 m_iconDatabaseImpl->removeAllIcons();
229 void WebIconDatabase::checkIntegrityBeforeOpening()
231 IconDatabase::checkIntegrityBeforeOpening();
234 void WebIconDatabase::close()
236 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
237 if (!m_iconDatabaseImpl)
240 m_iconDatabaseImpl->close();
243 void WebIconDatabase::initializeIconDatabaseClient(const WKIconDatabaseClient* client)
245 m_iconDatabaseClient.initialize(client);
248 // WebCore::IconDatabaseClient
249 bool WebIconDatabase::performImport()
251 // WebKit2 icon database doesn't currently support importing any old icon database formats.
255 void WebIconDatabase::didImportIconURLForPageURL(const String& pageURL)
257 didChangeIconForPageURL(pageURL);
260 void WebIconDatabase::didImportIconDataForPageURL(const String& pageURL)
262 notifyIconDataReadyForPageURL(pageURL);
265 void WebIconDatabase::didChangeIconForPageURL(const String& pageURL)
267 m_iconDatabaseClient.didChangeIconForPageURL(this, WebURL::create(pageURL).get());
270 void WebIconDatabase::didRemoveAllIcons()
272 m_iconDatabaseClient.didRemoveAllIcons(this);
275 void WebIconDatabase::didFinishURLImport()
277 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
278 if (!m_webContext || !m_iconDatabaseImpl)
285 ASSERT(!m_urlImportCompleted);
287 LOG(IconDatabase, "WK2 UIProcess URL import complete, notifying all %i pending page URL load decisions", m_pendingLoadDecisionURLMap.size());
289 HashMap<uint64_t, String>::iterator i = m_pendingLoadDecisionURLMap.begin();
290 HashMap<uint64_t, String>::iterator end = m_pendingLoadDecisionURLMap.end();
292 for (; i != end; ++i) {
293 LOG(IconDatabase, "WK2 UIProcess performing delayed callback on callback ID %i for page url %s", (int)i->first, i->second.ascii().data());
294 IconLoadDecision decision = m_iconDatabaseImpl->synchronousLoadDecisionForIconURL(i->second, 0);
296 // Decisions should never be unknown after the inital import is complete
297 ASSERT(decision != IconLoadUnknown);
299 // FIXME (Multi-WebProcess): We need to know which connection to send this message to.
300 m_webContext->sendToAllProcesses(Messages::WebIconDatabaseProxy::ReceivedIconLoadDecision(static_cast<int>(decision), i->first));
303 m_pendingLoadDecisionURLMap.clear();
305 m_urlImportCompleted = true;
308 void WebIconDatabase::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder)
310 didReceiveWebIconDatabaseMessage(connection, messageID, decoder);
313 void WebIconDatabase::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder, OwnPtr<CoreIPC::ArgumentEncoder>& reply)
315 didReceiveSyncWebIconDatabaseMessage(connection, messageID, decoder, reply);
318 void WebIconDatabase::notifyIconDataReadyForPageURL(const String& pageURL)
320 m_iconDatabaseClient.iconDataReadyForPageURL(this, WebURL::create(pageURL).get());
321 didChangeIconForPageURL(pageURL);
324 } // namespace WebKit