Example #1
0
        public static object GetValue(this CefV8Value val)
        {
            if (val.IsNull || val.IsUndefined)
            {
                return(null);
            }

            if (val.IsArray)
            {
                return(new V8Array(val));
            }
            if (val.IsBool)
            {
                return(val.GetBoolValue());
            }
            if (val.IsDouble)
            {
                return(val.GetDoubleValue());
            }
            if (val.IsInt)
            {
                return(val.GetIntValue());
            }
            if (val.IsString)
            {
                return(val.GetStringValue());
            }
            if (val.IsUInt)
            {
                return(val.GetUIntValue());
            }

            return(null);
        }
Example #2
0
        public bool GetSimpleValue(IJavascriptObject ijsvalue, out object res, Type iTargetType = null)
        {
            res = null;
            CefV8Value value = CefV8_JavascriptObject.Convert(ijsvalue);

            if ((value.IsUndefined) || (value.IsNull))
            {
                return(true);
            }

            if (value.IsString)
            {
                res = ijsvalue.GetStringValue();
                return(true);
            }

            if (value.IsBool)
            {
                res = value.GetBoolValue();
                return(true);
            }

            if (IsUnsigned(iTargetType))
            {
                if (value.IsUInt)
                {
                    res = value.GetUIntValue();
                }
            }
            else
            {
                if (value.IsInt)
                {
                    res = value.GetIntValue();
                }
            }

            if ((res == null) && (value.IsDouble))
            {
                res = value.GetDoubleValue();
            }

            if (res != null)
            {
                if (iTargetType != null)
                {
                    res = Convert.ChangeType(res, iTargetType);
                }

                return(true);
            }

            if (value.IsDate)
            {
                res = value.GetDateValue();
                return(true);
            }

            return(false);
        }
Example #3
0
 public static object CefV8ValueConvert(CefV8Value value)
 {
     if (value == null)
     {
         return(null);
     }
     else if (value.IsBool)
     {
         return(value.GetBoolValue());
     }
     else if (value.IsDate)
     {
         return(value.GetDateValue());
     }
     else if (value.IsDouble)
     {
         return(value.GetDoubleValue());
     }
     else if (value.IsInt)
     {
         return(value.GetIntValue());
     }
     else if (value.IsString)
     {
         return(value.GetStringValue());
     }
     return(null);
 }
