diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-06-02 16:50:20 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-06-09 08:01:02 +0200 |
commit | 5dc14c88f9510795835fb4f0a0d46d67c40f7020 (patch) | |
tree | 937173d548a6ad0d4c46cf75914adb6f6b140254 /src/qml/qml | |
parent | 6a48a81319b886c8a3f85e1eb024186b05d0f3af (diff) |
Allow QML plugins to be optional
If a plugin does nothing but load the library that provides the types,
we can skip the plugin loading by linking the library directly. State
that in the qmldir file, and evaluate it when loading the module.
Task-number: QTBUG-84639
Change-Id: I2097237866a50f66c55e4653ad119fe10e18a893
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml')
-rw-r--r-- | src/qml/qml/qqmlengine.cpp | 3 | ||||
-rw-r--r-- | src/qml/qml/qqmlimport.cpp | 124 | ||||
-rw-r--r-- | src/qml/qml/qqmlimport_p.h | 4 | ||||
-rw-r--r-- | src/qml/qml/qqmlmetatype.cpp | 21 | ||||
-rw-r--r-- | src/qml/qml/qqmlmetatype_p.h | 12 |
5 files changed, 97 insertions, 67 deletions
diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 1f203fae88..381d934b8b 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -2202,7 +2202,8 @@ void QQmlEngine::setPluginPathList(const QStringList &paths) bool QQmlEngine::importPlugin(const QString &filePath, const QString &uri, QList<QQmlError> *errors) { Q_D(QQmlEngine); - return d->importDatabase.importDynamicPlugin(filePath, uri, QString(), QTypeRevision(), errors); + return d->importDatabase.importDynamicPlugin(filePath, uri, QString(), QTypeRevision(), false, + errors); } #endif diff --git a/src/qml/qml/qqmlimport.cpp b/src/qml/qml/qqmlimport.cpp index c17e3db8a1..1ae9505dc8 100644 --- a/src/qml/qml/qqmlimport.cpp +++ b/src/qml/qml/qqmlimport.cpp @@ -138,7 +138,7 @@ bool isPathAbsolute(const QString &path) struct RegisteredPlugin { QString uri; - QPluginLoader* loader; + QPluginLoader *loader = nullptr; }; struct StringRegisteredPluginMap : public QMap<QString, RegisteredPlugin> { @@ -1229,30 +1229,36 @@ bool QQmlImportsPrivate::importExtension(const QString &qmldirFilePath, int dynamicPluginsFound = 0; int staticPluginsFound = 0; -#if QT_CONFIG(library) + auto handleErrors = [&]() { + if (errors) { + // XXX TODO: should we leave the import plugin error alone? + // Here, we pop it off the top and coalesce it into this error's message. + // The reason is that the lower level may add url and line/column numbering information. + QQmlError error; + error.setDescription( + QQmlImportDatabase::tr( + "plugin cannot be loaded for module \"%1\": %2") + .arg(uri, errors->takeFirst().description())); + error.setUrl(QUrl::fromLocalFile(qmldirFilePath)); + errors->prepend(error); + } + }; + const auto qmldirPlugins = qmldir.plugins(); for (const QQmlDirParser::Plugin &plugin : qmldirPlugins) { - QString resolvedFilePath = database->resolvePlugin(typeLoader, qmldirPath, plugin.path, plugin.name); - if (!resolvedFilePath.isEmpty()) { - dynamicPluginsFound++; - if (!database->importDynamicPlugin(resolvedFilePath, uri, typeNamespace, version, errors)) { - if (errors) { - // XXX TODO: should we leave the import plugin error alone? - // Here, we pop it off the top and coalesce it into this error's message. - // The reason is that the lower level may add url and line/column numbering information. - QQmlError error; - error.setDescription( - QQmlImportDatabase::tr( - "plugin cannot be loaded for module \"%1\": %2") - .arg(uri, errors->takeFirst().description())); - error.setUrl(QUrl::fromLocalFile(qmldirFilePath)); - errors->prepend(error); - } - return false; - } + const QString resolvedFilePath = database->resolvePlugin( + typeLoader, qmldirPath, plugin.path, plugin.name); + + if (resolvedFilePath.isEmpty()) + continue; + + ++dynamicPluginsFound; + if (!database->importDynamicPlugin( + resolvedFilePath, uri, typeNamespace, version, plugin.optional, errors)) { + handleErrors(); + return false; } } -#endif // QT_CONFIG(library) if (dynamicPluginsFound < qmldirPluginCount) { // Check if the missing plugins can be resolved statically. We do this by looking at @@ -2178,16 +2184,9 @@ void QQmlImportDatabase::setImportPathList(const QStringList &paths) /*! \internal */ -static bool registerPluginTypes(QObject *instance, const QString &basePath, const QString &uri, - const QString &typeNamespace, QTypeRevision version, - QList<QQmlError> *errors) +static bool lockModule(const QString &uri, const QString &typeNamespace, QTypeRevision version, + QList<QQmlError> *errors) { - if (qmlImportTrace()) - qDebug().nospace() << "QQmlImportDatabase::registerPluginTypes: " << uri << " from " << basePath; - - if (!QQmlMetaType::registerPluginTypes(instance, basePath, uri, typeNamespace, version, errors)) - return false; - if (version.hasMajorVersion() && !typeNamespace.isEmpty() && !QQmlMetaType::protectModule(uri, version)) { QQmlError error; @@ -2229,8 +2228,12 @@ bool QQmlImportDatabase::importStaticPlugin( plugin.loader = nullptr; plugins->insert(uniquePluginID, plugin); - if (!registerPluginTypes(instance, basePath, uri, typeNamespace, version, errors)) + if (QQmlMetaType::registerPluginTypes( + instance, basePath, uri, typeNamespace, version, errors) + == QQmlMetaType::RegistrationResult::Failure + || !lockModule(uri, typeNamespace, version, errors)) { return false; + } } // Release the lock on plugins early as we're done with the global part. Releasing the lock @@ -2245,13 +2248,12 @@ bool QQmlImportDatabase::importStaticPlugin( return true; } -#if QT_CONFIG(library) /*! \internal */ bool QQmlImportDatabase::importDynamicPlugin( const QString &filePath, const QString &uri, const QString &typeNamespace, - QTypeRevision version, QList<QQmlError> *errors) + QTypeRevision version, bool isOptional, QList<QQmlError> *errors) { QFileInfo fileInfo(filePath); const QString absoluteFilePath = fileInfo.absoluteFilePath(); @@ -2279,35 +2281,56 @@ bool QQmlImportDatabase::importDynamicPlugin( return false; } - QPluginLoader* loader = nullptr; - if (!typesRegistered) { - loader = new QPluginLoader(absoluteFilePath); + RegisteredPlugin plugin; + plugin.uri = uri; + + const QString absolutePath = fileInfo.absolutePath(); + if (!typesRegistered && isOptional) { + switch (QQmlMetaType::registerPluginTypes(nullptr, absolutePath, uri, typeNamespace, + version, errors)) { + case QQmlMetaType::RegistrationResult::NoRegistrationFunction: + // try again with plugin + break; + case QQmlMetaType::RegistrationResult::Success: + if (!lockModule(uri, typeNamespace, version, errors)) + return false; + // instance and loader intentionally left at nullptr + plugins->insert(absoluteFilePath, plugin); + typesRegistered = true; + break; + case QQmlMetaType::RegistrationResult::Failure: + return false; + } + } - if (!loader->load()) { +#if QT_CONFIG(library) + if (!typesRegistered) { + plugin.loader = new QPluginLoader(absoluteFilePath); + if (!plugin.loader->load()) { if (errors) { QQmlError error; - error.setDescription(loader->errorString()); + error.setDescription(plugin.loader->errorString()); errors->prepend(error); } - delete loader; + delete plugin.loader; return false; } - } else { - loader = plugins->value(absoluteFilePath).loader; - } - instance = loader->instance(); - - if (!typesRegistered) { - RegisteredPlugin plugin; - plugin.uri = uri; - plugin.loader = loader; + instance = plugin.loader->instance(); plugins->insert(absoluteFilePath, plugin); // Continue with shared code path for dynamic and static plugins: - if (!registerPluginTypes(instance, fileInfo.absolutePath(), uri, typeNamespace, version, errors)) + if (QQmlMetaType::registerPluginTypes( + instance, fileInfo.absolutePath(), uri, typeNamespace, version, errors) + == QQmlMetaType::RegistrationResult::Failure + || !lockModule(uri, typeNamespace, version, errors)) { return false; + } + } else { + if (QPluginLoader *loader = plugins->value(absoluteFilePath).loader) + instance = loader->instance(); } +#endif // QT_CONFIG(library) } // Release the lock on plugins early as we're done with the global part. Releasing the lock @@ -2335,10 +2358,12 @@ bool QQmlImportDatabase::removeDynamicPlugin(const QString &filePath) if (!loader) return false; +#if QT_CONFIG(library) if (!loader->unload()) { qWarning("Unloading %s failed: %s", qPrintable(it->uri), qPrintable(loader->errorString())); } +#endif delete loader; plugins->erase(it); @@ -2356,7 +2381,6 @@ QStringList QQmlImportDatabase::dynamicPlugins() const } return results; } -#endif // QT_CONFIG(library) void QQmlImportDatabase::clearDirCache() { diff --git a/src/qml/qml/qqmlimport_p.h b/src/qml/qml/qqmlimport_p.h index 57baa2c87f..5f16880199 100644 --- a/src/qml/qml/qqmlimport_p.h +++ b/src/qml/qml/qqmlimport_p.h @@ -226,13 +226,11 @@ public: QQmlImportDatabase(QQmlEngine *); ~QQmlImportDatabase(); -#if QT_CONFIG(library) bool importDynamicPlugin(const QString &filePath, const QString &uri, const QString &importNamespace, QTypeRevision version, - QList<QQmlError> *errors); + bool isOptional, QList<QQmlError> *errors); bool removeDynamicPlugin(const QString &filePath); QStringList dynamicPlugins() const; -#endif QStringList importPathList(PathType type = LocalOrRemote) const; void setImportPathList(const QStringList &paths); diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index 61e8e323c6..fd307e4575 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -681,9 +681,9 @@ public: }; -bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePath, - const QString &uri, const QString &typeNamespace, - QTypeRevision version, QList<QQmlError> *errors) +QQmlMetaType::RegistrationResult QQmlMetaType::registerPluginTypes( + QObject *instance, const QString &basePath, const QString &uri, + const QString &typeNamespace, QTypeRevision version, QList<QQmlError> *errors) { if (!typeNamespace.isEmpty() && typeNamespace != uri) { // This is an 'identified' module @@ -695,7 +695,7 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat .arg(typeNamespace).arg(uri)); errors->prepend(error); } - return false; + return RegistrationResult::Failure; } QStringList failures; @@ -713,7 +713,7 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat .arg(typeNamespace)); errors->prepend(error); } - return false; + return RegistrationResult::Failure; } } else { // This is not an identified module - provide a warning @@ -722,7 +722,7 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat "it cannot be protected from external registrations.").arg(uri)); } - if (!qobject_cast<QQmlEngineExtensionInterface *>(instance)) { + if (instance && !qobject_cast<QQmlEngineExtensionInterface *>(instance)) { QQmlTypesExtensionInterface *iface = qobject_cast<QQmlTypesExtensionInterface *>(instance); if (!iface) { if (errors) { @@ -732,7 +732,7 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat "QQmlEngineExtensionInterface").arg(typeNamespace)); errors->prepend(error); } - return false; + return RegistrationResult::Failure; } if (auto *plugin = qobject_cast<QQmlExtensionPlugin *>(instance)) { @@ -746,7 +746,8 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat iface->registerTypes(moduleId); } - data->registerModuleTypes(uri); + if (failures.isEmpty() && !data->registerModuleTypes(uri)) + return RegistrationResult::NoRegistrationFunction; if (!failures.isEmpty()) { if (errors) { @@ -756,11 +757,11 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat errors->prepend(error); } } - return false; + return RegistrationResult::Failure; } } - return true; + return RegistrationResult::Success; } /* diff --git a/src/qml/qml/qqmlmetatype_p.h b/src/qml/qml/qqmlmetatype_p.h index 08d79d2e5b..9ce33ab545 100644 --- a/src/qml/qml/qqmlmetatype_p.h +++ b/src/qml/qml/qqmlmetatype_p.h @@ -73,14 +73,20 @@ struct CompositeMetaTypeIds class Q_QML_PRIVATE_EXPORT QQmlMetaType { public: + enum class RegistrationResult { + Success, + Failure, + NoRegistrationFunction + }; + static QQmlType registerType(const QQmlPrivate::RegisterType &type); static QQmlType registerInterface(const QQmlPrivate::RegisterInterface &type); static QQmlType registerSingletonType(const QQmlPrivate::RegisterSingletonType &type); static QQmlType registerCompositeSingletonType(const QQmlPrivate::RegisterCompositeSingletonType &type); static QQmlType registerCompositeType(const QQmlPrivate::RegisterCompositeType &type); - static bool registerPluginTypes(QObject *instance, const QString &basePath, - const QString &uri, const QString &typeNamespace, - QTypeRevision version, QList<QQmlError> *errors); + static RegistrationResult registerPluginTypes(QObject *instance, const QString &basePath, + const QString &uri, const QString &typeNamespace, + QTypeRevision version, QList<QQmlError> *errors); static QQmlType typeForUrl(const QString &urlString, const QHashedStringRef& typeName, bool isCompositeSingleton, QList<QQmlError> *errors, QTypeRevision version = QTypeRevision()); |