refactor: use base::flat_set in WebContents::DidUpdateFaviconUrl() (#46530)

* refactor: add gin::Converter<std::span>::ToV8()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* feat: add ToV8(const base::flat_set<T>&)

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* perf: use a flat_set in WebContents::TitleWasSet()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: add gin::Converter<std::array>::ToV8()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-04-07 10:16:56 -05:00 committed by GitHub
parent 4392cb9434
commit 64c9afcf77
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 6 deletions

View file

@ -16,6 +16,7 @@
#include "base/base64.h" #include "base/base64.h"
#include "base/containers/fixed_flat_map.h" #include "base/containers/fixed_flat_map.h"
#include "base/containers/flat_set.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"
@ -2119,13 +2120,12 @@ void WebContents::TitleWasSet(content::NavigationEntry* entry) {
void WebContents::DidUpdateFaviconURL( void WebContents::DidUpdateFaviconURL(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const std::vector<blink::mojom::FaviconURLPtr>& urls) { const std::vector<blink::mojom::FaviconURLPtr>& urls) {
std::set<GURL> unique_urls; base::flat_set<GURL> unique_urls;
unique_urls.reserve(std::size(urls));
for (const auto& iter : urls) { for (const auto& iter : urls) {
if (iter->icon_type != blink::mojom::FaviconIconType::kFavicon) if (iter->icon_type == blink::mojom::FaviconIconType::kFavicon &&
continue; iter->icon_url.is_valid())
const GURL& url = iter->icon_url; unique_urls.insert(iter->icon_url);
if (url.is_valid())
unique_urls.insert(url);
} }
Emit("page-favicon-updated", unique_urls); Emit("page-favicon-updated", unique_urls);
} }

View file

@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_ #ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_
#define 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 "base/process/kill.h"
#include "gin/converter.h" #include "gin/converter.h"
#include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_converters/std_converter.h"
@ -41,6 +42,14 @@ struct Converter<base::TerminationStatus> {
} }
}; };
template <typename T>
struct Converter<base::flat_set<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::flat_set<T>& set) {
return Converter<std::span<T>>::ToV8(isolate, std::span{set});
}
};
} // namespace gin } // namespace gin
#endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_ #endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_

View file

@ -9,6 +9,7 @@
#include <functional> #include <functional>
#include <map> #include <map>
#include <set> #include <set>
#include <span>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -28,6 +29,33 @@ v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T&& input) {
isolate, std::forward<T>(input)); isolate, std::forward<T>(input));
} }
template <typename T>
struct Converter<std::span<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::span<const T>& span) {
int idx = 0;
auto context = isolate->GetCurrentContext();
auto result = v8::Array::New(isolate, static_cast<int>(span.size()));
for (const auto& val : span) {
v8::MaybeLocal<v8::Value> maybe = Converter<T>::ToV8(isolate, val);
v8::Local<v8::Value> element;
if (!maybe.ToLocal(&element))
return {};
if (!result->Set(context, idx++, element).FromMaybe(false))
NOTREACHED() << "CreateDataProperty should always succeed here.";
}
return result;
}
};
template <typename T, size_t N>
struct Converter<std::array<T, N>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::array<T, N>& array) {
return Converter<std::span<T>>::ToV8(isolate, std::span{array});
}
};
#if !BUILDFLAG(IS_LINUX) #if !BUILDFLAG(IS_LINUX)
template <> template <>
struct Converter<unsigned long> { // NOLINT(runtime/int) struct Converter<unsigned long> { // NOLINT(runtime/int)