public object ToNative(CefV8Value cefObject)
        {
            if (cefObject == null)
            {
                throw new ArgumentNullException("cefObject");
            }
            if (!cefObject.IsObject)
            {
                throw new ArgumentOutOfRangeException("cefObject", "Must be an Object");
            }

            var cefValueNames = cefObject.GetKeys();
            var nativeObject  = Activator.CreateInstance(_nativeType);

            foreach (var cefValueName in cefValueNames)
            {
                INativeTypeMemberConverter memberConverter;
                if (_members.TryGetValue(cefValueName, out memberConverter))
                {
                    memberConverter.SetNativeValue(nativeObject, cefObject);
                }
            }

            return(nativeObject);
        }
        private static JToken JObjectToWrappedJToken(CefV8Value cefV8Value)
        {
            var jObjectWrapper = new JObject();
            var jObjectPayload = new JObject();
            var adapter        = V8PluginAdapter.FromCefObject(cefV8Value);

            if (adapter != null)
            {
                // Send JObject that defines a reference to the plugin
                jObjectWrapper.Add("__type__", "reference");
                jObjectPayload.Add("id", adapter.Plugin.Descriptor.PluginId);
                jObjectWrapper.Add("__payload__", jObjectPayload);
            }
            else
            {
                // Not a plugin, serialize all fields to a structure in json
                var keys = cefV8Value.GetKeys();
                foreach (var fieldKey in keys)
                {
                    var value = cefV8Value.GetValue(fieldKey);
                    if (!value.IsFunction)
                    {
                        var fieldToken = ToJToken(value);
                        jObjectWrapper.Add(fieldKey, fieldToken);
                    }
                }
            }

            return(jObjectWrapper);
        }
        private Dictionary <string, object> GetSettings(CefV8Value obj, CefV8Value[] arguments)
        {
            var dic = new Dictionary <string, object>();

            dic.Add("Address", "");
            dic.Add("Port", 0);
            dic.Add("Threads", 0);

            var keys       = obj.GetKeys();
            var addressKey = keys.FirstOrDefault(s => s.ToLower() == "address");

            if (!string.IsNullOrEmpty(addressKey))
            {
                dic["Address"] = obj.GetValue(addressKey).GetStringValue();
            }

            var portKey = keys.FirstOrDefault(s => s.ToLower() == "port");

            if (!string.IsNullOrEmpty(portKey))
            {
                dic["Port"] = obj.GetValue(portKey).GetIntValue();
            }

            var threadsKey = keys.FirstOrDefault(s => s.ToLower() == "threads");

            if (!string.IsNullOrEmpty(threadsKey))
            {
                dic["Threads"] = obj.GetValue(threadsKey).GetIntValue();
            }

            return(dic);
        }
