diff options
author | Joerg Bornemann <joerg.bornemann@theqtcompany.com> | 2015-11-16 17:51:01 +0100 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@theqtcompany.com> | 2015-12-11 06:37:32 +0000 |
commit | 9d18f8d02b73c2c9310b772c972ea1e0d1356102 (patch) | |
tree | 7cc8c93950db0797916146620c6fb2e56ad0acc4 /src/core | |
parent | 222faea4af492f3286e9adca9c3d57a0f553a52a (diff) |
Add web action for saving the current web page
Add the possibility to save web pages as single HTML file,
complete HTML (with resource directory) or MHTML archive.
Change-Id: Ic7e7cfda9432f3534c13350a6369d79bb17fd8b3
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@theqtcompany.com>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/browser_context_adapter_client.h | 9 | ||||
-rw-r--r-- | src/core/download_manager_delegate_qt.cpp | 59 | ||||
-rw-r--r-- | src/core/download_manager_delegate_qt.h | 9 | ||||
-rw-r--r-- | src/core/web_contents_adapter.cpp | 7 | ||||
-rw-r--r-- | src/core/web_contents_adapter.h | 1 |
5 files changed, 85 insertions, 0 deletions
diff --git a/src/core/browser_context_adapter_client.h b/src/core/browser_context_adapter_client.h index 4a57b75c4..d237b25a1 100644 --- a/src/core/browser_context_adapter_client.h +++ b/src/core/browser_context_adapter_client.h @@ -58,6 +58,14 @@ public: DownloadInterrupted }; + // Keep in sync with content::SavePageType + enum SavePageFormat { + UnknownSavePageFormat = -1, + SingleHtmlSaveFormat, + CompleteHtmlSaveFormat, + MimeHtmlSaveFormat + }; + struct DownloadItemInfo { const quint32 id; const QUrl url; @@ -66,6 +74,7 @@ public: const qint64 receivedBytes; QString path; + int savePageFormat; bool accepted; }; diff --git a/src/core/download_manager_delegate_qt.cpp b/src/core/download_manager_delegate_qt.cpp index e9af98fd8..b6de27ca8 100644 --- a/src/core/download_manager_delegate_qt.cpp +++ b/src/core/download_manager_delegate_qt.cpp @@ -61,9 +61,15 @@ ASSERT_ENUMS_MATCH(content::DownloadItem::COMPLETE, BrowserContextAdapterClient: ASSERT_ENUMS_MATCH(content::DownloadItem::CANCELLED, BrowserContextAdapterClient::DownloadCancelled) ASSERT_ENUMS_MATCH(content::DownloadItem::INTERRUPTED, BrowserContextAdapterClient::DownloadInterrupted) +ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_UNKNOWN, BrowserContextAdapterClient::UnknownSavePageFormat) +ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_ONLY_HTML, BrowserContextAdapterClient::SingleHtmlSaveFormat) +ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, BrowserContextAdapterClient::CompleteHtmlSaveFormat) +ASSERT_ENUMS_MATCH(content::SAVE_PAGE_TYPE_AS_MHTML, BrowserContextAdapterClient::MimeHtmlSaveFormat) + DownloadManagerDelegateQt::DownloadManagerDelegateQt(BrowserContextAdapter *contextAdapter) : m_contextAdapter(contextAdapter) , m_currentId(0) + , m_weakPtrFactory(this) { Q_ASSERT(m_contextAdapter); } @@ -140,6 +146,7 @@ bool DownloadManagerDelegateQt::DetermineDownloadTarget(content::DownloadItem* i item->GetTotalBytes(), item->GetReceivedBytes(), suggestedFilePath, + BrowserContextAdapterClient::UnknownSavePageFormat, false /* accepted */ }; @@ -181,6 +188,57 @@ void DownloadManagerDelegateQt::GetSaveDir(content::BrowserContext* browser_cont *skip_dir_check = true; } +void DownloadManagerDelegateQt::ChooseSavePath(content::WebContents *web_contents, + const base::FilePath &suggested_path, + const base::FilePath::StringType &default_extension, + bool can_save_as_complete, + const content::SavePackagePathPickedCallback &callback) +{ + Q_UNUSED(default_extension); + Q_UNUSED(can_save_as_complete); + + QList<BrowserContextAdapterClient*> clients = m_contextAdapter->clients(); + if (clients.isEmpty()) + return; + + const QString suggestedFileName + = QFileInfo(toQt(suggested_path.AsUTF8Unsafe())).completeBaseName() + + QStringLiteral(".mhtml"); + const QDir defaultDownloadDirectory + = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); + const QString suggestedFilePath = defaultDownloadDirectory.absoluteFilePath(suggestedFileName); + + BrowserContextAdapterClient::DownloadItemInfo info = { + m_currentId + 1, + toQt(web_contents->GetURL()), + content::DownloadItem::IN_PROGRESS, + 0, /* totalBytes */ + 0, /* receivedBytes */ + suggestedFilePath, + BrowserContextAdapterClient::MimeHtmlSaveFormat, + false /* accepted */ + }; + + Q_FOREACH (BrowserContextAdapterClient *client, clients) { + client->downloadRequested(info); + if (info.accepted) + break; + } + + if (!info.accepted) + return; + + callback.Run(toFilePath(info.path), static_cast<content::SavePageType>(info.savePageFormat), + base::Bind(&DownloadManagerDelegateQt::savePackageDownloadCreated, + m_weakPtrFactory.GetWeakPtr())); +} + +void DownloadManagerDelegateQt::savePackageDownloadCreated(content::DownloadItem *item) +{ + OnDownloadUpdated(item); + item->AddObserver(this); +} + void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *download) { QList<BrowserContextAdapterClient*> clients = m_contextAdapter->clients(); @@ -192,6 +250,7 @@ void DownloadManagerDelegateQt::OnDownloadUpdated(content::DownloadItem *downloa download->GetTotalBytes(), download->GetReceivedBytes(), QString(), + BrowserContextAdapterClient::UnknownSavePageFormat, true /* accepted */ }; diff --git a/src/core/download_manager_delegate_qt.h b/src/core/download_manager_delegate_qt.h index fea965749..700c2f5a7 100644 --- a/src/core/download_manager_delegate_qt.h +++ b/src/core/download_manager_delegate_qt.h @@ -38,6 +38,7 @@ #define DOWNLOAD_MANAGER_DELEGATE_QT_H #include "content/public/browser/download_manager_delegate.h" +#include <base/memory/weak_ptr.h> #include <QtCore/qcompilerdetection.h> // Needed for Q_DECL_OVERRIDE @@ -72,6 +73,12 @@ public: base::FilePath* website_save_dir, base::FilePath* download_save_dir, bool* skip_dir_check) Q_DECL_OVERRIDE; + void ChooseSavePath(content::WebContents *web_contents, + const base::FilePath &suggested_path, + const base::FilePath::StringType &default_extension, + bool can_save_as_complete, + const content::SavePackagePathPickedCallback &callback) Q_DECL_OVERRIDE; + void cancelDownload(quint32 downloadId); @@ -81,9 +88,11 @@ public: private: void cancelDownload(const content::DownloadTargetCallback& callback); + void savePackageDownloadCreated(content::DownloadItem *download); BrowserContextAdapter *m_contextAdapter; uint64 m_currentId; + base::WeakPtrFactory<DownloadManagerDelegateQt> m_weakPtrFactory; 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 84c4e50f3..543ad24cb 100644 --- a/src/core/web_contents_adapter.cpp +++ b/src/core/web_contents_adapter.cpp @@ -44,6 +44,7 @@ #include "browser_accessibility_qt.h" #include "browser_context_adapter.h" #include "browser_context_qt.h" +#include "download_manager_delegate_qt.h" #include "media_capture_devices_dispatcher.h" #include "qwebenginecallback_p.h" #include "render_view_observer_host_qt.h" @@ -489,6 +490,12 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT d->webContents->Focus(); } +void WebContentsAdapter::save() +{ + Q_D(WebContentsAdapter); + d->webContents->OnSavePage(); +} + QUrl WebContentsAdapter::activeUrl() const { Q_D(const WebContentsAdapter); diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h index b6a90d3f1..df9cbb266 100644 --- a/src/core/web_contents_adapter.h +++ b/src/core/web_contents_adapter.h @@ -77,6 +77,7 @@ public: void reloadAndBypassCache(); void load(const QUrl&); void setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl); + void save(); QUrl activeUrl() const; QUrl requestedUrl() const; QString pageTitle() const; |