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);
        }
        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);
        }
Beispiel #3
0
        public void SetNativeValue(object nativeObject, CefV8Value cefObject)
        {
            var fieldValue = CefNativeValueConverter.ToNative(
                cefObject.GetValue(MemberName),
                _fieldInfo.FieldType);

            _fieldInfo.SetValue(nativeObject, fieldValue);
        }
        public void SetNativeValue(object nativeObject, CefV8Value cefObject)
        {
            var propertyValue = CefNativeValueConverter.ToNative(
                cefObject.GetValue(MemberName),
                _propertyInfo.PropertyType);

            _propertyInfo.SetValue(nativeObject, propertyValue, null);
        }
Beispiel #5
0
 public static Task <CefV8Value> InvokeAsync(this CefV8Value @this, string functionname, IWebView context, params CefV8Value[] args)
 {
     return(context.EvaluateAsync(() =>
     {
         var fn = @this.GetValue(functionname);
         if ((fn == null) || !fn.IsFunction)
         {
             return CefV8Value.CreateUndefined();
         }
         return fn.ExecuteFunction(@this, args);
     }
                                  ));
 }
Beispiel #6
0
 public dynamic ConvertToV8Value(CefV8Value result)
 {
     if (result != null)
     {
         if (result.IsBool)
         {
             return(result.GetBoolValue());
         }
         else if (result.IsDate)
         {
             return(result.GetDateValue());
         }
         else if (result.IsDouble)
         {
             return(result.GetDoubleValue());
         }
         else if (result.IsInt)
         {
             return(result.GetIntValue());
         }
         else if (result.IsNull || result.IsUndefined)
         {
             return(null);
         }
         else if (result.IsString)
         {
             return(result.GetStringValue());
         }
         else if (result.IsUInt)
         {
             return(result.GetUIntValue());
         }
         else if (result.IsArray)
         {
             var values = new List <dynamic>();
             for (int i = 0; i < result.GetArrayLength(); i++)
             {
                 var value = result.GetValue(i);
                 values.Add(ConvertToV8Value(value));
             }
             return(values);
         }
         else if (result.IsFunction)
         {
         }
         else if (result.IsObject)
         {
         }
     }
     return(null);
 }
Beispiel #7
0
        private static IEnumerable <CefV8Value> GetElements(this CefV8Value @this)
        {
            if ([email protected])
            {
                throw new ArgumentException("Method valid only for array");
            }

            int count = @this.GetArrayLength();

            for (int i = 0; i < count; i++)
            {
                yield return(@this.GetValue(i));
            }
        }
Beispiel #8
0
 private static CefV8Value Execute(this CefV8Value @this, string functionname, params CefV8Value[] args)
 {
     try
     {
         var fn = @this.GetValue(functionname);
         if ((fn == null) || !fn.IsFunction)
         {
             return(CefV8Value.CreateUndefined());
         }
         return(fn.ExecuteFunction(@this, args));
     }
     catch (Exception)
     {
         return(CefV8Value.CreateUndefined());
     }
 }
        public static long Get(CefV8Context context, XrayObject target, CefListValue args, out CefV8Value value)
        {
            CefV8Value   thisArg   = GetSafeThisArg(context, target);
            CefValue     arg3      = args.GetValue(3);
            CefValueType valueType = arg3.Type;

            if (valueType == CefValueType.Int)
            {
                value = thisArg.GetValueByIndex(arg3.GetInt());
                return(0);
            }

            string name = arg3.GetString();

            value = thisArg.GetValue(name);
            return(0);
        }
