Merge "Use EwkView's variables instead of drawingScaleFactor and drawingScrollPositio...
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / WebIconDatabase.cpp
1 /*
2  * Copyright (C) 2011 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 "WebIconDatabase.h"
28
29 #include "DataReference.h"
30 #include "Logging.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>
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 PassRefPtr<WebIconDatabase> WebIconDatabase::create(WebContext* context)
44 {
45     return adoptRef(new WebIconDatabase(context));
46 }
47
48 WebIconDatabase::~WebIconDatabase()
49 {
50 }
51
52 WebIconDatabase::WebIconDatabase(WebContext* context)
53     : m_webContext(context)
54     , m_urlImportCompleted(false)
55     , m_databaseCleanupDisabled(false)
56 {
57 }
58
59 void WebIconDatabase::invalidate()
60 {
61 }
62
63 void WebIconDatabase::setDatabasePath(const String& path)
64 {
65     if (isOpen()) {
66         LOG_ERROR("Icon database already has a path and is already open. We don't currently support changing its path and reopening.");
67         return;
68     }
69
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;
81     }
82     setGlobalIconDatabase(m_iconDatabaseImpl.get());
83 }
84
85 void WebIconDatabase::enableDatabaseCleanup()
86 {
87     if (!m_iconDatabaseImpl) {
88         LOG_ERROR("Cannot enabled Icon Database cleanup - it hasn't been opened yet.");
89         return;
90     }
91
92     if (!m_databaseCleanupDisabled) {
93         LOG_ERROR("Attempt to enable database cleanup, but it's already enabled.");
94         ASSERT_NOT_REACHED();
95         return;
96     }
97     
98     IconDatabase::allowDatabaseCleanup();
99     m_databaseCleanupDisabled = false;
100 }
101
102 void WebIconDatabase::retainIconForPageURL(const String& pageURL)
103 {
104     if (m_iconDatabaseImpl)
105         m_iconDatabaseImpl->retainIconForPageURL(pageURL);
106 }
107
108 void WebIconDatabase::releaseIconForPageURL(const String& pageURL)
109 {
110     if (m_iconDatabaseImpl)
111         m_iconDatabaseImpl->releaseIconForPageURL(pageURL);
112 }
113
114 void WebIconDatabase::setIconURLForPageURL(const String& iconURL, const String& pageURL)
115 {
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);
119 }
120
121 void WebIconDatabase::setIconDataForIconURL(const CoreIPC::DataReference& iconData, const String& iconURL)
122 {
123     LOG(IconDatabase, "WK2 UIProcess setting icon data (%i bytes) for page URL %s", (int)iconData.size(), iconURL.ascii().data());
124     if (!m_iconDatabaseImpl)
125         return;
126
127     RefPtr<SharedBuffer> buffer = SharedBuffer::create(iconData.data(), iconData.size());
128     m_iconDatabaseImpl->setIconDataForIconURL(buffer.release(), iconURL);
129 }
130
131 void WebIconDatabase::synchronousIconDataForPageURL(const String& pageURL, CoreIPC::DataReference& iconData)
132 {
133     iconData = CoreIPC::DataReference();
134
135 #if ENABLE(TIZEN_ICON_DATABASE)
136     m_iconDatabaseImpl->synchronousIconForPageURL(pageURL, IntSize(0, 0));
137 #endif
138 }
139
140 void WebIconDatabase::synchronousIconURLForPageURL(const String& pageURL, String& iconURL)
141 {
142     if (!m_iconDatabaseImpl) {
143         iconURL = String();
144         return;
145     }
146
147     iconURL = m_iconDatabaseImpl->synchronousIconURLForPageURL(pageURL);
148 }
149
150 void WebIconDatabase::synchronousIconDataKnownForIconURL(const String&, bool& iconDataKnown) const
151 {
152     iconDataKnown = false;
153 }
154
155 void WebIconDatabase::synchronousLoadDecisionForIconURL(const String&, int& loadDecision) const
156 {
157     loadDecision = static_cast<int>(IconLoadNo);
158 }
159
160 void WebIconDatabase::getLoadDecisionForIconURL(const String& iconURL, uint64_t callbackID)
161 {
162     LOG(IconDatabase, "WK2 UIProcess getting load decision for icon URL %s with callback ID %lli", iconURL.ascii().data(), static_cast<long long>(callbackID));
163
164     if (!m_webContext)
165         return;
166
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));
170         return;
171     }
172     
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);
178         
179         m_pendingLoadDecisionURLMap.set(callbackID, iconURL);
180         return;    
181     }
182
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));
185 }
186
187 Image* WebIconDatabase::imageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
188 {
189     if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
190         return 0;    
191
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);
195 }
196
197 WebCore::NativeImagePtr WebIconDatabase::nativeImageForPageURL(const String& pageURL, const WebCore::IntSize& iconSize)
198 {
199     if (!m_webContext || !m_iconDatabaseImpl || !m_iconDatabaseImpl->isOpen() || pageURL.isEmpty())
200         return 0;
201
202     return m_iconDatabaseImpl->synchronousNativeIconForPageURL(pageURL, iconSize);
203 }
204
205 bool WebIconDatabase::isOpen()
206 {
207     return m_iconDatabaseImpl && m_iconDatabaseImpl->isOpen();
208 }
209
210 void WebIconDatabase::removeAllIcons()
211 {
212 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
213     if (!m_iconDatabaseImpl)
214         return;
215 #endif
216     m_iconDatabaseImpl->removeAllIcons();   
217 }
218
219 void WebIconDatabase::checkIntegrityBeforeOpening()
220 {
221     IconDatabase::checkIntegrityBeforeOpening();
222 }
223
224 void WebIconDatabase::close()
225 {
226 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
227     if (!m_iconDatabaseImpl)
228         return;
229 #endif
230     m_iconDatabaseImpl->close();
231 }
232
233 void WebIconDatabase::initializeIconDatabaseClient(const WKIconDatabaseClient* client)
234 {
235     m_iconDatabaseClient.initialize(client);
236 }
237
238 // WebCore::IconDatabaseClient
239 bool WebIconDatabase::performImport()
240 {
241     // WebKit2 icon database doesn't currently support importing any old icon database formats.
242     return true;
243 }
244
245 void WebIconDatabase::didImportIconURLForPageURL(const String& pageURL)
246 {
247     didChangeIconForPageURL(pageURL);
248 }
249
250 void WebIconDatabase::didImportIconDataForPageURL(const String& pageURL)
251 {
252     didChangeIconForPageURL(pageURL);
253 }
254
255 void WebIconDatabase::didChangeIconForPageURL(const String& pageURL)
256 {
257     m_iconDatabaseClient.didChangeIconForPageURL(this, WebURL::create(pageURL).get());
258 }
259
260 void WebIconDatabase::didRemoveAllIcons()
261 {
262     m_iconDatabaseClient.didRemoveAllIcons(this);
263 }
264
265 void WebIconDatabase::didFinishURLImport()
266 {
267 #if ENABLE(TIZEN_WEBKIT2_PATCH_FOR_TC)
268     if (!m_webContext || !m_iconDatabaseImpl)
269         return;
270 #else
271     if (!m_webContext)
272         return;
273 #endif
274     
275     ASSERT(!m_urlImportCompleted);
276
277     LOG(IconDatabase, "WK2 UIProcess URL import complete, notifying all %i pending page URL load decisions", m_pendingLoadDecisionURLMap.size());
278
279     HashMap<uint64_t, String>::iterator i = m_pendingLoadDecisionURLMap.begin();
280     HashMap<uint64_t, String>::iterator end = m_pendingLoadDecisionURLMap.end();
281     
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);
285
286         // Decisions should never be unknown after the inital import is complete
287         ASSERT(decision != IconLoadUnknown);
288
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));
291     }
292     
293     m_pendingLoadDecisionURLMap.clear();
294     
295     m_urlImportCompleted = true;
296 }
297
298 void WebIconDatabase::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder)
299 {
300     didReceiveWebIconDatabaseMessage(connection, messageID, decoder);
301 }
302
303 void WebIconDatabase::didReceiveSyncMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder* decoder, OwnPtr<CoreIPC::ArgumentEncoder>& reply)
304 {
305     didReceiveSyncWebIconDatabaseMessage(connection, messageID, decoder, reply);
306 }
307
308 } // namespace WebKit