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>
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "content/public/browser/render_frame_host.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/native_mate_converters/string16_converter.h"
|
|
|
|
#include "shell/common/native_mate_converters/value_converter.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2018-05-21 22:18:38 +00:00
|
|
|
Event::Event(v8::Isolate* isolate) {
|
2016-04-25 01:17:54 +00:00
|
|
|
Init(isolate);
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
Event::~Event() {}
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
void Event::SetSenderAndMessage(content::RenderFrameHost* sender,
|
2019-04-02 22:38:16 +00:00
|
|
|
base::Optional<MessageSyncCallback> callback) {
|
2014-04-17 09:12:27 +00:00
|
|
|
DCHECK(!sender_);
|
2019-04-02 22:38:16 +00:00
|
|
|
DCHECK(!callback_);
|
2014-04-17 09:12:27 +00:00
|
|
|
sender_ = sender;
|
2019-04-02 22:38:16 +00:00
|
|
|
callback_ = std::move(callback);
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
Observe(content::WebContents::FromRenderFrameHost(sender));
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
void Event::RenderFrameDeleted(content::RenderFrameHost* rfh) {
|
|
|
|
if (sender_ != rfh)
|
|
|
|
return;
|
|
|
|
sender_ = nullptr;
|
2019-04-02 22:38:16 +00:00
|
|
|
callback_.reset();
|
2018-03-09 09:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Event::RenderFrameHostChanged(content::RenderFrameHost* old_rfh,
|
|
|
|
content::RenderFrameHost* new_rfh) {
|
|
|
|
if (sender_ && sender_ == old_rfh)
|
|
|
|
sender_ = new_rfh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Event::FrameDeleted(content::RenderFrameHost* rfh) {
|
|
|
|
if (sender_ != rfh)
|
|
|
|
return;
|
2016-07-10 09:52:28 +00:00
|
|
|
sender_ = nullptr;
|
2019-04-02 22:38:16 +00:00
|
|
|
callback_.reset();
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 09:55:28 +00:00
|
|
|
void Event::PreventDefault(v8::Isolate* isolate) {
|
2019-05-01 00:18:22 +00:00
|
|
|
GetWrapper()
|
|
|
|
->Set(isolate->GetCurrentContext(),
|
|
|
|
StringToV8(isolate, "defaultPrevented"), v8::True(isolate))
|
|
|
|
.Check();
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 17:25:19 +00:00
|
|
|
bool Event::SendReply(const base::Value& result) {
|
2019-04-02 22:38:16 +00:00
|
|
|
if (!callback_ || sender_ == nullptr)
|
2014-04-17 09:12:27 +00:00
|
|
|
return false;
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
std::move(*callback_).Run(result.Clone());
|
|
|
|
callback_.reset();
|
|
|
|
return true;
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Handle<Event> Event::Create(v8::Isolate* isolate) {
|
2016-04-25 01:40:19 +00:00
|
|
|
return mate::CreateHandle(isolate, new Event(isolate));
|
2016-04-25 01:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2018-04-18 01:55:30 +00:00
|
|
|
void Event::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Event"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("preventDefault", &Event::PreventDefault)
|
|
|
|
.SetMethod("sendReply", &Event::SendReply);
|
2014-04-17 09:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|