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); }
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); }
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 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); }
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)); }
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(); }
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); }
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("??"); }
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; } }
/// <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(); })); }
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); }
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); }
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)"); }