From 64c9afcf770a0fa360f44099f0ce9bdd8fb5a904 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 7 Apr 2025 10:16:56 -0500 Subject: [PATCH] refactor: use `base::flat_set` in `WebContents::DidUpdateFaviconUrl()` (#46530) * refactor: add gin::Converter::ToV8() Co-authored-by: Charles Kerr * feat: add ToV8(const base::flat_set&) Co-authored-by: Charles Kerr * perf: use a flat_set in WebContents::TitleWasSet() Co-authored-by: Charles Kerr * refactor: add gin::Converter::ToV8() Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- .../browser/api/electron_api_web_contents.cc | 12 ++++---- shell/common/gin_converters/base_converter.h | 9 ++++++ shell/common/gin_converters/std_converter.h | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index abaf90658231..349eb5d1f7ab 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -16,6 +16,7 @@ #include "base/base64.h" #include "base/containers/fixed_flat_map.h" +#include "base/containers/flat_set.h" #include "base/containers/id_map.h" #include "base/files/file_util.h" #include "base/json/json_reader.h" @@ -2119,13 +2120,12 @@ void WebContents::TitleWasSet(content::NavigationEntry* entry) { void WebContents::DidUpdateFaviconURL( content::RenderFrameHost* render_frame_host, const std::vector& urls) { - std::set unique_urls; + base::flat_set unique_urls; + unique_urls.reserve(std::size(urls)); for (const auto& iter : urls) { - if (iter->icon_type != blink::mojom::FaviconIconType::kFavicon) - continue; - const GURL& url = iter->icon_url; - if (url.is_valid()) - unique_urls.insert(url); + if (iter->icon_type == blink::mojom::FaviconIconType::kFavicon && + iter->icon_url.is_valid()) + unique_urls.insert(iter->icon_url); } Emit("page-favicon-updated", unique_urls); } diff --git a/shell/common/gin_converters/base_converter.h b/shell/common/gin_converters/base_converter.h index d48da0508931..56c420d77d81 100644 --- a/shell/common/gin_converters/base_converter.h +++ b/shell/common/gin_converters/base_converter.h @@ -5,6 +5,7 @@ #ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_ #define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_ +#include "base/containers/flat_set.h" #include "base/process/kill.h" #include "gin/converter.h" #include "shell/common/gin_converters/std_converter.h" @@ -41,6 +42,14 @@ struct Converter { } }; +template +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + const base::flat_set& set) { + return Converter>::ToV8(isolate, std::span{set}); + } +}; + } // namespace gin #endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_ diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index 3eaa7eecdbca..4ea06b487de7 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -28,6 +29,33 @@ v8::Local ConvertToV8(v8::Isolate* isolate, T&& input) { isolate, std::forward(input)); } +template +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + const std::span& span) { + int idx = 0; + auto context = isolate->GetCurrentContext(); + auto result = v8::Array::New(isolate, static_cast(span.size())); + for (const auto& val : span) { + v8::MaybeLocal maybe = Converter::ToV8(isolate, val); + v8::Local element; + if (!maybe.ToLocal(&element)) + return {}; + if (!result->Set(context, idx++, element).FromMaybe(false)) + NOTREACHED() << "CreateDataProperty should always succeed here."; + } + return result; + } +}; + +template +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + const std::array& array) { + return Converter>::ToV8(isolate, std::span{array}); + } +}; + #if !BUILDFLAG(IS_LINUX) template <> struct Converter { // NOLINT(runtime/int)