Beispiel #10
0
            static CefListValue ToListValue(CefV8Value value, CompositeDisposable disposable)
            {
                var result = CefListValue.Create();
                var count  = value.GetArrayLength();

                result.SetSize(count);
                for (int i = 0; i < count; i++)
                {
                    var val = value.GetValue(i);
                    if (val != null)
                    {
                        if (val.IsBool)
                        {
                            result.SetBool(i, val.GetBoolValue());
                        }
                        else if (val.IsInt)
                        {
                            result.SetInt(i, val.GetIntValue());
                        }
                        else if (val.IsDouble)
                        {
                            result.SetDouble(i, val.GetDoubleValue());
                        }
                        else if (val.IsString)
                        {
                            result.SetString(i, val.GetStringValue());
                        }
                        else if (val.IsNull)
                        {
                            result.SetNull(i);
                        }
                        else if (val.IsArray)
                        {
                            result.SetList(i, ToListValue(val, disposable));
                        }
                        else if (val.IsObject)
                        {
                            result.SetDictionary(i, ToDictionaryValue(val, disposable));
                        }
                    }
                }
                disposable.Add(result);
                return(result);
            }
        public static JToken ToJToken(CefV8Value cefV8Value)
        {
            if (!cefV8Value.IsNull && !cefV8Value.IsUndefined)
            {
                if (cefV8Value.IsBool)
                {
                    return(new JValue(cefV8Value.GetBoolValue()));
                }
                // CEF doesn't have a long data type so they are treated as doubles
                // To avoid overflow scenarios, IsDouble must be checked before IsInt because flags will be true
                if (cefV8Value.IsDouble || cefV8Value.IsInt || cefV8Value.IsUInt)
                {
                    return(new JValue(cefV8Value.GetDoubleValue()));
                }

                if (cefV8Value.IsString)
                {
                    return(new JValue(cefV8Value.GetStringValue()));
                }

                if (cefV8Value.IsDate)
                {
                    return(new JValue(cefV8Value.GetDateValue()));
                }
                if (cefV8Value.IsArray)
                {
                    var array         = new JArray();
                    var v8ArrayLength = cefV8Value.GetArrayLength();
                    for (var v8ArrayIndex = 0; v8ArrayIndex < v8ArrayLength; ++v8ArrayIndex)
                    {
                        var elementToken = ToJToken(cefV8Value.GetValue(v8ArrayIndex));
                        array.Add(elementToken);
                    }
                    return(array);
                }
                if (cefV8Value.IsObject)
                {
                    return(JObjectToWrappedJToken(cefV8Value));
                }
            }
            return(new JValue((object)null));
        }
        internal static CefV8Value InvokeMember(CefV8Context context, XrayObject target, CefListValue args)
        {
            CefV8Value thisArg = GetSafeThisArg(context, target);
            CefV8Value func    = thisArg.GetValue(args.GetString(3));

            if (!func.IsFunction)
            {
                func.Dispose();
                throw new MissingMethodException();
            }

            int size     = (int)(args.GetSize() - 4);
            var xraylist = new List <int>(size);
            var fnArgs   = new CefV8Value[size];

            try
            {
                for (int i = 0; i < fnArgs.Length; i++)
                {
                    int index = (i + 4);
                    fnArgs[i] = CastCefValueToCefV8Value(context, args.GetValue(index), out bool isNew);
                    if (!isNew)
                    {
                        xraylist.Add(index);
                    }
                }
                return(func.ExecuteFunction(thisArg, fnArgs));
            }
            finally
            {
                func.Dispose();
                for (int i = 0; i < fnArgs.Length; i++)
                {
                    if (!xraylist.Contains(i))
                    {
                        fnArgs[i].Dispose();
                    }
                }
            }
        }
Beispiel #13
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);
            }
Beispiel #14
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;
            }
        }
        public static object ToNative(CefV8Value obj, Type targetType)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            object retVal = null;

            if ((targetType == typeof(JObject) && obj.IsObject) ||
                (targetType == typeof(JArray) && obj.IsArray) ||
                targetType == typeof(JValue) ||
                targetType == typeof(JToken))
            {
                retVal = CefJsonValueConverter.ToJToken(obj);
            }
            else if (!obj.IsNull && !obj.IsUndefined)
            {
                if (obj.IsBool)
                {
                    retVal = obj.GetBoolValue();
                }
                else if (obj.IsDouble || obj.IsInt || obj.IsUInt)
                {
                    // CEF doesn't have a long data type so they are treated as doubles
                    // To avoid overflow scenarios, IsDouble must be checked before IsInt because flags will be true
                    retVal = obj.GetDoubleValue();
                }
                else if (obj.IsString)
                {
                    retVal = obj.GetStringValue();
                }
                else if (obj.IsDate)
                {
                    retVal = obj.GetDateValue();
                }
                else if (obj.IsArray)
                {
                    if (targetType.IsArray)
                    {
                        if (targetType.GetArrayRank() != 1)
                        {
                            throw new Exception("Cannot handle multidimensional arrays");
                        }

                        var v8ArrayLength = obj.GetArrayLength();
                        var elementType   = targetType.GetElementType();
                        var array         = Array.CreateInstance(elementType, v8ArrayLength);
                        for (var v8ArrayIndex = 0; v8ArrayIndex < v8ArrayLength; ++v8ArrayIndex)
                        {
                            var elementToken = ToNative(obj.GetValue(v8ArrayIndex), elementType);
                            array.SetValue(elementToken, v8ArrayIndex);
                        }

                        retVal = array;
                    }
                }
                else if (obj.IsObject)
                {
                    var pluginAdapter = V8PluginAdapter.FromCefObject(obj);
                    if (pluginAdapter != null)
                    {
                        throw new Exception("Passing plugins as parameters not yet supported");
                    }

                    var converter = GetTypeConverter(targetType);
                    if (converter != null)
                    {
                        retVal = converter.ToNative(obj);
                    }
                }
                else if (obj.IsFunction)
                {
                    // TODO : Throw an exception?
                }

                if (retVal != null && !targetType.IsInstanceOfType(retVal))
                {
                    retVal = Convert.ChangeType(retVal, targetType);
                }
            }

            return(retVal);
        }
