Browse Source

refactor: eliminate duplicate code exposing process APIs (#15824)

Milan Burda 6 years ago
parent
commit
07f8f5f95a

+ 28 - 19
atom/common/api/atom_bindings.cc

@@ -61,20 +61,38 @@ AtomBindings::~AtomBindings() {
   uv_close(reinterpret_cast<uv_handle_t*>(&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<v8::Object> 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<v8::Object> 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);

+ 10 - 1
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<v8::Value> GetHeapStatistics(v8::Isolate* isolate);
   static v8::Local<v8::Value> 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);

+ 1 - 17
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))

+ 1 - 0
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()`