Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eliminate duplicate code exposing process APIs #15824

Merged
merged 1 commit into from Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
miniak marked this conversation as resolved.
Show resolved Hide resolved
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);
ppontes marked this conversation as resolved.
Show resolved Hide resolved
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