Example #1
0
        public IJavascriptObject CreateObject(ObjectObservability objectObservability)
        {
            var readOnly = (int)objectObservability;
            var id       = GetNextId();

            return(_ObjectBuilder.Value.ExecuteFunction(null, new[] { CfrV8Value.CreateInt((int)id), CfrV8Value.CreateInt(readOnly) }).ConvertObject(id));
        }
Example #2
0
 private CfrV8Value CreateV8Value(object value)
 {
     if (value is bool)
     {
         return(CfrV8Value.CreateBool((bool)value));
     }
     else if (value is DateTime)
     {
         return(CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)value)));
     }
     else if (value is double)
     {
         return(CfrV8Value.CreateDouble((double)value));
     }
     else if (value is int)
     {
         return(CfrV8Value.CreateInt((int)value));
     }
     else if (value is uint)
     {
         return(CfrV8Value.CreateUint((uint)value));
     }
     else if (value is string)
     {
         return(CfrV8Value.CreateString((string)value));
     }
     else if (value == null)
     {
         return(CfrV8Value.CreateUndefined());
     }
     else
     {
         throw new ArgumentException($"Type \"{ value.GetType().Name }\" is not supported.");
     }
 }
Example #3
0
        public CfrV8Value GetHostWindowState()
        {
            var windowStateName = Container.WindowState.ToString().ToLower();
            var windowStateCode = (int)Container.WindowState;
            var clientRect      = new RECT();

            User32.GetClientRect(ContainerHandle, ref clientRect);

            var retval      = CfrV8Value.CreateObject(new CfrV8Accessor());
            var windowState = CfrV8Value.CreateObject(new CfrV8Accessor());



            windowState.SetValue("state", CfrV8Value.CreateString(windowStateName), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);
            windowState.SetValue("code", CfrV8Value.CreateInt(windowStateCode), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);

            retval.SetValue("windowState", windowState, CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);

            retval.SetValue("clientWidth", CfrV8Value.CreateInt(clientRect.Width), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);
            retval.SetValue("clientHeight", CfrV8Value.CreateInt(clientRect.Height), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);

            retval.SetValue("width", CfrV8Value.CreateInt(Container.Width), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);
            retval.SetValue("height", CfrV8Value.CreateInt(Container.Height), CfxV8PropertyAttribute.ReadOnly | CfxV8PropertyAttribute.DontDelete);

            return(retval);
        }
Example #4
0
        public void GetFlowPicSize(object func, CfrV8HandlerExecuteEventArgs args)
        {
            var jsObjectAccessor = new CfrV8Accessor();
            var jsObject         = CfrV8Value.CreateObject(jsObjectAccessor);

            jsObject.SetValue("width", CfrV8Value.CreateInt(GlobDef.sz_image.width), CfxV8PropertyAttribute.ReadOnly);
            jsObject.SetValue("height", CfrV8Value.CreateInt(GlobDef.sz_image.height), CfxV8PropertyAttribute.ReadOnly);
            args.SetReturnValue(jsObject);
        }
        public IEnumerable <IJavascriptObject> CreateArrays(int number)
        {
            _ArrayBulkBuilder.Value.ExecuteFunction(null, new[] {
                CfrV8Value.CreateInt((int)_Count),
                CfrV8Value.CreateInt(number),
                _ObjectCreationCallbackFunction.Value
            });
            var results = _ObjectCallback.GetLastArguments();

            return(results.Select(result => result.ConvertBasic(_Count++)));
        }