Example #4
0
 public dynamic ConvertToV8Value(CefV8Value result)
 {
     if (result != null)
     {
         if (result.IsBool)
         {
             return(result.GetBoolValue());
         }
         else if (result.IsDate)
         {
             return(result.GetDateValue());
         }
         else if (result.IsDouble)
         {
             return(result.GetDoubleValue());
         }
         else if (result.IsInt)
         {
             return(result.GetIntValue());
         }
         else if (result.IsNull || result.IsUndefined)
         {
             return(null);
         }
         else if (result.IsString)
         {
             return(result.GetStringValue());
         }
         else if (result.IsUInt)
         {
             return(result.GetUIntValue());
         }
         else if (result.IsArray)
         {
             var values = new List <dynamic>();
             for (int i = 0; i < result.GetArrayLength(); i++)
             {
                 var value = result.GetValue(i);
                 values.Add(ConvertToV8Value(value));
             }
             return(values);
         }
         else if (result.IsFunction)
         {
         }
         else if (result.IsObject)
         {
         }
     }
     return(null);
 }
        internal unsafe static object CastCefV8ValueToDotnetType(CefV8Context context, CefV8Value value, out bool isXray)
        {
            isXray = false;
            if (value == null)
            {
                return(null);
            }

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

            switch (value.Type)
            {
            case CefV8ValueType.Undefined:
                return(V8Undefined.Value);

            case CefV8ValueType.Null:
                return(null);

            case CefV8ValueType.Bool:
                return(value.GetBoolValue());

            case CefV8ValueType.Int:                     // TYPE_INT, TYPE_UINT
            case CefV8ValueType.UInt:
                return(value.GetIntValue());

            case CefV8ValueType.Double:
                return(value.GetDoubleValue());

            case CefV8ValueType.Date:
                return(value.GetDateValue().ToDateTime());

            case CefV8ValueType.String:
                return(value.GetStringValue());

            case CefV8ValueType.Object:
                isXray = true;
                if (value.IsArray)                         //TYPE_OBJECT (array)
                {
                    throw new NotImplementedException();
                }
                if (value.IsArrayBuffer)                         //TYPE_OBJECT (arraybuffer)
                {
                    throw new NotImplementedException();
                }
                return(XrayObject.Wrap(context, value).CreateHandle());
            }
            throw new NotImplementedException();
        }
        public static JToken ToJToken(CefV8Value cefV8Value)
        {
            if (!cefV8Value.IsNull && !cefV8Value.IsUndefined)
            {
                if (cefV8Value.IsBool)
                {
                    return(new JValue(cefV8Value.GetBoolValue()));
                }
                // CEF doesn't have a long data type so they are treated as doubles
                // To avoid overflow scenarios, IsDouble must be checked before IsInt because flags will be true
                if (cefV8Value.IsDouble || cefV8Value.IsInt || cefV8Value.IsUInt)
                {
                    return(new JValue(cefV8Value.GetDoubleValue()));
                }

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

                if (cefV8Value.IsDate)
                {
                    return(new JValue(cefV8Value.GetDateValue()));
                }
                if (cefV8Value.IsArray)
                {
                    var array         = new JArray();
                    var v8ArrayLength = cefV8Value.GetArrayLength();
                    for (var v8ArrayIndex = 0; v8ArrayIndex < v8ArrayLength; ++v8ArrayIndex)
                    {
                        var elementToken = ToJToken(cefV8Value.GetValue(v8ArrayIndex));
                        array.Add(elementToken);
                    }
                    return(array);
                }
                if (cefV8Value.IsObject)
                {
                    return(JObjectToWrappedJToken(cefV8Value));
                }
            }
            return(new JValue((object)null));
        }
 public bool GetBoolValue()
 {
     return(_CefV8Value.GetBoolValue());
 }
        private void WriteV8Value(CefV8Value value, StringBuilder result, int indent = 0)
        {
            /*
            var isUndefined = value.IsUndefined;
            var isNull = value.IsNull;
            var isBool = value.IsBool;
            var isInt = value.IsInt;
            var isDouble = value.IsDouble;
            var isDate = value.IsDate;
            var isString = value.IsString;
            var isArray = value.IsArray;
            var isObject = value.IsObject;
            var isFunction = value.IsFunction;

            result.Append("[");
            if (isUndefined) result.Append("undefined ");
            if (isNull) result.Append("null ");
            if (isBool) result.Append("bool ");
            if (isInt) result.Append("int ");
            if (isDouble) result.Append("double ");
            if (isDate) result.Append("date ");
            if (isString) result.Append("string ");
            if (isArray) result.Append("array ");
            if (isObject) result.Append("object ");
            if (isFunction) result.Append("function");
            result.Append("]");
            */

            if (value.IsUndefined)
            {
                result.Append("(undefined)");
            }
            else if (value.IsNull)
            {
                result.Append("(null)");
            }
            else if (value.IsBool)
            {
                result.AppendFormat("(bool) {0}", value.GetBoolValue() ? "true" : "false");
            }
            else if (value.IsInt)
            {
                result.AppendFormat("(int) {0}", value.GetIntValue());
            }
            else if (value.IsDouble)
            {
                result.AppendFormat("(double) {0}", value.GetDoubleValue().ToString(CultureInfo.InvariantCulture.NumberFormat));
            }
            else if (value.IsDate)
            {
                result.AppendFormat("(date) {0}", value.GetDateValue().ToString("s"));
            }
            else if (value.IsString)
            {
                result.AppendFormat("(string) {0}", value.GetStringValue());
            }
            else if (value.IsArray) // for array IsObject also is true
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(array) [");
                var length = value.GetArrayLength();
                for (var i = 0; i < length; i++)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, i);
                    WriteV8Value(value.GetValue(i), result, indent + 1);
                }
                if (length != 0)
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append(']');
            }
            else if (value.IsFunction) // for function IsObject also is true
            {
                var name = value.GetFunctionName();
                var handler = value.GetFunctionHandler();
                var declaration = value.GetStringValue();

                result.Append("(function) ");
                result.Append(!string.IsNullOrEmpty(name) ? name : "(anonymous)");
                if (handler != null)
                {
                    result.Append(" (handler: ");
                    result.Append(handler.ToString());
                    result.Append(")");
                }
                if (!string.IsNullOrEmpty(declaration))
                {
                    result.Append(" = ");
                    result.Append(declaration);
                }
            }
            else if (value.IsObject)
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(object) {");
                var keys = value.GetKeys().AsEnumerable();
                foreach (var key in keys)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, key);
                    WriteV8Value(value.GetValue(key), result, indent + 1);
                }
                if (keys.Any())
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append('}');
            }
            //else result.Append("(unknown)");
        }
        public static object ToNative(CefV8Value obj, Type targetType)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            object retVal = null;

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

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

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

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

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

            return(retVal);
        }
