2014-10-31 11:17:05 -07:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-04-25 17:49:37 +08:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-04-17 17:12:27 +08:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/api/event.h"
|
2014-04-17 17:12:27 +08:00
|
|
|
|
2019-04-02 15:38:16 -07:00
|
|
|
#include <utility>
|
|
|
|
|
2019-09-19 11:09:15 -04:00
|
|
|
#include "native_mate/object_template_builder_deprecated.h"
|
2019-10-09 10:59:08 -07:00
|
|
|
#include "shell/common/native_mate_converters/blink_converter.h"
|
2014-04-17 17:12:27 +08:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2018-05-22 00:18:38 +02:00
|
|
|
Event::Event(v8::Isolate* isolate) {
|
2016-04-25 10:17:54 +09:00
|
|
|
Init(isolate);
|
2014-04-17 17:12:27 +08:00
|
|
|
}
|
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
Event::~Event() = default;
|
2014-04-17 17:12:27 +08:00
|
|
|
|
2019-10-09 10:59:08 -07:00
|
|
|
void Event::SetCallback(base::Optional<InvokeCallback> callback) {
|
2019-04-02 15:38:16 -07:00
|
|
|
DCHECK(!callback_);
|
|
|
|
callback_ = std::move(callback);
|
2014-04-17 17:12:27 +08:00
|
|
|
}
|
|
|
|
|
2014-10-27 17:55:28 +08:00
|
|
|
void Event::PreventDefault(v8::Isolate* isolate) {
|
2019-04-30 20:18:22 -04:00
|
|
|
GetWrapper()
|
|
|
|
->Set(isolate->GetCurrentContext(),
|
|
|
|
StringToV8(isolate, "defaultPrevented"), v8::True(isolate))
|
|
|
|
.Check();
|
2014-04-17 17:12:27 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 10:59:08 -07:00
|
|
|
bool Event::SendReply(v8::Isolate* isolate, v8::Local<v8::Value> result) {
|
2019-08-01 00:20:28 +02:00
|
|
|
if (!callback_)
|
2014-04-17 17:12:27 +08:00
|
|
|
return false;
|
|
|
|
|
2019-10-09 10:59:08 -07:00
|
|
|
blink::CloneableMessage message;
|
|
|
|
if (!ConvertFromV8(isolate, result, &message)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(*callback_).Run(std::move(message));
|
2019-04-02 15:38:16 -07:00
|
|
|
callback_.reset();
|
|
|
|
return true;
|
2014-04-17 17:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Handle<Event> Event::Create(v8::Isolate* isolate) {
|
2016-04-25 10:40:19 +09:00
|
|
|
return mate::CreateHandle(isolate, new Event(isolate));
|
2016-04-25 10:17:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2018-04-17 21:55:30 -04:00
|
|
|
void Event::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 19:28:12 +09:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Event"));
|
2016-08-02 18:08:12 +09:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2016-04-25 10:17:54 +09:00
|
|
|
.SetMethod("preventDefault", &Event::PreventDefault)
|
|
|
|
.SetMethod("sendReply", &Event::SendReply);
|
2014-04-17 17:12:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|