diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index 0dd1ceab607d0..5b7e655eeb88b 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -61,20 +61,38 @@ AtomBindings::~AtomBindings() { uv_close(reinterpret_cast(&call_next_tick_async_), nullptr); } +// static +void AtomBindings::BindProcess(v8::Isolate* isolate, + mate::Dictionary* process, + base::ProcessMetrics* metrics) { + // These bindings are shared between sandboxed & unsandboxed renderers + process->SetMethod("crash", &Crash); + process->SetMethod("hang", &Hang); + process->SetMethod("log", &Log); + process->SetMethod("getCreationTime", &GetCreationTime); + process->SetMethod("getHeapStatistics", &GetHeapStatistics); + process->SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); + 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("getProcessMemoryInfo", &GetProcessMemoryInfo); - 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); @@ -82,15 +100,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 92cd9d2eb1758..b83d756e7d771 100644 --- a/atom/common/api/atom_bindings.h +++ b/atom/common/api/atom_bindings.h @@ -17,6 +17,10 @@ #include "uv.h" // NOLINT(build/include) #include "v8/include/v8.h" +namespace mate { +class Dictionary; +} + namespace memory_instrumentation { class GlobalMemoryDump; } @@ -43,8 +47,14 @@ class AtomBindings { // Should be called when a node::Environment has been destroyed. void EnvironmentDestroyed(node::Environment* env); + static void BindProcess(v8::Isolate* isolate, + mate::Dictionary* process, + base::ProcessMetrics* metrics); + static void Log(const base::string16& message); static void Crash(); + + private: static void Hang(); static v8::Local GetHeapStatistics(v8::Isolate* isolate); static v8::Local GetCreationTime(v8::Isolate* isolate); @@ -57,7 +67,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 e19d945000be9..a024f4f05c776 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -147,29 +147,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 6f9a04b47f26f..9513c15cf5483 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()` - `getProcessMemoryInfo()` - `getSystemMemoryInfo()`