From 0346e0a8bff7b712f86f51075b3effcc1d8358f1 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 19 Apr 2024 10:55:59 -0500 Subject: [PATCH] refactor: use `std::erase()` (#41881) * refactor: use std::erase() in WebContentsPreferences::~WebContentsPreferences() * refactor: use std::erase() in WindowList::RemoveWindow() * refactor: use std::erase() in ElectronBindings::EnvironmentDestroyed() * refactor: use std::erase() in CleanedUpAtExit::~CleanedUpAtExit() * refactor: use std::erase_if() in ElectronBrowserContext::RevokeDevicePermission() * refactor: use std::erase_if() in UsbChooserController::GotUsbDeviceList() * refactor: move DoesDeviceMatch() out of class into anonymous namespace --- shell/browser/electron_browser_context.cc | 139 +++++++++--------- shell/browser/electron_browser_context.h | 4 - shell/browser/usb/usb_chooser_controller.cc | 8 +- shell/browser/web_contents_preferences.cc | 3 +- shell/browser/window_list.cc | 3 +- shell/common/api/electron_bindings.cc | 5 +- shell/common/gin_helper/cleaned_up_at_exit.cc | 3 +- 7 files changed, 74 insertions(+), 91 deletions(-) diff --git a/shell/browser/electron_browser_context.cc b/shell/browser/electron_browser_context.cc index e9d124f9e10..6a0bf13c127 100644 --- a/shell/browser/electron_browser_context.cc +++ b/shell/browser/electron_browser_context.cc @@ -241,6 +241,67 @@ std::string MakePartitionName(const std::string& input) { return {}; } +bool DoesDeviceMatch(const base::Value& device, + const base::Value& device_to_compare, + const blink::PermissionType permission_type) { + if (permission_type == + static_cast( + WebContentsPermissionHelper::PermissionType::HID) || + permission_type == + static_cast( + WebContentsPermissionHelper::PermissionType::USB)) { + if (device.GetDict().FindInt(kDeviceVendorIdKey) != + device_to_compare.GetDict().FindInt(kDeviceVendorIdKey) || + device.GetDict().FindInt(kDeviceProductIdKey) != + device_to_compare.GetDict().FindInt(kDeviceProductIdKey)) { + return false; + } + + const auto* serial_number = + device_to_compare.GetDict().FindString(kDeviceSerialNumberKey); + const auto* device_serial_number = + device.GetDict().FindString(kDeviceSerialNumberKey); + + if (serial_number && device_serial_number && + *device_serial_number == *serial_number) + return true; + } else if (permission_type == + static_cast( + WebContentsPermissionHelper::PermissionType::SERIAL)) { +#if BUILDFLAG(IS_WIN) + const auto* instance_id = device.GetDict().FindString(kDeviceInstanceIdKey); + const auto* port_instance_id = + device_to_compare.GetDict().FindString(kDeviceInstanceIdKey); + if (instance_id && port_instance_id && *instance_id == *port_instance_id) + return true; +#else + const auto* serial_number = device.GetDict().FindString(kSerialNumberKey); + const auto* port_serial_number = + device_to_compare.GetDict().FindString(kSerialNumberKey); + if (device.GetDict().FindInt(kVendorIdKey) != + device_to_compare.GetDict().FindInt(kVendorIdKey) || + device.GetDict().FindInt(kProductIdKey) != + device_to_compare.GetDict().FindInt(kProductIdKey) || + (serial_number && port_serial_number && + *port_serial_number != *serial_number)) { + return false; + } + +#if BUILDFLAG(IS_MAC) + const auto* usb_driver_key = device.GetDict().FindString(kUsbDriverKey); + const auto* port_usb_driver_key = + device_to_compare.GetDict().FindString(kUsbDriverKey); + if (usb_driver_key && port_usb_driver_key && + *usb_driver_key != *port_usb_driver_key) { + return false; + } +#endif // BUILDFLAG(IS_MAC) + return true; +#endif // BUILDFLAG(IS_WIN) + } + return false; +} + } // namespace // static @@ -721,7 +782,7 @@ void ElectronBrowserContext::GrantDevicePermission( void ElectronBrowserContext::RevokeDevicePermission( const url::Origin& origin, const base::Value& device, - blink::PermissionType permission_type) { + const blink::PermissionType permission_type) { const auto& current_devices_it = granted_devices_.find(permission_type); if (current_devices_it == granted_devices_.end()) return; @@ -730,76 +791,10 @@ void ElectronBrowserContext::RevokeDevicePermission( if (origin_devices_it == current_devices_it->second.end()) return; - for (auto it = origin_devices_it->second.begin(); - it != origin_devices_it->second.end();) { - if (DoesDeviceMatch(device, it->get(), permission_type)) { - it = origin_devices_it->second.erase(it); - } else { - ++it; - } - } -} - -bool ElectronBrowserContext::DoesDeviceMatch( - const base::Value& device, - const base::Value* device_to_compare, - blink::PermissionType permission_type) { - if (permission_type == - static_cast( - WebContentsPermissionHelper::PermissionType::HID) || - permission_type == - static_cast( - WebContentsPermissionHelper::PermissionType::USB)) { - if (device.GetDict().FindInt(kDeviceVendorIdKey) != - device_to_compare->GetDict().FindInt(kDeviceVendorIdKey) || - device.GetDict().FindInt(kDeviceProductIdKey) != - device_to_compare->GetDict().FindInt(kDeviceProductIdKey)) { - return false; - } - - const auto* serial_number = - device_to_compare->GetDict().FindString(kDeviceSerialNumberKey); - const auto* device_serial_number = - device.GetDict().FindString(kDeviceSerialNumberKey); - - if (serial_number && device_serial_number && - *device_serial_number == *serial_number) - return true; - } else if (permission_type == - static_cast( - WebContentsPermissionHelper::PermissionType::SERIAL)) { -#if BUILDFLAG(IS_WIN) - const auto* instance_id = device.GetDict().FindString(kDeviceInstanceIdKey); - const auto* port_instance_id = - device_to_compare->GetDict().FindString(kDeviceInstanceIdKey); - if (instance_id && port_instance_id && *instance_id == *port_instance_id) - return true; -#else - const auto* serial_number = device.GetDict().FindString(kSerialNumberKey); - const auto* port_serial_number = - device_to_compare->GetDict().FindString(kSerialNumberKey); - if (device.GetDict().FindInt(kVendorIdKey) != - device_to_compare->GetDict().FindInt(kVendorIdKey) || - device.GetDict().FindInt(kProductIdKey) != - device_to_compare->GetDict().FindInt(kProductIdKey) || - (serial_number && port_serial_number && - *port_serial_number != *serial_number)) { - return false; - } - -#if BUILDFLAG(IS_MAC) - const auto* usb_driver_key = device.GetDict().FindString(kUsbDriverKey); - const auto* port_usb_driver_key = - device_to_compare->GetDict().FindString(kUsbDriverKey); - if (usb_driver_key && port_usb_driver_key && - *usb_driver_key != *port_usb_driver_key) { - return false; - } -#endif // BUILDFLAG(IS_MAC) - return true; -#endif // BUILDFLAG(IS_WIN) - } - return false; + std::erase_if(origin_devices_it->second, + [&device, &permission_type](auto const& val) { + return DoesDeviceMatch(device, *val, permission_type); + }); } bool ElectronBrowserContext::CheckDevicePermission( @@ -815,7 +810,7 @@ bool ElectronBrowserContext::CheckDevicePermission( return false; for (const auto& device_to_compare : origin_devices_it->second) { - if (DoesDeviceMatch(device, device_to_compare.get(), permission_type)) + if (DoesDeviceMatch(device, *device_to_compare, permission_type)) return true; } diff --git a/shell/browser/electron_browser_context.h b/shell/browser/electron_browser_context.h index 3002ebeb4c1..e3d6f4e2e7e 100644 --- a/shell/browser/electron_browser_context.h +++ b/shell/browser/electron_browser_context.h @@ -221,10 +221,6 @@ class ElectronBrowserContext : public content::BrowserContext { // Initialize pref registry. void InitPrefs(); - bool DoesDeviceMatch(const base::Value& device, - const base::Value* device_to_compare, - blink::PermissionType permission_type); - scoped_refptr in_memory_pref_store_; std::unique_ptr cookie_change_notifier_; std::unique_ptr prefs_; diff --git a/shell/browser/usb/usb_chooser_controller.cc b/shell/browser/usb/usb_chooser_controller.cc index b3676268367..d7cb9560c34 100644 --- a/shell/browser/usb/usb_chooser_controller.cc +++ b/shell/browser/usb/usb_chooser_controller.cc @@ -122,11 +122,9 @@ void UsbChooserController::GotUsbDeviceList( v8::HandleScope scope(isolate); // "select-usb-device" should respect |filters|. - devices.erase(std::remove_if(devices.begin(), devices.end(), - [this](const auto& device_info) { - return !DisplayDevice(*device_info); - }), - devices.end()); + std::erase_if(devices, [this](const auto& device_info) { + return !DisplayDevice(*device_info); + }); v8::Local details = gin::DataObjectBuilder(isolate) .Set("deviceList", devices) diff --git a/shell/browser/web_contents_preferences.cc b/shell/browser/web_contents_preferences.cc index e2dd0813cbb..475c7e7295f 100644 --- a/shell/browser/web_contents_preferences.cc +++ b/shell/browser/web_contents_preferences.cc @@ -107,8 +107,7 @@ WebContentsPreferences::WebContentsPreferences( } WebContentsPreferences::~WebContentsPreferences() { - Instances().erase(std::remove(Instances().begin(), Instances().end(), this), - Instances().end()); + std::erase(Instances(), this); } void WebContentsPreferences::Clear() { diff --git a/shell/browser/window_list.cc b/shell/browser/window_list.cc index 78378f3aa9a..1b9505383b6 100644 --- a/shell/browser/window_list.cc +++ b/shell/browser/window_list.cc @@ -59,8 +59,7 @@ void WindowList::AddWindow(NativeWindow* window) { // static void WindowList::RemoveWindow(NativeWindow* window) { WindowVector& windows = GetInstance()->windows_; - windows.erase(std::remove(windows.begin(), windows.end(), window), - windows.end()); + std::erase(windows, window); for (WindowListObserver& observer : GetObservers()) observer.OnWindowRemoved(window); diff --git a/shell/common/api/electron_bindings.cc b/shell/common/api/electron_bindings.cc index 549bccca4b1..1a13f3979cf 100644 --- a/shell/common/api/electron_bindings.cc +++ b/shell/common/api/electron_bindings.cc @@ -83,10 +83,7 @@ void ElectronBindings::BindTo(v8::Isolate* isolate, } void ElectronBindings::EnvironmentDestroyed(node::Environment* env) { - auto it = - std::find(pending_next_ticks_.begin(), pending_next_ticks_.end(), env); - if (it != pending_next_ticks_.end()) - pending_next_ticks_.erase(it); + std::erase(pending_next_ticks_, env); } void ElectronBindings::ActivateUVLoop(v8::Isolate* isolate) { diff --git a/shell/common/gin_helper/cleaned_up_at_exit.cc b/shell/common/gin_helper/cleaned_up_at_exit.cc index 1ed74675e7b..62127558a49 100644 --- a/shell/common/gin_helper/cleaned_up_at_exit.cc +++ b/shell/common/gin_helper/cleaned_up_at_exit.cc @@ -19,8 +19,7 @@ CleanedUpAtExit::CleanedUpAtExit() { GetDoomed().emplace_back(this); } CleanedUpAtExit::~CleanedUpAtExit() { - auto& doomed = GetDoomed(); - doomed.erase(std::remove(doomed.begin(), doomed.end(), this), doomed.end()); + std::erase(GetDoomed(), this); } // static