2013-04-18 13:42:20 +00:00
|
|
|
// 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.h"
|
|
|
|
|
2013-09-22 02:43:54 +00:00
|
|
|
#include "browser/native_window.h"
|
2013-09-22 01:52:58 +00:00
|
|
|
#include "common/api/api_messages.h"
|
2013-09-24 01:41:54 +00:00
|
|
|
#include "common/v8_conversions.h"
|
2013-09-20 14:55:42 +00:00
|
|
|
|
2013-04-18 13:42:20 +00:00
|
|
|
using node::node_isolate;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
v8::Persistent<v8::FunctionTemplate> Event::constructor_template_;
|
|
|
|
|
|
|
|
Event::Event()
|
2013-09-22 01:52:58 +00:00
|
|
|
: sender_(NULL),
|
|
|
|
message_(NULL),
|
|
|
|
prevent_default_(false) {
|
2013-04-18 13:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Event::~Event() {
|
2013-09-22 02:43:54 +00:00
|
|
|
if (sender_)
|
|
|
|
sender_->RemoveObserver(this);
|
2013-04-18 13:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Object> Event::CreateV8Object() {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
if (constructor_template_.IsEmpty()) {
|
|
|
|
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
|
|
|
constructor_template_ = v8::Persistent<v8::FunctionTemplate>::New(
|
|
|
|
node_isolate, t);
|
|
|
|
constructor_template_->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
constructor_template_->SetClassName(v8::String::NewSymbol("Event"));
|
|
|
|
|
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "preventDefault", PreventDefault);
|
2013-09-22 01:52:58 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "sendReply", SendReply);
|
2013-04-18 13:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> v8_event =
|
|
|
|
constructor_template_->GetFunction()->NewInstance(0, NULL);
|
|
|
|
|
|
|
|
return scope.Close(v8_event);
|
|
|
|
}
|
|
|
|
|
2013-09-22 02:43:54 +00:00
|
|
|
void Event::SetSenderAndMessage(NativeWindow* sender, IPC::Message* message) {
|
2013-09-22 01:52:58 +00:00
|
|
|
DCHECK(!sender_);
|
|
|
|
DCHECK(!message_);
|
|
|
|
sender_ = sender;
|
|
|
|
message_ = message;
|
2013-09-22 02:43:54 +00:00
|
|
|
|
|
|
|
sender_->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Event::OnWindowClosed() {
|
|
|
|
sender_ = NULL;
|
|
|
|
message_ = NULL;
|
2013-09-22 01:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> Event::New(const v8::Arguments& args) {
|
2013-04-18 13:42:20 +00:00
|
|
|
Event* event = new Event;
|
|
|
|
event->Wrap(args.This());
|
|
|
|
|
|
|
|
return args.This();
|
|
|
|
}
|
|
|
|
|
2013-09-22 01:52:58 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> Event::PreventDefault(const v8::Arguments& args) {
|
2013-04-18 13:42:20 +00:00
|
|
|
Event* event = Unwrap<Event>(args.This());
|
2013-04-29 11:38:18 +00:00
|
|
|
if (event == NULL)
|
|
|
|
return node::ThrowError("Event is already destroyed");
|
|
|
|
|
2013-04-18 13:42:20 +00:00
|
|
|
event->prevent_default_ = true;
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-09-22 01:52:58 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> Event::SendReply(const v8::Arguments& args) {
|
|
|
|
Event* event = Unwrap<Event>(args.This());
|
|
|
|
if (event == NULL)
|
|
|
|
return node::ThrowError("Event is already destroyed");
|
|
|
|
|
2013-09-22 02:43:54 +00:00
|
|
|
if (event->message_ == NULL)
|
2013-09-22 02:03:47 +00:00
|
|
|
return node::ThrowError("Can only send reply to synchronous events once");
|
2013-09-22 01:52:58 +00:00
|
|
|
|
2013-09-23 15:00:58 +00:00
|
|
|
string16 json = FromV8Value(args[0]);
|
2013-09-22 01:52:58 +00:00
|
|
|
|
|
|
|
AtomViewHostMsg_Message_Sync::WriteReplyParams(event->message_, json);
|
|
|
|
event->sender_->Send(event->message_);
|
|
|
|
|
2013-09-22 02:03:47 +00:00
|
|
|
event->message_ = NULL;
|
2013-09-22 01:52:58 +00:00
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:42:20 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|