]> khamidou — GitHub mirrors - electron.git/commitdiff
Avoid using app API in renderer process for #907
authorCheng Zhao <zcbenz@gmail.com>
Mon, 5 Jan 2015 06:45:51 +0000 (22:45 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 5 Jan 2015 06:51:00 +0000 (22:51 -0800)
atom/common/api/atom_api_screen.cc
atom/common/api/lib/screen.coffee

index 1ea0e1d971a93482bf40dbc5fa8ea0ea6048b2a6..0a6e6bac4c50aa052b773eddc53d6f989f510ae9 100644 (file)
@@ -2,41 +2,21 @@
 // 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<v8::Object> exports, v8::Handle<v8::Value> unused,
                 v8::Handle<v8::Context> context, void* priv) {
+  gfx::Screen* screen = gfx::Screen::GetNativeScreen();
   mate::Dictionary dict(context->GetIsolate(), exports);
-  dict.SetMethod("getCursorScreenPoint", &GetCursorScreenPoint);
-  dict.SetMethod("getPrimaryDisplay", &GetPrimaryDisplay);
+  dict.SetMethod("getCursorScreenPoint",
+                 base::Bind(&gfx::Screen::GetCursorScreenPoint,
+                            base::Unretained(screen)));
+  dict.SetMethod("getPrimaryDisplay",
+                 base::Bind(&gfx::Screen::GetPrimaryDisplay,
+                            base::Unretained(screen)));
 }
 
 }  // namespace
index da7701e55a4e003e8447ee08c61b872659f3b15f..e2913fcb8a9360230b982ad36ed6de12d761a587 100644 (file)
@@ -1,6 +1,17 @@
+binding = process.atomBinding 'screen'
+
+checkAppIsReady = ->
+  unless process.type is 'renderer' or require('app').isReady()
+    throw new Error('Can not use screen module before the "ready" event of app module gets emitted')
+
 module.exports =
   if process.platform in ['linux', 'win32'] and process.type is 'renderer'
     # On Linux we could not access screen in renderer process.
     require('remote').require 'screen'
   else
-    process.atomBinding 'screen'
+    getCursorScreenPoint: ->
+      checkAppIsReady()
+      binding.getCursorScreenPoint()
+    getPrimaryDisplay: ->
+      checkAppIsReady()
+      binding.getPrimaryDisplay()