]> khamidou — GitHub mirrors - electron.git/commitdiff
Emit 'page-title-updated' as javascript event for window.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 15:50:47 +0000 (23:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 18 Apr 2013 15:50:47 +0000 (23:50 +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/atom_bindings.cc
browser/api/lib/window.coffee
browser/atom_browser_main_parts.cc
browser/default_app/main.js
browser/native_window_observer.h

index 096f90dd32f02a413a04e83e6cca0c5abe268a4c..6fe790b9ecd6eea0503dba6b0e4b76c9840d3502 100644 (file)
@@ -4,6 +4,14 @@
 
 #include "browser/api/atom_api_event_emitter.h"
 
+#include <vector>
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/values.h"
+#include "browser/api/atom_api_event.h"
+#include "common/v8_value_converter_impl.h"
+
 namespace atom {
 
 namespace api {
@@ -15,6 +23,40 @@ EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
 EventEmitter::~EventEmitter() {
 }
 
+bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
+  v8::HandleScope scope;
+
+  v8::Handle<v8::Context> context = v8::Context::GetCurrent();
+  scoped_ptr<content::V8ValueConverter> converter(new V8ValueConverterImpl);
+
+  v8::Handle<v8::Object> v8_event = Event::CreateV8Object();
+  Event* event = Event::Unwrap<Event>(v8_event);
+
+  // Generate arguments for calling handle.emit.
+  std::vector<v8::Handle<v8::Value>> v8_args;
+  v8_args.reserve(args->GetSize() + 2);
+  v8_args.push_back(v8::String::New(name.c_str(), name.size()));
+  v8_args.push_back(v8_event);
+  for (size_t i = 0; i < args->GetSize(); i++) {
+    base::Value* value = NULL;
+    if (args->Get(i, &value)) {
+      DCHECK(value);
+      v8_args.push_back(converter->ToV8Value(value, context));
+    } else {
+      NOTREACHED() << "Wrong offset " << i << " for " << *args;
+    }
+  }
+
+  node::MakeCallback(handle(), "emit", v8_args.size(), &v8_args[0]);
+
+  bool prevent_default = event->prevent_default();
+
+  // Don't wait for V8 GC, delete it immediately.
+  delete event;
+
+  return prevent_default;
+}
+
 }  // namespace api
 
 }  // namespace atom
index 336821423e1db285307644c808bbd28f9bd1c136..4071355d1b56ac86c37f2d365e5a771ae0e0270d 100644 (file)
@@ -5,9 +5,15 @@
 #ifndef ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
 #define ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_
 
+#include <iosfwd>
+
 #include "base/basictypes.h"
 #include "vendor/node/src/node_object_wrap.h"
 
+namespace base {
+class ListValue;
+}
+
 namespace atom {
 
 namespace api {
@@ -19,6 +25,9 @@ class EventEmitter : public node::ObjectWrap {
  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<v8::Object>& handle() { return handle_; }
 
index 51f1ec290695846745ceb7c765f5c6f13e3c78fd..ca5b73fecc2c2065c3111126e682a463cc586cf8 100644 (file)
@@ -34,11 +34,19 @@ Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
     : EventEmitter(wrapper),
       window_(NativeWindow::Create(AtomBrowserContext::Get(), options)) {
   window_->InitFromOptions(options);
+  window_->AddObserver(this);
 }
 
 Window::~Window() {
 }
 
+void Window::OnPageTitleUpdated(bool* prevent_default,
+                                const std::string& title) {
+  scoped_ptr<base::ListValue> args(new base::ListValue);
+  args->AppendString(title);
+  *prevent_default = Emit("page-title-updated", args.get());
+}
+
 // static
 v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
   v8::HandleScope scope;
index a109bf55e6f73d0554a02da53e0c3712630fd223..91c707eee3b58727214d05819edd983dc9b5cac6 100644 (file)
@@ -5,7 +5,9 @@
 #ifndef ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 #define ATOM_BROWSER_API_ATOM_API_WINDOW_H_
 
+#include "base/memory/scoped_ptr.h"
 #include "browser/api/atom_api_event_emitter.h"
+#include "browser/native_window_observer.h"
 
 namespace base {
 class DictionaryValue;
@@ -17,7 +19,8 @@ class NativeWindow;
 
 namespace api {
 
-class Window : public EventEmitter {
+class Window : public EventEmitter,
+               public NativeWindowObserver {
  public:
   virtual ~Window();
 
@@ -29,6 +32,10 @@ class Window : public EventEmitter {
   explicit Window(v8::Handle<v8::Object> wrapper,
                   base::DictionaryValue* options);
 
+  // Implementations of NativeWindowObserver.
+  virtual void OnPageTitleUpdated(bool* prevent_default,
+                                  const std::string& title) OVERRIDE;
+
  private:
   static v8::Handle<v8::Value> New(const v8::Arguments &args);
   static v8::Handle<v8::Value> Destroy(const v8::Arguments &args);
index 94c08dbf47095a9bb3c89f2927c90bd727578101..123f1207c6b611b7afda686267a9368da85bc067 100644 (file)
@@ -27,14 +27,12 @@ AtomBindings::~AtomBindings() {
 
 void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
   v8::HandleScope scope;
-  v8::Context::Scope context_scope(node::g_context);
 
   node::SetMethod(process, "atom_binding", Binding);
 }
 
 void AtomBindings::AfterLoad() {
   v8::HandleScope scope;
-  v8::Context::Scope context_scope(node::g_context);
 
   v8::Handle<v8::Object> global = node::g_context->Global();
   v8::Handle<v8::Object> atom =
index e55fb73a490c78935256b864dea0179cd8d15c0e..c372f491b6ac973731ff2d24fcd8783ab520382f 100644 (file)
@@ -1,5 +1,11 @@
+EventEmitter = require('events').EventEmitter
+
 Window = process.atom_binding('window').Window
 
+# Inherits EventEmitter.
+for prop, func of EventEmitter.prototype
+  Window.prototype[prop] = func
+
 # Convient accessors.
 setupGetterAndSetter = (constructor, name, getter, setter) ->
   constructor.prototype.__defineGetter__ name, ->
index 7bc8e6cc59bc0cc36707629052feead77f1615bc..ae5405a5547c3909934a338394cd0ca88fd0ecb2 100644 (file)
@@ -30,6 +30,9 @@ void AtomBrowserMainParts::PostEarlyInitialization() {
 
   node_bindings_->Initialize();
 
+  // Wrap whole process in one global context.
+  node::g_context->Enter();
+
   atom_bindings_->BindTo(node::process);
 
   node_bindings_->Load();
index 92a8b8d38480e63928db225c2a3f7b1398cb9ec2..65fcb1549f397f915de3837dd2f8940decd215af 100644 (file)
@@ -6,4 +6,9 @@ var mainWindow = null;
 atom.browserMainParts.preMainMessageLoopRun = function() {
   mainWindow = new Window({ width: 800, height: 600 });
   mainWindow.loadURL('file://' + __dirname + '/index.html');
+  mainWindow.on('page-title-updated', function(event, title) {
+    event.preventDefault();
+
+    this.title = 'Atom Shell - ' + title;
+  });
 }
index 207ebdede43113877025631fe6491d3545e21c2f..335fd30be5d9619756f303c175e95e1c40dfb994 100644 (file)
@@ -11,7 +11,7 @@ namespace atom {
 
 class NativeWindowObserver {
  public:
-  virtual ~NativeWindowObserver() {};
+  virtual ~NativeWindowObserver() {}
 
   // Called when the web page of the window has updated it's document title.
   virtual void OnPageTitleUpdated(bool* prevent_default,