Skip to content

Commit

Permalink
feat: allow headers to be sent with webContents.downloadURL() (elec…
Browse files Browse the repository at this point in the history
…tron#39455)

feat: allow headers to be sent with webContents.downloadURL()
  • Loading branch information
codebytere authored Aug 17, 2023
1 parent 31dfde7 commit 00746e6
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 221 deletions.
2 changes: 1 addition & 1 deletion docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ 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).
unlike [`webContents.downloadURL`](web-contents.md#contentsdownloadurlurl-options).
#### `ses.createInterruptedDownload(options)`
Expand Down
4 changes: 3 additions & 1 deletion docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1046,9 +1046,11 @@ const win = new BrowserWindow()
win.loadFile('src/index.html')
```

#### `contents.downloadURL(url)`
#### `contents.downloadURL(url[, options])`

* `url` string
* `options` Object (optional)
* `headers` Record<string, string> (optional) - HTTP request headers.

Initiates a download of the resource at `url` without navigating. The
`will-download` event of `session` will be triggered.
Expand Down
4 changes: 3 additions & 1 deletion docs/api/webview-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,11 @@ if the page fails to load (see
Loads the `url` in the webview, the `url` must contain the protocol prefix,
e.g. the `http://` or `file://`.

### `<webview>.downloadURL(url)`
### `<webview>.downloadURL(url[, options])`

* `url` string
* `options` Object (optional)
* `headers` Record<string, string> (optional) - HTTP request headers.

Initiates a download of the resource at `url` without navigating.

Expand Down
19 changes: 16 additions & 3 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2444,12 +2444,25 @@ void WebContents::ReloadIgnoringCache() {
/* check_for_repost */ true);
}

void WebContents::DownloadURL(const GURL& url) {
auto* browser_context = web_contents()->GetBrowserContext();
auto* download_manager = browser_context->GetDownloadManager();
void WebContents::DownloadURL(const GURL& url, gin::Arguments* args) {
std::map<std::string, std::string> headers;
gin_helper::Dictionary options;
if (args->GetNext(&options)) {
if (options.Has("headers") && !options.Get("headers", &headers)) {
args->ThrowTypeError("Invalid value for headers - must be an object");
return;
}
}

std::unique_ptr<download::DownloadUrlParameters> download_params(
content::DownloadRequestUtils::CreateDownloadForWebContentsMainFrame(
web_contents(), url, MISSING_TRAFFIC_ANNOTATION));
for (const auto& [name, value] : headers) {
download_params->add_request_header(name, value);
}

auto* download_manager =
web_contents()->GetBrowserContext()->GetDownloadManager();
download_manager->DownloadUrl(std::move(download_params));
}

Expand Down
2 changes: 1 addition & 1 deletion shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class WebContents : public ExclusiveAccessContext,
void LoadURL(const GURL& url, const gin_helper::Dictionary& options);
void Reload();
void ReloadIgnoringCache();
void DownloadURL(const GURL& url);
void DownloadURL(const GURL& url, gin::Arguments* args);
GURL GetURL() const;
std::u16string GetTitle() const;
bool IsLoading() const;
Expand Down
Loading

0 comments on commit 00746e6

Please sign in to comment.