Example #4
0
            static CefDictionaryValue ToDictionaryValue(CefV8Value value, CompositeDisposable disposable)
            {
                var result = CefDictionaryValue.Create();

                foreach (var key in value.GetKeys())
                {
                    var val = value.GetValue(key);
                    if (val.IsBool)
                    {
                        result.SetBool(key, val.GetBoolValue());
                    }
                    else if (val.IsInt)
                    {
                        result.SetInt(key, val.GetIntValue());
                    }
                    else if (val.IsDouble)
                    {
                        result.SetDouble(key, val.GetDoubleValue());
                    }
                    else if (val.IsString)
                    {
                        result.SetString(key, val.GetStringValue());
                    }
                    else if (val.IsNull)
                    {
                        result.SetNull(key);
                    }
                    else if (val.IsArray)
                    {
                        result.SetList(key, ToListValue(val, disposable));
                    }
                    else if (val.IsObject)
                    {
                        result.SetDictionary(key, ToDictionaryValue(val, disposable));
                    }
                }
                disposable.Add(result);
                return(result);
            }
 public IEnumerable <string> GetAttributeKeys()
 {
     return(_CefV8Value.GetKeys());
 }
        private void WriteV8Value(CefV8Value value, StringBuilder result, int indent = 0)
        {
            /*
            var isUndefined = value.IsUndefined;
            var isNull = value.IsNull;
            var isBool = value.IsBool;
            var isInt = value.IsInt;
            var isDouble = value.IsDouble;
            var isDate = value.IsDate;
            var isString = value.IsString;
            var isArray = value.IsArray;
            var isObject = value.IsObject;
            var isFunction = value.IsFunction;

            result.Append("[");
            if (isUndefined) result.Append("undefined ");
            if (isNull) result.Append("null ");
            if (isBool) result.Append("bool ");
            if (isInt) result.Append("int ");
            if (isDouble) result.Append("double ");
            if (isDate) result.Append("date ");
            if (isString) result.Append("string ");
            if (isArray) result.Append("array ");
            if (isObject) result.Append("object ");
            if (isFunction) result.Append("function");
            result.Append("]");
            */

            if (value.IsUndefined)
            {
                result.Append("(undefined)");
            }
            else if (value.IsNull)
            {
                result.Append("(null)");
            }
            else if (value.IsBool)
            {
                result.AppendFormat("(bool) {0}", value.GetBoolValue() ? "true" : "false");
            }
            else if (value.IsInt)
            {
                result.AppendFormat("(int) {0}", value.GetIntValue());
            }
            else if (value.IsDouble)
            {
                result.AppendFormat("(double) {0}", value.GetDoubleValue().ToString(CultureInfo.InvariantCulture.NumberFormat));
            }
            else if (value.IsDate)
            {
                result.AppendFormat("(date) {0}", value.GetDateValue().ToString("s"));
            }
            else if (value.IsString)
            {
                result.AppendFormat("(string) {0}", value.GetStringValue());
            }
            else if (value.IsArray) // for array IsObject also is true
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(array) [");
                var length = value.GetArrayLength();
                for (var i = 0; i < length; i++)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, i);
                    WriteV8Value(value.GetValue(i), result, indent + 1);
                }
                if (length != 0)
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append(']');
            }
            else if (value.IsFunction) // for function IsObject also is true
            {
                var name = value.GetFunctionName();
                var handler = value.GetFunctionHandler();
                var declaration = value.GetStringValue();

                result.Append("(function) ");
                result.Append(!string.IsNullOrEmpty(name) ? name : "(anonymous)");
                if (handler != null)
                {
                    result.Append(" (handler: ");
                    result.Append(handler.ToString());
                    result.Append(")");
                }
                if (!string.IsNullOrEmpty(declaration))
                {
                    result.Append(" = ");
                    result.Append(declaration);
                }
            }
            else if (value.IsObject)
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(object) {");
                var keys = value.GetKeys().AsEnumerable();
                foreach (var key in keys)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, key);
                    WriteV8Value(value.GetValue(key), result, indent + 1);
                }
                if (keys.Any())
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append('}');
            }
            //else result.Append("(unknown)");
        }
Example #7
0
        public JSValue(CefV8Value value)
        {
            if (value == null)
            {
                return;
            }

            if (value.IsArray)
            {
                var result = new List <Object>();
                for (var i = 0; i < value.GetArrayLength(); i++)
                {
                    result.Add(new JSValue(value.GetValue(i)).Value);
                }
                this.Value = result.ToArray();
            }
            else if (value.IsObject)
            {
                var result = new Dictionary <String, Object>();
                var keys   = value.GetKeys();
                for (var i = 0; i < keys.Length; i++)
                {
                    try
                    {
                        result.Add(keys[i], new JSValue(value.GetValue(keys[i])).Value);
                    }
                    catch (ArgumentException)
                    {
                    }
                }
                this.Value = result;
            }
            else if (value.IsInt)
            {
                this.Value = value.GetIntValue();
            }
            else if (value.IsUInt)
            {
                this.Value = value.GetUIntValue();
            }
            else if (value.IsDouble)
            {
                this.Value = value.GetDoubleValue();
            }
            else if (value.IsBool)
            {
                this.Value = value.GetBoolValue();
            }
            else if (value.IsDate)
            {
                this.Value = value.GetDateValue();
            }
            else if (value.IsString)
            {
                this.Value = value.GetStringValue();
            }
            else if (value.IsNull)
            {
                this.Value = null;
            }
            else if (value.IsUndefined)
            {
                this.Value = null;
            }
        }
