From afa3998e6518bb7e640f2ccdf472f72a7817ec73 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 8 Jan 2018 15:12:52 +1000 Subject: [PATCH] JSB - Add support for async methods to return classes Structs and classes can now be returned from an async method Properties and fields are supported now. The returned class/struct is readonly effectively, no updates will be sent to the bound object. Just to clarify this is only for objects bound using RegisterAsyncJsObject --- .../Serialization/V8Serialization.cpp | 14 +++++++++-- CefSharp.Example/AsyncBoundObject.cs | 24 +++++++++++-------- CefSharp.Example/CefSharp.Example.csproj | 2 ++ CefSharp.Example/JsSerializableClass.cs | 11 +++++++++ CefSharp.Example/JsSerializableStruct.cs | 11 +++++++++ CefSharp.Example/Resources/BindingTest.html | 21 ++++++++++++---- 6 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 CefSharp.Example/JsSerializableClass.cs create mode 100644 CefSharp.Example/JsSerializableStruct.cs diff --git a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp index d7e7ee548..bc6dcbf37 100644 --- a/CefSharp.Core/Internals/Serialization/V8Serialization.cpp +++ b/CefSharp.Core/Internals/Serialization/V8Serialization.cpp @@ -111,7 +111,8 @@ namespace CefSharp } list->SetList(index, subList); } - else if (type->IsValueType && !type->IsPrimitive && !type->IsEnum) + // Serialize class/structs to CefDictionary (key,value pairs) + else if (!type->IsPrimitive && !type->IsEnum) { auto fields = type->GetFields(); auto subDict = CefDictionaryValue::Create(); @@ -122,11 +123,20 @@ namespace CefSharp auto fieldValue = fields[i]->GetValue(obj); SerializeV8SimpleObject(subDict, fieldName, fieldValue, seen); } + + auto properties = type->GetProperties(); + + for (int i = 0; i < properties->Length; i++) + { + auto propertyName = StringUtils::ToNative(properties[i]->Name); + auto propertyValue = properties[i]->GetValue(obj); + SerializeV8SimpleObject(subDict, propertyName, propertyValue, seen); + } list->SetDictionary(index, subDict); } else { - throw gcnew NotSupportedException("Complex types cannot be serialized to Cef lists"); + throw gcnew NotSupportedException("Unable to serialize Type"); } seen->Pop(); diff --git a/CefSharp.Example/AsyncBoundObject.cs b/CefSharp.Example/AsyncBoundObject.cs index 07ff2c819..14fd840b1 100644 --- a/CefSharp.Example/AsyncBoundObject.cs +++ b/CefSharp.Example/AsyncBoundObject.cs @@ -10,11 +10,6 @@ using System.Threading; namespace CefSharp.Example { - public struct JsObject - { - public string Value; - } - public class AsyncBoundObject { //We expect an exception here, so tell VS to ignore @@ -43,20 +38,29 @@ namespace CefSharp.Example return "Waited for 1000ms before returning"; } - public JsObject ReturnObject(string name) + public JsSerializableStruct ReturnObject(string name) { - return new JsObject + return new JsSerializableStruct { Value = name }; } - public JsObject[] ObjectArray(string name) + public JsSerializableStruct[] ReturnStructArray(string name) { return new[] { - new JsObject() { Value = "Item1" }, - new JsObject() { Value = "Item2" } + new JsSerializableStruct { Value = name + "Item1" }, + new JsSerializableStruct { Value = name + "Item2" } + }; + } + + public JsSerializableClass[] ReturnClassesArray(string name) + { + return new[] + { + new JsSerializableClass { Value = name + "Item1" }, + new JsSerializableClass { Value = name + "Item2" } }; } diff --git a/CefSharp.Example/CefSharp.Example.csproj b/CefSharp.Example/CefSharp.Example.csproj index 6403e1a76..6e1a12ff4 100644 --- a/CefSharp.Example/CefSharp.Example.csproj +++ b/CefSharp.Example/CefSharp.Example.csproj @@ -82,6 +82,8 @@ + + diff --git a/CefSharp.Example/JsSerializableClass.cs b/CefSharp.Example/JsSerializableClass.cs new file mode 100644 index 000000000..8d38f9231 --- /dev/null +++ b/CefSharp.Example/JsSerializableClass.cs @@ -0,0 +1,11 @@ +// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp.Example +{ + public class JsSerializableClass + { + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/CefSharp.Example/JsSerializableStruct.cs b/CefSharp.Example/JsSerializableStruct.cs new file mode 100644 index 000000000..d1d740d22 --- /dev/null +++ b/CefSharp.Example/JsSerializableStruct.cs @@ -0,0 +1,11 @@ +// Copyright © 2010-2017 The CefSharp Authors. All rights reserved. +// +// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. + +namespace CefSharp.Example +{ + public struct JsSerializableStruct + { + public string Value; + } +} \ No newline at end of file diff --git a/CefSharp.Example/Resources/BindingTest.html b/CefSharp.Example/Resources/BindingTest.html index 438a6bf6e..8cd2fbd12 100644 --- a/CefSharp.Example/Resources/BindingTest.html +++ b/CefSharp.Example/Resources/BindingTest.html @@ -190,14 +190,27 @@ }); }); - QUnit.test("Async call (ObjectArray):", function( assert ) + QUnit.test("Async call (returnStructArray):", function( assert ) { var asyncCallback = assert.async(); - boundAsync.objectArray('CefSharp').then(function (res) + boundAsync.returnStructArray('CefSharp').then(function (res) { - assert.equal(res[0].Value, "Item1"); - assert.equal(res[1].Value, "Item2"); + assert.equal(res[0].Value, "CefSharpItem1", "Expected Result of CefSharpItem1"); + assert.equal(res[1].Value, "CefSharpItem2", "Expected Result of CefSharpItem2"); + + asyncCallback(); + }); + }); + + QUnit.test("Async call (returnClassesArray):", function( assert ) + { + var asyncCallback = assert.async(); + + boundAsync.returnClassesArray('CefSharp').then(function (res) + { + assert.equal(res[0].Value, "CefSharpItem1", "Expected Result of CefSharpItem1"); + assert.equal(res[1].Value, "CefSharpItem2", "Expected Result of CefSharpItem2"); asyncCallback(); });