protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            if (name == _name)
            {
                var bid = _context.GetBrowser()?.Identifier ?? 0;
                var fid = _context.GetFrame()?.Identifier ?? 0;

                var request = MessageBridgeRequest.Create(JavaScriptCommunicationBridge.EXECUTE_JAVASCRIPT_FUNCTION, bid, fid, _context.GetHashCode());

                request.Arguments.Add(MessageValue.CreateString(_parentKey));
                request.Arguments.Add(MessageValue.CreateString(_name));

                var args = JavaScriptValue.CreateArray();

                var index = 0;

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

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

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

                var guid = Guid.NewGuid();

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

                var response = _bridge.SendExecutionRequest(request);

                if (response.IsSuccess)
                {
                    if (_functionInfo.IsAsync)
                    {
                        var callback = CefV8Value.CreateObject();

                        var successFunc = CefV8Value.CreateFunction("success", new JavaScriptBridgeFunctionCallbackHandler(/*_parentKey, _name,*/ guid, _context));

                        var errorFunc = CefV8Value.CreateFunction("error", new JavaScriptBridgeFunctionCallbackHandler(/*_parentKey, _name,*/ guid, _context));

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

                        returnValue = callback;
                        exception   = null;
                    }
                    else
                    {
                        var retval = JavaScriptValue.FromJson(response.Arguments[0].GetString())?.ToCefV8Value();

                        exception = null;

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

                return(true);
            }

            returnValue = null;
            exception   = $"{name} is not defined.";

            return(true);
        }
Ejemplo n.º 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);
        }