1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
* Copyright (C) 2012, 2017 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "WebKitFaviconDatabase.h"
#include "IconDatabase.h"
#include "WebKitFaviconDatabasePrivate.h"
#include <WebCore/Image.h>
#include <WebCore/IntSize.h>
#include <WebCore/SharedBuffer.h>
#include <glib/gi18n-lib.h>
#include <wtf/FileSystem.h>
#include <wtf/RunLoop.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/glib/WTFGType.h>
#include <wtf/text/CString.h>
#if PLATFORM(GTK)
#include <WebCore/GdkCairoUtilities.h>
#include <WebCore/RefPtrCairo.h>
#endif
using namespace WebKit;
using namespace WebCore;
/**
* WebKitFaviconDatabase:
*
* Provides access to the icons associated with web sites.
*
* WebKit will automatically look for available icons in <link>
* elements on opened pages as well as an existing favicon.ico and
* load the images found into a memory cache if possible. That cache
* is frozen to an on-disk database for persistence.
*
* If #WebKitSettings:enable-private-browsing is %TRUE, new icons
* won't be added to the on-disk database and no existing icons will
* be deleted from it. Nevertheless, WebKit will still store them in
* the in-memory cache during the current execution.
*/
enum {
FAVICON_CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0, };
struct _WebKitFaviconDatabasePrivate {
RefPtr<IconDatabase> iconDatabase;
};
WEBKIT_DEFINE_FINAL_TYPE(WebKitFaviconDatabase, webkit_favicon_database, G_TYPE_OBJECT, GObject)
static void webkit_favicon_database_class_init(WebKitFaviconDatabaseClass* faviconDatabaseClass)
{
/**
* WebKitFaviconDatabase::favicon-changed:
* @database: the object on which the signal is emitted
* @page_uri: the URI of the Web page containing the icon
* @favicon_uri: the URI of the favicon
*
* This signal is emitted when the favicon URI of @page_uri has
* been changed to @favicon_uri in the database. You can connect
* to this signal and call webkit_favicon_database_get_favicon()
* to get the favicon. If you are interested in the favicon of a
* #WebKitWebView it's easier to use the #WebKitWebView:favicon
* property. See webkit_web_view_get_favicon() for more details.
*/
signals[FAVICON_CHANGED] = g_signal_new(
"favicon-changed",
G_TYPE_FROM_CLASS(faviconDatabaseClass),
G_SIGNAL_RUN_LAST,
0, nullptr, nullptr,
g_cclosure_marshal_generic,
G_TYPE_NONE, 2,
G_TYPE_STRING,
G_TYPE_STRING);
}
WebKitFaviconDatabase* webkitFaviconDatabaseCreate()
{
return WEBKIT_FAVICON_DATABASE(g_object_new(WEBKIT_TYPE_FAVICON_DATABASE, nullptr));
}
static bool webkitFaviconDatabaseIsOpen(WebKitFaviconDatabase* database)
{
return !!database->priv->iconDatabase;
}
void webkitFaviconDatabaseOpen(WebKitFaviconDatabase* database, const String& path, bool isEphemeral)
{
if (webkitFaviconDatabaseIsOpen(database))
return;
database->priv->iconDatabase = IconDatabase::create(path, isEphemeral ? IconDatabase::AllowDatabaseWrite::No : IconDatabase::AllowDatabaseWrite::Yes);
}
void webkitFaviconDatabaseClose(WebKitFaviconDatabase* database)
{
database->priv->iconDatabase = nullptr;
}
#if PLATFORM(GTK)
void webkitFaviconDatabaseGetLoadDecisionForIcon(WebKitFaviconDatabase* database, const LinkIcon& icon, const String& pageURL, bool isEphemeral, Function<void(bool)>&& completionHandler)
{
if (!webkitFaviconDatabaseIsOpen(database)) {
completionHandler(false);
return;
}
database->priv->iconDatabase->checkIconURLAndSetPageURLIfNeeded(icon.url.string(), pageURL,
isEphemeral ? IconDatabase::AllowDatabaseWrite::No : IconDatabase::AllowDatabaseWrite::Yes,
[database = GRefPtr<WebKitFaviconDatabase>(database), url = icon.url.string().isolatedCopy(), pageURL = pageURL.isolatedCopy(), completionHandler = WTFMove(completionHandler)](bool found, bool changed) {
if (!webkitFaviconDatabaseIsOpen(database.get())) {
completionHandler(false);
return;
}
if (found && changed)
g_signal_emit(database.get(), signals[FAVICON_CHANGED], 0, pageURL.utf8().data(), url.utf8().data());
completionHandler(!found);
});
}
void webkitFaviconDatabaseSetIconForPageURL(WebKitFaviconDatabase* database, const LinkIcon& icon, API::Data& iconData, const String& pageURL, bool isEphemeral)
{
if (!webkitFaviconDatabaseIsOpen(database))
return;
database->priv->iconDatabase->setIconForPageURL(icon.url.string(), iconData.bytes(), iconData.size(), pageURL,
isEphemeral ? IconDatabase::AllowDatabaseWrite::No : IconDatabase::AllowDatabaseWrite::Yes,
[database = GRefPtr<WebKitFaviconDatabase>(database), url = icon.url.string().isolatedCopy(), pageURL = pageURL.isolatedCopy()](bool success) {
if (!webkitFaviconDatabaseIsOpen(database.get()) || !success)
return;
g_signal_emit(database.get(), signals[FAVICON_CHANGED], 0, pageURL.utf8().data(), url.utf8().data());
});
}
#endif
/**
* webkit_favicon_database_error_quark:
*
* Gets the quark for the domain of favicon database errors.
*
* Returns: favicon database error domain.
*/
GQuark webkit_favicon_database_error_quark(void)
{
return g_quark_from_static_string("WebKitFaviconDatabaseError");
}
#if PLATFORM(GTK)
void webkitFaviconDatabaseGetFaviconInternal(WebKitFaviconDatabase* database, const gchar* pageURI, bool isEphemeral, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
{
if (!webkitFaviconDatabaseIsOpen(database)) {
g_task_report_new_error(database, callback, userData, 0,
WEBKIT_FAVICON_DATABASE_ERROR, WEBKIT_FAVICON_DATABASE_ERROR_NOT_INITIALIZED, _("Favicons database not initialized yet"));
return;
}
if (g_str_has_prefix(pageURI, "about:")) {
g_task_report_new_error(database, callback, userData, 0,
WEBKIT_FAVICON_DATABASE_ERROR, WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_NOT_FOUND, _("Page %s does not have a favicon"), pageURI);
return;
}
GRefPtr<GTask> task = adoptGRef(g_task_new(database, cancellable, callback, userData));
WebKitFaviconDatabasePrivate* priv = database->priv;
priv->iconDatabase->loadIconForPageURL(String::fromUTF8(pageURI), isEphemeral ? IconDatabase::AllowDatabaseWrite::No : IconDatabase::AllowDatabaseWrite::Yes,
[task = WTFMove(task), pageURI = CString(pageURI)](RefPtr<cairo_surface_t>&& icon) {
if (!icon) {
g_task_return_new_error(task.get(), WEBKIT_FAVICON_DATABASE_ERROR, WEBKIT_FAVICON_DATABASE_ERROR_FAVICON_UNKNOWN,
_("Unknown favicon for page %s"), pageURI.data());
return;
}
g_task_return_pointer(task.get(), icon.leakRef(), reinterpret_cast<GDestroyNotify>(cairo_surface_destroy));
});
}
/**
* webkit_favicon_database_get_favicon:
* @database: a #WebKitFaviconDatabase
* @page_uri: URI of the page for which we want to retrieve the favicon
* @cancellable: (allow-none): A #GCancellable or %NULL.
* @callback: (scope async) (nullable): A #GAsyncReadyCallback to call when the request is
* satisfied or %NULL if you don't care about the result.
* @user_data: The data to pass to @callback.
*
* Asynchronously obtains a favicon image.
*
* Asynchronously obtains an image of the favicon for the
* given page URI. It returns the cached icon if it's in the database
* asynchronously waiting for the icon to be read from the database.
*
* This is an asynchronous method. When the operation is finished, callback will
* be invoked. You can then call webkit_favicon_database_get_favicon_finish()
* to get the result of the operation.
*/
void webkit_favicon_database_get_favicon(WebKitFaviconDatabase* database, const gchar* pageURI, GCancellable* cancellable, GAsyncReadyCallback callback, gpointer userData)
{
g_return_if_fail(WEBKIT_IS_FAVICON_DATABASE(database));
g_return_if_fail(pageURI);
webkitFaviconDatabaseGetFaviconInternal(database, pageURI, false, cancellable, callback, userData);
}
/**
* webkit_favicon_database_get_favicon_finish:
* @database: a #WebKitFaviconDatabase
* @result: A #GAsyncResult obtained from the #GAsyncReadyCallback passed to webkit_favicon_database_get_favicon()
* @error: (allow-none): Return location for error or %NULL.
*
* Finishes an operation started with webkit_favicon_database_get_favicon().
*
* Returns: (transfer full): a new favicon image, or %NULL in case of error.
*/
#if USE(GTK4)
GdkTexture* webkit_favicon_database_get_favicon_finish(WebKitFaviconDatabase* database, GAsyncResult* result, GError** error)
#else
cairo_surface_t* webkit_favicon_database_get_favicon_finish(WebKitFaviconDatabase* database, GAsyncResult* result, GError** error)
#endif
{
g_return_val_if_fail(WEBKIT_IS_FAVICON_DATABASE(database), nullptr);
g_return_val_if_fail(g_task_is_valid(result, database), nullptr);
#if USE(GTK4)
auto icon = adoptRef(static_cast<cairo_surface_t*>(g_task_propagate_pointer(G_TASK(result), error)));
return icon ? cairoSurfaceToGdkTexture(icon.get()).leakRef() : nullptr;
#else
return static_cast<cairo_surface_t*>(g_task_propagate_pointer(G_TASK(result), error));
#endif
}
#endif
/**
* webkit_favicon_database_get_favicon_uri:
* @database: a #WebKitFaviconDatabase
* @page_uri: URI of the page containing the icon
*
* Obtains the URI of the favicon for the given @page_uri.
*
* Returns: a newly allocated URI for the favicon, or %NULL if the
* database doesn't have a favicon for @page_uri.
*/
gchar* webkit_favicon_database_get_favicon_uri(WebKitFaviconDatabase* database, const gchar* pageURL)
{
g_return_val_if_fail(WEBKIT_IS_FAVICON_DATABASE(database), nullptr);
g_return_val_if_fail(pageURL, nullptr);
ASSERT(RunLoop::isMain());
if (!webkitFaviconDatabaseIsOpen(database))
return nullptr;
String iconURLForPageURL = database->priv->iconDatabase->iconURLForPageURL(String::fromUTF8(pageURL));
if (iconURLForPageURL.isEmpty())
return nullptr;
return g_strdup(iconURLForPageURL.utf8().data());
}
/**
* webkit_favicon_database_clear:
* @database: a #WebKitFaviconDatabase
*
* Clears all icons from the database.
*/
void webkit_favicon_database_clear(WebKitFaviconDatabase* database)
{
g_return_if_fail(WEBKIT_IS_FAVICON_DATABASE(database));
if (!webkitFaviconDatabaseIsOpen(database))
return;
database->priv->iconDatabase->clear([] { });
}
|