Example #10
0
        protected override bool OnProcessMessageReceived(CefBrowser browser, CefProcessId sourceProcess, CefProcessMessage message)
        {
            string[] items = message.Name.Split(new char[] { '|' });
            if (items.Length == 0)
            {
                return(false);
            }

            switch (items[0])
            {
            case "GetElementValue":
            {
                string elementID = items[1];
                long[] frameIDs  = browser.GetFrameIdentifiers();
                foreach (long frameID in frameIDs)
                {
                    CefFrame frame = browser.GetFrame(frameID);
                    if (frame == null)
                    {
                        continue;
                    }
                    frame.VisitDom(new CwbDOMVisitor(browser, CwbBusinStyle.bsGetElementValue, elementID));
                }
                browser.GetMainFrame().VisitDom(new CwbDOMVisitor(browser, CwbBusinStyle.bsGetElementValue, elementID));
                return(true);
            }

            case "SetElementValue":
            {
                string elementID    = items[1];
                string elementValue = items[2];
                long[] frameIDs     = browser.GetFrameIdentifiers();
                foreach (long frameID in frameIDs)
                {
                    CefFrame frame = browser.GetFrame(frameID);
                    if (frame == null)
                    {
                        continue;
                    }
                    frame.VisitDom(new CwbDOMVisitor(browser, CwbBusinStyle.bsGetElementValue, elementID, elementValue));
                }
                browser.GetMainFrame().VisitDom(new CwbDOMVisitor(browser, CwbBusinStyle.bsSetElementValue, elementID, elementValue));
                return(true);
            }

            case "EvaluateScript":
            {
                CefV8Value     value = CefV8Value.CreateString("t");
                CefV8Exception exp;
                browser.GetMainFrame().V8Context.TryEval(items[1], out value, out exp);
                Global.JsEvaResult = null;
                if (value.IsString)
                {
                    Global.JsEvaResult = value.GetStringValue();
                }
                if (value.IsInt)
                {
                    Global.JsEvaResult = value.GetIntValue();
                }
                if (value.IsDouble)
                {
                    Global.JsEvaResult = value.GetDoubleValue();
                }
                if (value.IsBool)
                {
                    Global.JsEvaResult = value.GetBoolValue();
                }
                if (value.IsDate)
                {
                    Global.JsEvaResult = value.GetDateValue();
                }
                Global.flag = true;
                return(true);
            }

            case "AppendListener":
            {
                CwbBusinStyle cbStyle = CwbBusinStyle.bsAddElementEvent;
                if (items.Length > 2)
                {
                    cbStyle = CwbBusinStyle.bsAttachElementEvent;
                }
                string elementID = items[1];
                long[] frameIDs  = browser.GetFrameIdentifiers();
                foreach (long frameID in frameIDs)
                {
                    CefFrame frame = browser.GetFrame(frameID);
                    if (frame == null)
                    {
                        continue;
                    }
                    frame.VisitDom(new CwbDOMVisitor(browser, cbStyle, webBrowser.getEventListener(elementID)));
                }
                browser.GetMainFrame().VisitDom(new CwbDOMVisitor(browser, cbStyle, webBrowser.getEventListener(elementID)));
                return(true);
            }

            case "GetDocument":
            {
                browser.GetMainFrame().VisitDom(new CwbDOMVisitor(browser, CwbBusinStyle.bsVisitDocument, ""));
                return(true);
            }
            }
            return(false);
        }
