public object SendRequest(XrayAction action, XrayHandle thisArg, params object[] args)
        {
            var sqi = new ScriptableRequestInfo();
            CefProcessMessage msg = null;

            try
            {
                msg = new CefProcessMessage(CefNetApplication.XrayRequestKey);
                CefListValue argList = msg.ArgumentList;
                if (!argList.SetSize(3 + args.Length))
                {
                    throw new InvalidOperationException();
                }
                argList.SetInt(0, sqi.RequestId);
                argList.SetInt(1, (int)action);
                argList.SetBinary(2, ValidateXrayHandle(thisArg).ToCfxBinaryValue());
                AppendArgs(argList, 3, args);

                CefFrame frame = this.Frame;
                if (!frame.IsValid || frame.Identifier != _frameId)
                {
                    throw new ObjectDeadException();
                }
                frame.SendProcessMessage(CefProcessId.Renderer, msg);
                sqi.Wait();
                return(sqi.GetResult());
            }
            finally
            {
                msg?.Dispose();
                sqi.Dispose();
            }
        }
Esempio n. 2
0
        //public unsafe string Dump()
        //{
        //	var buffer = new byte[sizeof(XrayHandle)];
        //	fixed(byte* buf = buffer)
        //	{
        //		Marshal.StructureToPtr(this, (IntPtr)buf, false);
        //	}
        //	return string.Join(", ", buffer);
        //}

        public unsafe static XrayHandle FromCfxBinaryValue(CefBinaryValue v)
        {
            var xray = new XrayHandle();

            v.GetData((IntPtr)(void *)&xray, sizeof(XrayHandle), 0);
            return(xray);
        }
 protected XrayHandle ValidateXrayHandle(XrayHandle handle)
 {
     if (_frameId != handle.frame)
     {
         throw new InvalidOperationException();
     }
     return(handle);
 }
Esempio n. 4
0
        public static XrayHandle FromDateTime(DateTime t)
        {
            var h = new XrayHandle();

            h.dataType = XrayDataType.Date;
            h.iRaw     = t.ToBinary();
            return(h);
        }
 public object Invoke(XrayHandle self, params object[] args)
 {
     if (CefNetApplication.ProcessType == ProcessType.Renderer)
     {
         return(InvokeInternal(self, args));
     }
     else
     {
         return(SendRequest(XrayAction.Invoke, self, args));
     }
 }
 public void SetProperty(XrayHandle self, string name, object value)
 {
     if (CefNetApplication.ProcessType == ProcessType.Renderer)
     {
         SetPropertyInternal(self, name, value);
     }
     else
     {
         SendRequest(XrayAction.Set, self, name, value);
     }
 }
 public object GetProperty(XrayHandle self, string name)
 {
     if (CefNetApplication.ProcessType == ProcessType.Renderer)
     {
         return(GetPropertyInternal(self, name));
     }
     else
     {
         return(SendRequest(XrayAction.Get, self, name));
     }
 }
        protected void AppendArgs(CefListValue argList, int start, IEnumerable <object> args)
        {
            foreach (object value in args)
            {
                int index = start;
                start++;

                if (value is V8Undefined)
                {
                    argList.SetBinary(index, new byte[1]);
                    continue;
                }

                if (value is null)
                {
                    argList.SetNull(index);
                    continue;
                }

                switch (value)
                {
                case string v:
                    argList.SetString(index, v);
                    continue;

                case int v:
                    argList.SetInt(index, v);
                    continue;

                case double v:
                    argList.SetDouble(index, v);
                    continue;

                case bool v:
                    argList.SetBool(index, v);
                    continue;

                case DateTime v:
                    argList.SetBinary(index, XrayHandle.FromDateTime(v).ToCfxBinaryValue());
                    continue;

                case XrayHandle v:
                    argList.SetBinary(index, ValidateXrayHandle(v).ToCfxBinaryValue());
                    continue;

                case ScriptableObject v:
                    argList.SetBinary(index, ValidateXrayHandle((XrayHandle)v).ToCfxBinaryValue());
                    continue;
                }

                throw new NotImplementedException("Type: " + value.GetType().Name);
            }
        }
        internal static CefV8Value CastCefValueToCefV8Value(CefV8Context context, CefValue value, out bool isNew)
        {
            isNew = true;

            if (value is null)
            {
                return(CefV8Value.CreateNull());
            }

            if (!value.IsValid)
            {
                throw new InvalidCastException();
            }

            CefValueType valueType = value.Type;

            switch (valueType)
            {
            case CefValueType.String:
                return(new CefV8Value(value.GetString()));

            case CefValueType.Int:
                return(new CefV8Value(value.GetInt()));

            case CefValueType.Bool:
                return(new CefV8Value(value.GetBool()));

            case CefValueType.Null:
                return(CefV8Value.CreateNull());

            case CefValueType.Double:
                return(new CefV8Value(value.GetDouble()));

            case CefValueType.Binary:
                CefBinaryValue v = value.GetBinary();
                if (v.Size == 1)
                {
                    return(CefV8Value.CreateUndefined());
                }

                XrayHandle handle = XrayHandle.FromCfxBinaryValue(v);
                if (handle == XrayHandle.Zero)
                {
                    return(context.GetGlobal());
                }

                isNew = (handle.dataType != XrayDataType.Object && handle.dataType != XrayDataType.Function);
                return(handle.ToCefV8Value(context.Frame));
            }

            throw new NotSupportedException();
        }
