diff --git a/atom.gyp b/atom.gyp index f36d586d467a..6fccc3e14ef3 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 000000000000..096f90dd32f0 --- /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 000000000000..8bcfc62adce4 --- /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 8a2dbd21daad..3a4cff2fd273 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 4402e0d9c626..db6c508389f9 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 000000000000..c7e3162667f3 --- /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 000000000000..86c3a7549f79 --- /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 855ca412e708..658152ff9286 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 aa50ef7d7d22..ace9103f6003 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 a9fce7c2ed4e..8ee1d5d3ab8a 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_