Example #11
0
        public static object GetClrValue(CefV8Value v)
        {
            if (v.IsArray)
            {
                int      length = v.GetArrayLength();
                object[] objs   = new object[length];
                for (int i = 0; i < length; i++)
                {
                    var value = v.GetValue(i);
                    objs[i] = GetClrValue(value);
                }
                return(objs);
            }

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

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

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

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


            if (v.IsNull)
            {
                return(null);
            }
            if (v.IsObject)
            {
                //throw new NotSupportedException("IsObject");
                //todo:这里可能存在BUG
                var map = v.GetUserData() as UnmanagedWrapper;
                if (map != null)
                {
                    return(map.ClrObject);
                }
                return(null);
            }
            if (v.IsString)
            {
                return(v.GetStringValue());
            }
            if (v.IsUInt)
            {
                return(v.GetUIntValue());
            }
            if (v.IsUndefined)
            {
                return(null);
            }
            if (v.IsUserCreated)
            {
                throw new NotSupportedException("IsUserCreated");
            }
            throw new NotSupportedException("??");
        }
Example #12
0
        public JSValue(CefV8Value value)
        {
            if (value == null)
            {
                return;
            }

            if (value.IsArray)
            {
                var result = new List <Object>();
                for (var i = 0; i < value.GetArrayLength(); i++)
                {
                    result.Add(new JSValue(value.GetValue(i)).Value);
                }
                this.Value = result.ToArray();
            }
            else if (value.IsObject)
            {
                var result = new Dictionary <String, Object>();
                var keys   = value.GetKeys();
                for (var i = 0; i < keys.Length; i++)
                {
                    try
                    {
                        result.Add(keys[i], new JSValue(value.GetValue(keys[i])).Value);
                    }
                    catch (ArgumentException)
                    {
                    }
                }
                this.Value = result;
            }
            else if (value.IsInt)
            {
                this.Value = value.GetIntValue();
            }
            else if (value.IsUInt)
            {
                this.Value = value.GetUIntValue();
            }
            else if (value.IsDouble)
            {
                this.Value = value.GetDoubleValue();
            }
            else if (value.IsBool)
            {
                this.Value = value.GetBoolValue();
            }
            else if (value.IsDate)
            {
                this.Value = value.GetDateValue();
            }
            else if (value.IsString)
            {
                this.Value = value.GetStringValue();
            }
            else if (value.IsNull)
            {
                this.Value = null;
            }
            else if (value.IsUndefined)
            {
                this.Value = null;
            }
        }
Example #13
0
        /// <summary>
        /// Parses the cef v8 value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private JToken ParseCefV8Value(CefV8Value value)
        {
            if (value == null)
            {
                return(null);
            }

            return(CefUtility.RunInContext(Context, () =>
            {
                if (value.IsInt)
                {
                    return JToken.FromObject(value.GetIntValue());
                }

                if (value.IsUInt)
                {
                    return JToken.FromObject(value.GetUIntValue());
                }

                if (value.IsDouble)
                {
                    return JToken.FromObject(value.GetDoubleValue());
                }

                if (value.IsBool)
                {
                    return JToken.FromObject(value.GetBoolValue());
                }

                if (value.IsDate)
                {
                    return JToken.FromObject(value.GetDateValue());
                }

                if (value.IsString)
                {
                    return JToken.FromObject(value.GetStringValue());
                }

                if (value.IsUndefined)
                {
                    return JValue.CreateUndefined();
                }

                if (value.IsArray)
                {
                    var array = new JArray();

                    for (var i = 0; i < value.GetArrayLength(); i++)
                    {
                        array.Add(ParseCefV8Value(value.GetValue(i)));
                    }

                    return array;
                }

                if (value.IsObject)
                {
                    var obj = new JObject();

                    foreach (var propertyName in value.GetKeys())
                    {
                        obj.Add(propertyName, ParseCefV8Value(value.GetValue(propertyName)));
                    }

                    return obj;
                }

                return JValue.CreateNull();
            }));
        }
