コード例 #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);
        }
コード例 #2
0
 public void Test_GetSimpleValue_Double()
 {
     Test(() =>
     {
         object res = null;
         bool ok    = _CefV8_Converter.GetSimpleValue(CefV8Value.CreateDouble(0.5).Convert(), out res);
         ok.Should().BeTrue();
         res.Should().Be(0.5D);
     });
 }
コード例 #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("??");
 }
コード例 #4
0
 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());
 }
コード例 #5
0
ファイル: CefClient.cs プロジェクト: Sofunny17/GTANetwork
        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);
        }
コード例 #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());
        }
コード例 #7
0
 private CefV8Value InvokeMethod(MethodInfo method, List <object> args, Type type)
 {
     if (method != null)
     {
         if (type == typeof(bool))
         {
             return(CefV8Value.CreateBool((bool)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(DateTime))
         {
             return(CefV8Value.CreateDate((DateTime)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(double))
         {
             return(CefV8Value.CreateDouble((double)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(int))
         {
             return(CefV8Value.CreateInt((int)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(string))
         {
             return(CefV8Value.CreateString((string)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(uint))
         {
             return(CefV8Value.CreateUInt((uint)method.Invoke(this, args.ToArray())));
         }
         else if (type == typeof(string[]) ||
                  type == typeof(int[]) ||
                  type == typeof(double[]) ||
                  type == typeof(DateTime[]) ||
                  type == typeof(bool[]) ||
                  type == typeof(uint[]) ||
                  type == typeof(Enum[]))
         {
             return(Extension.ConvertToV8Value(method.Invoke(this, args.ToArray())));
         }
     }
     return(null);
 }
コード例 #8
0
 private CefV8Value InvokeContructor(ConstructorInfo constructor, List <object> args, Type type)
 {
     if (constructor != null)
     {
         if (type == typeof(bool))
         {
             return(CefV8Value.CreateBool((bool)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(DateTime))
         {
             return(CefV8Value.CreateDate((DateTime)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(double))
         {
             return(CefV8Value.CreateDouble((double)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(int))
         {
             return(CefV8Value.CreateInt((int)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(string))
         {
             return(CefV8Value.CreateString((string)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(uint))
         {
             return(CefV8Value.CreateUInt((uint)constructor.Invoke(args.ToArray())));
         }
         else if (type == typeof(string[]) ||
                  type == typeof(int[]) ||
                  type == typeof(double[]) ||
                  type == typeof(DateTime[]) ||
                  type == typeof(bool[]) ||
                  type == typeof(uint[]) ||
                  type == typeof(Enum[]))
         {
             return(Extension.ConvertToV8Value(constructor.Invoke(args.ToArray())));
         }
     }
     return(null);
 }
コード例 #9
0
        static CefV8_Factory()
        {
            Register <string>(CefV8Value.CreateString);

            Register <Int64>((source) => CefV8Value.CreateDouble((double)source));
            Register <UInt64>((source) => CefV8Value.CreateDouble((double)source));
            Register <float>((source) => CefV8Value.CreateDouble((double)source));

            Register <Int32>(CefV8Value.CreateInt);
            Register <Int16>((source) => CefV8Value.CreateInt((int)source));

            Register <UInt32>(CefV8Value.CreateUInt);
            Register <UInt16>((source) => CefV8Value.CreateUInt((UInt32)source));

            //check two way and convertion back
            Register <char>((source) => CefV8Value.CreateString(new StringBuilder().Append(source).ToString()));
            Register <double>(CefV8Value.CreateDouble);
            Register <decimal>((source) => CefV8Value.CreateDouble((double)source));
            Register <bool>(CefV8Value.CreateBool);
            Register <DateTime>(CefV8Value.CreateDate);
        }
        public 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));
        }
コード例 #11
0
ファイル: V8Deserializer.cs プロジェクト: rpc-bindings/core
        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());
        }
コード例 #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);
        }
コード例 #13
0
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            returnValue = CefV8Value.CreateNull();
            exception   = null;

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

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

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

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

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

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

            return(false);
        }
コード例 #14
0
        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();
            }
        }
コード例 #15
0
 public IJavascriptObject CreateDouble(double value)
 {
     return(new CefV8_JavascriptObject(CefV8Value.CreateDouble(value)));
 }
コード例 #16
0
 private static ICefV8Value CreateDoubleValue(double d)
 {
     return(new CefV8ValueImpl(CefV8Value.CreateDouble(d)));
 }
コード例 #17
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);
            }
        }