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);
        }
Exemplo n.º 2
0
        public static JavaScriptValue ToJSValue(this CefV8Value source)
        {
            if (source == null || !source.IsValid)
            {
                throw new ArgumentNullException();
            }

            JavaScriptValue target;

            if (source.IsFunction)
            {
                var context = CefV8Context.GetEnteredContext();

                var info = new JavaScriptRenderSideFunction(context, source);

                JavaScriptObjectRepository.RenderSideFunctions.Add(info);

                target = JavaScriptValue.CreateFunction(info);
            }
            else if (source.IsArray)
            {
                target = JavaScriptValue.CreateArray();
                for (int i = 0; i < source.GetArrayLength(); i++)
                {
                    var item = source.GetValue(i);
                    if (item != null && item.IsValid)
                    {
                        target.AddArrayValue(item.ToJSValue());
                    }
                }
            }
            else if (source.IsObject)
            {
                target = JavaScriptValue.CreateObject();
                foreach (var key in source.GetKeys())
                {
                    var item = source.GetValue(key);
                    if (item != null && item.IsValid)
                    {
                        target.SetValue(key, item.ToJSValue());
                    }
                }
            }
            else if (source.IsBool)
            {
                target = JavaScriptValue.CreateBool(source.GetBoolValue());
            }
            else if (source.IsDate)
            {
                target = JavaScriptValue.CreateDateTime(source.GetDateValue());
            }
            else if (source.IsDouble)
            {
                target = JavaScriptValue.CreateNumber(source.GetDoubleValue());
            }
            else if (source.IsInt)
            {
                target = JavaScriptValue.CreateNumber(source.GetIntValue());
            }
            else if (source.IsUInt)
            {
                target = JavaScriptValue.CreateNumber(source.GetUIntValue());
            }
            else if (source.IsString)
            {
                target = JavaScriptValue.CreateString(source.GetStringValue());
            }
            else
            {
                target = JavaScriptValue.CreateNull();
            }


            return(target);
        }
        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);
        }