コード例 #1
0
        public InternalHandle executeFunction(V8Engine engine, bool isConstructCall, InternalHandle _this, InternalHandle[] args)
        {
            try
            {
                if (args.Length >= 2)
                {
                    var id = args[0].AsString;
                    if (!string.IsNullOrEmpty(id) && args[1].IsArray)
                    {
                        var _context = CefV8Context.GetCurrentContext();
                        if (_context != null)
                        {
                            var _global = _context.GetGlobal();
                            if (_global != null)
                            {
                                var _function = _global.GetValue(id);
                                if (_function != null && _function.IsFunction)
                                {
                                    var len        = args[1].ArrayLength;
                                    var parameters = new dynamic[len];
                                    for (int x = 0; x < len; x++)
                                    {
                                        parameters[x] = args[1].GetProperty(x).Value;
                                    }

                                    var runner = CefTaskRunner.GetForCurrentThread();
                                    new Task(() =>
                                    {
                                        _context.Enter();
                                        if (runner != null)
                                        {
                                            runner.PostTask(new CallbackTask(_context, _function, parameters));
                                        }
                                        _context.Exit();
                                    }).Start();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Trace(ex.Message, ex.StackTrace, configuration.Verbose);
            }

            return(Engine.CreateNullValue());
        }
コード例 #2
0
        private CefV8Value DontMeetRequirements(CefV8Context context, CefV8Value callback)
        {
            var msg = JsonMapper.ToJson(new StatusHelper("Error", "Arguments do not meet function requirement"));

            if (callback != null)
            {
                var runner = CefTaskRunner.GetForCurrentThread();
                new Task(() =>
                {
                    context.Enter();
                    runner.PostTask(new CallbackTask(context, callback, msg));
                    context.Exit();
                }).Start();
                return(CefV8Value.CreateNull());
            }
            else
            {
                return(CefV8Value.CreateString(msg));
            }
        }
コード例 #3
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name == "query")
                {
                    if (arguments.Length >= 4)
                    {
                        var sql           = arguments[0].GetStringValue();
                        var connection    = arguments[1].GetStringValue();
                        var returntype    = arguments[2].GetStringValue();
                        var executionMode = arguments[3].GetStringValue();
                        var callback      = (arguments.Length == 5 ? arguments[4] : null);

                        if (!string.IsNullOrEmpty(sql) && !string.IsNullOrEmpty(connection) && !string.IsNullOrEmpty(returntype) && !string.IsNullOrEmpty(executionMode))
                        {
                            if (callback != null && callback.IsFunction)
                            {
                                var runner = CefTaskRunner.GetForCurrentThread();
                                new Task(() =>
                                {
                                    context.Enter();
                                    runner.PostTask(new CallbackTask(context, callback, query(sql, connection, returntype, executionMode)));
                                    context.Exit();
                                }).Start();
                            }
                            else
                            {
                                returnValue = CefV8Value.CreateString(query(sql, connection, returntype, executionMode));
                            }
                        }
                        else
                        {
                            returnValue = Extension.DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = Extension.DontMeetRequirements(context, null);
                    }
                }
                else if (name == "reader")
                {
                    if (arguments.Length >= 4)
                    {
                        var sql        = arguments[0].GetStringValue();
                        var connection = arguments[1].GetStringValue();
                        var returntype = arguments[2].GetStringValue();
                        var callback   = arguments[3];

                        if (!string.IsNullOrEmpty(sql) && !string.IsNullOrEmpty(connection) && !string.IsNullOrEmpty(returntype) && callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                try
                                {
                                    var db = new ConnectionHelper(connection);
                                    if (db != null)
                                    {
                                        db.Open();
                                        var cmd = db.CreateCommand(sql);
                                        if (cmd != null)
                                        {
                                            var dataReader = cmd.ExecuteReader();
                                            if (dataReader != null)
                                            {
                                                var hasData = dataReader.Read();
                                                if (!hasData)
                                                {
                                                    if (returntype.ToLower().Equals("json"))
                                                    {
                                                        var row = new Dictionary <string, object>();
                                                        runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(row)));
                                                    }
                                                    else if (returntype.ToLower().Equals("xml"))
                                                    {
                                                        var doc = new XElement("Item");
                                                        runner.PostTask(new CallbackTask(context, callback, doc.ToString()));
                                                    }
                                                }

                                                while (hasData)
                                                {
                                                    var columns = Enumerable.Range(0, dataReader.FieldCount()).Select(dataReader.GetName).ToList();
                                                    if (returntype.ToLower().Equals("json"))
                                                    {
                                                        while (dataReader.Read())
                                                        {
                                                            var row = new Dictionary <string, object>();
                                                            for (int x = 0; x < columns.Count; x++)
                                                            {
                                                                var column = columns[x];
                                                                row.Add(columns[x], Extension.ObjectToString(dataReader.GetValue(dataReader.GetOrdinal(column))));
                                                            }
                                                            runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(row)));
                                                        }
                                                    }
                                                    else if (returntype.ToLower().Equals("xml"))
                                                    {
                                                        while (dataReader.Read())
                                                        {
                                                            var row = new Dictionary <string, object>();
                                                            for (int x = 0; x < columns.Count; x++)
                                                            {
                                                                var column = columns[x];
                                                                row.Add(columns[x], Extension.ObjectToString(dataReader.GetValue(dataReader.GetOrdinal(column))));
                                                            }
                                                            var doc = new XElement("Item", row.Select(x => new XElement(x.Key, x.Value)));
                                                            runner.PostTask(new CallbackTask(context, callback, doc.ToString()));
                                                        }
                                                    }
                                                    hasData = dataReader.Read();
                                                }
                                                dataReader.Close();
                                            }
                                        }
                                        db.Close();
                                    }
                                    else
                                    {
                                        var msg = new StatusHelper("Error", "Failed to create database connection");
                                        runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(msg)));
                                    }
                                }
                                catch (Exception ex)
                                {
                                    var msg = new StatusHelper("Error", ex.Message);
                                    runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(msg)));
                                }
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            returnValue = Extension.DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = Extension.DontMeetRequirements(context, null);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }
コード例 #4
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name == "compile" || name == "loadScript" || name == "loadScriptCompiled")
                {
                    if (arguments.Length >= 3)
                    {
                        var script                = arguments[0].GetStringValue();
                        var sourceName            = arguments[1].GetStringValue();
                        var throwExceptionOnError = arguments[2].GetBoolValue();
                        var callback              = (arguments.Length == 4 ? arguments[3] : null);

                        if (!string.IsNullOrEmpty(script) && !string.IsNullOrEmpty(sourceName))
                        {
                            if (callback != null && callback.IsFunction)
                            {
                                var runner = CefTaskRunner.GetForCurrentThread();
                                new Task(() =>
                                {
                                    context.Enter();
                                    if (name == "compile")
                                    {
                                        engine.Compile(script, sourceName, throwExceptionOnError);
                                    }
                                    else if (name == "loadScript")
                                    {
                                        engine.LoadScript(script, sourceName, throwExceptionOnError);
                                    }
                                    else if (name == "loadScriptCompiled")
                                    {
                                        engine.LoadScriptCompiled(script, sourceName, throwExceptionOnError);
                                    }
                                    runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", name))));
                                    context.Exit();
                                }).Start();
                            }
                            else
                            {
                                if (name == "compile")
                                {
                                    engine.Compile(script, sourceName, throwExceptionOnError);
                                }
                                else if (name == "loadScript")
                                {
                                    engine.LoadScript(script, sourceName, throwExceptionOnError);
                                }
                                else if (name == "loadScriptCompiled")
                                {
                                    engine.LoadScriptCompiled(script, sourceName, throwExceptionOnError);
                                }
                                returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", name)));
                            }
                        }
                        else
                        {
                            returnValue = DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    }
                }
                else if (name == "consoleExecute" || name == "execute" || name == "verboseConsoleExecute")
                {
                    if (arguments.Length >= 4)
                    {
                        var script                = arguments[0].GetStringValue();
                        var sourceName            = arguments[1].GetStringValue();
                        var throwExceptionOnError = arguments[2].GetBoolValue();
                        var timeOut               = arguments[3].GetIntValue();
                        var callback              = (arguments.Length == 5 ? arguments[4] : null);

                        if (!string.IsNullOrEmpty(script) && !string.IsNullOrEmpty(sourceName))
                        {
                            if (callback != null && callback.IsFunction)
                            {
                                var runner = CefTaskRunner.GetForCurrentThread();
                                new Task(() =>
                                {
                                    context.Enter();
                                    if (name == "consoleExecute")
                                    {
                                        engine.ConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    else if (name == "execute")
                                    {
                                        engine.Execute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    else if (name == "verboseConsoleExecute")
                                    {
                                        engine.VerboseConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                    }
                                    runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", name))));
                                    context.Exit();
                                }).Start();
                            }
                            else
                            {
                                if (name == "consoleExecute")
                                {
                                    engine.ConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                else if (name == "execute")
                                {
                                    engine.Execute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                else if (name == "verboseConsoleExecute")
                                {
                                    engine.VerboseConsoleExecute(script, sourceName, throwExceptionOnError, timeOut);
                                }
                                returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", name)));
                            }
                        }
                        else
                        {
                            returnValue = DontMeetRequirements(context, callback);
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    }
                }
                else if (name == "terminateExecution")
                {
                    if (arguments.Length >= 0)
                    {
                        var callback = (arguments.Length == 1 ? arguments[0] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                engine.TerminateExecution();
                                runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", "TerminateExecution"))));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            engine.TerminateExecution();
                            returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", "TerminateExecution")));
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    };
                }
                else if (name == "runStartupFile")
                {
                    if (arguments.Length >= 0)
                    {
                        var callback = (arguments.Length == 1 ? arguments[0] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();

                                var message = "V8 Script Engine : Started";
                                Log.Trace(message, configuration.Verbose);
                                runner.PostTask(new CallbackTask(context, callback, JsonMapper.ToJson(new StatusHelper("OK", message))));

                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            var message = "V8 Script Engine : Started";
                            Log.Trace(message, configuration.Verbose);
                            returnValue = CefV8Value.CreateString(JsonMapper.ToJson(new StatusHelper("OK", message)));
                        }
                    }
                    else
                    {
                        returnValue = DontMeetRequirements(context, null);
                    };
                }
                return(true);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }
コード例 #5
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var runner  = CefTaskRunner.GetForCurrentThread();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name == "create")
                {
                    var address = "";
                    var port    = 0;
                    var threads = 1;

                    if (arguments.Length >= 1)
                    {
                        address = arguments[0].GetStringValue();
                    }
                    if (arguments.Length >= 2)
                    {
                        port = arguments[1].GetIntValue();
                    }
                    if (arguments.Length >= 3)
                    {
                        threads = arguments[2].GetIntValue();
                    }

                    var handler = CefV8Value.CreateObject();
                    handler.SetValue("Address", CefV8Value.CreateString(address), CefV8PropertyAttribute.None);
                    handler.SetValue("Port", CefV8Value.CreateInt(port), CefV8PropertyAttribute.None);
                    handler.SetValue("Threads", CefV8Value.CreateInt(threads), CefV8PropertyAttribute.None);

                    handler.SetValue("onClientConnected", CefV8Value.CreateFunction("onClientConnected", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onClientDisconnected", CefV8Value.CreateFunction("onClientDisconnected", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onHttpRequest", CefV8Value.CreateFunction("onHttpRequest", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onServerCreated", CefV8Value.CreateFunction("onServerCreated", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onServerDestroyed", CefV8Value.CreateFunction("onServerDestroyed", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onWebSocketConnected", CefV8Value.CreateFunction("onWebSocketConnected", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onWebSocketMessage", CefV8Value.CreateFunction("onWebSocketMessage", this), CefV8PropertyAttribute.None);
                    handler.SetValue("onWebSocketRequest", CefV8Value.CreateFunction("onWebSocketRequest", this), CefV8PropertyAttribute.None);

                    handler.SetValue("sendHttp200Response", CefV8Value.CreateFunction("sendHttp200Response", this), CefV8PropertyAttribute.None);
                    handler.SetValue("sendHttp404Response", CefV8Value.CreateFunction("sendHttp404Response", this), CefV8PropertyAttribute.None);
                    handler.SetValue("sendHttp500Response", CefV8Value.CreateFunction("sendHttp500Response", this), CefV8PropertyAttribute.None);
                    handler.SetValue("sendHttpResponse", CefV8Value.CreateFunction("sendHttpResponse", this), CefV8PropertyAttribute.None);
                    handler.SetValue("sendRawData", CefV8Value.CreateFunction("sendRawData", this), CefV8PropertyAttribute.None);
                    handler.SetValue("sendWebSocketMessage", CefV8Value.CreateFunction("sendWebSocketMessage", this), CefV8PropertyAttribute.None);

                    handler.SetValue("connect", CefV8Value.CreateFunction("connect", this), CefV8PropertyAttribute.None);

                    if (this.parent != null && !this.parent.Servers.Any(s => s.Address == address && s.Port == port && s.Threads == threads))
                    {
                        this.parent.Servers.Add(new ServerHandler(address, port, threads));
                    }
                    returnValue = handler;
                }
                else if (name == "connect")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null)
                        {
                            serverHandler.Connect();
                            if (arguments.Length >= 1 && arguments[0].IsFunction)
                            {
                                new Task(() =>
                                {
                                    context.Enter();
                                    runner.PostTask(new CallbackTask(context, arguments[0]));
                                    context.Exit();
                                }).Start();
                            }
                        }
                    }
                }
                else if (name == "onClientConnected" ||
                         name == "onClientDisconnected" ||
                         name == "onHttpRequest" ||
                         name == "onServerCreated" ||
                         name == "onServerDestroyed" ||
                         name == "onWebSocketConnected" ||
                         name == "onWebSocketMessage" ||
                         name == "onWebSocketRequest")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 1 && arguments[0].IsFunction)
                        {
                            serverHandler.AddEventListener(name, new WebCallback(context, runner, arguments[0]));
                        }
                    }
                }
                else if (name == "sendHttp200Response")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 3)
                        {
                            var id          = arguments[0].GetIntValue();
                            var contentType = arguments[1].GetStringValue();
                            var data        = Encoding.UTF8.GetBytes(arguments[2].GetStringValue());
                            serverHandler.sendHttp200Response(id, contentType, data);
                        }
                    }
                }
                else if (name == "sendHttp404Response")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 1)
                        {
                            var id = arguments[0].GetIntValue();
                            serverHandler.sendHttp404Response(id);
                        }
                    }
                }
                else if (name == "sendHttp500Response")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 2)
                        {
                            var id    = arguments[0].GetIntValue();
                            var error = arguments[1].GetStringValue();
                            serverHandler.sendHttp500Response(id, error);
                        }
                    }
                }
                else if (name == "sendHttpResponse")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 5)
                        {
                            var id            = arguments[0].GetIntValue();
                            var responseCode  = arguments[1].GetIntValue();
                            var contentType   = arguments[2].GetStringValue();
                            var contentLength = arguments[3].GetIntValue();
                            var headers       = arguments[4].GetStringValue();
                            serverHandler.sendHttpResponse(id, responseCode, contentType, contentLength, headers);
                        }
                    }
                }
                else if (name == "sendRawData")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 2)
                        {
                            var id   = arguments[0].GetIntValue();
                            var data = Encoding.UTF8.GetBytes(arguments[1].GetStringValue());
                            serverHandler.sendRawData(id, data);
                        }
                    }
                }
                else if (name == "sendWebSocketMessage")
                {
                    var dic = GetSettings(obj, arguments);
                    if (this.parent != null && dic["Address"] != null && dic["Port"] != null && dic["Threads"] != null && !string.IsNullOrEmpty(dic["Address"].ToString()) && (int)dic["Port"] > 0 && (int)dic["Threads"] > 0)
                    {
                        var serverHandler = this.parent.Servers.SingleOrDefault(s => !string.IsNullOrEmpty(s.Address) && s.Address == dic["Address"].ToString() && s.Port == (int)dic["Port"] && s.Threads == (int)dic["Threads"]);
                        if (serverHandler != null && arguments.Length >= 2)
                        {
                            var id   = arguments[0].GetIntValue();
                            var data = Encoding.UTF8.GetBytes(arguments[1].GetStringValue());
                            serverHandler.sendWebSocketMessage(id, data);
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }
コード例 #6
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                var className  = name.Substring(0, name.LastIndexOf('.'));
                var methodName = name.Substring(name.LastIndexOf('.') + 1);

                if (!string.IsNullOrEmpty(className) && !string.IsNullOrEmpty(methodName))
                {
                    var type = GetClassType(className);
                    if (type != null)
                    {
                        var ret = false;
                        foreach (var method in type.GetMethods())
                        {
                            if (method.Name == methodName && validTypes.Contains(method.ReturnType))
                            {
                                var parameters = method.GetParameters();
                                var pc         = parameters.Where(ss => validTypes.Contains(ss.ParameterType)).Count();
                                if (parameters.Length == pc && arguments.Length >= parameters.Length)
                                {
                                    var        args     = new List <object>();
                                    CefV8Value callback = null;
                                    if (parameters.Length + 1 == arguments.Length)
                                    {
                                        callback = arguments[arguments.Length - 1];
                                    }

                                    for (int x = 0; x < parameters.Length; x++)
                                    {
                                        var a = Extension.ConvertToValue(arguments[x]);
                                        var p = parameters[x];
                                        if (a == null || (a != null && a.GetType() == p.ParameterType))
                                        {
                                            args.Add(a);
                                        }
                                    }

                                    if (method.ReturnType != typeof(void))
                                    {
                                        if (callback != null && callback.IsFunction)
                                        {
                                            ret = true;
                                            var runner = CefTaskRunner.GetForCurrentThread();
                                            new Task(() =>
                                            {
                                                context.Enter();
                                                var value = method.Invoke(this, args.ToArray());
                                                if (runner != null)
                                                {
                                                    runner.PostTask(new CallbackTask(context, callback, value));
                                                }
                                                context.Exit();
                                            }).Start();
                                        }
                                        else
                                        {
                                            returnValue = InvokeMethod(method, args, method.ReturnType);
                                            if (returnValue != null)
                                            {
                                                ret = true;
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        return(ret);
                    }
                    else
                    {
                        type = GetClassType(name);
                        if (type != null)
                        {
                            var args     = new List <object>();
                            var argTypes = new List <Type>();
                            for (int x = 0; x < arguments.Length; x++)
                            {
                                var result = arguments[x];
                                if (result.IsBool)
                                {
                                    args.Add(result.GetBoolValue());
                                    argTypes.Add(typeof(bool));
                                }
                                else if (result.IsDate)
                                {
                                    args.Add(result.GetDateValue());
                                    argTypes.Add(typeof(DateTime));
                                }
                                else if (result.IsInt)
                                {
                                    args.Add(result.GetIntValue());
                                    argTypes.Add(typeof(int));
                                }
                                else if (result.IsDouble)
                                {
                                    args.Add(result.GetDoubleValue());
                                    argTypes.Add(typeof(double));
                                }
                                else if (result.IsNull || result.IsUndefined)
                                {
                                    args.Add(null);
                                    argTypes.Add(null);
                                }
                                else if (result.IsString)
                                {
                                    args.Add(result.GetStringValue());
                                    argTypes.Add(typeof(string));
                                }
                                else if (result.IsUInt)
                                {
                                    args.Add(result.GetUIntValue());
                                    argTypes.Add(typeof(uint));
                                }
                                else if (result.IsArray)
                                {
                                    var values = new List <dynamic>();
                                    for (int i = 0; i < result.GetArrayLength(); i++)
                                    {
                                        var value = result.GetValue(i);
                                        values.Add(Extension.ConvertToV8Value(value));
                                    }
                                    args.Add(values);
                                    argTypes.Add(typeof(Array));
                                }
                            }

                            ConstructorInfo constructor = null;
                            foreach (var constr in type.GetConstructors())
                            {
                                var parameters = constr.GetParameters().Where(ss => validTypes.Contains(ss.ParameterType)).ToList();
                                if (parameters.Count == argTypes.Count)
                                {
                                    constructor = type.GetConstructor(argTypes.ToArray());
                                    if (constructor == null)
                                    {
                                        for (int x = 0; x < parameters.Count; x++)
                                        {
                                            var a = argTypes[x];
                                            var p = parameters[x];
                                            var v = args[x];

                                            if (p.ParameterType != a)
                                            {
                                                argTypes[x] = p.ParameterType;
                                                if (v != null)
                                                {
                                                    if (p.ParameterType == typeof(char[]) && a == typeof(string))
                                                    {
                                                        args[x] = ((string)v).ToCharArray();
                                                    }
                                                    else
                                                    {
                                                        args[x] = Convert.ChangeType(v, p.ParameterType);
                                                    }
                                                }
                                            }
                                        }
                                        constructor = type.GetConstructor(argTypes.ToArray());
                                        if (constructor != null)
                                        {
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }

                            returnValue = InvokeContructor(constructor, args, type);
                            if (returnValue != null)
                            {
                                return(true);
                            }
                        }
                    }
                }
                return(false);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }
コード例 #7
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name.ToLower().Equals("back"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            back();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        back();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("copy"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            copy();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        copy();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("cut"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            cut();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        cut();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("delete"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            delete();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        delete();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("find"))
                {
                    if (arguments.Length >= 5)
                    {
                        var identifier = arguments[0].GetIntValue();
                        var searchText = arguments[1].GetStringValue();
                        var forward    = arguments[2].GetBoolValue();
                        var matchCase  = arguments[3].GetBoolValue();
                        var findNext   = arguments[4].GetBoolValue();
                        var callback   = (arguments.Length == 6 ? arguments[5] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                find(identifier, searchText, forward, matchCase, findNext);
                                runner.PostTask(new CallbackTask(context, callback, null));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            find(identifier, searchText, forward, matchCase, findNext);
                        }
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("forward"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            forward();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        forward();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("getsource"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            getSource();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        getSource();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("getzoomlevel"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            runner.PostTask(new CallbackTask(context, callback, getZoomLevel()));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        returnValue = CefV8Value.CreateDouble(getZoomLevel());
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("load"))
                {
                    if (arguments.Length >= 1)
                    {
                        var url      = arguments[0].GetStringValue();
                        var callback = (arguments.Length == 2 ? arguments[1] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                load(url);
                                runner.PostTask(new CallbackTask(context, callback, null));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            load(url);
                        }
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("loadstring"))
                {
                    if (arguments.Length >= 2)
                    {
                        var html     = arguments[0].GetStringValue();
                        var url      = arguments[1].GetStringValue();
                        var callback = (arguments.Length == 3 ? arguments[2] : null);

                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                loadString(html, url);
                                runner.PostTask(new CallbackTask(context, callback, null));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            loadString(html, url);
                        }
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("paste"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            paste();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        paste();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("print"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            print();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        print();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("redo"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            redo();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        redo();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("refresh"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            refresh();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        refresh();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("selectall"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            selectAll();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        selectAll();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("showdevtools"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            showDevTools();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        showDevTools();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("stop"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            stop();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        stop();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("undo"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            undo();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        undo();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("viewsource"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            viewSource();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        viewSource();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("shutdown"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            shutdown();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        shutdown();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("printtopdf"))
                {
                    if (arguments.Length >= 1)
                    {
                        var path      = arguments[0].GetStringValue();
                        var settings  = new CefPdfPrintSettings();
                        var _callback = new CefPdfClient();

                        var callback = (arguments.Length == 2 ? arguments[1] : null);
                        if (callback != null && callback.IsFunction)
                        {
                            var runner = CefTaskRunner.GetForCurrentThread();
                            new Task(() =>
                            {
                                context.Enter();
                                printToPDF(path, settings, null);
                                runner.PostTask(new CallbackTask(context, callback, _callback));
                                context.Exit();
                            }).Start();
                        }
                        else
                        {
                            printToPDF(path, settings, _callback);
                        }
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("getwindowsize"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            getWindowSize();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        getWindowSize();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("update"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            Update();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        Update();
                    }
                    return(true);
                }
                else if (name.ToLower().Equals("checkforupdates"))
                {
                    var callback = (arguments.Length == 1 ? arguments[0] : null);
                    if (callback != null && callback.IsFunction)
                    {
                        var runner = CefTaskRunner.GetForCurrentThread();
                        new Task(() =>
                        {
                            context.Enter();
                            CheckForUpdates();
                            runner.PostTask(new CallbackTask(context, callback, null));
                            context.Exit();
                        }).Start();
                    }
                    else
                    {
                        CheckForUpdates();
                    }
                    return(true);
                }
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }

            return(false);
        }
コード例 #8
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

            try
            {
                var context = CefV8Context.GetCurrentContext();
                var runner  = CefTaskRunner.GetForCurrentThread();
                var browser = context.GetBrowser();
                var frame   = browser.GetMainFrame();

                if (name == "Protocols") // && arguments.Length == 1
                {
                    //CefV8Value callback = arguments[0];
                    var Handler = CefV8Value.CreateObject();

                    //if (callback.IsFunction)
                    {
                        if (protocol != null && protocol.Keys.Contains("domains"))
                        {
                            foreach (JsonData _domain in protocol["domains"])
                            {
                                if (_domain.Keys.Contains("domain"))
                                {
                                    var DomainHandler = CefV8Value.CreateObject();

                                    var domainName = _domain["domain"].ToString();

                                    if (_domain.Keys.Contains("commands"))
                                    {
                                        foreach (JsonData command in _domain["commands"])
                                        {
                                            var commandName = command["name"].ToString();
                                            DomainHandler.SetValue(commandName, CefV8Value.CreateFunction(domainName + "." + commandName, this), CefV8PropertyAttribute.None);
                                        }
                                    }

                                    if (_domain.Keys.Contains("events"))
                                    {
                                        foreach (JsonData @event in _domain["events"])
                                        {
                                            var eventName = @event["name"].ToString();
                                            DomainHandler.SetValue(eventName, CefV8Value.CreateFunction(domainName + "." + eventName, this), CefV8PropertyAttribute.None);
                                        }
                                    }

                                    if (_domain.Keys.Contains("types"))
                                    {
                                        foreach (JsonData @type in _domain["types"])
                                        {
                                            var typeName = @type["id"].ToString();
                                            DomainHandler.SetValue(typeName, CefV8Value.CreateFunction(domainName + "." + typeName, this), CefV8PropertyAttribute.None);
                                        }
                                    }

                                    Handler.SetValue(domainName, DomainHandler, CefV8PropertyAttribute.None);
                                }
                            }
                        }

                        //new Task(() =>
                        //{
                        //    context.Enter();
                        //    runner.PostTask(new CallbackTask(context, callback));
                        //    context.Exit();
                        //}).Start();
                    }
                    returnValue = Handler;
                }
                else
                {
                    if (frame != null)
                    {
                        var sock = this.DevToolsSockets.FirstOrDefault(s => s.Url == frame.Url);
                        if (sock != null)
                        {
                            string     @params  = "{}";
                            CefV8Value callback = null;
                            if (arguments.Length >= 1)
                            {
                                if (arguments[0].IsFunction)
                                {
                                    callback = arguments[0];
                                }
                                else
                                {
                                    @params = Extension.CefV8ValueToJson(arguments[0]);
                                    if (arguments.Length >= 2 && arguments[1].IsFunction)
                                    {
                                        callback = arguments[1];
                                    }
                                }
                            }

                            WebCallback webSocketCallback = null;
                            if (callback != null && callback.IsFunction)
                            {
                                webSocketCallback = new WebCallback(context, runner, callback);
                            }

                            sock.Query(name, @params, webSocketCallback);
                        }
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                returnValue = CefV8Value.CreateNull();
                exception   = ex.Message;
                return(true);
            }
        }