Skip to content

Commit 801d53b

Browse files
committed
Add JavascriptBindingCompleteEventArgs - Use for IJavascriptObjectRepository.ObjectsBound event
This contains the breaking changes required for #2362
1 parent 9eb1975 commit 801d53b

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

CefSharp/CefSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<Compile Include="CdmRegistration.cs" />
9898
<Compile Include="Enums\AlphaType.cs" />
9999
<Compile Include="Enums\ColorType.cs" />
100+
<Compile Include="Event\JavascriptBindingCompleteEventArgs.cs" />
100101
<Compile Include="Event\JavascriptBindingEventArgs.cs" />
101102
<Compile Include="Handler\DefaultRequestHandler.cs" />
102103
<Compile Include="Enums\UrlRequestFlags.cs" />
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright © 2010-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+
7+
namespace CefSharp.Event
8+
{
9+
/// <summary>
10+
/// Event arguments for the <see cref="IJavascriptObjectRepository.ObjectBoundInJavascript"/> event
11+
/// </summary>
12+
public class JavascriptBindingCompleteEventArgs : EventArgs
13+
{
14+
/// <summary>
15+
/// The javascript object repository, used to register objects
16+
/// </summary>
17+
public IJavascriptObjectRepository ObjectRepository { get; private set; }
18+
19+
/// <summary>
20+
/// Name of the object
21+
/// </summary>
22+
public string ObjectName { get; private set; }
23+
24+
/// <summary>
25+
/// Was the object already bound. The default is false for the first js call to
26+
/// CefSharp.BindObjectAsync, and subsiquently true if already bound in a given context.
27+
/// </summary>
28+
public bool AlreadyBound { get; private set; }
29+
30+
/// <summary>
31+
/// Is the object cached
32+
/// </summary>
33+
public bool IsCached { get; private set; }
34+
35+
public JavascriptBindingCompleteEventArgs(IJavascriptObjectRepository objectRepository, string name, bool alreadyBound, bool isCached)
36+
{
37+
ObjectRepository = objectRepository;
38+
ObjectName = name;
39+
AlreadyBound = alreadyBound;
40+
IsCached = isCached;
41+
}
42+
}
43+
}

CefSharp/Event/JavascriptBindingEventArgs.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22
//
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

5-
using CefSharp;
65
using System;
7-
using System.Collections.Generic;
86

97
namespace CefSharp.Event
108
{
119
/// <summary>
12-
/// Event arguments for the <see cref="IJavascriptObjectRepository.ResolveObject"/> and
13-
/// <see cref="IJavascriptObjectRepository.ObjectBoundInJavascript"/> events
10+
/// Event arguments for the <see cref="IJavascriptObjectRepository.ResolveObject"/> event
1411
/// </summary>
1512
public class JavascriptBindingEventArgs : EventArgs
1613
{
@@ -24,6 +21,11 @@ public class JavascriptBindingEventArgs : EventArgs
2421
/// </summary>
2522
public string ObjectName { get; private set; }
2623

24+
/// <summary>
25+
/// Constructor
26+
/// </summary>
27+
/// <param name="objectRepository">object repository</param>
28+
/// <param name="name">object name</param>
2729
public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string name)
2830
{
2931
ObjectRepository = objectRepository;

CefSharp/IJavascriptObjectRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ public interface IJavascriptObjectRepository : IDisposable
5353
/// <summary>
5454
/// Event handler is triggered when a object has been successfully bound on javascript
5555
/// </summary>
56-
event EventHandler<JavascriptBindingEventArgs> ObjectBoundInJavascript;
56+
event EventHandler<JavascriptBindingCompleteEventArgs> ObjectBoundInJavascript;
5757
}
5858
}

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Linq;
88
using System.Reflection;
99
using System.Threading;
10-
using CefSharp.ModelBinding;
1110
using CefSharp.Event;
1211
using System.Threading.Tasks;
1312

@@ -37,7 +36,7 @@ public class JavascriptObjectRepository : IJavascriptObjectRepository
3736
private static long lastId;
3837

3938
public event EventHandler<JavascriptBindingEventArgs> ResolveObject;
40-
public event EventHandler<JavascriptBindingEventArgs> ObjectBoundInJavascript;
39+
public event EventHandler<JavascriptBindingCompleteEventArgs> ObjectBoundInJavascript;
4140

4241
/// <summary>
4342
/// A hash from assigned object ids to the objects,
@@ -67,6 +66,8 @@ public bool IsBound(string name)
6766
return objects.Values.Any(x => x.Name == name);
6867
}
6968

69+
//Ideally this would internal, unfurtunately it's used in C++
70+
//and it's hard to expose internals
7071
public List<JavascriptObject> GetObjects(List<string> names = null)
7172
{
7273
//If there are no objects names or the count is 0 then we will raise
@@ -96,23 +97,17 @@ public List<JavascriptObject> GetObjects(List<string> names = null)
9697
}
9798

9899

99-
public void ObjectsBound(List<string> names)
100+
public void ObjectsBound(List<string> objs)
100101
{
101-
//TODO: JSB Should this be a single event invocation or
102-
// one per object that was bound??? (Currently one per object)
103-
104102
//Execute on Threadpool so we don't unnessicarily block the CEF IO thread
105103
var handler = ObjectBoundInJavascript;
106104
if(handler != null)
107105
{
108106
Task.Run(() =>
109107
{
110-
foreach (var name in names)
108+
foreach (var obj in objs)
111109
{
112-
if (handler != null)
113-
{
114-
handler(this, new JavascriptBindingEventArgs(this, name));
115-
}
110+
handler?.Invoke(this, new JavascriptBindingCompleteEventArgs(this, obj, false, false));
116111
}
117112
});
118113
}
@@ -161,7 +156,7 @@ public void Register(string name, object value, bool isAsync, BindingOptions opt
161156
AnalyseObjectForBinding(jsObject, analyseMethods: true, analyseProperties: !isAsync, readPropertyValue: false, camelCaseJavascriptNames: camelCaseJavascriptNames);
162157
}
163158

164-
public bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
159+
internal bool TryCallMethod(long objectId, string name, object[] parameters, out object result, out string exception)
165160
{
166161
exception = "";
167162
result = null;
@@ -297,7 +292,7 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
297292
return false;
298293
}
299294

300-
public bool TryGetProperty(long objectId, string name, out object result, out string exception)
295+
internal bool TryGetProperty(long objectId, string name, out object result, out string exception)
301296
{
302297
exception = "";
303298
result = null;
@@ -327,7 +322,7 @@ public bool TryGetProperty(long objectId, string name, out object result, out st
327322
return false;
328323
}
329324

330-
public bool TrySetProperty(long objectId, string name, object value, out string exception)
325+
internal bool TrySetProperty(long objectId, string name, object value, out string exception)
331326
{
332327
exception = "";
333328
JavascriptObject obj;
@@ -429,12 +424,7 @@ private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods,
429424

430425
private void RaiseResolveObjectEvent(string name)
431426
{
432-
var handler = ResolveObject;
433-
434-
if(handler != null)
435-
{
436-
handler(this, new JavascriptBindingEventArgs(this, name));
437-
}
427+
ResolveObject?.Invoke(this, new JavascriptBindingEventArgs(this, name));
438428
}
439429

440430
private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, bool camelCaseJavascriptNames)

0 commit comments

Comments
 (0)