From 1f580cbb6756955ad9eecfb02b75b0774097987a Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Wed, 15 Apr 2015 16:50:25 +0530 Subject: [PATCH] pm: throw when initialising module before ready event --- atom/browser/api/atom_api_power_monitor.cc | 14 ++++++++++---- atom/browser/api/atom_api_power_monitor.h | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_power_monitor.cc b/atom/browser/api/atom_api_power_monitor.cc index 905f61f590ea..58496853f13f 100644 --- a/atom/browser/api/atom_api_power_monitor.cc +++ b/atom/browser/api/atom_api_power_monitor.cc @@ -4,6 +4,7 @@ #include "atom/browser/api/atom_api_power_monitor.h" +#include "atom/browser/browser.h" #include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor_device_source.h" #include "native_mate/dictionary.h" @@ -38,8 +39,14 @@ void PowerMonitor::OnResume() { } // static -mate::Handle PowerMonitor::Create(v8::Isolate* isolate) { - return CreateHandle(isolate, new PowerMonitor); +v8::Handle PowerMonitor::Create(v8::Isolate* isolate) { + if (!Browser::Get()->is_ready()) { + node::ThrowError("Cannot initialize \"power-monitor\" module" + "before app is ready"); + return v8::Null(isolate); + } + + return CreateHandle(isolate, new PowerMonitor).ToV8(); } } // namespace api @@ -57,9 +64,8 @@ void Initialize(v8::Handle exports, v8::Handle unused, using atom::api::PowerMonitor; v8::Isolate* isolate = context->GetIsolate(); - mate::Handle power_monitor = PowerMonitor::Create(isolate); mate::Dictionary dict(isolate, exports); - dict.Set("powerMonitor", power_monitor); + dict.Set("powerMonitor", PowerMonitor::Create(isolate)); } } // namespace diff --git a/atom/browser/api/atom_api_power_monitor.h b/atom/browser/api/atom_api_power_monitor.h index 2fccc7fd311b..cdd14ff6e859 100644 --- a/atom/browser/api/atom_api_power_monitor.h +++ b/atom/browser/api/atom_api_power_monitor.h @@ -17,7 +17,7 @@ namespace api { class PowerMonitor : public mate::EventEmitter, public base::PowerObserver { public: - static mate::Handle Create(v8::Isolate* isolate); + static v8::Handle Create(v8::Isolate* isolate); protected: PowerMonitor();