]> khamidou — GitHub mirrors - electron.git/commitdiff
Make BrowserWindow inheritable. Fixed #23.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 18 Jun 2013 10:35:08 +0000 (18:35 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 18 Jun 2013 10:35:08 +0000 (18:35 +0800)
browser/api/atom_api_event_emitter.cc
browser/api/atom_api_event_emitter.h
browser/api/atom_api_window.cc
browser/api/atom_api_window.h
browser/api/lib/browser-window.coffee

index 48f27cdd209416a1d05902a22ba979d143fa928a..13acafe0b03703cc243bd89bf9973215c726a298 100644 (file)
@@ -71,6 +71,26 @@ bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
   return prevent_default;
 }
 
+// static
+v8::Handle<v8::Value> EventEmitter::FromConstructorTemplate(
+    v8::Persistent<v8::FunctionTemplate> t,
+    const v8::Arguments& args) {
+  v8::Handle<v8::Value> new_object = node::FromConstructorTemplate(t, args);
+  args.This()->SetHiddenValue(v8::String::New("native_object"), new_object);
+  return new_object;
+}
+
+// static
+v8::Handle<v8::Object> EventEmitter::SearchNativeObject(
+    v8::Handle<v8::Object> handle) {
+  if (handle->InternalFieldCount() > 0)
+    return handle;
+
+  // If the object is not a native object, we assume the native object is
+  // stored in it.
+  return handle->GetHiddenValue(v8::String::New("native_object"))->ToObject();
+}
+
 }  // namespace api
 
 }  // namespace atom
index af514503dca11b5fa78191e8309d91ec9762196e..fc3853f9faabe3792be97bfacee3deeff55bb4f6 100644 (file)
@@ -29,12 +29,37 @@ class EventEmitter : public node::ObjectWrap {
   bool Emit(const std::string& name);
   bool Emit(const std::string& name, base::ListValue* args);
 
+  // Same with Unwrap but if the handle is not a native object, which happened
+  // when user inherits a native class, we would search for the native object,
+  // otherwise we won't get the correct C++ object.
+  template <typename T>
+  static inline bool SafeUnwrap(v8::Handle<v8::Object> handle, T** pointer) {
+    v8::Handle<v8::Object> real_handle = SearchNativeObject(handle);
+    if (real_handle->InternalFieldCount() == 0)
+      return false;
+
+    *pointer = Unwrap<T>(real_handle);
+    return true;
+  }
+
+  // Helper function to call "new" when constructor is called as normal
+  // function, this usually happens when inheriting native class.
+  //
+  // This function also does some special hacks to make sure the inheritance
+  // would work, because normal inheritance would not inherit internal fields.
+  static v8::Handle<v8::Value> FromConstructorTemplate(
+      v8::Persistent<v8::FunctionTemplate> t,
+      const v8::Arguments& args);
+
   // Small accessor to return handle_, this follows Google C++ Style.
   v8::Persistent<v8::Object> handle() const { return handle_; }
 
  protected:
   explicit EventEmitter(v8::Handle<v8::Object> wrapper);
 
+  static v8::Handle<v8::Object> SearchNativeObject(
+      v8::Handle<v8::Object> handle);
+
  private:
   DISALLOW_COPY_AND_ASSIGN(EventEmitter);
 };
index 0f4f870fde47939fe0bb443ed888e53dccfc6b4b..1c89a7722c20d443205a8f906287a001e88c5359 100644 (file)
@@ -19,9 +19,13 @@ using content::NavigationController;
 using node::ObjectWrap;
 
 #define UNWRAP_WINDOW_AND_CHECK \