Example #6
0
        private static CfrV8Value ClrToV8Value(object clrValue)
        {
            var type = clrValue.GetType();

            var typeCode = Type.GetTypeCode(type);

            CfrV8Value value = null;


            switch (typeCode)
            {
            case TypeCode.Boolean:
                value = CfrV8Value.CreateBool((bool)clrValue);
                break;

            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
                value = CfrV8Value.CreateInt((int)clrValue);

                break;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                value = CfrV8Value.CreateUint((uint)clrValue);

                break;

            case TypeCode.Single:
            case TypeCode.Double:
                value = CfrV8Value.CreateDouble((double)clrValue);

                break;

            case TypeCode.Decimal:
                value = CfrV8Value.CreateDouble((double)Convert.ChangeType(clrValue, TypeCode.Double));
                break;

            case TypeCode.DateTime:
                value = CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)clrValue));
                break;

            case TypeCode.Char:
            case TypeCode.String:
                value = CfrV8Value.CreateString((string)clrValue);
                break;
            }

            return(value);
        }
Example #7
0
        public IEnumerable <IJavascriptObject> CreateArrays(int number)
        {
            if (number == 0)
            {
                return(Enumerable.Empty <IJavascriptObject>());
            }

            _ArrayBulkBuilder.Value.ExecuteFunction(null, new[] {
                CfrV8Value.CreateInt((int)_Count),
                CfrV8Value.CreateInt(number),
                _ObjectCreationCallbackFunction.Value
            });
            return(_ObjectCallback.GetLastArguments().Select(ConvertBasic));
        }
Example #8
0
        public IEnumerable <IJavascriptObject> CreateObjects(int readWriteNumber, int readOnlyNumber)
        {
            if (readWriteNumber + readOnlyNumber == 0)
            {
                return(Enumerable.Empty <IJavascriptObject>());
            }

            _ObjectBulkBuilder.Value.ExecuteFunction(null, new[] {
                CfrV8Value.CreateInt((int)_Count),
                CfrV8Value.CreateInt(readWriteNumber),
                CfrV8Value.CreateInt(readOnlyNumber),
                _ObjectCreationCallbackFunction.Value
            });
            return(_ObjectCallback.GetLastArguments().Select(ConvertObject));
        }
Example #9
0
 static ChromiumFxFactory()
 {
     Register <string>(CfrV8Value.CreateString);
     Register <Int64>((source) => CfrV8Value.CreateDouble((double)source));
     Register <UInt64>((source) => CfrV8Value.CreateDouble((double)source));
     Register <float>((source) => CfrV8Value.CreateDouble((double)source));
     Register <Int32>(CfrV8Value.CreateInt);
     Register <Int16>((source) => CfrV8Value.CreateInt((int)source));
     Register <UInt32>(CfrV8Value.CreateUint);
     Register <UInt16>((source) => CfrV8Value.CreateUint((UInt32)source));
     Register <char>((source) => CfrV8Value.CreateString(new StringBuilder().Append(source).ToString()));
     Register <double>(CfrV8Value.CreateDouble);
     Register <decimal>((source) => CfrV8Value.CreateDouble((double)source));
     Register <bool>(CfrV8Value.CreateBool);
     Register <DateTime>((source) => CfrV8Value.CreateDate(CfrTime.FromUniversalTime(source.ToUniversalTime())));
 }
Example #10
0
        public IEnumerable <IJavascriptObject> CreateObjectsFromContructor(int number, IJavascriptObject constructor, params IJavascriptObject[] parameters)
        {
            var args = new List <CfrV8Value> {
                CfrV8Value.CreateInt((int)_Count),
                CfrV8Value.CreateInt(number),
                constructor.Convert(),
                _ObjectCreationCallbackFunction.Value
            };

            args.AddRange(parameters.Convert());

            _ObjectWithConstructorBulkBuilder.Value.ExecuteFunction(null, args.ToArray());
            var results = _ObjectCallback.GetLastArguments();

            return(results.Select(result => result.ConvertObject(_Count++)));
        }
Example #11
0
        public IEnumerable <IJavascriptObject> CreateObjects(ObjectsCreationOption option)
        {
            if (option.TotalNumber == 0)
            {
                return(Enumerable.Empty <IJavascriptObject>());
            }

            _ObjectBulkBuilder.Value.ExecuteFunction(null, new[] {
                CfrV8Value.CreateInt((int)_Count),
                CfrV8Value.CreateInt(option.NoneObservableNumber),
                CfrV8Value.CreateInt(option.ReadOnlyNumber),
                CfrV8Value.CreateInt(option.ObservableNumber),
                CfrV8Value.CreateInt(option.ReadOnlyObservableNumber),
                _ObjectCreationCallbackFunction.Value
            });
            return(_ObjectCallback.GetLastArguments().Select(ConvertObject));
        }
