Skip to content

Commit

Permalink
First step towards getting JSB functions to return complex types - us…
Browse files Browse the repository at this point in the history
…es JavascriptOjbect/JavascriptProperty classes to represent the metadata transferred over WCF

(Sending the complex type it's self is difficult as the type is not known by both client and server)
  • Loading branch information
amaitland committed Jan 9, 2015
1 parent 901a70a commit 224cbcd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
25 changes: 25 additions & 0 deletions CefSharp.BrowserSubprocess.Core/TypeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ namespace CefSharp
{
return CefV8Value::CreateDate(TypeUtils::ConvertDateTimeToCefTime(safe_cast<DateTime>(obj)));
}
if (type == JavascriptObject::typeid)
{
JavascriptObject^ javascriptObject = (JavascriptObject^)obj;
CefRefPtr<CefV8Value> 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;
Expand Down
55 changes: 51 additions & 4 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions CefSharp/Internals/JavascriptProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public class JavascriptProperty
[DataMember]
public bool IsReadOnly { get; set; }

/// <summary>
/// Gets or sets the property value
/// Only primative types can be stored in this property
/// </summary>
[DataMember]
public object PropertyValue { get; set; }

public override string ToString()
{
return ManagedName ?? base.ToString();
Expand Down
4 changes: 4 additions & 0 deletions CefSharp/JavascriptKnownTypesRegistra.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using CefSharp.Internals;

namespace CefSharp
{
Expand All @@ -12,6 +13,9 @@ static JavascriptKnownTypesRegistra()
{
Register(typeof(object[]));
Register(typeof(Dictionary<string, object>));
Register(typeof(JavascriptObject));
Register(typeof(JavascriptProperty));
Register(typeof(JavascriptMethod));
}

public static void Register(Type type)
Expand Down

0 comments on commit 224cbcd

Please sign in to comment.