Skip to content

Commit 444a31e

Browse files
authored
Merge 9f471f7 into f90271f
2 parents f90271f + 9f471f7 commit 444a31e

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright © 2017 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System;
6+
using System.Diagnostics;
7+
using CefSharp.ModelBinding;
8+
9+
namespace CefSharp.Example.ModelBinding
10+
{
11+
public class PropertyInterceptorLogger : IPropertyInterceptor
12+
{
13+
object IPropertyInterceptor.InterceptGet(Func<object> propertyGetter, string propertyName)
14+
{
15+
object result = propertyGetter();
16+
Debug.WriteLine("InterceptGet " + propertyName);
17+
return result;
18+
}
19+
20+
void IPropertyInterceptor.InterceptSet(Action<object> propertySetter, object parameter, string propertName)
21+
{
22+
Debug.WriteLine("InterceptSet " + propertName);
23+
propertySetter(parameter);
24+
}
25+
}
26+
}

CefSharp.Wpf.Example/Views/BrowserTabView.xaml.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public BrowserTabView()
4646
var bindingOptions = new BindingOptions()
4747
{
4848
Binder = BindingOptions.DefaultBinder.Binder,
49-
MethodInterceptor = new MethodInterceptorLogger() // intercept .net methods calls from js and log it
49+
MethodInterceptor = new MethodInterceptorLogger(), // intercept .net methods calls from js and log it
50+
PropertyInterceptor = new PropertyInterceptorLogger()
5051
};
5152

5253
//To use the ResolveObject below and bind an object with isAsync:false we must set CefSharpSettings.WcfEnabled = true before
@@ -90,7 +91,7 @@ public BrowserTabView()
9091
{
9192
if (e.ObjectName == "bound")
9293
{
93-
repo.Register("bound", new BoundObject(), isAsync: false, options: BindingOptions.DefaultBinder);
94+
repo.Register("bound", new BoundObject(), isAsync: false, options: bindingOptions);
9495
}
9596
else if (e.ObjectName == "boundAsync")
9697
{

CefSharp/BindingOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,12 @@ public static BindingOptions DefaultBinder
2929
/// for logging calls (from js) to .net methods.
3030
/// </summary>
3131
public IMethodInterceptor MethodInterceptor { get; set; }
32+
33+
/// <summary>
34+
/// Interceptor used for intercepting get/set calls to the target object property. For instance, can be used
35+
/// for logging calls to .net property (from js)
36+
/// </summary>
37+
public IPropertyInterceptor PropertyInterceptor { get; set; }
38+
3239
}
3340
}

CefSharp/Internals/JavascriptObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public class JavascriptObject //: DynamicObject maybe later
6262

6363
public IMethodInterceptor MethodInterceptor { get; set; }
6464

65+
public IPropertyInterceptor PropertyInterceptor { get; set; }
66+
6567
public JavascriptObject()
6668
{
6769
Methods = new List<JavascriptMethod>();

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public void Register(string name, object value, bool isAsync, BindingOptions opt
250250
jsObject.IsAsync = isAsync;
251251
jsObject.Binder = options?.Binder;
252252
jsObject.MethodInterceptor = options?.MethodInterceptor;
253+
jsObject.PropertyInterceptor = options?.PropertyInterceptor;
253254

254255
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: !isAsync, readPropertyValue: false);
255256
}
@@ -585,7 +586,14 @@ protected virtual bool TryGetProperty(long objectId, string name, out object res
585586

586587
try
587588
{
588-
result = property.GetValue(obj.Value);
589+
if (obj.PropertyInterceptor == null)
590+
{
591+
result = property.GetValue(obj.Value);
592+
}
593+
else
594+
{
595+
result = obj.PropertyInterceptor.InterceptGet(() => property.GetValue(obj.Value), property.ManagedName);
596+
}
589597

590598
return true;
591599
}
@@ -618,7 +626,14 @@ protected virtual bool TrySetProperty(long objectId, string name, object value,
618626
}
619627
try
620628
{
621-
property.SetValue(obj.Value, value);
629+
if (obj.PropertyInterceptor == null)
630+
{
631+
property.SetValue(obj.Value, value);
632+
}
633+
else
634+
{
635+
obj.PropertyInterceptor.InterceptSet((p) => property.SetValue(obj.Value, p), value, property.ManagedName);
636+
}
622637

623638
return true;
624639
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CefSharp.ModelBinding
8+
{
9+
/// <summary>
10+
/// Provides the capability intercepting get/set property calls made from javascript as part of the
11+
/// JavascriptBinding (JSB) implementation.
12+
/// </summary>
13+
public interface IPropertyInterceptor
14+
{
15+
/// <summary>
16+
/// Called before the get property is invokved. You are now responsible for evaluating
17+
/// the property and returning the result.
18+
/// </summary>
19+
/// <param name="propertyGetter">A Func that represents the property to be called</param>
20+
/// <param name="propertName">Name of the property to be called</param>
21+
/// <returns>The property result</returns>
22+
/// <example>
23+
///
24+
///public object IPropertyInterceptor.InterceptGet(Func<object> propertyGetter, string propertyName)
25+
///{
26+
/// object result = propertyGetter();
27+
/// Debug.WriteLine("InterceptGet " + propertyName);
28+
/// return result;
29+
///}
30+
/// </example>
31+
///
32+
object InterceptGet(Func<object> propertyGetter, string propertName);
33+
34+
/// <summary>
35+
/// Called before the set property is invokved. You are now responsible for evaluating
36+
/// the property.
37+
/// </summary>
38+
/// <param name="propertySetter">A Func that represents the property to be called</param>
39+
/// <param name="parameter">paramater to be set to property</param>
40+
/// <param name="propertName">Name of the property to be called</param>
41+
/// <example>
42+
///
43+
/// public void IPropertyInterceptor.InterceptSet(Action<object> propertySetter, object parameter, string propertName)
44+
/// {
45+
/// Debug.WriteLine("InterceptSet " + propertName);
46+
/// propertySetter(parameter);
47+
/// }
48+
/// </example>
49+
///
50+
void InterceptSet(Action<Object> propertySetter, object parameter, string propertName);
51+
}
52+
}
53+
54+

0 commit comments

Comments
 (0)