2023-03-03 17:08:06 +08:00
|
|
|
//----------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright © yanghy. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under Apache License Version 2.0, January 2004
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
//----------------------------------------
|
|
|
|
|
|
|
|
package ipc
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
import (
|
|
|
|
"github.com/energye/energy/common"
|
|
|
|
"sync"
|
|
|
|
)
|
2023-03-03 17:08:06 +08:00
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
var (
|
|
|
|
browser *browserIPC
|
|
|
|
)
|
2023-03-03 17:08:06 +08:00
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
// browserIPC 主进程 IPC
|
|
|
|
type browserIPC struct {
|
|
|
|
onEvent map[string]EmitCallback
|
|
|
|
lock sync.Mutex
|
2023-03-03 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
type EmitCallback func(context IContext)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if common.Args.IsMain() {
|
|
|
|
browser = &browserIPC{onEvent: make(map[string]EmitCallback)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type IResult interface {
|
|
|
|
}
|
|
|
|
|
|
|
|
// IContext 进程间IPC通信回调上下文
|
|
|
|
type IContext interface {
|
|
|
|
ArgumentList() IArrayValue //参数列表
|
|
|
|
BrowserId() int32 //事件所属 browser id
|
|
|
|
FrameId() int64 //事件所属 frame id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context IPC 事件上下文
|
|
|
|
type Context struct {
|
|
|
|
browserId int32
|
|
|
|
frameId int64
|
|
|
|
argument IArrayValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(browserId int32, frameId int64, argument IArrayValue) IContext {
|
|
|
|
return &Context{
|
|
|
|
browserId: browserId,
|
|
|
|
frameId: frameId,
|
|
|
|
argument: argument,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//On
|
|
|
|
// IPC GO 监听事件
|
|
|
|
func On(name string, fn EmitCallback) {
|
|
|
|
if browser != nil {
|
|
|
|
browser.addOnEvent(name, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//RemoveOn
|
|
|
|
// IPC GO 移除监听事件
|
|
|
|
func RemoveOn(name string) {
|
|
|
|
if browser == nil || name == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
browser.lock.Lock()
|
|
|
|
defer browser.lock.Unlock()
|
|
|
|
delete(browser.onEvent, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Emit
|
|
|
|
// IPC GO 中触发 JS 监听的事件
|
2023-03-03 17:08:06 +08:00
|
|
|
func Emit(name string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
//CheckOnEvent
|
|
|
|
// IPC 检查 GO 中监听的事件是存在, 并返回回调函数
|
|
|
|
func CheckOnEvent(name string) EmitCallback {
|
|
|
|
if browser == nil || name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
browser.lock.Lock()
|
|
|
|
defer browser.lock.Unlock()
|
|
|
|
if callback, ok := browser.onEvent[name]; ok {
|
|
|
|
return callback
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-03 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
// addOnEvent 添加监听事件
|
|
|
|
func (m *browserIPC) addOnEvent(name string, fn EmitCallback) {
|
|
|
|
if m == nil || name == "" || fn == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
m.onEvent[name] = fn
|
2023-03-03 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
// emitOnEvent 触发监听事件
|
|
|
|
func (m *browserIPC) emitOnEvent(name string, argumentList IArrayValue) {
|
|
|
|
if m == nil || name == "" || argumentList == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.lock.Lock()
|
|
|
|
defer m.lock.Unlock()
|
|
|
|
|
2023-03-03 17:08:06 +08:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:53:18 +08:00
|
|
|
// ArgumentList 参数列表
|
|
|
|
func (m *Context) ArgumentList() IArrayValue {
|
|
|
|
return m.argument
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Context) BrowserId() int32 {
|
|
|
|
return m.browserId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Context) FrameId() int64 {
|
|
|
|
return m.frameId
|
2023-03-03 17:08:06 +08:00
|
|
|
}
|