mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add permission callback for user-initated downloads (fixes issue #3183)
This change adds a CefDownloadHandler::CanDownload callback for optionally blocking user-initiated downloads (e.g. alt + link click or link click that returns a `Content-Disposition: attachment` response from the server). To test: - Run `ceftests --gtest_filter=DownloadTest.*`. - Run `cefclient --hide-controls`. User-initiated downloads will be blocked.
This commit is contained in:
@@ -1225,6 +1225,13 @@ bool AlloyBrowserHostImpl::HandleContextMenu(
|
||||
return HandleContextMenu(web_contents(), params);
|
||||
}
|
||||
|
||||
void AlloyBrowserHostImpl::CanDownload(
|
||||
const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) {
|
||||
contents_delegate_->CanDownload(url, request_method, std::move(callback));
|
||||
}
|
||||
|
||||
KeyboardEventProcessingResult AlloyBrowserHostImpl::PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event) {
|
||||
|
@@ -225,6 +225,9 @@ class AlloyBrowserHostImpl : public CefBrowserHostBase,
|
||||
bool TakeFocus(content::WebContents* source, bool reverse) override;
|
||||
bool HandleContextMenu(content::RenderFrameHost& render_frame_host,
|
||||
const content::ContextMenuParams& params) override;
|
||||
void CanDownload(const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) override;
|
||||
content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event) override;
|
||||
|
@@ -180,6 +180,23 @@ void CefBrowserContentsDelegate::ExitFullscreenModeForTab(
|
||||
OnFullscreenModeChange(/*fullscreen=*/false);
|
||||
}
|
||||
|
||||
void CefBrowserContentsDelegate::CanDownload(
|
||||
const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) {
|
||||
bool allow = true;
|
||||
|
||||
if (auto delegate = platform_delegate()) {
|
||||
if (auto c = client()) {
|
||||
if (auto handler = c->GetDownloadHandler()) {
|
||||
allow = handler->CanDownload(browser(), url.spec(), request_method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::move(callback).Run(allow);
|
||||
}
|
||||
|
||||
KeyboardEventProcessingResult
|
||||
CefBrowserContentsDelegate::PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
|
@@ -101,6 +101,9 @@ class CefBrowserContentsDelegate : public content::WebContentsDelegate,
|
||||
content::RenderFrameHost* requesting_frame,
|
||||
const blink::mojom::FullscreenOptions& options) override;
|
||||
void ExitFullscreenModeForTab(content::WebContents* web_contents) override;
|
||||
void CanDownload(const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) override;
|
||||
content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event) override;
|
||||
|
@@ -235,6 +235,20 @@ void ChromeBrowserDelegate::ExitFullscreenModeForTab(
|
||||
}
|
||||
}
|
||||
|
||||
void ChromeBrowserDelegate::CanDownload(
|
||||
const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) {
|
||||
auto source = browser_->tab_strip_model()->GetActiveWebContents();
|
||||
DCHECK(source);
|
||||
|
||||
if (auto delegate = GetDelegateForWebContents(source)) {
|
||||
delegate->CanDownload(url, request_method, std::move(callback));
|
||||
return;
|
||||
}
|
||||
std::move(callback).Run(true);
|
||||
}
|
||||
|
||||
KeyboardEventProcessingResult ChromeBrowserDelegate::PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event) {
|
||||
|
@@ -86,6 +86,9 @@ class ChromeBrowserDelegate : public cef::BrowserDelegate {
|
||||
content::RenderFrameHost* requesting_frame,
|
||||
const blink::mojom::FullscreenOptions& options) override;
|
||||
void ExitFullscreenModeForTab(content::WebContents* web_contents) override;
|
||||
void CanDownload(const GURL& url,
|
||||
const std::string& request_method,
|
||||
base::OnceCallback<void(bool)> callback) override;
|
||||
content::KeyboardEventProcessingResult PreHandleKeyboardEvent(
|
||||
content::WebContents* source,
|
||||
const content::NativeWebKeyboardEvent& event) override;
|
||||
|
Reference in New Issue
Block a user