From 914e0300792856ddac9b99b20a8d88dd6f087fa6 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Tue, 1 Oct 2019 12:37:55 +0200 Subject: Use qmlProtectModule to protect a module from further modification We don't need two mechanisms to do essentially the same thing. QQmlTypeLoader::Blob::addImport() had an "optimization" to never check for qmldir files of locked imports. This meant the first time you imported a module with a plugin that locked the module you could use the qmldir file to load additional .qml files afterwards. The second time you imported the same thing, you couldn't. As this is not a great example of consistent behavior, we drop this optimization and always allow the qmldir files of plugins that lock the module to specify additional QML files. As a side effect of this, additional plugins listed in a qmldir file can also now be loaded after the module has been locked by some other means. However, any qmlRegisterFooBar() called from the module will be prevented. Change-Id: Idabb2bd5f75fc85b62f42625173672b4ae84382e Reviewed-by: Fabian Kosmale Reviewed-by: Simon Hausmann --- src/qml/qml/qqmlmetatype.cpp | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'src/qml/qml/qqmlmetatype.cpp') diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index 268d58a856..1a5affb0ad 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -342,7 +342,7 @@ QString registrationTypeString(QQmlType::RegistrationType typeType) // NOTE: caller must hold a QMutexLocker on "data" bool checkRegistration(QQmlType::RegistrationType typeType, QQmlMetaTypeData *data, - const char *uri, const QString &typeName, int majorVersion = -1) + const char *uri, const QString &typeName, int majorVersion) { if (!typeName.isEmpty()) { if (typeName.at(0).isLower()) { @@ -363,27 +363,16 @@ bool checkRegistration(QQmlType::RegistrationType typeType, QQmlMetaTypeData *da if (uri && !typeName.isEmpty()) { QString nameSpace = QString::fromUtf8(uri); - - if (data->typeRegistrationNamespace.isEmpty() && !nameSpace.isEmpty()) { - // Is the target namespace protected against further registrations? - if (data->protectedNamespaces.contains(nameSpace)) { + QQmlMetaTypeData::VersionedUri versionedUri; + versionedUri.uri = nameSpace; + versionedUri.majorVersion = majorVersion; + if (QQmlTypeModule* qqtm = data->uriToModule.value(versionedUri, 0)){ + if (qqtm->isLocked()){ QString failure(QCoreApplication::translate("qmlRegisterType", - "Cannot install %1 '%2' into protected namespace '%3'")); - data->recordTypeRegFailure(failure.arg(registrationTypeString(typeType)).arg(typeName).arg(nameSpace)); + "Cannot install %1 '%2' into protected module '%3' version '%4'")); + data->recordTypeRegFailure(failure.arg(registrationTypeString(typeType)).arg(typeName).arg(nameSpace).arg(majorVersion)); return false; } - } else if (majorVersion >= 0) { - QQmlMetaTypeData::VersionedUri versionedUri; - versionedUri.uri = nameSpace; - versionedUri.majorVersion = majorVersion; - if (QQmlTypeModule* qqtm = data->uriToModule.value(versionedUri, 0)){ - if (qqtm->isLocked()){ - QString failure(QCoreApplication::translate("qmlRegisterType", - "Cannot install %1 '%2' into protected module '%3' version '%4'")); - data->recordTypeRegFailure(failure.arg(registrationTypeString(typeType)).arg(typeName).arg(nameSpace).arg(majorVersion)); - return false; - } - } } } @@ -477,8 +466,10 @@ QQmlType QQmlMetaType::registerCompositeSingletonType(const QQmlPrivate::Registe bool fileImport = false; if (*(type.uri) == '\0') fileImport = true; - if (!checkRegistration(QQmlType::CompositeSingletonType, data, fileImport ? nullptr : type.uri, typeName)) + if (!checkRegistration(QQmlType::CompositeSingletonType, data, fileImport ? nullptr : type.uri, + typeName, type.versionMajor)) { return QQmlType(); + } QQmlTypePrivate *priv = createQQmlType(data, typeName, type); addTypeToData(priv, data); @@ -560,12 +551,12 @@ int QQmlMetaType::registerUnitCacheHook( return 0; } -bool QQmlMetaType::protectModule(const char *uri, int majVersion) +bool QQmlMetaType::protectModule(const QString &uri, int majVersion) { QQmlMetaTypeDataPtr data; QQmlMetaTypeData::VersionedUri versionedUri; - versionedUri.uri = QString::fromUtf8(uri); + versionedUri.uri = uri; versionedUri.majorVersion = majVersion; if (QQmlTypeModule* qqtm = data->uriToModule.value(versionedUri, 0)) { @@ -683,8 +674,6 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat } return false; } - - data->protectedNamespaces.insert(uri); } else { // This is not an identified module - provide a warning qWarning().nospace() << qPrintable( @@ -698,11 +687,9 @@ bool QQmlMetaType::registerPluginTypes(QObject *instance, const QString &basePat = QQmlImports::urlFromLocalFileOrQrcOrUrl(basePath); } - data->typeRegistrationNamespace = typeNamespace; const QByteArray bytes = uri.toUtf8(); const char *moduleId = bytes.constData(); iface->registerTypes(moduleId); - data->typeRegistrationNamespace.clear(); } if (!failures.isEmpty()) { -- cgit v1.2.3