Skip to content

Commit 2d6bd9d

Browse files
authored
Merge 4d1bb4a into f07be8c
2 parents f07be8c + 4d1bb4a commit 2d6bd9d

File tree

6 files changed

+90
-4
lines changed

6 files changed

+90
-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.GetIntercept(Func<object> property, string propertyName)
14+
{
15+
object result = property();
16+
Debug.WriteLine("GetIntercept " + propertyName);
17+
return result;
18+
}
19+
20+
public void SetIntercept(Action<object> property, object parameter, string propertName)
21+
{
22+
property(parameter);
23+
Debug.WriteLine("SetIntercept " + propertName);
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 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.GetIntercept(() => 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.SetIntercept((p) => property.SetValue(obj.Value, p), value, property.ManagedName);
636+
}
622637

623638
return true;
624639
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 intercept Net property calls made from javascript as part of the
11+
/// JavascriptBinding (JSB) implementation.
12+
/// </summary>
13+
public interface IPropertyInterceptor
14+
{
15+
/// <summary>
16+
/// Intercept the get property
17+
/// </summary>
18+
/// <param name="property"></param>
19+
/// <param name="parameters"></param>
20+
/// <param name="propertName"></param>
21+
/// <returns></returns>
22+
object GetIntercept(Func<object> property, string propertName);
23+
24+
/// <summary>
25+
/// Intercept the set property
26+
/// </summary>
27+
/// <param name="property"></param>
28+
/// <param name="parameters"></param>
29+
/// <param name="propertName"></param>
30+
/// <returns></returns>
31+
void SetIntercept(Action<Object> property, object parameter, string propertName);
32+
}
33+
}
34+
35+

0 commit comments

Comments
 (0)