Use absl::Cleanup in various installer/updater helpers/tests
absl::Cleanup is a helper to run arbitrary code when the absl::Cleanup
object goes out of scope, which is useful for executing cleanup code or
ensuring something always runs. The current //base equivalent is
base::ScopedClosureRunner, which executes a base::OnceClosure when the
runner object goes out of scope.
Compared to base::ScopedClosureRunner, there are several benefits to
using absl::Cleanup:
- works with capturing lambdas, which are often much more concise than
base::BindOnce()
- requires no heap allocations
- less impact on binary size since absl::Cleanup instantiates fewer
templates
This CL is part of a project-wide cleanup to migrate to absl::Cleanup
where appropriate. The general criteria for migrating usages of
base::ScopedClosureRunner:
- The cleanup scoper must not escape block scope, e.g. it is not
returned from the function, passed to another function, or bound into
a callback.
- The cleanup scoper's type does not need to be named, e.g. the scoper
construction can use CTAD:
absl::Cleanup run_at_exit = [] { RestoreSettings(original); };
Note: having to write absl::Cleanup<decltype(lambda)> as a type is
often a sign that absl::Cleanup is not a good fit for how the code is
currently structured.
- The cleanup scoper is not simply running a base::OnceClosure.
Bug: 339492604
Change-Id: I7f3ceaa835f62779a979a2519f20af00cef23060
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5530366
Reviewed-by: S Ganesh <ganesh@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1299361}
diff --git a/chrome/elevation_service/elevated_recovery_impl.cc b/chrome/elevation_service/elevated_recovery_impl.cc
index 52a55cc..62718bb 100644
--- a/chrome/elevation_service/elevated_recovery_impl.cc
+++ b/chrome/elevation_service/elevated_recovery_impl.cc
@@ -15,8 +15,6 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
-#include "base/functional/bind.h"
-#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/process/launch.h"
@@ -25,6 +23,7 @@
#include "base/version.h"
#include "base/win/scoped_process_information.h"
#include "chrome/install_static/install_util.h"
+#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "third_party/zlib/google/zip.h"
namespace elevation_service {
@@ -76,8 +75,7 @@
if (FAILED(hr))
return hr;
- base::ScopedClosureRunner revert_to_self(
- base::BindOnce([]() { ::CoRevertToSelf(); }));
+ absl::Cleanup revert_to_self = [] { ::CoRevertToSelf(); };
*process = base::Process::OpenWithAccess(proc_id, PROCESS_DUP_HANDLE);
return process->IsValid() ? S_OK : HRESULTFromLastError();
@@ -95,8 +93,7 @@
if (FAILED(hr))
return hr;
- base::ScopedClosureRunner revert_to_self(
- base::BindOnce([]() { ::CoRevertToSelf(); }));
+ absl::Cleanup revert_to_self = [] { ::CoRevertToSelf(); };
file->Initialize(file_path, flags);
if (!file->IsValid())
diff --git a/chrome/elevation_service/elevator.cc b/chrome/elevation_service/elevator.cc
index c2ab769..7bfae80 100644
--- a/chrome/elevation_service/elevator.cc
+++ b/chrome/elevation_service/elevator.cc
@@ -6,15 +6,12 @@
#include <dpapi.h>
#include <oleauto.h>
-
#include <stdint.h>
#include <string>
#include <vector>
#include "base/files/file_path.h"
-#include "base/functional/bind.h"
-#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/process.h"
@@ -27,6 +24,7 @@
#include "chrome/elevation_service/caller_validation.h"
#include "chrome/elevation_service/elevated_recovery_impl.h"
#include "chrome/install_static/install_util.h"
+#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/elevation_service/internal/elevation_service_internal.h"
@@ -97,8 +95,7 @@
DATA_BLOB intermediate = {};
{
- base::ScopedClosureRunner revert_to_self(
- base::BindOnce([]() { ::CoRevertToSelf(); }));
+ absl::Cleanup revert_to_self = [] { ::CoRevertToSelf(); };
const auto calling_process = GetCallingProcess();
if (!calling_process.IsValid())
@@ -185,8 +182,7 @@
std::string plaintext_str;
{
DATA_BLOB output = {};
- base::ScopedClosureRunner revert_to_self(
- base::BindOnce([]() { ::CoRevertToSelf(); }));
+ absl::Cleanup revert_to_self = [] { ::CoRevertToSelf(); };
// Decrypt using the user store.
if (!::CryptUnprotectData(&intermediate, nullptr, nullptr, nullptr, nullptr,
0, &output)) {
diff --git a/chrome/installer/setup/setup_util.cc b/chrome/installer/setup/setup_util.cc
index b9471ce..a32f462a 100644
--- a/chrome/installer/setup/setup_util.cc
+++ b/chrome/installer/setup/setup_util.cc
@@ -29,7 +29,6 @@
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
-#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
@@ -64,6 +63,7 @@
#include "components/zucchini/zucchini_integration.h"
#include "courgette/courgette.h"
#include "courgette/third_party/bsdiff/bsdiff.h"
+#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
namespace installer {
@@ -655,8 +655,7 @@
&buffer_size)) {
return base::Time();
}
- base::ScopedClosureRunner wts_deleter(
- base::BindOnce(&::WTSFreeMemory, base::Unretained(buffer)));
+ absl::Cleanup wts_deleter = [buffer] { ::WTSFreeMemory(buffer); };
WTSINFO* wts_info = nullptr;
if (buffer_size < sizeof(*wts_info))
diff --git a/chrome/installer/util/shell_util.cc b/chrome/installer/util/shell_util.cc
index 4669456..3db0ccc 100644
--- a/chrome/installer/util/shell_util.cc
+++ b/chrome/installer/util/shell_util.cc
@@ -30,7 +30,6 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
-#include "base/functional/callback_helpers.h"
#include "base/hash/md5.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
@@ -70,6 +69,7 @@
#include "chrome/installer/util/util_constants.h"
#include "chrome/installer/util/work_item.h"
#include "components/base32/base32.h"
+#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
using base::win::RegKey;
@@ -1350,9 +1350,9 @@
// Ensure that the shell is notified of the mutations below. Specific exit
// points may disable this if no mutations are made.
- base::ScopedClosureRunner notify_on_exit(base::BindOnce([] {
+ absl::Cleanup notify_on_exit = [] {
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr);
- }));
+ };
// Do the full registration at user-level or if the user is an admin.
if (root == HKEY_CURRENT_USER || IsUserAnAdmin()) {
@@ -1373,7 +1373,7 @@
// localization issues (see https://crbug.com/717913#c18). It likely is not,
// so return success to allow Chrome to be made default.
if (!user_level) {
- notify_on_exit.Release().Reset();
+ std::move(notify_on_exit).Cancel();
return true;
}
// Try to elevate and register if requested for per-user installs if the user
diff --git a/chrome/updater/test/run_all_unittests.cc b/chrome/updater/test/run_all_unittests.cc
index 0ba4a9e..85c2010 100644
--- a/chrome/updater/test/run_all_unittests.cc
+++ b/chrome/updater/test/run_all_unittests.cc
@@ -7,7 +7,6 @@
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
-#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/process/process.h"
#include "base/test/launcher/unit_test_launcher.h"
@@ -17,9 +16,10 @@
#include "chrome/common/chrome_paths.h"
#include "chrome/updater/test/integration_test_commands.h"
#include "chrome/updater/test/test_scope.h"
+#include "chrome/updater/test/unit_test_util.h"
#include "chrome/updater/updater_branding.h"
#include "chrome/updater/updater_scope.h"
-#include "chrome/updater/test/unit_test_util.h"
+#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#if BUILDFLAG(IS_WIN)
#include <shlobj.h>
@@ -163,8 +163,7 @@
int main(int argc, char** argv) {
base::CommandLine::Init(argc, argv);
- base::ScopedClosureRunner reset_command_line(
- base::BindOnce(&base::CommandLine::Reset));
+ absl::Cleanup reset_command_line = &base::CommandLine::Reset;
// Change the test timeout defaults if the command line arguments to override
// them are not present.