Example #14
0
        public static JavaScriptValue ToJSValue(this CefV8Value source)
        {
            if (source == null || !source.IsValid)
            {
                throw new ArgumentNullException();
            }

            JavaScriptValue target;

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

                var info = new JavaScriptRenderSideFunction(context, source);

                JavaScriptObjectRepository.RenderSideFunctions.Add(info);

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


            return(target);
        }
Example #15
0
        /// <summary>
        /// 网页脚本与后台程序交互方法
        /// 提示一:如果 returnValue = null; 则会导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// 提示二:还存在其他的可能,导致导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="obj">对象</param>
        /// <param name="arguments">参数</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="exception">返回异常信息</param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            //  这里要加 try,不然发生异常直接导致 Render Process 崩溃,问题是遗留下另一个 Process,
            //  窗口关闭时遗留的这个 Process 不会退出

            try
            {
                string     result = string.Empty;
                Object     retObj = null;
                Type       t      = JsObject.GetType();
                MethodInfo mi     = t.GetMethod(name);
                if (mi != null)
                {
                    if (arguments.Length > 0)
                    {
                        Object[]   param = new Object[arguments.Length];
                        CefV8Value value = null;
                        for (int i = 0, j = arguments.Length; i < j; i++)
                        {
                            value = arguments[i];
                            if (value.IsString)
                            {
                                param[i] = value.GetStringValue();
                            }
                            else if (value.IsInt)
                            {
                                param[i] = value.GetIntValue();
                            }
                            else if (value.IsDouble)
                            {
                                param[i] = value.GetDoubleValue();
                            }
                            else if (value.IsArray)
                            {
                                int      len = value.GetArrayLength();
                                Object[] p2  = new Object[len];
                                for (int k = 0; k < len; k++)
                                {
                                    CefV8Value va = value.GetValue(k);
                                    if (va.IsString)
                                    {
                                        p2[k] = va.GetStringValue();
                                    }
                                    else if (va.IsInt)
                                    {
                                        p2[k] = va.GetIntValue();
                                    }
                                    else if (va.IsDouble)
                                    {
                                        p2[k] = va.GetDoubleValue();
                                    }
                                }
                                param[i] = p2;
                            }
                            else if (value.IsBool)
                            {
                                param[i] = value.GetBoolValue();
                            }
                            else if (value.IsNull || value.IsUndefined)
                            {
                                param[i] = null;
                            }
                        }
                        retObj = mi.Invoke(JsObject, param);
                    }
                    else
                    {
                        retObj = mi.Invoke(JsObject, null);
                    }
                }
                if (retObj != null)
                {
                    result = retObj.ToString();
                }
                returnValue = CefV8Value.CreateString(result);
                exception   = null;
                return(true);
            }
            catch (Exception ex)
            {
                returnValue = null;
                exception   = ex.ToString();
                MessageBox.Show(exception);
                return(true);
            }
        }
        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();
        }
