Skip to content

Commit

Permalink
chrome: Add CefDownloadHandler support (see #3681)
Browse files Browse the repository at this point in the history
Behaves the same as Alloy runtime except that CanDownload is not
called for invalid protocol schemes.
  • Loading branch information
magreenblatt committed Apr 27, 2024
1 parent ed07979 commit be6642d
Show file tree
Hide file tree
Showing 23 changed files with 873 additions and 616 deletions.
4 changes: 4 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ source_set("libcef_static") {
"libcef/browser/alloy/alloy_browser_main.h",
"libcef/browser/alloy/alloy_content_browser_client.cc",
"libcef/browser/alloy/alloy_content_browser_client.h",
"libcef/browser/alloy/alloy_download_manager_delegate.cc",
"libcef/browser/alloy/alloy_download_manager_delegate.h",
"libcef/browser/alloy/alloy_download_util.cc",
"libcef/browser/alloy/alloy_download_util.h",
"libcef/browser/alloy/alloy_web_contents_view_delegate.cc",
Expand Down Expand Up @@ -576,6 +578,8 @@ source_set("libcef_static") {
"libcef/browser/download_item_impl.h",
"libcef/browser/download_manager_delegate.cc",
"libcef/browser/download_manager_delegate.h",
"libcef/browser/download_manager_delegate_impl.cc",
"libcef/browser/download_manager_delegate_impl.h",
"libcef/browser/extension_impl.cc",
"libcef/browser/extension_impl.h",
"libcef/browser/extensions/api/file_system/cef_file_system_delegate.cc",
Expand Down
13 changes: 7 additions & 6 deletions include/capi/cef_download_handler_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=60a08a60be70e8fe5df17f18f8e5758e1830d5e1$
// $hash=7ecfb07a95315ff81937e9f68d419122fc88f1b7$
//

#ifndef CEF_INCLUDE_CAPI_CEF_DOWNLOAD_HANDLER_CAPI_H_
Expand Down Expand Up @@ -117,12 +117,13 @@ typedef struct _cef_download_handler_t {

///
/// Called before a download begins. |suggested_name| is the suggested name
/// for the download file. By default the download will be canceled. Execute
/// |callback| either asynchronously or in this function to continue the
/// download if desired. Do not keep a reference to |download_item| outside of
/// this function.
/// for the download file. Return true (1) and execute |callback| either
/// asynchronously or in this function to continue or cancel the download.
/// Return false (0) to proceed with default handling (cancel with Alloy
/// style, download shelf with Chrome style). Do not keep a reference to
/// |download_item| outside of this function.
///
void(CEF_CALLBACK* on_before_download)(
int(CEF_CALLBACK* on_before_download)(
struct _cef_download_handler_t* self,
struct _cef_browser_t* browser,
struct _cef_download_item_t* download_item,
Expand Down
8 changes: 4 additions & 4 deletions include/cef_api_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
// way that may cause binary incompatibility with other builds. The universal
// hash value will change if any platform is affected whereas the platform hash
// values will change only if that particular platform is affected.
#define CEF_API_HASH_UNIVERSAL "faa8a52df058a26e1228da66052a9900a7120de4"
#define CEF_API_HASH_UNIVERSAL "73e8743408bfeba0df3b567bfd04b2eb642df11d"
#if defined(OS_WIN)
#define CEF_API_HASH_PLATFORM "41a15a683e4a2b927f043c22d8a9f07671168170"
#define CEF_API_HASH_PLATFORM "3a73f4c69062c6f413e00c1821f6e6df91a2a787"
#elif defined(OS_MAC)
#define CEF_API_HASH_PLATFORM "6933ff4566b919c045ff0743682889d45ffcba28"
#define CEF_API_HASH_PLATFORM "f5fb92747082f06a9c42724b0b717accc29e3879"
#elif defined(OS_LINUX)
#define CEF_API_HASH_PLATFORM "b12778ab8fd0974e45a246484a31d12f097c7936"
#define CEF_API_HASH_PLATFORM "8775d620c6b17725bddba2c1c3a21353b4d8acb1"
#endif

#ifdef __cplusplus
Expand Down
20 changes: 11 additions & 9 deletions include/cef_download_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,19 @@ class CefDownloadHandler : public virtual CefBaseRefCounted {

///
/// Called before a download begins. |suggested_name| is the suggested name
/// for the download file. By default the download will be canceled. Execute
/// |callback| either asynchronously or in this method to continue the
/// download if desired. Do not keep a reference to |download_item| outside of
/// this method.
/// for the download file. Return true and execute |callback| either
/// asynchronously or in this method to continue or cancel the download.
/// Return false to proceed with default handling (cancel with Alloy style,
/// download shelf with Chrome style). Do not keep a reference to
/// |download_item| outside of this method.
///
/*--cef()--*/
virtual void OnBeforeDownload(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback) = 0;
virtual bool OnBeforeDownload(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback) {
return false;
}

///
/// Called when a download's status or progress information has been updated.
Expand Down
4 changes: 2 additions & 2 deletions libcef/browser/alloy/alloy_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <memory>
#include <utility>

#include "libcef/browser/download_manager_delegate.h"
#include "libcef/browser/alloy/alloy_download_manager_delegate.h"
#include "libcef/browser/extensions/extension_system.h"
#include "libcef/browser/prefs/browser_prefs.h"
#include "libcef/browser/ssl_host_state_delegate.h"
Expand Down Expand Up @@ -336,7 +336,7 @@ content::DownloadManagerDelegate*
AlloyBrowserContext::GetDownloadManagerDelegate() {
if (!download_manager_delegate_) {
download_manager_delegate_ =
std::make_unique<CefDownloadManagerDelegate>(GetDownloadManager());
std::make_unique<AlloyDownloadManagerDelegate>(GetDownloadManager());
}
return download_manager_delegate_.get();
}
Expand Down
4 changes: 2 additions & 2 deletions libcef/browser/alloy/alloy_browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "components/proxy_config/pref_proxy_config_tracker.h"
#include "components/visitedlink/browser/visitedlink_delegate.h"

class CefDownloadManagerDelegate;
class AlloyDownloadManagerDelegate;
class CefSSLHostStateDelegate;
class CefVisitedLinkListener;
class PrefService;
Expand Down Expand Up @@ -128,7 +128,7 @@ class AlloyBrowserContext : public ChromeProfileAlloy,
std::unique_ptr<PrefService> pref_service_;
std::unique_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;

std::unique_ptr<CefDownloadManagerDelegate> download_manager_delegate_;
std::unique_ptr<AlloyDownloadManagerDelegate> download_manager_delegate_;
std::unique_ptr<CefSSLHostStateDelegate> ssl_host_state_delegate_;
std::unique_ptr<visitedlink::VisitedLinkWriter> visitedlink_master_;
// |visitedlink_listener_| is owned by visitedlink_master_.
Expand Down
22 changes: 22 additions & 0 deletions libcef/browser/alloy/alloy_download_manager_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "libcef/browser/alloy/alloy_download_manager_delegate.h"

#include "chrome/common/chrome_constants.h"
#include "components/download/public/common/download_item.h"

AlloyDownloadManagerDelegate::AlloyDownloadManagerDelegate(
content::DownloadManager* manager)
: CefDownloadManagerDelegateImpl(manager, /*alloy_bootstrap=*/true) {}

void AlloyDownloadManagerDelegate::GetNextId(
content::DownloadIdCallback callback) {
static uint32_t next_id = download::DownloadItem::kInvalidId + 1;
std::move(callback).Run(next_id++);
}

std::string AlloyDownloadManagerDelegate::ApplicationClientIdForFileScanning() {
return std::string(chrome::kApplicationClientIDStringForAVScanning);
}
26 changes: 26 additions & 0 deletions libcef/browser/alloy/alloy_download_manager_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CEF_LIBCEF_BROWSER_ALLOY_ALLOY_DOWNLOAD_MANAGER_DELEGATE_H_
#define CEF_LIBCEF_BROWSER_ALLOY_ALLOY_DOWNLOAD_MANAGER_DELEGATE_H_
#pragma once

#include "libcef/browser/download_manager_delegate_impl.h"

// Specialization for the Alloy bootstrap.
class AlloyDownloadManagerDelegate : public CefDownloadManagerDelegateImpl {
public:
explicit AlloyDownloadManagerDelegate(content::DownloadManager* manager);

AlloyDownloadManagerDelegate(const AlloyDownloadManagerDelegate&) = delete;
AlloyDownloadManagerDelegate& operator=(const AlloyDownloadManagerDelegate&) =
delete;

private:
// DownloadManagerDelegate methods.
void GetNextId(content::DownloadIdCallback callback) override;
std::string ApplicationClientIdForFileScanning() override;
};

#endif // CEF_LIBCEF_BROWSER_ALLOY_ALLOY_DOWNLOAD_MANAGER_DELEGATE_H_
Loading

0 comments on commit be6642d

Please sign in to comment.