Beispiel #16
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();
            }));
        }
Beispiel #17
0
        public static JavaScriptValue ToJSValue(this CefV8Value source)
        {
            if (source == null || !source.IsValid)
            {
                throw new ArgumentNullException();
            }

            JavaScriptValue target;

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

                var info = new JavaScriptRenderSideFunction(context, source);

                JavaScriptObjectRepository.RenderSideFunctions.Add(info);

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


            return(target);
        }
        private 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)");
        }
 public IJavascriptObject GetValue(string ivalue)
 {
     return(Convert(_CefV8Value.GetValue(ivalue)));
 }
Beispiel #20
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;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #21
0
        private static object InvokeScript(CefV8Context context, string memberName, params object[] args)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (!context.Enter())
            {
                throw new CefException("Failed to enter V8 context.");
            }
            try
            {
                // TODO: this list can be private list of context, 'cause we can invoke only one function at one time
                List <CefV8Value> proxies = new List <CefV8Value>(16);

                // javascript 'this' object
                CefV8Value obj = null;

                CefV8Value target = context.GetGlobal();
                proxies.Add(target);
                if (!memberName.Contains('.'))
                {
                    obj    = target;
                    target = obj.GetValue(memberName);
                    proxies.Add(target);
                }
                else
                {
                    foreach (var member in memberName.Split('.'))
                    {
                        obj    = target;
                        target = obj.GetValue(member); // TODO: do analysis of target - if it is not an object - throw
                        if (!target.IsObject)
                        {
                            throw new CefException("Argument 'memberName' must be member access expression to a function. Invalid object in path.");
                        }
                        proxies.Add(target);
                    }
                }
                if (!target.IsFunction)
                {
                    throw new ArgumentException("Argument 'memberName' must be member access expression to a function.");
                }

                CefV8Value[] v8Args;

                if (args.Length == 0)
                {
                    v8Args = null;
                }
                else
                {
                    v8Args = new CefV8Value[args.Length]; // TODO: InvokeScript core can be optimized by prevent recreating arrays
                    for (var i = 0; i < args.Length; i++)
                    {
                        var value = CefConvert.ToV8Value(args[i]);
                        v8Args[i] = value;
                    }
                }

                var v8RetVal = target.ExecuteFunctionWithContext(context, obj, v8Args);

                // force destroing of proxies, to avoid unneccessary GC load (CLR and V8)
                foreach (var proxy in proxies)
                {
                    proxy.Dispose();
                }

                proxies.Clear();

                // FIXME: not sure if exception CAN be null, this need to be checked
                if (v8RetVal.HasException)
                {
                    var exception = v8RetVal.GetException();
                    throw new JavaScriptException(exception.GetMessage());
                }

                //if (!string.IsNullOrEmpty(exception))


                var result = CefConvert.ToObject(v8RetVal);
                v8RetVal.Dispose();
                return(result);
            }
            finally
            {
                if (!context.Exit())
                {
                    throw new CefException("Failed to exit V8 context.");
                }
            }
        }
Beispiel #22
0
        public static object GetClrValue(CefV8Value v)
        {
            if (v.IsArray)
            {
                int      length = v.GetArrayLength();
                object[] objs   = new object[length];
                for (int i = 0; i < length; i++)
                {
                    var value = v.GetValue(i);
                    objs[i] = GetClrValue(value);
                }
                return(objs);
            }

            if (v.IsBool)
            {
                return(v.GetBoolValue());
            }

            if (v.IsDate)
            {
                return(v.GetDateValue());
            }
            if (v.IsInt)
            {
                return(v.GetIntValue());
            }

            if (v.IsDouble)
            {
                return(v.GetDoubleValue());
            }

            if (v.IsFunction)
            {
                throw new NotSupportedException("IsFunction");
            }


            if (v.IsNull)
            {
                return(null);
            }
            if (v.IsObject)
            {
                //throw new NotSupportedException("IsObject");
                //todo:这里可能存在BUG
                var map = v.GetUserData() as UnmanagedWrapper;
                if (map != null)
                {
                    return(map.ClrObject);
                }
                return(null);
            }
            if (v.IsString)
            {
                return(v.GetStringValue());
            }
            if (v.IsUInt)
            {
                return(v.GetUIntValue());
            }
            if (v.IsUndefined)
            {
                return(null);
            }
            if (v.IsUserCreated)
            {
                throw new NotSupportedException("IsUserCreated");
            }
            throw new NotSupportedException("??");
        }
Beispiel #23
0
 public object this[int index] => _value.GetValue(index).GetValue();