Esempio n. 10
0
        public void ReleaseObject(XrayHandle handle)
        {
            if (CefNetApplication.ProcessType == ProcessType.Renderer)
            {
                if (CefApi.CurrentlyOn(CefThreadId.Renderer))
                {
                    handle.Release();
                }
                else
                {
                    CefApi.PostTask(CefThreadId.Renderer, new V8CallTask(handle.Release));
                }
                return;
            }

            CefBinaryValue obj = null;

            CefProcessMessage msg = null;

            try
            {
                if (_frameId != handle.frame)
                {
                    return;
                }
                obj = handle.ToCfxBinaryValue();

                msg = new CefProcessMessage(CefNetApplication.XrayReleaseKey);
                using (CefListValue args = msg.ArgumentList)
                {
                    if (!args.SetSize(1))
                    {
                        return;
                    }
                    args.SetBinary(0, obj);
                }
                _frame.SendProcessMessage(CefProcessId.Renderer, msg);
            }
            finally
            {
                if (msg != null)
                {
                    msg.Dispose();
                }
                if (obj != null)
                {
                    obj.Dispose();
                }
            }
        }
Esempio n. 11
0
 public object InvokeMember(XrayHandle self, string name, params object[] args)
 {
     if (CefNetApplication.ProcessType == ProcessType.Renderer)
     {
         return(InvokeMemberInternal(self, name, args));
     }
     else
     {
         var callArgs = new object[args.Length + 1];
         callArgs[0] = name;
         Array.Copy(args, 0, callArgs, 1, args.Length);
         return(SendRequest(XrayAction.InvokeMember, self, callArgs));
     }
 }
Esempio n. 12
0
        internal static CefV8Value CastDotnetTypeToCefV8Value(CefV8Context context, object value, out bool isNew)
        {
            isNew = true;

            if (value is null)
            {
                return(CefV8Value.CreateNull());
            }

            if (value is V8Undefined)
            {
                return(CefV8Value.CreateUndefined());
            }

            switch (value)
            {
            case string v:
                return(new CefV8Value(v));

            case int v:
                return(new CefV8Value(v));

            case double v:
                return(new CefV8Value(v));

            case bool v:
                return(new CefV8Value(v));

            case DateTime v:
                return(new CefV8Value(v));

            case XrayHandle v:
                isNew = (v.dataType != XrayDataType.Object && v.dataType != XrayDataType.Function);
                return(v.ToCefV8Value(context.Frame));

            case ScriptableObject v:
                XrayHandle hv = (XrayHandle)v;
                isNew = (hv.dataType != XrayDataType.Object && hv.dataType != XrayDataType.Function);
                CefV8Value cv8 = hv.ToCefV8Value(context.Frame);
                GC.KeepAlive(v);
                return(cv8);
            }

            throw new NotImplementedException("Type: " + value.GetType().Name);
        }
