From 38f58ee4693272ca8e206dac8b484d012b683ebf Mon Sep 17 00:00:00 2001 From: Andras Becsi Date: Mon, 19 Jan 2015 13:27:41 +0100 Subject: Add DownloadItemInfo to BrowserContextAdapterClient This extends the carried information from Chromium's content::DownloadItem with url, totalBytes and receivedBytes in preparation of adding a Widget API for downloads. DownloadItemInfo struct is now constructed to carry information about individual downloads from the content layer to the Qt API layer. Change-Id: I3ee7aea02b74994e612e1b3709195776d5e7570b Reviewed-by: Allan Sandfeld Jensen --- src/core/browser_context_adapter_client.h | 18 ++++++++-- src/core/download_manager_delegate_qt.cpp | 43 ++++++++++++++++------- src/webengine/api/qquickwebenginedownloaditem.cpp | 3 +- src/webengine/api/qquickwebengineprofile.cpp | 31 ++++++++-------- src/webengine/api/qquickwebengineprofile_p_p.h | 4 +-- 5 files changed, 67 insertions(+), 32 deletions(-) diff --git a/src/core/browser_context_adapter_client.h b/src/core/browser_context_adapter_client.h index 2b6b4f434..1d84ffb45 100644 --- a/src/core/browser_context_adapter_client.h +++ b/src/core/browser_context_adapter_client.h @@ -39,6 +39,7 @@ #include "qtwebenginecoreglobal.h" #include +#include class QWEBENGINE_EXPORT BrowserContextAdapterClient { @@ -54,10 +55,23 @@ public: // This state indicates that the download has been interrupted. DownloadInterrupted }; + + struct DownloadItemInfo { + const quint32 id; + const QUrl url; + const int state; + const int percentComplete; + const qint64 totalBytes; + const qint64 receivedBytes; + + QString path; + bool cancelled; + }; + virtual ~BrowserContextAdapterClient() { } - virtual void downloadRequested(quint32 downloadId, QString &downloadPath, bool &cancelled) = 0; - virtual void downloadUpdated(quint32 downloadId, int downloadState, int percentComplete) = 0; + virtual void downloadRequested(DownloadItemInfo &info) = 0; + virtual void downloadUpdated(const DownloadItemInfo &info) = 0; }; #endif // BROWSER_CONTEXT_ADAPTER_CLIENT_H diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index 3f6ec7bcc..c46bfb080 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -54,8 +54,8 @@ #include "qtwebenginecoreglobal.h" DownloadManagerDelegateQt::DownloadManagerDelegateQt(BrowserContextAdapter *contextAdapter) - : m_currentId(0) - , m_contextAdapter(contextAdapter) + : m_contextAdapter(contextAdapter) + , m_currentId(0) { Q_ASSERT(m_contextAdapter); } @@ -120,18 +120,28 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i } item->AddObserver(this); - quint32 downloadId = item->GetId(); if (m_contextAdapter->client()) { bool cancelled = false; - m_contextAdapter->client()->downloadRequested(downloadId, suggestedFilePath, cancelled); - suggestedFile.setFile(suggestedFilePath); - - if (!cancelled && !suggestedFile.absoluteDir().mkpath(suggestedFile.absolutePath())) { + BrowserContextAdapterClient::DownloadItemInfo info = { + item->GetId(), + toQt(item->GetURL()), + item->GetState(), + item->PercentComplete(), + item->GetTotalBytes(), + item->GetReceivedBytes(), + suggestedFilePath, + cancelled + }; + m_contextAdapter->client()->downloadRequested(info); + + suggestedFile.setFile(info.path); + + if (!info.cancelled && !suggestedFile.absoluteDir().mkpath(suggestedFile.absolutePath())) { qWarning("Creating download path failed, download cancelled: %s", suggestedFile.absolutePath().toUtf8().data()); cancelled = true; } - if (cancelled) { + if (info.cancelled) { cancelDownload(callback); return true; } @@ -158,10 +168,19 @@ void DownloadManagerDelegateQt::GetSaveDir(content::BrowserContext* browser_cont void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *download) { - const quint32 downloadId = download->GetId(); - - if (m_contextAdapter->client()) - m_contextAdapter->client()->downloadUpdated(downloadId, download->GetState(), download->PercentComplete()); + if (m_contextAdapter->client()) { + BrowserContextAdapterClient::DownloadItemInfo info = { + download->GetId(), + toQt(download->GetURL()), + download->GetState(), + download->PercentComplete(), + download->GetTotalBytes(), + download->GetReceivedBytes(), + QString(), + false + }; + m_contextAdapter->client()->downloadUpdated(info); + } } void DownloadManagerDelegateQt::OnDownloadDestroyed(content::DownloadItem *download) diff --git a/src/webengine/api/qquickwebenginedownloaditem.cpp b/src/webengine/api/qquickwebenginedownloaditem.cpp index 729cb0d3f..967638b7e 100644 --- a/src/webengine/api/qquickwebenginedownloaditem.cpp +++ b/src/webengine/api/qquickwebenginedownloaditem.cpp @@ -50,8 +50,7 @@ QQuickWebEngineDownloadItemPrivate::QQuickWebEngineDownloadItemPrivate(QQuickWeb QQuickWebEngineDownloadItemPrivate::~QQuickWebEngineDownloadItemPrivate() { - Q_Q(QQuickWebEngineDownloadItem); - q->cancel(); + profile->downloadDestroyed(downloadId); } void QQuickWebEngineDownloadItemPrivate::update(QQuickWebEngineDownloadItem::DownloadState state, int progress) diff --git a/src/webengine/api/qquickwebengineprofile.cpp b/src/webengine/api/qquickwebengineprofile.cpp index 9eecae073..534df6fae 100644 --- a/src/webengine/api/qquickwebengineprofile.cpp +++ b/src/webengine/api/qquickwebengineprofile.cpp @@ -90,47 +90,50 @@ void QQuickWebEngineProfilePrivate::cancelDownload(quint32 downloadId) void QQuickWebEngineProfilePrivate::downloadDestroyed(quint32 downloadId) { m_ongoingDownloads.remove(downloadId); + cancelDownload(downloadId); } -void QQuickWebEngineProfilePrivate::downloadRequested(quint32 downloadId, QString &downloadPath, bool &cancelled) +void QQuickWebEngineProfilePrivate::downloadRequested(DownloadItemInfo &info) { Q_Q(QQuickWebEngineProfile); - Q_ASSERT(!m_ongoingDownloads.contains(downloadId)); + Q_ASSERT(!m_ongoingDownloads.contains(info.id)); QQuickWebEngineDownloadItemPrivate *itemPrivate = new QQuickWebEngineDownloadItemPrivate(this); - itemPrivate->downloadId = downloadId; + itemPrivate->downloadId = info.id; itemPrivate->downloadState = QQuickWebEngineDownloadItem::DownloadInProgress; - itemPrivate->downloadPath = downloadPath; + itemPrivate->downloadPath = info.path; QQuickWebEngineDownloadItem *download = new QQuickWebEngineDownloadItem(itemPrivate, q); - m_ongoingDownloads.insert(downloadId, download); + m_ongoingDownloads.insert(info.id, download); QQmlEngine::setObjectOwnership(download, QQmlEngine::JavaScriptOwnership); Q_EMIT q->downloadStarted(download); download->d_func()->downloadStarted = true; - downloadPath = download->path(); - cancelled = download->state() == QQuickWebEngineDownloadItem::DownloadCancelled; + info.path = download->path(); + info.cancelled = download->state() == QQuickWebEngineDownloadItem::DownloadCancelled; } -void QQuickWebEngineProfilePrivate::downloadUpdated(quint32 downloadId, int downloadState, int percentComplete) +void QQuickWebEngineProfilePrivate::downloadUpdated(const DownloadItemInfo &info) { + if (!m_ongoingDownloads.contains(info.id)) + return; + Q_Q(QQuickWebEngineProfile); - Q_ASSERT(m_ongoingDownloads.contains(downloadId)); - QQuickWebEngineDownloadItem* download = m_ongoingDownloads.value(downloadId).data(); + QQuickWebEngineDownloadItem* download = m_ongoingDownloads.value(info.id).data(); if (!download) { - cancelDownload(downloadId); + downloadDestroyed(info.id); return; } - download->d_func()->update(toDownloadState(downloadState), percentComplete); + download->d_func()->update(toDownloadState(info.state), info.percentComplete); - if (downloadState != BrowserContextAdapterClient::DownloadInProgress) { + if (info.state != BrowserContextAdapterClient::DownloadInProgress) { Q_EMIT q->downloadFinished(download); - m_ongoingDownloads.remove(downloadId); + m_ongoingDownloads.remove(info.id); } } diff --git a/src/webengine/api/qquickwebengineprofile_p_p.h b/src/webengine/api/qquickwebengineprofile_p_p.h index 18ccfc467..359db6ede 100644 --- a/src/webengine/api/qquickwebengineprofile_p_p.h +++ b/src/webengine/api/qquickwebengineprofile_p_p.h @@ -61,8 +61,8 @@ public: void cancelDownload(quint32 downloadId); void downloadDestroyed(quint32 downloadId); - void downloadRequested(quint32 downloadId, QString &downloadPath, bool &cancelled) Q_DECL_OVERRIDE; - void downloadUpdated(quint32 downloadId, int downloadState, int percentComplete) Q_DECL_OVERRIDE; + void downloadRequested(DownloadItemInfo &info) Q_DECL_OVERRIDE; + void downloadUpdated(const DownloadItemInfo &info) Q_DECL_OVERRIDE; private: friend class QQuickWebEngineViewPrivate; -- cgit v1.2.3