refactor: add gin_helper::Dictionary::CreateEmpty() helper (#39547)

This commit is contained in:
Milan Burda 2023-08-21 03:43:41 +02:00 committed by GitHub
parent 9937a2bbe8
commit 09190085c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 66 additions and 64 deletions

View file

@ -215,7 +215,7 @@ static void SplitPath(const v8::FunctionCallbackInfo<v8::Value>& args) {
return;
}
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
base::FilePath asar_path, file_path;
if (asar::GetAsarArchivePath(path, &asar_path, &file_path, true)) {
dict.Set("isAsar", true);

View file

@ -643,7 +643,7 @@ void Initialize(v8::Local<v8::Object> exports,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate);
auto native_image = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("nativeImage", native_image);
native_image.SetMethod("createEmpty", &NativeImage::CreateEmpty);

View file

@ -139,7 +139,7 @@ v8::Local<v8::Value> ElectronBindings::GetHeapStatistics(v8::Isolate* isolate) {
v8::HeapStatistics v8_heap_stats;
isolate->GetHeapStatistics(&v8_heap_stats);
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("totalHeapSize",
static_cast<double>(v8_heap_stats.total_heap_size() >> 10));
@ -184,7 +184,7 @@ v8::Local<v8::Value> ElectronBindings::GetSystemMemoryInfo(
return v8::Undefined(isolate);
}
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("total", mem_info.total);
@ -235,7 +235,7 @@ v8::Local<v8::Value> ElectronBindings::GetBlinkMemoryInfo(
auto allocated = blink::ProcessHeap::TotalAllocatedObjectSize();
auto total = blink::ProcessHeap::TotalAllocatedSpace();
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("allocated", static_cast<double>(allocated >> 10));
dict.Set("total", static_cast<double>(total >> 10));
@ -266,7 +266,7 @@ void ElectronBindings::DidReceiveMemoryDump(
for (const memory_instrumentation::GlobalMemoryDump::ProcessDump& dump :
global_dump->process_dumps()) {
if (target_pid == dump.pid()) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
const auto& osdump = dump.os_dump();
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
dict.Set("residentSet", osdump.resident_set_kb);
@ -288,7 +288,7 @@ void ElectronBindings::DidReceiveMemoryDump(
v8::Local<v8::Value> ElectronBindings::GetCPUUsage(
base::ProcessMetrics* metrics,
v8::Isolate* isolate) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
int processor_count = base::SysInfo::NumberOfProcessors();
dict.Set("percentCPUUsage",
@ -309,7 +309,7 @@ v8::Local<v8::Value> ElectronBindings::GetCPUUsage(
v8::Local<v8::Value> ElectronBindings::GetIOCounters(v8::Isolate* isolate) {
auto metrics = base::ProcessMetrics::CreateCurrentProcessMetrics();
base::IoCounters io_counters;
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
if (metrics->GetIOCounters(&io_counters)) {

View file

@ -311,7 +311,7 @@ int GetKeyLocationCode(const blink::WebInputEvent& key) {
v8::Local<v8::Value> Converter<blink::WebKeyboardEvent>::ToV8(
v8::Isolate* isolate,
const blink::WebKeyboardEvent& in) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("type", in.GetType());
dict.Set("key", ui::KeycodeConverter::DomKeyToKeyString(in.dom_key));
@ -468,7 +468,7 @@ Converter<blink::mojom::ContextMenuDataInputFieldType>::ToV8(
}
v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("canUndo",
!!(editFlags & blink::ContextMenuDataEditFlags::kCanUndo));
dict.Set("canRedo",
@ -497,7 +497,7 @@ v8::Local<v8::Value> EditFlagsToV8(v8::Isolate* isolate, int editFlags) {
}
v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("inError", !!(mediaFlags & blink::ContextMenuData::kMediaInError));
dict.Set("isPaused", !!(mediaFlags & blink::ContextMenuData::kMediaPaused));
dict.Set("isMuted", !!(mediaFlags & blink::ContextMenuData::kMediaMuted));
@ -522,7 +522,7 @@ v8::Local<v8::Value> MediaFlagsToV8(v8::Isolate* isolate, int mediaFlags) {
v8::Local<v8::Value> Converter<blink::WebCacheResourceTypeStat>::ToV8(
v8::Isolate* isolate,
const blink::WebCacheResourceTypeStat& stat) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("count", static_cast<uint32_t>(stat.count));
dict.Set("size", static_cast<double>(stat.size));
dict.Set("liveSize", static_cast<double>(stat.decoded_size));
@ -532,7 +532,7 @@ v8::Local<v8::Value> Converter<blink::WebCacheResourceTypeStat>::ToV8(
v8::Local<v8::Value> Converter<blink::WebCacheResourceTypeStats>::ToV8(
v8::Isolate* isolate,
const blink::WebCacheResourceTypeStats& stats) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("images", stats.images);
dict.Set("scripts", stats.scripts);
dict.Set("cssStyleSheets", stats.css_style_sheets);
@ -565,7 +565,7 @@ bool Converter<network::mojom::ReferrerPolicy>::FromV8(
v8::Local<v8::Value> Converter<blink::mojom::Referrer>::ToV8(
v8::Isolate* isolate,
const blink::mojom::Referrer& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("url", ConvertToV8(isolate, val.url));
dict.Set("policy", ConvertToV8(isolate, val.policy));
return gin::ConvertToV8(isolate, dict);

View file

@ -125,7 +125,7 @@ v8::Local<v8::Value> Converter<ContextMenuParamsWithRenderFrameHost>::ToV8(
const ContextMenuParamsWithRenderFrameHost& val) {
const auto& params = val.first;
content::RenderFrameHost* render_frame_host = val.second;
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetGetter("frame", render_frame_host, v8::DontEnum);
dict.Set("x", params.x);
dict.Set("y", params.y);
@ -309,7 +309,7 @@ bool Converter<content::WebContents*>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> Converter<content::Referrer>::ToV8(
v8::Isolate* isolate,
const content::Referrer& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("url", ConvertToV8(isolate, val.url));
dict.Set("policy", ConvertToV8(isolate, val.policy));
return gin::ConvertToV8(isolate, dict);

View file

@ -27,7 +27,7 @@ bool Converter<file_dialog::Filter>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> Converter<file_dialog::Filter>::ToV8(
v8::Isolate* isolate,
const file_dialog::Filter& in) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("name", in.first);
dict.Set("extensions", in.second);
@ -58,7 +58,7 @@ bool Converter<file_dialog::DialogSettings>::FromV8(
v8::Local<v8::Value> Converter<file_dialog::DialogSettings>::ToV8(
v8::Isolate* isolate,
const file_dialog::DialogSettings& in) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("window",
electron::api::BrowserWindow::From(isolate, in.parent_window));

View file

@ -17,7 +17,7 @@ namespace gin {
v8::Local<v8::Value> Converter<gfx::Point>::ToV8(v8::Isolate* isolate,
const gfx::Point& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
@ -40,7 +40,7 @@ bool Converter<gfx::Point>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> Converter<gfx::PointF>::ToV8(v8::Isolate* isolate,
const gfx::PointF& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
@ -62,7 +62,7 @@ bool Converter<gfx::PointF>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> Converter<gfx::Size>::ToV8(v8::Isolate* isolate,
const gfx::Size& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("width", val.width());
dict.Set("height", val.height());
@ -84,7 +84,7 @@ bool Converter<gfx::Size>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> Converter<gfx::Rect>::ToV8(v8::Isolate* isolate,
const gfx::Rect& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
@ -141,7 +141,7 @@ struct Converter<display::Display::TouchSupport> {
v8::Local<v8::Value> Converter<display::Display>::ToV8(
v8::Isolate* isolate,
const display::Display& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("id", val.id());
dict.Set("label", val.label());

View file

@ -59,7 +59,7 @@ bool CertFromData(const std::string& data,
v8::Local<v8::Value> Converter<net::AuthChallengeInfo>::ToV8(
v8::Isolate* isolate,
const net::AuthChallengeInfo& val) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("isProxy", val.is_proxy);
dict.Set("scheme", val.scheme);
dict.Set("host", val.challenger.host());
@ -610,7 +610,7 @@ bool Converter<scoped_refptr<network::ResourceRequestBody>>::FromV8(
v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
v8::Isolate* isolate,
const network::ResourceRequest& val) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("method", val.method);
dict.Set("url", val.url.spec());
dict.Set("referrer", val.referrer.spec());
@ -624,7 +624,7 @@ v8::Local<v8::Value> Converter<network::ResourceRequest>::ToV8(
v8::Local<v8::Value> Converter<electron::VerifyRequestParams>::ToV8(
v8::Isolate* isolate,
electron::VerifyRequestParams val) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("hostname", val.hostname);
dict.Set("certificate", val.certificate);
dict.Set("validatedCertificate", val.validated_certificate);
@ -638,7 +638,7 @@ v8::Local<v8::Value> Converter<electron::VerifyRequestParams>::ToV8(
v8::Local<v8::Value> Converter<net::HttpVersion>::ToV8(
v8::Isolate* isolate,
const net::HttpVersion& val) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("major", static_cast<uint32_t>(val.major_value()));
dict.Set("minor", static_cast<uint32_t>(val.minor_value()));
return ConvertToV8(isolate, dict);
@ -648,7 +648,7 @@ v8::Local<v8::Value> Converter<net::HttpVersion>::ToV8(
v8::Local<v8::Value> Converter<net::RedirectInfo>::ToV8(
v8::Isolate* isolate,
const net::RedirectInfo& val) {
gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("statusCode", val.status_code);
dict.Set("newMethod", val.new_method);

View file

@ -16,7 +16,7 @@ struct Converter<device::mojom::SerialPortInfoPtr> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const device::mojom::SerialPortInfoPtr& port) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("portId", port->token.ToString());
dict.Set("portName", port->path.BaseName().LossyDisplayName());
if (port->display_name && !port->display_name->empty())

View file

@ -126,7 +126,7 @@ v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate>::New(isolate, g_call_translater);
auto* holder = new TranslaterHolder(isolate);
holder->translater = translater;
gin::Dictionary state = gin::Dictionary::CreateEmpty(isolate);
auto state = gin::Dictionary::CreateEmpty(isolate);
if (one_time)
state.Set("oneTime", true);
auto context = isolate->GetCurrentContext();

View file

@ -31,6 +31,10 @@ class Dictionary : public gin::Dictionary {
Dictionary(const gin::Dictionary& dict) // NOLINT(runtime/explicit)
: gin::Dictionary(dict) {}
static Dictionary CreateEmpty(v8::Isolate* isolate) {
return gin::Dictionary::CreateEmpty(isolate);
}
// Differences from the Get method in gin::Dictionary:
// 1. This is a const method;
// 2. It checks whether the key exists before reading;