Example #8
0
        public void CreateObject(CefV8Value handler, Assembly assmebly, string @namespace = "")
        {
            if (assmebly != null)
            {
                foreach (var type in assmebly.GetTypes())
                {
                    if (string.IsNullOrEmpty(@namespace) || (!string.IsNullOrEmpty(@namespace) && type.Namespace == @namespace))
                    {
                        CefV8Value methodHandler = null;
                        var        methods       = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
                        foreach (var method in methods)
                        {
                            if (validTypes.Contains(method.ReturnType))
                            {
                                var parameters = method.GetParameters();
                                var p          = parameters.Where(ss => validTypes.Contains(ss.ParameterType)).ToList();
                                if (parameters.Length == p.Count)
                                {
                                    if (methodHandler == null)
                                    {
                                        methodHandler = CefV8Value.CreateFunction(type.FullName, this);
                                    }
                                    methodHandler.SetValue(method.Name, CefV8Value.CreateFunction(type.FullName + "." + method.Name, this), CefV8PropertyAttribute.None);
                                }
                            }
                        }

                        if (methodHandler != null)
                        {
                            CefV8Value current  = handler;
                            var        sections = type.FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int i = 0; i < sections.Length; i++)
                            {
                                var section = sections[i];
                                if (current.GetKeys().Contains(section))
                                {
                                    if (i + 1 == sections.Length)
                                    {
                                        current.SetValue(section, methodHandler, CefV8PropertyAttribute.None);
                                    }
                                    else
                                    {
                                        current = current.GetValue(section);
                                    }
                                }
                                else
                                {
                                    if (i + 1 == sections.Length)
                                    {
                                        current.SetValue(section, methodHandler, CefV8PropertyAttribute.None);
                                    }
                                    else
                                    {
                                        var temp = CefV8Value.CreateObject();
                                        current.SetValue(section, temp, CefV8PropertyAttribute.None);
                                        current = temp;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Example #9
0
        /// <summary>
        /// Parses the cef v8 value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private JToken ParseCefV8Value(CefV8Value value)
        {
            if (value == null)
            {
                return(null);
            }

            return(CefUtility.RunInContext(Context, () =>
            {
                if (value.IsInt)
                {
                    return JToken.FromObject(value.GetIntValue());
                }

                if (value.IsUInt)
                {
                    return JToken.FromObject(value.GetUIntValue());
                }

                if (value.IsDouble)
                {
                    return JToken.FromObject(value.GetDoubleValue());
                }

                if (value.IsBool)
                {
                    return JToken.FromObject(value.GetBoolValue());
                }

                if (value.IsDate)
                {
                    return JToken.FromObject(value.GetDateValue());
                }

                if (value.IsString)
                {
                    return JToken.FromObject(value.GetStringValue());
                }

                if (value.IsUndefined)
                {
                    return JValue.CreateUndefined();
                }

                if (value.IsArray)
                {
                    var array = new JArray();

                    for (var i = 0; i < value.GetArrayLength(); i++)
                    {
                        array.Add(ParseCefV8Value(value.GetValue(i)));
                    }

                    return array;
                }

                if (value.IsObject)
                {
                    var obj = new JObject();

                    foreach (var propertyName in value.GetKeys())
                    {
                        obj.Add(propertyName, ParseCefV8Value(value.GetValue(propertyName)));
                    }

                    return obj;
                }

                return JValue.CreateNull();
            }));
        }
Example #10
0
        public static JavaScriptValue ToJSValue(this CefV8Value source)
        {
            if (source == null || !source.IsValid)
            {
                throw new ArgumentNullException();
            }

            JavaScriptValue target;

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

                var info = new JavaScriptRenderSideFunction(context, source);

                JavaScriptObjectRepository.RenderSideFunctions.Add(info);

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


            return(target);
        }
Example #11
0
        public static dynamic CefV8ValueToJson(CefV8Value result)
        {
            if (result != null)
            {
                if (result.IsObject)
                {
                    string item = "{";
                    var    keys = result.GetKeys();
                    for (int x = 0; x < keys.Length; x++)
                    {
                        var key   = keys[x];
                        var value = result.GetValue(key);

                        item += "\"" + key + "\" : ";
                        if (value.IsArray)
                        {
                            item += "[";
                            for (int i = 0; i < value.GetArrayLength(); i++)
                            {
                                item += CefV8ValueToJson(value.GetValue(i));
                            }
                            item += "]";
                        }
                        else
                        {
                            if (value.IsBool)
                            {
                                item += value.GetBoolValue();
                            }
                            else if (value.IsDouble)
                            {
                                item += value.GetDoubleValue();
                            }
                            else if (value.IsInt)
                            {
                                item += value.GetIntValue();
                            }
                            else if (value.IsUInt)
                            {
                                item += value.GetUIntValue();
                            }
                            else if (value.IsString)
                            {
                                item += "\"" + value.GetStringValue() + "\"";
                            }
                            else if (value.IsDate)
                            {
                                item += "\"" + value.GetDateValue() + "\"";
                            }
                            else if (value.IsNull || value.IsUndefined)
                            {
                                item += "";
                            }
                            else
                            {
                                item += "";
                            }
                        }
                    }
                    item += "}";
                    return(item);
                }
                else if (result.IsArray)
                {
                    string items = "[";
                    for (int i = 0; i < result.GetArrayLength(); i++)
                    {
                        items += CefV8ValueToJson(result.GetValue(i));
                    }
                    items += "]";
                    return(items);
                }
            }
            return(null);
        }
Example #12
0
        private void WriteV8Value(CefV8Value value, StringBuilder result, int indent = 0)
        {
            /*
             * var isUndefined = value.IsUndefined;
             * var isNull = value.IsNull;
             * var isBool = value.IsBool;
             * var isInt = value.IsInt;
             * var isDouble = value.IsDouble;
             * var isDate = value.IsDate;
             * var isString = value.IsString;
             * var isArray = value.IsArray;
             * var isObject = value.IsObject;
             * var isFunction = value.IsFunction;
             *
             * result.Append("[");
             * if (isUndefined) result.Append("undefined ");
             * if (isNull) result.Append("null ");
             * if (isBool) result.Append("bool ");
             * if (isInt) result.Append("int ");
             * if (isDouble) result.Append("double ");
             * if (isDate) result.Append("date ");
             * if (isString) result.Append("string ");
             * if (isArray) result.Append("array ");
             * if (isObject) result.Append("object ");
             * if (isFunction) result.Append("function");
             * result.Append("]");
             */

            if (value.IsUndefined)
            {
                result.Append("(undefined)");
            }
            else if (value.IsNull)
            {
                result.Append("(null)");
            }
            else if (value.IsBool)
            {
                result.AppendFormat("(bool) {0}", value.GetBoolValue() ? "true" : "false");
            }
            else if (value.IsInt)
            {
                result.AppendFormat("(int) {0}", value.GetIntValue());
            }
            else if (value.IsDouble)
            {
                result.AppendFormat("(double) {0}", value.GetDoubleValue().ToString(CultureInfo.InvariantCulture.NumberFormat));
            }
            else if (value.IsDate)
            {
                result.AppendFormat("(date) {0}", value.GetDateValue().ToString("s"));
            }
            else if (value.IsString)
            {
                result.AppendFormat("(string) {0}", value.GetStringValue());
            }
            else if (value.IsArray) // for array IsObject also is true
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(array) [");
                var length = value.GetArrayLength();
                for (var i = 0; i < length; i++)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, i);
                    WriteV8Value(value.GetValue(i), result, indent + 1);
                }
                if (length != 0)
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append(']');
            }
            else if (value.IsFunction) // for function IsObject also is true
            {
                var name        = value.GetFunctionName();
                var handler     = value.GetFunctionHandler();
                var declaration = value.GetStringValue();

                result.Append("(function) ");
                result.Append(!string.IsNullOrEmpty(name) ? name : "(anonymous)");
                if (handler != null)
                {
                    result.Append(" (handler: ");
                    result.Append(handler.ToString());
                    result.Append(")");
                }
                if (!string.IsNullOrEmpty(declaration))
                {
                    result.Append(" = ");
                    result.Append(declaration);
                }
            }
            else if (value.IsObject)
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(object) {");
                var keys = value.GetKeys().AsEnumerable();
                foreach (var key in keys)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, key);
                    WriteV8Value(value.GetValue(key), result, indent + 1);
                }
                if (keys.Any())
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append('}');
            }
            //else result.Append("(unknown)");
        }