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:
Marshall Greenblatt
2022-03-22 17:40:28 -04:00
parent 2f5838eaaa
commit 6d7a680187
16 changed files with 280 additions and 38 deletions

View File

@@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=c4e47ffd023b528b9c5b658126f4a1d9fd05cf98$
// $hash=bb3a8a9a02dc3d2e4bdf0e926f61adc05e3af351$
//
#include "libcef_dll/ctocpp/download_handler_ctocpp.h"
@@ -21,6 +21,40 @@
// VIRTUAL METHODS - Body may be edited by hand.
NO_SANITIZE("cfi-icall")
bool CefDownloadHandlerCToCpp::CanDownload(CefRefPtr<CefBrowser> browser,
const CefString& url,
const CefString& request_method) {
shutdown_checker::AssertNotShutdown();
cef_download_handler_t* _struct = GetStruct();
if (CEF_MEMBER_MISSING(_struct, can_download))
return false;
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: browser; type: refptr_diff
DCHECK(browser.get());
if (!browser.get())
return false;
// Verify param: url; type: string_byref_const
DCHECK(!url.empty());
if (url.empty())
return false;
// Verify param: request_method; type: string_byref_const
DCHECK(!request_method.empty());
if (request_method.empty())
return false;
// Execute
int _retval =
_struct->can_download(_struct, CefBrowserCppToC::Wrap(browser),
url.GetStruct(), request_method.GetStruct());
// Return type: bool
return _retval ? true : false;
}
NO_SANITIZE("cfi-icall")
void CefDownloadHandlerCToCpp::OnBeforeDownload(
CefRefPtr<CefBrowser> browser,

View File

@@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=172a12dd9e68b65afff9eef5b93f0e480beaf904$
// $hash=fa32af3253cface693ec3ef420863852f5c68379$
//
#ifndef CEF_LIBCEF_DLL_CTOCPP_DOWNLOAD_HANDLER_CTOCPP_H_
@@ -35,6 +35,9 @@ class CefDownloadHandlerCToCpp
virtual ~CefDownloadHandlerCToCpp();
// CefDownloadHandler methods.
bool CanDownload(CefRefPtr<CefBrowser> browser,
const CefString& url,
const CefString& request_method) override;
void OnBeforeDownload(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,