Skip to content

Commit ecd4fd0

Browse files
committed
JSB - Fix bug where properties weren't being analysed for sync bound objects
Added test case Reordered method to make it more logical, used named properties so it's clear what's being set. Add TODO for unresolved object event
1 parent 55ea42b commit ecd4fd0

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

CefSharp.Example/Resources/BindingTest.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@
4343
assert.equal( actualResult, expectedResult, "We expect value to be " + actualResult );
4444
});
4545

46+
QUnit.test( "bound.myProperty", function( assert )
47+
{
48+
const expectedResult = 42;
49+
let actualResult = bound.myProperty;
50+
51+
assert.equal(actualResult, expectedResult, "We expect value to be " + expectedResult);
52+
});
53+
54+
QUnit.test( "bound.subObject.simpleProperty", function( assert )
55+
{
56+
const expectedResult = "This is a very simple property.";
57+
let actualResult = bound.subObject.simpleProperty;
58+
59+
assert.equal(actualResult, expectedResult, "We expect value to be " + expectedResult);
60+
61+
bound.subObject.simpleProperty = expectedResult + "1";
62+
63+
actualResult = bound.subObject.simpleProperty;
64+
65+
assert.equal(actualResult, (expectedResult + "1"), "We expect value to be " + (expectedResult + 1));
66+
67+
//Reset to default value
68+
bound.subObject.simpleProperty = expectedResult;
69+
});
70+
4671
QUnit.test( "Function delegate to c# method", function( assert )
4772
{
4873
function myFunction(functionParam)

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public bool IsBound(string name)
6666

6767
public List<JavascriptObject> GetObjects(List<string> names = null)
6868
{
69-
if (names == null || names.Count == 0)
69+
//If there are no objects names or the count is 0 then we will raise
70+
//the resolve event then return all objects that are registered,
71+
//we'll only perform checking if object(s) of specific name is requested.
72+
var getAllObjects = names == null || names.Count == 0;
73+
if (getAllObjects)
7074
{
7175
//TODO: JSB Declare Constant for All
7276
RaiseResolveObjectEvent("All");
@@ -81,8 +85,12 @@ public List<JavascriptObject> GetObjects(List<string> names = null)
8185
RaiseResolveObjectEvent(name);
8286
}
8387
}
84-
85-
return objects.Values.Where(x => names.Contains(x.JavascriptName)).ToList();
88+
89+
var objectsByName = objects.Values.Where(x => names.Contains(x.JavascriptName)).ToList();
90+
91+
//TODO: JSB Add another event that signals when no object matching a name
92+
//in the list was provided.
93+
return objectsByName;
8694
}
8795

8896

@@ -122,7 +130,7 @@ public void Register(string name, object value, bool isAsync, BindingOptions opt
122130
{
123131
//Enable WCF if not already enabled - can only be done before the browser has been initliazed
124132
//if done after the subprocess won't be WCF enabled it we'll have to throw an exception
125-
if (!IsBrowserInitialized)
133+
if (!IsBrowserInitialized && !isAsync)
126134
{
127135
CefSharpSettings.WcfEnabled = true;
128136
}
@@ -148,7 +156,7 @@ public void Register(string name, object value, bool isAsync, BindingOptions opt
148156
jsObject.Binder = options == null ? null : options.Binder;
149157
jsObject.MethodInterceptor = options == null ? null : options.MethodInterceptor;
150158

151-
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: isAsync, readPropertyValue: false, camelCaseJavascriptNames: camelCaseJavascriptNames);
159+
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: !isAsync, readPropertyValue: false, camelCaseJavascriptNames: camelCaseJavascriptNames);
152160
}
153161

154162
public bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
@@ -349,10 +357,10 @@ public bool TrySetProperty(long objectId, string name, object value, out string
349357
/// </summary>
350358
/// <param name="obj">Javascript object</param>
351359
/// <param name="analyseMethods">Analyse methods for inclusion in metadata model</param>
360+
/// <param name="analyseProperties">Analyse properties for inclusion in metadata model</param>
352361
/// <param name="readPropertyValue">When analysis is done on a property, if true then get it's value for transmission over WCF</param>
353362
/// <param name="camelCaseJavascriptNames">camel case the javascript names of properties/methods</param>
354-
/// <param name="analyseProperties">Analyse properties for binding</param>
355-
private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods, bool readPropertyValue, bool camelCaseJavascriptNames, bool analyseProperties)
363+
private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods, bool analyseProperties, bool readPropertyValue, bool camelCaseJavascriptNames)
356364
{
357365
if (obj.Value == null)
358366
{
@@ -403,7 +411,7 @@ private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods,
403411
jsObject.Value = jsProperty.GetValue(obj.Value);
404412
jsProperty.JsObject = jsObject;
405413

406-
AnalyseObjectForBinding(jsProperty.JsObject, analyseMethods, readPropertyValue, camelCaseJavascriptNames, true);
414+
AnalyseObjectForBinding(jsProperty.JsObject, analyseMethods, analyseProperties: true, readPropertyValue: readPropertyValue, camelCaseJavascriptNames: camelCaseJavascriptNames);
407415
}
408416
else if (readPropertyValue)
409417
{

0 commit comments

Comments
 (0)