diff --git a/atom.gyp b/atom.gyp index c59fbd1293fc..4317261b13c8 100644 --- a/atom.gyp +++ b/atom.gyp @@ -19,6 +19,10 @@ 'browser/api/atom_api_event_emitter.h', 'browser/api/atom_api_extensions.cc', 'browser/api/atom_api_extensions.h', + 'browser/api/atom_api_objects_registry.cc', + 'browser/api/atom_api_objects_registry.h', + 'browser/api/atom_api_recorded_object.cc', + 'browser/api/atom_api_recorded_object.h', 'browser/api/atom_api_window.cc', 'browser/api/atom_api_window.h', 'browser/api/atom_bindings.cc', diff --git a/browser/api/atom_api_event_emitter.cc b/browser/api/atom_api_event_emitter.cc index 6fe790b9ecd6..7a88a1eb710b 100644 --- a/browser/api/atom_api_event_emitter.cc +++ b/browser/api/atom_api_event_emitter.cc @@ -16,8 +16,8 @@ namespace atom { namespace api { -EventEmitter::EventEmitter(v8::Handle wrapper) { - Wrap(wrapper); +EventEmitter::EventEmitter(v8::Handle wrapper) + : RecordedObject(wrapper) { } EventEmitter::~EventEmitter() { diff --git a/browser/api/atom_api_event_emitter.h b/browser/api/atom_api_event_emitter.h index 4071355d1b56..021d91c7fe2a 100644 --- a/browser/api/atom_api_event_emitter.h +++ b/browser/api/atom_api_event_emitter.h @@ -7,8 +7,7 @@ #include -#include "base/basictypes.h" -#include "vendor/node/src/node_object_wrap.h" +#include "browser/api/atom_api_recorded_object.h" namespace base { class ListValue; @@ -21,16 +20,13 @@ 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 { +class EventEmitter : public RecordedObject { public: virtual ~EventEmitter(); // Emit an event and returns whether the handler has called preventDefault(). bool Emit(const std::string& name, base::ListValue* args); - // Small accessor to return handle_, this follows Google C++ Style. - v8::Persistent& handle() { return handle_; } - protected: explicit EventEmitter(v8::Handle wrapper); diff --git a/browser/api/atom_api_objects_registry.cc b/browser/api/atom_api_objects_registry.cc new file mode 100644 index 000000000000..71e7efd815e2 --- /dev/null +++ b/browser/api/atom_api_objects_registry.cc @@ -0,0 +1,19 @@ +// 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_objects_registry.h" + +namespace atom { + +namespace api { + +ObjectsRegistry::ObjectsRegistry() { +} + +ObjectsRegistry::~ObjectsRegistry() { +} + +} // namespace api + +} // namespace atom diff --git a/browser/api/atom_api_objects_registry.h b/browser/api/atom_api_objects_registry.h new file mode 100644 index 000000000000..a92bb9a54dbb --- /dev/null +++ b/browser/api/atom_api_objects_registry.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_OBJECTS_REGISTRY_H_ +#define ATOM_BROWSER_API_ATOM_API_OBJECTS_REGISTRY_H_ + +#include "base/id_map.h" +#include "base/basictypes.h" + +namespace atom { + +namespace api { + +class RecordedObject; + +class ObjectsRegistry { + public: + ObjectsRegistry(); + virtual ~ObjectsRegistry(); + + int Add(RecordedObject* data) { return id_map_.Add(data); } + void Remove(int id) { id_map_.Remove(id); } + void Clear() { id_map_.Clear(); } + RecordedObject* Lookup(int id) const { return id_map_.Lookup(id); } + + private: + IDMap id_map_; + + DISALLOW_COPY_AND_ASSIGN(ObjectsRegistry); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_OBJECTS_REGISTRY_H_ diff --git a/browser/api/atom_api_recorded_object.cc b/browser/api/atom_api_recorded_object.cc new file mode 100644 index 000000000000..f530baf5c8e7 --- /dev/null +++ b/browser/api/atom_api_recorded_object.cc @@ -0,0 +1,40 @@ +// 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_RECORDED_OBJECT_ +#define ATOM_BROWSER_API_ATOM_API_RECORDED_OBJECT_ + +#include "browser/api/atom_api_recorded_object.h" + +#include "base/compiler_specific.h" +#include "browser/api/atom_api_objects_registry.h" +#include "browser/atom_browser_context.h" + +namespace atom { + +namespace api { + +RecordedObject::RecordedObject(v8::Handle wrapper) + : ALLOW_THIS_IN_INITIALIZER_LIST(id_( + AtomBrowserContext::Get()->objects_registry()->Add(this))) { + Wrap(wrapper); + + wrapper->SetAccessor(v8::String::New("id"), IDGetter); +} + +RecordedObject::~RecordedObject() { +} + +// static +v8::Handle RecordedObject::IDGetter(v8::Local property, + const v8::AccessorInfo& info) { + RecordedObject* self = RecordedObject::Unwrap(info.This()); + return v8::Integer::New(self->id_); +} + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_RECORDED_OBJECT_ diff --git a/browser/api/atom_api_recorded_object.h b/browser/api/atom_api_recorded_object.h new file mode 100644 index 000000000000..a5806ee8f9a2 --- /dev/null +++ b/browser/api/atom_api_recorded_object.h @@ -0,0 +1,42 @@ +// 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_RECORDED_OBJECT_H_ +#define ATOM_BROWSER_API_ATOM_API_RECORDED_OBJECT_H_ + +#include "base/basictypes.h" +#include "vendor/node/src/node_object_wrap.h" + +namespace atom { + +namespace api { + +// Objects of this class will be recorded in C++ and available for RPC from +// renderer. +class RecordedObject : public node::ObjectWrap { + public: + virtual ~RecordedObject(); + + // Small accessor to return handle_, this follows Google C++ Style. + v8::Persistent& handle() { return handle_; } + + int id() const { return id_; } + + protected: + explicit RecordedObject(v8::Handle wrapper); + + private: + static v8::Handle IDGetter(v8::Local property, + const v8::AccessorInfo& info); + + int id_; + + DISALLOW_COPY_AND_ASSIGN(RecordedObject); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_BROWSER_API_ATOM_API_RECORDED_OBJECT_H_ diff --git a/browser/atom_browser_context.cc b/browser/atom_browser_context.cc index c7e3162667f3..0d43f9ba930a 100644 --- a/browser/atom_browser_context.cc +++ b/browser/atom_browser_context.cc @@ -4,12 +4,15 @@ #include "browser/atom_browser_context.h" +#include "browser/api/atom_api_objects_registry.h" + namespace atom { // static AtomBrowserContext* AtomBrowserContext::self_; -AtomBrowserContext::AtomBrowserContext() { +AtomBrowserContext::AtomBrowserContext() + : objects_registry_(new api::ObjectsRegistry) { DCHECK(!self_); self_ = this; diff --git a/browser/atom_browser_context.h b/browser/atom_browser_context.h index 86c3a7549f79..da60cfa39a14 100644 --- a/browser/atom_browser_context.h +++ b/browser/atom_browser_context.h @@ -5,10 +5,15 @@ #ifndef ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_ #define ATOM_BROWSER_ATOM_BROWSER_CONTEXT_H_ +#include "base/memory/scoped_ptr.h" #include "brightray/browser/browser_context.h" namespace atom { +namespace api { +class ObjectsRegistry; +} + class AtomBrowserContext : public brightray::BrowserContext { public: AtomBrowserContext(); @@ -17,9 +22,15 @@ class AtomBrowserContext : public brightray::BrowserContext { // We assume there is only one BrowserContext per browser process. static AtomBrowserContext* Get(); + api::ObjectsRegistry* objects_registry() const { + return objects_registry_.get(); + } + private: static AtomBrowserContext* self_; + scoped_ptr objects_registry_; + DISALLOW_COPY_AND_ASSIGN(AtomBrowserContext); }; diff --git a/browser/default_app/main.js b/browser/default_app/main.js index f0fd53cca7a6..149a68e3a16e 100644 --- a/browser/default_app/main.js +++ b/browser/default_app/main.js @@ -7,6 +7,8 @@ atom.browserMainParts.preMainMessageLoopRun = function() { mainWindow = new Window({ width: 800, height: 600 }); mainWindow.url = 'file://' + __dirname + '/index.html'; + console.log(mainWindow.id); + mainWindow.on('page-title-updated', function(event, title) { event.preventDefault();