Esempio n. 13
0
        private static object CastToDotnetType(CefValue value)
        {
            if (value == null)
            {
                return(null);
            }

            if (!value.IsValid)
            {
                throw new InvalidCastException();
            }

            switch (value.Type)
            {
            case CefValueType.String:
                return(value.GetString());

            case CefValueType.Int:
                return(value.GetInt());

            case CefValueType.Bool:
                return(value.GetBool());

            case CefValueType.Null:
                return(null);

            case CefValueType.Double:
                return(value.GetDouble());

            case CefValueType.Binary:
                CefBinaryValue v = value.GetBinary();
                if (v.Size == 1)
                {
                    return(V8Undefined.Value);
                }
                return(XrayHandle.FromCfxBinaryValue(v).ToObject());
            }

            throw new NotImplementedException();
        }
Esempio n. 14
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);
        }
Esempio n. 15
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);
        }
Esempio n. 16
0
 internal XrayProxy(XrayHandle instance, ScriptableObjectProvider provider)
 {
     _instance       = instance;
     _providerHandle = GCHandle.Alloc(provider, GCHandleType.Normal);
 }
Esempio n. 17
0
        internal unsafe static CefValue CastCefV8ValueToCefValue(CefV8Context context, CefV8Value value, out bool isXray)
        {
            isXray = false;
            if (value == null)
            {
                return(null);
            }

            if (!value.IsValid)
            {
                throw new InvalidCastException();
            }

            CefValue v;

            switch (value.Type)
            {
            case CefV8ValueType.Undefined:
                v = new CefValue();
                v.SetBinary(new byte[1]);
                return(v);

            case CefV8ValueType.Null:
                v = new CefValue();
                v.SetNull();
                return(v);

            case CefV8ValueType.Bool:
                v = new CefValue();
                v.SetBool(value.GetBoolValue());
                return(v);

            case CefV8ValueType.Int:                     // TYPE_INT, TYPE_UINT
            case CefV8ValueType.UInt:
                v = new CefValue();
                v.SetInt(value.GetIntValue());
                return(v);

            case CefV8ValueType.Double:
                v = new CefValue();
                v.SetDouble(value.GetDoubleValue());
                return(v);

            case CefV8ValueType.Date:
                v = new CefValue();
                v.SetBinary(XrayHandle.FromDateTime(value.GetDateValue().ToDateTime()).ToCfxBinaryValue());
                return(v);

            case CefV8ValueType.String:
                v = new CefValue();
                if (!value.CopyV8StringToCefValue(v))
                {
                    throw new CefRuntimeException("Can't copy the string.");
                }
                return(v);

            case CefV8ValueType.Object:
                isXray = true;
                if (value.IsArray)                         //TYPE_OBJECT (array)
                {
                    throw new NotImplementedException();
                }
                if (value.IsArrayBuffer)                         //TYPE_OBJECT (arraybuffer)
                {
                    throw new NotImplementedException();
                }
                v = new CefValue();
                v.SetBinary(XrayObject.Wrap(context, value).CreateHandle().ToCfxBinaryValue());
                return(v);
            }
            throw new NotImplementedException();
        }
Esempio n. 18
0
        private object InvokeInternal(XrayHandle self, object[] args)
        {
            if (args is null || args.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(args));
            }

            if (!CefApi.CurrentlyOn(CefThreadId.Renderer))
            {
                using (var callTask = new V8CallTask(() => InvokeInternal(self, 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 func    = target.Value;
                CefV8Value thisArg = CastDotnetTypeToCefV8Value(target.Context, args[0], out bool isNewThisArg);
                CefV8Value value;

                int size     = args.Length - 1;
                var xraylist = new List <int>(size);
                var fnArgs   = new CefV8Value[size];
                try
                {
                    for (int i = 0; i < fnArgs.Length; i++)
                    {
                        int index = (i + 1);
                        fnArgs[i] = CastDotnetTypeToCefV8Value(target.Context, args[index], out bool isNew);
                        if (!isNew)
                        {
                            xraylist.Add(index);
                        }
                    }
                    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);
        }
Esempio n. 19
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);
        }
Esempio n. 20
0
 internal bool TryGetHandle(out XrayHandle handle)
 {
     handle = _instance;
     return(_providerHandle.IsAllocated);
 }
Esempio n. 21
0
 protected internal ScriptableObject(XrayHandle instance, ScriptableObjectProvider provider)
 {
     _instance = new XrayProxy(instance, provider);
 }