From 7da34e65f7f57115292440789eef9f0f5430a0a5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 16 Apr 2013 00:25:08 +0800 Subject: [PATCH] Initial empty window javascript binding. --- atom.gyp | 4 +++ browser/api/atom_api_event_emitter.cc | 20 +++++++++++ browser/api/atom_api_event_emitter.h | 37 ++++++++++++++++++++ browser/api/atom_api_window.cc | 50 +++++++++++++++++++++++++-- browser/api/atom_api_window.h | 33 ++++++++++++++++-- browser/atom_browser_context.cc | 28 +++++++++++++++ browser/atom_browser_context.h | 28 +++++++++++++++ browser/atom_browser_main_parts.cc | 5 +++ browser/atom_browser_main_parts.h | 4 +++ browser/native_window_mac.mm | 2 ++ 10 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 browser/api/atom_api_event_emitter.cc create mode 100644 browser/api/atom_api_event_emitter.h create mode 100644 browser/atom_browser_context.cc create mode 100644 browser/atom_browser_context.h diff --git a/atom.gyp b/atom.gyp index f36d586d..6fccc3e1 100644 --- 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 index 00000000..096f90dd --- /dev/null +++ b/browser/api/atom_api_event_emitter.cc @@ -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 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 index 00000000..8bcfc62a --- /dev/null +++ b/browser/api/atom_api_event_emitter.h @@ -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& handle() { return handle_; } + + protected: + explicit EventEmitter(v8::Handle wrapper); + + private: + DISALLOW_COPY_AND_ASSIGN(EventEmitter); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 8a2dbd21..3a4cff2f 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -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 target) { - target->Set(v8::String::New("hello"), v8::String::New("world")); +namespace api { + +Window::Window(v8::Handle wrapper, base::DictionaryValue* options) + : EventEmitter(wrapper), + window_(NativeWindow::Create(AtomBrowserContext::Get(), options)) { +} + +Window::~Window() { +} + +// static +v8::Handle Window::New(const v8::Arguments &args) { + v8::HandleScope scope; + + if (!args[0]->IsObject()) + return node::ThrowTypeError("Bad argument"); + + scoped_ptr converter(V8ValueConverter::create()); + scoped_ptr 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(options.get())); + + return args.This(); } +// static +void Window::Initialize(v8::Handle target) { + v8::HandleScope scope; + + v8::Local 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) diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index 4402e0d9..db6c5083 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -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 target); + + NativeWindow* window() { return window_.get(); } + + protected: + explicit Window(v8::Handle wrapper, + base::DictionaryValue* options); + + private: + static v8::Handle New(const v8::Arguments &args); + + scoped_ptr 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 index 00000000..c7e31626 --- /dev/null +++ b/browser/atom_browser_context.cc @@ -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 index 00000000..86c3a754 --- /dev/null +++ b/browser/atom_browser_context.h @@ -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_ diff --git a/browser/atom_browser_main_parts.cc b/browser/atom_browser_main_parts.cc index 855ca412..658152ff 100644 --- a/browser/atom_browser_main_parts.cc +++ b/browser/atom_browser_main_parts.cc @@ -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(); diff --git a/browser/atom_browser_main_parts.h b/browser/atom_browser_main_parts.h index aa50ef7d..ace9103f 100644 --- a/browser/atom_browser_main_parts.h +++ b/browser/atom_browser_main_parts.h @@ -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; diff --git a/browser/native_window_mac.mm b/browser/native_window_mac.mm index a9fce7c2..8ee1d5d3 100644 --- a/browser/native_window_mac.mm +++ b/browser/native_window_mac.mm @@ -4,6 +4,8 @@ #include "browser/native_window_mac.h" +#include + // FIXME: The foundation_util.h is aborting our compilation, do not // include it. #define BASE_MAC_FOUNDATION_UTIL_H_ -- 2.43.0