/**************************************************************************** ** ** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWebView module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:COMM$ ** ** 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. ** ** $QT_END_LICENSE$ ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ****************************************************************************/ #include "qwebenginewebview_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include QT_BEGIN_NAMESPACE static QByteArray qmlSource() { return QByteArrayLiteral("import QtWebEngine 1.1\n" " WebEngineView {\n" "}\n"); } QWebEngineWebViewPrivate::QWebEngineWebViewPrivate(QObject *p) : QAbstractWebView(p), m_profile(nullptr) { m_webEngineView.m_parent = this; } QWebEngineWebViewPrivate::~QWebEngineWebViewPrivate() { } QString QWebEngineWebViewPrivate::httpUserAgent() const { return m_httpUserAgent; } void QWebEngineWebViewPrivate::setHttpUserAgent(const QString &userAgent) { m_httpUserAgent = userAgent; if (m_profile) { m_profile->setHttpUserAgent(userAgent); Q_EMIT httpUserAgentChanged(userAgent); } } QUrl QWebEngineWebViewPrivate::url() const { return m_webEngineView->url(); } void QWebEngineWebViewPrivate::setUrl(const QUrl &url) { m_webEngineView->setUrl(url); } void QWebEngineWebViewPrivate::loadHtml(const QString &html, const QUrl &baseUrl) { m_webEngineView->loadHtml(html, baseUrl); } bool QWebEngineWebViewPrivate::canGoBack() const { return m_webEngineView->canGoBack(); } void QWebEngineWebViewPrivate::goBack() { m_webEngineView->goBack(); } bool QWebEngineWebViewPrivate::canGoForward() const { return m_webEngineView->canGoForward(); } void QWebEngineWebViewPrivate::goForward() { m_webEngineView->goForward(); } void QWebEngineWebViewPrivate::reload() { m_webEngineView->reload(); } QString QWebEngineWebViewPrivate::title() const { return m_webEngineView->title(); } void QWebEngineWebViewPrivate::setGeometry(const QRect &geometry) { m_webEngineView->setSize(geometry.size()); } void QWebEngineWebViewPrivate::setVisibility(QWindow::Visibility visibility) { setVisible(visibility != QWindow::Hidden ? true : false); } void QWebEngineWebViewPrivate::runJavaScriptPrivate(const QString &script, int callbackId) { m_webEngineView->runJavaScript(script, QQuickWebView::takeCallback(callbackId)); } void QWebEngineWebViewPrivate::setVisible(bool visible) { m_webEngineView->setVisible(visible); } void QWebEngineWebViewPrivate::setFocus(bool focus) { if (focus) m_webEngineView->forceActiveFocus(); } int QWebEngineWebViewPrivate::loadProgress() const { return m_webEngineView->loadProgress(); } bool QWebEngineWebViewPrivate::isLoading() const { return m_webEngineView->isLoading(); } void QWebEngineWebViewPrivate::setParentView(QObject *parentView) { Q_UNUSED(parentView); } QObject *QWebEngineWebViewPrivate::parentView() const { return m_webEngineView->window(); } void QWebEngineWebViewPrivate::stop() { m_webEngineView->stop(); } void QWebEngineWebViewPrivate::q_urlChanged() { Q_EMIT urlChanged(m_webEngineView->url()); } void QWebEngineWebViewPrivate::q_loadProgressChanged() { Q_EMIT loadProgressChanged(m_webEngineView->loadProgress()); } void QWebEngineWebViewPrivate::q_titleChanged() { Q_EMIT titleChanged(m_webEngineView->title()); } void QWebEngineWebViewPrivate::q_loadingChanged(QQuickWebEngineLoadRequest *loadRequest) { QWebViewLoadRequestPrivate lr(loadRequest->url(), static_cast(loadRequest->status()), // These "should" match... loadRequest->errorString()); Q_EMIT loadingChanged(lr); } void QWebEngineWebViewPrivate::q_profileChanged() { auto profile = m_webEngineView->profile(); if (profile == m_profile) return; m_profile = profile; auto userAgent = m_profile->httpUserAgent(); if (m_httpUserAgent == userAgent) return; m_httpUserAgent = userAgent; QObject::connect(m_profile, &QQuickWebEngineProfile::httpUserAgentChanged, this, &QWebEngineWebViewPrivate::q_httpUserAgentChanged); Q_EMIT httpUserAgentChanged(userAgent); } void QWebEngineWebViewPrivate::q_httpUserAgentChanged() { QString httpUserAgent = m_profile->httpUserAgent(); if (m_httpUserAgent == httpUserAgent) return; m_httpUserAgent = httpUserAgent; Q_EMIT httpUserAgentChanged(m_httpUserAgent); } void QWebEngineWebViewPrivate::QQuickWebEngineViewPtr::init() const { Q_ASSERT(!m_webEngineView); QObject *p = qobject_cast(m_parent); QQuickItem *parentItem = nullptr; while (p) { p = p->parent(); parentItem = qobject_cast(p); if (parentItem) break; } if (!parentItem) return; QQmlEngine *engine = qmlEngine(parentItem); if (!engine) return; QQmlComponent *component = new QQmlComponent(engine); component->setData(qmlSource(), QUrl::fromLocalFile(QLatin1String(""))); QQuickWebEngineView *webEngineView = qobject_cast(component->create()); Q_ASSERT(webEngineView); QQuickWebEngineProfile *profile = webEngineView->profile(); m_parent->m_profile = profile; // 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); QObject::connect(webEngineView, &QQuickWebEngineView::titleChanged, m_parent, &QWebEngineWebViewPrivate::q_titleChanged); QObject::connect(webEngineView, &QQuickWebEngineView::profileChanged,m_parent, &QWebEngineWebViewPrivate::q_profileChanged); QObject::connect(profile, &QQuickWebEngineProfile::httpUserAgentChanged, m_parent, &QWebEngineWebViewPrivate::q_httpUserAgentChanged); webEngineView->setParentItem(parentItem); m_webEngineView.reset(webEngineView); } QT_END_NAMESPACE