refactor: use base::flat_set
in WebContents::DidUpdateFaviconUrl()
(#46531)
* 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:
parent
1fe77cdcf2
commit
1ebad64192
3 changed files with 43 additions and 6 deletions
|
@ -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"
|
||||
|
@ -2210,13 +2211,12 @@ void WebContents::TitleWasSet(content::NavigationEntry* entry) {
|
|||
void WebContents::DidUpdateFaviconURL(
|
||||
content::RenderFrameHost* render_frame_host,
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
|
||||
#endif // ELECTRON_SHELL_COMMON_GIN_CONVERTERS_BASE_CONVERTER_H_
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <span>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
|
@ -28,6 +29,33 @@ v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, 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)
|
||||
template <>
|
||||
struct Converter<unsigned long> { // NOLINT(runtime/int)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue