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