diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index 675ec46a8c2a..f4a645315295 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -13,7 +13,6 @@ #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/node_includes.h" #include "base/logging.h" -#include "base/process/process_metrics.h" #include "base/sys_info.h" #include "native_mate/dictionary.h" @@ -37,6 +36,7 @@ void FatalErrorCallback(const char* location, const char* message) { AtomBindings::AtomBindings(uv_loop_t* loop) { uv_async_init(loop, &call_next_tick_async_, OnCallNextTick); call_next_tick_async_.data = this; + metrics_ = base::ProcessMetrics::CreateCurrentProcessMetrics(); } AtomBindings::~AtomBindings() { @@ -53,7 +53,8 @@ void AtomBindings::BindTo(v8::Isolate* isolate, dict.SetMethod("log", &Log); dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); - dict.SetMethod("getCPUUsage", &GetCPUUsage); + dict.SetMethod("getCPUUsage", + base::Bind(&AtomBindings::GetCPUUsage, base::Unretained(this))); dict.SetMethod("getIOCounters", &GetIOCounters); #if defined(OS_POSIX) dict.SetMethod("setFdLimit", &base::SetFdLimit); @@ -174,16 +175,12 @@ v8::Local AtomBindings::GetSystemMemoryInfo(v8::Isolate* isolate, return dict.GetHandle(); } -// static v8::Local AtomBindings::GetCPUUsage(v8::Isolate* isolate) { - std::unique_ptr metrics( - base::ProcessMetrics::CreateCurrentProcessMetrics()); - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); int processor_count = base::SysInfo::NumberOfProcessors(); dict.Set("percentCPUUsage", - metrics->GetPlatformIndependentCPUUsage() / processor_count); - dict.Set("idleWakeupsPerSecond", metrics->GetIdleWakeupsPerSecond()); + metrics_->GetPlatformIndependentCPUUsage() / processor_count); + dict.Set("idleWakeupsPerSecond", metrics_->GetIdleWakeupsPerSecond()); return dict.GetHandle(); } diff --git a/atom/common/api/atom_bindings.h b/atom/common/api/atom_bindings.h index 1deb50f7d0f2..a37497ccc5b7 100644 --- a/atom/common/api/atom_bindings.h +++ b/atom/common/api/atom_bindings.h @@ -8,6 +8,7 @@ #include #include "base/macros.h" +#include "base/process/process_metrics.h" #include "base/strings/string16.h" #include "native_mate/arguments.h" #include "v8/include/v8.h" @@ -37,7 +38,7 @@ class AtomBindings { static v8::Local GetProcessMemoryInfo(v8::Isolate* isolate); static v8::Local GetSystemMemoryInfo(v8::Isolate* isolate, mate::Arguments* args); - static v8::Local GetCPUUsage(v8::Isolate* isolate); + v8::Local GetCPUUsage(v8::Isolate* isolate); static v8::Local GetIOCounters(v8::Isolate* isolate); private: @@ -47,6 +48,7 @@ class AtomBindings { uv_async_t call_next_tick_async_; std::list pending_next_ticks_; + std::unique_ptr metrics_; DISALLOW_COPY_AND_ASSIGN(AtomBindings); };