Skip to content

Commit 4567d0f

Browse files
committed
Javascript Binding - Add Url to JavascriptBindingEventArgs (#5021)
Co-authored-by: amaitland <[email protected]>
1 parent c15d75e commit 4567d0f

File tree

7 files changed

+22
-14
lines changed

7 files changed

+22
-14
lines changed

CefSharp.Core.Runtime/Internals/ClientAdapter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ namespace CefSharp
13111311
//Call GetObjects with the list of names provided (will default to all if the list is empty
13121312
//Previously we only sent a response if there were bound objects, now we always send
13131313
//a response so the promise is resolved.
1314-
auto objs = objectRepository->GetObjects(names);
1314+
auto objs = objectRepository->GetObjects(StringUtils::ToClr(frame->GetURL()), names);
13151315

13161316
auto msg = CefProcessMessage::Create(kJavascriptRootObjectResponse);
13171317
auto responseArgList = msg->GetArgumentList();

CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace CefSharp
5151

5252
if (objectRepositorySettings->LegacyBindingEnabled)
5353
{
54-
auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects();
54+
auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects(address);
5555

5656
legacyBindingEnabled = objectRepository->HasBoundObjects;
5757

CefSharp.Test/JavascriptBinding/JavaScriptObjectRepositoryTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void ShouldRegisterJavascriptObjectBindWhenNamespaceIsNull()
4141
#endif
4242
Assert.True(javascriptObjectRepository.IsBound(name));
4343

44-
var boundObjects = javascriptObjectRepository.GetObjects(new List<string> { name });
44+
var boundObjects = javascriptObjectRepository.GetObjects("example.com", new List<string> { name });
4545
Assert.Single(boundObjects);
4646

4747
var result = javascriptObjectRepository.TryCallMethod(boundObjects[0].Id, "getExampleString", new object[0]);
@@ -74,7 +74,7 @@ public void ShouldRegisterJavascriptObjectPropertyBindWhenNamespaceIsNull()
7474
javascriptObjectRepository.Register(name, new NoNamespaceClass(), false, bindingOptions);
7575
Assert.True(javascriptObjectRepository.IsBound(name));
7676

77-
var boundObjects = javascriptObjectRepository.GetObjects(new List<string> { name });
77+
var boundObjects = javascriptObjectRepository.GetObjects("example.com", new List<string> { name });
7878
Assert.Single(boundObjects);
7979

8080
object getResult, setResult = 100;

CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public async Task ShouldFireResolveObject()
210210

211211
Assert.NotNull(evt);
212212
Assert.Equal("first", evt.Arguments.ObjectName);
213+
Assert.Equal("https://cefsharp.example/HelloWorld.html", evt.Arguments.Url);
213214
}
214215

215216
[Fact]

CefSharp/Event/JavascriptBindingEventArgs.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ public class JavascriptBindingEventArgs : EventArgs
2121
/// </summary>
2222
public string ObjectName { get; private set; }
2323

24+
/// <summary>
25+
/// URL of frame the <see cref="IJavascriptObjectRepository.ResolveObject"/> call originated from.
26+
/// </summary>
27+
public string Url { get; private set; }
28+
2429
/// <summary>
2530
/// Constructor
2631
/// </summary>
2732
/// <param name="objectRepository">object repository</param>
33+
/// <param name="url">URL of frame the <see cref="IJavascriptObjectRepository.ResolveObject"/> call originated from.</param>
2834
/// <param name="name">object name</param>
29-
public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string name)
35+
public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string url, string name)
3036
{
3137
ObjectRepository = objectRepository;
3238
ObjectName = name;
39+
Url = url;
3340
}
3441
}
3542
}

CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public interface IJavascriptObjectRepositoryInternal : IJavascriptObjectReposito
1919
bool TrySetProperty(long objectId, string name, object value, out string exception);
2020
#endif
2121
bool IsBrowserInitialized { get; set; }
22-
List<JavascriptObject> GetObjects(List<string> names = null);
23-
List<JavascriptObject> GetLegacyBoundObjects();
22+
List<JavascriptObject> GetObjects(string url, List<string> names = null);
23+
List<JavascriptObject> GetLegacyBoundObjects(string url);
2424
void ObjectsBound(List<Tuple<string, bool, bool>> objs);
2525
}
2626
}

CefSharp/Internals/JavascriptObjectRepository.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,24 @@ public bool IsBound(string name)
123123
return objects.Values.Any(x => x.Name == name);
124124
}
125125

126-
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects()
126+
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects(string url)
127127
{
128-
RaiseResolveObjectEvent(LegacyObjects);
128+
RaiseResolveObjectEvent(url, LegacyObjects);
129129

130130
return objects.Values.Where(x => x.RootObject).ToList();
131131
}
132132

133133
//Ideally this would internal, unfurtunately it's used in C++
134134
//and it's hard to expose internals
135-
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetObjects(List<string> names)
135+
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetObjects(string url, List<string> names)
136136
{
137137
//If there are no objects names or the count is 0 then we will raise
138138
//the resolve event then return all objects that are registered,
139139
//we'll only perform checking if object(s) of specific name is requested.
140140
var getAllObjects = names == null || names.Count == 0;
141141
if (getAllObjects)
142142
{
143-
RaiseResolveObjectEvent(AllObjects);
143+
RaiseResolveObjectEvent(url, AllObjects);
144144

145145
return objects.Values.Where(x => x.RootObject).ToList();
146146
}
@@ -149,7 +149,7 @@ List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetObjects(List<strin
149149
{
150150
if (!IsBound(name))
151151
{
152-
RaiseResolveObjectEvent(name);
152+
RaiseResolveObjectEvent(url, name);
153153
}
154154
}
155155

@@ -723,9 +723,9 @@ private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods,
723723
}
724724
}
725725

726-
private void RaiseResolveObjectEvent(string name)
726+
private void RaiseResolveObjectEvent(string url, string name)
727727
{
728-
ResolveObject?.Invoke(this, new JavascriptBindingEventArgs(this, name));
728+
ResolveObject?.Invoke(this, new JavascriptBindingEventArgs(this, url, name));
729729
}
730730

731731
private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, IJavascriptNameConverter nameConverter)

0 commit comments

Comments
 (0)