2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-04-17 09:12:27 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/event.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
namespace gin_helper {
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
gin::WrapperInfo Event::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
|
|
Event::Event() {}
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
Event::~Event() = default;
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
void Event::SetCallback(InvokeCallback callback) {
|
2019-04-02 22:38:16 +00:00
|
|
|
DCHECK(!callback_);
|
|
|
|
callback_ = std::move(callback);
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 09:55:28 +00:00
|
|
|
void Event::PreventDefault(v8::Isolate* isolate) {
|
2019-10-31 07:56:00 +00:00
|
|
|
v8::Local<v8::Object> self = GetWrapper(isolate).ToLocalChecked();
|
|
|
|
self->Set(isolate->GetCurrentContext(),
|
|
|
|
gin::StringToV8(isolate, "defaultPrevented"), v8::True(isolate))
|
2019-05-01 00:18:22 +00:00
|
|
|
.Check();
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
bool Event::SendReply(v8::Isolate* isolate, v8::Local<v8::Value> result) {
|
2019-07-31 22:20:28 +00:00
|
|
|
if (!callback_)
|
2014-04-17 09:12:27 +00:00
|
|
|
return false;
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, result, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
std::move(callback_).Run(std::move(message));
|
2019-04-02 22:38:16 +00:00
|
|
|
return true;
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
gin::ObjectTemplateBuilder Event::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin::Wrappable<Event>::GetObjectTemplateBuilder(isolate)
|
|
|
|
.SetMethod("preventDefault", &Event::PreventDefault)
|
|
|
|
.SetMethod("sendReply", &Event::SendReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Event::GetTypeName() {
|
|
|
|
return "WebRequest";
|
2016-04-25 01:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-10-31 07:56:00 +00:00
|
|
|
gin::Handle<Event> Event::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new Event());
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 07:56:00 +00:00
|
|
|
} // namespace gin_helper
|