#include "config.h"
#include "QtWebContext.h"
#include "QtDownloadManager.h"
#include "QtWebIconDatabaseClient.h"
#include "WebInspectorServer.h"
#include "qquickwebview_p_p.h"
#include <QDir>
#include <QStandardPaths>
#include <QStringBuilder>
#include <WKAPICast.h>
#include <WKArray.h>
#include <WKContextPrivate.h>
#include <WKPage.h>
#include <WKString.h>
#include <WKStringQt.h>
#include <WKType.h>
namespace WebKit {
static QtWebContext* s_defaultQtWebContext = 0;
static void initInspectorServer()
{
#if ENABLE(INSPECTOR_SERVER)
QString inspectorEnv = QString::fromUtf8(qgetenv("QTWEBKIT_INSPECTOR_SERVER"));
if (!inspectorEnv.isEmpty()) {
QString bindAddress = QLatin1String("127.0.0.1");
QString portStr = inspectorEnv;
int port = 0;
int portColonPos = inspectorEnv.lastIndexOf(':');
if (portColonPos != -1) {
portStr = inspectorEnv.mid(portColonPos + 1);
bindAddress = inspectorEnv.mid(0, portColonPos);
}
bool ok = false;
port = portStr.toInt(&ok);
if (!ok) {
qWarning("Non numeric port for the inspector server \"%s\". Examples of valid input: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's interface).", qPrintable(portStr));
return;
}
bool success = WebInspectorServer::shared().listen(bindAddress, port);
if (success) {
QString inspectorServerUrl = QString::fromLatin1("http://%1:%2").arg(bindAddress).arg(port);
qWarning("Inspector server started successfully. Try pointing a WebKit browser to %s", qPrintable(inspectorServerUrl));
} else
qWarning("Couldn't start the inspector server on bind address \"%s\" and port \"%d\". In case of invalid input, try something like: \"12345\" or \"192.168.2.14:12345\" (with the address of one of this host's interface).", qPrintable(bindAddress), port);
}
#endif
}
static void globalInitialization()
{
static bool initialized = false;
if (initialized)
return;
initInspectorServer();
initialized = true;
}
static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*)
{
if (!WKStringIsEqualToUTF8CString(messageName, "MessageFromNavigatorQtObject"))
return;
ASSERT(messageBody);
ASSERT(WKGetTypeID(messageBody) == WKArrayGetTypeID());
WKArrayRef body = static_cast<WKArrayRef>(messageBody);
ASSERT(WKArrayGetSize(body) == 2);
ASSERT(WKGetTypeID(WKArrayGetItemAtIndex(body, 0)) == WKPageGetTypeID());
ASSERT(WKGetTypeID(WKArrayGetItemAtIndex(body, 1)) == WKStringGetTypeID());
WKPageRef page = static_cast<WKPageRef>(WKArrayGetItemAtIndex(body, 0));
WKStringRef str = static_cast<WKStringRef>(WKArrayGetItemAtIndex(body, 1));
QQuickWebViewPrivate::get(page)->didReceiveMessageFromNavigatorQtObject(str);
}
static void initializeContextInjectedBundleClient(WKContextRef context)
{
WKContextInjectedBundleClient injectedBundleClient;
memset(&injectedBundleClient, 0, sizeof(WKContextInjectedBundleClient));
injectedBundleClient.version = kWKContextInjectedBundleClientCurrentVersion;
injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
WKContextSetInjectedBundleClient(context, &injectedBundleClient);
}
QtWebContext::QtWebContext(WKContextRef context)
: m_context(context)
, m_downloadManager(new QtDownloadManager(context))
, m_iconDatabase(new QtWebIconDatabaseClient(context))
{
}
QtWebContext::~QtWebContext()
{
}
QtWebContext* QtWebContext::create(WKContextRef context)
{
globalInitialization();
return new QtWebContext(context);
}
QtWebContext* QtWebContext::defaultContext()
{
if (!s_defaultQtWebContext) {
WKRetainPtr<WKContextRef> wkContext = adoptWK(WKContextCreate());
initializeContextInjectedBundleClient(wkContext.get());
WKContextSetCacheModel(wkContext.get(), kWKCacheModelDocumentBrowser);
WKContextSetDatabaseDirectory(wkContext.get(), adoptWK(WKStringCreateWithQString(preparedStoragePath(DatabaseStorage))).get());
WKContextSetLocalStorageDirectory(wkContext.get(), adoptWK(WKStringCreateWithQString(preparedStoragePath(LocalStorage))).get());
WKContextSetCookieStorageDirectory(wkContext.get(), adoptWK(WKStringCreateWithQString(preparedStoragePath(CookieStorage))).get());
WKContextSetDiskCacheDirectory(wkContext.get(), adoptWK(WKStringCreateWithQString(preparedStoragePath(DiskCacheStorage))).get());
s_defaultQtWebContext = QtWebContext::create(wkContext.get());
}
return s_defaultQtWebContext;
}
static QString defaultLocation(QStandardPaths::StandardLocation type)
{
QString path = QStandardPaths::writableLocation(type);
Q_ASSERT(!path.isEmpty());
return path % QDir::separator() % QStringLiteral(".QtWebKit") % QDir::separator();
}
QString QtWebContext::preparedStoragePath(StorageType type)
{
QString path;
switch (type) {
case DatabaseStorage:
path = defaultLocation(QStandardPaths::DataLocation) % QStringLiteral("Databases");
QDir::root().mkpath(path);
break;
case LocalStorage:
path = defaultLocation(QStandardPaths::DataLocation) % QStringLiteral("LocalStorage");
QDir::root().mkpath(path);
break;
case CookieStorage:
path = defaultLocation(QStandardPaths::DataLocation);
QDir::root().mkpath(path);
break;
case DiskCacheStorage:
path = defaultLocation(QStandardPaths::CacheLocation) % QStringLiteral("DiskCache");
QDir::root().mkpath(path);
break;
case IconDatabaseStorage:
path = defaultLocation(QStandardPaths::DataLocation);
QDir::root().mkpath(path);
path += QStringLiteral("WebpageIcons.db");
break;
default:
Q_ASSERT(false);
}
return path;
}
}