From df93624cc31e9da29532eed1d86c9f172575355a Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Thu, 19 Mar 2020 15:59:05 +0100 Subject: Set the httpUserAgent after the component has been completed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since httpUserAgent can be set as a property then it should account for the fact that init() has not yet been called. So ensure that the httpUserAgent is set on the profile created when init() is called. Fixes: QTBUG-82778 Change-Id: I072676e719eedcb0fbc8024f3e902fd1b7073a3e Reviewed-by: Christian Strømme --- src/plugins/webengine/qwebenginewebview.cpp | 14 +++++++++++--- tests/auto/qml/qquickwebview/tst_qquickwebview.cpp | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/plugins/webengine/qwebenginewebview.cpp b/src/plugins/webengine/qwebenginewebview.cpp index f16371d..7870b3a 100644 --- a/src/plugins/webengine/qwebenginewebview.cpp +++ b/src/plugins/webengine/qwebenginewebview.cpp @@ -81,8 +81,11 @@ QString QWebEngineWebViewPrivate::httpUserAgent() const void QWebEngineWebViewPrivate::setHttpUserAgent(const QString &userAgent) { - m_profile->setHttpUserAgent(userAgent); - Q_EMIT httpUserAgentChanged(userAgent); + m_httpUserAgent = userAgent; + if (m_profile) { + m_profile->setHttpUserAgent(userAgent); + Q_EMIT httpUserAgentChanged(userAgent); + } } QUrl QWebEngineWebViewPrivate::url() const @@ -255,7 +258,12 @@ void QWebEngineWebViewPrivate::QQuickWebEngineViewPtr::init() const Q_ASSERT(webEngineView); QQuickWebEngineProfile *profile = webEngineView->profile(); m_parent->m_profile = profile; - m_parent->m_httpUserAgent = profile->httpUserAgent(); + // When the httpUserAgent is set as a property then it will be set before + // init() is called + if (m_parent->m_httpUserAgent.isEmpty()) + m_parent->m_httpUserAgent = profile->httpUserAgent(); + else + profile->setHttpUserAgent(m_parent->m_httpUserAgent); QObject::connect(webEngineView, &QQuickWebEngineView::urlChanged, m_parent, &QWebEngineWebViewPrivate::q_urlChanged); QObject::connect(webEngineView, &QQuickWebEngineView::loadProgressChanged, m_parent, &QWebEngineWebViewPrivate::q_loadProgressChanged); QObject::connect(webEngineView, &QQuickWebEngineView::loadingChanged, m_parent, &QWebEngineWebViewPrivate::q_loadingChanged); diff --git a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp index e3162bc..b1a1364 100644 --- a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp +++ b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp @@ -68,6 +68,7 @@ private Q_SLOTS: void multipleWebViewWindows(); void multipleWebViews(); void titleUpdate(); + void changeUserAgent(); private: inline QQuickWebView *newWebView(); @@ -75,11 +76,12 @@ private: void runJavaScript(const QString &script); QScopedPointer m_window; QScopedPointer m_component; + QQmlEngine *engine = nullptr; }; tst_QQuickWebView::tst_QQuickWebView() { - static QQmlEngine *engine = new QQmlEngine(this); + engine = new QQmlEngine(this); m_component.reset(new QQmlComponent(engine, this)); m_component->setData(QByteArrayLiteral("import QtQuick 2.0\n" "import QtWebView 1.1\n" @@ -332,5 +334,19 @@ void tst_QQuickWebView::titleUpdate() } +void tst_QQuickWebView::changeUserAgent() +{ + QQmlComponent userAgentWebView(engine, this); + userAgentWebView.setData(QByteArrayLiteral("import QtQuick 2.0\n" + "import QtWebView 1.14\n" + "WebView {\n" + "httpUserAgent: \"dummy\"\n" + "}"), + QUrl()); + QObject *viewInstance = userAgentWebView.create(); + QQuickWebView *webView = qobject_cast(viewInstance); + QCOMPARE(webView->httpUserAgent(), "dummy"); +} + QTEST_MAIN(tst_QQuickWebView) #include "tst_qquickwebview.moc" -- cgit v1.2.3