protected override void Execute()
            {
                var cachedFuncInfo = JavaScriptObjectRepository.RenderSideFunctions.SingleOrDefault(x => x.Id == functionId);

                if (cachedFuncInfo == null)
                {
                    return;
                }

                var func = cachedFuncInfo.Function;

                var context = cachedFuncInfo.Context;

                var msg = BridgeMessage.Create(EVALUATE_JS_CALLBACK);

                msg.Arguments.Add(MessageValue.CreateInt(taskId));

                context.Enter();

                CefV8Value[] argumentList = null;
                CefV8Value   result       = null;

                try
                {
                    argumentList = arguments.ToCefV8Arguments();
                    result       = func.ExecuteFunctionWithContext(context, null, argumentList);
                }
                finally
                {
                    if (argumentList != null)
                    {
                        foreach (var value in argumentList)
                        {
                            value.Dispose();
                        }
                    }

                    context.Exit();
                }

                if (result != null)
                {
                    var jsvalue = result.ToJSValue();
                    msg.Arguments.Add(MessageValue.CreateBool(true));
                    msg.Arguments.Add(MessageValue.CreateString(jsvalue.ToDefinition()));

                    result.Dispose();
                }
                else
                {
                    msg.Arguments.Add(MessageValue.CreateBool(false));
                    msg.Arguments.Add(MessageValue.CreateString("Executing function failed."));
                }

                jsbridge.SendProcessMessage(frame, msg);
            }
Ejemplo n.º 2
0
        public static void Set(CefV8Context context, XrayObject target, CefListValue args)
        {
            CefV8Value thisArg = GetSafeThisArg(context, target);
            CefV8Value value   = CastCefValueToCefV8Value(context, args.GetValue(4), out bool isNotXray);

            thisArg.SetValueByKey(args.GetString(3), value, CefV8PropertyAttribute.None);
            if (isNotXray)
            {
                value.Dispose();
            }
        }
Ejemplo n.º 3
0
        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();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private object GetPropertyInternal(XrayHandle self, string name)
        {
            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => GetPropertyInternal(self, name)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return(callTask.GetResult());
                }
            }


            XrayObject target = self.GetTarget(this.Frame);

            if (!target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            object retval;

            try
            {
                CefV8Value value = target.Value.GetValueByKey(name);
                retval = CastCefV8ValueToDotnetType(target.Context, value, out bool isxray);
                if (!isxray)
                {
                    value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(retval);
        }
Ejemplo n.º 5
0
        private bool SetPropertyInternal(XrayHandle self, string name, object value)
        {
            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => SetPropertyInternal(self, name, value)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((bool)callTask.GetResult());
                }
            }

            XrayObject target = self.GetTarget(this.Frame);

            if (target is null || !target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            bool result;

            try
            {
                CefV8Value v8value = CastDotnetTypeToCefV8Value(target.Context, value, out bool isNotXray);
                result = target.Value.SetValueByKey(name, v8value, CefV8PropertyAttribute.None);
                if (isNotXray)
                {
                    v8value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(result);
        }
Ejemplo n.º 6
0
        private void Dispose(bool disposing)
        {
            CefV8Value value = _value;

            if (value != null)
            {
                _value = null;
                value.Dispose();

                List <XrayObject> values;
                lock (XrayRoots)
                {
                    XrayRoots.TryGetValue(_context, out values);
                }
                if (values == null)
                {
                    return;
                }
                lock (values)
                {
                    values.Remove(this);
                }
            }
        }
Ejemplo n.º 7
0
        public object InvokeMemberInternal(XrayHandle self, string name, object[] args)
        {
            if (args is null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => InvokeMemberInternal(self, name, args)))
                {
                    if (!CefApi.PostTask(CefThreadId.Renderer, callTask))
                    {
                        throw new InvalidOperationException();
                    }
                    return((bool)callTask.GetResult());
                }
            }

            XrayObject target = self.GetTarget(this.Frame);

            if (target is null || !target.Context.Enter())
            {
                throw new InvalidOperationException();
            }

            object retval;

            try
            {
                CefV8Value thisArg = target.Value;
                CefV8Value func    = thisArg.GetValueByKey(name);
                if (!func.IsFunction)
                {
                    func.Dispose();
                    throw new MissingMethodException(string.Format("'{0} is not a function.'", name));
                }

                CefV8Value value;
                var        xraylist = new List <int>(args.Length);
                var        fnArgs   = new CefV8Value[args.Length];
                try
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        fnArgs[i] = CastDotnetTypeToCefV8Value(target.Context, args[i], out bool isNew);
                        if (!isNew)
                        {
                            xraylist.Add(i);
                        }
                    }
                    value = func.ExecuteFunction(thisArg, fnArgs);
                }
                finally
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        if (!xraylist.Contains(i))
                        {
                            fnArgs[i].Dispose();
                        }
                    }
                }
                retval = CastCefV8ValueToDotnetType(target.Context, value, out bool isxray);
                if (!isxray)
                {
                    value.Dispose();
                }
            }
            finally
            {
                target.Context.Exit();
            }
            return(retval);
        }
Ejemplo n.º 8
0
 public void Dispose()
 {
     _CefV8Value.Dispose();
 }
Ejemplo n.º 9
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;
            }
        }
Ejemplo n.º 10
0
 public void Dispose()
 {
     resolve.Dispose();
     reject.Dispose();
     context.Dispose();
 }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
 public void Dispose()
 {
     function.Dispose();
     context.Dispose();
 }