Move "id" and "fromId" to C++
authorCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 08:37:48 +0000 (16:37 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 08:37:48 +0000 (16:37 +0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/api/lib/browser-window.coffee
vendor/native_mate

index 21c6b32..317d5e4 100644 (file)
@@ -435,6 +435,10 @@ bool Window::IsVisibleOnAllWorkspaces() {
   return window_->IsVisibleOnAllWorkspaces();
 }
 
+int32_t Window::ID() const {
+  return weak_map_id();
+}
+
 v8::Local<v8::Value> Window::WebContents(v8::Isolate* isolate) {
   if (web_contents_.IsEmpty()) {
     auto handle =
@@ -518,6 +522,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("showDefinitionForSelection",
                  &Window::ShowDefinitionForSelection)
 #endif
+      .SetProperty("id", &Window::ID)
       .SetProperty("webContents", &Window::WebContents)
       .SetProperty("devToolsWebContents", &Window::DevToolsWebContents);
 }
@@ -529,14 +534,18 @@ void Window::BuildPrototype(v8::Isolate* isolate,
 
 namespace {
 
+using atom::api::Window;
+
 void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
                 v8::Local<v8::Context> context, void* priv) {
-  using atom::api::Window;
   v8::Isolate* isolate = context->GetIsolate();
   v8::Local<v8::Function> constructor = mate::CreateConstructor<Window>(
       isolate, "BrowserWindow", base::Bind(&Window::New));
+  mate::Dictionary browser_window(isolate, constructor);
+  browser_window.SetMethod("fromId", &mate::TrackableObject::FromWeakMapID);
+
   mate::Dictionary dict(isolate, exports);
-  dict.Set("BrowserWindow", static_cast<v8::Local<v8::Value>>(constructor));
+  dict.Set("BrowserWindow", browser_window);
 }
 
 }  // namespace
index d2b9d28..c6a3dd6 100644 (file)
@@ -10,8 +10,8 @@
 
 #include "base/memory/scoped_ptr.h"
 #include "ui/gfx/image/image.h"
+#include "atom/browser/api/trackable_object.h"
 #include "atom/browser/native_window_observer.h"
-#include "atom/browser/api/event_emitter.h"
 #include "native_mate/handle.h"
 
 class GURL;
@@ -37,7 +37,7 @@ namespace api {
 
 class WebContents;
 
-class Window : public mate::EventEmitter,
+class Window : public mate::TrackableObject,
                public NativeWindowObserver {
  public:
   static mate::Wrappable* New(v8::Isolate* isolate,
@@ -147,6 +147,7 @@ class Window : public mate::EventEmitter,
   void SetVisibleOnAllWorkspaces(bool visible);
   bool IsVisibleOnAllWorkspaces();
 
+  int32_t ID() const;
   v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
   v8::Local<v8::Value> DevToolsWebContents(v8::Isolate* isolate);
 
index 475b98d..68144dd 100644 (file)
@@ -15,11 +15,6 @@ BrowserWindow::_init = ->
     menu = app.getApplicationMenu()
     @setMenu menu if menu?
 
-  # Remember the window ID.
-  Object.defineProperty this, 'id',
-    value: BrowserWindow.windows.add(this)
-    enumerable: true
-
   # Make new windows requested by links behave like "window.open"
   @on '-new-window', (event, url, frameName) =>
     event.sender = @webContents
@@ -63,9 +58,6 @@ BrowserWindow.fromDevToolsWebContents = (webContents) ->
   windows = BrowserWindow.getAllWindows()
   return window for window in windows when window.devToolsWebContents?.equal webContents
 
-BrowserWindow.fromId = (id) ->
-  BrowserWindow.windows.get id if BrowserWindow.windows.has id
-
 # Helpers.
 BrowserWindow::loadUrl = -> @webContents.loadUrl.apply @webContents, arguments
 BrowserWindow::send = -> @webContents.send.apply @webContents, arguments
index cad1fa5..cc4e2fc 160000 (submodule)
@@ -1 +1 @@
-Subproject commit cad1fa50a95ca4185c435846e4868d5bd6cc94df
+Subproject commit cc4e2fcd94b5a22e6720f0fba1c586a89640f1f6