private MessageBridgeResponse OnSetObjectProperty(MessageBridgeRequest request)
        {
            if (request.Name == SET_OBJECT_PROPERTY)
            {
                var objectKey = request.Arguments[0].GetString();

                var propertyName = request.Arguments[1].GetString();

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


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

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

                        if (value.IsProperty)
                        {
                            value.Property?.Setter?.Invoke(retval);

                            return(MessageBridgeResponse.CreateSuccessResponse());
                        }
                    }
                }

                return(MessageBridgeResponse.CreateFailureResponse("Setter is not defined."));
            }

            return(null);
        }
        private MessageBridgeResponse OnExecuteExtensionFunction(MessageBridgeRequest request)
        {
            if (request.Name == EXECUTE_EXT_JAVASCRIPT_FUNCION)
            {
                var owner = BrowserSideBridge.Owner;

                var extensionName = request.Arguments[0].GetString();
                var functionName  = request.Arguments[1].GetString();
                var arguments     = JavaScriptValue.FromJson(request.Arguments[2].GetString()).ToArray();
                var uuid          = new Guid(request.Arguments[3].GetString());


                var extension = WinFormium.Runtime.Container.GetInstance <JavaScriptExtensionBase>(extensionName);

                if (extension == null)
                {
                    return(MessageBridgeResponse.CreateFailureResponse($"JavaScript extension handler is not found."));
                }

                var function = extension.FunctionHandlers.SingleOrDefault(x => x.FuntionName.Equals(functionName));

                if (function == null)
                {
                    return(MessageBridgeResponse.CreateFailureResponse($"{functionName} is not defined."));
                }

                if (function.FunctionType == JavaScriptExtensionFunctionHandlerType.BrowserSideFunction && function.BrowserSideFunction != null)
                {
                    JavaScriptValue retval = null;


                    owner.InvokeIfRequired(() => {
                        retval = function.BrowserSideFunction.Invoke(owner, arguments);
                    });

                    retval = retval ?? JavaScriptValue.CreateNull();

                    var response = MessageBridgeResponse.CreateSuccessResponse();
                    response.Arguments.Add(MessageValue.CreateString(retval.ToDefinition()));
                    return(response);
                }
                else if (function.FunctionType == JavaScriptExtensionFunctionHandlerType.BrowserSideAsyncFunction && function.BrowserSideAsyncFunction != null)
                {
                    owner.InvokeIfRequired(() => {
                        function.BrowserSideAsyncFunction.Invoke(owner, arguments, new JavaScriptAsyncFunctionCallback(owner.GetMainFrame(), uuid, this));
                    });

                    var response = MessageBridgeResponse.CreateSuccessResponse();
                    return(response);
                }


                return(MessageBridgeResponse.CreateFailureResponse($"{functionName} is not defined."));
            }

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

            return(null);
        }
 public JavaScriptExecuteionTask(JavaScriptCommunicationBridge bridge, CefFrame frame, MessageArrayValue arguments)
 {
     this.jsbridge  = bridge;
     this.frame     = frame;
     taskId         = arguments[0].GetInt();
     functionId     = arguments[1].GetInt();
     this.arguments = JavaScriptValue.FromJson(arguments[2].GetString());
 }
        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);
        }
        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);
        }
Пример #6
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);
        }