Example #12
0
        public static CfrV8Value GetCfrObject(this JSObject _, object item)
        {
            var nameDict = new Dictionary <string, string>();

            var accessor = new CfrV8Accessor();

            var o = CfrV8Value.CreateObject(accessor);

            var t = item.GetType();

            foreach (var p in t.GetProperties())
            {
                var name = p.Name.Substring(0, 1).ToLower() + p.Name.Substring(1);

                nameDict[name] = p.Name;


                o.SetValue(name, CfxV8AccessControl.Default, CfxV8PropertyAttribute.DontDelete);
            }

            accessor.Get += (s, e) =>
            {
                var name = nameDict[e.Name];

                var p = t.GetProperty(name);

                var value     = p.GetValue(item, null);
                var valueType = value?.GetType();



                if (value == null)
                {
                    e.Retval = CfrV8Value.CreateNull();
                }
                else if (valueType == typeof(string) || valueType == typeof(Guid))
                {
                    e.Retval = CfrV8Value.CreateString((string)value);
                }
                else if (valueType == typeof(int) || valueType == typeof(short) || valueType == typeof(long))
                {
                    e.Retval = CfrV8Value.CreateInt((int)value);
                }
                else if (valueType == typeof(decimal))
                {
                    e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value));
                }
                else if (valueType == typeof(float) || valueType == typeof(double))
                {
                    e.Retval = CfrV8Value.CreateDouble(Convert.ToDouble(value));
                }
                else if (valueType == typeof(bool))
                {
                    e.Retval = CfrV8Value.CreateBool(Convert.ToBoolean(value));
                }
                else if (valueType == typeof(DateTime))
                {
                    e.Retval = CfrV8Value.CreateDate(CfrTime.FromUniversalTime((DateTime)value));
                }
                else
                {
                    e.Retval = CfrV8Value.CreateNull();
                }



                e.SetReturnValue(true);
            };


            return(o);
        }
Example #13
0
        private void FormV8Handler_Execute(object sender, Chromium.Remote.Event.CfrV8HandlerExecuteEventArgs e)
        {
            switch (e.Name)
            {
            case "Minimize":
            {
                HostForm.RequireUIThread(() =>
                    {
                        if (HostForm.WindowState == FormWindowState.Minimized)
                        {
                            HostForm.WindowState = FormWindowState.Normal;
                        }
                        else
                        {
                            HostForm.WindowState = FormWindowState.Minimized;
                        }
                    });
            }
            break;

            case "Maximize":
            {
                HostForm.RequireUIThread(() =>
                    {
                        if (HostForm.WindowState == FormWindowState.Maximized)
                        {
                            HostForm.WindowState = FormWindowState.Normal;
                        }
                        else
                        {
                            HostForm.WindowState = FormWindowState.Maximized;
                        }
                    });
            }
            break;

            case "Restore":
            {
                HostForm.RequireUIThread(() =>
                    {
                        HostForm.WindowState = FormWindowState.Normal;
                    });
            }
            break;

            case "Close":
            {
                if (HostForm != null && !HostForm.IsDisposed)
                {
                    HostForm.RequireUIThread(() =>
                        {
                            HostForm.Close();
                        });
                }
            }
            break;

            case "GetWinActivated":
            {
                if (HostForm != null && !HostForm.IsDisposed)
                {
                    e.SetReturnValue(CfrV8Value.CreateBool(Form.ActiveForm == HostForm));
                }
            }
            break;

            case "GetWinState":
            {
                if (HostForm != null && !HostForm.IsDisposed)
                {
                    var obj          = CfrV8Value.CreateObject(new CfrV8Accessor());
                    var stateString  = "normal";
                    var currentState = 0;

                    if (HostForm.WindowState == FormWindowState.Maximized)
                    {
                        currentState = 2;
                        stateString  = "maximized";
                    }
                    else if (HostForm.WindowState == FormWindowState.Minimized)
                    {
                        currentState = 1;
                        stateString  = "minimized";
                    }

                    obj.SetValue("state", CfrV8Value.CreateInt(currentState), Chromium.CfxV8PropertyAttribute.ReadOnly | Chromium.CfxV8PropertyAttribute.DontDelete);
                    obj.SetValue("stateName", CfrV8Value.CreateString(stateString), Chromium.CfxV8PropertyAttribute.ReadOnly | Chromium.CfxV8PropertyAttribute.DontDelete);
                    obj.SetValue("width", CfrV8Value.CreateInt(HostForm.ClientSize.Width), Chromium.CfxV8PropertyAttribute.ReadOnly | Chromium.CfxV8PropertyAttribute.DontDelete);
                    obj.SetValue("height", CfrV8Value.CreateInt(HostForm.ClientSize.Height), Chromium.CfxV8PropertyAttribute.ReadOnly | Chromium.CfxV8PropertyAttribute.DontDelete);

                    e.SetReturnValue(obj);
                }
            }
            break;
            }
        }
