From: Cheng Zhao Date: Thu, 18 Apr 2013 15:50:47 +0000 (+0800) Subject: Emit 'page-title-updated' as javascript event for window. X-Git-Tag: v0.1.0~291 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=d3e4db7ba357d515cce0f61eec058bd212ba0506;p=electron.git Emit 'page-title-updated' as javascript event for window. --- diff --git a/browser/api/atom_api_event_emitter.cc b/browser/api/atom_api_event_emitter.cc index 096f90dd..6fe790b9 100644 --- a/browser/api/atom_api_event_emitter.cc +++ b/browser/api/atom_api_event_emitter.cc @@ -4,6 +4,14 @@ #include "browser/api/atom_api_event_emitter.h" +#include + +#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 wrapper) { EventEmitter::~EventEmitter() { } +bool EventEmitter::Emit(const std::string& name, base::ListValue* args) { + v8::HandleScope scope; + + v8::Handle context = v8::Context::GetCurrent(); + scoped_ptr converter(new V8ValueConverterImpl); + + v8::Handle v8_event = Event::CreateV8Object(); + Event* event = Event::Unwrap(v8_event); + + // Generate arguments for calling handle.emit. + std::vector> 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 diff --git a/browser/api/atom_api_event_emitter.h b/browser/api/atom_api_event_emitter.h index 33682142..4071355d 100644 --- a/browser/api/atom_api_event_emitter.h +++ b/browser/api/atom_api_event_emitter.h @@ -5,9 +5,15 @@ #ifndef ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ #define ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ +#include + #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& handle() { return handle_; } diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 51f1ec29..ca5b73fe 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -34,11 +34,19 @@ Window::Window(v8::Handle 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 args(new base::ListValue); + args->AppendString(title); + *prevent_default = Emit("page-title-updated", args.get()); +} + // static v8::Handle Window::New(const v8::Arguments &args) { v8::HandleScope scope; diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index a109bf55..91c707ee 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -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 wrapper, base::DictionaryValue* options); + // Implementations of NativeWindowObserver. + virtual void OnPageTitleUpdated(bool* prevent_default, + const std::string& title) OVERRIDE; + private: static v8::Handle New(const v8::Arguments &args); static v8::Handle Destroy(const v8::Arguments &args); diff --git a/browser/api/atom_bindings.cc b/browser/api/atom_bindings.cc index 94c08dbf..123f1207 100644 --- a/browser/api/atom_bindings.cc +++ b/browser/api/atom_bindings.cc @@ -27,14 +27,12 @@ AtomBindings::~AtomBindings() { void AtomBindings::BindTo(v8::Handle 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 global = node::g_context->Global(); v8::Handle atom = diff --git a/browser/api/lib/window.coffee b/browser/api/lib/window.coffee index e55fb73a..c372f491 100644 --- a/browser/api/lib/window.coffee +++ b/browser/api/lib/window.coffee @@ -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, -> diff --git a/browser/atom_browser_main_parts.cc b/browser/atom_browser_main_parts.cc index 7bc8e6cc..ae5405a5 100644 --- a/browser/atom_browser_main_parts.cc +++ b/browser/atom_browser_main_parts.cc @@ -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(); diff --git a/browser/default_app/main.js b/browser/default_app/main.js index 92a8b8d3..65fcb154 100644 --- a/browser/default_app/main.js +++ b/browser/default_app/main.js @@ -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; + }); } diff --git a/browser/native_window_observer.h b/browser/native_window_observer.h index 207ebded..335fd30b 100644 --- a/browser/native_window_observer.h +++ b/browser/native_window_observer.h @@ -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,