refactor: prefer base::Contains() over find() + end() (#38443)

* refactor: use base::Contains() in KeyWeakMap::Has()

* refactor: use base::Contains() in WebRequest::RequestFilter::MatchesType()

* refactor: use base::Contains() in BaseWindow::AddBrowserView()

* refactor: use base::Contains() in DeepFreeze()

* refactor: use base::Contains() in Clipboard::Read()

* Revert "refactor: use base::Contains() in BaseWindow::AddBrowserView()"
This reverts commit 60152359d3978451ebdd7c8eed602c2fb8a9cafa.

* refactor: use base::Contains() in BaseWindow::AddBrowserView()

* refactor: use base::Contains() in IsDevToolsFileSystemAdded()

* refactor: use base::Contains() in MessagePort::DisentanglePorts()

* refactor: use base::Contains() in PowerSaveBlocker::IsStarted()

* refactor: use base::Contains() in SpellCheckClient::OnSpellCheckDone()

* refactor: use base::Contains() in ShowTaskDialogWstr()

* refactor: use base::Contains() in PrintViewManagerElectron::ScriptedPrint()

* refactor: use base::Contains() in PrintViewManagerElectron::DidGetPrintedPagesCount()

* refactor: use base::Contains() in NativeWindow::AddDraggableRegionProvider()

* refactor: use base::Contains() in ElectronBindings::ActivateUVLoop()

* refactor: use base::Contains() in NativeWindowViews::IsVisibleOnAllWorkspaces()

* refactor: use base::Contains() in HidChooserController::OnDeviceAdded()

* refactor: use base::Contains() in ElectronSandboxedRendererClient::WillReleaseScriptContext()

* refactor: use base::Contains() in ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread()

* refactor: use base::Contains() in GlobalShortcut::OnKeyPressed()
This commit is contained in:
Charles Kerr 2023-05-30 03:28:43 -05:00 committed by GitHub
parent 9640ac441d
commit 0203bd3305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 39 additions and 36 deletions

View file

@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/task/single_thread_task_runner.h" #include "base/task/single_thread_task_runner.h"
#include "electron/buildflags/buildflags.h" #include "electron/buildflags/buildflags.h"
#include "gin/dictionary.h" #include "gin/dictionary.h"
@ -757,8 +758,7 @@ void BaseWindow::SetBrowserView(
} }
void BaseWindow::AddBrowserView(gin::Handle<BrowserView> browser_view) { void BaseWindow::AddBrowserView(gin::Handle<BrowserView> browser_view) {
auto iter = browser_views_.find(browser_view->ID()); if (!base::Contains(browser_views_, browser_view->ID())) {
if (iter == browser_views_.end()) {
// If we're reparenting a BrowserView, ensure that it's detached from // If we're reparenting a BrowserView, ensure that it's detached from
// its previous owner window. // its previous owner window.
BaseWindow* owner_window = browser_view->owner_window(); BaseWindow* owner_window = browser_view->owner_window();

View file

@ -62,8 +62,7 @@ GlobalShortcut::~GlobalShortcut() {
} }
void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) { void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
if (accelerator_callback_map_.find(accelerator) == if (!base::Contains(accelerator_callback_map_, accelerator)) {
accelerator_callback_map_.end()) {
// This should never occur, because if it does, GlobalShortcutListener // This should never occur, because if it does, GlobalShortcutListener
// notifies us with wrong accelerator. // notifies us with wrong accelerator.
NOTREACHED(); NOTREACHED();

View file

@ -6,6 +6,7 @@
#include <string> #include <string>
#include "base/containers/contains.h"
#include "base/functional/callback_helpers.h" #include "base/functional/callback_helpers.h"
#include "content/public/browser/device_service.h" #include "content/public/browser/device_service.h"
#include "gin/dictionary.h" #include "gin/dictionary.h"
@ -106,8 +107,8 @@ bool PowerSaveBlocker::Stop(int id) {
return success; return success;
} }
bool PowerSaveBlocker::IsStarted(int id) { bool PowerSaveBlocker::IsStarted(int id) const {
return wake_lock_types_.find(id) != wake_lock_types_.end(); return base::Contains(wake_lock_types_, id);
} }
// static // static

View file

@ -37,7 +37,7 @@ class PowerSaveBlocker : public gin::Wrappable<PowerSaveBlocker> {
void UpdatePowerSaveBlocker(); void UpdatePowerSaveBlocker();
int Start(device::mojom::WakeLockType type); int Start(device::mojom::WakeLockType type);
bool Stop(int id); bool Stop(int id);
bool IsStarted(int id); bool IsStarted(int id) const;
device::mojom::WakeLock* GetWakeLock(); device::mojom::WakeLock* GetWakeLock();

View file

@ -12,6 +12,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/containers/id_map.h" #include "base/containers/id_map.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
@ -729,8 +730,8 @@ std::map<std::string, std::string> GetAddedFileSystemPaths(
bool IsDevToolsFileSystemAdded(content::WebContents* web_contents, bool IsDevToolsFileSystemAdded(content::WebContents* web_contents,
const std::string& file_system_path) { const std::string& file_system_path) {
auto file_system_paths = GetAddedFileSystemPaths(web_contents); return base::Contains(GetAddedFileSystemPaths(web_contents),
return file_system_paths.find(file_system_path) != file_system_paths.end(); file_system_path);
} }
void SetBackgroundColor(content::RenderWidgetHostView* rwhv, SkColor color) { void SetBackgroundColor(content::RenderWidgetHostView* rwhv, SkColor color) {

View file

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/task/sequenced_task_runner.h" #include "base/task/sequenced_task_runner.h"
@ -294,7 +295,7 @@ bool WebRequest::RequestFilter::MatchesURL(const GURL& url) const {
bool WebRequest::RequestFilter::MatchesType( bool WebRequest::RequestFilter::MatchesType(
extensions::WebRequestResourceType type) const { extensions::WebRequestResourceType type) const {
return types_.empty() || types_.find(type) != types_.end(); return types_.empty() || base::Contains(types_, type);
} }
bool WebRequest::RequestFilter::MatchesRequest( bool WebRequest::RequestFilter::MatchesRequest(

View file

@ -8,6 +8,7 @@
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include "base/containers/contains.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h" #include "base/task/single_thread_task_runner.h"
#include "gin/arguments.h" #include "gin/arguments.h"
@ -221,7 +222,7 @@ std::vector<blink::MessagePortChannel> MessagePort::DisentanglePorts(
// or cloned ports, throw an error (per section 8.3.3 of the HTML5 spec). // or cloned ports, throw an error (per section 8.3.3 of the HTML5 spec).
for (unsigned i = 0; i < ports.size(); ++i) { for (unsigned i = 0; i < ports.size(); ++i) {
auto* port = ports[i].get(); auto* port = ports[i].get();
if (!port || port->IsNeutered() || visited.find(port) != visited.end()) { if (!port || port->IsNeutered() || base::Contains(visited, port)) {
std::string type; std::string type;
if (!port) if (!port)
type = "null"; type = "null";

View file

@ -149,8 +149,7 @@ void HidChooserController::OnDeviceAdded(
void HidChooserController::OnDeviceRemoved( void HidChooserController::OnDeviceRemoved(
const device::mojom::HidDeviceInfo& device) { const device::mojom::HidDeviceInfo& device) {
auto id = PhysicalDeviceIdFromDeviceInfo(device); auto id = PhysicalDeviceIdFromDeviceInfo(device);
auto items_it = std::find(items_.begin(), items_.end(), id); if (!base::Contains(items_, id))
if (items_it == items_.end())
return; return;
api::Session* session = GetSession(); api::Session* session = GetSession();
if (session) { if (session) {

View file

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h" #include "base/values.h"
@ -725,9 +726,7 @@ int NativeWindow::NonClientHitTest(const gfx::Point& point) {
void NativeWindow::AddDraggableRegionProvider( void NativeWindow::AddDraggableRegionProvider(
DraggableRegionProvider* provider) { DraggableRegionProvider* provider) {
if (std::find(draggable_region_providers_.begin(), if (!base::Contains(draggable_region_providers_, provider)) {
draggable_region_providers_.end(),
provider) == draggable_region_providers_.end()) {
draggable_region_providers_.push_back(provider); draggable_region_providers_.push_back(provider);
} }
} }

View file

@ -13,6 +13,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
@ -1438,8 +1439,7 @@ bool NativeWindowViews::IsVisibleOnAllWorkspaces() {
std::vector<x11::Atom> wm_states; std::vector<x11::Atom> wm_states;
GetArrayProperty(static_cast<x11::Window>(GetAcceleratedWidget()), GetArrayProperty(static_cast<x11::Window>(GetAcceleratedWidget()),
x11::GetAtom("_NET_WM_STATE"), &wm_states); x11::GetAtom("_NET_WM_STATE"), &wm_states);
return std::find(wm_states.begin(), wm_states.end(), sticky_atom) != return base::Contains(wm_states, sticky_atom);
wm_states.end();
} }
#endif #endif
return false; return false;

View file

@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h" #include "base/functional/bind.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/printing/browser/print_to_pdf/pdf_print_utils.h" #include "components/printing/browser/print_to_pdf/pdf_print_utils.h"
@ -98,8 +99,7 @@ void PrintViewManagerElectron::GetDefaultPrintSettings(
void PrintViewManagerElectron::ScriptedPrint( void PrintViewManagerElectron::ScriptedPrint(
printing::mojom::ScriptedPrintParamsPtr params, printing::mojom::ScriptedPrintParamsPtr params,
ScriptedPrintCallback callback) { ScriptedPrintCallback callback) {
auto entry = std::find(pdf_jobs_.begin(), pdf_jobs_.end(), params->cookie); if (!base::Contains(pdf_jobs_, params->cookie)) {
if (entry == pdf_jobs_.end()) {
PrintViewManagerBase::ScriptedPrint(std::move(params), std::move(callback)); PrintViewManagerBase::ScriptedPrint(std::move(params), std::move(callback));
return; return;
} }
@ -135,8 +135,7 @@ void PrintViewManagerElectron::CheckForCancel(int32_t preview_ui_id,
void PrintViewManagerElectron::DidGetPrintedPagesCount(int32_t cookie, void PrintViewManagerElectron::DidGetPrintedPagesCount(int32_t cookie,
uint32_t number_pages) { uint32_t number_pages) {
auto entry = std::find(pdf_jobs_.begin(), pdf_jobs_.end(), cookie); if (!base::Contains(pdf_jobs_, cookie)) {
if (entry == pdf_jobs_.end()) {
PrintViewManagerBase::DidGetPrintedPagesCount(cookie, number_pages); PrintViewManagerBase::DidGetPrintedPagesCount(cookie, number_pages);
} }
} }

View file

@ -242,7 +242,7 @@ DialogResult ShowTaskDialogWstr(gfx::AcceleratedWidget parent,
TaskDialogIndirect(&config, &id, nullptr, &verification_flag_checked); TaskDialogIndirect(&config, &id, nullptr, &verification_flag_checked);
int button_id; int button_id;
if (id_map.find(id) != id_map.end()) // common button. if (base::Contains(id_map, id)) // common button.
button_id = id_map[id]; button_id = id_map[id];
else if (id >= kIDStart) // custom button. else if (id >= kIDStart) // custom button.
button_id = id - kIDStart; button_id = id - kIDStart;

View file

@ -6,6 +6,7 @@
#include <map> #include <map>
#include "base/containers/contains.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "shell/common/gin_converters/image_converter.h" #include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/dictionary.h"
@ -71,7 +72,7 @@ std::string Clipboard::Read(const std::string& format_string) {
clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kCopyPaste, clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kCopyPaste,
/* data_dst = */ nullptr); /* data_dst = */ nullptr);
#if BUILDFLAG(IS_LINUX) #if BUILDFLAG(IS_LINUX)
if (custom_format_names.find(format_string) == custom_format_names.end()) { if (!base::Contains(custom_format_names, format_string)) {
custom_format_names = custom_format_names =
clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kSelection, clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kSelection,
/* data_dst = */ nullptr); /* data_dst = */ nullptr);
@ -79,7 +80,7 @@ std::string Clipboard::Read(const std::string& format_string) {
#endif #endif
ui::ClipboardFormatType format; ui::ClipboardFormatType format;
if (custom_format_names.find(format_string) != custom_format_names.end()) { if (base::Contains(custom_format_names, format_string)) {
format = format =
ui::ClipboardFormatType(ui::ClipboardFormatType::CustomPlatformType( ui::ClipboardFormatType(ui::ClipboardFormatType::CustomPlatformType(
custom_format_names[format_string])); custom_format_names[format_string]));

View file

@ -9,6 +9,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/process/process.h" #include "base/process/process.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
@ -100,8 +101,7 @@ void ElectronBindings::EnvironmentDestroyed(node::Environment* env) {
void ElectronBindings::ActivateUVLoop(v8::Isolate* isolate) { void ElectronBindings::ActivateUVLoop(v8::Isolate* isolate) {
node::Environment* env = node::Environment::GetCurrent(isolate); node::Environment* env = node::Environment::GetCurrent(isolate);
if (std::find(pending_next_ticks_.begin(), pending_next_ticks_.end(), env) != if (base::Contains(pending_next_ticks_, env))
pending_next_ticks_.end())
return; return;
pending_next_ticks_.push_back(env); pending_next_ticks_.push_back(env);

View file

@ -9,6 +9,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
@ -52,7 +53,7 @@ class KeyWeakMap {
} }
// Whether there is an object with |key| in this WeakMap. // Whether there is an object with |key| in this WeakMap.
bool Has(const K& key) const { return map_.find(key) != map_.end(); } constexpr bool Has(const K& key) const { return base::Contains(map_, key); }
// Returns all objects. // Returns all objects.
std::vector<v8::Local<v8::Object>> Values(v8::Isolate* isolate) const { std::vector<v8::Local<v8::Object>> Values(v8::Isolate* isolate) const {

View file

@ -10,6 +10,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
@ -63,7 +64,7 @@ bool DeepFreeze(const v8::Local<v8::Object>& object,
const v8::Local<v8::Context>& context, const v8::Local<v8::Context>& context,
std::set<int> frozen = std::set<int>()) { std::set<int> frozen = std::set<int>()) {
int hash = object->GetIdentityHash(); int hash = object->GetIdentityHash();
if (frozen.find(hash) != frozen.end()) if (base::Contains(frozen, hash))
return true; return true;
frozen.insert(hash); frozen.insert(hash);

View file

@ -11,6 +11,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "base/containers/contains.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/numerics/safe_conversions.h" #include "base/numerics/safe_conversions.h"
#include "base/task/single_thread_task_runner.h" #include "base/task/single_thread_task_runner.h"
@ -192,13 +193,13 @@ void SpellCheckClient::OnSpellCheckDone(
auto& word_list = pending_request_param_->wordlist(); auto& word_list = pending_request_param_->wordlist();
for (const auto& word : word_list) { for (const auto& word : word_list) {
if (misspelled.find(word.text) != misspelled.end()) { if (base::Contains(misspelled, word.text)) {
// If this is a contraction, iterate through parts and accept the word // If this is a contraction, iterate through parts and accept the word
// if none of them are misspelled // if none of them are misspelled
if (!word.contraction_words.empty()) { if (!word.contraction_words.empty()) {
auto all_correct = true; auto all_correct = true;
for (const auto& contraction_word : word.contraction_words) { for (const auto& contraction_word : word.contraction_words) {
if (misspelled.find(contraction_word) != misspelled.end()) { if (base::Contains(misspelled, contraction_word)) {
all_correct = false; all_correct = false;
break; break;
} }

View file

@ -7,6 +7,7 @@
#include <string> #include <string>
#include "base/command_line.h" #include "base/command_line.h"
#include "base/containers/contains.h"
#include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_frame.h"
#include "electron/buildflags/buildflags.h" #include "electron/buildflags/buildflags.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
@ -195,15 +196,13 @@ void ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread(
node::Environment* ElectronRendererClient::GetEnvironment( node::Environment* ElectronRendererClient::GetEnvironment(
content::RenderFrame* render_frame) const { content::RenderFrame* render_frame) const {
if (injected_frames_.find(render_frame) == injected_frames_.end()) if (!base::Contains(injected_frames_, render_frame))
return nullptr; return nullptr;
v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
auto context = auto context =
GetContext(render_frame->GetWebFrame(), v8::Isolate::GetCurrent()); GetContext(render_frame->GetWebFrame(), v8::Isolate::GetCurrent());
node::Environment* env = node::Environment::GetCurrent(context); node::Environment* env = node::Environment::GetCurrent(context);
if (environments_.find(env) == environments_.end()) return base::Contains(environments_, env) ? env : nullptr;
return nullptr;
return env;
} }
} // namespace electron } // namespace electron

View file

@ -217,7 +217,7 @@ void ElectronSandboxedRendererClient::WillReleaseScriptContext(
void ElectronSandboxedRendererClient::EmitProcessEvent( void ElectronSandboxedRendererClient::EmitProcessEvent(
content::RenderFrame* render_frame, content::RenderFrame* render_frame,
const char* event_name) { const char* event_name) {
if (injected_frames_.find(render_frame) == injected_frames_.end()) if (!base::Contains(injected_frames_, render_frame))
return; return;
auto* isolate = blink::MainThreadIsolate(); auto* isolate = blink::MainThreadIsolate();