提交 f3788450 编写于 作者: yanghye's avatar yanghye

upgrade-dev v2.3.26

上级 0d736b0b
......@@ -30,8 +30,6 @@ func appMainRunCallback() {
func renderProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, sourceProcess consts.CefProcessId, message *ICefProcessMessage) (result bool) {
if message.Name() == internalIPCJSExecuteGoEventReplay {
result = ipcRender.ipcJSExecuteGoEventMessageReply(browser, frame, sourceProcess, message)
} else if message.Name() == internalIPCJSExecuteGoSyncEventReplay {
} else if message.Name() == internalIPCGoExecuteJSEvent {
result = ipcRender.ipcGoExecuteJSEvent(browser, frame, sourceProcess, message)
}
......@@ -42,8 +40,6 @@ func renderProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, source
func browserProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) {
if message.Name() == internalIPCJSExecuteGoEvent {
result = ipcBrowser.jsExecuteGoMethodMessage(browser, frame, message)
} else if message.Name() == internalIPCJSExecuteGoSyncEvent {
} else if message.Name() == internalIPCGoExecuteJSEventReplay {
result = ipcBrowser.ipcGoExecuteMethodMessageReply(browser, frame, message)
}
......
......@@ -12,8 +12,8 @@
package cef
import (
"fmt"
"github.com/energye/energy/pkgs/channel"
"github.com/energye/energy/pkgs/json"
)
// browserIPCChan
......@@ -26,8 +26,45 @@ func (m *ipcBrowserProcess) ipcChannelBrowser() {
m.ipcChannel = new(browserIPCChan)
m.ipcChannel.ipc = channel.NewBrowser()
m.ipcChannel.ipc.Handler(func(context channel.IIPCContext) {
fmt.Println("data:", context.Message().JSON().ToJSONString())
messageJSON := context.Message().JSON().JSONObject()
messageId := messageJSON.GetIntByKey(ipc_id)
emitName := messageJSON.GetStringByKey(ipc_event)
name := messageJSON.GetStringByKey(ipc_name)
browserId := messageJSON.GetIntByKey(ipc_browser_id)
argumentList := messageJSON.GetArrayByKey(ipc_argumentList)
if name == internalIPCJSExecuteGoSyncEvent {
m.jsExecuteGoSyncMethodMessage(int32(messageId), int32(browserId), context.ChannelId(), emitName, argumentList)
}
context.Free()
})
}
}
func (m *ipcBrowserProcess) jsExecuteGoSyncMethodMessage(messageId, browserId int32, frameId int64, emitName string, argumentList json.JSONArray) {
var ipcContext = m.jsExecuteGoMethod(browserId, frameId, emitName, argumentList)
if messageId != 0 { // 同步回调函数处理
message := json.NewJSONObject(nil)
message.Set(ipc_id, messageId)
message.Set(ipc_name, internalIPCJSExecuteGoSyncEventReplay)
message.Set(ipc_argumentList, nil)
if ipcContext != nil {
//处理回复消息
replay := ipcContext.Replay()
if replay.Result() != nil && len(replay.Result()) > 0 {
switch replay.Result()[0].(type) {
case []byte:
message.Set(ipc_argumentList, json.NewJSONArray((replay.Result()[0]).([]byte)).Data())
}
}
}
m.ipcChannel.ipc.Send(frameId, message.Bytes())
message.Free()
}
if ipcContext != nil {
if ipcContext.ArgumentList() != nil {
ipcContext.ArgumentList().Free()
}
ipcContext.Result(nil)
}
}
......@@ -45,13 +45,11 @@ func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame
}
var messageId int32
var emitName string
var isSync bool
var argument json.JSON
var argumentList json.JSONArray
if messageDataBytes != nil {
argument = json.NewJSON(messageDataBytes)
messageId = int32(argument.GetIntByKey(ipc_id))
isSync = argument.GetBoolByKey(ipc_type)
emitName = argument.GetStringByKey(ipc_event)
argumentList = argument.GetArrayByKey(ipc_argumentList)
messageDataBytes = nil
......@@ -66,17 +64,7 @@ func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame
}()
argumentListBytes = nil
var ipcContext = m.jsExecuteGoMethod(browser.Identifier(), frame.Identifier(), emitName, argumentList)
//eventCallback := ipc.CheckOnEvent(emitName)
//if eventCallback != nil {
// ipcContext = ipc.NewContext(browser.Identifier(), frame.Identifier(), true, argumentList)
// //调用监听函数
// if ctxCallback := eventCallback.ContextCallback(); ctxCallback != nil {
// ctxCallback.Invoke(ipcContext)
// } else if argsCallback := eventCallback.ArgumentCallback(); argsCallback != nil {
// argsCallback.Invoke(ipcContext)
// }
//}
if messageId != 0 || isSync { // 异步回调函数处理
if messageId != 0 { // 异步回调函数处理
replyMessage := json.NewJSONArray(nil)
replyMessage.Add(messageId)
replyMessage.Add(false)
......
......@@ -11,7 +11,11 @@
// GO render IPC通道
package cef
import "github.com/energye/energy/pkgs/channel"
import (
"fmt"
"github.com/energye/energy/pkgs/channel"
"github.com/energye/energy/pkgs/json"
)
// renderIPCChan
type renderIPCChan struct {
......@@ -27,7 +31,28 @@ func (m *ipcRenderProcess) ipcChannelRender(browser *ICefBrowser, frame *ICefFra
m.ipcChannel.frameId = frame.Identifier()
m.ipcChannel.ipc = channel.NewRender(m.ipcChannel.frameId)
m.ipcChannel.ipc.Handler(func(context channel.IIPCContext) {
messageJSON := context.Message().JSON().JSONObject()
messageId := messageJSON.GetIntByKey(ipc_id)
name := messageJSON.GetStringByKey(ipc_name)
argumentList := messageJSON.GetArrayByKey(ipc_argumentList)
fmt.Println(messageId, name, argumentList)
if name == internalIPCJSExecuteGoSyncEventReplay {
m.ipcJSExecuteGoSyncEventMessageReply(int32(messageId), argumentList)
}
context.Free()
})
}
}
func (m *ipcRenderProcess) ipcJSExecuteGoSyncEventMessageReply(messageId int32, argumentList json.JSONArray) {
if callback := m.emitHandler.getCallback(messageId); callback != nil {
if callback.isSync {
if argumentList != nil {
callback.resultSyncChan <- argumentList.Bytes()
} else {
callback.resultSyncChan <- nil
}
}
callback.free()
}
}
......@@ -236,7 +236,7 @@ func (m *ipcRenderProcess) jsExecuteGoEvent(name string, object *ICefV8Value, ar
//单进程只有同步
if consts.SingleProcess {
//不通过进程消息
callback := &ipcCallback{isSync: true}
callback := &ipcCallback{}
if emitCallback != nil { //callback function
callback.resultType = rt_function
callback.function = emitCallback
......@@ -254,17 +254,18 @@ func (m *ipcRenderProcess) jsExecuteGoEvent(name string, object *ICefV8Value, ar
retVal.SetResult(V8ValueRef.NewBool(true))
}
} else { //多进程
var messageId int32 = 0
// 同步
if isSync {
callback := &ipcCallback{isSync: true}
callback := &ipcCallback{isSync: true, resultSyncChan: make(chan []byte)}
if emitCallback != nil {
callback.resultType = rt_function
callback.function = emitCallback
} else {
callback.resultType = rt_variable
}
m.emitHandler.addCallback(callback)
m.multiProcessSync(callback, isSync, emitNameValue, args)
messageId = m.emitHandler.addCallback(callback)
m.multiProcessSync(messageId, emitNameValue, callback, args)
if callback.resultType == rt_variable {
if callback.variable != nil {
retVal.SetResult(callback.variable)
......@@ -277,13 +278,12 @@ func (m *ipcRenderProcess) jsExecuteGoEvent(name string, object *ICefV8Value, ar
return
} else {
//异步
var messageId int32 = 0
if emitCallback != nil {
emitCallback.SetCanNotFree(true)
callback := &ipcCallback{resultType: rt_function, function: V8ValueRef.UnWrap(emitCallback)}
messageId = m.emitHandler.addCallback(callback)
}
if success := m.multiProcessAsync(m.v8Context.Frame(), messageId, isSync, emitNameValue, args); !success {
if success := m.multiProcessAsync(m.v8Context.Frame(), messageId, emitNameValue, args); !success {
//失败,释放回调函数
emitCallback.SetCanNotFree(false)
}
......@@ -315,27 +315,28 @@ func (m *ipcRenderProcess) singleProcess(emitName string, callback *ipcCallback,
m.executeCallbackFunction(false, callback, nil)
}
// multiProcessSync 多进程同步消息
func (m *ipcRenderProcess) multiProcessSync(callback *ipcCallback, isSync bool, emitName string, data []byte) {
fmt.Println("data", string(data))
// multiProcessSync 多进程消息 - 同步
func (m *ipcRenderProcess) multiProcessSync(messageId int32, emitName string, callback *ipcCallback, data []byte) {
message := json.NewJSONObject(nil)
message.Set(ipc_id, 1)
message.Set(ipc_type, isSync)
message.Set(ipc_id, messageId)
message.Set(ipc_event, emitName)
message.Set(ipc_name, internalIPCJSExecuteGoSyncEvent)
message.Set(ipc_browser_id, m.ipcChannel.browserId)
if data != nil {
message.Set(ipc_argumentList, json.NewJSONArray(data).Data())
} else {
message.Set(ipc_argumentList, nil)
}
m.ipcChannel.ipc.Send(message.Bytes())
dataBytes := <-callback.resultSyncChan
fmt.Println("resultSyncChan", dataBytes)
}
// multiProcessAsync 多进程异步消息
func (m *ipcRenderProcess) multiProcessAsync(frame *ICefFrame, messageId int32, isSync bool, emitName string, data []byte) bool {
// multiProcessAsync 多进程消息 - 异步
func (m *ipcRenderProcess) multiProcessAsync(frame *ICefFrame, messageId int32, emitName string, data []byte) bool {
if frame != nil {
message := json.NewJSONObject(nil)
message.Set(ipc_id, messageId)
message.Set(ipc_type, isSync)
message.Set(ipc_event, emitName)
if data != nil {
message.Set(ipc_argumentList, json.NewJSONArray(data).Data())
......@@ -395,22 +396,16 @@ func (m *ipcRenderProcess) ipcJSExecuteGoEventMessageReply(browser *ICefBrowser,
}()
//[]byte
returnArgs = argumentList.GetArrayByIndex(2)
if callback.isSync { // 同步
if returnArgs != nil {
} else {
}
} else { //异步
if callback.function != nil {
//设置允许释放
callback.function.SetCanNotFree(false)
}
if returnArgs != nil {
m.executeCallbackFunction(isReturnArgs, callback, returnArgs.Bytes())
} else {
m.executeCallbackFunction(isReturnArgs, callback, nil)
}
callback.free()
if callback.function != nil {
//设置允许释放
callback.function.SetCanNotFree(false)
}
if returnArgs != nil {
m.executeCallbackFunction(isReturnArgs, callback, returnArgs.Bytes())
} else {
m.executeCallbackFunction(isReturnArgs, callback, nil)
}
callback.free()
}
return
}
......
......@@ -35,10 +35,11 @@ const (
// ipc process message key
const (
ipc_id = "id"
ipc_type = "type"
ipc_event = "event"
ipc_argumentList = "argumentList"
ipc_id = "id" // 消息ID
ipc_event = "event" // 事件名
ipc_name = "name" // 消息名
ipc_browser_id = "browserId" //
ipc_argumentList = "argumentList" //
)
// js execute go 返回类型
......@@ -73,10 +74,11 @@ type ipcOnHandler struct {
// ipcCallback ipc.emit 回调结果
type ipcCallback struct {
isSync bool //是否异步
resultType result_type //返回值类型 0:function 1:variable 默认:0
variable *ICefV8Value //回调函数, 根据 resultType
function *ICefV8Value //回调函数, 根据 resultType
isSync bool //同否同步 true:同步 false:异步, 默认false
resultSyncChan chan []byte //接收同步chan, 默认 nil
resultType result_type //返回值类型 0:function 1:variable 默认:0
variable *ICefV8Value //回调函数, 根据 resultType
function *ICefV8Value //回调函数, 根据 resultType
}
// isIPCInternalKey IPC 内部定义使用 key 不允许使用
......@@ -180,6 +182,11 @@ func (m *ipcOnHandler) clear() {
m.callbackList = make(map[string]*ipcCallback)
}
// delayWaiting 同步消息, 延迟
func (m *ipcCallback) delayWaiting() {
}
//free 清空所有回调函数
func (m *ipcCallback) free() {
if m.function != nil {
......
......@@ -59,7 +59,9 @@ type JSONArray interface {
RemoveByIndex(index int) //删除指定下标数据
GetStringByIndex(index int) string //根据下标返回 string 类型值
GetIntByIndex(index int) int //根据下标返回 int 类型值
GetInt64ByIndex(index int) int64 //根据下标返回 int64 类型值
GetUIntByIndex(index int) uint //根据下标返回 uint 类型值
GetUInt64ByIndex(index int) uint64 //根据下标返回 uint64 类型值
GetBytesByIndex(index int) []byte //根据下标返回 []byte 类型值
GetFloatByIndex(index int) float64 //根据下标返回 float64 类型值
GetBoolByIndex(index int) bool //根据下标返回 bool 类型值
......@@ -77,7 +79,9 @@ type JSONObject interface {
RemoveByKey(key string) //删除指定key数据
GetStringByKey(key string) string //根据 key 返回 string 类型值
GetIntByKey(key string) int //根据 key 返回 int 类型值
GetInt64ByKey(key string) int64 //根据 key 返回 int64 类型值
GetUIntByKey(key string) uint //根据 key 返回 uint 类型值
GetUInt64ByKey(key string) uint64 //根据 key 返回 uint64 类型值
GetBytesByKey(key string) []byte //根据 key 返回 []byte 类型值
GetFloatByKey(key string) float64 //根据 key 返回 float64 类型值
GetBoolByKey(key string) bool //根据 key 返回 bool 类型值
......@@ -281,6 +285,13 @@ func (m *jsonData) GetIntByIndex(index int) int {
return 0
}
func (m *jsonData) GetInt64ByIndex(index int) int64 {
if m.IsArray() && index < m.S {
return m.toInt64(m.V.([]any)[index])
}
return 0
}
func (m *jsonData) GetUIntByIndex(index int) uint {
if m.IsArray() && index < m.S {
return m.toUInt(m.V.([]any)[index])
......@@ -288,6 +299,13 @@ func (m *jsonData) GetUIntByIndex(index int) uint {
return 0
}
func (m *jsonData) GetUInt64ByIndex(index int) uint64 {
if m.IsArray() && index < m.S {
return m.toUInt64(m.V.([]any)[index])
}
return 0
}
func (m *jsonData) GetBytesByIndex(index int) []byte {
if m.IsArray() && index < m.S {
return m.toBytes(m.V.([]any)[index])
......@@ -424,6 +442,13 @@ func (m *jsonData) GetIntByKey(key string) int {
return 0
}
func (m *jsonData) GetInt64ByKey(key string) int64 {
if m.IsObject() {
return m.toInt64(m.V.(map[string]any)[key])
}
return 0
}
func (m *jsonData) GetUIntByKey(key string) uint {
if m.IsObject() {
return m.toUInt(m.V.(map[string]any)[key])
......@@ -431,6 +456,13 @@ func (m *jsonData) GetUIntByKey(key string) uint {
return 0
}
func (m *jsonData) GetUInt64ByKey(key string) uint64 {
if m.IsObject() {
return m.toUInt64(m.V.(map[string]any)[key])
}
return 0
}
func (m *jsonData) GetBytesByKey(key string) []byte {
if m.IsObject() {
return m.toBytes(m.V.(map[string]any)[key])
......@@ -766,3 +798,63 @@ func (m *jsonData) toUInt(s any) uint {
}
return 0
}
func (m *jsonData) toUInt64(s any) uint64 {
switch s.(type) {
case float32:
return uint64(s.(float32))
case float64:
return uint64(s.(float64))
case int:
return uint64(s.(int))
case int8:
return uint64(s.(int8))
case int16:
return uint64(s.(int16))
case int32:
return uint64(s.(int32))
case int64:
return uint64(s.(int64))
case uint:
return uint64(s.(uint))
case uint8:
return uint64(s.(uint8))
case uint16:
return uint64(s.(uint16))
case uint32:
return uint64(s.(uint32))
case uint64:
return s.(uint64)
}
return 0
}
func (m *jsonData) toInt64(s any) int64 {
switch s.(type) {
case float32:
return int64(s.(float32))
case float64:
return int64(s.(float64))
case int:
return int64(s.(int))
case int8:
return int64(s.(int8))
case int16:
return int64(s.(int16))
case int32:
return int64(s.(int32))
case int64:
return s.(int64)
case uint:
return int64(s.(uint))
case uint8:
return int64(s.(uint8))
case uint16:
return int64(s.(uint16))
case uint32:
return int64(s.(uint32))
case uint64:
return int64(s.(uint64))
}
return 0
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册