|
Line 0
a/Source/WebKit2/UIProcess/API/gtk/WebKitFaviconDatabase.cpp_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 Igalia S.L. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2,1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Library General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Library General Public License |
| 15 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 16 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 |
* Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "WebKitFaviconDatabase.h" |
| 22 |
|
| 23 |
#include "WebIconDatabase.h" |
| 24 |
#include "WebKitFaviconDatabasePrivate.h" |
| 25 |
#include "WebKitPrivate.h" |
| 26 |
#include "WebKitWebContext.h" |
| 27 |
#include <WebCore/FileSystem.h> |
| 28 |
#include <WebCore/Image.h> |
| 29 |
#include <WebCore/IntSize.h> |
| 30 |
#include <WebCore/RefPtrCairo.h> |
| 31 |
#include <glib/gi18n-lib.h> |
| 32 |
#include <wtf/MainThread.h> |
| 33 |
#include <wtf/gobject/GOwnPtr.h> |
| 34 |
#include <wtf/gobject/GRefPtr.h> |
| 35 |
#include <wtf/text/CString.h> |
| 36 |
|
| 37 |
using namespace WebKit; |
| 38 |
|
| 39 |
enum { |
| 40 |
ICON_READY, |
| 41 |
|
| 42 |
LAST_SIGNAL |
| 43 |
}; |
| 44 |
|
| 45 |
static guint signals[LAST_SIGNAL] = { 0, }; |
| 46 |
|
| 47 |
typedef Vector<GRefPtr<GSimpleAsyncResult> > PendingIconRequestVector; |
| 48 |
typedef HashMap<String, PendingIconRequestVector*> PendingIconRequestMap; |
| 49 |
|
| 50 |
struct _WebKitFaviconDatabasePrivate { |
| 51 |
RefPtr<WebIconDatabase> iconDatabase; |
| 52 |
PendingIconRequestMap pendingIconRequests; |
| 53 |
}; |
| 54 |
|
| 55 |
G_DEFINE_TYPE(WebKitFaviconDatabase, webkit_favicon_database, G_TYPE_OBJECT) |
| 56 |
|
| 57 |
static void closeIconDatabaseIfNeeded(WebKitFaviconDatabase* database) |
| 58 |
{ |
| 59 |
WebKitFaviconDatabasePrivate* priv = database->priv; |
| 60 |
if (priv->iconDatabase->isOpen()) |
| 61 |
priv->iconDatabase->close(); |
| 62 |
} |
| 63 |
|
| 64 |
static void webkit_favicon_database_init(WebKitFaviconDatabase* manager) |
| 65 |
{ |
| 66 |
WebKitFaviconDatabasePrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(manager, WEBKIT_TYPE_FAVICON_DATABASE, WebKitFaviconDatabasePrivate); |
| 67 |
manager->priv = priv; |
| 68 |
new (priv) WebKitFaviconDatabasePrivate(); |
| 69 |
} |
| 70 |
|
| 71 |
static void webkitFaviconDatabaseFinalize(GObject* object) |
| 72 |
{ |
| 73 |
WebKitFaviconDatabase* database = WEBKIT_FAVICON_DATABASE(object); |
| 74 |
WebKitFaviconDatabasePrivate* priv = database->priv; |
| 75 |
|
| 76 |
closeIconDatabaseIfNeeded(database); |
| 77 |
priv->~WebKitFaviconDatabasePrivate(); |
| 78 |
|
| 79 |
G_OBJECT_CLASS(webkit_favicon_database_parent_class)->finalize(object); |
| 80 |
} |
| 81 |
|
| 82 |
static void webkit_favicon_database_class_init(WebKitFaviconDatabaseClass* faviconDatabaseClass) |
| 83 |
{ |
| 84 |
GObjectClass* gObjectClass = G_OBJECT_CLASS(faviconDatabaseClass); |
| 85 |
gObjectClass->finalize = webkitFaviconDatabaseFinalize; |
| 86 |
|
| 87 |
/** |
| 88 |
* WebKitFaviconDatabase::favicon-ready: |
| 89 |
* @database: the object on which the signal is emitted |
| 90 |
* @page_uri: the URI of the Web page containing the icon. |
| 91 |
* |
| 92 |
* This signal gets emitted when the favicon of @page_uri is |
| 93 |
* ready. This means that the favicon's data is ready to be used |
| 94 |
* by the application, either because it has been loaded from the |
| 95 |
* network, if it's the first time it gets retrieved, or because |
| 96 |
* it has been already imported from the icon database. |
| 97 |
*/ |
| 98 |
signals[ICON_READY] = |
| 99 |
g_signal_new("favicon-ready", |
| 100 |
G_TYPE_FROM_CLASS(faviconDatabaseClass), |
| 101 |
G_SIGNAL_RUN_LAST, |
| 102 |
0, 0, 0, |
| 103 |
g_cclosure_marshal_VOID__STRING, |
| 104 |
G_TYPE_NONE, 1, |
| 105 |
G_TYPE_STRING); |
| 106 |
|
| 107 |
g_type_class_add_private(faviconDatabaseClass, sizeof(WebKitFaviconDatabasePrivate)); |
| 108 |
} |
| 109 |
|
| 110 |
struct GetFaviconSurfaceAsyncData { |
| 111 |
GRefPtr<WebKitFaviconDatabase> faviconDatabase; |
| 112 |
String pageURL; |
| 113 |
RefPtr<cairo_surface_t> icon; |
| 114 |
GRefPtr<GCancellable> cancellable; |
| 115 |
unsigned long cancelledId; |
| 116 |
|
| 117 |
~GetFaviconSurfaceAsyncData() |
| 118 |
{ |
| 119 |
if (cancelledId) |
| 120 |
g_cancellable_disconnect(cancellable.get(), cancelledId); |
| 121 |
} |
| 122 |
}; |
| 123 |
WEBKIT_DEFINE_ASYNC_DATA_STRUCT(GetFaviconSurfaceAsyncData) |
| 124 |
|
| 125 |
static cairo_surface_t* getIconSurfaceSynchronously(WebKitFaviconDatabase* database, const String& pageURL) |
| 126 |
{ |
| 127 |
ASSERT(isMainThread()); |
| 128 |
|
| 129 |
WebKitFaviconDatabasePrivate* priv = database->priv; |
| 130 |
|
| 131 |
// The exact size we pass is irrelevant to the iconDatabase code. |
| 132 |
// We must pass something greater than 0x0 to get an icon. |
| 133 |
WebCore::NativeImagePtr icon = priv->iconDatabase->nativeImageForPageURL(pageURL, WebCore::IntSize(1, 1)); |
| 134 |
if (!icon) |
| 135 |
return 0; |
| 136 |
|
| 137 |
return icon ? icon->surface() : 0; |
| 138 |
} |
| 139 |
|
| 140 |
static void deletePendingIconRequests(WebKitFaviconDatabase* database, PendingIconRequestVector* requests, const String& pageURL) |
| 141 |
{ |
| 142 |
database->priv->pendingIconRequests.remove(pageURL); |
| 143 |
delete requests; |
| 144 |
} |
| 145 |
|
| 146 |
static void processPendingIconsForURI(WebKitFaviconDatabase* database, const String& pageURL) |
| 147 |
{ |
| 148 |
PendingIconRequestVector* icons = database->priv->pendingIconRequests.get(pageURL); |
| 149 |
if (!icons) |
| 150 |
return; |
| 151 |
|
| 152 |
for (size_t i = 0; i < icons->size(); ++i) { |
| 153 |
GSimpleAsyncResult* result = icons->at(i).get(); |
| 154 |
GetFaviconSurfaceAsyncData* data = static_cast<GetFaviconSurfaceAsyncData*>(g_simple_async_result_get_op_res_gpointer(result)); |
| 155 |
data->icon = getIconSurfaceSynchronously(database, pageURL); |
| 156 |
|
| 157 |
g_simple_async_result_complete(result); |
| 158 |
} |
| 159 |
deletePendingIconRequests(database, icons, pageURL); |
| 160 |
} |
| 161 |
|
| 162 |
static void iconDataReadyForPageURLCallback(WKIconDatabaseRef wkIconDatabase, WKURLRef wkPageURL, const void* clientInfo) |
| 163 |
{ |
| 164 |
ASSERT(isMainThread()); |
| 165 |
|
| 166 |
WebKitFaviconDatabase* database = WEBKIT_FAVICON_DATABASE(clientInfo); |
| 167 |
String pageURLString = toImpl(wkPageURL)->string(); |
| 168 |
|
| 169 |
database->priv->iconDatabase->retainIconForPageURL(pageURLString); |
| 170 |
processPendingIconsForURI(database, pageURLString); |
| 171 |
g_signal_emit(database, signals[ICON_READY], 0, pageURLString.utf8().data()); |
| 172 |
} |
| 173 |
|
| 174 |
WebKitFaviconDatabase* webkitFaviconDatabaseCreate(WebIconDatabase* iconDatabase) |
| 175 |
{ |
| 176 |
WebKitFaviconDatabase* faviconDatabase = WEBKIT_FAVICON_DATABASE(g_object_new(WEBKIT_TYPE_FAVICON_DATABASE, NULL)); |
| 177 |
faviconDatabase->priv->iconDatabase = iconDatabase; |
| 178 |
|
| 179 |
WKIconDatabaseClient wkIconDatabaseClient = { |
| 180 |
kWKIconDatabaseClientCurrentVersion, |
| 181 |
faviconDatabase, // clientInfo |
| 182 |
0, // didChangeIconForPageURLCallback |
| 183 |
0, // didRemoveAllIconsCallback |
| 184 |
iconDataReadyForPageURLCallback, |
| 185 |
}; |
| 186 |
WKIconDatabaseSetIconDatabaseClient(toAPI(iconDatabase), &wkIconDatabaseClient); |
| 187 |
return faviconDatabase; |
| 188 |
} |
| 189 |
|
| 190 |
static PendingIconRequestVector* getOrCreatePendingIconRequests(WebKitFaviconDatabase* database, const String& pageURL) |
| 191 |
{ |
| 192 |
PendingIconRequestVector* icons = database->priv->pendingIconRequests.get(pageURL); |
| 193 |
if (!icons) { |
| 194 |
icons = new PendingIconRequestVector; |
| 195 |
database->priv->pendingIconRequests.set(pageURL, icons); |
| 196 |
} |
| 197 |
|
| 198 |
return icons; |
| 199 |
} |
| 200 |
|
| 201 |
static void getIconSurfaceCancelled(void* userData) |
| 202 |
{ |
| 203 |
GSimpleAsyncResult* result = static_cast<GSimpleAsyncResult*>(userData); |
| 204 |
|
| 205 |
// Get the data we might need and complete the request. |
| 206 |
GetFaviconSurfaceAsyncData* data = static_cast<GetFaviconSurfaceAsyncData*>(g_simple_async_result_get_op_res_gpointer(result)); |
| 207 |
WebKitFaviconDatabase* database = data->faviconDatabase.get(); |
| 208 |
String pageURL = data->pageURL; |
| 209 |
|
| 210 |
g_simple_async_result_complete(result); |
| 211 |
|
| 212 |
PendingIconRequestVector* icons = database->priv->pendingIconRequests.get(pageURL); |
| 213 |
if (!icons) |
| 214 |
return; |
| 215 |
|
| 216 |
size_t itemIndex = icons->find(result); |
| 217 |
if (itemIndex != notFound) |
| 218 |
icons->remove(itemIndex); |
| 219 |
if (icons->isEmpty()) |
| 220 |
deletePendingIconRequests(database, icons, pageURL); |
| 221 |
} |
| 222 |
|
| 223 |
static void getIconSurfaceCancelledCallback(GCancellable* cancellable, GSimpleAsyncResult* result) |
| 224 |
{ |
| 225 |
// Handle cancelled in a in idle since it might be called from any thread. |
| 226 |
callOnMainThread(getIconSurfaceCancelled, result); |
| 227 |
} |
| 228 |
|
| 229 |
static void setErrorForAsyncResult(GSimpleAsyncResult* result, WebKitFaviconDatabaseError error, const String& pageURL = String()) |
| 230 |
{ |
| 231 |
ASSERT(result); |
| 232 |
|
| 233 |
switch (error) { |
| 234 |
case WEBKIT_FAVICON_DATABASE_ERROR_NOT_INITIALIZED: |
| 235 |
g_simple_async_result_set_error(result, WEBKIT_FAVICON_DATABASE_ERROR, error, _("Favicons database not initialized yet")); |
| 236 |
break; |
| 237 |
|
| 238 |
case WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_NOT_FOUND: |
| 239 |
g_simple_async_result_set_error(result, WEBKIT_FAVICON_DATABASE_ERROR, error, _("Page %s does not have a favicon"), pageURL.utf8().data()); |
| 240 |
break; |
| 241 |
|
| 242 |
default: |
| 243 |
ASSERT_NOT_REACHED(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
GQuark webkit_favicon_database_error_quark(void) |
| 248 |
{ |
| 249 |
return g_quark_from_static_string("WebKitFaviconDatabaseError"); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* webkit_favicon_database_get_favicon: |
| 254 |
* @database: a #WebKitFaviconDatabase |
| 255 |
* @page_uri: URI of the page for which we want to retrieve the favicon |
| 256 |
* @cancellable: (allow-none): A #GCancellable or %NULL. |
| 257 |
* @callback: (scope async): A #GAsyncReadyCallback to call when the request is |
| 258 |
* satisfied or %NULL if you don't care about the result. |
| 259 |
* @user_data: (closure): The data to pass to @callback. |
| 260 |
* |
| 261 |
* Asynchronously obtains a #cairo_surface_t of the favicon for the |
| 262 |
* given page URI. It returns the cached icon if it's in the database |
| 263 |
* asynchronously waiting for the icon to be read from the database. |
| 264 |
* |
| 265 |
* This is an asynchronous method. When the operation is finished, callback will |
| 266 |
* be invoked. You can then call webkit_favicon_database_get_favicon_finish() |
| 267 |
* to get the result of the operation. |
| 268 |
*/ |
| 269 |
void webkit_favicon_database_get_favicon(WebKitFaviconDatabase* database, const gchar* pageURI, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData) |
| 270 |
{ |
| 271 |
g_return_if_fail(WEBKIT_IS_FAVICON_DATABASE(database)); |
| 272 |
g_return_if_fail(pageURI); |
| 273 |
|
| 274 |
GRefPtr<GSimpleAsyncResult> result = adoptGRef(g_simple_async_result_new(G_OBJECT(database), callback, userData, reinterpret_cast<gpointer>(webkit_favicon_database_get_favicon))); |
| 275 |
GetFaviconSurfaceAsyncData* data = createGetFaviconSurfaceAsyncData(); |
| 276 |
g_simple_async_result_set_op_res_gpointer(result.get(), data, reinterpret_cast<GDestroyNotify>(destroyGetFaviconSurfaceAsyncData)); |
| 277 |
data->faviconDatabase = database; |
| 278 |
data->pageURL = String::fromUTF8(pageURI); |
| 279 |
data->cancellable = cancellable; |
| 280 |
if (cancellable) { |
| 281 |
data->cancelledId = |
| 282 |
g_cancellable_connect(cancellable, G_CALLBACK(getIconSurfaceCancelledCallback), result.get(), 0); |
| 283 |
g_simple_async_result_set_check_cancellable(result.get(), cancellable); |
| 284 |
} |
| 285 |
|
| 286 |
WebKitFaviconDatabasePrivate* priv = database->priv; |
| 287 |
WebIconDatabase* iconDatabaseImpl = priv->iconDatabase.get(); |
| 288 |
if (!iconDatabaseImpl->isOpen()) { |
| 289 |
setErrorForAsyncResult(result.get(), WEBKIT_FAVICON_DATABASE_ERROR_NOT_INITIALIZED); |
| 290 |
g_simple_async_result_complete_in_idle(result.get()); |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
if (data->pageURL.isEmpty() || data->pageURL.startsWith("about:")) { |
| 295 |
setErrorForAsyncResult(result.get(), WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_NOT_FOUND, data->pageURL); |
| 296 |
g_simple_async_result_complete_in_idle(result.get()); |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
// We ask for the icon directly. If we don't get the icon data now, |
| 301 |
// we'll be notified later (even if the database is still importing icons). |
| 302 |
data->icon = getIconSurfaceSynchronously(database, data->pageURL); |
| 303 |
|
| 304 |
// If there's not a valid icon, but there's an iconURL registered, |
| 305 |
// or it's still not registered but the import process hasn't |
| 306 |
// finished yet, we need to wait for iconDataReadyForPage to be |
| 307 |
// called before making and informed decision. |
| 308 |
String iconURLForPageURL; |
| 309 |
iconDatabaseImpl->synchronousIconURLForPageURL(data->pageURL, iconURLForPageURL); |
| 310 |
if (!data->icon && (!iconURLForPageURL.isEmpty() || !iconDatabaseImpl->isUrlImportCompleted())) { |
| 311 |
PendingIconRequestVector* icons = getOrCreatePendingIconRequests(database, data->pageURL); |
| 312 |
ASSERT(icons); |
| 313 |
icons->append(result); |
| 314 |
return; |
| 315 |
} |
| 316 |
|
| 317 |
// If we reached this point without a valid icon, which means that |
| 318 |
// there's no iconURL registered for this pageURI and the |
| 319 |
// urlImport process has already finished, or do have a valid icon |
| 320 |
// but it's an empty image, that means there's no favicon. |
| 321 |
if (!data->icon || !cairo_image_surface_get_data(data->icon.get())) |
| 322 |
setErrorForAsyncResult(result.get(), WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_NOT_FOUND, data->pageURL); |
| 323 |
|
| 324 |
// Complete the asynchronous process; |
| 325 |
g_simple_async_result_complete_in_idle(result.get()); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* webkit_favicon_database_get_favicon_finish: |
| 330 |
* @database: a #WebKitFaviconDatabase |
| 331 |
* @result: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to webkit_favicon_database_get_favicon() |
| 332 |
* @error: (allow-none): Return location for error or %NULL. |
| 333 |
* |
| 334 |
* Finishes an operation started with webkit_favicon_database_get_favicon(). |
| 335 |
* |
| 336 |
* Returns: (transfer full): a new reference to a #cairo_surface_t, or |
| 337 |
* %NULL in case of error. |
| 338 |
*/ |
| 339 |
cairo_surface_t* webkit_favicon_database_get_favicon_finish(WebKitFaviconDatabase* database, GAsyncResult* result, GError** error) |
| 340 |
{ |
| 341 |
GSimpleAsyncResult* simpleResult = G_SIMPLE_ASYNC_RESULT(result); |
| 342 |
g_warn_if_fail(g_simple_async_result_get_source_tag(simpleResult) == webkit_favicon_database_get_favicon); |
| 343 |
|
| 344 |
if (g_simple_async_result_propagate_error(simpleResult, error)) |
| 345 |
return 0; |
| 346 |
|
| 347 |
GetFaviconSurfaceAsyncData* data = static_cast<GetFaviconSurfaceAsyncData*>(g_simple_async_result_get_op_res_gpointer(simpleResult)); |
| 348 |
ASSERT(data); |
| 349 |
|
| 350 |
// Return the icon is we can get it already. |
| 351 |
if (data->icon) |
| 352 |
return cairo_surface_reference(data->icon.get()); |
| 353 |
|
| 354 |
// If the icon is not available at this stage, investigate why |
| 355 |
// and come up with a descriptive, and hopefully helpful, error. |
| 356 |
String iconURLForPageURL; |
| 357 |
database->priv->iconDatabase->synchronousIconURLForPageURL(data->pageURL, iconURLForPageURL); |
| 358 |
|
| 359 |
if (iconURLForPageURL.isEmpty()) { |
| 360 |
// If there's no icon URL in the database at this point, we can |
| 361 |
// conclude there's no favicon at all for the requested page URL. |
| 362 |
g_set_error(error, WEBKIT_FAVICON_DATABASE_ERROR, WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_NOT_FOUND, |
| 363 |
_("Page %s does not have a favicon"), data->pageURL.utf8().data()); |
| 364 |
} else { |
| 365 |
// If the icon URL is known it obviously means that the icon data |
| 366 |
// hasn't been retrieved yet, so report that it's 'unknown'. |
| 367 |
g_set_error(error, WEBKIT_FAVICON_DATABASE_ERROR, WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_UNKNOWN, |
| 368 |
_("Unknown favicon for page %s"), data->pageURL.utf8().data()); |
| 369 |
} |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* webkit_favicon_database_get_favicon_uri: |
| 376 |
* @database: a #WebKitFaviconDatabase |
| 377 |
* @page_uri: URI of the page containing the icon |
| 378 |
* |
| 379 |
* Obtains the URI of the favicon for the given @page_uri. |
| 380 |
* |
| 381 |
* Returns: a newly allocated URI for the favicon, or %NULL if the |
| 382 |
* database doesn't have a favicon for @page_uri. |
| 383 |
*/ |
| 384 |
gchar* webkit_favicon_database_get_favicon_uri(WebKitFaviconDatabase* database, const gchar* pageURL) |
| 385 |
{ |
| 386 |
g_return_val_if_fail(WEBKIT_IS_FAVICON_DATABASE(database), 0); |
| 387 |
g_return_val_if_fail(pageURL, 0); |
| 388 |
ASSERT(isMainThread()); |
| 389 |
|
| 390 |
String iconURLForPageURL; |
| 391 |
database->priv->iconDatabase->synchronousIconURLForPageURL(String::fromUTF8(pageURL), iconURLForPageURL); |
| 392 |
if (iconURLForPageURL.isEmpty()) |
| 393 |
return 0; |
| 394 |
|
| 395 |
return g_strdup(iconURLForPageURL.utf8().data()); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* webkit_favicon_database_clear: |
| 400 |
* @database: a #WebKitFaviconDatabase |
| 401 |
* |
| 402 |
* Clears all icons from the database. |
| 403 |
*/ |
| 404 |
void webkit_favicon_database_clear(WebKitFaviconDatabase* database) |
| 405 |
{ |
| 406 |
g_return_if_fail(WEBKIT_IS_FAVICON_DATABASE(database)); |
| 407 |
|
| 408 |
database->priv->iconDatabase->removeAllIcons(); |
| 409 |
} |