public static CefV8Value ToCefV8Value(this JavaScriptValue source) { switch (source.ValueType) { case JavaScriptValueType.Null: return(CefV8Value.CreateNull()); case JavaScriptValueType.Bool: return(CefV8Value.CreateBool(source.GetBool())); case JavaScriptValueType.Int: return(CefV8Value.CreateInt(source.GetInt())); case JavaScriptValueType.Double: return(CefV8Value.CreateDouble(source.GetDouble())); case JavaScriptValueType.String: return(CefV8Value.CreateString(source.GetString())); case JavaScriptValueType.DateTime: return(CefV8Value.CreateDate(source.GetDateTime())); case JavaScriptValueType.Property: break; case JavaScriptValueType.Function: break; case JavaScriptValueType.Object: var obj = CefV8Value.CreateObject(); foreach (var key in source.Keys) { var retval = source.GetValue(key)?.ToCefV8Value(); if (retval != null) { obj.SetValue(key, retval); } } return(obj); case JavaScriptValueType.Array: var result = new List <CefV8Value>(); for (int i = 0; i < source.ArrayLength; i++) { var retval = source.GetValue(i)?.ToCefV8Value(); if (retval != null) { result.Add(retval); } } var array = CefV8Value.CreateArray(result.Count); for (int i = 0; i < result.Count; i++) { array.SetValue(i, result[i]); } return(array); } return(null); }
// Execute the onFailure JavaScript callback. void ExecuteFailureCallback(int browserId, int contextId, int requestId, int errorCode, string errorMessage) { Helpers.RequireRendererThread(); bool removed = false; var info = GetRequestInfo(browserId, contextId, requestId, true, ref removed); if (info == null) { return; } var context = GetContextByID(contextId); if (context != null && info.FailureCallback != null) { var args = new[] { CefV8Value.CreateInt(errorCode), CefV8Value.CreateString(errorMessage) }; info.FailureCallback.ExecuteFunctionWithContext(context, null, args); } Debug.Assert(removed); info.Dispose(); }
public void Test_GetSimpleValue_Int() { Test(() => { object res = null; bool ok = _CefV8_Converter.GetSimpleValue(CefV8Value.CreateInt(10).Convert(), out res); ok.Should().BeTrue(); res.Should().Be(10); }); }
public virtual void Invoke(IV8PluginRouter router, CefV8Context context, object result, int errorCode, string error) { if (CallbackFunction == null) { throw new ObjectDisposedException("_callback"); } // Have to enter the context in order to be able to create object/array/function/date V8 instances context.Enter(); try { var args = new List <CefV8Value>(); switch (_callbackType) { case V8CallbackType.ParameterCallback: case V8CallbackType.EventListener: { var remoteResult = result as ResultData; var localArray = result as object[]; if (remoteResult != null) { if (remoteResult.Items != null) { args.AddRange(remoteResult.Items.Select(item => ToCefV8Value(router, item))); } } else if (localArray != null) { args.AddRange(localArray.Select(item => ToCefV8Value(router, item))); } break; } case V8CallbackType.FunctionCallback: { args.Add(ToCefV8Value(router, result)); args.Add(CefV8Value.CreateInt(errorCode)); args.Add(CefV8Value.CreateString(error)); break; } } var functionResult = CallbackFunction.ExecuteFunction(null, args.ToArray()); if (functionResult == null && CallbackFunction.HasException) { var exception = CallbackFunction.GetException(); Logger.Error("Error executing callback: ", exception.Message); } } finally { context.Exit(); } }
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception) { var eventName = arguments[0].GetStringValue(); var eventHandler = arguments[1]; int listenerId = eventListeners.AddEventListener(eventName, eventHandler); exception = null !; returnValue = CefV8Value.CreateInt(listenerId); return(true); }
public static CefV8Value ConvertToV8Value(object o) { if (o == null) { return(CefV8Value.CreateUndefined()); } if (o is bool) { return(CefV8Value.CreateBool((bool)o)); } if (o is DateTime) { return(CefV8Value.CreateDate((DateTime)o)); } if (o is double) { return(CefV8Value.CreateDouble((double)o)); } if (o is int) { return(CefV8Value.CreateInt((int)o)); } if (o is string) { return(CefV8Value.CreateString((string)o)); } if (o is uint) { return(CefV8Value.CreateUInt((uint)o)); } if (o is Array) { var a = (Array)o; var rtn = CefV8Value.CreateArray(a.Length); for (int i = 0; i < a.Length; i++) { rtn.SetValue(i, ConvertToV8Value(a.GetValue(i))); } return(rtn); } if (o is System.Collections.IList) { var a = (System.Collections.IList)o; var rtn = CefV8Value.CreateArray(a.Count); for (int i = 0; i < a.Count; i++) { rtn.SetValue(i, ConvertToV8Value(a[i])); } return(rtn); } throw new NotSupportedException("??"); }
public static CefV8Value ConvertToV8Value(dynamic result) { if (result != null) { if (result is bool) { return(CefV8Value.CreateBool(result)); } else if (result is DateTime) { return(CefV8Value.CreateDate(result)); } else if (result is double) { return(CefV8Value.CreateDouble(result)); } else if (result is int) { return(CefV8Value.CreateInt(result)); } else if (result is string) { return(CefV8Value.CreateString(result)); } else if (result is uint) { return(CefV8Value.CreateUInt(result)); } else if (result is Array) { var source = (Array)result; var destination = CefV8Value.CreateArray(source.Length); for (int x = 0; x < source.Length; x++) { destination.SetValue(x, ConvertToV8Value(source.GetValue(x))); } return(destination); } else if (result is System.Collections.IList) { var source = (System.Collections.IList)result; var destination = CefV8Value.CreateArray(source.Count); for (int x = 0; x < source.Count; x++) { destination.SetValue(x, ConvertToV8Value(source[x])); } return(destination); } //else throw new NotSupportedException("V8 Value"); } return(CefV8Value.CreateNull()); }
private void BasicUpdateObject(CefV8Value res, ObjectObservability?objectObservability = null) { if (res == null) { return; } SetAttribute(res, NeutroniumConstants.ObjectId, CefV8Value.CreateUInt(_Count++)); if (!objectObservability.HasValue) { return; } SetAttribute(res, NeutroniumConstants.ReadOnlyFlag, CefV8Value.CreateInt((int)objectObservability.Value)); }
public static CefV8Value CreateValue(object value) { if (value == null) { return(CefV8Value.CreateNull()); } if (value is bool) { return(CefV8Value.CreateBool((bool)value)); } if (value is double) { return(CefV8Value.CreateDouble((double)value)); } if (value is float) { return(CefV8Value.CreateDouble((double)(float)value)); } if (value is int) { return(CefV8Value.CreateInt((int)value)); } var s = value as string; if (s != null) { return(CefV8Value.CreateString(s)); } if (value is uint) { return(CefV8Value.CreateUInt((uint)value)); } var list = value as IList; if (list == null) { return(CefV8Value.CreateUndefined()); } var val = list; var arr = CefV8Value.CreateArray(val.Count); for (var i = 0; i < val.Count; i++) { arr.SetValue(i, CreateValue(val[i])); } return(arr); }
public IEnumerable <IJavascriptObject> CreateObjectsFromConstructor(int number, IJavascriptObject constructor, params IJavascriptObject[] parameters) { var builder = _ObjectWithConstructorBuilder.Value; for (var i = 0; i < number; i++) { var args = new List <CefV8Value>() { CefV8Value.CreateInt((int)_Count++), constructor.Convert() }; var command = builder.ExecuteFunction(null, args.Concat(parameters.Select(p => p.Convert())).ToArray()); yield return(UpdateObject(command)); } }
public static CefV8Value CreateValue(object value) { if (value == null) { return(CefV8Value.CreateNull()); } if (value is bool) { return(CefV8Value.CreateBool((bool)value)); } if (value is double) { return(CefV8Value.CreateDouble((double)value)); } if (value is float) { return(CefV8Value.CreateDouble((double)(float)value)); } if (value is int) { return(CefV8Value.CreateInt((int)value)); } if (value is string) { return(CefV8Value.CreateString((string)value)); } if (value is uint) { return(CefV8Value.CreateUInt((uint)value)); } if (value is IList) { IList val = (IList)value; var arr = CefV8Value.CreateArray(val.Count); for (int i = 0; i < val.Count; i++) { arr.SetValue(i, CreateValue(val[i])); } return(arr); } return(CefV8Value.CreateUndefined()); }
private CefV8Value InvokeContructor(ConstructorInfo constructor, List <object> args, Type type) { if (constructor != null) { if (type == typeof(bool)) { return(CefV8Value.CreateBool((bool)constructor.Invoke(args.ToArray()))); } else if (type == typeof(DateTime)) { return(CefV8Value.CreateDate((DateTime)constructor.Invoke(args.ToArray()))); } else if (type == typeof(double)) { return(CefV8Value.CreateDouble((double)constructor.Invoke(args.ToArray()))); } else if (type == typeof(int)) { return(CefV8Value.CreateInt((int)constructor.Invoke(args.ToArray()))); } else if (type == typeof(string)) { return(CefV8Value.CreateString((string)constructor.Invoke(args.ToArray()))); } else if (type == typeof(uint)) { return(CefV8Value.CreateUInt((uint)constructor.Invoke(args.ToArray()))); } else if (type == typeof(string[]) || type == typeof(int[]) || type == typeof(double[]) || type == typeof(DateTime[]) || type == typeof(bool[]) || type == typeof(uint[]) || type == typeof(Enum[])) { return(Extension.ConvertToV8Value(constructor.Invoke(args.ToArray()))); } } return(null); }
private CefV8Value InvokeMethod(MethodInfo method, List <object> args, Type type) { if (method != null) { if (type == typeof(bool)) { return(CefV8Value.CreateBool((bool)method.Invoke(this, args.ToArray()))); } else if (type == typeof(DateTime)) { return(CefV8Value.CreateDate((DateTime)method.Invoke(this, args.ToArray()))); } else if (type == typeof(double)) { return(CefV8Value.CreateDouble((double)method.Invoke(this, args.ToArray()))); } else if (type == typeof(int)) { return(CefV8Value.CreateInt((int)method.Invoke(this, args.ToArray()))); } else if (type == typeof(string)) { return(CefV8Value.CreateString((string)method.Invoke(this, args.ToArray()))); } else if (type == typeof(uint)) { return(CefV8Value.CreateUInt((uint)method.Invoke(this, args.ToArray()))); } else if (type == typeof(string[]) || type == typeof(int[]) || type == typeof(double[]) || type == typeof(DateTime[]) || type == typeof(bool[]) || type == typeof(uint[]) || type == typeof(Enum[])) { return(Extension.ConvertToV8Value(method.Invoke(this, args.ToArray()))); } } return(null); }
static CefV8_Factory() { Register <string>(CefV8Value.CreateString); Register <Int64>((source) => CefV8Value.CreateDouble((double)source)); Register <UInt64>((source) => CefV8Value.CreateDouble((double)source)); Register <float>((source) => CefV8Value.CreateDouble((double)source)); Register <Int32>(CefV8Value.CreateInt); Register <Int16>((source) => CefV8Value.CreateInt((int)source)); Register <UInt32>(CefV8Value.CreateUInt); Register <UInt16>((source) => CefV8Value.CreateUInt((UInt32)source)); //check two way and convertion back Register <char>((source) => CefV8Value.CreateString(new StringBuilder().Append(source).ToString())); Register <double>(CefV8Value.CreateDouble); Register <decimal>((source) => CefV8Value.CreateDouble((double)source)); Register <bool>(CefV8Value.CreateBool); Register <DateTime>(CefV8Value.CreateDate); }
public IJavascriptObject CreateInt(int value) { return(new CefV8_JavascriptObject(CefV8Value.CreateInt(value))); }
public static CefV8Value ToCef(object nativeObject, Type type) { if (nativeObject == null) { if (type != null && type == typeof(void)) { return(CefV8Value.CreateUndefined()); } return(CefV8Value.CreateNull()); } if (nativeObject is bool) { return(CefV8Value.CreateBool((bool)nativeObject)); } var stringValue = nativeObject as string; if (stringValue != null) { return(CefV8Value.CreateString(stringValue)); } if (nativeObject is int || nativeObject is byte || nativeObject is ushort || nativeObject is sbyte || nativeObject is short || nativeObject is char) { return(CefV8Value.CreateInt(Convert.ToInt32(nativeObject))); } if (nativeObject is double || nativeObject is decimal || nativeObject is long || nativeObject is uint || nativeObject is ulong || nativeObject is float) { return(CefV8Value.CreateDouble(Convert.ToDouble(nativeObject))); } var nativeArray = nativeObject as Array; if (nativeArray != null) { var cefArray = CefV8Value.CreateArray(nativeArray.Length); for (var i = 0; i < nativeArray.Length; i++) { var nativeArrayItem = nativeArray.GetValue(i); var cefArrayItem = ToCef(nativeArrayItem, null); cefArray.SetValue(i, cefArrayItem); } return(cefArray); } var jsonToken = nativeObject as JToken; if (jsonToken != null) { return(CefJsonValueConverter.ToCef(jsonToken)); } if (type == null) { type = nativeObject.GetType(); } var typeConverter = GetTypeConverter(type); if (typeConverter != null) { return(typeConverter.ToCef(nativeObject)); } throw new Exception(string.Format("Cannot convert '{0}' object from CLR to CEF.", type.FullName)); }
public static CefV8Value ToCef(JToken jsonToken) { switch (jsonToken.Type) { case JTokenType.Object: { var jObject = (JObject)jsonToken; var plainObject = CefV8Value.CreateObject(null); foreach (var jsonProperty in jObject.Properties()) { plainObject.SetValue(jsonProperty.Name, ToCef(jsonProperty.Value), CefV8PropertyAttribute.None); } return(plainObject); } case JTokenType.Array: { var jArray = (JArray)jsonToken; var arrayValue = CefV8Value.CreateArray(jArray.Count); for (var i = 0; i < jArray.Count; i++) { arrayValue.SetValue(i, ToCef(jArray[i])); } return(arrayValue); } case JTokenType.Integer: { var jsonValue = (JValue)jsonToken; if (jsonValue.Value is long) { return(CefV8Value.CreateDouble((double)jsonToken)); } return(CefV8Value.CreateInt((int)jsonToken)); } case JTokenType.Float: return(CefV8Value.CreateDouble((double)jsonToken)); case JTokenType.Guid: case JTokenType.Uri: case JTokenType.String: return(CefV8Value.CreateString((string)jsonToken)); case JTokenType.Boolean: return(CefV8Value.CreateBool((bool)jsonToken)); case JTokenType.Null: return(CefV8Value.CreateNull()); case JTokenType.Undefined: return(CefV8Value.CreateUndefined()); case JTokenType.Date: return(CefV8Value.CreateDate((DateTime)jsonToken)); case JTokenType.Raw: case JTokenType.Bytes: case JTokenType.TimeSpan: case JTokenType.Property: case JTokenType.Comment: case JTokenType.Constructor: case JTokenType.None: throw new NotSupportedException(); default: throw new ArgumentOutOfRangeException(); } }
private static ICefV8Value CreateIntValue(int i) { return(new CefV8ValueImpl(CefV8Value.CreateInt(i))); }
public CefV8Value AsV8Value() { CefV8Value result = null; if (this.Value is IList) { var v = (IList)this.Value; result = CefV8Value.CreateArray(v.Count); for (var i = 0; i < v.Count; i++) { result.SetValue(i, new JSValue(v[i]).AsV8Value()); } } else if (this.Value is Boolean) { result = CefV8Value.CreateBool((Boolean)this.Value); } else if (this.Value is DateTime) { result = CefV8Value.CreateDate((DateTime)this.Value); } else if (this.Value is Single || this.Value is Double || this.Value is Decimal || this.Value is UInt64 || this.Value is Int64) { result = CefV8Value.CreateDouble((Double)this.Value); } else if (this.Value is CefV8Handler) { result = CefV8Value.CreateFunction(null, (CefV8Handler)this.Value); } else if (this.Value is SByte || this.Value is Int16 || this.Value is Int32) { result = CefV8Value.CreateInt((Int32)this.Value); } else if (this.Value == null) { result = CefV8Value.CreateNull(); } else if (this.Value is IDictionary) { var v = (IDictionary)this.Value; Debug.Assert(v.Keys.Count == v.Values.Count); var vKeys = new String[v.Keys.Count]; var vValues = new CefV8Value[v.Values.Count]; result = CefV8Value.CreateObject(null); for (var i = 0; i < vKeys.Length; i++) { result.SetValue(vKeys[i], new JSValue(vValues[i]).AsV8Value(), CefV8PropertyAttribute.None); } } else if (this.Value is String) { result = CefV8Value.CreateString((String)this.Value); } else if (this.Value is Byte || this.Value is UInt16 || this.Value is UInt32) { result = CefV8Value.CreateUInt((UInt32)this.Value); } if (result == null) { result = CefV8Value.CreateUndefined(); } return(result); }
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception) { if (name == _config.JSQueryFunction) { if (arguments.Length != 1 || !arguments[0].IsObject) { returnValue = null; exception = "Invalid arguments; expecting a single object"; return(true); } var arg = arguments[0]; var requestVal = arg.GetValue(CefMessageRouter.MemberRequest); if (requestVal == null || !requestVal.IsString) { returnValue = null; exception = "Invalid arguments; object member '" + CefMessageRouter.MemberRequest + "' is required and must " + "have type string"; return(true); } CefV8Value successVal = null; if (arg.HasValue(CefMessageRouter.MemberOnSuccess)) { successVal = arg.GetValue(CefMessageRouter.MemberOnSuccess); if (!successVal.IsFunction) { returnValue = null; exception = "Invalid arguments; object member '" + CefMessageRouter.MemberOnSuccess + "' must have type " + "function"; return(true); } } CefV8Value failureVal = null; if (arg.HasValue(CefMessageRouter.MemberOnFailure)) { failureVal = arg.GetValue(CefMessageRouter.MemberOnFailure); if (!failureVal.IsFunction) { returnValue = null; exception = "Invalid arguments; object member '" + CefMessageRouter.MemberOnFailure + "' must have type " + "function"; return(true); } } CefV8Value persistentVal = null; if (arg.HasValue(CefMessageRouter.MemberPersistent)) { persistentVal = arg.GetValue(CefMessageRouter.MemberPersistent); if (!persistentVal.IsBool) { returnValue = null; exception = "Invalid arguments; object member '" + CefMessageRouter.MemberPersistent + "' must have type " + "boolean"; return(true); } } var context = CefV8Context.GetCurrentContext(); var contextId = GetIDForContext(context); var frameId = context.GetFrame().Identifier; var persistent = (persistentVal != null && persistentVal.GetBoolValue()); var requestId = _router.SendQuery(context.GetBrowser(), frameId, contextId, requestVal.GetStringValue(), persistent, successVal, failureVal); returnValue = CefV8Value.CreateInt(requestId); exception = null; return(true); } else if (name == _config.JSCancelFunction) { if (arguments.Length != 1 || !arguments[0].IsInt) { returnValue = null; exception = "Invalid arguments; expecting a single integer"; return(true); } var result = false; var requestId = arguments[0].GetIntValue(); if (requestId != CefMessageRouter.ReservedId) { CefV8Context context = CefV8Context.GetCurrentContext(); var contextId = GetIDForContext(context); var frameId = context.GetFrame().Identifier; result = _router.SendCancel(context.GetBrowser(), frameId, contextId, requestId); } returnValue = CefV8Value.CreateBool(result); exception = null; return(true); } returnValue = null; exception = null; return(false); }
public object Deserialize(CefValue value, Type targetType, ObjectSerializer objectSerializer) { if (!CanHandle(value, targetType)) { throw new InvalidOperationException(); } var valueType = value.GetValueType(); if (valueType == CefValueType.String) { return(CefV8Value.CreateString(value.GetString())); } if (valueType == CefValueType.Int) { return(CefV8Value.CreateInt(value.GetInt())); } if (valueType == CefValueType.Double) { return(CefV8Value.CreateDouble(value.GetDouble())); } if (value.IsType(CefTypes.Int64)) { return(CefV8Value.CreateDouble(value.GetInt64())); } if (value.IsType(CefTypes.Time)) { return(CefV8Value.CreateDate(value.GetTime())); } if (valueType == CefValueType.Bool) { return(CefV8Value.CreateBool(value.GetBool())); } if (valueType == CefValueType.List) { using (var list = value.GetList()) { if (list.Count > 0) { var array = CefV8Value.CreateArray(list.Count); for (var i = 0; i < list.Count; i++) { using (var cefValue = list.GetValue(i)) { array.SetValue(i, (CefV8Value)objectSerializer.Deserialize(cefValue, typeof(CefV8Value))); } } return(array); } } } if (valueType == CefValueType.Dictionary) { using (var dictionary = value.GetDictionary()) using (var valDict = dictionary.GetDictionary(ObjectSerializer.ValuePropertyName)) { var typeId = dictionary.GetString(ObjectSerializer.TypeIdPropertyName); if (typeId == ObjectSerializer.DictionaryTypeId) { var obj = CefV8Value.CreateObject(); foreach (var key in valDict.GetKeys()) { obj.SetValue(key, (CefV8Value)objectSerializer.Deserialize(valDict.GetValue(key), typeof(CefV8Value))); } return(obj); } else { var deserialized = objectSerializer.Deserialize(value, typeof(object)); if (deserialized is ObjectDescriptor descriptor) { return(new ObjectBinder(descriptor, objectSerializer, functionCallPromiseRegistry).BindToNew()); } } } } return(CefV8Value.CreateNull()); }
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); } }
protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception) { try { if (name == "Log") { var message = arguments[0].GetStringValue(); #if DIAGNOSTICS Cef.Logger.Info(LogTarget.Default, message); #endif returnValue = null; } else if (name == "ReturnVoid") { returnValue = null; } else if (name == "ReturnVoidAndDisposeThis") { returnValue = null; if (obj != null) { obj.Dispose(); obj = null; } } else if (name == "ReturnUndefined") { returnValue = CefV8Value.CreateUndefined(); } else if (name == "ReturnNull") { returnValue = CefV8Value.CreateNull(); } else if (name == "ReturnBool") { returnValue = CefV8Value.CreateBool(true); } else if (name == "ReturnInt") { returnValue = CefV8Value.CreateInt(12345678); } else if (name == "ReturnDouble") { returnValue = CefV8Value.CreateDouble(1234.5678); } else if (name == "ReturnDate") { returnValue = CefV8Value.CreateDate(DateTime.UtcNow); } else if (name == "ReturnString") { returnValue = CefV8Value.CreateString("Some string, passed to CEF!"); } else if (name == "ReturnArray") { var array = CefV8Value.CreateArray(3); array.SetValue(0, CefV8Value.CreateInt(123)); array.SetValue(1, CefV8Value.CreateString("hello!")); array.SetValue(2, CefV8Value.CreateBool(false)); returnValue = array; } else if (name == "ReturnObject") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); returnValue = obj1; } else if (name == "ReturnComplexArray") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var array = CefV8Value.CreateArray(5); array.SetValue(0, CefV8Value.CreateInt(123)); array.SetValue(1, CefV8Value.CreateString("hello!")); array.SetValue(2, CefV8Value.CreateBool(false)); array.SetValue(3, obj1); array.SetValue(4, CefV8Value.CreateString("hello2!")); obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var obj2 = CefV8Value.CreateObject(); obj2.SetValue("i'm still", CefV8Value.CreateString("alive")); obj1.SetValue("inner", obj2); array.SetValue(5, obj1); returnValue = array; } else if (name == "ReturnComplexObject") { var obj1 = CefV8Value.CreateObject(); obj1.SetValue("index", CefV8Value.CreateInt(123)); obj1.SetValue("reply", CefV8Value.CreateString("hello!")); obj1.SetValue("success", CefV8Value.CreateBool(false)); var obj2 = CefV8Value.CreateObject(); obj2.SetValue("i'm still", CefV8Value.CreateString("alive")); obj1.SetValue("inner", obj2); obj2.Dispose(); // force to dispose object wrapper and underlying v8 persistent handle. // note, that obj2 will passed in obj before, but it anyway safe to destroy obj2 handle, // 'cause v8 api internally always open handles. returnValue = obj1; } else if (name == "SubtractIntImplicit") { var a = arguments[0].GetIntValue(); var b = arguments[1].GetIntValue(); returnValue = CefV8Value.CreateInt(a - b); } else if (name == "SubtractIntExplicit") { if (!arguments[0].IsInt) { throw new ArgumentException("arg0"); } var a = arguments[0].GetIntValue(); if (!arguments[1].IsInt) { throw new ArgumentException("arg1"); } var b = arguments[1].GetIntValue(); returnValue = CefV8Value.CreateInt(a - b); } else if (name == "Dump") { returnValue = CefV8Value.CreateString(Dump(arguments)); } else if (name == "get_PrivateWorkingSet") { var result = Process.GetCurrentProcess().PrivateMemorySize64 / (1024.0 * 1024.0); returnValue = CefV8Value.CreateDouble(result); } else if (name == "leakTestV8Func") { var handler = new TestV8Handler(); for (var i = 0; i < 100000; i++) { var x = CefV8Value.CreateFunction("LeakTest", handler); x.Dispose(); } returnValue = CefV8Value.CreateBool(true); } else { returnValue = null; exception = null; return(false); } exception = null; return(true); } catch (Exception ex) { returnValue = null; exception = ex.ToString(); return(true); } }