2019-10-21 07:05:40 +00:00
|
|
|
// Copyright (c) 2019 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-10-21 07:05:40 +00:00
|
|
|
#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
|
|
|
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
#include <utility>
|
2014-10-25 03:30:35 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "electron/shell/common/api/api.mojom.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_helper/event_emitter_caller.h"
|
2019-12-05 09:46:34 +00:00
|
|
|
#include "shell/common/gin_helper/wrappable.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-25 13:03:28 +00:00
|
|
|
namespace content {
|
|
|
|
class RenderFrameHost;
|
|
|
|
}
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
namespace gin_helper {
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2021-10-06 02:21:00 +00:00
|
|
|
v8::Local<v8::Object> CreateCustomEvent(
|
2019-10-24 05:47:58 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> sender = v8::Local<v8::Object>(),
|
|
|
|
v8::Local<v8::Object> custom_event = v8::Local<v8::Object>());
|
2019-10-25 13:03:28 +00:00
|
|
|
v8::Local<v8::Object> CreateNativeEvent(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> sender,
|
|
|
|
content::RenderFrameHost* frame,
|
2019-10-31 07:56:00 +00:00
|
|
|
electron::mojom::ElectronBrowser::MessageSyncCallback callback);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
// Provide helperers to emit event in JavaScript.
|
2019-10-25 13:03:28 +00:00
|
|
|
template <typename T>
|
2019-12-05 09:46:34 +00:00
|
|
|
class EventEmitter : public gin_helper::Wrappable<T> {
|
2014-10-25 03:30:35 +00:00
|
|
|
public:
|
2019-12-05 09:46:34 +00:00
|
|
|
using Base = gin_helper::Wrappable<T>;
|
2019-10-25 13:03:28 +00:00
|
|
|
using ValueArray = std::vector<v8::Local<v8::Value>>;
|
2014-10-25 03:30:35 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// Make the convinient methods visible:
|
|
|
|
// https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
|
2019-10-21 07:05:40 +00:00
|
|
|
v8::Isolate* isolate() const { return Base::isolate(); }
|
|
|
|
v8::Local<v8::Object> GetWrapper() const { return Base::GetWrapper(); }
|
2019-04-30 13:45:05 +00:00
|
|
|
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const {
|
2019-10-21 07:05:40 +00:00
|
|
|
return Base::GetWrapper(isolate);
|
2019-04-30 13:45:05 +00:00
|
|
|
}
|
2016-04-25 01:17:54 +00:00
|
|
|
|
2015-07-29 06:24:45 +00:00
|
|
|
// this.emit(name, event, args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitCustomEvent(base::StringPiece name,
|
2015-07-29 06:24:45 +00:00
|
|
|
v8::Local<v8::Object> event,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2021-10-06 02:21:00 +00:00
|
|
|
return EmitWithEvent(
|
|
|
|
name, internal::CreateCustomEvent(isolate(), GetWrapper(), event),
|
|
|
|
std::forward<Args>(args)...);
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
// this.emit(name, new Event(), args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool Emit(base::StringPiece name, Args&&... args) {
|
2015-08-27 07:22:02 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2017-10-24 05:38:55 +00:00
|
|
|
v8::Local<v8::Object> wrapper = GetWrapper();
|
2019-10-25 13:03:28 +00:00
|
|
|
if (wrapper.IsEmpty())
|
2017-10-24 05:38:55 +00:00
|
|
|
return false;
|
2021-10-06 02:21:00 +00:00
|
|
|
v8::Local<v8::Object> event =
|
|
|
|
internal::CreateCustomEvent(isolate(), wrapper);
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
2019-10-25 13:03:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 06:28:13 +00:00
|
|
|
protected:
|
2016-04-25 01:17:54 +00:00
|
|
|
EventEmitter() {}
|
2015-06-25 06:28:13 +00:00
|
|
|
|
2015-01-15 01:51:54 +00:00
|
|
|
private:
|
2015-07-29 06:24:45 +00:00
|
|
|
// this.emit(name, event, args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitWithEvent(base::StringPiece name,
|
2015-07-29 06:24:45 +00:00
|
|
|
v8::Local<v8::Object> event,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2019-09-30 22:00:47 +00:00
|
|
|
// It's possible that |this| will be deleted by EmitEvent, so save anything
|
|
|
|
// we need from |this| before calling EmitEvent.
|
|
|
|
auto* isolate = this->isolate();
|
|
|
|
auto context = isolate->GetCurrentContext();
|
2019-10-24 05:47:58 +00:00
|
|
|
gin_helper::EmitEvent(isolate, GetWrapper(), name, event,
|
|
|
|
std::forward<Args>(args)...);
|
2019-05-01 00:18:22 +00:00
|
|
|
v8::Local<v8::Value> defaultPrevented;
|
2019-10-21 07:05:40 +00:00
|
|
|
if (event->Get(context, gin::StringToV8(isolate, "defaultPrevented"))
|
2019-05-01 00:18:22 +00:00
|
|
|
.ToLocal(&defaultPrevented)) {
|
2019-09-30 22:00:47 +00:00
|
|
|
return defaultPrevented->BooleanValue(isolate);
|
2019-05-01 00:18:22 +00:00
|
|
|
}
|
|
|
|
return false;
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin_helper
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|