From f2d938962fe9a029ec0baf37ca7f478051125780 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCri=20Valdmann?= <juri.valdmann@qt.io>
Date: Mon, 14 Aug 2017 10:25:56 +0200
Subject: [PATCH] Fix QWebEngineDownloadItem::type()

Task-number: QTBUG-62640
Change-Id: I2b16f24533b38c20a7071319723382ba240e35f3
Reviewed-by: Peter Varga <pvarga@inf.u-szeged.hu>
---
 src/core/download_manager_delegate_qt.cpp     | 21 ++++++++++++-------
 src/core/download_manager_delegate_qt.h       |  4 ++--
 src/core/web_contents_adapter.cpp             |  2 +-
 .../tst_qwebenginedownloads.cpp               |  8 +++----
 4 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp
index 77469a8ea..0eabd340c 100644
--- a/src/core/download_manager_delegate_qt.cpp
+++ b/src/core/download_manager_delegate_qt.cpp
@@ -65,7 +65,7 @@ DownloadManagerDelegateQt::DownloadManagerDelegateQt(BrowserContextAdapter *cont
     : m_contextAdapter(contextAdapter)
     , m_currentId(0)
     , m_weakPtrFactory(this)
-    , m_downloadType(BrowserContextAdapterClient::Attachment)
+    , m_nextDownloadIsUserRequested(false)
 {
     Q_ASSERT(m_contextAdapter);
 }
@@ -107,10 +107,17 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i
     QString suggestedFilename = toQt(item->GetSuggestedFilename());
     QString mimeTypeString = toQt(item->GetMimeType());
 
-    bool isAttachment = net::HttpContentDisposition(item->GetContentDisposition(), std::string()).is_attachment();
-
-    if (!isAttachment || !BrowserContextAdapterClient::UserRequested)
-        m_downloadType = BrowserContextAdapterClient::DownloadAttribute;
+    int downloadType = 0;
+    if (m_nextDownloadIsUserRequested) {
+        downloadType = BrowserContextAdapterClient::UserRequested;
+        m_nextDownloadIsUserRequested = false;
+    } else {
+        bool isAttachment = net::HttpContentDisposition(item->GetContentDisposition(), std::string()).is_attachment();
+        if (isAttachment)
+            downloadType = BrowserContextAdapterClient::Attachment;
+        else
+            downloadType = BrowserContextAdapterClient::DownloadAttribute;
+    }
 
     if (suggestedFilename.isEmpty())
         suggestedFilename = toQt(net::HttpContentDisposition(item->GetContentDisposition(), std::string()).filename());
@@ -155,7 +162,7 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i
             suggestedFilePath,
             BrowserContextAdapterClient::UnknownSavePageFormat,
             false /* accepted */,
-            m_downloadType,
+            downloadType,
             item->GetLastReason()
         };
 
@@ -283,7 +290,7 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *downloa
             QString(),
             BrowserContextAdapterClient::UnknownSavePageFormat,
             true /* accepted */,
-            m_downloadType,
+            0 /* downloadType (unused) */,
             download->GetLastReason()
         };
 
diff --git a/src/core/download_manager_delegate_qt.h b/src/core/download_manager_delegate_qt.h
index cd21d330c..d23a78b0b 100644
--- a/src/core/download_manager_delegate_qt.h
+++ b/src/core/download_manager_delegate_qt.h
@@ -84,7 +84,7 @@ public:
 
     void cancelDownload(quint32 downloadId);
 
-    void setDownloadType(int downloadType) { m_downloadType = downloadType; }
+    void markNextDownloadAsUserRequested() { m_nextDownloadIsUserRequested = true; }
 
     // Inherited from content::DownloadItem::Observer
     void OnDownloadUpdated(content::DownloadItem *download) override;
@@ -97,7 +97,7 @@ private:
 
     uint64_t m_currentId;
     base::WeakPtrFactory<DownloadManagerDelegateQt> m_weakPtrFactory;
-    int m_downloadType;
+    bool m_nextDownloadIsUserRequested;
 
     friend class DownloadManagerDelegateInstance;
     DISALLOW_COPY_AND_ASSIGN(DownloadManagerDelegateQt);
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index ff4f09f0e..b301622d4 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -942,7 +942,7 @@ void WebContentsAdapter::download(const QUrl &url, const QString &suggestedFileN
     if (!dlm)
         return;
 
-    dlmd->setDownloadType(BrowserContextAdapterClient::UserRequested);
+    dlmd->markNextDownloadAsUserRequested();
     dlm->SetDelegate(dlmd);
 
     GURL gurl = toGurl(url);
diff --git a/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp b/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
index c25e82d41..aba5bdeb1 100644
--- a/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
+++ b/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
@@ -86,7 +86,7 @@ void tst_QWebEngineDownloads::downloadLink_data()
         /* fileDisposition            */ << QByteArrayLiteral("")
         /* fileHasReferer             */ << true
         /* fileAction                 */ << FileIsDownloaded
-        /* downloadType               */ << QWebEngineDownloadItem::DownloadAttribute;
+        /* downloadType               */ << QWebEngineDownloadItem::UserRequested;
 
     // SaveLink should always trigger a download, also for text files.
     QTest::newRow("save link to text file")
@@ -99,7 +99,7 @@ void tst_QWebEngineDownloads::downloadLink_data()
         /* fileDisposition            */ << QByteArrayLiteral("")
         /* fileHasReferer             */ << true
         /* fileAction                 */ << FileIsDownloaded
-        /* downloadType               */ << QWebEngineDownloadItem::DownloadAttribute;
+        /* downloadType               */ << QWebEngineDownloadItem::UserRequested;
 
     // ... adding the "download" attribute should have no effect.
     QTest::newRow("save link to text file (attribute)")
@@ -112,7 +112,7 @@ void tst_QWebEngineDownloads::downloadLink_data()
         /* fileDisposition            */ << QByteArrayLiteral("")
         /* fileHasReferer             */ << true
         /* fileAction                 */ << FileIsDownloaded
-        /* downloadType               */ << QWebEngineDownloadItem::DownloadAttribute;
+        /* downloadType               */ << QWebEngineDownloadItem::UserRequested;
 
     // ... adding the "attachment" content disposition should also have no effect.
     QTest::newRow("save link to text file (attachment)")
@@ -508,7 +508,7 @@ void tst_QWebEngineDownloads::downloadTwoLinks()
         QCOMPARE(item->totalBytes(), -1);
         QCOMPARE(item->receivedBytes(), 0);
         QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason);
-        QCOMPARE(item->type(), QWebEngineDownloadItem::DownloadAttribute);
+        QCOMPARE(item->type(), QWebEngineDownloadItem::Attachment);
         QCOMPARE(item->mimeType(), QStringLiteral("text/plain"));
         QCOMPARE(item->path(), standardDir + QByteArrayLiteral("/file2"));
         QCOMPARE(item->savePageFormat(), QWebEngineDownloadItem::UnknownSaveFormat);
-- 
GitLab