summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Terrier <b.terrier@gmail.com>2022-07-30 20:24:34 +0000
committerBenjamin Terrier <b.terrier@gmail.com>2023-05-02 12:42:23 +0200
commit000f31fb643bb8fb0b89b1ab26a895223c318996 (patch)
treedcfa174620071fa199486951080c85fd800d1363
parent17c64fd5d2778df4a52849e8942104d4d9555a1e (diff)
downloadqtwebengine-000f31fb643bb8fb0b89b1ab26a895223c318996.tar.gz
Add qWebEngineGetDomainAndRegistry()
[ChangeLog][WebEngineCore] Add qWebEngineGetDomainAndRegistry() Fixes: QTBUG-105294 Change-Id: Ica40453f807b00c8548f974a495ddf84bbe8e194 Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
-rw-r--r--src/core/api/qtwebenginecoreglobal.cpp12
-rw-r--r--src/core/api/qtwebenginecoreglobal.h3
-rw-r--r--src/core/doc/src/qtwebengine-global.qdoc11
-rw-r--r--tests/auto/core/CMakeLists.txt1
-rw-r--r--tests/auto/core/getdomainandregistry/CMakeLists.txt12
-rw-r--r--tests/auto/core/getdomainandregistry/tst_getdomainandregistry.cpp27
6 files changed, 66 insertions, 0 deletions
diff --git a/src/core/api/qtwebenginecoreglobal.cpp b/src/core/api/qtwebenginecoreglobal.cpp
index d8d3d3b26..9082052f2 100644
--- a/src/core/api/qtwebenginecoreglobal.cpp
+++ b/src/core/api/qtwebenginecoreglobal.cpp
@@ -17,6 +17,8 @@
#include <QQuickWindow>
#include "web_engine_context.h"
+#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
+
#if QT_CONFIG(opengl)
QT_BEGIN_NAMESPACE
Q_GUI_EXPORT void qt_gl_set_global_share_context(QOpenGLContext *context);
@@ -218,4 +220,14 @@ static void initialize()
#endif // QT_CONFIG(opengl)
}
+QT_BEGIN_NAMESPACE
+
+QString qWebEngineGetDomainAndRegistry(const QUrl &url) {
+ const QString host = url.host();
+ const std::string domain = net::registry_controlled_domains::GetDomainAndRegistry(host.toStdString(), net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
+ return QString::fromStdString(domain);
+}
+
+QT_END_NAMESPACE
+
Q_CONSTRUCTOR_FUNCTION(initialize)
diff --git a/src/core/api/qtwebenginecoreglobal.h b/src/core/api/qtwebenginecoreglobal.h
index 3583e0848..166a82b78 100644
--- a/src/core/api/qtwebenginecoreglobal.h
+++ b/src/core/api/qtwebenginecoreglobal.h
@@ -5,6 +5,7 @@
#define QTWEBENGINECOREGLOBAL_H
#include <QtCore/qglobal.h>
+#include <QtCore/qurl.h>
#include <QtWebEngineCore/qtwebenginecore-config.h>
QT_BEGIN_NAMESPACE
@@ -22,6 +23,8 @@ Q_WEBENGINECORE_EXPORT Q_DECL_CONST_FUNCTION const char *qWebEngineProcessName()
Q_WEBENGINECORE_EXPORT Q_DECL_CONST_FUNCTION const char *qWebEngineChromiumVersion() noexcept;
Q_WEBENGINECORE_EXPORT Q_DECL_CONST_FUNCTION const char *qWebEngineChromiumSecurityPatchVersion() noexcept;
+Q_WEBENGINECORE_EXPORT QString qWebEngineGetDomainAndRegistry(const QUrl &url);
+
QT_END_NAMESPACE
#endif // QTWEBENGINECOREGLOBAL_H
diff --git a/src/core/doc/src/qtwebengine-global.qdoc b/src/core/doc/src/qtwebengine-global.qdoc
index 57df8b3ac..d4bbef01a 100644
--- a/src/core/doc/src/qtwebengine-global.qdoc
+++ b/src/core/doc/src/qtwebengine-global.qdoc
@@ -40,3 +40,14 @@
Returns the version number of last Chromium version security patches have been
merged from.
*/
+
+/*!
+ \fn QString qWebEngineGetDomainAndRegistry(const QUrl &url)
+ \relates <qtwebenginecoreglobal.h>
+ \since 6.6
+
+ Returns the domain of the host i.e. the effective top-level domain (eTLD)
+ plus the first domain below.
+
+ If the host is not a domain an empty string is returned.
+*/
diff --git a/tests/auto/core/CMakeLists.txt b/tests/auto/core/CMakeLists.txt
index ae10d918b..9981d4950 100644
--- a/tests/auto/core/CMakeLists.txt
+++ b/tests/auto/core/CMakeLists.txt
@@ -10,6 +10,7 @@ add_subdirectory(qwebengineurlresponseinterceptor)
add_subdirectory(qwebengineurlrequestjob)
add_subdirectory(origins)
add_subdirectory(devtools)
+add_subdirectory(getdomainandregistry)
if(QT_FEATURE_ssl)
add_subdirectory(qwebengineclientcertificatestore)
diff --git a/tests/auto/core/getdomainandregistry/CMakeLists.txt b/tests/auto/core/getdomainandregistry/CMakeLists.txt
new file mode 100644
index 000000000..bed27c812
--- /dev/null
+++ b/tests/auto/core/getdomainandregistry/CMakeLists.txt
@@ -0,0 +1,12 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+include(../../util/util.cmake)
+
+qt_internal_add_test(tst_getdomainandregistry
+ SOURCES
+ tst_getdomainandregistry.cpp
+ LIBRARIES
+ Qt::WebEngineCore
+ Test::Util
+)
diff --git a/tests/auto/core/getdomainandregistry/tst_getdomainandregistry.cpp b/tests/auto/core/getdomainandregistry/tst_getdomainandregistry.cpp
new file mode 100644
index 000000000..0e42d3e01
--- /dev/null
+++ b/tests/auto/core/getdomainandregistry/tst_getdomainandregistry.cpp
@@ -0,0 +1,27 @@
+// Copyright (C) 2022 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+#include <QtTest/QtTest>
+#include <QtWebEngineCore/qtwebenginecoreglobal.h>
+
+class tst_GetDomainAndRegistry final : public QObject {
+ Q_OBJECT
+
+private Q_SLOTS:
+ void getDomainAndRegistry();
+};
+
+void tst_GetDomainAndRegistry::getDomainAndRegistry() {
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"http://www.google.com/"}), QString("google.com"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"http://www.google.co.uk/"}), QString("google.co.uk"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"http://127.0.0.1/"}), QString());
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"https://qt.io/"}), QString("qt.io"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"https://download.qt.io/"}), QString("qt.io"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"https://foo.fr/"}), QString("foo.fr"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"https://foo.gouv.fr/"}), QString("foo.gouv.fr"));
+ QCOMPARE(qWebEngineGetDomainAndRegistry({"https://bar.foo.gouv.fr/"}), QString("foo.gouv.fr"));
+}
+
+
+QTEST_MAIN(tst_GetDomainAndRegistry)
+#include "tst_getdomainandregistry.moc"