From 48d2066d6c9b38f29368e5205a5b7f49ab3fa857 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Thu, 2 Feb 2023 08:43:05 +0100 Subject: Fix project structure and auto registry Create QtWebChannelQuick and move the required sources, so auto registry works and qml types files and friends are auto generated. This makes things inline what other modules do. This change has a side effect as qqmlwebchannel.h will change its module, however this is semi-pulibc header, hopefully only used by QtWebEngine. [ChangeLog] Created new QtWebChannelQuick which holds qml sources. Task-number: QTBUG-110795 Fixes: QTBUG-99959 Change-Id: Ic8aa72f070d6a9e23d918a1210f4df62df739397 Reviewed-by: Ulf Hermann --- src/CMakeLists.txt | 2 +- src/imports/CMakeLists.txt | 6 - src/imports/webchannel/CMakeLists.txt | 32 ---- src/imports/webchannel/dependencies.json | 2 - src/imports/webchannel/plugin.cpp | 35 ---- src/imports/webchannel/plugins.qmltypes | 67 ------- src/imports/webchannel/qmldir | 4 - src/webchannel/CMakeLists.txt | 48 +---- src/webchannel/doc/qtwebchannel.qdocconf | 4 +- src/webchannel/qqmlwebchannel.cpp | 247 ------------------------ src/webchannel/qqmlwebchannel.h | 59 ------ src/webchannel/qqmlwebchannelattached.cpp | 39 ---- src/webchannel/qqmlwebchannelattached_p.h | 46 ----- src/webchannel/qwebchannel_p.h | 2 +- src/webchannel/signalhandler_p.h | 1 - src/webchannelquick/CMakeLists.txt | 15 ++ src/webchannelquick/qqmlwebchannel.cpp | 248 +++++++++++++++++++++++++ src/webchannelquick/qqmlwebchannel.h | 62 +++++++ src/webchannelquick/qqmlwebchannelattached.cpp | 34 ++++ src/webchannelquick/qqmlwebchannelattached_p.h | 47 +++++ src/webchannelquick/qwebchannelquickglobal.h | 10 + tests/auto/qml/CMakeLists.txt | 14 +- tests/auto/qml/testwebchannel.cpp | 4 +- tests/auto/qml/testwebchannel.h | 2 +- tests/auto/webchannel/CMakeLists.txt | 11 -- tests/auto/webchannel/tst_webchannel.cpp | 6 +- 26 files changed, 438 insertions(+), 609 deletions(-) delete mode 100644 src/imports/CMakeLists.txt delete mode 100644 src/imports/webchannel/CMakeLists.txt delete mode 100644 src/imports/webchannel/dependencies.json delete mode 100644 src/imports/webchannel/plugin.cpp delete mode 100644 src/imports/webchannel/plugins.qmltypes delete mode 100644 src/imports/webchannel/qmldir delete mode 100644 src/webchannel/qqmlwebchannel.cpp delete mode 100644 src/webchannel/qqmlwebchannel.h delete mode 100644 src/webchannel/qqmlwebchannelattached.cpp delete mode 100644 src/webchannel/qqmlwebchannelattached_p.h create mode 100644 src/webchannelquick/CMakeLists.txt create mode 100644 src/webchannelquick/qqmlwebchannel.cpp create mode 100644 src/webchannelquick/qqmlwebchannel.h create mode 100644 src/webchannelquick/qqmlwebchannelattached.cpp create mode 100644 src/webchannelquick/qqmlwebchannelattached_p.h create mode 100644 src/webchannelquick/qwebchannelquickglobal.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa1d28d..8eee8c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,5 +5,5 @@ add_subdirectory(webchannel) if(TARGET Qt::Quick) - add_subdirectory(imports) + add_subdirectory(webchannelquick) endif() diff --git a/src/imports/CMakeLists.txt b/src/imports/CMakeLists.txt deleted file mode 100644 index e3c2e04..0000000 --- a/src/imports/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -# Generated from imports.pro. - -add_subdirectory(webchannel) diff --git a/src/imports/webchannel/CMakeLists.txt b/src/imports/webchannel/CMakeLists.txt deleted file mode 100644 index 52405c8..0000000 --- a/src/imports/webchannel/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -# Generated from webchannel.pro. - -##################################################################### -## webchannel Plugin: -##################################################################### - -qt_internal_add_qml_module(webchannel - URI "QtWebChannel" - VERSION "${PROJECT_VERSION}" - PAST_MAJOR_VERSIONS 1 - CLASS_NAME QWebChannelPlugin - PLUGIN_TARGET webchannel - NO_PLUGIN_OPTIONAL - NO_GENERATE_PLUGIN_SOURCE - NO_GENERATE_QMLTYPES - INSTALL_SOURCE_QMLTYPES "plugins.qmltypes" - SOURCES - plugin.cpp - INCLUDE_DIRECTORIES - ../../webchannel - LIBRARIES - Qt::Core - Qt::Quick - Qt::WebChannelPrivate -) - -#### Keys ignored in scope 1:.:.:webchannel.pro:: -# IMPORT_VERSION = "1.$$QT_MINOR_VERSION" -# TARGETPATH = "QtWebChannel" diff --git a/src/imports/webchannel/dependencies.json b/src/imports/webchannel/dependencies.json deleted file mode 100644 index 0d4f101..0000000 --- a/src/imports/webchannel/dependencies.json +++ /dev/null @@ -1,2 +0,0 @@ -[ -] diff --git a/src/imports/webchannel/plugin.cpp b/src/imports/webchannel/plugin.cpp deleted file mode 100644 index 2914bb4..0000000 --- a/src/imports/webchannel/plugin.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#include -#include - -#include -#include - -QT_BEGIN_NAMESPACE - -class QWebChannelPlugin : public QQmlExtensionPlugin -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid) - -public: - QWebChannelPlugin(QObject *parent = 0) : QQmlExtensionPlugin(parent) { } - void registerTypes(const char *uri) override; -}; - -void QWebChannelPlugin::registerTypes(const char *uri) -{ - int major = 1; - int minor = 0; - qmlRegisterType(uri, major, minor, "WebChannel"); - - // The minor version used to be the current Qt 5 minor. For compatibility it is the last - // Qt 5 release. - qmlRegisterModule(uri, major, 15); -} - -QT_END_NAMESPACE - -#include "plugin.moc" diff --git a/src/imports/webchannel/plugins.qmltypes b/src/imports/webchannel/plugins.qmltypes deleted file mode 100644 index 68378d6..0000000 --- a/src/imports/webchannel/plugins.qmltypes +++ /dev/null @@ -1,67 +0,0 @@ -import QtQuick.tooling 1.2 - -// This file describes the plugin-supplied types contained in the library. -// It is used for QML tooling purposes only. -// -// This file was auto-generated by: -// 'qmlplugindump -nonrelocatable -dependencies dependencies.json QtWebChannel 1.15' - -Module { - dependencies: [] - Component { - name: "QQmlWebChannel" - prototype: "QWebChannel" - exports: ["QtWebChannel/WebChannel 1.0"] - exportMetaObjectRevisions: [0] - attachedType: "QQmlWebChannelAttached" - Property { name: "transports"; type: "QObject"; isList: true; isReadonly: true } - Property { name: "registeredObjects"; type: "QObject"; isList: true; isReadonly: true } - Method { - name: "registerObjects" - Parameter { name: "objects"; type: "QVariantMap" } - } - Method { - name: "connectTo" - Parameter { name: "transport"; type: "QObject"; isPointer: true } - } - Method { - name: "disconnectFrom" - Parameter { name: "transport"; type: "QObject"; isPointer: true } - } - } - Component { - name: "QQmlWebChannelAttached" - prototype: "QObject" - Property { name: "id"; type: "string" } - Signal { - name: "idChanged" - Parameter { name: "id"; type: "string" } - } - } - Component { - name: "QWebChannel" - prototype: "QObject" - Property { name: "blockUpdates"; type: "bool" } - Signal { - name: "blockUpdatesChanged" - Parameter { name: "block"; type: "bool" } - } - Method { - name: "connectTo" - Parameter { name: "transport"; type: "QWebChannelAbstractTransport"; isPointer: true } - } - Method { - name: "disconnectFrom" - Parameter { name: "transport"; type: "QWebChannelAbstractTransport"; isPointer: true } - } - Method { - name: "registerObject" - Parameter { name: "id"; type: "string" } - Parameter { name: "object"; type: "QObject"; isPointer: true } - } - Method { - name: "deregisterObject" - Parameter { name: "object"; type: "QObject"; isPointer: true } - } - } -} diff --git a/src/imports/webchannel/qmldir b/src/imports/webchannel/qmldir deleted file mode 100644 index c521f2f..0000000 --- a/src/imports/webchannel/qmldir +++ /dev/null @@ -1,4 +0,0 @@ -module QtWebChannel -classname QWebChannelPlugin -plugin declarative_webchannel -typeinfo plugins.qmltypes diff --git a/src/webchannel/CMakeLists.txt b/src/webchannel/CMakeLists.txt index 9354973..fd5c08b 100644 --- a/src/webchannel/CMakeLists.txt +++ b/src/webchannel/CMakeLists.txt @@ -1,13 +1,8 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from webchannel.pro. - -##################################################################### -## WebChannel Module: -##################################################################### - qt_internal_add_module(WebChannel + GENERATE_CPP_EXPORTS SOURCES qmetaobjectpublisher.cpp qmetaobjectpublisher_p.h qwebchannel.cpp qwebchannel.h qwebchannel_p.h @@ -16,49 +11,26 @@ qt_internal_add_module(WebChannel qwebchannelglobal.h LIBRARIES Qt::CorePrivate - PUBLIC_LIBRARIES - Qt::Core - PRIVATE_MODULE_INTERFACE - Qt::CorePrivate - GENERATE_CPP_EXPORTS ) -# Resources: -set_source_files_properties("../../examples/webchannel/shared/qwebchannel.js" - PROPERTIES QT_RESOURCE_ALIAS "qwebchannel.js" -) -set(resources_resource_files - "../../examples/webchannel/shared/qwebchannel.js" +set(resource_file "../../examples/webchannel/shared/qwebchannel.js") +set_source_files_properties(${resource_file} PROPERTIES + QT_RESOURCE_ALIAS "qwebchannel.js" ) -set_source_files_properties(${resources_resource_files} PROPERTIES QT_SKIP_QUICKCOMPILER 1) - qt_internal_add_resource(WebChannel "resources" PREFIX "/qtwebchannel/" FILES - ${resources_resource_files} + ${resource_file} ) +if(TARGET Qt::Qml) + qt_internal_extend_target(WebChannel PUBLIC_LIBRARIES Qt::Qml) +else() + qt_internal_extend_target(WebChannel DEFINES QT_NO_JSVALUE) +endif() -#### Keys ignored in scope 1:.:.:webchannel.pro:: -# OTHER_FILES = "qwebchannel.js" - -## Scopes: -##################################################################### - -qt_internal_extend_target(WebChannel CONDITION TARGET Qt::Qml - SOURCES - qqmlwebchannel.cpp qqmlwebchannel.h - qqmlwebchannelattached.cpp qqmlwebchannelattached_p.h - PUBLIC_LIBRARIES - Qt::Qml -) - -qt_internal_extend_target(WebChannel CONDITION NOT TARGET Qt::Qml - DEFINES - QT_NO_JSVALUE -) qt_internal_add_docs(WebChannel doc/qtwebchannel.qdocconf ) diff --git a/src/webchannel/doc/qtwebchannel.qdocconf b/src/webchannel/doc/qtwebchannel.qdocconf index 766e73f..b77d5f3 100644 --- a/src/webchannel/doc/qtwebchannel.qdocconf +++ b/src/webchannel/doc/qtwebchannel.qdocconf @@ -40,10 +40,10 @@ tagfile = ../../../doc/qtwebchannel/ depends += qtcore qtquick qtqml qtdoc qtwebengine qtwebsockets qmake qtcmake headerdirs += .. \ - ../../imports + ../../webchannelquick sourcedirs += .. \ - ../../imports + ../../webchannelquick imagedirs += images diff --git a/src/webchannel/qqmlwebchannel.cpp b/src/webchannel/qqmlwebchannel.cpp deleted file mode 100644 index c8f8bd8..0000000 --- a/src/webchannel/qqmlwebchannel.cpp +++ /dev/null @@ -1,247 +0,0 @@ -// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#include "qqmlwebchannel.h" - -#include "qwebchannel_p.h" -#include "qmetaobjectpublisher_p.h" -#include "qwebchannelabstracttransport.h" - -#include - -#include "qqmlwebchannelattached_p.h" - -QT_BEGIN_NAMESPACE - -/*! - \qmltype WebChannel - \instantiates QQmlWebChannel - - \inqmlmodule QtWebChannel - \ingroup webchannel-qml - \brief QML interface to QWebChannel. - \since 5.4 - - The WebChannel provides a mechanism to transparently access QObject or QML objects from HTML - clients. All properties, signals and public slots can be used from the HTML clients. - - \sa QWebChannel, {Qt WebChannel JavaScript API}{JavaScript API} -*/ - -/*! - \qmlproperty QQmlListProperty WebChannel::transports - A list of transport objects, which implement QWebChannelAbstractTransport. The transports - are used to talk to the remote clients. - - \sa connectTo(), disconnectFrom() -*/ - -/*! - \qmlproperty QQmlListProperty WebChannel::registeredObjects - - \brief A list of objects which should be accessible to remote clients. - - The objects must have the attached \l id property set to an identifier, under which the - object is then known on the HTML side. - - Once registered, all signals and property changes are automatically propagated to the clients. - Public invokable methods, including slots, are also accessible to the clients. - - If one needs to register objects which are not available when the component is created, use the - imperative registerObjects method. - - \sa registerObjects(), id -*/ - -class QQmlWebChannelPrivate : public QWebChannelPrivate -{ - Q_DECLARE_PUBLIC(QQmlWebChannel) -public: - QList registeredObjects; - - void _q_objectIdChanged(const QString &newId); -}; - -/*! - \internal - - Update the name of the sender object, when its attached WebChannel.id property changed. - This is required, since during startup the property is empty and only gets set later on. -*/ -void QQmlWebChannelPrivate::_q_objectIdChanged(const QString &newId) -{ - Q_Q(QQmlWebChannel); - const QQmlWebChannelAttached *const attached = qobject_cast(q->sender()); - Q_ASSERT(attached); - Q_ASSERT(attached->parent()); - Q_ASSERT(registeredObjects.contains(attached->parent())); - - QObject *const object = attached->parent(); - const QString &oldId = publisher->registeredObjectIds.value(object); - - if (!oldId.isEmpty()) { - q->deregisterObject(object); - } - - q->registerObject(newId, object); -} - -QQmlWebChannel::QQmlWebChannel(QObject *parent) - : QWebChannel(*(new QQmlWebChannelPrivate), parent) -{ -} - -QQmlWebChannel::~QQmlWebChannel() -{ - -} - -/*! - \qmlmethod void WebChannel::registerObjects(QVariantMap objects) - Registers the specified \a objects to make them accessible to HTML clients. - The key of the map is used as an identifier for the object on the client side. - - Once registered, all signals and property changes are automatically propagated to the clients. - Public invokable methods, including slots, are also accessible to the clients. - - This imperative API can be used to register objects on the fly. For static objects, the declarative - registeredObjects property should be preferred. - - \sa registeredObjects -*/ -void QQmlWebChannel::registerObjects(const QVariantMap &objects) -{ - Q_D(QQmlWebChannel); - QMap::const_iterator it = objects.constBegin(); - for (; it != objects.constEnd(); ++it) { - QObject *object = it.value().value(); - if (!object) { - qWarning("Invalid QObject given to register under name %s", qPrintable(it.key())); - continue; - } - d->publisher->registerObject(it.key(), object); - } -} - -QQmlWebChannelAttached *QQmlWebChannel::qmlAttachedProperties(QObject *obj) -{ - return new QQmlWebChannelAttached(obj); -} - -/*! - \qmlmethod void WebChannel::connectTo(QWebChannelAbstractTransport transport) - - \brief Connects to the \a transport, which represents a communication - channel to a single client. - - The transport object must be an implementation of QWebChannelAbstractTransport. - - \sa transports, disconnectFrom() -*/ -void QQmlWebChannel::connectTo(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { - QWebChannel::connectTo(realTransport); - } else { - qWarning() << "Cannot connect to transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -/*! - \qmlmethod void WebChannel::disconnectFrom(QWebChannelAbstractTransport transport) - - \brief Disconnects the \a transport from this WebChannel. - - The client will not be able to communicate with the WebChannel anymore, nor will it receive any - signals or property updates. - - \sa connectTo() -*/ -void QQmlWebChannel::disconnectFrom(QObject *transport) -{ - if (QWebChannelAbstractTransport *realTransport = qobject_cast(transport)) { - QWebChannel::disconnectFrom(realTransport); - } else { - qWarning() << "Cannot disconnect from transport" << transport << " - it is not a QWebChannelAbstractTransport."; - } -} - -QQmlListProperty QQmlWebChannel::registeredObjects() -{ - return QQmlListProperty(this, nullptr, registeredObjects_append, - registeredObjects_count, registeredObjects_at, - registeredObjects_clear); -} - -void QQmlWebChannel::registeredObjects_append(QQmlListProperty *prop, QObject *object) -{ - const QQmlWebChannelAttached *const attached = qobject_cast( - qmlAttachedPropertiesObject(object, false /* don't create */)); - if (!attached) { - const QQmlContext *const context = qmlContext(object); - qWarning() << "Cannot register object" << context->nameForObject(object) << '(' << object << ") without attached WebChannel.id property. Did you forget to set it?"; - return; - } - QQmlWebChannel *channel = static_cast(prop->object); - if (!attached->id().isEmpty()) { - // TODO: warning in such cases? - channel->registerObject(attached->id(), object); - } - channel->d_func()->registeredObjects.append(object); - connect(attached, SIGNAL(idChanged(QString)), channel, SLOT(_q_objectIdChanged(QString))); -} - -qsizetype QQmlWebChannel::registeredObjects_count(QQmlListProperty *prop) -{ - return static_cast(prop->object)->d_func()->registeredObjects.size(); -} - -QObject *QQmlWebChannel::registeredObjects_at(QQmlListProperty *prop, qsizetype index) -{ - return static_cast(prop->object)->d_func()->registeredObjects.at(index); -} - -void QQmlWebChannel::registeredObjects_clear(QQmlListProperty *prop) -{ - QQmlWebChannel *channel = static_cast(prop->object); - foreach (QObject *object, channel->d_func()->registeredObjects) { - channel->deregisterObject(object); - } - return channel->d_func()->registeredObjects.clear(); -} - -QQmlListProperty QQmlWebChannel::transports() -{ - return QQmlListProperty(this, nullptr, transports_append, transports_count, - transports_at, transports_clear); -} - -void QQmlWebChannel::transports_append(QQmlListProperty *prop, QObject *transport) -{ - QQmlWebChannel *channel = static_cast(prop->object); - channel->connectTo(transport); -} - -qsizetype QQmlWebChannel::transports_count(QQmlListProperty *prop) -{ - return static_cast(prop->object)->d_func()->transports.size(); -} - -QObject *QQmlWebChannel::transports_at(QQmlListProperty *prop, qsizetype index) -{ - QQmlWebChannel *channel = static_cast(prop->object); - return channel->d_func()->transports.at(index); -} - -void QQmlWebChannel::transports_clear(QQmlListProperty *prop) -{ - QWebChannel *channel = static_cast(prop->object); - foreach (QWebChannelAbstractTransport *transport, channel->d_func()->transports) { - channel->disconnectFrom(transport); - } - Q_ASSERT(channel->d_func()->transports.isEmpty()); -} - -QT_END_NAMESPACE - -#include "moc_qqmlwebchannel.cpp" diff --git a/src/webchannel/qqmlwebchannel.h b/src/webchannel/qqmlwebchannel.h deleted file mode 100644 index f57a510..0000000 --- a/src/webchannel/qqmlwebchannel.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#ifndef QQMLWEBCHANNEL_H -#define QQMLWEBCHANNEL_H - -#include -#include - -#include -#include - -QT_BEGIN_NAMESPACE - -class QQmlWebChannelPrivate; -class QQmlWebChannelAttached; -class Q_WEBCHANNEL_EXPORT QQmlWebChannel : public QWebChannel -{ - Q_OBJECT - Q_DISABLE_COPY(QQmlWebChannel) - - Q_PROPERTY( QQmlListProperty transports READ transports ) - Q_PROPERTY( QQmlListProperty registeredObjects READ registeredObjects ) - -public: - explicit QQmlWebChannel(QObject *parent = nullptr); - virtual ~QQmlWebChannel(); - - Q_INVOKABLE void registerObjects(const QVariantMap &objects); - QQmlListProperty registeredObjects(); - - QQmlListProperty transports(); - - static QQmlWebChannelAttached *qmlAttachedProperties(QObject *obj); - - Q_INVOKABLE void connectTo(QObject *transport); - Q_INVOKABLE void disconnectFrom(QObject *transport); - -private: - Q_DECLARE_PRIVATE(QQmlWebChannel) - Q_PRIVATE_SLOT(d_func(), void _q_objectIdChanged(const QString &newId)) - - static void registeredObjects_append(QQmlListProperty *prop, QObject *item); - static qsizetype registeredObjects_count(QQmlListProperty *prop); - static QObject *registeredObjects_at(QQmlListProperty *prop, qsizetype index); - static void registeredObjects_clear(QQmlListProperty *prop); - - static void transports_append(QQmlListProperty *prop, QObject *item); - static qsizetype transports_count(QQmlListProperty *prop); - static QObject *transports_at(QQmlListProperty *prop, qsizetype index); - static void transports_clear(QQmlListProperty *prop); -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE( QQmlWebChannel ) -QML_DECLARE_TYPEINFO( QQmlWebChannel, QML_HAS_ATTACHED_PROPERTIES ) - -#endif // QQMLWEBCHANNEL_H diff --git a/src/webchannel/qqmlwebchannelattached.cpp b/src/webchannel/qqmlwebchannelattached.cpp deleted file mode 100644 index 5019590..0000000 --- a/src/webchannel/qqmlwebchannelattached.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#include "qqmlwebchannelattached_p.h" - -QT_USE_NAMESPACE - -QQmlWebChannelAttached::QQmlWebChannelAttached(QObject *parent) - : QObject(parent) -{ - -} - -QQmlWebChannelAttached::~QQmlWebChannelAttached() -{ - -} - -/*! - \qmlattachedproperty QString WebChannel::id - - \brief The identifier under which an object, registered to a WebChannel, is known to remote clients. - - This property must be set for every object that should be published over the WebChannel. - While no restrictions are enforced on the format of the id, it is usually a good idea to - choose a string that is also a valid JavaScript identifier. -*/ -QString QQmlWebChannelAttached::id() const -{ - return m_id; -} - -void QQmlWebChannelAttached::setId(const QString &id) -{ - if (id != m_id) { - m_id = id; - emit idChanged(id); - } -} diff --git a/src/webchannel/qqmlwebchannelattached_p.h b/src/webchannel/qqmlwebchannelattached_p.h deleted file mode 100644 index c4c70fd..0000000 --- a/src/webchannel/qqmlwebchannelattached_p.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only - -#ifndef QQMLWEBCHANNELATTACHED_H -#define QQMLWEBCHANNELATTACHED_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include - -#include "qwebchannelglobal.h" -#include "private/qglobal_p.h" - -QT_BEGIN_NAMESPACE - -class Q_WEBCHANNEL_EXPORT QQmlWebChannelAttached : public QObject -{ - Q_OBJECT - - Q_PROPERTY( QString id READ id WRITE setId NOTIFY idChanged FINAL ) -public: - explicit QQmlWebChannelAttached(QObject *parent = 0); - virtual ~QQmlWebChannelAttached(); - - QString id() const; - void setId(const QString &id); - -Q_SIGNALS: - void idChanged(const QString &id); - -private: - QString m_id; -}; - -QT_END_NAMESPACE - -#endif // QQMLWEBCHANNELATTACHED_H diff --git a/src/webchannel/qwebchannel_p.h b/src/webchannel/qwebchannel_p.h index 7aad0ab..71cf4a7 100644 --- a/src/webchannel/qwebchannel_p.h +++ b/src/webchannel/qwebchannel_p.h @@ -17,7 +17,7 @@ #include "qwebchannelglobal.h" -#include +#include #include QT_BEGIN_NAMESPACE diff --git a/src/webchannel/signalhandler_p.h b/src/webchannel/signalhandler_p.h index 3431f1b..022762a 100644 --- a/src/webchannel/signalhandler_p.h +++ b/src/webchannel/signalhandler_p.h @@ -21,7 +21,6 @@ #include #include #include -#include QT_BEGIN_NAMESPACE diff --git a/src/webchannelquick/CMakeLists.txt b/src/webchannelquick/CMakeLists.txt new file mode 100644 index 0000000..0c88145 --- /dev/null +++ b/src/webchannelquick/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +qt_internal_add_qml_module(WebChannelQuick + URI "QtWebChannel" + VERSION "${PROJECT_VERSION}" + PAST_MAJOR_VERSIONS 1 + GENERATE_CPP_EXPORTS + SOURCES + qqmlwebchannel.h qqmlwebchannel.cpp + qqmlwebchannelattached_p.h qqmlwebchannelattached.cpp + qwebchannelquickglobal.h + LIBRARIES + Qt::WebChannelPrivate +) diff --git a/src/webchannelquick/qqmlwebchannel.cpp b/src/webchannelquick/qqmlwebchannel.cpp new file mode 100644 index 0000000..b0d803d --- /dev/null +++ b/src/webchannelquick/qqmlwebchannel.cpp @@ -0,0 +1,248 @@ +// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author +// Milian Wolff +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qqmlwebchannel.h" +#include +#include +#include +#include + +#include "qqmlwebchannelattached_p.h" + +QT_BEGIN_NAMESPACE + +/*! + \qmltype WebChannel + \instantiates QQmlWebChannel + + \inqmlmodule QtWebChannel + \ingroup webchannel-qml + \brief QML interface to QWebChannel. + \since 5.4 + + The WebChannel provides a mechanism to transparently access QObject or QML objects from HTML + clients. All properties, signals and public slots can be used from the HTML clients. + + \sa QWebChannel, {Qt WebChannel JavaScript API}{JavaScript API} +*/ + +/*! + \qmlproperty QQmlListProperty WebChannel::transports + A list of transport objects, which implement QWebChannelAbstractTransport. The transports + are used to talk to the remote clients. + + \sa connectTo(), disconnectFrom() +*/ + +/*! + \qmlproperty QQmlListProperty WebChannel::registeredObjects + + \brief A list of objects which should be accessible to remote clients. + + The objects must have the attached \l id property set to an identifier, under which the + object is then known on the HTML side. + + Once registered, all signals and property changes are automatically propagated to the clients. + Public invokable methods, including slots, are also accessible to the clients. + + If one needs to register objects which are not available when the component is created, use the + imperative registerObjects method. + + \sa registerObjects(), id +*/ + +class QQmlWebChannelPrivate : public QWebChannelPrivate +{ + Q_DECLARE_PUBLIC(QQmlWebChannel) +public: + QList registeredObjects; + + void _q_objectIdChanged(const QString &newId); +}; + +/*! + \internal + + Update the name of the sender object, when its attached WebChannel.id property changed. + This is required, since during startup the property is empty and only gets set later on. +*/ +void QQmlWebChannelPrivate::_q_objectIdChanged(const QString &newId) +{ + Q_Q(QQmlWebChannel); + const QQmlWebChannelAttached *const attached = + qobject_cast(q->sender()); + Q_ASSERT(attached); + Q_ASSERT(attached->parent()); + Q_ASSERT(registeredObjects.contains(attached->parent())); + + QObject *const object = attached->parent(); + const QString &oldId = publisher->registeredObjectIds.value(object); + + if (!oldId.isEmpty()) { + q->deregisterObject(object); + } + + q->registerObject(newId, object); +} + +QQmlWebChannel::QQmlWebChannel(QObject *parent) : QWebChannel(*(new QQmlWebChannelPrivate), parent) +{ +} + +QQmlWebChannel::~QQmlWebChannel() { } + +/*! + \qmlmethod void WebChannel::registerObjects(QVariantMap objects) + Registers the specified \a objects to make them accessible to HTML clients. + The key of the map is used as an identifier for the object on the client side. + + Once registered, all signals and property changes are automatically propagated to the clients. + Public invokable methods, including slots, are also accessible to the clients. + + This imperative API can be used to register objects on the fly. For static objects, the + declarative registeredObjects property should be preferred. + + \sa registeredObjects +*/ +void QQmlWebChannel::registerObjects(const QVariantMap &objects) +{ + Q_D(QQmlWebChannel); + QMap::const_iterator it = objects.constBegin(); + for (; it != objects.constEnd(); ++it) { + QObject *object = it.value().value(); + if (!object) { + qWarning("Invalid QObject given to register under name %s", qPrintable(it.key())); + continue; + } + d->publisher->registerObject(it.key(), object); + } +} + +QQmlWebChannelAttached *QQmlWebChannel::qmlAttachedProperties(QObject *obj) +{ + return new QQmlWebChannelAttached(obj); +} + +/*! + \qmlmethod void WebChannel::connectTo(QWebChannelAbstractTransport transport) + + \brief Connects to the \a transport, which represents a communication + channel to a single client. + + The transport object must be an implementation of QWebChannelAbstractTransport. + + \sa transports, disconnectFrom() +*/ +void QQmlWebChannel::connectTo(QObject *transport) +{ + if (QWebChannelAbstractTransport *realTransport = + qobject_cast(transport)) { + QWebChannel::connectTo(realTransport); + } else { + qWarning() << "Cannot connect to transport" << transport + << " - it is not a QWebChannelAbstractTransport."; + } +} + +/*! + \qmlmethod void WebChannel::disconnectFrom(QWebChannelAbstractTransport transport) + + \brief Disconnects the \a transport from this WebChannel. + + The client will not be able to communicate with the WebChannel anymore, nor will it receive any + signals or property updates. + + \sa connectTo() +*/ +void QQmlWebChannel::disconnectFrom(QObject *transport) +{ + if (QWebChannelAbstractTransport *realTransport = + qobject_cast(transport)) { + QWebChannel::disconnectFrom(realTransport); + } else { + qWarning() << "Cannot disconnect from transport" << transport + << " - it is not a QWebChannelAbstractTransport."; + } +} + +QQmlListProperty QQmlWebChannel::registeredObjects() +{ + return QQmlListProperty(this, nullptr, registeredObjects_append, + registeredObjects_count, registeredObjects_at, + registeredObjects_clear); +} + +void QQmlWebChannel::registeredObjects_append(QQmlListProperty *prop, QObject *object) +{ + const QQmlWebChannelAttached *const attached = qobject_cast( + qmlAttachedPropertiesObject(object, false /* don't create */)); + if (!attached) { + const QQmlContext *const context = qmlContext(object); + qWarning() << "Cannot register object" << context->nameForObject(object) << '(' << object + << ") without attached WebChannel.id property. Did you forget to set it?"; + return; + } + QQmlWebChannel *channel = static_cast(prop->object); + if (!attached->id().isEmpty()) { + // TODO: warning in such cases? + channel->registerObject(attached->id(), object); + } + channel->d_func()->registeredObjects.append(object); + connect(attached, SIGNAL(idChanged(QString)), channel, SLOT(_q_objectIdChanged(QString))); +} + +qsizetype QQmlWebChannel::registeredObjects_count(QQmlListProperty *prop) +{ + return static_cast(prop->object)->d_func()->registeredObjects.size(); +} + +QObject *QQmlWebChannel::registeredObjects_at(QQmlListProperty *prop, qsizetype index) +{ + return static_cast(prop->object)->d_func()->registeredObjects.at(index); +} + +void QQmlWebChannel::registeredObjects_clear(QQmlListProperty *prop) +{ + QQmlWebChannel *channel = static_cast(prop->object); + foreach (QObject *object, channel->d_func()->registeredObjects) { + channel->deregisterObject(object); + } + return channel->d_func()->registeredObjects.clear(); +} + +QQmlListProperty QQmlWebChannel::transports() +{ + return QQmlListProperty(this, nullptr, transports_append, transports_count, + transports_at, transports_clear); +} + +void QQmlWebChannel::transports_append(QQmlListProperty *prop, QObject *transport) +{ + QQmlWebChannel *channel = static_cast(prop->object); + channel->connectTo(transport); +} + +qsizetype QQmlWebChannel::transports_count(QQmlListProperty *prop) +{ + return static_cast(prop->object)->d_func()->transports.size(); +} + +QObject *QQmlWebChannel::transports_at(QQmlListProperty *prop, qsizetype index) +{ + QQmlWebChannel *channel = static_cast(prop->object); + return channel->d_func()->transports.at(index); +} + +void QQmlWebChannel::transports_clear(QQmlListProperty *prop) +{ + QWebChannel *channel = static_cast(prop->object); + foreach (QWebChannelAbstractTransport *transport, channel->d_func()->transports) { + channel->disconnectFrom(transport); + } + Q_ASSERT(channel->d_func()->transports.isEmpty()); +} + +QT_END_NAMESPACE + +#include "moc_qqmlwebchannel.cpp" diff --git a/src/webchannelquick/qqmlwebchannel.h b/src/webchannelquick/qqmlwebchannel.h new file mode 100644 index 0000000..292ea6c --- /dev/null +++ b/src/webchannelquick/qqmlwebchannel.h @@ -0,0 +1,62 @@ +// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author +// Milian Wolff +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQMLWEBCHANNEL_H +#define QQMLWEBCHANNEL_H + +#include +#include + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQmlWebChannelPrivate; +class QQmlWebChannelAttached; +class Q_WEBCHANNELQUICK_EXPORT QQmlWebChannel : public QWebChannel +{ + Q_OBJECT + Q_DISABLE_COPY(QQmlWebChannel) + + Q_PROPERTY(QQmlListProperty transports READ transports) + Q_PROPERTY(QQmlListProperty registeredObjects READ registeredObjects) + QML_NAMED_ELEMENT(WebChannel) + QML_ATTACHED(QQmlWebChannelAttached) + QML_ADDED_IN_VERSION(1, 0) +public: + explicit QQmlWebChannel(QObject *parent = nullptr); + virtual ~QQmlWebChannel(); + + Q_INVOKABLE void registerObjects(const QVariantMap &objects); + QQmlListProperty registeredObjects(); + + QQmlListProperty transports(); + + static QQmlWebChannelAttached *qmlAttachedProperties(QObject *obj); + + Q_INVOKABLE void connectTo(QObject *transport); + Q_INVOKABLE void disconnectFrom(QObject *transport); + +private: + Q_DECLARE_PRIVATE(QQmlWebChannel) + Q_PRIVATE_SLOT(d_func(), void _q_objectIdChanged(const QString &newId)) + + static void registeredObjects_append(QQmlListProperty *prop, QObject *item); + static qsizetype registeredObjects_count(QQmlListProperty *prop); + static QObject *registeredObjects_at(QQmlListProperty *prop, qsizetype index); + static void registeredObjects_clear(QQmlListProperty *prop); + + static void transports_append(QQmlListProperty *prop, QObject *item); + static qsizetype transports_count(QQmlListProperty *prop); + static QObject *transports_at(QQmlListProperty *prop, qsizetype index); + static void transports_clear(QQmlListProperty *prop); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QQmlWebChannel) +QML_DECLARE_TYPEINFO(QQmlWebChannel, QML_HAS_ATTACHED_PROPERTIES) + +#endif // QQMLWEBCHANNEL_H diff --git a/src/webchannelquick/qqmlwebchannelattached.cpp b/src/webchannelquick/qqmlwebchannelattached.cpp new file mode 100644 index 0000000..5bbd1be --- /dev/null +++ b/src/webchannelquick/qqmlwebchannelattached.cpp @@ -0,0 +1,34 @@ +// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author +// Milian Wolff +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qqmlwebchannelattached_p.h" + +QT_USE_NAMESPACE + +QQmlWebChannelAttached::QQmlWebChannelAttached(QObject *parent) : QObject(parent) { } + +QQmlWebChannelAttached::~QQmlWebChannelAttached() { } + +/*! + \qmlattachedproperty QString WebChannel::id + + \brief The identifier under which an object, registered to a WebChannel, is known to remote + clients. + + This property must be set for every object that should be published over the WebChannel. + While no restrictions are enforced on the format of the id, it is usually a good idea to + choose a string that is also a valid JavaScript identifier. +*/ +QString QQmlWebChannelAttached::id() const +{ + return m_id; +} + +void QQmlWebChannelAttached::setId(const QString &id) +{ + if (id != m_id) { + m_id = id; + emit idChanged(id); + } +} diff --git a/src/webchannelquick/qqmlwebchannelattached_p.h b/src/webchannelquick/qqmlwebchannelattached_p.h new file mode 100644 index 0000000..c3d715e --- /dev/null +++ b/src/webchannelquick/qqmlwebchannelattached_p.h @@ -0,0 +1,47 @@ +// Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author +// Milian Wolff +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQMLWEBCHANNELATTACHED_H +#define QQMLWEBCHANNELATTACHED_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Q_WEBCHANNELQUICK_EXPORT QQmlWebChannelAttached : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString id READ id WRITE setId NOTIFY idChanged FINAL) + QML_ANONYMOUS + QML_ADDED_IN_VERSION(1, 0) +public: + explicit QQmlWebChannelAttached(QObject *parent = 0); + virtual ~QQmlWebChannelAttached(); + + QString id() const; + void setId(const QString &id); + +Q_SIGNALS: + void idChanged(const QString &id); + +private: + QString m_id; +}; + +QT_END_NAMESPACE + +#endif // QQMLWEBCHANNELATTACHED_H diff --git a/src/webchannelquick/qwebchannelquickglobal.h b/src/webchannelquick/qwebchannelquickglobal.h new file mode 100644 index 0000000..d16a2a8 --- /dev/null +++ b/src/webchannelquick/qwebchannelquickglobal.h @@ -0,0 +1,10 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QTWEBCHANNELQUICK_H +#define QTWEBCHANNELQUICK_H + +#include +#include + +#endif // QTWEBCHANNELQUICK_H diff --git a/tests/auto/qml/CMakeLists.txt b/tests/auto/qml/CMakeLists.txt index eaac420..3fc18ff 100644 --- a/tests/auto/qml/CMakeLists.txt +++ b/tests/auto/qml/CMakeLists.txt @@ -1,11 +1,6 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from qml.pro. - -##################################################################### -## qml Test: -##################################################################### # Collect test data file(GLOB_RECURSE test_data_glob @@ -13,7 +8,7 @@ file(GLOB_RECURSE test_data_glob data/*) list(APPEND test_data ${test_data_glob}) -qt_internal_add_test(tst_qmlwebchannel # special case +qt_internal_add_test(tst_qmlwebchannel QMLTEST QML_IMPORTPATH "${CMAKE_CURRENT_BINARY_DIR}/../../../qml" @@ -25,13 +20,8 @@ qt_internal_add_test(tst_qmlwebchannel # special case testwebchannel.cpp testwebchannel.h LIBRARIES Qt::CorePrivate - Qt::Gui Qt::WebChannelPrivate + Qt::WebChannelQuick TESTDATA ${test_data} ) -#### Keys ignored in scope 1:.:.:qml.pro:: -# DISTFILES = "tst_webchannelseparation.qml" -# IMPORTPATH = "$$OUT_PWD/../../../qml" "$$PWD" -# OTHER_FILES = "Client.qml" "WebChannelTest.qml" "tst_webchannel.qml" "tst_metaobjectpublisher.qml" "tst_bench.qml" "tst_multiclient.qml" -# TEMPLATE = "app" diff --git a/tests/auto/qml/testwebchannel.cpp b/tests/auto/qml/testwebchannel.cpp index a353c50..e2f7b20 100644 --- a/tests/auto/qml/testwebchannel.cpp +++ b/tests/auto/qml/testwebchannel.cpp @@ -3,8 +3,8 @@ #include "testwebchannel.h" -#include -#include +#include +#include QT_BEGIN_NAMESPACE diff --git a/tests/auto/qml/testwebchannel.h b/tests/auto/qml/testwebchannel.h index 4768f99..462e6de 100644 --- a/tests/auto/qml/testwebchannel.h +++ b/tests/auto/qml/testwebchannel.h @@ -4,7 +4,7 @@ #ifndef TESTWEBCHANNEL_H #define TESTWEBCHANNEL_H -#include +#include QT_BEGIN_NAMESPACE diff --git a/tests/auto/webchannel/CMakeLists.txt b/tests/auto/webchannel/CMakeLists.txt index 0dd4c31..ae747bb 100644 --- a/tests/auto/webchannel/CMakeLists.txt +++ b/tests/auto/webchannel/CMakeLists.txt @@ -1,25 +1,14 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# Generated from webchannel.pro. - -##################################################################### -## tst_webchannel Test: -##################################################################### - qt_internal_add_test(tst_webchannel SOURCES tst_webchannel.cpp tst_webchannel.h - INCLUDE_DIRECTORIES - ../../../src/webchannel LIBRARIES Qt::CorePrivate Qt::WebChannelPrivate ) -## Scopes: -##################################################################### - qt_internal_extend_target(tst_webchannel CONDITION TARGET Qt::Qml DEFINES WEBCHANNEL_TESTS_CAN_USE_JS_ENGINE diff --git a/tests/auto/webchannel/tst_webchannel.cpp b/tests/auto/webchannel/tst_webchannel.cpp index 28f1482..7bf04dd 100644 --- a/tests/auto/webchannel/tst_webchannel.cpp +++ b/tests/auto/webchannel/tst_webchannel.cpp @@ -4,9 +4,9 @@ #include "tst_webchannel.h" -#include -#include -#include +#include +#include +#include #include #ifdef WEBCHANNEL_TESTS_CAN_USE_JS_ENGINE -- cgit v1.2.1