-  Window* self = ObjectWrap::Unwrap<Window>(args.This()); \
-  if (self == NULL) \
-    return node::ThrowError("Window is already destroyed")
+  Window* self; \
+  if (SafeUnwrap<Window>(args.This(), &self)) { \
+    if (self == NULL) \
+      return node::ThrowError("Window is already destroyed"); \
+  } else { \
+    return node::ThrowTypeError("Window methods are not generic");\
+  }
 
 namespace atom {
 
@@ -36,6 +40,9 @@ v8::Handle<v8::String> UTF16ToV8String(const string16& s) {
 
 }  // namespace
 
+// static
+v8::Persistent<v8::FunctionTemplate> Window::constructor_template_;
+
 Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
     : EventEmitter(wrapper),
       window_(NativeWindow::Create(options)) {
@@ -85,10 +92,10 @@ void Window::OnRendererCrashed() {
 
 // static
 v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
-  v8::HandleScope scope;
-
   if (!args.IsConstructCall())
-    return node::ThrowError("Require constructor call");
+    return FromConstructorTemplate(constructor_template_, args);
+
+  v8::HandleScope scope;
 
   if (!args[0]->IsObject())
     return node::ThrowTypeError("Need options creating Window");
@@ -643,7 +650,10 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
 
   v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(Window::New);
   t->InstanceTemplate()->SetInternalFieldCount(1);
-  t->SetClassName(v8::String::NewSymbol("BrowserWindow"));
+  t->SetClassName(v8::String::NewSymbol("Window"));
+
+  constructor_template_ = v8::Persistent<v8::FunctionTemplate>::New(
+      node::node_isolate, t);
 
   NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy);
   NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
@@ -703,7 +713,7 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
   NODE_SET_PROTOTYPE_METHOD(t, "reload", Reload);
   NODE_SET_PROTOTYPE_METHOD(t, "reloadIgnoringCache", ReloadIgnoringCache);
 
-  target->Set(v8::String::NewSymbol("BrowserWindow"), t->GetFunction());
+  target->Set(v8::String::NewSymbol("Window"), t->GetFunction());
 }
 
 }  // namespace api
index c9ffec2135266caab76892036974fadc6d2499b2..01c4f7ad85694da8ad37268d6df4db2b546a7216 100644 (file)
@@ -105,6 +105,8 @@ class Window : public EventEmitter,
   static v8::Handle<v8::Value> Reload(const v8::Arguments &args);
   static v8::Handle<v8::Value> ReloadIgnoringCache(const v8::Arguments &args);
 
+  static v8::Persistent<v8::FunctionTemplate> constructor_template_;
+
   scoped_ptr<NativeWindow> window_;
 
   DISALLOW_COPY_AND_ASSIGN(Window);
index 452151c7bcd7fc8ab153ad5910a91687f0188a2f..9c96add7a52f598ce4a2781b77d46ffc7e174858 100644 (file)
@@ -2,28 +2,30 @@ EventEmitter = require('events').EventEmitter
 v8Util = process.atomBinding 'v8_util'
 objectsRegistry = require '../../atom/objects-registry.js'
 
-BrowserWindow = process.atomBinding('window').BrowserWindow
-BrowserWindow::__proto__ = EventEmitter.prototype
+Window = process.atomBinding('window').Window
+Window::__proto__ = EventEmitter.prototype
 
-BrowserWindow::toggleDevTools = ->
-  opened = v8Util.getHiddenValue this, 'devtoolsOpened'
-  if opened
-    @closeDevTools()
-    v8Util.setHiddenValue this, 'devtoolsOpened', false
-  else
-    @openDevTools()
-    v8Util.setHiddenValue this, 'devtoolsOpened', true
+module.exports = class BrowserWindow extends Window
+  constructor: ->
+    super
 
-BrowserWindow::restart = ->
-  @loadUrl(@getUrl())
+  toggleDevTools: ->
+    opened = v8Util.getHiddenValue this, 'devtoolsOpened'
+    if opened
+      @closeDevTools()
+      v8Util.setHiddenValue this, 'devtoolsOpened', false
+    else
+      @openDevTools()
+      v8Util.setHiddenValue this, 'devtoolsOpened', true
 
-BrowserWindow.getFocusedWindow = ->
-  windows = objectsRegistry.getAllWindows()
-  return window for window in windows when window.isFocused()
+  restart: ->
+    @loadUrl @getUrl()
 
-BrowserWindow.fromProcessIdAndRoutingId = (processId, routingId) ->
-  windows = objectsRegistry.getAllWindows()
-  return window for window in windows when window.getProcessId() == processId and
-                                           window.getRoutingId() == routingId
+  @getFocusedWindow: ->
+    windows = objectsRegistry.getAllWindows()
+    return window for window in windows when window.isFocused()
 
-module.exports = BrowserWindow
+  @fromProcessIdAndRoutingId = (processId, routingId) ->
+    windows = objectsRegistry.getAllWindows()
+    return window for window in windows when window.getProcessId() == processId and
+                                             window.getRoutingId() == routingId