From 9c279bec0cac934f3c8495c8c5ecba8ad59be47d Mon Sep 17 00:00:00 2001 From: Pierre Rossi Date: Tue, 3 Feb 2015 16:27:54 +0100 Subject: QtQuick API for userScripts QObject-based, allowing the same things as the Widgets API in a declarative fashion. Change-Id: I9c9819b691b2098ec47e0536d47ccd2a950126c2 Reviewed-by: Andras Becsi --- src/webengine/api/qquickwebenginescript.cpp | 202 ++++++++++++++++++++++++++ src/webengine/api/qquickwebenginescript_p.h | 109 ++++++++++++++ src/webengine/api/qquickwebenginescript_p_p.h | 68 +++++++++ src/webengine/api/qquickwebengineview.cpp | 50 +++++++ src/webengine/api/qquickwebengineview_p.h | 3 + src/webengine/api/qquickwebengineview_p_p.h | 7 + src/webengine/plugin/plugin.cpp | 1 + src/webengine/webengine.pro | 2 + 8 files changed, 442 insertions(+) create mode 100644 src/webengine/api/qquickwebenginescript.cpp create mode 100644 src/webengine/api/qquickwebenginescript_p.h create mode 100644 src/webengine/api/qquickwebenginescript_p_p.h (limited to 'src/webengine') diff --git a/src/webengine/api/qquickwebenginescript.cpp b/src/webengine/api/qquickwebenginescript.cpp new file mode 100644 index 000000000..872ba1533 --- /dev/null +++ b/src/webengine/api/qquickwebenginescript.cpp @@ -0,0 +1,202 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickwebenginescript_p.h" +#include "qquickwebenginescript_p_p.h" + +#include +#include +#include "user_script_controller_host.h" + +QQuickWebEngineScript::QQuickWebEngineScript() + : d_ptr(new QQuickWebEngineScriptPrivate) +{ + d_ptr->q_ptr = this; +} + +QQuickWebEngineScript::~QQuickWebEngineScript() +{ +} + +QString QQuickWebEngineScript::toString() const +{ + Q_D(const QQuickWebEngineScript); + if (d->coreScript.isNull()) + return QStringLiteral("QWebEngineScript()"); + QString ret = QStringLiteral("QWebEngineScript(") % d->coreScript.name() % QStringLiteral(", "); + switch (d->coreScript.injectionPoint()) { + case UserScript::DocumentElementCreation: + ret.append(QStringLiteral("WebEngineScript::DocumentCreation, ")); + break; + case UserScript::DocumentLoadFinished: + ret.append(QStringLiteral("WebEngineScript::DocumentReady, ")); + break; + case UserScript::AfterLoad: + ret.append(QStringLiteral("WebEngineScript::Deferred, ")); + break; + } + ret.append(QString::number(d->coreScript.worldId()) % QStringLiteral(", ") + % (d->coreScript.runsOnSubFrames() ? QStringLiteral("true") : QStringLiteral("false")) + % QStringLiteral(", ") % d->coreScript.source() % QLatin1Char(')')); + return ret; +} + +QString QQuickWebEngineScript::name() const +{ + Q_D(const QQuickWebEngineScript); + return d->coreScript.name(); +} + + +QString QQuickWebEngineScript::source() const +{ + Q_D(const QQuickWebEngineScript); + return d->coreScript.source(); +} + +ASSERT_ENUMS_MATCH(QQuickWebEngineScript::Deferred, UserScript::AfterLoad) +ASSERT_ENUMS_MATCH(QQuickWebEngineScript::DocumentReady, UserScript::DocumentLoadFinished) +ASSERT_ENUMS_MATCH(QQuickWebEngineScript::DocumentCreation, UserScript::DocumentElementCreation) + +QQuickWebEngineScript::InjectionPoint QQuickWebEngineScript::injectionPoint() const +{ + Q_D(const QQuickWebEngineScript); + return static_cast(d->coreScript.injectionPoint()); +} + + +QQuickWebEngineScript::ScriptWorldId QQuickWebEngineScript::worldId() const +{ + Q_D(const QQuickWebEngineScript); + return static_cast(d->coreScript.worldId()); +} + + +bool QQuickWebEngineScript::runOnSubframes() const +{ + Q_D(const QQuickWebEngineScript); + return d->coreScript.runsOnSubFrames(); +} + + +void QQuickWebEngineScript::setName(QString arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == name()) + return; + d->aboutToUpdateUnderlyingScript(); + d->coreScript.setName(arg); + Q_EMIT nameChanged(arg); +} + +void QQuickWebEngineScript::setSource(QString arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == source()) + return; + d->aboutToUpdateUnderlyingScript(); + d->coreScript.setSource(arg); + Q_EMIT sourceChanged(arg); +} + + +void QQuickWebEngineScript::setInjectionPoint(QQuickWebEngineScript::InjectionPoint arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == injectionPoint()) + return; + d->aboutToUpdateUnderlyingScript(); + d->coreScript.setInjectionPoint(static_cast(arg)); + Q_EMIT injectionPointChanged(arg); +} + + +void QQuickWebEngineScript::setWorldId(QQuickWebEngineScript::ScriptWorldId arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == worldId()) + return; + d->aboutToUpdateUnderlyingScript(); + d->coreScript.setWorldId(arg); + Q_EMIT worldIdChanged(arg); +} + + +void QQuickWebEngineScript::setRunOnSubframes(bool arg) +{ + Q_D(QQuickWebEngineScript); + if (arg == runOnSubframes()) + return; + d->aboutToUpdateUnderlyingScript(); + d->coreScript.setRunsOnSubFrames(arg); + Q_EMIT runOnSubframesChanged(arg); +} + +void QQuickWebEngineScript::timerEvent(QTimerEvent *e) +{ + Q_D(QQuickWebEngineScript); + if (e->timerId() != d->m_basicTimer.timerId()) { + QObject::timerEvent(e); + return; + } + if (!d->m_controllerHost) + return; + d->m_basicTimer.stop(); + d->m_controllerHost->addUserScript(d->coreScript, d->m_adapter); +} + +void QQuickWebEngineScriptPrivate::bind(UserScriptControllerHost *scriptController, WebContentsAdapter *adapter) +{ + aboutToUpdateUnderlyingScript(); + m_adapter = adapter; + m_controllerHost = scriptController; +} + +QQuickWebEngineScriptPrivate::QQuickWebEngineScriptPrivate() + :m_controllerHost(0) + , m_adapter(0) + +{ +} + +void QQuickWebEngineScriptPrivate::aboutToUpdateUnderlyingScript() +{ + Q_Q(QQuickWebEngineScript); + if (m_controllerHost) + m_controllerHost->removeUserScript(coreScript, m_adapter); + // Defer updates to the next event loop + m_basicTimer.start(0, q); +} diff --git a/src/webengine/api/qquickwebenginescript_p.h b/src/webengine/api/qquickwebenginescript_p.h new file mode 100644 index 000000000..a42aed41b --- /dev/null +++ b/src/webengine/api/qquickwebenginescript_p.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKWEBENGINESCRIPT_P_H +#define QQUICKWEBENGINESCRIPT_P_H + +#include +#include + +class UserScriptControllerHost; +class WebContentsAdapter; + +QT_BEGIN_NAMESPACE +class QQuickWebEngineScriptPrivate; +class QQuickWebEngineView; + +class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineScript : public QObject +{ + Q_OBJECT + Q_ENUMS(InjectionPoint) + Q_ENUMS(ScriptWorldId) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged) + Q_PROPERTY(InjectionPoint injectionPoint READ injectionPoint WRITE setInjectionPoint NOTIFY injectionPointChanged) + Q_PROPERTY(ScriptWorldId worldId READ worldId WRITE setWorldId NOTIFY worldIdChanged) + Q_PROPERTY(bool runOnSubframes READ runOnSubframes WRITE setRunOnSubframes NOTIFY runOnSubframesChanged) + + +public: + enum InjectionPoint { + Deferred, + DocumentReady, + DocumentCreation + }; + + enum ScriptWorldId { + MainWorld = 0, + ApplicationWorld, + UserWorld + }; + + QQuickWebEngineScript(); + ~QQuickWebEngineScript(); + Q_INVOKABLE QString toString() const; + + QString name() const; + QString source() const; + InjectionPoint injectionPoint() const; + ScriptWorldId worldId() const; + bool runOnSubframes() const; + +public Q_SLOTS: + void setName(QString arg); + void setSource(QString arg); + void setInjectionPoint(InjectionPoint arg); + void setWorldId(ScriptWorldId arg); + void setRunOnSubframes(bool arg); + +Q_SIGNALS: + void nameChanged(QString arg); + void sourceChanged(QString arg); + void injectionPointChanged(InjectionPoint arg); + void worldIdChanged(ScriptWorldId arg); + void runOnSubframesChanged(bool arg); + +protected: + void timerEvent(QTimerEvent *e) override; + +private: + friend class QQuickWebEngineViewPrivate; + Q_DECLARE_PRIVATE(QQuickWebEngineScript); + QScopedPointer d_ptr; +}; +QT_END_NAMESPACE + +#endif // QQUICKWEBENGINESCRIPT_P_H diff --git a/src/webengine/api/qquickwebenginescript_p_p.h b/src/webengine/api/qquickwebenginescript_p_p.h new file mode 100644 index 000000000..962a04cb2 --- /dev/null +++ b/src/webengine/api/qquickwebenginescript_p_p.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKWEBENGINESCRIPT_P_P_H +#define QQUICKWEBENGINESCRIPT_P_P_H + +#include "qquickwebenginescript_p.h" + +#include +#include "user_script.h" +#include "web_contents_adapter.h" + + +QT_BEGIN_NAMESPACE + +class QQuickWebEngineScriptPrivate { +public: + Q_DECLARE_PUBLIC(QQuickWebEngineScript) + QQuickWebEngineScriptPrivate(); + void aboutToUpdateUnderlyingScript(); + void bind(UserScriptControllerHost *, WebContentsAdapter * = 0); + + UserScript coreScript; + QBasicTimer m_basicTimer; + UserScriptControllerHost *m_controllerHost; + WebContentsAdapter *m_adapter; + +private: + QQuickWebEngineScript *q_ptr; + +}; + +QT_END_NAMESPACE + +#endif // QQUICKWEBENGINESCRIPT_P_P_H diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp index acae4a11b..13eb63e07 100644 --- a/src/webengine/api/qquickwebengineview.cpp +++ b/src/webengine/api/qquickwebengineview.cpp @@ -48,9 +48,11 @@ #include "qquickwebengineprofile_p.h" #include "qquickwebengineprofile_p_p.h" #include "qquickwebenginesettings_p.h" +#include "qquickwebenginescript_p_p.h" #include "render_widget_host_view_qt_delegate_quick.h" #include "render_widget_host_view_qt_delegate_quickwindow.h" #include "ui_delegates_manager.h" +#include "user_script_controller_host.h" #include "web_contents_adapter.h" #include "web_engine_error.h" #include "web_engine_settings.h" @@ -567,6 +569,9 @@ void QQuickWebEngineViewPrivate::ensureContentsAdapter() adapter->initialize(this); if (explicitUrl.isValid()) adapter->load(explicitUrl); + // push down the page's user scripts + Q_FOREACH (QQuickWebEngineScript *script, m_userScripts) + script->d_func()->bind(browserContextAdapter()->userScriptController(), adapter.data()); } } @@ -668,6 +673,16 @@ QQuickWebEngineSettings *QQuickWebEngineView::settings() const return d->m_settings.data(); } +QQmlListProperty QQuickWebEngineView::userScripts() +{ + Q_D(QQuickWebEngineView); + return QQmlListProperty(this, d, + d->userScripts_append, + d->userScripts_count, + d->userScripts_at, + d->userScripts_clear); +} + void QQuickWebEngineViewPrivate::setProfile(QQuickWebEngineProfile *profile) { if (profile == m_profile) @@ -911,6 +926,41 @@ void QQuickWebEngineView::itemChange(ItemChange change, const ItemChangeData &va QQuickItem::itemChange(change, value); } +void QQuickWebEngineViewPrivate::userScripts_append(QQmlListProperty *p, QQuickWebEngineScript *script) +{ + Q_ASSERT(p && p->data); + QQuickWebEngineViewPrivate *d = static_cast(p->data); + UserScriptControllerHost *scriptController = d->browserContextAdapter()->userScriptController(); + d->m_userScripts.append(script); + // If the adapter hasn't been instantiated, we'll bind the scripts in ensureContentsAdapter() + if (!d->adapter) + return; + script->d_func()->bind(scriptController, d->adapter.data()); +} + +int QQuickWebEngineViewPrivate::userScripts_count(QQmlListProperty *p) +{ + Q_ASSERT(p && p->data); + QQuickWebEngineViewPrivate *d = static_cast(p->data); + return d->m_userScripts.count(); +} + +QQuickWebEngineScript *QQuickWebEngineViewPrivate::userScripts_at(QQmlListProperty *p, int idx) +{ + Q_ASSERT(p && p->data); + QQuickWebEngineViewPrivate *d = static_cast(p->data); + return d->m_userScripts.at(idx); +} + +void QQuickWebEngineViewPrivate::userScripts_clear(QQmlListProperty *p) +{ + Q_ASSERT(p && p->data); + QQuickWebEngineViewPrivate *d = static_cast(p->data); + UserScriptControllerHost *scriptController = d->browserContextAdapter()->userScriptController(); + scriptController->clearAllScripts(d->adapter.data()); + d->m_userScripts.clear(); +} + void QQuickWebEngineView::componentComplete() { Q_D(QQuickWebEngineView); diff --git a/src/webengine/api/qquickwebengineview_p.h b/src/webengine/api/qquickwebengineview_p.h index a9f387e87..93e9ad61b 100644 --- a/src/webengine/api/qquickwebengineview_p.h +++ b/src/webengine/api/qquickwebengineview_p.h @@ -38,6 +38,7 @@ #define QQUICKWEBENGINEVIEW_P_H #include +#include "qquickwebenginescript_p.h" #include QT_BEGIN_NAMESPACE @@ -67,6 +68,7 @@ class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineView : public QQuickItem { Q_PROPERTY(QQuickWebEngineSettings *settings READ settings REVISION 1) Q_PROPERTY(QQuickWebEngineHistory *navigationHistory READ navigationHistory CONSTANT FINAL REVISION 1) Q_PROPERTY(QQmlWebChannel *webChannel READ webChannel WRITE setWebChannel NOTIFY webChannelChanged REVISION 1) + Q_PROPERTY(QQmlListProperty userScripts READ userScripts FINAL) Q_ENUMS(NavigationRequestAction); Q_ENUMS(NavigationType); Q_ENUMS(LoadStatus); @@ -145,6 +147,7 @@ public: QQuickWebEngineProfile *profile() const; void setProfile(QQuickWebEngineProfile *); + QQmlListProperty userScripts(); QQuickWebEngineSettings *settings() const; QQmlWebChannel *webChannel(); diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index a3b29af19..faf567d3a 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -180,6 +180,12 @@ public: void setProfile(QQuickWebEngineProfile *profile); void ensureContentsAdapter(); + // QQmlListPropertyHelpers + static void userScripts_append(QQmlListProperty *p, QQuickWebEngineScript *script); + static int userScripts_count(QQmlListProperty *p); + static QQuickWebEngineScript *userScripts_at(QQmlListProperty *p, int idx); + static void userScripts_clear(QQmlListProperty *p); + QExplicitlySharedDataPointer adapter; QScopedPointer e; QScopedPointer v; @@ -198,6 +204,7 @@ public: private: QScopedPointer m_uIDelegatesManager; + QList m_userScripts; qreal m_dpiScale; }; diff --git a/src/webengine/plugin/plugin.cpp b/src/webengine/plugin/plugin.cpp index b752315f7..dc10a50a7 100644 --- a/src/webengine/plugin/plugin.cpp +++ b/src/webengine/plugin/plugin.cpp @@ -70,6 +70,7 @@ public: qmlRegisterType(uri, 1, 1, "WebEngineView"); qmlRegisterType(uri, 1, 1, "WebEngineProfile"); + qmlRegisterType(uri, 1, 1, "WebEngineScript"); qmlRegisterUncreatableType(uri, 1, 1, "WebEngineCertificateError", QObject::tr("Cannot create separate instance of WebEngineCertificateError")); qmlRegisterUncreatableType(uri, 1, 1, "WebEngineDownloadItem", QObject::tr("Cannot create a separate instance of WebEngineDownloadItem")); diff --git a/src/webengine/webengine.pro b/src/webengine/webengine.pro index 8a6d06c10..154286e9e 100644 --- a/src/webengine/webengine.pro +++ b/src/webengine/webengine.pro @@ -18,6 +18,7 @@ SOURCES = \ api/qquickwebenginenavigationrequest.cpp \ api/qquickwebenginenewviewrequest.cpp \ api/qquickwebengineprofile.cpp \ + api/qquickwebenginescript.cpp \ api/qquickwebenginesettings.cpp \ api/qquickwebenginesingleton.cpp \ api/qquickwebengineview.cpp \ @@ -38,6 +39,7 @@ HEADERS = \ api/qquickwebenginenewviewrequest_p.h \ api/qquickwebengineprofile_p.h \ api/qquickwebengineprofile_p_p.h \ + api/qquickwebenginescript_p.h \ api/qquickwebenginesettings_p.h \ api/qquickwebenginesingleton_p.h \ api/qquickwebengineview_p.h \ -- cgit v1.2.3