summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Tokarev <annulen@yandex.ru>2015-09-24 21:24:57 +0300
committerKonstantin Tokarev <annulen@yandex.ru>2015-09-24 23:27:31 +0000
commit3ca84bc574c141ea317d2715d868ff4d321a00fa (patch)
treefac2f9e37a475e628a4c8557052b622cc0393f88
parent7dac8c2d5f743563df76c2347c6ad394b6779ffc (diff)
downloadqtwebkit-3ca84bc574c141ea317d2715d868ff4d321a00fa.tar.gz
Added userStyleSheets property to WebView.experimental.
This is similar to QWebSettings::setUserStyleSheetUrl() from widgets API, and shares most of helper code with existing userScripts property. Change-Id: I9352e6b4338c5750ed2ea28efbb2aba045cf7847 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp52
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h4
-rw-r--r--Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h2
3 files changed, 47 insertions, 11 deletions
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp b/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
index 11ebb4f81..bc2d5d21c 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp
@@ -621,6 +621,7 @@ void QQuickWebViewPrivate::didRelaunchProcess()
updateViewportSize();
updateUserScripts();
+ updateUserStyleSheets();
updateSchemeDelegates();
}
@@ -874,27 +875,32 @@ void QQuickWebViewPrivate::setNavigatorQtObjectEnabled(bool enabled)
WKPagePostMessageToInjectedBundle(webPage.get(), messageName, wkEnabled.get());
}
-static WKRetainPtr<WKStringRef> readUserScript(const QUrl& url)
+static WKRetainPtr<WKStringRef> readUserFile(const QUrl& url, const char* userFileType)
{
+ if (!url.isValid()) {
+ qWarning("QQuickWebView: Couldn't open '%s' as %s because URL is invalid.", qPrintable(url.toString()), userFileType);
+ return 0;
+ }
+
QString path;
if (url.isLocalFile())
path = url.toLocalFile();
else if (url.scheme() == QLatin1String("qrc"))
path = QStringLiteral(":") + url.path();
else {
- qWarning("QQuickWebView: Couldn't open '%s' as user script because only file:/// and qrc:/// URLs are supported.", qPrintable(url.toString()));
+ qWarning("QQuickWebView: Couldn't open '%s' as %s because only file:/// and qrc:/// URLs are supported.", qPrintable(url.toString()), userFileType);
return 0;
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- qWarning("QQuickWebView: Couldn't open '%s' as user script due to error '%s'.", qPrintable(url.toString()), qPrintable(file.errorString()));
+ qWarning("QQuickWebView: Couldn't open '%s' as %s due to error '%s'.", qPrintable(url.toString()), userFileType, qPrintable(file.errorString()));
return 0;
}
QByteArray contents = file.readAll();
if (contents.isEmpty())
- qWarning("QQuickWebView: Ignoring '%s' as user script because file is empty.", qPrintable(url.toString()));
+ qWarning("QQuickWebView: Ignoring '%s' as %s because file is empty.", qPrintable(url.toString()), userFileType);
return adoptWK(WKStringCreateWithUTF8CString(contents.constData()));
}
@@ -905,17 +911,25 @@ void QQuickWebViewPrivate::updateUserScripts()
// each Page/WebView pair we create.
WKPageGroupRemoveAllUserScripts(pageGroup.get());
- for (unsigned i = 0; i < userScripts.size(); ++i) {
- const QUrl& url = userScripts.at(i);
- if (!url.isValid()) {
- qWarning("QQuickWebView: Couldn't open '%s' as user script because URL is invalid.", qPrintable(url.toString()));
+ foreach (const QUrl& url, userScripts) {
+ WKRetainPtr<WKStringRef> contents = readUserFile(url, "user script");
+ if (!contents || WKStringIsEmpty(contents.get()))
continue;
- }
+ WKPageGroupAddUserScript(pageGroup.get(), contents.get(), /*baseURL*/ 0, /*whitelistedURLPatterns*/ 0, /*blacklistedURLPatterns*/ 0, kWKInjectInTopFrameOnly, kWKInjectAtDocumentEnd);
+ }
+}
+
+void QQuickWebViewPrivate::updateUserStyleSheets()
+{
+ // This feature works per-WebView because we keep an unique page group for
+ // each Page/WebView pair we create.
+ WKPageGroupRemoveAllUserStyleSheets(pageGroup.get());
- WKRetainPtr<WKStringRef> contents = readUserScript(url);
+ foreach (const QUrl& url, userStyleSheets) {
+ WKRetainPtr<WKStringRef> contents = readUserFile(url, "user style sheet");
if (!contents || WKStringIsEmpty(contents.get()))
continue;
- WKPageGroupAddUserScript(pageGroup.get(), contents.get(), /*baseURL*/ 0, /*whitelistedURLPatterns*/ 0, /*blacklistedURLPatterns*/ 0, kWKInjectInTopFrameOnly, kWKInjectAtDocumentEnd);
+ WKPageGroupAddUserStyleSheet(pageGroup.get(), contents.get(), /*baseURL*/ 0, /*whitelistedURLPatterns*/ 0, /*blacklistedURLPatterns*/ 0, kWKInjectInTopFrameOnly);
}
}
@@ -1524,6 +1538,22 @@ void QQuickWebViewExperimental::setUserScripts(const QList<QUrl>& userScripts)
emit userScriptsChanged();
}
+QList<QUrl> QQuickWebViewExperimental::userStyleSheets() const
+{
+ Q_D(const QQuickWebView);
+ return d->userStyleSheets;
+}
+
+void QQuickWebViewExperimental::setUserStyleSheets(const QList<QUrl>& userStyleSheets)
+{
+ Q_D(QQuickWebView);
+ if (d->userStyleSheets == userStyleSheets)
+ return;
+ d->userStyleSheets = userStyleSheets;
+ d->updateUserStyleSheets();
+ emit userStyleSheetsChanged();
+}
+
QUrl QQuickWebViewExperimental::remoteInspectorUrl() const
{
#if ENABLE(INSPECTOR_SERVER)
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
index 8604dead2..1daf5db0a 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h
@@ -281,6 +281,7 @@ class QWEBKIT_EXPORT QQuickWebViewExperimental : public QObject {
Q_PROPERTY(QQmlListProperty<QQuickUrlSchemeDelegate> urlSchemeDelegates READ schemeDelegates)
Q_PROPERTY(QString userAgent READ userAgent WRITE setUserAgent NOTIFY userAgentChanged)
Q_PROPERTY(QList<QUrl> userScripts READ userScripts WRITE setUserScripts NOTIFY userScriptsChanged)
+ Q_PROPERTY(QList<QUrl> userStyleSheets READ userStyleSheets WRITE setUserStyleSheets NOTIFY userStyleSheetsChanged)
Q_PROPERTY(QUrl remoteInspectorUrl READ remoteInspectorUrl NOTIFY remoteInspectorUrlChanged FINAL)
#ifdef HAVE_WEBCHANNEL
Q_PROPERTY(QQmlWebChannel* webChannel READ webChannel WRITE setWebChannel NOTIFY webChannelChanged)
@@ -331,6 +332,8 @@ public:
void setDeviceHeight(int);
QList<QUrl> userScripts() const;
void setUserScripts(const QList<QUrl>& userScripts);
+ QList<QUrl> userStyleSheets() const;
+ void setUserStyleSheets(const QList<QUrl>& userScripts);
QUrl remoteInspectorUrl() const;
QWebKitTest* test();
@@ -396,6 +399,7 @@ Q_SIGNALS:
void enterFullScreenRequested();
void exitFullScreenRequested();
void userScriptsChanged();
+ void userStyleSheetsChanged();
void preferredMinimumContentsWidthChanged();
void remoteInspectorUrlChanged();
void textFound(int matchCount);
diff --git a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
index f09c16e90..a7358b5cf 100644
--- a/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
+++ b/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h
@@ -122,6 +122,7 @@ public:
bool transparentBackground() const;
void setNavigatorQtObjectEnabled(bool);
void updateUserScripts();
+ void updateUserStyleSheets();
void updateSchemeDelegates();
QPointF contentPos() const;
@@ -214,6 +215,7 @@ protected:
QQmlComponent* colorChooser;
QList<QUrl> userScripts;
+ QList<QUrl> userStyleSheets;
bool m_betweenLoadCommitAndFirstFrame;
bool m_useDefaultContentItemSize;