2 * Copyright (C) 2006, 2007, 2008, 2009 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WebKitDLL.h"
28 #include "WebIconDatabase.h"
30 #include "CFDictionaryPropertyBag.h"
31 #include "WebPreferences.h"
32 #include "WebNotificationCenter.h"
33 #include <WebCore/BitmapInfo.h>
34 #include <WebCore/BString.h>
35 #include <WebCore/COMPtr.h>
36 #include <WebCore/FileSystem.h>
37 #include <WebCore/HWndDC.h>
38 #include <WebCore/IconDatabase.h>
39 #include <WebCore/Image.h>
40 #include <WebCore/PlatformString.h>
41 #include <WebCore/SharedBuffer.h>
42 #include <wtf/MainThread.h>
45 using namespace WebCore;
48 // WebIconDatabase ----------------------------------------------------------------
50 WebIconDatabase* WebIconDatabase::m_sharedWebIconDatabase = 0;
52 WebIconDatabase::WebIconDatabase()
54 , m_deliveryRequested(false)
57 gClassNameCount.add("WebIconDatabase");
60 WebIconDatabase::~WebIconDatabase()
63 gClassNameCount.remove("WebIconDatabase");
66 void WebIconDatabase::init()
68 WebPreferences* standardPrefs = WebPreferences::sharedStandardPreferences();
70 if (FAILED(standardPrefs->iconDatabaseEnabled(&enabled))) {
72 LOG_ERROR("Unable to get icon database enabled preference");
74 iconDatabase().setEnabled(!!enabled);
78 startUpIconDatabase();
81 void WebIconDatabase::startUpIconDatabase()
83 WebPreferences* standardPrefs = WebPreferences::sharedStandardPreferences();
85 iconDatabase().setClient(this);
87 BSTR prefDatabasePath = 0;
88 if (FAILED(standardPrefs->iconDatabaseLocation(&prefDatabasePath)))
89 LOG_ERROR("Unable to get icon database location preference");
91 String databasePath(prefDatabasePath, SysStringLen(prefDatabasePath));
92 SysFreeString(prefDatabasePath);
94 if (databasePath.isEmpty()) {
95 databasePath = localUserSpecificStorageDirectory();
96 if (databasePath.isEmpty())
97 LOG_ERROR("Failed to construct default icon database path");
100 if (!iconDatabase().open(databasePath, WebCore::IconDatabase::defaultDatabaseFilename()))
101 LOG_ERROR("Failed to open icon database path");
104 void WebIconDatabase::shutDownIconDatabase()
108 WebIconDatabase* WebIconDatabase::createInstance()
110 WebIconDatabase* instance = new WebIconDatabase();
115 WebIconDatabase* WebIconDatabase::sharedWebIconDatabase()
117 if (m_sharedWebIconDatabase) {
118 m_sharedWebIconDatabase->AddRef();
119 return m_sharedWebIconDatabase;
121 m_sharedWebIconDatabase = createInstance();
122 m_sharedWebIconDatabase->init();
123 return m_sharedWebIconDatabase;
126 // IUnknown -------------------------------------------------------------------
128 HRESULT STDMETHODCALLTYPE WebIconDatabase::QueryInterface(REFIID riid, void** ppvObject)
131 if (IsEqualGUID(riid, IID_IUnknown))
132 *ppvObject = static_cast<IWebIconDatabase*>(this);
133 else if (IsEqualGUID(riid, IID_IWebIconDatabase))
134 *ppvObject = static_cast<IWebIconDatabase*>(this);
136 return E_NOINTERFACE;
142 ULONG STDMETHODCALLTYPE WebIconDatabase::AddRef(void)
147 ULONG STDMETHODCALLTYPE WebIconDatabase::Release(void)
149 ULONG newRef = --m_refCount;
156 // IWebIconDatabase --------------------------------------------------------------------
158 HRESULT STDMETHODCALLTYPE WebIconDatabase::sharedIconDatabase(
159 /* [retval][out] */ IWebIconDatabase** result)
161 *result = sharedWebIconDatabase();
165 HRESULT STDMETHODCALLTYPE WebIconDatabase::iconForURL(
167 /* [optional][in] */ LPSIZE size,
168 /* [optional][in] */ BOOL /*cache*/,
169 /* [retval][out] */ OLE_HANDLE* bitmap)
171 IntSize intSize(*size);
175 icon = iconDatabase().synchronousIconForPageURL(String(url, SysStringLen(url)), intSize);
177 // Make sure we check for the case of an "empty image"
178 if (icon && icon->width()) {
179 *bitmap = (OLE_HANDLE)(ULONG64)getOrCreateSharedBitmap(size);
180 if (!icon->getHBITMAPOfSize((HBITMAP)(ULONG64)*bitmap, size)) {
181 LOG_ERROR("Failed to draw Image to HBITMAP");
188 return defaultIconWithSize(size, bitmap);
191 HRESULT STDMETHODCALLTYPE WebIconDatabase::defaultIconWithSize(
192 /* [in] */ LPSIZE size,
193 /* [retval][out] */ OLE_HANDLE* result)
195 *result = (OLE_HANDLE)(ULONG64)getOrCreateDefaultIconBitmap(size);
199 HRESULT STDMETHODCALLTYPE WebIconDatabase::retainIconForURL(
202 iconDatabase().retainIconForPageURL(String(url, SysStringLen(url)));
206 HRESULT STDMETHODCALLTYPE WebIconDatabase::releaseIconForURL(
209 iconDatabase().releaseIconForPageURL(String(url, SysStringLen(url)));
213 HRESULT STDMETHODCALLTYPE WebIconDatabase::removeAllIcons(void)
215 iconDatabase().removeAllIcons();
219 HRESULT STDMETHODCALLTYPE WebIconDatabase::delayDatabaseCleanup(void)
221 IconDatabase::delayDatabaseCleanup();
225 HRESULT STDMETHODCALLTYPE WebIconDatabase::allowDatabaseCleanup(void)
227 IconDatabase::allowDatabaseCleanup();
231 HRESULT STDMETHODCALLTYPE WebIconDatabase::iconURLForURL(
233 /* [retval][out] */ BSTR* iconURL)
235 if (!url || !iconURL)
237 BString iconURLBSTR(iconDatabase().synchronousIconURLForPageURL(String(url, SysStringLen(url))));
238 *iconURL = iconURLBSTR.release();
242 HRESULT STDMETHODCALLTYPE WebIconDatabase::isEnabled(
243 /* [retval][out] */ BOOL *result)
245 *result = iconDatabase().isEnabled();
249 HRESULT STDMETHODCALLTYPE WebIconDatabase::setEnabled(
250 /* [in] */ BOOL flag)
252 BOOL currentlyEnabled;
253 isEnabled(¤tlyEnabled);
254 if (currentlyEnabled && !flag) {
255 iconDatabase().setEnabled(false);
256 shutDownIconDatabase();
257 } else if (!currentlyEnabled && flag) {
258 iconDatabase().setEnabled(true);
259 startUpIconDatabase();
264 HRESULT STDMETHODCALLTYPE WebIconDatabase::hasIconForURL(
266 /* [out][retval] */ BOOL* result)
271 String urlString(url, SysStringLen(url));
273 // Passing a size parameter of 0, 0 means we don't care about the result of the image, we just
274 // want to make sure the read from disk to load the icon is kicked off.
275 iconDatabase().synchronousIconForPageURL(urlString, IntSize(0, 0));
277 // Check to see if we have a non-empty icon URL for the page, and if we do, we have an icon for
279 *result = !(iconDatabase().synchronousIconURLForPageURL(urlString).isEmpty());
284 HBITMAP createDIB(LPSIZE size)
286 BitmapInfo bmInfo = BitmapInfo::create(IntSize(*size));
289 return CreateDIBSection(dc, &bmInfo, DIB_RGB_COLORS, 0, 0, 0);
292 HBITMAP WebIconDatabase::getOrCreateSharedBitmap(LPSIZE size)
294 HBITMAP result = m_sharedIconMap.get(*size);
297 result = createDIB(size);
298 m_sharedIconMap.set(*size, result);
302 HBITMAP WebIconDatabase::getOrCreateDefaultIconBitmap(LPSIZE size)
304 HBITMAP result = m_defaultIconMap.get(*size);
308 result = createDIB(size);
310 m_defaultIconMap.set(*size, result);
311 if (!iconDatabase().defaultIcon(*size)->getHBITMAPOfSize(result, size)) {
312 LOG_ERROR("Failed to draw Image to HBITMAP");
318 // IconDatabaseClient
320 bool WebIconDatabase::performImport()
322 // Windows doesn't do any old-style database importing.
326 void WebIconDatabase::didRemoveAllIcons()
328 // Queueing the empty string is a special way of saying "this queued notification is the didRemoveAllIcons notification"
329 MutexLocker locker(m_notificationMutex);
330 m_notificationQueue.append(String());
331 scheduleNotificationDelivery();
334 void WebIconDatabase::didImportIconURLForPageURL(const WTF::String& pageURL)
336 MutexLocker locker(m_notificationMutex);
337 m_notificationQueue.append(pageURL.isolatedCopy());
338 scheduleNotificationDelivery();
341 void WebIconDatabase::didImportIconDataForPageURL(const WTF::String& pageURL)
343 // WebKit1 only has a single "icon did change" notification.
344 didImportIconURLForPageURL(pageURL);
347 void WebIconDatabase::didChangeIconForPageURL(const WTF::String& pageURL)
349 // WebKit1 only has a single "icon did change" notification.
350 didImportIconURLForPageURL(pageURL);
353 void WebIconDatabase::didFinishURLImport()
357 void WebIconDatabase::scheduleNotificationDelivery()
359 // Caller of this method must hold the m_notificationQueue lock
360 ASSERT(!m_notificationMutex.tryLock());
362 if (!m_deliveryRequested) {
363 m_deliveryRequested = true;
364 callOnMainThread(deliverNotifications, 0);
368 BSTR WebIconDatabase::iconDatabaseDidAddIconNotification()
370 static BSTR didAddIconName = SysAllocString(WebIconDatabaseDidAddIconNotification);
371 return didAddIconName;
374 CFStringRef WebIconDatabase::iconDatabaseNotificationUserInfoURLKey()
376 static CFStringRef iconUserInfoURLKey = String(WebIconNotificationUserInfoURLKey).createCFString();
377 return iconUserInfoURLKey;
380 BSTR WebIconDatabase::iconDatabaseDidRemoveAllIconsNotification()
382 static BSTR didRemoveAllIconsName = SysAllocString(WebIconDatabaseDidRemoveAllIconsNotification);
383 return didRemoveAllIconsName;
386 static void postDidRemoveAllIconsNotification(WebIconDatabase* iconDB)
388 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
389 notifyCenter->postNotificationName(WebIconDatabase::iconDatabaseDidRemoveAllIconsNotification(), static_cast<IWebIconDatabase*>(iconDB), 0);
392 static void postDidAddIconNotification(const String& pageURL, WebIconDatabase* iconDB)
394 RetainPtr<CFMutableDictionaryRef> dictionary(AdoptCF,
395 CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
397 RetainPtr<CFStringRef> url(AdoptCF, pageURL.createCFString());
398 CFDictionaryAddValue(dictionary.get(), WebIconDatabase::iconDatabaseNotificationUserInfoURLKey(), url.get());
400 COMPtr<CFDictionaryPropertyBag> userInfo = CFDictionaryPropertyBag::createInstance();
401 userInfo->setDictionary(dictionary.get());
403 IWebNotificationCenter* notifyCenter = WebNotificationCenter::defaultCenterInternal();
404 notifyCenter->postNotificationName(WebIconDatabase::iconDatabaseDidAddIconNotification(), static_cast<IWebIconDatabase*>(iconDB), userInfo.get());
407 void WebIconDatabase::deliverNotifications(void*)
409 ASSERT(m_sharedWebIconDatabase);
410 if (!m_sharedWebIconDatabase)
413 ASSERT(m_sharedWebIconDatabase->m_deliveryRequested);
415 Vector<String> queue;
417 MutexLocker locker(m_sharedWebIconDatabase->m_notificationMutex);
418 queue.swap(m_sharedWebIconDatabase->m_notificationQueue);
419 m_sharedWebIconDatabase->m_deliveryRequested = false;
422 for (unsigned i = 0; i < queue.size(); ++i) {
423 if (queue[i].isNull())
424 postDidRemoveAllIconsNotification(m_sharedWebIconDatabase);
426 postDidAddIconNotification(queue[i], m_sharedWebIconDatabase);