Skip to content

Commit

Permalink
Javascript Binding - Add Url to JavascriptBindingEventArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
amaitland committed Jan 1, 2025
1 parent 2bf01fa commit 0cb2878
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CefSharp.Core.Runtime/Internals/ClientAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ namespace CefSharp
//Call GetObjects with the list of names provided (will default to all if the list is empty
//Previously we only sent a response if there were bound objects, now we always send
//a response so the promise is resolved.
auto objs = objectRepository->GetObjects(names);
auto objs = objectRepository->GetObjects(StringUtils::ToClr(frame->GetURL()), names);

auto msg = CefProcessMessage::Create(kJavascriptRootObjectResponse);
auto responseArgList = msg->GetArgumentList();
Expand Down
2 changes: 1 addition & 1 deletion CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace CefSharp

if (objectRepositorySettings->LegacyBindingEnabled)
{
auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects();
auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects(address);

legacyBindingEnabled = objectRepository->HasBoundObjects;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ShouldRegisterJavascriptObjectBindWhenNamespaceIsNull()
#endif
Assert.True(javascriptObjectRepository.IsBound(name));

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

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

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

object getResult, setResult = 100;
Expand Down
1 change: 1 addition & 0 deletions CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public async Task ShouldFireResolveObject()

Assert.NotNull(evt);
Assert.Equal("first", evt.Arguments.ObjectName);
Assert.Equal("https://cefsharp.example/HelloWorld.html", evt.Arguments.Url);
}

[Fact]
Expand Down
9 changes: 8 additions & 1 deletion CefSharp/Event/JavascriptBindingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ public class JavascriptBindingEventArgs : EventArgs
/// </summary>
public string ObjectName { get; private set; }

/// <summary>
/// URL of frame the <see cref="IJavascriptObjectRepository.ResolveObject"/> call originated from.
/// </summary>
public string Url { get; private set; }

/// <summary>
/// Constructor
/// </summary>
/// <param name="objectRepository">object repository</param>
/// <param name="url">URL of frame the <see cref="IJavascriptObjectRepository.ResolveObject"/> call originated from.</param>
/// <param name="name">object name</param>
public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string name)
public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string url, string name)
{
ObjectRepository = objectRepository;
ObjectName = name;
Url = url;
}
}
}
4 changes: 2 additions & 2 deletions CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public interface IJavascriptObjectRepositoryInternal : IJavascriptObjectReposito
bool TrySetProperty(long objectId, string name, object value, out string exception);
#endif
bool IsBrowserInitialized { get; set; }
List<JavascriptObject> GetObjects(List<string> names = null);
List<JavascriptObject> GetLegacyBoundObjects();
List<JavascriptObject> GetObjects(string url, List<string> names = null);
List<JavascriptObject> GetLegacyBoundObjects(string url);
void ObjectsBound(List<Tuple<string, bool, bool>> objs);
}
}
14 changes: 7 additions & 7 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,24 @@ public bool IsBound(string name)
return objects.Values.Any(x => x.Name == name);
}

List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects()
List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects(string url)
{
RaiseResolveObjectEvent(LegacyObjects);
RaiseResolveObjectEvent(url, LegacyObjects);

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

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

return objects.Values.Where(x => x.RootObject).ToList();
}
Expand All @@ -149,7 +149,7 @@ List<JavascriptObject> IJavascriptObjectRepositoryInternal.GetObjects(List<strin
{
if (!IsBound(name))
{
RaiseResolveObjectEvent(name);
RaiseResolveObjectEvent(url, name);
}
}

Expand Down Expand Up @@ -723,9 +723,9 @@ private void AnalyseObjectForBinding(JavascriptObject obj, bool analyseMethods,
}
}

private void RaiseResolveObjectEvent(string name)
private void RaiseResolveObjectEvent(string url, string name)
{
ResolveObject?.Invoke(this, new JavascriptBindingEventArgs(this, name));
ResolveObject?.Invoke(this, new JavascriptBindingEventArgs(this, url, name));
}

private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, IJavascriptNameConverter nameConverter)
Expand Down

0 comments on commit 0cb2878

Please sign in to comment.