2013-04-15 16:25:08 +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_emitter.h"
|
|
|
|
|
2013-04-18 15:50:47 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "browser/api/atom_api_event.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
#include "common/v8/native_type_conversions.h"
|
2013-04-18 15:50:47 +00:00
|
|
|
|
2013-12-26 10:41:21 +00:00
|
|
|
#include "common/v8/node_common.h"
|
|
|
|
|
2013-04-15 16:25:08 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2013-04-25 07:02:58 +00:00
|
|
|
EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
|
|
|
|
Wrap(wrapper);
|
2013-04-15 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventEmitter::~EventEmitter() {
|
2013-12-27 06:47:50 +00:00
|
|
|
// Clear the aligned pointer, it should have been done by ObjectWrap but
|
|
|
|
// somehow node v0.11.x changed this behaviour.
|
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
|
|
|
handle()->SetAlignedPointerInInternalField(0, NULL);
|
2013-04-15 16:25:08 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 10:19:07 +00:00
|
|
|
bool EventEmitter::Emit(const std::string& name) {
|
|
|
|
base::ListValue args;
|
|
|
|
return Emit(name, &args);
|
|
|
|
}
|
|
|
|
|
2013-04-18 15:50:47 +00:00
|
|
|
bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::HandleScope handle_scope(node_isolate);
|
2013-04-18 15:50:47 +00:00
|
|
|
|
|
|
|
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
2013-12-23 14:42:21 +00:00
|
|
|
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
2013-04-18 15:50:47 +00:00
|
|
|
|
|
|
|
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);
|
2013-12-05 15:47:07 +00:00
|
|
|
v8_args.push_back(ToV8Value(name));
|
2013-04-18 15:50:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:25:08 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|