diff --git a/cef/cef-application-callback.go b/cef/cef-application-callback.go index 16d58b29..bf13eea8 100644 --- a/cef/cef-application-callback.go +++ b/cef/cef-application-callback.go @@ -46,8 +46,8 @@ func browserProcessMessageReceived(browser *ICefBrowser, frame *ICefFrame, messa result = ipcBrowser.jsExecuteGoMethodMessage(browser, frame, message) } else if message.Name() == internalProcessMessageIPCOn { result = ipcBrowser.ipcOnMessage(browser, frame, message) - } else if message.Name() == internalProcessMessageIPCEmitReply { - //result = ipcBrowser.ipcGoExecuteMethodMessageReply(browser, frame, sourceProcess, message) + } else if message.Name() == internalIPCGoExecuteJSEventReplay { + result = ipcBrowser.ipcGoExecuteMethodMessageReply(browser, frame, message) } return } diff --git a/cef/cef-chromium-proc.go b/cef/cef-chromium-proc.go index 175bb722..83762090 100644 --- a/cef/cef-chromium-proc.go +++ b/cef/cef-chromium-proc.go @@ -749,7 +749,9 @@ func (m *TCEFChromium) SendProcessMessageForV8Value(messageName string, targetPr func (m *TCEFChromium) EmitRender(messageId int32, eventName string, target ipc.ITarget, data ...any) { if !m.initialized { m.initialized = m.Initialized() - return + if !m.initialized { + return + } } if target == nil || target.GetBrowserId() <= 0 || target.GetFrameId() <= 0 { message := json.NewJSONObject(nil) diff --git a/cef/cef-ipc-browser.go b/cef/cef-ipc-browser.go index 420e497c..b7c6e796 100644 --- a/cef/cef-ipc-browser.go +++ b/cef/cef-ipc-browser.go @@ -17,7 +17,6 @@ import ( "github.com/energye/energy/ipc" "github.com/energye/energy/ipc/channel" "github.com/energye/energy/pkgs/json" - jsoniter "github.com/json-iterator/go" ) // ipcBrowserProcess 主进程 @@ -28,30 +27,30 @@ type ipcBrowserProcess struct { ipcChannel channel.IBrowserChannel } -func (m *ipcBrowserProcess) ipcChannelBrowser() { - if m.ipcChannel == nil { - m.ipcChannel = channel.NewBrowser() - m.ipcChannel.Handler(func(context channel.IIPCContext) { - var data ipcChannelMessage - if err := jsoniter.Unmarshal(context.Message().Data(), &data); err == nil { - //messageId := message.GetUIntByKey(ipc_id) - //name := message.GetStringByKey(ipc_name) - //message.Set(ipc_event, emitNameValue) - //message.Set(ipc_argumentList, json.NewJSONArray(args).Data()) - fmt.Println("ipcChannelBrowser", err) - fmt.Println("data", data.Name, data.EventName, json.NewJSONArray(data.Data).GetIntByIndex(1)) - if data.Name == internalIPCJSExecuteGoEvent { - //result = ipcBrowser.ipcGoExecuteMethodMessage(browser, frame, sourceProcess, message) - } else if data.Name == internalProcessMessageIPCOn { - //result = ipcBrowser.ipcOnMessage(browser, frame, sourceProcess, message) - } else if data.Name == internalProcessMessageIPCEmitReply { - //result = ipcBrowser.ipcGoExecuteMethodMessageReply(browser, frame, sourceProcess, message) - } - } - context.Free() - }) - } -} +//func (m *ipcBrowserProcess) ipcChannelBrowser() { +// if m.ipcChannel == nil { +// m.ipcChannel = channel.NewBrowser() +// m.ipcChannel.Handler(func(context channel.IIPCContext) { +// var data ipcChannelMessage +// if err := jsoniter.Unmarshal(context.Message().Data(), &data); err == nil { +// //messageId := message.GetUIntByKey(ipc_id) +// //name := message.GetStringByKey(ipc_name) +// //message.Set(ipc_event, emitNameValue) +// //message.Set(ipc_argumentList, json.NewJSONArray(args).Data()) +// fmt.Println("ipcChannelBrowser", err) +// fmt.Println("data", data.Name, data.EventName, json.NewJSONArray(data.Data).GetIntByIndex(1)) +// if data.Name == internalIPCJSExecuteGoEvent { +// //result = ipcBrowser.ipcGoExecuteMethodMessage(browser, frame, sourceProcess, message) +// } else if data.Name == internalProcessMessageIPCOn { +// //result = ipcBrowser.ipcOnMessage(browser, frame, sourceProcess, message) +// } else if data.Name == internalProcessMessageIPCEmitReply { +// //result = ipcBrowser.ipcGoExecuteMethodMessageReply(browser, frame, sourceProcess, message) +// } +// } +// context.Free() +// }) +// } +//} // ipcGoExecuteMethodMessage 执行 Go 监听函数 func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) { @@ -126,39 +125,49 @@ func (m *ipcBrowserProcess) jsExecuteGoMethodMessage(browser *ICefBrowser, frame return } -func (m *ipcBrowserProcess) ipcGoExecuteMethodMessageReply(browser *ICefBrowser, frame *ICefFrame, sourceProcess consts.CefProcessId, message *ICefProcessMessage) (result bool) { - //fmt.Println("ipcGoExecuteMethodMessageReply", message.Name()) - //messageId := message.ArgumentList().GetInt(0) - //if callback := ipc.CheckEmitCallback(messageId); callback != nil { - // //第二个参数 true 有返回参数 - // if isReturn := message.ArgumentList().GetBool(1); isReturn { - // //[]byte - // binaryValue := message.ArgumentList().GetBinary(2) - // var ( - // count uint32 - // resultArgsBytes []byte - // ) - // if binaryValue.IsValid() { - // size := binaryValue.GetSize() - // resultArgsBytes = make([]byte, size) - // count = binaryValue.GetData(resultArgsBytes, 0) - // binaryValue.Free() - // } - // if count > 0 { - // ipcContext := ipc.NewContext(browser.Identifier(), frame.Identifier(), false, resultArgsBytes) - // if ctxCallback := callback.ContextCallback(); ctxCallback != nil { - // ctxCallback.Invoke(ipcContext) - // } else if argsCallback := callback.ArgumentCallback(); argsCallback != nil { - // argsCallback.Invoke(ipcContext) - // } - // if ipcContext.ArgumentList() != nil { - // ipcContext.ArgumentList().Free() - // } - // ipcContext.Result(nil) - // } - // resultArgsBytes = nil - // } - //} +func (m *ipcBrowserProcess) ipcGoExecuteMethodMessageReply(browser *ICefBrowser, frame *ICefFrame, message *ICefProcessMessage) (result bool) { + argumentListBytes := message.ArgumentList().GetBinary(0) + if argumentListBytes == nil { + return + } + result = true + var messageDataBytes []byte + if argumentListBytes.IsValid() { + size := argumentListBytes.GetSize() + messageDataBytes = make([]byte, size) + c := argumentListBytes.GetData(messageDataBytes, 0) + argumentListBytes.Free() + message.Free() + if c == 0 { + return + } + } + var messageId int32 + var argument json.JSON + var argumentList json.JSONArray + if messageDataBytes != nil { + argument = json.NewJSON(messageDataBytes) + messageId = int32(argument.GetIntByKey(ipc_id)) + argumentList = argument.GetArrayByKey(ipc_argumentList) + messageDataBytes = nil + } + defer func() { + if argument != nil { + argument.Free() + } + }() + if callback := ipc.CheckEmitCallback(messageId); callback != nil { + ipcContext := ipc.NewContext(browser.Identifier(), frame.Identifier(), false, argumentList) + if ctxCallback := callback.ContextCallback(); ctxCallback != nil { + ctxCallback.Invoke(ipcContext) + } else if argsCallback := callback.ArgumentCallback(); argsCallback != nil { + argsCallback.Invoke(ipcContext) + } + if ipcContext.ArgumentList() != nil { + ipcContext.ArgumentList().Free() + } + ipcContext.Result(nil) + } return } diff --git a/cef/cef-ipc-render.go b/cef/cef-ipc-render.go index f8d86146..c75e5703 100644 --- a/cef/cef-ipc-render.go +++ b/cef/cef-ipc-render.go @@ -135,40 +135,38 @@ func (m *ipcRenderProcess) ipcGoExecuteJSEvent(browser *ICefBrowser, frame *ICef }() if callback := ipcRender.onHandler.getCallback(emitName); callback != nil { - //if callback.context.Enter() { - fmt.Println("messageId:", messageId) - //1 - //argumentArrayValue, _ := ipcValueConvert.BytesToV8ArrayValue(argumentList.Bytes()) - //fmt.Println("messageId", messageId, "err", err, argumentArrayValue) - //ret := callback.function.ExecuteFunctionWithContext(callback.context, nil, argumentArrayValue) - //argumentArrayValue.Free() - //2 - argumentArrayValue := ipcValueConvert.BytesToV8ValueArray(argumentList.Bytes()) - ret := callback.function.ExecuteFunctionWithContext(m.v8Context, nil, argumentArrayValue) - argumentArrayValue.Free() - //3 - //ret := callback.function.ExecuteFunctionWithContextForArgsBytes(callback.context, nil, argumentList.Bytes()) - argumentList.Free() - //fmt.Println("messageId", messageId, "ret", ret) - //if messageId != 0 { // callback - //ipcValueConvert.V8ValueToProcessMessage(ret) - //ret := V8ValueRef.NewString("字符串") - frame.SendProcessMessageForV8Value(internalProcessMessageIPCEmitReply, consts.PID_BROWSER, ret) - - //var retBytes []byte - //retBytes = ipcValueConvert.V8ValueToProcessMessageBytes(ret) - //retMessage := ProcessMessageRef.new(internalProcessMessageIPCEmitReply) - //if len(retBytes) > 0 { - // retMessage.ArgumentList().SetBinary(0, BinaryValueRef.New(retBytes)) - // retBytes = nil - //} - //frame.SendProcessMessage(consts.PID_BROWSER, retMessage) - //retMessage.Free() - //} - //ret.Free() - //m.v8Context.Exit() - //frame.SendProcessMessageForV8Value(internalProcessMessageIPCEmitReply, consts.PID_BROWSER, ret) - //} + var callbackArgsBytes []byte + if m.v8Context.Enter() { + if m.v8Context.Enter() { + var ret *ICefV8Value + var argsArray *TCefV8ValueArray + argsArray, _ = ipcValueConvert.BytesToV8ArrayValue(argumentList.Bytes()) + if argsArray != nil { + ret = callback.function.ExecuteFunctionWithContext(m.v8Context, nil, argsArray) + argsArray.Free() + } else { + ret = callback.function.ExecuteFunctionWithContext(m.v8Context, nil, nil) + } + if ret != nil && ret.IsValid() && messageId != 0 { //callback func args + callbackArgsBytes = ipcValueConvert.V8ValueToProcessMessageBytes(ret) + ret.Free() + } else if ret != nil { + ret.Free() + } + m.v8Context.Exit() + } + argumentList.Free() + } + if messageId != 0 { //callback func + callbackMessage := json.NewJSONObject(nil) + callbackMessage.Set(ipc_id, messageId) + if callbackArgsBytes != nil { + callbackMessage.Set(ipc_argumentList, json.NewJSONArray(callbackArgsBytes).Data()) + } else { + callbackMessage.Set(ipc_argumentList, nil) + } + m.v8Context.Frame().SendProcessMessageForJSONBytes(internalIPCGoExecuteJSEventReplay, consts.PID_BROWSER, callbackMessage.Bytes()) + } result = true } return @@ -313,15 +311,11 @@ func (m *ipcRenderProcess) ipcJSExecuteGoEventMessageReply(browser *ICefBrowser, //解析 '[]byte' 参数 if m.v8Context.Enter() { var argsArray *TCefV8ValueArray - //argsArray = ipcValueConvert.BytesToV8ValueArray(returnArgs.Bytes()) - //fmt.Println("json:", returnArgs.ToJSONString()) argsArray, _ = ipcValueConvert.BytesToV8ArrayValue(returnArgs.Bytes()) if argsArray != nil { - //callback.function.ExecuteFunctionWithContextForArgsBytes(m.v8Context, nil, returnArgs.Bytes()).Free() callback.function.ExecuteFunctionWithContext(m.v8Context, nil, argsArray).Free() argsArray.Free() } else { - //callback.function.ExecuteFunctionWithContextForArgsBytes(m.v8Context, nil, returnArgs.Bytes()).Free() callback.function.ExecuteFunctionWithContext(m.v8Context, nil, nil).Free() } m.v8Context.Exit() diff --git a/cef/cef-ipc-value-convert.go b/cef/cef-ipc-value-convert.go index 9a7fdbfb..5cc68f1b 100644 --- a/cef/cef-ipc-value-convert.go +++ b/cef/cef-ipc-value-convert.go @@ -15,7 +15,6 @@ package cef import ( "errors" - "fmt" "github.com/energye/energy/common/imports" "github.com/energye/energy/consts" "github.com/energye/energy/pkgs/json" @@ -320,7 +319,6 @@ func (m *v8ValueProcessMessageConvert) V8valueArrayToSlice(v8value *ICefV8Value) for i := 0; i < argsLen; i++ { args := v8value.GetValueByIndex(i) if !args.IsValid() { - fmt.Println("slice IsValid:", i) result[i] = "null" continue } @@ -373,7 +371,6 @@ func (m *v8ValueProcessMessageConvert) V8valueObjectToMap(v8value *ICefV8Value) key := keys.Get(i) args := v8value.getValueByKey(key) if !args.IsValid() { - fmt.Println("map IsValid:", i, key) result[key] = "null" continue } diff --git a/cef/cef-ipc.go b/cef/cef-ipc.go index 7c959968..e01e4fcd 100644 --- a/cef/cef-ipc.go +++ b/cef/cef-ipc.go @@ -24,7 +24,8 @@ const ( const ( internalIPCJSExecuteGoEvent = "JSEmitGo" internalIPCJSExecuteGoEventReplay = "JSEmitGoReplay" - internalIPCGoExecuteJSEvent = "GoEmitJS" // 进程消息 on监听事件处理 + internalIPCGoExecuteJSEvent = "GoEmitJS" + internalIPCGoExecuteJSEventReplay = "GoEmitJSReplay" internalProcessMessageIPCEmit = "emitHandler" // 进程消息 emit事件处理 internalProcessMessageIPCEmitReply = "emitReply" // 进程消息 emit事件回复消息 internalProcessMessageIPCOn = "onHandler" // 进程消息 on监听事件处理 diff --git a/example/dev-test/ipc-event/ipc-event.go b/example/dev-test/ipc-event/ipc-event.go index 77a28549..d3fadded 100644 --- a/example/dev-test/ipc-event/ipc-event.go +++ b/example/dev-test/ipc-event/ipc-event.go @@ -83,7 +83,10 @@ func main() { tm = time.Now().Second() } //触发JS监听的事件,并传入参数 - ipc.Emit("onTestName1", r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10) + //ipc.Emit("onTestName1", r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10) + ipc.EmitAndCallback("onTestName2", []interface{}{r0, testGoEmit, r2, r3, r4, r5, r6, r7, r8, r9, r10}, func(r1 string, r2 int, r3 float64, r4 bool) { + fmt.Println("onTestName1 callback", r1, r2, r3, r4) + }) }) ipc.On("testGoEmitAndCallback", func() { testGoEmitAndCallback++ diff --git a/example/dev-test/ipc-event/resources/ipc-event.html b/example/dev-test/ipc-event/resources/ipc-event.html index bde0305d..dc4e0876 100644 --- a/example/dev-test/ipc-event/resources/ipc-event.html +++ b/example/dev-test/ipc-event/resources/ipc-event.html @@ -147,10 +147,23 @@ msg("R9:", r9, JSON.stringify(r9)); msg("R10:", r10, JSON.stringify(r10)); }); + let onTestName2Idx = 0; ipc.on('onTestName2', function (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10) { + onTestName2Idx++; clearMsg() msg("onTestName2"); - return ["返回值", 111111, 2222.22, true] + msg("R0:", r0); + msg("R1:", r1); + msg("R2:", r2); + msg("R3:", r3); + msg("R4:", r4); + msg("R5:", r5); + msg("R6:", r6, JSON.stringify(r6)); + msg("R7:", r7, JSON.stringify(r7)); + msg("R8:", r8, JSON.stringify(r8)); + msg("R9:", r9, JSON.stringify(r9)); + msg("R10:", r10, JSON.stringify(r10)); + return ["返回值", onTestName2Idx, 2222.22, true] }); let tgesi = null