feat: add session.downloadURL() (#19889)

This commit is contained in:
Milan Burda 2019-08-29 05:27:20 +02:00 committed by Shelley Vohr
parent 79e936aea8
commit eed72c35d7
4 changed files with 34 additions and 0 deletions

View file

@ -408,6 +408,17 @@ Returns `String` - The user agent for this session.
Returns `Promise<Buffer>` - resolves with blob data. Returns `Promise<Buffer>` - 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)` #### `ses.createInterruptedDownload(options)`
* `options` Object * `options` Object

View file

@ -19,6 +19,7 @@
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "components/download/public/common/download_danger_type.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/pref_service.h"
#include "components/prefs/value_map_pref_store.h" #include "components/prefs/value_map_pref_store.h"
#include "components/proxy_config/proxy_config_dictionary.h" #include "components/proxy_config/proxy_config_dictionary.h"
@ -510,6 +511,14 @@ v8::Local<v8::Promise> Session::GetBlobData(v8::Isolate* isolate,
return handle; return handle;
} }
void Session::DownloadURL(const GURL& url) {
auto* download_manager =
content::BrowserContext::GetDownloadManager(browser_context());
auto download_params = std::make_unique<download::DownloadUrlParameters>(
url, MISSING_TRAFFIC_ANNOTATION);
download_manager->DownloadUrl(std::move(download_params));
}
void Session::CreateInterruptedDownload(const mate::Dictionary& options) { void Session::CreateInterruptedDownload(const mate::Dictionary& options) {
int64_t offset = 0, length = 0; int64_t offset = 0, length = 0;
double start_time = 0.0; double start_time = 0.0;
@ -694,6 +703,7 @@ void Session::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setUserAgent", &Session::SetUserAgent) .SetMethod("setUserAgent", &Session::SetUserAgent)
.SetMethod("getUserAgent", &Session::GetUserAgent) .SetMethod("getUserAgent", &Session::GetUserAgent)
.SetMethod("getBlobData", &Session::GetBlobData) .SetMethod("getBlobData", &Session::GetBlobData)
.SetMethod("downloadURL", &Session::DownloadURL)
.SetMethod("createInterruptedDownload", .SetMethod("createInterruptedDownload",
&Session::CreateInterruptedDownload) &Session::CreateInterruptedDownload)
.SetMethod("setPreloads", &Session::SetPreloads) .SetMethod("setPreloads", &Session::SetPreloads)

View file

@ -79,6 +79,7 @@ class Session : public mate::TrackableObject<Session>,
std::string GetUserAgent(); std::string GetUserAgent();
v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate, v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
const std::string& uuid); const std::string& uuid);
void DownloadURL(const GURL& url);
void CreateInterruptedDownload(const mate::Dictionary& options); void CreateInterruptedDownload(const mate::Dictionary& options);
void SetPreloads(const std::vector<base::FilePath::StringType>& preloads); void SetPreloads(const std::vector<base::FilePath::StringType>& preloads);
std::vector<base::FilePath::StringType> GetPreloads() const; std::vector<base::FilePath::StringType> GetPreloads() const;

View file

@ -577,6 +577,18 @@ describe('session module', () => {
fs.unlinkSync(downloadFilePath) 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) => { it('can download using WebContents.downloadURL', (done) => {
const port = address.port const port = address.port
w.webContents.session.once('will-download', function (e, item) { w.webContents.session.once('will-download', function (e, item) {