Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed RegisterJavascriptObject #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CefGlue.Common/CommonBrowserAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private void NavigateTo(string url)
// Remove leading whitespace from the URL
url = url.TrimStart();

// to play safe, load url must be called after OnBrowserCreated(CefBrowser) which runs on CefThreadId.UI,
// to play safe, load url must be called after OnBrowserCreated(CefBrowser) which runs on CefThreadId.UI,
// otherwise the navigation will be aborted
ActionTask.Run(() =>
{
Expand Down Expand Up @@ -599,6 +599,8 @@ void ICefBrowserHost.HandleLoadStart(CefBrowser browser, CefFrame frame, CefTran

void ICefBrowserHost.HandleLoadEnd(CefBrowser browser, CefFrame frame, int httpStatusCode)
{
// Register all objects
_objectRegistry.SetBrowser(browser);
Copy link

@AndreBooysen AndreBooysen Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my scenario I needed to make sure that the objects are already registered during the page load, so I called this method inside HandleLoadStart instead.

LoadEnd?.Invoke(_eventsEmitter, new LoadEndEventArgs(frame, httpStatusCode));
}

Expand Down
9 changes: 5 additions & 4 deletions CefGlue.Common/ObjectBinding/NativeObjectRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class NativeObjectRegistry : IDisposable
private readonly object _registrationSyncRoot = new object();

/// <summary>
///
///
/// </summary>
/// <param name="obj"></param>
/// <param name="name"></param>
Expand All @@ -24,7 +24,7 @@ public bool Register(object obj, string name, MethodCallHandler methodHandler =
{
return false;
}

var nativeObj = new NativeObject(name, obj, methodHandler);

lock (_registrationSyncRoot)
Expand All @@ -36,8 +36,9 @@ public bool Register(object obj, string name, MethodCallHandler methodHandler =
}

_registeredObjects.Add(name, nativeObj);

if (_browser != null)

// If the browser is loading the object will be registered by CommonBrowserAdapter.HandleLoadEnd
if (_browser != null && !_browser.IsLoading)
{
SendRegistrationMessage(nativeObj);
}
Expand Down
Loading