Skip to content

Commit 335cd79

Browse files
committed
Don't call Screen functions until app is ready
Fixes electron#907.
1 parent e3bad23 commit 335cd79

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

atom/common/api/atom_api_screen.cc

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,41 @@
22
// Use of this source code is governed by the MIT license that can be
33
// found in the LICENSE file.
44

5+
#include "atom/browser/browser.h"
56
#include "atom/common/native_mate_converters/gfx_converter.h"
67
#include "atom/common/node_includes.h"
78

89
namespace {
910

11+
const char* kRequireAppReadyMessage =
12+
"APIs of \"screen\" module can only be used after the \"ready\" event of "
13+
"\"app\" module gets emitted.";
14+
15+
gfx::Point GetCursorScreenPoint(mate::Arguments* args) {
16+
if (!atom::Browser::Get()->is_ready()) {
17+
args->ThrowError(kRequireAppReadyMessage);
18+
return gfx::Point();
19+
}
20+
21+
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
22+
return screen->GetCursorScreenPoint();
23+
}
24+
25+
gfx::Display GetPrimaryDisplay(mate::Arguments* args) {
26+
if (!atom::Browser::Get()->is_ready()) {
27+
args->ThrowError(kRequireAppReadyMessage);
28+
return gfx::Display();
29+
}
30+
31+
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
32+
return screen->GetPrimaryDisplay();
33+
}
34+
1035
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
1136
v8::Handle<v8::Context> context, void* priv) {
12-
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
1337
mate::Dictionary dict(context->GetIsolate(), exports);
14-
dict.SetMethod("getCursorScreenPoint",
15-
base::Bind(&gfx::Screen::GetCursorScreenPoint,
16-
base::Unretained(screen)));
17-
dict.SetMethod("getPrimaryDisplay",
18-
base::Bind(&gfx::Screen::GetPrimaryDisplay,
19-
base::Unretained(screen)));
38+
dict.SetMethod("getCursorScreenPoint", &GetCursorScreenPoint);
39+
dict.SetMethod("getPrimaryDisplay", &GetPrimaryDisplay);
2040
}
2141

2242
} // namespace

0 commit comments

Comments
 (0)