From 3a749c0bf27be35db2d2ea23a40599e9e8f6d541 Mon Sep 17 00:00:00 2001 From: Alex Maitland Date: Sat, 1 Feb 2025 06:47:42 +1000 Subject: [PATCH] Javascript Binding - Add Url to JavascriptBindingEventArgs (#5021) Co-authored-by: amaitland <307872+amaitland@users.noreply.github.com> --- CefSharp.Core.Runtime/Internals/ClientAdapter.cpp | 2 +- CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp | 2 +- .../JavaScriptObjectRepositoryTests.cs | 4 ++-- .../JavascriptBinding/JavascriptBindingTests.cs | 1 + CefSharp/Event/JavascriptBindingEventArgs.cs | 9 ++++++++- .../IJavascriptObjectRepositoryInternal.cs | 4 ++-- CefSharp/Internals/JavascriptObjectRepository.cs | 14 +++++++------- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/CefSharp.Core.Runtime/Internals/ClientAdapter.cpp b/CefSharp.Core.Runtime/Internals/ClientAdapter.cpp index fc315ae0f..6a48a4668 100644 --- a/CefSharp.Core.Runtime/Internals/ClientAdapter.cpp +++ b/CefSharp.Core.Runtime/Internals/ClientAdapter.cpp @@ -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(); diff --git a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp index eb0d26102..47daea5dd 100644 --- a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp +++ b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp @@ -51,7 +51,7 @@ namespace CefSharp if (objectRepositorySettings->LegacyBindingEnabled) { - auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects(); + auto legacyBoundObjects = objectRepository->GetLegacyBoundObjects(address); legacyBindingEnabled = objectRepository->HasBoundObjects; diff --git a/CefSharp.Test/JavascriptBinding/JavaScriptObjectRepositoryTests.cs b/CefSharp.Test/JavascriptBinding/JavaScriptObjectRepositoryTests.cs index c1679612f..848184596 100644 --- a/CefSharp.Test/JavascriptBinding/JavaScriptObjectRepositoryTests.cs +++ b/CefSharp.Test/JavascriptBinding/JavaScriptObjectRepositoryTests.cs @@ -41,7 +41,7 @@ namespace CefSharp.Test.JavascriptBinding #endif Assert.True(javascriptObjectRepository.IsBound(name)); - var boundObjects = javascriptObjectRepository.GetObjects(new List { name }); + var boundObjects = javascriptObjectRepository.GetObjects("example.com", new List { name }); Assert.Single(boundObjects); var result = javascriptObjectRepository.TryCallMethod(boundObjects[0].Id, "getExampleString", new object[0]); @@ -74,7 +74,7 @@ namespace CefSharp.Test.JavascriptBinding javascriptObjectRepository.Register(name, new NoNamespaceClass(), false, bindingOptions); Assert.True(javascriptObjectRepository.IsBound(name)); - var boundObjects = javascriptObjectRepository.GetObjects(new List { name }); + var boundObjects = javascriptObjectRepository.GetObjects("example.com", new List { name }); Assert.Single(boundObjects); object getResult, setResult = 100; diff --git a/CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs b/CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs index a0ceac4c6..3bfbdd13d 100644 --- a/CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs +++ b/CefSharp.Test/JavascriptBinding/JavascriptBindingTests.cs @@ -210,6 +210,7 @@ namespace CefSharp.Test.JavascriptBinding Assert.NotNull(evt); Assert.Equal("first", evt.Arguments.ObjectName); + Assert.Equal("https://cefsharp.example/HelloWorld.html", evt.Arguments.Url); } [Fact] diff --git a/CefSharp/Event/JavascriptBindingEventArgs.cs b/CefSharp/Event/JavascriptBindingEventArgs.cs index 955c735b8..9275a1af2 100644 --- a/CefSharp/Event/JavascriptBindingEventArgs.cs +++ b/CefSharp/Event/JavascriptBindingEventArgs.cs @@ -21,15 +21,22 @@ namespace CefSharp.Event /// public string ObjectName { get; private set; } + /// + /// URL of frame the call originated from. + /// + public string Url { get; private set; } + /// /// Constructor /// /// object repository + /// URL of frame the call originated from. /// object name - public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string name) + public JavascriptBindingEventArgs(IJavascriptObjectRepository objectRepository, string url, string name) { ObjectRepository = objectRepository; ObjectName = name; + Url = url; } } } diff --git a/CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs b/CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs index 1069ebf52..d2e4ae3e7 100644 --- a/CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs +++ b/CefSharp/Internals/IJavascriptObjectRepositoryInternal.cs @@ -19,8 +19,8 @@ namespace CefSharp.Internals bool TrySetProperty(long objectId, string name, object value, out string exception); #endif bool IsBrowserInitialized { get; set; } - List GetObjects(List names = null); - List GetLegacyBoundObjects(); + List GetObjects(string url, List names = null); + List GetLegacyBoundObjects(string url); void ObjectsBound(List> objs); } } diff --git a/CefSharp/Internals/JavascriptObjectRepository.cs b/CefSharp/Internals/JavascriptObjectRepository.cs index 8136b9e50..af9fca0be 100644 --- a/CefSharp/Internals/JavascriptObjectRepository.cs +++ b/CefSharp/Internals/JavascriptObjectRepository.cs @@ -123,16 +123,16 @@ namespace CefSharp.Internals return objects.Values.Any(x => x.Name == name); } - List IJavascriptObjectRepositoryInternal.GetLegacyBoundObjects() + List 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 IJavascriptObjectRepositoryInternal.GetObjects(List names) + List IJavascriptObjectRepositoryInternal.GetObjects(string url, List 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, @@ -140,7 +140,7 @@ namespace CefSharp.Internals var getAllObjects = names == null || names.Count == 0; if (getAllObjects) { - RaiseResolveObjectEvent(AllObjects); + RaiseResolveObjectEvent(url, AllObjects); return objects.Values.Where(x => x.RootObject).ToList(); } @@ -149,7 +149,7 @@ namespace CefSharp.Internals { if (!IsBound(name)) { - RaiseResolveObjectEvent(name); + RaiseResolveObjectEvent(url, name); } } @@ -723,9 +723,9 @@ namespace CefSharp.Internals } } - 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)