diff --git a/filenames.gni b/filenames.gni index a41f5cae22..941dd9f7e4 100644 --- a/filenames.gni +++ b/filenames.gni @@ -580,6 +580,7 @@ filenames = { "shell/common/asar/asar_util.h", "shell/common/asar/scoped_temporary_file.cc", "shell/common/asar/scoped_temporary_file.h", + "shell/common/callback_util.h", "shell/common/color_util.cc", "shell/common/color_util.h", "shell/common/crash_keys.cc", diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 47ae576770..0c203ab91c 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -59,7 +59,6 @@ webview_fullscreen.patch extend_apply_webpreferences.patch build_libc_as_static_library.patch build_do_not_depend_on_packed_resource_integrity.patch -refactor_restore_base_adaptcallbackforrepeating.patch logging_win32_only_create_a_console_if_logging_to_stderr.patch fix_media_key_usage_with_globalshortcuts.patch feat_expose_raw_response_headers_from_urlloader.patch diff --git a/patches/chromium/refactor_restore_base_adaptcallbackforrepeating.patch b/patches/chromium/refactor_restore_base_adaptcallbackforrepeating.patch deleted file mode 100644 index 5b21cb50a2..0000000000 --- a/patches/chromium/refactor_restore_base_adaptcallbackforrepeating.patch +++ /dev/null @@ -1,40 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Charles Kerr -Date: Wed, 9 Jun 2021 14:28:08 -0500 -Subject: refactor: restore base::AdaptCallbackForRepeating - -Undo https://chromium-review.googlesource.com/c/chromium/src/+/2941842 -to reinstate base::AdaptCallbackForRepeating(). It was removed to fix -https://bugs.chromium.org/p/chromium/issues/detail?id=730593 . - -We use AdaptCallbackForRepeating() in about a dozen places. This patch -should be removed as soon as those have been updated. Patching because -every instance is a FTBFS that prevents testing any one instance's fix. - -diff --git a/base/functional/callback_helpers.h b/base/functional/callback_helpers.h -index f1aa11fec7c0994ac19a26a02800f25de8f2f519..bbfdb3e4839ed96e4c6238235458a421c917411f 100644 ---- a/base/functional/callback_helpers.h -+++ b/base/functional/callback_helpers.h -@@ -99,6 +99,22 @@ RepeatingCallback ForwardRepeatingCallbacks( - std::move(v)); - } - -+// Wraps the given OnceCallback into a RepeatingCallback that relays its -+// invocation to the original OnceCallback on the first invocation. The -+// following invocations are just ignored. -+// -+// Note that this deliberately subverts the Once/Repeating paradigm of Callbacks -+// but helps ease the migration from old-style Callbacks. Avoid if possible; use -+// if necessary for migration. TODO(tzik): Remove it. https://crbug.com/730593 -+template -+RepeatingCallback AdaptCallbackForRepeating( -+ OnceCallback callback) { -+ using Helper = internal::OnceCallbackHolder; -+ return base::BindRepeating( -+ &Helper::Run, std::make_unique(std::move(callback), -+ /*ignore_extra_runs=*/true)); -+} -+ - // Wraps the given OnceCallback and returns two OnceCallbacks with an identical - // signature. On first invokation of either returned callbacks, the original - // callback is invoked. Invoking the remaining callback results in a crash. diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index a6153ff0da..2632282b52 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -62,6 +62,7 @@ #include "shell/browser/net/resolve_proxy_helper.h" #include "shell/browser/relauncher.h" #include "shell/common/application_info.h" +#include "shell/common/callback_util.h" #include "shell/common/electron_command_line.h" #include "shell/common/electron_paths.h" #include "shell/common/gin_converters/base_converter.h" @@ -735,7 +736,8 @@ void App::AllowCertificateError( bool is_main_frame_request, bool strict_enforcement, base::OnceCallback callback) { - auto adapted_callback = base::AdaptCallbackForRepeating(std::move(callback)); + auto adapted_callback = + electron::AdaptCallbackForRepeating(std::move(callback)); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::HandleScope handle_scope(isolate); bool prevent_default = Emit( diff --git a/shell/browser/api/electron_api_menu_views.cc b/shell/browser/api/electron_api_menu_views.cc index 1fa67b3bb4..2003ae4e59 100644 --- a/shell/browser/api/electron_api_menu_views.cc +++ b/shell/browser/api/electron_api_menu_views.cc @@ -10,6 +10,7 @@ #include "shell/browser/api/electron_api_base_window.h" #include "shell/browser/api/electron_api_web_frame_main.h" #include "shell/browser/native_window_views.h" +#include "shell/common/callback_util.h" #include "ui/display/screen.h" #include "v8/include/cppgc/allocation.h" #include "v8/include/v8-cppgc.h" @@ -54,7 +55,7 @@ void MenuViews::PopupAt(BaseWindow* window, // callback, it is fine passing OnceCallback to it because we reset the // menu runner immediately when the menu is closed. int32_t window_id = window->weak_map_id(); - auto close_callback = base::AdaptCallbackForRepeating( + auto close_callback = electron::AdaptCallbackForRepeating( base::BindOnce(&MenuViews::OnClosed, weak_factory_.GetWeakPtr(), window_id, std::move(callback_with_ref))); auto& runner = menu_runners_[window_id] = diff --git a/shell/common/callback_util.h b/shell/common/callback_util.h new file mode 100644 index 0000000000..70f454cf58 --- /dev/null +++ b/shell/common/callback_util.h @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft GmbH. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ELECTRON_SHELL_COMMON_CALLBACK_UTIL_H_ +#define ELECTRON_SHELL_COMMON_CALLBACK_UTIL_H_ + +#include +#include + +#include "base/functional/bind.h" +#include "base/functional/callback.h" + +namespace electron { + +namespace internal { +template +class OnceCallbackHolder { + public: + explicit OnceCallbackHolder(base::OnceCallback cb) + : cb_(std::move(cb)) {} + + void Run(Args... args) { + if (cb_) + std::move(cb_).Run(std::forward(args)...); + } + + private: + base::OnceCallback cb_; +}; +} // namespace internal + +template +base::RepeatingCallback AdaptCallbackForRepeating( + base::OnceCallback cb) { + using Holder = internal::OnceCallbackHolder; + return base::BindRepeating(&Holder::Run, + std::make_unique(std::move(cb))); +} + +} // namespace electron + +#endif // ELECTRON_SHELL_COMMON_CALLBACK_UTIL_H_ diff --git a/shell/common/gin_converters/callback_converter.h b/shell/common/gin_converters/callback_converter.h index ca2bc900c0..1fe807de6c 100644 --- a/shell/common/gin_converters/callback_converter.h +++ b/shell/common/gin_converters/callback_converter.h @@ -8,6 +8,7 @@ #include #include "base/functional/callback_helpers.h" +#include "shell/common/callback_util.h" #include "shell/common/gin_helper/callback.h" namespace gin { @@ -41,7 +42,7 @@ struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, base::OnceCallback in) { return gin::ConvertToV8(isolate, - base::AdaptCallbackForRepeating(std::move(in))); + electron::AdaptCallbackForRepeating(std::move(in))); } static bool FromV8(v8::Isolate* isolate, v8::Local val,