Example #17
0
        /// <summary>
        /// 网页脚本与后台程序交互方法
        /// 提示一:如果 returnValue = null; 则会导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// 提示二:还存在其他的可能,导致导致网页前端出现错误:Cannot read property ’constructor’ of undefined
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="obj">对象</param>
        /// <param name="arguments">参数</param>
        /// <param name="returnValue">返回值</param>
        /// <param name="exception">返回异常信息</param>
        /// <returns></returns>
        protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
        {
            string     result = string.Empty;
            Object     retObj = null;
            Type       t      = JsObject.GetType();
            MethodInfo mi     = t.GetMethod(name);

            if (mi != null)
            {
                if (arguments.Length > 0)
                {
                    Object[]   param = new Object[arguments.Length];
                    CefV8Value value = null;
                    for (int i = 0, j = arguments.Length; i < j; i++)
                    {
                        value = arguments[i];
                        if (value.IsString)
                        {
                            param[i] = value.GetStringValue();
                        }
                        else if (value.IsInt)
                        {
                            param[i] = value.GetIntValue();
                        }
                        else if (value.IsDouble)
                        {
                            param[i] = value.GetIntValue();
                        }
                        else if (value.IsDouble)
                        {
                            param[i] = value.GetDoubleValue();
                            System.Windows.Forms.MessageBox.Show(param[i].ToString());
                        }
                        else if (value.IsArray)
                        {
                            int      len = value.GetArrayLength();
                            Object[] p2  = new Object[len];
                            for (int k = 0; k < len; k++)
                            {
                                CefV8Value va = value.GetValue(k);
                                if (va.IsString)
                                {
                                    p2[k] = va.GetStringValue();
                                }
                                else if (va.IsInt)
                                {
                                    p2[k] = va.GetIntValue();
                                }
                                else if (va.IsDouble)
                                {
                                    p2[k] = va.GetDoubleValue();
                                }
                            }
                            param[i] = p2;
                        }
                        else if (value.IsBool)
                        {
                            param[i] = value.GetBoolValue();
                        }
                        else if (value.IsNull || value.IsUndefined)
                        {
                            param[i] = null;
                        }
                    }
                    retObj = mi.Invoke(JsObject, param);
                }
                else
                {
                    retObj = mi.Invoke(JsObject, null);
                }
            }
            if (retObj != null)
            {
                result = retObj.ToString();
            }
            returnValue = CefV8Value.CreateString(result);
            exception   = null;
            return(true);
        }
Example #18
0
            protected override bool Execute(string name, CefV8Value obj, CefV8Value[] arguments, out CefV8Value returnValue, out string exception)
            {
                if (name == _config.JSQueryFunction)
                {
                    if (arguments.Length != 1 || !arguments[0].IsObject)
                    {
                        returnValue = null;
                        exception   = "Invalid arguments; expecting a single object";
                        return(true);
                    }

                    var arg = arguments[0];

                    var requestVal = arg.GetValue(CefMessageRouter.MemberRequest);
                    if (requestVal == null || !requestVal.IsString)
                    {
                        returnValue = null;
                        exception   = "Invalid arguments; object member '" +
                                      CefMessageRouter.MemberRequest + "' is required and must " +
                                      "have type string";
                        return(true);
                    }

                    CefV8Value successVal = null;
                    if (arg.HasValue(CefMessageRouter.MemberOnSuccess))
                    {
                        successVal = arg.GetValue(CefMessageRouter.MemberOnSuccess);
                        if (!successVal.IsFunction)
                        {
                            returnValue = null;
                            exception   = "Invalid arguments; object member '" +
                                          CefMessageRouter.MemberOnSuccess + "' must have type " +
                                          "function";
                            return(true);
                        }
                    }

                    CefV8Value failureVal = null;
                    if (arg.HasValue(CefMessageRouter.MemberOnFailure))
                    {
                        failureVal = arg.GetValue(CefMessageRouter.MemberOnFailure);
                        if (!failureVal.IsFunction)
                        {
                            returnValue = null;
                            exception   = "Invalid arguments; object member '" +
                                          CefMessageRouter.MemberOnFailure + "' must have type " +
                                          "function";
                            return(true);
                        }
                    }

                    CefV8Value persistentVal = null;
                    if (arg.HasValue(CefMessageRouter.MemberPersistent))
                    {
                        persistentVal = arg.GetValue(CefMessageRouter.MemberPersistent);
                        if (!persistentVal.IsBool)
                        {
                            returnValue = null;
                            exception   = "Invalid arguments; object member '" +
                                          CefMessageRouter.MemberPersistent + "' must have type " +
                                          "boolean";
                            return(true);
                        }
                    }

                    var context    = CefV8Context.GetCurrentContext();
                    var contextId  = GetIDForContext(context);
                    var frameId    = context.GetFrame().Identifier;
                    var persistent = (persistentVal != null && persistentVal.GetBoolValue());

                    var requestId = _router.SendQuery(context.GetBrowser(), frameId, contextId,
                                                      requestVal.GetStringValue(), persistent, successVal, failureVal);
                    returnValue = CefV8Value.CreateInt(requestId);
                    exception   = null;
                    return(true);
                }
                else if (name == _config.JSCancelFunction)
                {
                    if (arguments.Length != 1 || !arguments[0].IsInt)
                    {
                        returnValue = null;
                        exception   = "Invalid arguments; expecting a single integer";
                        return(true);
                    }

                    var result    = false;
                    var requestId = arguments[0].GetIntValue();
                    if (requestId != CefMessageRouter.ReservedId)
                    {
                        CefV8Context context   = CefV8Context.GetCurrentContext();
                        var          contextId = GetIDForContext(context);
                        var          frameId   = context.GetFrame().Identifier;

                        result = _router.SendCancel(context.GetBrowser(), frameId, contextId, requestId);
                    }
                    returnValue = CefV8Value.CreateBool(result);
                    exception   = null;
                    return(true);
                }

                returnValue = null;
                exception   = null;
                return(false);
            }
