Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow headers to be sent with webContents.downloadURL() #39562

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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