Skip to content

Commit

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

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Aug 24, 2023
1 parent 1e70e54 commit bf1dbaf
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
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
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
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
Expand Up @@ -2462,12 +2462,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
Expand Up @@ -172,7 +172,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

0 comments on commit bf1dbaf

Please sign in to comment.