private MessageBridgeResponse OnExecuteFunction(MessageBridgeRequest request)
        {
            if (request.Name == EXECUTE_JAVASCRIPT_FUNCTION)
            {
                var objectKey    = request.Arguments[0].GetString();
                var propertyName = request.Arguments[1].GetString();

                var arguments = JavaScriptValue.FromJson(request.Arguments[2].GetString()).ToArray();


                if (RegisteredJavaScriptObjects.ContainsKey(objectKey))
                {
                    var targetObject = RegisteredJavaScriptObjects[objectKey];

                    if (targetObject != null && targetObject.HasValue(propertyName))
                    {
                        var value = targetObject.GetValue(propertyName);

                        if (value.IsFunction)
                        {
                            var functionInfo = value.JSFunctionDescriber;

                            if (functionInfo != null)
                            {
                                if (functionInfo.IsAsync)
                                {
                                    var uuid  = new Guid(request.Arguments[3].GetString());
                                    var frame = BrowserSideBridge.Owner.GetMainFrame();
                                    value.AsyncMethod?.Invoke(arguments, new JavaScriptAsyncFunctionCallback(frame, /* objectKey, propertyName,*/ uuid, this));

                                    var response = MessageBridgeResponse.CreateSuccessResponse();

                                    return(response);
                                }
                                else
                                {
                                    var retval   = value.Method?.Invoke(arguments) ?? JavaScriptValue.CreateNull();
                                    var response = MessageBridgeResponse.CreateSuccessResponse();

                                    response.Arguments.Add(MessageValue.CreateString(retval.ToDefinition()));

                                    return(response);
                                }
                            }
                        }
                    }
                }
            }

            //return MessageBridgeResponse.CreateFailureResponse("Function is not defined.");

            return(null);
        }
示例#2
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            var context = CefV8Context.GetCurrentContext();

            var browser = context.GetBrowser();

            var frame = browser.GetMainFrame();

            var function = Extension.FunctionHandlers.SingleOrDefault(x => x.FuntionName.Equals(name));

            //WinFormium.GetLogger().Debug($"{name}");

            if (function == null)
            {
                exception   = $"[NanUI]:{name} is not defined.";
                returnValue = null;


                return(true);
            }



            var args = JavaScriptValue.CreateArray();

            var index = 0;

            foreach (var arg in arguments)
            {
                var value = arg.ToJSValue();

                if (value != null)
                {
                    args.SetValue(index++, value);
                }
            }


            exception = null;

            var uuid = Guid.NewGuid();



            if (function.FunctionType == JavaScriptExtensionFunctionHandlerType.RendererSideFunction || function.FunctionType == JavaScriptExtensionFunctionHandlerType.RendererSideAsyncFunction)
            {
                if (function.FunctionType == JavaScriptExtensionFunctionHandlerType.RendererSideFunction)
                {
                    var retval = function.RendererSideFunction.Invoke(args.ToArray());
                    returnValue = retval?.ToCefV8Value() ?? CefV8Value.CreateUndefined();
                }
                else
                {
                    function.RendererSideAsyncFunction.Invoke(args.ToArray(), new JavaScriptRendererSideAsyncFunctionCallback(frame, uuid, JSBridge));

                    var callback    = CefV8Value.CreateObject();
                    var successFunc = CefV8Value.CreateFunction("success", new JavaScriptBridgeFunctionCallbackHandler(uuid, context));

                    var errorFunc = CefV8Value.CreateFunction("error", new JavaScriptBridgeFunctionCallbackHandler(uuid, context));


                    callback.SetValue("success", successFunc);
                    callback.SetValue("error", errorFunc);

                    returnValue = callback;
                }

                return(true);
            }
            else
            {
                var request = MessageBridgeRequest.Create(JavaScriptCommunicationBridge.EXECUTE_EXT_JAVASCRIPT_FUNCION, browser.Identifier, frame.Identifier, context.GetHashCode());


                request.Arguments.Add(MessageValue.CreateString(Extension.Name));

                request.Arguments.Add(MessageValue.CreateString(name));

                request.Arguments.Add(MessageValue.CreateString(args.ToDefinition()));

                request.Arguments.Add(MessageValue.CreateString($"{uuid}"));


                var response = JSBridge.SendExecutionRequest(request);



                if (response.IsSuccess)
                {
                    if (function.FunctionType == JavaScriptExtensionFunctionHandlerType.BrowserSideFunction)
                    {
                        if (response.Arguments != null && response.Arguments.Count > 0)
                        {
                            var retval = JavaScriptValue.FromJson(response.Arguments[0].GetString())?.ToCefV8Value();


                            if (retval != null)
                            {
                                returnValue = retval;
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        var callback    = CefV8Value.CreateObject();
                        var successFunc = CefV8Value.CreateFunction("success", new JavaScriptBridgeFunctionCallbackHandler(uuid, context));

                        var errorFunc = CefV8Value.CreateFunction("error", new JavaScriptBridgeFunctionCallbackHandler(uuid, context));

                        callback.SetValue("success", successFunc);
                        callback.SetValue("error", errorFunc);

                        returnValue = callback;

                        return(true);
                    }

                    returnValue = CefV8Value.CreateUndefined();
                }
                else
                {
                    returnValue = null;
                    exception   = response.ExceptionMessage;
                }
            }

            return(true);
        }