Skip to content

Commit

Permalink
refactor: eliminate duplicate code exposing process APIs (#15824)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak authored and codebytere committed Nov 28, 2018
1 parent d7d4b86 commit 07f8f5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
47 changes: 28 additions & 19 deletions atom/common/api/atom_bindings.cc
Expand Up @@ -61,36 +61,45 @@ 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);
#endif
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);
Expand Down
11 changes: 10 additions & 1 deletion atom/common/api/atom_bindings.h
Expand Up @@ -17,6 +17,10 @@
#include "uv.h" // NOLINT(build/include)
#include "v8/include/v8.h"

namespace mate {
class Dictionary;
}

namespace memory_instrumentation {
class GlobalMemoryDump;
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
18 changes: 1 addition & 17 deletions atom/renderer/atom_sandboxed_renderer_client.cc
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions docs/api/process.md
Expand Up @@ -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()`
Expand Down

0 comments on commit 07f8f5f

Please sign in to comment.