Skip to content

Commit

Permalink
feat: add session.downloadURL()
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Aug 22, 2019
1 parent 991a561 commit 6c4ab91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/api/session.md
Expand Up @@ -386,6 +386,14 @@ Returns `String` - The user agent for this session.

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.

#### `ses.createInterruptedDownload(options)`

* `options` Object
Expand Down
10 changes: 10 additions & 0 deletions shell/browser/api/atom_api_session.cc
Expand Up @@ -18,6 +18,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"
Expand Down Expand Up @@ -506,6 +507,14 @@ v8::Local<v8::Promise> 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<download::DownloadUrlParameters>(
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;
Expand Down Expand Up @@ -654,6 +663,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)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/atom_api_session.h
Expand Up @@ -79,6 +79,7 @@ class Session : public mate::TrackableObject<Session>,
std::string GetUserAgent();
v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
const std::string& uuid);
void DownloadURL(const GURL& url);
void CreateInterruptedDownload(const mate::Dictionary& options);
void SetPreloads(const std::vector<base::FilePath::StringType>& preloads);
std::vector<base::FilePath::StringType> GetPreloads() const;
Expand Down
12 changes: 12 additions & 0 deletions spec-main/api-session-spec.js
Expand Up @@ -595,6 +595,18 @@ describe('session module', () => {
fs.unlinkSync(downloadFilePath)
}

it('can download using session.downloadURL', (done) => {
const port = downloadServer.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 = downloadServer.address().port
w.webContents.session.once('will-download', function (e, item) {
Expand Down

0 comments on commit 6c4ab91

Please sign in to comment.