Beispiel #24
0
        /// <summary>
        /// 网页脚本与后台程序交互方法
        /// 提示一:如果 returnValue = null; 则会导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// 提示二:还存在其他的可能,导致导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="obj">对象</param>
        /// <param name="arguments">参数</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="exception">返回异常信息</param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            string     result = string.Empty;
            Object     retObj = null;
            Type       t      = JsObject.GetType();
            MethodInfo mi     = t.GetMethod(name);

            if (mi != null)
            {
                if (arguments.Length > 0)
                {
                    Object[]   param = new Object[arguments.Length];
                    CefV8Value value = null;
                    for (int i = 0, j = arguments.Length; i < j; i++)
                    {
                        value = arguments[i];
                        if (value.IsString)
                        {
                            param[i] = value.GetStringValue();
                        }
                        else if (value.IsInt)
                        {
                            param[i] = value.GetIntValue();
                        }
                        else if (value.IsDouble)
                        {
                            param[i] = value.GetIntValue();
                        }
                        else if (value.IsDouble)
                        {
                            param[i] = value.GetDoubleValue();
                            System.Windows.Forms.MessageBox.Show(param[i].ToString());
                        }
                        else if (value.IsArray)
                        {
                            int      len = value.GetArrayLength();
                            Object[] p2  = new Object[len];
                            for (int k = 0; k < len; k++)
                            {
                                CefV8Value va = value.GetValue(k);
                                if (va.IsString)
                                {
                                    p2[k] = va.GetStringValue();
                                }
                                else if (va.IsInt)
                                {
                                    p2[k] = va.GetIntValue();
                                }
                                else if (va.IsDouble)
                                {
                                    p2[k] = va.GetDoubleValue();
                                }
                            }
                            param[i] = p2;
                        }
                        else if (value.IsBool)
                        {
                            param[i] = value.GetBoolValue();
                        }
                        else if (value.IsNull || value.IsUndefined)
                        {
                            param[i] = null;
                        }
                    }
                    retObj = mi.Invoke(JsObject, param);
                }
                else
                {
                    retObj = mi.Invoke(JsObject, null);
                }
            }
            if (retObj != null)
            {
                result = retObj.ToString();
            }
            returnValue = CefV8Value.CreateString(result);
            exception   = null;
            return(true);
        }
        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);
        }
Beispiel #26
0
        /// <summary>
        /// 网页脚本与后台程序交互方法
        /// 提示一:如果 returnValue = null; 则会导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// 提示二:还存在其他的可能,导致导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="obj">对象</param>
        /// <param name="arguments">参数</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="exception">返回异常信息</param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            //  这里要加 try,不然发生异常直接导致 Render Process 崩溃,问题是遗留下另一个 Process,
            //  窗口关闭时遗留的这个 Process 不会退出

            try
            {
                string     result = string.Empty;
                Object     retObj = null;
                Type       t      = JsObject.GetType();
                MethodInfo mi     = t.GetMethod(name);
                if (mi != null)
                {
                    if (arguments.Length > 0)
                    {
                        Object[]   param = new Object[arguments.Length];
                        CefV8Value value = null;
                        for (int i = 0, j = arguments.Length; i < j; i++)
                        {
                            value = arguments[i];
                            if (value.IsString)
                            {
                                param[i] = value.GetStringValue();
                            }
                            else if (value.IsInt)
                            {
                                param[i] = value.GetIntValue();
                            }
                            else if (value.IsDouble)
                            {
                                param[i] = value.GetDoubleValue();
                            }
                            else if (value.IsArray)
                            {
                                int      len = value.GetArrayLength();
                                Object[] p2  = new Object[len];
                                for (int k = 0; k < len; k++)
                                {
                                    CefV8Value va = value.GetValue(k);
                                    if (va.IsString)
                                    {
                                        p2[k] = va.GetStringValue();
                                    }
                                    else if (va.IsInt)
                                    {
                                        p2[k] = va.GetIntValue();
                                    }
                                    else if (va.IsDouble)
                                    {
                                        p2[k] = va.GetDoubleValue();
                                    }
                                }
                                param[i] = p2;
                            }
                            else if (value.IsBool)
                            {
                                param[i] = value.GetBoolValue();
                            }
                            else if (value.IsNull || value.IsUndefined)
                            {
                                param[i] = null;
                            }
                        }
                        retObj = mi.Invoke(JsObject, param);
                    }
                    else
                    {
                        retObj = mi.Invoke(JsObject, null);
                    }
                }
                if (retObj != null)
                {
                    result = retObj.ToString();
                }
                returnValue = CefV8Value.CreateString(result);
                exception   = null;
                return(true);
            }
            catch (Exception ex)
            {
                returnValue = null;
                exception   = ex.ToString();
                MessageBox.Show(exception);
                return(true);
            }
        }
Beispiel #27
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)");
        }
Beispiel #28
0
 public object this[int index]
 {
     get { return(_value.GetValue(index).GetValue()); }
 }