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.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
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"
|
2023-02-13 21:39:18 +00:00
|
|
|
#include "gin/handle.h"
|
|
|
|
#include "shell/common/gin_helper/event.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
|
|
|
|
2023-11-13 03:51:56 +00:00
|
|
|
// Provide helpers 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
|
|
|
|
2022-06-16 07:46:11 +00:00
|
|
|
// Make the convenient methods visible:
|
2016-04-25 01:17:54 +00:00
|
|
|
// 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
|
|
|
|
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>
|
2024-01-11 01:00:37 +00:00
|
|
|
bool Emit(const std::string_view name, Args&&... args) {
|
2015-08-27 07:22:02 +00:00
|
|
|
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;
|
2023-02-13 21:39:18 +00:00
|
|
|
gin::Handle<gin_helper::internal::Event> event =
|
|
|
|
internal::Event::New(isolate());
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
2019-10-25 13:03:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
EventEmitter(const EventEmitter&) = delete;
|
|
|
|
EventEmitter& operator=(const EventEmitter&) = delete;
|
|
|
|
|
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>
|
2024-01-11 01:00:37 +00:00
|
|
|
bool EmitWithEvent(const std::string_view name,
|
2023-02-13 21:39:18 +00:00
|
|
|
gin::Handle<gin_helper::internal::Event> 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();
|
2019-10-24 05:47:58 +00:00
|
|
|
gin_helper::EmitEvent(isolate, GetWrapper(), name, event,
|
|
|
|
std::forward<Args>(args)...);
|
2023-02-13 21:39:18 +00:00
|
|
|
return event->GetDefaultPrevented();
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
2014-04-17 09:12:27 +00:00
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin_helper
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|