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:56:58 +00:00
|
|
|
#ifndef SHELL_BROWSER_API_EVENT_EMITTER_H_
|
|
|
|
#define SHELL_BROWSER_API_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-04-02 22:38:16 +00:00
|
|
|
#include "base/optional.h"
|
2018-11-30 09:48:27 +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"
|
2014-04-17 09:12:27 +00:00
|
|
|
#include "native_mate/wrappable.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/api/event_emitter_caller.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
namespace content {
|
2018-03-09 09:31:09 +00:00
|
|
|
class RenderFrameHost;
|
2014-04-25 08:13:16 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
v8::Local<v8::Object> CreateJSEvent(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
content::RenderFrameHost* sender,
|
2019-06-19 21:23:04 +00:00
|
|
|
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
|
|
|
|
callback);
|
2019-07-29 20:54:37 +00:00
|
|
|
v8::Local<v8::Object> CreateEmptyJSEvent(v8::Isolate* isolate);
|
2018-04-18 01:44:10 +00:00
|
|
|
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::Object> event);
|
2016-06-22 02:00:45 +00:00
|
|
|
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
|
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.
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename T>
|
2016-04-25 01:17:54 +00:00
|
|
|
class EventEmitter : public Wrappable<T> {
|
2014-10-25 03:30:35 +00:00
|
|
|
public:
|
2015-05-22 11:11:22 +00:00
|
|
|
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
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
|
|
|
|
v8::Isolate* isolate() const { return Wrappable<T>::isolate(); }
|
2018-02-20 16:11:35 +00:00
|
|
|
v8::Local<v8::Object> GetWrapper() const {
|
|
|
|
return Wrappable<T>::GetWrapper();
|
|
|
|
}
|
2019-04-30 13:45:05 +00:00
|
|
|
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const {
|
|
|
|
return Wrappable<T>::GetWrapper(isolate);
|
|
|
|
}
|
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) {
|
2016-04-25 01:17:54 +00:00
|
|
|
return EmitWithEvent(
|
2018-04-18 01:44:10 +00:00
|
|
|
name, internal::CreateCustomEvent(isolate(), GetWrapper(), event),
|
2019-04-27 04:42:56 +00:00
|
|
|
std::forward<Args>(args)...);
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 02:00:45 +00:00
|
|
|
// this.emit(name, new Event(flags), args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitWithFlags(base::StringPiece name, int flags, Args&&... args) {
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitCustomEvent(name,
|
|
|
|
internal::CreateEventFromFlags(isolate(), flags),
|
|
|
|
std::forward<Args>(args)...);
|
2016-06-22 02:00: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) {
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitWithSender(name, nullptr, base::nullopt,
|
|
|
|
std::forward<Args>(args)...);
|
2015-01-15 01:51:54 +00:00
|
|
|
}
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
// this.emit(name, new Event(sender, message), args...);
|
2018-03-09 09:31:09 +00:00
|
|
|
template <typename... Args>
|
2019-04-02 22:38:16 +00:00
|
|
|
bool EmitWithSender(
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece name,
|
2019-04-02 22:38:16 +00:00
|
|
|
content::RenderFrameHost* sender,
|
2019-06-19 21:23:04 +00:00
|
|
|
base::Optional<electron::mojom::ElectronBrowser::MessageSyncCallback>
|
2019-04-02 22:38:16 +00:00
|
|
|
callback,
|
2019-04-27 04:42:56 +00:00
|
|
|
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();
|
2017-10-24 05:58:43 +00:00
|
|
|
if (wrapper.IsEmpty()) {
|
2017-10-24 05:38:55 +00:00
|
|
|
return false;
|
2017-10-24 05:58:43 +00:00
|
|
|
}
|
2019-04-02 22:38:16 +00:00
|
|
|
v8::Local<v8::Object> event = internal::CreateJSEvent(
|
|
|
|
isolate(), wrapper, sender, std::move(callback));
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
2015-01-15 01:51:54 +00:00
|
|
|
}
|
2014-04-25 08:13:16 +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) {
|
2018-11-30 09:48:27 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2015-07-29 06:24:45 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2019-04-27 04:42:56 +00:00
|
|
|
EmitEvent(isolate(), GetWrapper(), name, event,
|
|
|
|
std::forward<Args>(args)...);
|
2019-05-01 00:18:22 +00:00
|
|
|
auto context = isolate()->GetCurrentContext();
|
|
|
|
v8::Local<v8::Value> defaultPrevented;
|
|
|
|
if (event->Get(context, StringToV8(isolate(), "defaultPrevented"))
|
|
|
|
.ToLocal(&defaultPrevented)) {
|
|
|
|
return defaultPrevented->BooleanValue(isolate());
|
|
|
|
}
|
|
|
|
return false;
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_BROWSER_API_EVENT_EMITTER_H_
|