Example #19
0
        private void WriteV8Value(CefV8Value value, StringBuilder result, int indent = 0)
        {
            /*
             * var isUndefined = value.IsUndefined;
             * var isNull = value.IsNull;
             * var isBool = value.IsBool;
             * var isInt = value.IsInt;
             * var isDouble = value.IsDouble;
             * var isDate = value.IsDate;
             * var isString = value.IsString;
             * var isArray = value.IsArray;
             * var isObject = value.IsObject;
             * var isFunction = value.IsFunction;
             *
             * result.Append("[");
             * if (isUndefined) result.Append("undefined ");
             * if (isNull) result.Append("null ");
             * if (isBool) result.Append("bool ");
             * if (isInt) result.Append("int ");
             * if (isDouble) result.Append("double ");
             * if (isDate) result.Append("date ");
             * if (isString) result.Append("string ");
             * if (isArray) result.Append("array ");
             * if (isObject) result.Append("object ");
             * if (isFunction) result.Append("function");
             * result.Append("]");
             */

            if (value.IsUndefined)
            {
                result.Append("(undefined)");
            }
            else if (value.IsNull)
            {
                result.Append("(null)");
            }
            else if (value.IsBool)
            {
                result.AppendFormat("(bool) {0}", value.GetBoolValue() ? "true" : "false");
            }
            else if (value.IsInt)
            {
                result.AppendFormat("(int) {0}", value.GetIntValue());
            }
            else if (value.IsDouble)
            {
                result.AppendFormat("(double) {0}", value.GetDoubleValue().ToString(CultureInfo.InvariantCulture.NumberFormat));
            }
            else if (value.IsDate)
            {
                result.AppendFormat("(date) {0}", value.GetDateValue().ToString("s"));
            }
            else if (value.IsString)
            {
                result.AppendFormat("(string) {0}", value.GetStringValue());
            }
            else if (value.IsArray) // for array IsObject also is true
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(array) [");
                var length = value.GetArrayLength();
                for (var i = 0; i < length; i++)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, i);
                    WriteV8Value(value.GetValue(i), result, indent + 1);
                }
                if (length != 0)
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append(']');
            }
            else if (value.IsFunction) // for function IsObject also is true
            {
                var name        = value.GetFunctionName();
                var handler     = value.GetFunctionHandler();
                var declaration = value.GetStringValue();

                result.Append("(function) ");
                result.Append(!string.IsNullOrEmpty(name) ? name : "(anonymous)");
                if (handler != null)
                {
                    result.Append(" (handler: ");
                    result.Append(handler.ToString());
                    result.Append(")");
                }
                if (!string.IsNullOrEmpty(declaration))
                {
                    result.Append(" = ");
                    result.Append(declaration);
                }
            }
            else if (value.IsObject)
            {
                var indentString = string.Empty.PadLeft((indent + 1) * 4, ' ');
                result.Append("(object) {");
                var keys = value.GetKeys().AsEnumerable();
                foreach (var key in keys)
                {
                    result.AppendFormat("\n{0}{1} = ", indentString, key);
                    WriteV8Value(value.GetValue(key), result, indent + 1);
                }
                if (keys.Any())
                {
                    result.Append('\n');
                    result.Append(indentString);
                }
                result.Append('}');
            }
            //else result.Append("(unknown)");
        }