diff --git a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp index 9b347e01d9..2c26ee04d6 100644 --- a/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp +++ b/CefSharp.BrowserSubprocess.Core/TypeUtils.cpp @@ -94,6 +94,31 @@ namespace CefSharp { return CefV8Value::CreateDate(TypeUtils::ConvertDateTimeToCefTime(safe_cast(obj))); } + if (type == JavascriptObject::typeid) + { + JavascriptObject^ javascriptObject = (JavascriptObject^)obj; + CefRefPtr cefObject = CefV8Value::CreateObject(NULL); + + for (int i = 0; i < javascriptObject->Properties->Count; i++) + { + auto prop = javascriptObject->Properties[i]; + + if(prop->IsComplexType) + { + auto v8Value = TypeUtils::ConvertToCef(prop->JsObject, nullptr); + + cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + } + else + { + auto v8Value = TypeUtils::ConvertToCef(prop->PropertyValue, nullptr); + + cefObject->SetValue(StringUtils::ToNative(prop->JavascriptName), v8Value, CefV8Value::PropertyAttribute::V8_PROPERTY_ATTRIBUTE_NONE); + } + } + + return cefObject; + } if (type->IsArray) { Array^ managedArray = (Array^)obj; diff --git a/CefSharp/Internals/JavascriptObjectRepository.cs b/CefSharp/Internals/JavascriptObjectRepository.cs index 6d640bc1c2..73748df781 100644 --- a/CefSharp/Internals/JavascriptObjectRepository.cs +++ b/CefSharp/Internals/JavascriptObjectRepository.cs @@ -67,6 +67,18 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o { result = method.Function(obj.Value, parameters); + if(IsComplexType(result.GetType())) + { + var jsObject = CreateJavascriptObject(); + jsObject.Value = result; + jsObject.Name = "FunctionResult(" + name + ")"; + jsObject.JavascriptName = jsObject.Name; + + BuildJavascriptObjectForComplexFunctionResult(jsObject); + + result = jsObject; + } + return true; } catch (Exception ex) @@ -156,10 +168,10 @@ private void Analyse(JavascriptObject obj) continue; } - if (IsComplexType(methodInfo.ReturnType)) - { - JavascriptKnownTypesRegistra.Register(methodInfo.ReturnType); - } + //if (IsComplexType(methodInfo.ReturnType)) + //{ + // JavascriptKnownTypesRegistra.Register(methodInfo.ReturnType); + //} var jsMethod = CreateJavaScriptMethod(methodInfo); obj.Methods.Add(jsMethod); @@ -187,6 +199,41 @@ private void Analyse(JavascriptObject obj) } } + private void BuildJavascriptObjectForComplexFunctionResult(JavascriptObject obj) + { + if (obj.Value == null) + { + return; + } + + var type = obj.Value.GetType(); + + foreach (var propertyInfo in type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => !p.IsSpecialName)) + { + if (propertyInfo.PropertyType == typeof(Type) || Attribute.IsDefined(propertyInfo, typeof(JavascriptIgnoreAttribute))) + { + continue; + } + + var jsProperty = CreateJavaScriptProperty(propertyInfo); + if (jsProperty.IsComplexType) + { + var jsObject = CreateJavascriptObject(); + jsObject.Name = propertyInfo.Name; + jsObject.JavascriptName = LowercaseFirst(propertyInfo.Name); + jsObject.Value = jsProperty.GetValue(obj.Value); + jsProperty.JsObject = jsObject; + + BuildJavascriptObjectForComplexFunctionResult(jsProperty.JsObject); + } + else + { + jsProperty.PropertyValue = jsProperty.GetValue(obj.Value); + } + obj.Properties.Add(jsProperty); + } + } + private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo) { var jsMethod = new JavascriptMethod(); diff --git a/CefSharp/Internals/JavascriptProperty.cs b/CefSharp/Internals/JavascriptProperty.cs index 483442897f..ef49dba5dc 100644 --- a/CefSharp/Internals/JavascriptProperty.cs +++ b/CefSharp/Internals/JavascriptProperty.cs @@ -53,6 +53,13 @@ public class JavascriptProperty [DataMember] public bool IsReadOnly { get; set; } + /// + /// Gets or sets the property value + /// Only primative types can be stored in this property + /// + [DataMember] + public object PropertyValue { get; set; } + public override string ToString() { return ManagedName ?? base.ToString(); diff --git a/CefSharp/JavascriptKnownTypesRegistra.cs b/CefSharp/JavascriptKnownTypesRegistra.cs index 0588ad013d..d0bcaabb03 100644 --- a/CefSharp/JavascriptKnownTypesRegistra.cs +++ b/CefSharp/JavascriptKnownTypesRegistra.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using CefSharp.Internals; namespace CefSharp { @@ -12,6 +13,9 @@ static JavascriptKnownTypesRegistra() { Register(typeof(object[])); Register(typeof(Dictionary)); + Register(typeof(JavascriptObject)); + Register(typeof(JavascriptProperty)); + Register(typeof(JavascriptMethod)); } public static void Register(Type type)