Example #1
0
        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);
        }
Example #2
0
        public IJavascriptObject CreateArray(IEnumerable <IJavascriptObject> iCollection)
        {
            var col = iCollection.ToList();
            var res = CefV8Value.CreateArray(col.Count);

            col.ForEach((el, i) => res.SetValue(i, el.Convert()));
            return(UpdateObject(res));
        }
Example #3
0
 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());
 }
Example #5
0
        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);
        }
Example #6
0
        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());
        }
        protected CefV8Value ToCefV8Value(IV8PluginRouter router, object result)
        {
            if (result == null)
            {
                return(CefV8Value.CreateNull());
            }

            // VALUES FROM REMOTE PLUGINS
            var remoteResult = result as ResultData;

            if (remoteResult != null)
            {
                switch (remoteResult.DataType)
                {
                case ResultDataType.Scalar:
                    if (remoteResult.Items != null && remoteResult.Items.Count != 0)
                    {
                        return(ToCefV8Value(router, remoteResult.Items[0]));
                    }
                    return(CefV8Value.CreateNull());

                case ResultDataType.Array:
                {
                    var cefArray = CefV8Value.CreateArray(remoteResult.Items.Count);
                    if (remoteResult.Items != null)
                    {
                        for (var resultIndex = 0; resultIndex < remoteResult.Items.Count; ++resultIndex)
                        {
                            var cefValue = ToCefV8Value(router, remoteResult.Items[resultIndex]);
                            cefArray.SetValue(resultIndex, cefValue);
                        }
                    }
                    return(cefArray);
                }

                case ResultDataType.Dictionary:
                {
                    var cefObject = CefV8Value.CreateObject(null);
                    if (remoteResult.Items != null)
                    {
                        foreach (var dictionaryItem in remoteResult.Items)
                        {
                            if (string.IsNullOrEmpty(dictionaryItem.Name))
                            {
                                continue;
                            }
                            var cefValue = ToCefV8Value(router, dictionaryItem);
                            cefObject.SetValue(dictionaryItem.Name, cefValue, CefV8PropertyAttribute.None);
                        }
                    }
                    return(cefObject);
                }
                }
            }
            var resultItem = result as ResultItem;

            if (resultItem != null)
            {
                return(ToCefV8Value(router, (object)resultItem.DynamicPlugin ?? resultItem.PlainData));
            }
            var pluginObjectDescriptor = result as PluginDescriptor;

            if (pluginObjectDescriptor != null)
            {
                return(V8PluginAdapter.CreateRemote(router, _plugin.PluginContext, pluginObjectDescriptor).V8Object);
            }

            // VALUES FROM REMOTE OR LOCAL PLUGINS
            var plainData = result as JToken;

            if (plainData != null)
            {
                return(CefJsonValueConverter.ToCef(plainData));
            }

            // VALUES FROM LOCAL PLUGINS
            var localArray = result as object[];

            if (localArray != null)
            {
                var cefArray = CefV8Value.CreateArray(localArray.Length);
                for (var resultIndex = 0; resultIndex < localArray.Length; ++resultIndex)
                {
                    var cefValue = ToCefV8Value(router, localArray[resultIndex]);
                    cefArray.SetValue(resultIndex, cefValue);
                }
                return(cefArray);
            }
            var localPlugin = result as JavaScriptPlugin;

            if (localPlugin != null)
            {
                return(V8PluginAdapter.CreateLocal(router, _plugin.PluginContext, localPlugin).V8Object);
            }
            if (JavaScriptPlugin.IsDynamicPlugin(result))
            {
                var dynPlugin = JavaScriptPlugin.CreateFromObject(PluginProcess.Renderer, result);
                _plugin.PluginContext.PluginManager.AddLocalPlugin(dynPlugin);
                return(V8PluginAdapter.CreateLocal(router, _plugin.PluginContext, dynPlugin).V8Object);
            }

            // local C# POCO
            return(CefNativeValueConverter.ToCef(result));
        }
Example #8
0
        public IJavascriptObject CreateArray(int size)
        {
            var res = CefV8Value.CreateArray(size);

            return(UpdateObject(res));
        }
        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));
        }
Example #10
0
        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());
        }
 private static ICefV8Value CreateArrayValue(int i)
 {
     return(new CefV8ValueImpl(CefV8Value.CreateArray(i)));
 }
Example #12
0
        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);
        }
        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();
            }
        }
Example #14
0
        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);
            }
        }