From 595589197313178551cf7ccd645d2732643875bf Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 23 Jul 2018 17:17:40 +0200 Subject: Add QWebEngineDownloadItem page/view accessor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To be able to determine where a download was triggered. [ChangeLog][DownloadItem] Added a page/view accessor to tell were the download was triggered. Change-Id: I21843a545a3e0eb66f5e5fa8a50e77564f2118a7 Reviewed-by: Jüri Valdmann --- src/core/download_manager_delegate_qt.cpp | 23 +++++++++++++++++++--- src/core/profile_adapter_client.h | 3 +++ src/core/web_contents_adapter_client.h | 6 ++++++ src/core/web_contents_delegate_qt.h | 1 + src/webengine/api/qquickwebenginedownloaditem.cpp | 15 ++++++++++++++ src/webengine/api/qquickwebenginedownloaditem_p.h | 3 +++ .../api/qquickwebenginedownloaditem_p_p.h | 2 ++ src/webengine/api/qquickwebengineprofile.cpp | 4 ++++ src/webengine/api/qquickwebengineview_p_p.h | 1 + src/webengine/plugin/plugin.cpp | 2 ++ .../api/qwebenginedownloaditem.cpp | 12 +++++++++++ src/webenginewidgets/api/qwebenginedownloaditem.h | 3 +++ .../api/qwebenginedownloaditem_p.h | 1 + src/webenginewidgets/api/qwebenginepage_p.h | 1 + src/webenginewidgets/api/qwebengineprofile.cpp | 7 ++++++- 15 files changed, 80 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index 813657365..4ed77688e 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -168,6 +168,11 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem* item->AddObserver(this); QList clients = m_profileAdapter->clients(); if (!clients.isEmpty()) { + content::WebContents *webContents = content::DownloadItemUtils::GetWebContents(item); + WebContentsAdapterClient *adapterClient = nullptr; + if (webContents) + adapterClient = static_cast(webContents->GetDelegate())->adapterClient(); + ProfileAdapterClient::DownloadItemInfo info = { item->GetId(), toQt(item->GetURL()), @@ -181,7 +186,8 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(download::DownloadItem* false /* paused */, false /* done */, downloadType, - item->GetLastReason() + item->GetLastReason(), + adapterClient }; for (ProfileAdapterClient *client : qAsConst(clients)) { @@ -263,6 +269,10 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content // Clear the delegate's SavePageInfo. It's only valid for the page currently being saved. contentsDelegate->setSavePageInfo(SavePageInfo()); + WebContentsAdapterClient *adapterClient = nullptr; + if (web_contents) + adapterClient = static_cast(web_contents->GetDelegate())->adapterClient(); + ProfileAdapterClient::DownloadItemInfo info = { m_currentId + 1, toQt(web_contents->GetURL()), @@ -276,7 +286,8 @@ void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_content false, /* paused */ false, /* done */ ProfileAdapterClient::SavePage, - ProfileAdapterClient::NoReason + ProfileAdapterClient::NoReason, + adapterClient }; for (ProfileAdapterClient *client : qAsConst(clients)) { @@ -323,6 +334,11 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(download::DownloadItem *downlo { QList clients = m_profileAdapter->clients(); if (!clients.isEmpty()) { + WebContentsAdapterClient *adapterClient = nullptr; + content::WebContents *webContents = content::DownloadItemUtils::GetWebContents(download); + if (webContents) + adapterClient = static_cast(webContents->GetDelegate())->adapterClient(); + ProfileAdapterClient::DownloadItemInfo info = { download->GetId(), toQt(download->GetURL()), @@ -336,7 +352,8 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(download::DownloadItem *downlo download->IsPaused(), download->IsDone(), 0 /* downloadType (unused) */, - download->GetLastReason() + download->GetLastReason(), + adapterClient }; for (ProfileAdapterClient *client : qAsConst(clients)) { diff --git a/src/core/profile_adapter_client.h b/src/core/profile_adapter_client.h index 65b0aed6a..06051fab6 100644 --- a/src/core/profile_adapter_client.h +++ b/src/core/profile_adapter_client.h @@ -57,6 +57,8 @@ namespace QtWebEngineCore { +class WebContentsAdapterClient; + class QWEBENGINECORE_PRIVATE_EXPORT ProfileAdapterClient { public: @@ -133,6 +135,7 @@ public: bool done; int downloadType; int downloadInterruptReason; + WebContentsAdapterClient *page; }; virtual ~ProfileAdapterClient() { } diff --git a/src/core/web_contents_adapter_client.h b/src/core/web_contents_adapter_client.h index 2419a1259..851a6122f 100644 --- a/src/core/web_contents_adapter_client.h +++ b/src/core/web_contents_adapter_client.h @@ -394,6 +394,11 @@ public: KilledTerminationStatus }; + enum ClientType { + QmlClient, + WidgetsClient + }; + enum MediaRequestFlag { MediaNone = 0, MediaAudioCapture = 0x01, @@ -466,6 +471,7 @@ public: virtual bool isEnabled() const = 0; virtual const QObject *holdingQObject() const = 0; virtual void setToolTip(const QString& toolTipText) = 0; + virtual ClientType clientType() = 0; virtual ProfileAdapter *profileAdapter() = 0; virtual WebContentsAdapter* webContentsAdapter() = 0; diff --git a/src/core/web_contents_delegate_qt.h b/src/core/web_contents_delegate_qt.h index b2ca4e491..674e75fcd 100644 --- a/src/core/web_contents_delegate_qt.h +++ b/src/core/web_contents_delegate_qt.h @@ -154,6 +154,7 @@ public: WebEngineSettings *webEngineSettings() const; WebContentsAdapter *webContentsAdapter() const; + WebContentsAdapterClient *adapterClient() const { return m_viewClient; } private: QWeakPointer createWindow(content::WebContents *new_contents, WindowOpenDisposition disposition, const gfx::Rect& initial_pos, bool user_gesture); diff --git a/src/webengine/api/qquickwebenginedownloaditem.cpp b/src/webengine/api/qquickwebenginedownloaditem.cpp index f44351f4c..4dce4ecd9 100644 --- a/src/webengine/api/qquickwebenginedownloaditem.cpp +++ b/src/webengine/api/qquickwebenginedownloaditem.cpp @@ -107,6 +107,7 @@ QQuickWebEngineDownloadItemPrivate::QQuickWebEngineDownloadItemPrivate(QQuickWeb , receivedBytes(0) , downloadFinished(false) , downloadPaused(false) + , view(nullptr) { } @@ -591,6 +592,20 @@ bool QQuickWebEngineDownloadItem::isPaused() const return d->downloadPaused; } +/*! + \qmlproperty bool WebEngineDownloadItem::view + \readonly + \since QtWebEngine 1.8 + + Returns the view the download was requested on. If the download was not triggered by content in a view, + \c nullptr is returned. +*/ +QQuickWebEngineView *QQuickWebEngineDownloadItem::view() const +{ + Q_D(const QQuickWebEngineDownloadItem); + return d->view; +} + QQuickWebEngineDownloadItem::QQuickWebEngineDownloadItem(QQuickWebEngineDownloadItemPrivate *p, QObject *parent) : QObject(parent) , d_ptr(p) diff --git a/src/webengine/api/qquickwebenginedownloaditem_p.h b/src/webengine/api/qquickwebenginedownloaditem_p.h index 88be0dbd1..d19ca4828 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p.h @@ -60,6 +60,7 @@ QT_BEGIN_NAMESPACE class QQuickWebEngineDownloadItemPrivate; class QQuickWebEngineProfilePrivate; +class QQuickWebEngineView; class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineDownloadItem: public QObject { Q_OBJECT @@ -134,6 +135,7 @@ public: Q_PROPERTY(bool isFinished READ isFinished NOTIFY isFinishedChanged REVISION 5 FINAL) Q_PROPERTY(bool isPaused READ isPaused NOTIFY isPausedChanged REVISION 5 FINAL) Q_PROPERTY(bool isSavePageDownload READ isSavePageDownload CONSTANT REVISION 6 FINAL) + Q_PROPERTY(QQuickWebEngineView *view READ view CONSTANT REVISION 7 FINAL) Q_INVOKABLE void accept(); Q_INVOKABLE void cancel(); @@ -155,6 +157,7 @@ public: bool isFinished() const; bool isPaused() const; bool isSavePageDownload() const; + QQuickWebEngineView *view() const; Q_SIGNALS: void stateChanged(); diff --git a/src/webengine/api/qquickwebenginedownloaditem_p_p.h b/src/webengine/api/qquickwebenginedownloaditem_p_p.h index 631353738..4b89335bd 100644 --- a/src/webengine/api/qquickwebenginedownloaditem_p_p.h +++ b/src/webengine/api/qquickwebenginedownloaditem_p_p.h @@ -59,6 +59,7 @@ QT_BEGIN_NAMESPACE class QQuickWebEngineProfilePrivate; +class QQuickWebEngineView; class QQuickWebEngineDownloadItemPrivate { QQuickWebEngineDownloadItem *q_ptr; @@ -80,6 +81,7 @@ public: QString downloadPath; bool downloadFinished; bool downloadPaused; + QQuickWebEngineView *view; void update(const QtWebEngineCore::ProfileAdapterClient::DownloadItemInfo &info); void updateState(QQuickWebEngineDownloadItem::DownloadState newState); diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index f76741212..d11214716 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -222,6 +222,10 @@ void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->savePageFormat = static_cast( info.savePageFormat); itemPrivate->type = static_cast(info.downloadType); + if (info.page && info.page->clientType() == QtWebEngineCore::WebContentsAdapterClient::QmlClient) + itemPrivate->view = static_cast(info.page)->q_ptr; + else + itemPrivate->view = nullptr; QQuickWebEngineDownloadItem *download = new QQuickWebEngineDownloadItem(itemPrivate, q); diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h index 7f52c7216..7e5d1e64f 100644 --- a/src/webengine/api/qquickwebengineview_p_p.h +++ b/src/webengine/api/qquickwebengineview_p_p.h @@ -151,6 +151,7 @@ public: bool isEnabled() const override; void setToolTip(const QString &toolTipText) override; const QObject *holdingQObject() const override; + ClientType clientType() override { return QtWebEngineCore::WebContentsAdapterClient::QmlClient; } QtWebEngineCore::ProfileAdapter *profileAdapter() override; QtWebEngineCore::WebContentsAdapter *webContentsAdapter() override; diff --git a/src/webengine/plugin/plugin.cpp b/src/webengine/plugin/plugin.cpp index 76bee50be..84a12c930 100644 --- a/src/webengine/plugin/plugin.cpp +++ b/src/webengine/plugin/plugin.cpp @@ -112,6 +112,8 @@ public: tr("Cannot create a separate instance of WebEngineDownloadItem")); qmlRegisterUncreatableType(uri, 1, 7, "WebEngineDownloadItem", tr("Cannot create a separate instance of WebEngineDownloadItem")); + qmlRegisterUncreatableType(uri, 1, 8, "WebEngineDownloadItem", + tr("Cannot create a separate instance of WebEngineDownloadItem")); qmlRegisterUncreatableType(uri, 1, 1, "WebEngineNewViewRequest", msgUncreatableType("WebEngineNewViewRequest")); qmlRegisterUncreatableType(uri, 1, 5, "WebEngineNewViewRequest", tr("Cannot create separate instance of WebEngineNewViewRequest")); qmlRegisterUncreatableType(uri, 1, 1, "WebEngineSettings", tr("Cannot create a separate instance of WebEngineSettings")); diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.cpp b/src/webenginewidgets/api/qwebenginedownloaditem.cpp index 631c028c3..fc27e104d 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.cpp +++ b/src/webenginewidgets/api/qwebenginedownloaditem.cpp @@ -169,6 +169,7 @@ QWebEngineDownloadItemPrivate::QWebEngineDownloadItemPrivate(QWebEngineProfilePr , downloadPaused(false) , totalBytes(-1) , receivedBytes(0) + , page(0) { } @@ -630,6 +631,17 @@ QString QWebEngineDownloadItem::interruptReasonString() const static_cast(interruptReason())); } +/*! + \since 5.12 + Returns the page the download was requested on. If the download was not triggered by content in a page, + \c nullptr is returned. +*/ +QWebEnginePage *QWebEngineDownloadItem::page() const +{ + Q_D(const QWebEngineDownloadItem); + return d->page; +} + QWebEngineDownloadItem::QWebEngineDownloadItem(QWebEngineDownloadItemPrivate *p, QObject *parent) : QObject(parent) , d_ptr(p) diff --git a/src/webenginewidgets/api/qwebenginedownloaditem.h b/src/webenginewidgets/api/qwebenginedownloaditem.h index 073b97170..981a3c374 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem.h @@ -46,6 +46,7 @@ QT_BEGIN_NAMESPACE +class QWebEnginePage; class QWebEngineDownloadItemPrivate; class QWebEngineProfilePrivate; @@ -128,6 +129,8 @@ public: QString interruptReasonString() const; bool isSavePageDownload() const; + QWebEnginePage *page() const; + public Q_SLOTS: void accept(); void cancel(); diff --git a/src/webenginewidgets/api/qwebenginedownloaditem_p.h b/src/webenginewidgets/api/qwebenginedownloaditem_p.h index 514beacc7..bdcda5be6 100644 --- a/src/webenginewidgets/api/qwebenginedownloaditem_p.h +++ b/src/webenginewidgets/api/qwebenginedownloaditem_p.h @@ -81,6 +81,7 @@ public: qint64 totalBytes; qint64 receivedBytes; + QWebEnginePage *page; void update(const QtWebEngineCore::ProfileAdapterClient::DownloadItemInfo &info); }; diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h index fbaf0b579..e2329e8f8 100644 --- a/src/webenginewidgets/api/qwebenginepage_p.h +++ b/src/webenginewidgets/api/qwebenginepage_p.h @@ -147,6 +147,7 @@ public: bool isEnabled() const override; void setToolTip(const QString &toolTipText) override; const QObject *holdingQObject() const override; + ClientType clientType() override { return QtWebEngineCore::WebContentsAdapterClient::WidgetsClient; } QtWebEngineCore::ProfileAdapter *profileAdapter() override; QtWebEngineCore::WebContentsAdapter *webContentsAdapter() override; diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index a7a7ea19b..4523a425a 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -38,12 +38,13 @@ ****************************************************************************/ #include "qwebengineprofile.h" +#include "qwebengineprofile_p.h" #include "qwebenginecookiestore.h" #include "qwebenginedownloaditem.h" #include "qwebenginedownloaditem_p.h" #include "qwebenginepage.h" -#include "qwebengineprofile_p.h" +#include "qwebenginepage_p.h" #include "qwebenginesettings.h" #include "qwebenginescriptcollection_p.h" #include "qtwebenginecoreglobal.h" @@ -202,6 +203,10 @@ void QWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) itemPrivate->mimeType = info.mimeType; itemPrivate->savePageFormat = static_cast(info.savePageFormat); itemPrivate->type = static_cast(info.downloadType); + if (info.page && info.page->clientType() == QtWebEngineCore::WebContentsAdapterClient::WidgetsClient) + itemPrivate->page = static_cast(info.page)->q_ptr; + else + itemPrivate->page = nullptr; QWebEngineDownloadItem *download = new QWebEngineDownloadItem(itemPrivate, q); -- cgit v1.2.3