From 335cd79b37a768873cdc6feb9ee4d114ec25d77f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 4 Jan 2015 22:27:02 -0800 Subject: [PATCH] Don't call Screen functions until app is ready Fixes #907. --- atom/common/api/atom_api_screen.cc | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/atom/common/api/atom_api_screen.cc b/atom/common/api/atom_api_screen.cc index 0a6e6bac4c50..1ea0e1d971a9 100644 --- a/atom/common/api/atom_api_screen.cc +++ b/atom/common/api/atom_api_screen.cc @@ -2,21 +2,41 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#include "atom/browser/browser.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/node_includes.h" namespace { +const char* kRequireAppReadyMessage = + "APIs of \"screen\" module can only be used after the \"ready\" event of " + "\"app\" module gets emitted."; + +gfx::Point GetCursorScreenPoint(mate::Arguments* args) { + if (!atom::Browser::Get()->is_ready()) { + args->ThrowError(kRequireAppReadyMessage); + return gfx::Point(); + } + + gfx::Screen* screen = gfx::Screen::GetNativeScreen(); + return screen->GetCursorScreenPoint(); +} + +gfx::Display GetPrimaryDisplay(mate::Arguments* args) { + if (!atom::Browser::Get()->is_ready()) { + args->ThrowError(kRequireAppReadyMessage); + return gfx::Display(); + } + + gfx::Screen* screen = gfx::Screen::GetNativeScreen(); + return screen->GetPrimaryDisplay(); +} + void Initialize(v8::Handle exports, v8::Handle unused, v8::Handle context, void* priv) { - gfx::Screen* screen = gfx::Screen::GetNativeScreen(); mate::Dictionary dict(context->GetIsolate(), exports); - dict.SetMethod("getCursorScreenPoint", - base::Bind(&gfx::Screen::GetCursorScreenPoint, - base::Unretained(screen))); - dict.SetMethod("getPrimaryDisplay", - base::Bind(&gfx::Screen::GetPrimaryDisplay, - base::Unretained(screen))); + dict.SetMethod("getCursorScreenPoint", &GetCursorScreenPoint); + dict.SetMethod("getPrimaryDisplay", &GetPrimaryDisplay); } } // namespace