Skip to content

Commit 25be78c

Browse files
authored
Merge dc9eece into dc9d0a2
2 parents dc9d0a2 + dc9eece commit 25be78c

File tree

8 files changed

+130
-4
lines changed

8 files changed

+130
-4
lines changed

CefSharp.Example/CefSharp.Example.netcore.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
<None Remove="obj/**/*.*" />
8888
<Compile Remove="obj/**/*.*" />
8989
</ItemGroup>
90+
91+
<ItemGroup>
92+
<Compile Remove="ModelBinding\PropertyInterceptorLogger.cs" />
93+
</ItemGroup>
9094

9195
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk.WindowsDesktop" />
9296
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright © 2022 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ 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+
#if !NETCOREAPP
51+
PropertyInterceptor = new PropertyInterceptorLogger()
52+
#endif
5053
};
5154

5255
//To use the ResolveObject below and bind an object with isAsync:false we must set CefSharpSettings.WcfEnabled = true before
@@ -90,7 +93,7 @@ public BrowserTabView()
9093
{
9194
if (e.ObjectName == "bound")
9295
{
93-
repo.Register("bound", new BoundObject(), isAsync: false, options: BindingOptions.DefaultBinder);
96+
repo.Register("bound", new BoundObject(), isAsync: false, options: bindingOptions);
9497
}
9598
else if (e.ObjectName == "boundAsync")
9699
{

CefSharp/BindingOptions.cs

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

CefSharp/CefSharp.netcore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Compile Remove="CefRuntime.cs" />
4646
<Compile Remove="DevTools\DevToolsClient.Generated.cs" />
4747
<Compile Remove="Internals\Partial\ChromiumWebBrowser.Partial.cs" />
48+
<Compile Remove="ModelBinding\IPropertyInterceptor.cs" />
4849
</ItemGroup>
4950

5051
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />

CefSharp/Internals/JavascriptObject.cs

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

6363
public IMethodInterceptor MethodInterceptor { get; set; }
6464

65+
#if !NETCOREAPP
66+
public IPropertyInterceptor PropertyInterceptor { get; set; }
67+
#endif
68+
6569
public JavascriptObject()
6670
{
6771
Methods = new List<JavascriptMethod>();

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ 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+
#if !NETCOREAPP
254+
jsObject.PropertyInterceptor = options?.PropertyInterceptor;
255+
#endif
253256

254257
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: !isAsync, readPropertyValue: false);
255258
}
@@ -585,8 +588,18 @@ protected virtual bool TryGetProperty(long objectId, string name, out object res
585588

586589
try
587590
{
591+
#if NETCOREAPP
588592
result = property.GetValue(obj.Value);
589-
593+
#else
594+
if (obj.PropertyInterceptor == null)
595+
{
596+
result = property.GetValue(obj.Value);
597+
}
598+
else
599+
{
600+
result = obj.PropertyInterceptor.InterceptGet(() => property.GetValue(obj.Value), property.ManagedName);
601+
}
602+
#endif
590603
return true;
591604
}
592605
catch (Exception ex)
@@ -618,8 +631,18 @@ protected virtual bool TrySetProperty(long objectId, string name, object value,
618631
}
619632
try
620633
{
634+
#if NETCOREAPP
621635
property.SetValue(obj.Value, value);
622-
636+
#else
637+
if (obj.PropertyInterceptor == null)
638+
{
639+
property.SetValue(obj.Value, value);
640+
}
641+
else
642+
{
643+
obj.PropertyInterceptor.InterceptSet((p) => property.SetValue(obj.Value, p), value, property.ManagedName);
644+
}
645+
#endif
623646
return true;
624647
}
625648
catch (Exception ex)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
/// <code>
24+
/// <![CDATA[
25+
/// public object IPropertyInterceptor.InterceptGet(Func<object> propertyGetter, string propertyName)
26+
/// {
27+
/// object result = propertyGetter();
28+
/// Debug.WriteLine("InterceptGet " + propertyName);
29+
/// return result;
30+
/// }
31+
/// ]]>
32+
/// </code>
33+
/// </example>
34+
object InterceptGet(Func<object> propertyGetter, string propertName);
35+
36+
/// <summary>
37+
/// Called before the set property is invokved. You are now responsible for evaluating
38+
/// the property.
39+
/// </summary>
40+
/// <param name="propertySetter">A Func that represents the property to be called</param>
41+
/// <param name="parameter">paramater to be set to property</param>
42+
/// <param name="propertName">Name of the property to be called</param>
43+
/// <example>
44+
/// <code>
45+
/// <![CDATA[
46+
/// public object IPropertyInterceptor.InterceptSet(Action<object> propertySetter, object parameter, string propertName)
47+
/// {
48+
/// Debug.WriteLine("InterceptSet " + propertName);
49+
/// propertySetter(parameter);
50+
/// }
51+
/// ]]>
52+
/// </code>
53+
/// </example>
54+
void InterceptSet(Action<Object> propertySetter, object parameter, string propertName);
55+
}
56+
}

0 commit comments

Comments
 (0)