From eed72c35d74947853ed75dd10e85d5ed56c1ce86 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 29 Aug 2019 05:27:20 +0200 Subject: [PATCH] feat: add session.downloadURL() (#19889) --- docs/api/session.md | 11 +++++++++++ shell/browser/api/atom_api_session.cc | 10 ++++++++++ shell/browser/api/atom_api_session.h | 1 + spec-main/api-session-spec.ts | 12 ++++++++++++ 4 files changed, 34 insertions(+) diff --git a/docs/api/session.md b/docs/api/session.md index 4ac52574837a..7eec76e226a5 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -408,6 +408,17 @@ Returns `String` - The user agent for this session. Returns `Promise` - resolves with blob data. +#### `ses.downloadURL(url)` + +* `url` String + +Initiates a download of the resource at `url`. +The API will generate a [DownloadItem](download-item.md) that can be accessed +with the [will-download](#event-will-download) event. + +**Note:** This does not perform any security checks that relate to a page's origin, +unlike [`webContents.downloadURL`](web-contents.md#contentsdownloadurlurl). + #### `ses.createInterruptedDownload(options)` * `options` Object diff --git a/shell/browser/api/atom_api_session.cc b/shell/browser/api/atom_api_session.cc index d6a047fcdbe2..c367c85686ff 100644 --- a/shell/browser/api/atom_api_session.cc +++ b/shell/browser/api/atom_api_session.cc @@ -19,6 +19,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/common/pref_names.h" #include "components/download/public/common/download_danger_type.h" +#include "components/download/public/common/download_url_parameters.h" #include "components/prefs/pref_service.h" #include "components/prefs/value_map_pref_store.h" #include "components/proxy_config/proxy_config_dictionary.h" @@ -510,6 +511,14 @@ v8::Local Session::GetBlobData(v8::Isolate* isolate, return handle; } +void Session::DownloadURL(const GURL& url) { + auto* download_manager = + content::BrowserContext::GetDownloadManager(browser_context()); + auto download_params = std::make_unique( + url, MISSING_TRAFFIC_ANNOTATION); + download_manager->DownloadUrl(std::move(download_params)); +} + void Session::CreateInterruptedDownload(const mate::Dictionary& options) { int64_t offset = 0, length = 0; double start_time = 0.0; @@ -694,6 +703,7 @@ void Session::BuildPrototype(v8::Isolate* isolate, .SetMethod("setUserAgent", &Session::SetUserAgent) .SetMethod("getUserAgent", &Session::GetUserAgent) .SetMethod("getBlobData", &Session::GetBlobData) + .SetMethod("downloadURL", &Session::DownloadURL) .SetMethod("createInterruptedDownload", &Session::CreateInterruptedDownload) .SetMethod("setPreloads", &Session::SetPreloads) diff --git a/shell/browser/api/atom_api_session.h b/shell/browser/api/atom_api_session.h index ebf467d3d463..7cf6548f9329 100644 --- a/shell/browser/api/atom_api_session.h +++ b/shell/browser/api/atom_api_session.h @@ -79,6 +79,7 @@ class Session : public mate::TrackableObject, std::string GetUserAgent(); v8::Local GetBlobData(v8::Isolate* isolate, const std::string& uuid); + void DownloadURL(const GURL& url); void CreateInterruptedDownload(const mate::Dictionary& options); void SetPreloads(const std::vector& preloads); std::vector GetPreloads() const; diff --git a/spec-main/api-session-spec.ts b/spec-main/api-session-spec.ts index 7f67294b6422..47f08ec16000 100644 --- a/spec-main/api-session-spec.ts +++ b/spec-main/api-session-spec.ts @@ -577,6 +577,18 @@ describe('session module', () => { fs.unlinkSync(downloadFilePath) } + it('can download using session.downloadURL', (done) => { + const port = address.port + session.defaultSession.once('will-download', function (e, item) { + item.savePath = downloadFilePath + item.on('done', function (e, state) { + assertDownload(state, item) + done() + }) + }) + session.defaultSession.downloadURL(`${url}:${port}`) + }) + it('can download using WebContents.downloadURL', (done) => { const port = address.port w.webContents.session.once('will-download', function (e, item) {