Example #14
0
 public IJavascriptObject CreateInt(int value)
 {
     return(CfrV8Value.CreateInt(value).Convert());
 }
Example #15
0
 void dynamicProperty_PropertyGet(object sender, Chromium.Remote.Event.CfrV8AccessorGetEventArgs e)
 {
     form.LogWriteLine("PropertyGet({0}, {1})", sender, e);
     e.Retval = CfrV8Value.CreateInt(dynamicPropertyValue);
     e.SetReturnValue(true);
 }
        public static CfrV8Value V8Serialize(this object o, HashSet <object> refin = null)
        {
            if (Accessor == null)
            {
                Accessor = new CfrV8Accessor();
            }
            var propattr = CfxV8PropertyAttribute.DontDelete | CfxV8PropertyAttribute.ReadOnly;

            switch (o)
            {
            case null:
                return(CfrV8Value.CreateNull());

            case bool onb:
                return(CfrV8Value.CreateBool(onb));

            case string os:
                return(CfrV8Value.CreateString(os));

            case Enum oenum:
                return(CfrV8Value.CreateString(oenum.ToString()));

            case int oni:
                return(CfrV8Value.CreateInt(oni));

            case sbyte oni:
                return(CfrV8Value.CreateInt(oni));

            case short oni:
                return(CfrV8Value.CreateInt(oni));

            case long oni:
                return(CfrV8Value.CreateInt((int)oni));

            case uint onui:
                return(CfrV8Value.CreateUint(onui));

            case byte onui:
                return(CfrV8Value.CreateUint(onui));

            case ushort onui:
                return(CfrV8Value.CreateUint(onui));

            case double ond:
                return(CfrV8Value.CreateDouble(ond));

            case float onf:
                return(CfrV8Value.CreateDouble(onf));

            case ObjectBuilder oob:
                return(oob.Serialize(refin));

            case RGBAColor ocv:
                var vcolres = CfrV8Value.CreateObject(Accessor);
                vcolres.SetValue("r", CfrV8Value.CreateInt(ocv.Color.R), propattr);
                vcolres.SetValue("g", CfrV8Value.CreateInt(ocv.Color.G), propattr);
                vcolres.SetValue("b", CfrV8Value.CreateInt(ocv.Color.B), propattr);
                vcolres.SetValue("a", CfrV8Value.CreateDouble(ocv.A), propattr);
                vcolres.SetValue("name", CfrV8Value.CreateString("#" + ocv.Color.Name.Remove(0, 2)), propattr);
                return(vcolres);

            case Color4 ocdx:
                var dxcolres = CfrV8Value.CreateObject(Accessor);
                dxcolres.SetValue("r", CfrV8Value.CreateDouble(ocdx.Red * 255), propattr);
                dxcolres.SetValue("g", CfrV8Value.CreateDouble(ocdx.Green * 255), propattr);
                dxcolres.SetValue("b", CfrV8Value.CreateDouble(ocdx.Blue * 255), propattr);
                dxcolres.SetValue("a", CfrV8Value.CreateDouble(ocdx.Alpha), propattr);
                dxcolres.SetValue("name", CfrV8Value.CreateString(ocdx.ToString()), propattr);
                return(dxcolres);

            case TimeSpan ots:
                return(CfrV8Value.CreateDate(new CfrTime()
                {
                    Year = 0,
                    Month = 0,
                    DayOfMonth = ots.Days,
                    DayOfWeek = ots.Days,
                    Hour = ots.Hours,
                    Minute = ots.Minutes,
                    Second = ots.Seconds,
                    Millisecond = ots.Milliseconds
                }));

            case DateTime odt:
                return(CfrV8Value.CreateDate(new CfrTime()
                {
                    Year = odt.Year,
                    Month = odt.Month,
                    DayOfMonth = odt.Day,
                    DayOfWeek = (int)odt.DayOfWeek,
                    Hour = odt.Hour,
                    Minute = odt.Minute,
                    Second = odt.Second,
                    Millisecond = odt.Millisecond
                }));

            case IEnumerable oenum:
                lock (oenum)
                {
                    var reslist = (from object obj in oenum select obj.V8Serialize()).ToList();
                    var res     = CfrV8Value.CreateArray(reslist.Count);
                    for (int i = 0; i < reslist.Count; i++)
                    {
                        res.SetValue(i, reslist[i]);
                    }
                    return(res);
                }

            default:
                var referenced = refin;
                if (referenced != null && referenced.Contains(o))
                {
                    return(CfrV8Value.CreateNull());
                }
                else if (referenced == null)
                {
                    referenced = new HashSet <object> {
                        o
                    };
                }
                else
                {
                    referenced.Add(o);
                }

                var oT = o.GetType();
                if (oT.IsClass || (oT.IsValueType && !oT.IsPrimitive))
                {
                    var reso = CfrV8Value.CreateObject(Accessor);
                    foreach (var prop in oT.GetProperties().Where(p =>
                                                                  p.CanRead &&
                                                                  p.GetMethod.IsPublic &&
                                                                  !p.GetMethod.IsStatic &&
                                                                  !p.PropertyType.IsPointer)
                             )
                    {
                        var cobj = prop.GetValue(o).V8Serialize(referenced);
                        reso.SetValue(prop.Name, cobj, propattr);
                    }
                    foreach (var field in oT.GetFields().Where(f =>
                                                               f.IsPublic &&
                                                               !f.IsStatic &&
                                                               !f.FieldType.IsPointer)
                             )
                    {
                        var cobj = field.GetValue(o).V8Serialize(referenced);
                        reso.SetValue(field.Name, cobj, propattr);
                    }
                    return(reso);
                }
                else
                {
                    return(CfrV8Value.CreateNull());
                }
            }
        }
Example #17
0
 public void GetLiveMaxWidth(object func, CfrV8HandlerExecuteEventArgs args)
 {
     args.SetReturnValue(CfrV8Value.CreateInt(GlobDef.sz_image.width));
 }
Example #18
0
        public IJavascriptObject CreateObject(bool readOnly)
        {
            var id = GetNextId();

            return(_ObjectBuilder.Value.ExecuteFunction(null, new[] { CfrV8Value.CreateInt((int)id), CfrV8Value.CreateBool(readOnly) }).ConvertObject(id));
        }
Example #19
0
 public CfrV8Value GetHostWindowWidth()
 {
     return(CfrV8Value.CreateInt(Container.Width));
 }
Example #20
0
 public CfrV8Value GetHostWindowHeight()
 {
     return(CfrV8Value.CreateInt(Container.Height));
 }