WebKitWebContext.cpp [plain text]
#include "config.h"
#include "WebKitWebContext.h"
#include "WebContext.h"
#include "WebKitCookieManagerPrivate.h"
#include "WebKitDownloadClient.h"
#include "WebKitDownloadPrivate.h"
#include "WebKitPrivate.h"
#include "WebKitWebContextPrivate.h"
#include <wtf/HashMap.h>
#include <wtf/gobject/GRefPtr.h>
#include <wtf/text/CString.h>
using namespace WebKit;
enum {
DOWNLOAD_STARTED,
LAST_SIGNAL
};
struct _WebKitWebContextPrivate {
WKRetainPtr<WKContextRef> context;
GRefPtr<WebKitCookieManager> cookieManager;
};
static guint signals[LAST_SIGNAL] = { 0, };
G_DEFINE_TYPE(WebKitWebContext, webkit_web_context, G_TYPE_OBJECT)
static void webkitWebContextFinalize(GObject* object)
{
WEBKIT_WEB_CONTEXT(object)->priv->~WebKitWebContextPrivate();
G_OBJECT_CLASS(webkit_web_context_parent_class)->finalize(object);
}
static void webkit_web_context_init(WebKitWebContext* webContext)
{
WebKitWebContextPrivate* priv = G_TYPE_INSTANCE_GET_PRIVATE(webContext, WEBKIT_TYPE_WEB_CONTEXT, WebKitWebContextPrivate);
webContext->priv = priv;
new (priv) WebKitWebContextPrivate();
}
static void webkit_web_context_class_init(WebKitWebContextClass* webContextClass)
{
GObjectClass* gObjectClass = G_OBJECT_CLASS(webContextClass);
gObjectClass->finalize = webkitWebContextFinalize;
signals[DOWNLOAD_STARTED] =
g_signal_new("download-started",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
WEBKIT_TYPE_DOWNLOAD);
g_type_class_add_private(webContextClass, sizeof(WebKitWebContextPrivate));
}
static gpointer createDefaultWebContext(gpointer)
{
static GRefPtr<WebKitWebContext> webContext = adoptGRef(WEBKIT_WEB_CONTEXT(g_object_new(WEBKIT_TYPE_WEB_CONTEXT, NULL)));
webContext->priv->context = WKContextGetSharedProcessContext();
WKContextSetCacheModel(webContext->priv->context.get(), kWKCacheModelPrimaryWebBrowser);
attachDownloadClientToContext(webContext.get());
return webContext.get();
}
WebKitWebContext* webkit_web_context_get_default(void)
{
static GOnce onceInit = G_ONCE_INIT;
return WEBKIT_WEB_CONTEXT(g_once(&onceInit, createDefaultWebContext, 0));
}
void webkit_web_context_set_cache_model(WebKitWebContext* context, WebKitCacheModel model)
{
WKCacheModel cacheModel;
g_return_if_fail(WEBKIT_IS_WEB_CONTEXT(context));
switch (model) {
case WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER:
cacheModel = kWKCacheModelDocumentViewer;
break;
case WEBKIT_CACHE_MODEL_WEB_BROWSER:
cacheModel = kWKCacheModelPrimaryWebBrowser;
break;
case WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER:
cacheModel = kWKCacheModelDocumentBrowser;
break;
default:
g_assert_not_reached();
}
WebKitWebContextPrivate* priv = context->priv;
if (cacheModel != WKContextGetCacheModel(priv->context.get()))
WKContextSetCacheModel(priv->context.get(), cacheModel);
}
WebKitCacheModel webkit_web_context_get_cache_model(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), WEBKIT_CACHE_MODEL_WEB_BROWSER);
WebKitWebContextPrivate* priv = context->priv;
switch (WKContextGetCacheModel(priv->context.get())) {
case kWKCacheModelDocumentViewer:
return WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER;
case kWKCacheModelPrimaryWebBrowser:
return WEBKIT_CACHE_MODEL_WEB_BROWSER;
case kWKCacheModelDocumentBrowser:
return WEBKIT_CACHE_MODEL_DOCUMENT_BROWSER;
default:
g_assert_not_reached();
}
return WEBKIT_CACHE_MODEL_WEB_BROWSER;
}
typedef HashMap<WKDownloadRef, GRefPtr<WebKitDownload> > DownloadsMap;
static DownloadsMap& downloadsMap()
{
DEFINE_STATIC_LOCAL(DownloadsMap, downloads, ());
return downloads;
}
WebKitDownload* webkit_web_context_download_uri(WebKitWebContext* context, const gchar* uri)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
g_return_val_if_fail(uri, 0);
WebKitWebContextPrivate* priv = context->priv;
WKRetainPtr<WKURLRef> wkURL(AdoptWK, WKURLCreateWithUTF8CString(uri));
WKRetainPtr<WKURLRequestRef> wkRequest(AdoptWK, WKURLRequestCreateWithWKURL(wkURL.get()));
WKRetainPtr<WKDownloadRef> wkDownload = WKContextDownloadURLRequest(priv->context.get(), wkRequest.get());
WebKitDownload* download = webkitDownloadCreate(wkDownload.get());
downloadsMap().set(wkDownload.get(), download);
return download;
}
WebKitCookieManager* webkit_web_context_get_cookie_manager(WebKitWebContext* context)
{
g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(context), 0);
WebKitWebContextPrivate* priv = context->priv;
if (!priv->cookieManager)
priv->cookieManager = adoptGRef(webkitCookieManagerCreate(WKContextGetCookieManager(priv->context.get())));
return priv->cookieManager.get();
}
WebKitDownload* webkitWebContextGetOrCreateDownload(WKDownloadRef wkDownload)
{
GRefPtr<WebKitDownload> download = downloadsMap().get(wkDownload);
if (download)
return download.get();
download = adoptGRef(webkitDownloadCreate(wkDownload));
downloadsMap().set(wkDownload, download.get());
return download.get();
}
void webkitWebContextRemoveDownload(WKDownloadRef wkDownload)
{
downloadsMap().remove(wkDownload);
}
void webkitWebContextDownloadStarted(WebKitWebContext* context, WebKitDownload* download)
{
g_signal_emit(context, signals[DOWNLOAD_STARTED], 0, download);
}
WKContextRef webkitWebContextGetWKContext(WebKitWebContext* context)
{
g_assert(WEBKIT_IS_WEB_CONTEXT(context));
return context->priv->context.get();
}