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)
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 Image* WebIconDatabase::imageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
189 if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
192 // The WebCore IconDatabase ignores the passed in size parameter.
193 // If that changes we'll need to rethink how this API is exposed.
194 return m_iconDatabaseImpl->synchronousIconForPageURL(pageURL, iconSize);
197 WebCore::NativeImagePtr WebIconDatabase::nativeImageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
199 if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
202 return m_iconDatabaseImpl->synchronousNativeIconForPageURL(pageURL, iconSize);
205 bool WebIconDatabase::isOpen()
207 return m_iconDatabaseImpl && m_iconDatabaseImpl->isOpen();
210 void WebIconDatabase::removeAllIcons()
212 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
213 if (!m_iconDatabaseImpl)
216 m_iconDatabaseImpl->removeAllIcons();
219 void WebIconDatabase::checkIntegrityBeforeOpening()
221 IconDatabase::checkIntegrityBeforeOpening();
224 void WebIconDatabase::close()
226 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
227 if (!m_iconDatabaseImpl)
230 m_iconDatabaseImpl->close();
233 void WebIconDatabase::initializeIconDatabaseClient(const WKIconDatabaseClient* client)
235 m_iconDatabaseClient.initialize(client);
238 // WebCore::IconDatabaseClient
239 bool WebIconDatabase::performImport()
241 // WebKit2 icon database doesn't currently support importing any old icon database formats.
245 void WebIconDatabase::didImportIconURLForPageURL(const String& pageURL)
247 didChangeIconForPageURL(pageURL);
250 void WebIconDatabase::didImportIconDataForPageURL(const String& pageURL)
252 didChangeIconForPageURL(pageURL);
255 void WebIconDatabase::didChangeIconForPageURL(const String& pageURL)
257 m_iconDatabaseClient.didChangeIconForPageURL(this, WebURL::create(pageURL).get());
260 void WebIconDatabase::didRemoveAllIcons()
262 m_iconDatabaseClient.didRemoveAllIcons(this);
265 void WebIconDatabase::didFinishURLImport()
267 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
268 if (!m_webContext || !m_iconDatabaseImpl)
275 ASSERT(!m_urlImportCompleted);
277 LOG(IconDatabase, "WK2 UIProcess URL import complete, notifying all %i pending page URL load decisions", m_pendingLoadDecisionURLMap.size());
279 HashMap<uint64_t, String>::iterator i = m_pendingLoadDecisionURLMap.begin();
280 HashMap<uint64_t, String>::iterator end = m_pendingLoadDecisionURLMap.end();
282 for (; i != end; ++i) {
283 LOG(IconDatabase, "WK2 UIProcess performing delayed callback on callback ID %i for page url %s", (int)i->first, i->second.ascii().data());
284 IconLoadDecision decision = m_iconDatabaseImpl->synchronousLoadDecisionForIconURL(i->second, 0);
286 // Decisions should never be unknown after the inital import is complete
287 ASSERT(decision != IconLoadUnknown);
289 // FIXME (Multi-WebProcess): We need to know which connection to send this message to.
290 m_webContext->sendToAllProcesses(Messages::WebIconDatabaseProxy::ReceivedIconLoadDecision(static_cast<int>(decision), i->first));
293 m_pendingLoadDecisionURLMap.clear();
295 m_urlImportCompleted = true;
298 void WebIconDatabase::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder)
300 didReceiveWebIconDatabaseMessage(connection, messageID, decoder);
303 void WebIconDatabase::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder, OwnPtr<CoreIPC::ArgumentEncoder>& reply)
305 didReceiveSyncWebIconDatabaseMessage(connection, messageID, decoder, reply);
308 } // namespace WebKit