electron/atom/browser/api/event_emitter.cc

64 lines
1.9 KiB
C++
Raw Normal View History

// Copyright (c) 2014 GitHub, Inc. All rights reserved.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/event_emitter.h"
#include <vector>
#include "atom/browser/api/event.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "atom/common/node_includes.h"
namespace mate {
EventEmitter::EventEmitter() {
}
bool EventEmitter::Emit(const base::StringPiece& name) {
return Emit(name, base::ListValue());
}
bool EventEmitter::Emit(const base::StringPiece& name,
const base::ListValue& args) {
return Emit(name, args, NULL, NULL);
}
bool EventEmitter::Emit(const base::StringPiece& name,
const base::ListValue& args,
content::WebContents* sender,
IPC::Message* message) {
2014-06-28 11:49:55 +00:00
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
2014-06-28 11:49:55 +00:00
v8::Handle<v8::Context> context = isolate->GetCurrentContext();
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
2014-06-28 11:49:55 +00:00
mate::Handle<mate::Event> event = mate::Event::Create(isolate);
if (sender && message)
event->SetSenderAndMessage(sender, message);
// v8_args = [name, event, args...];
std::vector<v8::Handle<v8::Value>> v8_args;
v8_args.reserve(args.GetSize() + 2);
2014-06-28 11:49:55 +00:00
v8_args.push_back(mate::StringToV8(isolate, name));
v8_args.push_back(event.ToV8());
for (size_t i = 0; i < args.GetSize(); i++) {
const base::Value* value(NULL);
if (args.Get(i, &value))
v8_args.push_back(converter->ToV8Value(value, context));
}
// this.emit.apply(this, v8_args);
2014-06-28 11:49:55 +00:00
node::MakeCallback(isolate, GetWrapper(isolate), "emit", v8_args.size(),
&v8_args[0]);
return event->prevent_default();
}
} // namespace mate