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
This commit is contained in:
Charles Kerr 2024-04-19 10:55:59 -05:00 committed by GitHub
parent 39bf441b3b
commit 0346e0a8bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 74 additions and 91 deletions

View file

@ -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<blink::PermissionType>(
WebContentsPermissionHelper::PermissionType::HID) ||
permission_type ==
static_cast<blink::PermissionType>(
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<blink::PermissionType>(
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<blink::PermissionType>(
WebContentsPermissionHelper::PermissionType::HID) ||
permission_type ==
static_cast<blink::PermissionType>(
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<blink::PermissionType>(
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;
}

View file

@ -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<ValueMapPrefStore> in_memory_pref_store_;
std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
std::unique_ptr<PrefService> prefs_;

View file

@ -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<v8::Object> details = gin::DataObjectBuilder(isolate)
.Set("deviceList", devices)

View file

@ -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() {

View file

@ -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);

View file

@ -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) {

View file

@ -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