diff options
author | Konstantin Tokarev <annulen@yandex.ru> | 2015-09-24 21:24:57 +0300 |
---|---|---|
committer | Konstantin Tokarev <annulen@yandex.ru> | 2015-09-24 23:27:31 +0000 |
commit | 3ca84bc574c141ea317d2715d868ff4d321a00fa (patch) | |
tree | fac2f9e37a475e628a4c8557052b622cc0393f88 /Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp | |
parent | 7dac8c2d5f743563df76c2347c6ad394b6779ffc (diff) | |
download | qtwebkit-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>
Diffstat (limited to 'Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp')
-rw-r--r-- | Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp | 52 |
1 files changed, 41 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) |