Initial empty window javascript binding.
authorCheng Zhao <zcbenz@gmail.com>
Mon, 15 Apr 2013 16:25:08 +0000 (00:25 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 15 Apr 2013 16:25:08 +0000 (00:25 +0800)
atom.gyp
browser/api/atom_api_event_emitter.cc [new file with mode: 0644]
browser/api/atom_api_event_emitter.h [new file with mode: 0644]
browser/api/atom_api_window.cc
browser/api/atom_api_window.h
browser/atom_browser_context.cc [new file with mode: 0644]
browser/atom_browser_context.h [new file with mode: 0644]
browser/atom_browser_main_parts.cc
browser/atom_browser_main_parts.h
browser/native_window_mac.mm

index f36d586..6fccc3e 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -12,6 +12,8 @@
     'lib_sources': [
       'app/atom_main_delegate.cc',
       'app/atom_main_delegate.h',
+      'browser/api/atom_api_event_emitter.cc',
+      'browser/api/atom_api_event_emitter.h',
       'browser/api/atom_api_extensions.cc',
       'browser/api/atom_api_extensions.h',
       'browser/api/atom_api_window.cc',
@@ -20,6 +22,8 @@
       'browser/api/atom_bindings.h',
       'browser/atom_browser_client.cc',
       'browser/atom_browser_client.h',
+      'browser/atom_browser_context.cc',
+      'browser/atom_browser_context.h',
       'browser/atom_browser_main_parts.cc',
       'browser/atom_browser_main_parts.h',
       'browser/atom_event_processing_window.h',
diff --git a/browser/api/atom_api_event_emitter.cc b/browser/api/atom_api_event_emitter.cc
new file mode 100644 (file)
index 0000000..096f90d
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright (c) 2013 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "browser/api/atom_api_event_emitter.h"
+
+namespace atom {
+
+namespace api {
+
+EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
+  Wrap(wrapper);
+}
+
+EventEmitter::~EventEmitter() {
+}
+
+}  // namespace api
+
+}  // namespace atom
diff --git a/browser/api/atom_api_event_emitter.h b/browser/api/atom_api_event_emitter.h
new file mode 100644 (file)
index 0000000..8bcfc62
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright (c) 2013 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
+#define ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "vendor/node/src/node_object_wrap.h"
+
+namespace atom {
+
+namespace api {
+
+// Class interiting EventEmitter should assume it's a javascript object which
+// interits require('events').EventEmitter, this class provides many helper
+// methods to do event processing in C++.
+class EventEmitter : public node::ObjectWrap {
+ public:
+  virtual ~EventEmitter();
+
+  // Small accessor to return handle_, this follows Google C++ Style.
+  v8::Persistent<v8::Object>& handle() { return handle_; }
+
+ protected:
+  explicit EventEmitter(v8::Handle<v8::Object> wrapper);
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(EventEmitter);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
index 8a2dbd2..3a4cff2 100644 (file)
@@ -4,12 +4,56 @@
 
 #include "browser/api/atom_api_window.h"
 
+#include "base/values.h"
+#include "browser/atom_browser_context.h"
+#include "browser/native_window.h"
+#include "content/public/renderer/v8_value_converter.h"
+
+using content::V8ValueConverter;
+
 namespace atom {
 
-static void Initialize(v8::Handle<v8::Object> target) {
-  target->Set(v8::String::New("hello"), v8::String::New("world"));
+namespace api {
+
+Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
+    : EventEmitter(wrapper),
+      window_(NativeWindow::Create(AtomBrowserContext::Get(), options)) {
+}
+
+Window::~Window() {
+}
+
+// static
+v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
+  v8::HandleScope scope;
+
+  if (!args[0]->IsObject())
+    return node::ThrowTypeError("Bad argument");
+
+  scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
+  scoped_ptr<base::Value> options(
+      converter->FromV8Value(args[0], v8::Context::GetCurrent()));
+
+  if (!options || !options->IsType(base::Value::TYPE_DICTIONARY))
+    return node::ThrowTypeError("Bad argument");
+
+  new Window(args.This(), static_cast<base::DictionaryValue*>(options.get()));
+
+  return args.This();
 }
 
+// static
+void Window::Initialize(v8::Handle<v8::Object> target) {
+  v8::HandleScope scope;
+
+  v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(Window::New);
+  t->InstanceTemplate()->SetInternalFieldCount(1);
+  t->SetClassName(v8::String::NewSymbol("Window"));
+  target->Set(v8::String::NewSymbol("Window"), t->GetFunction());
+}
+
+}  // namespace api
+
 }  // namespace atom
 
-NODE_MODULE(atom_api_window, atom::Initialize)
+NODE_MODULE(atom_api_window, atom::api::Window::Initialize)
index 4402e0d..db6c508 100644 (file)
@@ -5,11 +5,40 @@
 #ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 
-#include "vendor/node/src/node.h"
-#include "vendor/node/src/node_object_wrap.h"
+#include "browser/api/atom_api_event_emitter.h"
+
+namespace base {
+class DictionaryValue;
+}
 
 namespace atom {
 
+class NativeWindow;
+
+namespace api {
+
+class Window : public EventEmitter {
+ public:
+  virtual ~Window();
+
+  static void Initialize(v8::Handle<v8::Object> target);
+
+  NativeWindow* window() { return window_.get(); }
+
+ protected:
+  explicit Window(v8::Handle<v8::Object> wrapper,
+                  base::DictionaryValue* options);
+
+ private:
+  static v8::Handle<v8::Value> New(const v8::Arguments &args);
+
+  scoped_ptr<NativeWindow> window_;
+
+  DISALLOW_COPY_AND_ASSIGN(Window);
+};
+
+}  // namespace api
+
 }  // namespace atom
 
 #endif  // ATOM_BROWSER_API_ATOM_API_WINDOW_H_
diff --git a/browser/atom_browser_context.cc b/browser/atom_browser_context.cc
new file mode 100644 (file)
index 0000000..c7e3162
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright (c) 2013 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "browser/atom_browser_context.h"
+
+namespace atom {
+
+// static
+AtomBrowserContext* AtomBrowserContext::self_;
+
+AtomBrowserContext::AtomBrowserContext() {
+  DCHECK(!self_);
+
+  self_ = this;
+}
+
+AtomBrowserContext::~AtomBrowserContext() {
+}
+
+// static
+AtomBrowserContext* AtomBrowserContext::Get() {
+  DCHECK(self_);
+
+  return self_;
+}
+
+}  // namespace atom
diff --git a/browser/atom_browser_context.h b/browser/atom_browser_context.h
new file mode 100644 (file)
index 0000000..86c3a75
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright (c) 2013 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
+#define ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
+
+#include "brightray/browser/browser_context.h"
+
+namespace atom {
+
+class AtomBrowserContext : public brightray::BrowserContext {
+ public:
+  AtomBrowserContext();
+  virtual ~AtomBrowserContext();
+
+  // We assume there is only one BrowserContext per browser process.
+  static AtomBrowserContext* Get();
+
+ private:
+  static AtomBrowserContext* self_;
+
+  DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_
index 855ca41..658152f 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "base/values.h"
 #include "browser/api/atom_bindings.h"
+#include "browser/atom_browser_context.h"
 #include "browser/native_window.h"
 #include "brightray/browser/browser_context.h"
 #include "brightray/browser/inspectable_web_contents.h"
@@ -23,6 +24,10 @@ AtomBrowserMainParts::AtomBrowserMainParts()
 AtomBrowserMainParts::~AtomBrowserMainParts() {
 }
 
+brightray::BrowserContext* AtomBrowserMainParts::CreateBrowserContext() {
+  return new AtomBrowserContext();
+}
+
 void AtomBrowserMainParts::PostEarlyInitialization() {
   brightray::BrowserMainParts::PostEarlyInitialization();
 
index aa50ef7..ace9103 100644 (file)
@@ -19,6 +19,10 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts {
   virtual ~AtomBrowserMainParts();
 
  protected:
+  // Implementations of brightray::BrowserMainParts.
+  virtual brightray::BrowserContext* CreateBrowserContext() OVERRIDE;
+
+  // Implementations of content::BrowserMainParts.
   virtual void PostEarlyInitialization() OVERRIDE;
   virtual void PreMainMessageLoopStart() OVERRIDE;
   virtual void PreMainMessageLoopRun() OVERRIDE;
index a9fce7c..8ee1d5d 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "browser/native_window_mac.h"
 
+#include <string>
+
 // FIXME: The foundation_util.h is aborting our compilation, do not
 // include it.
 #define BASE_MAC_FOUNDATION_UTIL_H_