From 811701d805aee445567347ca481c55d19602a54c Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Sat, 24 Nov 2018 12:54:34 +0100 Subject: [PATCH] refactor: eliminate duplicate code exposing process APIs --- atom/common/api/atom_bindings.cc | 44 +++++++++++-------- atom/common/api/atom_bindings.h | 13 +++++- .../atom_sandboxed_renderer_client.cc | 18 +------- docs/api/process.md | 1 + 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index 991cbd96943be..3a58080aa8ca3 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -51,19 +51,36 @@ AtomBindings::~AtomBindings() { uv_close(reinterpret_cast(&call_next_tick_async_), nullptr); } +// static +void AtomBindings::BindProcess(v8::Isolate* isolate, + mate::Dictionary* process, + base::ProcessMetrics* metrics) { + process->SetMethod("crash", &Crash); + process->SetMethod("hang", &Hang); + process->SetMethod("log", &Log); + process->SetMethod("getCreationTime", &GetCreationTime); + process->SetMethod("getHeapStatistics", &GetHeapStatistics); + process->SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); + process->SetMethod("getIOCounters", &GetIOCounters); + process->SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, + base::Unretained(metrics))); + +#if defined(MAS_BUILD) + process->SetReadOnly("mas", true); +#endif + +#if defined(OS_WIN) + if (IsRunningInDesktopBridge()) + process->SetReadOnly("windowsStore", true); +#endif +} + void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local process) { isolate->SetFatalErrorHandler(FatalErrorCallback); mate::Dictionary dict(isolate, process); - dict.SetMethod("crash", &AtomBindings::Crash); - dict.SetMethod("hang", &Hang); - dict.SetMethod("log", &Log); - dict.SetMethod("getHeapStatistics", &GetHeapStatistics); - dict.SetMethod("getCreationTime", &GetCreationTime); - dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); - dict.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, - base::Unretained(metrics_.get()))); - dict.SetMethod("getIOCounters", &GetIOCounters); + BindProcess(isolate, &dict, metrics_.get()); + dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot); #if defined(OS_POSIX) dict.SetMethod("setFdLimit", &base::IncreaseFdLimitTo); @@ -71,15 +88,6 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local process) { dict.SetMethod("activateUvLoop", base::Bind(&AtomBindings::ActivateUVLoop, base::Unretained(this))); -#if defined(MAS_BUILD) - dict.SetReadOnly("mas", true); -#endif - -#if defined(OS_WIN) - if (IsRunningInDesktopBridge()) - dict.SetReadOnly("windowsStore", true); -#endif - mate::Dictionary versions; if (dict.Get("versions", &versions)) { versions.SetReadOnly(ATOM_PROJECT_NAME, ATOM_VERSION_STRING); diff --git a/atom/common/api/atom_bindings.h b/atom/common/api/atom_bindings.h index fd08c2dc1b6f9..1218fe25f4394 100644 --- a/atom/common/api/atom_bindings.h +++ b/atom/common/api/atom_bindings.h @@ -16,6 +16,10 @@ #include "uv.h" // NOLINT(build/include) #include "v8/include/v8.h" +namespace mate { +class Dictionary; +} + namespace node { class Environment; } @@ -34,8 +38,14 @@ class AtomBindings { // Should be called when a node::Environment has been destroyed. void EnvironmentDestroyed(node::Environment* env); - static void Log(const base::string16& message); + static void BindProcess(v8::Isolate* isolate, + mate::Dictionary* process, + base::ProcessMetrics* metrics); + static void Crash(); + + private: + static void Log(const base::string16& message); static void Hang(); static v8::Local GetHeapStatistics(v8::Isolate* isolate); static v8::Local GetCreationTime(v8::Isolate* isolate); @@ -47,7 +57,6 @@ class AtomBindings { static bool TakeHeapSnapshot(v8::Isolate* isolate, const base::FilePath& file_path); - private: void ActivateUVLoop(v8::Isolate* isolate); static void OnCallNextTick(uv_async_t* handle); diff --git a/atom/renderer/atom_sandboxed_renderer_client.cc b/atom/renderer/atom_sandboxed_renderer_client.cc index 940cc9b00b709..df66d598a844d 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -146,29 +146,13 @@ void AtomSandboxedRendererClient::InitializeBindings( mate::Dictionary process = mate::Dictionary::CreateEmpty(isolate); b.Set("process", process); - process.SetMethod("crash", AtomBindings::Crash); - process.SetMethod("hang", AtomBindings::Hang); - process.SetMethod("getHeapStatistics", &AtomBindings::GetHeapStatistics); - process.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo); - process.SetMethod( - "getCPUUsage", - base::Bind(&AtomBindings::GetCPUUsage, base::Unretained(metrics_.get()))); - process.SetMethod("getIOCounters", &AtomBindings::GetIOCounters); + AtomBindings::BindProcess(isolate, &process, metrics_.get()); process.Set("argv", base::CommandLine::ForCurrentProcess()->argv()); process.SetReadOnly("pid", base::GetCurrentProcId()); process.SetReadOnly("sandboxed", true); process.SetReadOnly("type", "renderer"); -#if defined(MAS_BUILD) - process.SetReadOnly("mas", true); -#endif - -#if defined(OS_WIN) - if (IsRunningInDesktopBridge()) - process.SetReadOnly("windowsStore", true); -#endif - // Pass in CLI flags needed to setup the renderer base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); if (command_line->HasSwitch(switches::kGuestInstanceID)) diff --git a/docs/api/process.md b/docs/api/process.md index c8c2560ff6215..1f227881af6a4 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -13,6 +13,7 @@ It adds the following events, properties, and methods: In sandboxed renderers the `process` object contains only a subset of the APIs: - `crash()` - `hang()` +- `getCreationTime()` - `getHeapStatistics()` - `getSystemMemoryInfo()` - `getCPUUsage()`