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:
parent
39bf441b3b
commit
0346e0a8bf
7 changed files with 74 additions and 91 deletions
|
@ -241,6 +241,67 @@ std::string MakePartitionName(const std::string& input) {
|
||||||
return {};
|
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
|
} // namespace
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -721,7 +782,7 @@ void ElectronBrowserContext::GrantDevicePermission(
|
||||||
void ElectronBrowserContext::RevokeDevicePermission(
|
void ElectronBrowserContext::RevokeDevicePermission(
|
||||||
const url::Origin& origin,
|
const url::Origin& origin,
|
||||||
const base::Value& device,
|
const base::Value& device,
|
||||||
blink::PermissionType permission_type) {
|
const blink::PermissionType permission_type) {
|
||||||
const auto& current_devices_it = granted_devices_.find(permission_type);
|
const auto& current_devices_it = granted_devices_.find(permission_type);
|
||||||
if (current_devices_it == granted_devices_.end())
|
if (current_devices_it == granted_devices_.end())
|
||||||
return;
|
return;
|
||||||
|
@ -730,76 +791,10 @@ void ElectronBrowserContext::RevokeDevicePermission(
|
||||||
if (origin_devices_it == current_devices_it->second.end())
|
if (origin_devices_it == current_devices_it->second.end())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (auto it = origin_devices_it->second.begin();
|
std::erase_if(origin_devices_it->second,
|
||||||
it != origin_devices_it->second.end();) {
|
[&device, &permission_type](auto const& val) {
|
||||||
if (DoesDeviceMatch(device, it->get(), permission_type)) {
|
return DoesDeviceMatch(device, *val, 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ElectronBrowserContext::CheckDevicePermission(
|
bool ElectronBrowserContext::CheckDevicePermission(
|
||||||
|
@ -815,7 +810,7 @@ bool ElectronBrowserContext::CheckDevicePermission(
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const auto& device_to_compare : origin_devices_it->second) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -221,10 +221,6 @@ class ElectronBrowserContext : public content::BrowserContext {
|
||||||
// Initialize pref registry.
|
// Initialize pref registry.
|
||||||
void InitPrefs();
|
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_;
|
scoped_refptr<ValueMapPrefStore> in_memory_pref_store_;
|
||||||
std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
|
std::unique_ptr<CookieChangeNotifier> cookie_change_notifier_;
|
||||||
std::unique_ptr<PrefService> prefs_;
|
std::unique_ptr<PrefService> prefs_;
|
||||||
|
|
|
@ -122,11 +122,9 @@ void UsbChooserController::GotUsbDeviceList(
|
||||||
v8::HandleScope scope(isolate);
|
v8::HandleScope scope(isolate);
|
||||||
|
|
||||||
// "select-usb-device" should respect |filters|.
|
// "select-usb-device" should respect |filters|.
|
||||||
devices.erase(std::remove_if(devices.begin(), devices.end(),
|
std::erase_if(devices, [this](const auto& device_info) {
|
||||||
[this](const auto& device_info) {
|
|
||||||
return !DisplayDevice(*device_info);
|
return !DisplayDevice(*device_info);
|
||||||
}),
|
});
|
||||||
devices.end());
|
|
||||||
|
|
||||||
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
|
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
|
||||||
.Set("deviceList", devices)
|
.Set("deviceList", devices)
|
||||||
|
|
|
@ -107,8 +107,7 @@ WebContentsPreferences::WebContentsPreferences(
|
||||||
}
|
}
|
||||||
|
|
||||||
WebContentsPreferences::~WebContentsPreferences() {
|
WebContentsPreferences::~WebContentsPreferences() {
|
||||||
Instances().erase(std::remove(Instances().begin(), Instances().end(), this),
|
std::erase(Instances(), this);
|
||||||
Instances().end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContentsPreferences::Clear() {
|
void WebContentsPreferences::Clear() {
|
||||||
|
|
|
@ -59,8 +59,7 @@ void WindowList::AddWindow(NativeWindow* window) {
|
||||||
// static
|
// static
|
||||||
void WindowList::RemoveWindow(NativeWindow* window) {
|
void WindowList::RemoveWindow(NativeWindow* window) {
|
||||||
WindowVector& windows = GetInstance()->windows_;
|
WindowVector& windows = GetInstance()->windows_;
|
||||||
windows.erase(std::remove(windows.begin(), windows.end(), window),
|
std::erase(windows, window);
|
||||||
windows.end());
|
|
||||||
|
|
||||||
for (WindowListObserver& observer : GetObservers())
|
for (WindowListObserver& observer : GetObservers())
|
||||||
observer.OnWindowRemoved(window);
|
observer.OnWindowRemoved(window);
|
||||||
|
|
|
@ -83,10 +83,7 @@ void ElectronBindings::BindTo(v8::Isolate* isolate,
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElectronBindings::EnvironmentDestroyed(node::Environment* env) {
|
void ElectronBindings::EnvironmentDestroyed(node::Environment* env) {
|
||||||
auto it =
|
std::erase(pending_next_ticks_, env);
|
||||||
std::find(pending_next_ticks_.begin(), pending_next_ticks_.end(), env);
|
|
||||||
if (it != pending_next_ticks_.end())
|
|
||||||
pending_next_ticks_.erase(it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElectronBindings::ActivateUVLoop(v8::Isolate* isolate) {
|
void ElectronBindings::ActivateUVLoop(v8::Isolate* isolate) {
|
||||||
|
|
|
@ -19,8 +19,7 @@ CleanedUpAtExit::CleanedUpAtExit() {
|
||||||
GetDoomed().emplace_back(this);
|
GetDoomed().emplace_back(this);
|
||||||
}
|
}
|
||||||
CleanedUpAtExit::~CleanedUpAtExit() {
|
CleanedUpAtExit::~CleanedUpAtExit() {
|
||||||
auto& doomed = GetDoomed();
|
std::erase(GetDoomed(), this);
|
||||||
doomed.erase(std::remove(doomed.begin(), doomed.end(), this), doomed.end());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
|
Loading…
Reference in a new issue