diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp index 5625695fc79b5e6b3e04a31e29618e68cddf363b..2e3f3785a71e2595f17c3131248fe76e5128d7fa 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -1266,11 +1266,11 @@ void WebContentsAdapter::dpiScaleChanged() impl->NotifyScreenInfoChanged(); } -void WebContentsAdapter::backgroundColorChanged() +void WebContentsAdapter::setBackgroundColor(const QColor &color) { CHECK_INITIALIZED(); if (content::RenderWidgetHostView *rwhv = m_webContents->GetRenderWidgetHostView()) - rwhv->SetBackgroundColor(toSk(m_adapterClient->backgroundColor())); + rwhv->SetBackgroundColor(toSk(color)); } content::WebContents *WebContentsAdapter::webContents() const diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index 99114546dc339fab840a6328a8c292f7a2203dda..fcdac94b82fa2c89216643e74809289b3590e78b 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -187,7 +187,7 @@ public: void grantMouseLockPermission(bool granted); void dpiScaleChanged(); - void backgroundColorChanged(); + void setBackgroundColor(const QColor &color); QAccessibleInterface *browserAccessible(); ProfileQt* profile(); ProfileAdapter* profileAdapter(); diff --git a/src/webengine/api/qquickwebengineprofile.h b/src/webengine/api/qquickwebengineprofile.h index 4f10539243554ac7829046c6ee2fc97169918ab0..9fc4f9eca46666236354bd6588bfefcf4b3ae630 100644 --- a/src/webengine/api/qquickwebengineprofile.h +++ b/src/webengine/api/qquickwebengineprofile.h @@ -168,6 +168,7 @@ private: friend class QQuickWebEngineViewPrivate; friend class QQuickWebEngineDownloadItem; friend class QQuickWebEngineDownloadItemPrivate; + friend class QQuickWebEngineView; QScopedPointer<QQuickWebEngineProfilePrivate> d_ptr; }; diff --git a/src/webengine/api/qquickwebengineprofile_p.h b/src/webengine/api/qquickwebengineprofile_p.h index 9ed8b89de724df97c11be487b0bd617295f410e7..d31ded0ec5ea66b9a1d97e67adcffcaa309c2d80 100644 --- a/src/webengine/api/qquickwebengineprofile_p.h +++ b/src/webengine/api/qquickwebengineprofile_p.h @@ -90,7 +90,7 @@ public: static void userScripts_clear(QQmlListProperty<QQuickWebEngineScript> *p); private: - friend class QQuickWebEngineViewPrivate; + friend class QQuickWebEngineView; QQuickWebEngineProfile *q_ptr; QScopedPointer<QQuickWebEngineSettings> m_settings; QPointer<QtWebEngineCore::ProfileAdapter> m_profileAdapter; diff --git a/src/webengine/api/qquickwebenginesettings_p.h b/src/webengine/api/qquickwebenginesettings_p.h index 179416b544170028d51ca9186765c63846555629..6e1aaca390dfb27ed4aa44857f173fdc3b58e547 100644 --- a/src/webengine/api/qquickwebenginesettings_p.h +++ b/src/webengine/api/qquickwebenginesettings_p.h @@ -204,7 +204,7 @@ private: Q_DISABLE_COPY(QQuickWebEngineSettings) friend class QQuickWebEngineProfilePrivate; friend class QQuickWebEngineViewPrivate; - + friend class QQuickWebEngineView; void setParentSettings(QQuickWebEngineSettings *parentSettings); QScopedPointer<QtWebEngineCore::WebEngineSettings> d_ptr; diff --git a/src/webengine/api/qquickwebengineview.cpp b/src/webengine/api/qquickwebengineview.cpp index bdd3bfe266f058112f8d20ff432830aef9d4c370..6bf23ea7b4fa9d7dff3148d4f846563b6080fbda 100644 --- a/src/webengine/api/qquickwebengineview.cpp +++ b/src/webengine/api/qquickwebengineview.cpp @@ -107,8 +107,11 @@ static QAccessibleInterface *webAccessibleFactory(const QString &, QObject *obje } #endif // QT_NO_ACCESSIBILITY +static QLatin1String defaultMimeType("text/html;charset=UTF-8"); + QQuickWebEngineViewPrivate::QQuickWebEngineViewPrivate() : m_profile(nullptr) + , adapter(QSharedPointer<WebContentsAdapter>::create()) , m_history(new QQuickWebEngineHistory(this)) #if QT_CONFIG(webengine_testsupport) , m_testSupport(0) @@ -125,7 +128,7 @@ QQuickWebEngineViewPrivate::QQuickWebEngineViewPrivate() , m_isBeingAdopted(false) , m_dpiScale(1.0) , m_backgroundColor(Qt::white) - , m_defaultZoomFactor(1.0) + , m_zoomFactor(1.0) , m_ui2Enabled(false) , m_profileInitialized(false) { @@ -168,16 +171,13 @@ QQuickWebEngineViewPrivate::~QQuickWebEngineViewPrivate() void QQuickWebEngineViewPrivate::initializeProfile() { if (!m_profileInitialized) { + Q_ASSERT(!adapter->isInitialized()); m_profileInitialized = true; if (!m_profile) m_profile = QQuickWebEngineProfile::defaultProfile(); m_profile->d_ptr->addWebContentsAdapterClient(this); - adapter = QSharedPointer<WebContentsAdapter>::create(); m_settings.reset(new QQuickWebEngineSettings(m_profile->settings())); -#if QT_CONFIG(webengine_webchannel) - if (m_webChannel) - adapter->setWebChannel(m_webChannel, m_webChannelWorld); -#endif + adapter->setClient(this); } } @@ -348,7 +348,6 @@ void QQuickWebEngineViewPrivate::urlChanged(const QUrl &url) { Q_Q(QQuickWebEngineView); Q_UNUSED(url); - explicitUrl = QUrl(); Q_EMIT q->urlChanged(); } @@ -473,7 +472,6 @@ void QQuickWebEngineViewPrivate::loadFinished(bool success, const QUrl &url, boo return; } if (success) { - explicitUrl = QUrl(); QTimer::singleShot(0, q, [q, url, errorDescription, errorCode]() { QQuickWebEngineLoadRequest loadRequest(url, QQuickWebEngineView::LoadSucceededStatus, errorDescription, errorCode); emit q->loadingChanged(&loadRequest); @@ -784,9 +782,12 @@ QQuickWebEngineView::~QQuickWebEngineView() void QQuickWebEngineViewPrivate::ensureContentsAdapter() { + initializeProfile(); if (!adapter->isInitialized()) { - if (explicitUrl.isValid()) - adapter->load(explicitUrl); + if (!m_html.isEmpty()) + adapter->setContent(m_html.toUtf8(), defaultMimeType, m_url); + else if (m_url.isValid()) + adapter->load(m_url); else adapter->loadDefault(); } @@ -796,14 +797,21 @@ void QQuickWebEngineViewPrivate::initializationFinished() { Q_Q(QQuickWebEngineView); - if (m_backgroundColor != Qt::white) - adapter->backgroundColorChanged(); + Q_ASSERT(m_profileInitialized); + if (m_backgroundColor != Qt::white) { + adapter->setBackgroundColor(m_backgroundColor); + emit q->backgroundColorChanged(); + } + + if (!qFuzzyCompare(adapter->currentZoomFactor(), m_zoomFactor)) { + adapter->setZoomFactor(m_zoomFactor); + emit q->zoomFactorChanged(m_zoomFactor); + } + #if QT_CONFIG(webengine_webchannel) if (m_webChannel) adapter->setWebChannel(m_webChannel, m_webChannelWorld); #endif - if (!qFuzzyCompare(adapter->currentZoomFactor(), m_defaultZoomFactor)) - q->setZoomFactor(m_defaultZoomFactor); if (devToolsView && devToolsView->d_ptr->adapter) adapter->openDevToolsFrontend(devToolsView->d_ptr->adapter); @@ -874,20 +882,25 @@ void QQuickWebEngineViewPrivate::updateAction(QQuickWebEngineView::WebAction act QUrl QQuickWebEngineView::url() const { Q_D(const QQuickWebEngineView); - return d->explicitUrl.isValid() ? d->explicitUrl : d->adapter->activeUrl(); + if (d->adapter->isInitialized()) + return d->adapter->activeUrl(); + else + return d->m_url; } void QQuickWebEngineView::setUrl(const QUrl& url) { + Q_D(QQuickWebEngineView); if (url.isEmpty()) return; - Q_D(QQuickWebEngineView); - d->explicitUrl = url; - if (d->profileInitialized() && d->adapter->isInitialized()) + if (d->adapter->isInitialized()) { d->adapter->load(url); - if (!qmlEngine(this) || isComponentComplete()) - d->ensureContentsAdapter(); + return; + } + + d->m_url = url; + d->m_html.clear(); } QUrl QQuickWebEngineView::icon() const @@ -899,11 +912,12 @@ QUrl QQuickWebEngineView::icon() const void QQuickWebEngineView::loadHtml(const QString &html, const QUrl &baseUrl) { Q_D(QQuickWebEngineView); - d->explicitUrl = QUrl(); - if (!qmlEngine(this) || isComponentComplete()) - d->ensureContentsAdapter(); - if (d->adapter->isInitialized()) - d->adapter->setContent(html.toUtf8(), QStringLiteral("text/html;charset=UTF-8"), baseUrl); + d->m_url = baseUrl; + d->m_html = html; + if (d->adapter->isInitialized()) { + d->adapter->setContent(html.toUtf8(), defaultMimeType, baseUrl); + return; + } } void QQuickWebEngineView::goBack() @@ -939,20 +953,17 @@ void QQuickWebEngineView::stop() void QQuickWebEngineView::setZoomFactor(qreal arg) { Q_D(QQuickWebEngineView); - d->m_defaultZoomFactor = arg; - - qreal oldFactor = d->adapter->currentZoomFactor(); - d->adapter->setZoomFactor(arg); - if (qFuzzyCompare(oldFactor, d->adapter->currentZoomFactor())) - return; - - emit zoomFactorChanged(arg); + if (d->adapter->isInitialized() && !qFuzzyCompare(d->m_zoomFactor, d->adapter->currentZoomFactor())) { + d->adapter->setZoomFactor(arg); + emit zoomFactorChanged(arg); + } else { + d->m_zoomFactor = arg; + } } QQuickWebEngineProfile *QQuickWebEngineView::profile() { Q_D(QQuickWebEngineView); - // this can be called before onComplete for group properties d->initializeProfile(); return d->m_profile; } @@ -960,7 +971,24 @@ QQuickWebEngineProfile *QQuickWebEngineView::profile() void QQuickWebEngineView::setProfile(QQuickWebEngineProfile *profile) { Q_D(QQuickWebEngineView); - d->setProfile(profile); + + if (d->m_profile == profile) + return; + + if (!d->profileInitialized()) { + d->m_profile = profile; + return; + } + + if (d->m_profile) + d->m_profile->d_ptr->removeWebContentsAdapterClient(d); + + d->m_profile = profile; + d->m_profile->d_ptr->addWebContentsAdapterClient(d); + d->m_settings->setParentSettings(profile->settings()); + + d->updateAdapter(); + Q_EMIT profileChanged(); } QQuickWebEngineSettings *QQuickWebEngineView::settings() @@ -980,40 +1008,22 @@ QQmlListProperty<QQuickWebEngineScript> QQuickWebEngineView::userScripts() d->userScripts_clear); } -void QQuickWebEngineViewPrivate::setProfile(QQuickWebEngineProfile *profile) +void QQuickWebEngineViewPrivate::updateAdapter() { - Q_Q(QQuickWebEngineView); - - if (profile == m_profile) - return; - - if (!m_profileInitialized) { - m_profile = profile; - return; - } - - if (m_profile) - m_profile->d_ptr->removeWebContentsAdapterClient(this); - - m_profile = profile; - m_profile->d_ptr->addWebContentsAdapterClient(this); - Q_EMIT q->profileChanged(); - m_settings->setParentSettings(profile->settings()); - - if (adapter->profile() != profileAdapter()->profile()) { - // When the profile changes we need to create a new WebContentAdapter and reload the active URL. - bool wasInitialized = adapter->isInitialized(); - QUrl activeUrl = adapter->activeUrl(); - adapter = QSharedPointer<WebContentsAdapter>::create(); - adapter->setClient(this); - if (wasInitialized) { - if (explicitUrl.isValid()) - adapter->load(explicitUrl); - else if (activeUrl.isValid()) - adapter->load(activeUrl); - else - adapter->loadDefault(); - } + // When the profile changes we need to create a new WebContentAdapter and reload the active URL. + bool wasInitialized = adapter->isInitialized(); + QUrl activeUrl = adapter->activeUrl(); + adapter = QSharedPointer<WebContentsAdapter>::create(); + adapter->setClient(this); + if (wasInitialized) { + if (!m_html.isEmpty()) + adapter->setContent(m_html.toUtf8(), defaultMimeType, m_url); + else if (m_url.isValid()) + adapter->load(m_url); + else if (activeUrl.isValid()) + adapter->load(activeUrl); + else + adapter->loadDefault(); } } @@ -1178,7 +1188,7 @@ qreal QQuickWebEngineView::zoomFactor() const { Q_D(const QQuickWebEngineView); if (!d->adapter->isInitialized()) - return d->m_defaultZoomFactor; + return d->m_zoomFactor; return d->adapter->currentZoomFactor(); } @@ -1194,8 +1204,10 @@ void QQuickWebEngineView::setBackgroundColor(const QColor &color) if (color == d->m_backgroundColor) return; d->m_backgroundColor = color; - d->adapter->backgroundColorChanged(); - emit backgroundColorChanged(); + if (d->adapter->isInitialized()) { + d->adapter->setBackgroundColor(color); + emit backgroundColorChanged(); + } } /*! @@ -1963,7 +1975,6 @@ void QQuickWebEngineView::componentComplete() QQuickItem::componentComplete(); Q_D(QQuickWebEngineView); d->initializeProfile(); - d->adapter->setClient(d); #ifndef QT_NO_ACCESSIBILITY // Enable accessibility via a dynamic QQmlProperty, instead of using private API call // QQuickAccessibleAttached::qmlAttachedProperties(this). The qmlContext is required, otherwise diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index fda67a6bff0d760b8381a0cd5d542ca6df046c4b..ee38ece6b735d8eef1ca302606931b669182506e 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -160,6 +160,7 @@ public: void updateAction(QQuickWebEngineView::WebAction) const; void adoptWebContents(QtWebEngineCore::WebContentsAdapter *webContents); void setProfile(QQuickWebEngineProfile *profile); + void updateAdapter(); void ensureContentsAdapter(); void setFullScreenMode(bool); @@ -178,7 +179,8 @@ public: #endif QQmlComponent *contextMenuExtraItems; QtWebEngineCore::WebEngineContextMenuData m_contextMenuData; - QUrl explicitUrl; + QUrl m_url; + QString m_html; QUrl iconUrl; QQuickWebEngineFaviconProvider *faviconProvider; int loadProgress; @@ -203,7 +205,7 @@ private: QList<QQuickWebEngineScript *> m_userScripts; qreal m_dpiScale; QColor m_backgroundColor; - qreal m_defaultZoomFactor; + qreal m_zoomFactor; bool m_ui2Enabled; bool m_profileInitialized; }; diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp index a86681191a10a39664b09d7f7a0555cdb3df5792..8362f190e961d580f1e584979aa1b0412d896cb8 100644 --- a/src/webenginewidgets/api/qwebenginepage.cpp +++ b/src/webenginewidgets/api/qwebenginepage.cpp @@ -281,7 +281,7 @@ RenderWidgetHostViewQtDelegate *QWebEnginePagePrivate::CreateRenderWidgetHostVie void QWebEnginePagePrivate::initializationFinished() { if (m_backgroundColor != Qt::white) - adapter->backgroundColorChanged(); + adapter->setBackgroundColor(m_backgroundColor); #if QT_CONFIG(webengine_webchannel) if (webChannel) adapter->setWebChannel(webChannel, webChannelWorldId); @@ -1007,7 +1007,7 @@ void QWebEnginePage::setBackgroundColor(const QColor &color) if (d->m_backgroundColor == color) return; d->m_backgroundColor = color; - d->adapter->backgroundColorChanged(); + d->adapter->setBackgroundColor(color); } /*! diff --git a/tests/auto/quick/qtbug-70248/qtbug-70248.pro b/tests/auto/quick/qtbug-70248/qtbug-70248.pro new file mode 100644 index 0000000000000000000000000000000000000000..e1b18bc16345dcbc6af5abfb250fc2059378c23b --- /dev/null +++ b/tests/auto/quick/qtbug-70248/qtbug-70248.pro @@ -0,0 +1,5 @@ +include(../tests.pri) +QT += webengine webengine-private + +RESOURCES += \ + test.qrc diff --git a/tests/auto/quick/qtbug-70248/test.qml b/tests/auto/quick/qtbug-70248/test.qml new file mode 100644 index 0000000000000000000000000000000000000000..35962aff543ec4a38641fab21b77771e76c6f945 --- /dev/null +++ b/tests/auto/quick/qtbug-70248/test.qml @@ -0,0 +1,16 @@ +import QtQuick 2.9 +import QtQuick.Window 2.2 +import QtWebEngine 1.3 + +Window { + visible: true + width: 640 + height: 480 + + property var url: view && view.url + + WebEngineView { + id: view + anchors.fill: parent + } +} diff --git a/tests/auto/quick/qtbug-70248/test.qrc b/tests/auto/quick/qtbug-70248/test.qrc new file mode 100644 index 0000000000000000000000000000000000000000..83fea5eb09ac43ab4d9cf5b9b585f102ecc9df72 --- /dev/null +++ b/tests/auto/quick/qtbug-70248/test.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>test.qml</file> + </qresource> +</RCC> diff --git a/tests/auto/quick/qtbug-70248/tst_qtbug-70248.cpp b/tests/auto/quick/qtbug-70248/tst_qtbug-70248.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dffa1d843a5a5b5a6e54c967feb0f481e6db237 --- /dev/null +++ b/tests/auto/quick/qtbug-70248/tst_qtbug-70248.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qtwebengineglobal.h" +#include <QQuickWebEngineProfile> +#include <QQmlApplicationEngine> +#include <QQuickWindow> +#include <QTest> +#include <QSignalSpy> + +class tst_qtbug_70248: public QObject { + Q_OBJECT +public: + tst_qtbug_70248(){} +private slots: + void test(); +}; + +void tst_qtbug_70248::test() +{ + QtWebEngine::initialize(); + QScopedPointer<QQmlApplicationEngine> engine; + QQuickWebEngineProfile::defaultProfile()->setOffTheRecord(true); + engine.reset(new QQmlApplicationEngine()); + engine->load(QUrl(QStringLiteral("qrc:/test.qml"))); + QQuickWindow *widnow = qobject_cast<QQuickWindow*>(engine->rootObjects().first()); + QVERIFY(widnow); +} + +#include "tst_qtbug-70248.moc" +QTEST_MAIN(tst_qtbug_70248) + diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index 02ce59f177fbde3632d7c883ca51ed92e6ffc3df..50a6a8587274ecb80636dd1318bd63e0f22e7625 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -8,7 +8,8 @@ SUBDIRS += \ inspectorserver \ publicapi \ qquickwebenginedefaultsurfaceformat \ - qquickwebengineview + qquickwebengineview \ + qtbug-70248 qtConfig(webengine-testsupport) { SUBDIRS += \ @@ -17,4 +18,4 @@ qtConfig(webengine-testsupport) { } # QTBUG-66055 -boot2qt: SUBDIRS -= inspectorserver qquickwebenginedefaultsurfaceformat qquickwebengineview qmltests dialogs +boot2qt: SUBDIRS -= inspectorserver qquickwebenginedefaultsurfaceformat qquickwebengineview qmltests dialogs qtbug-70248