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

OnContextCreated may not always be called in renderer process #3823

Open
cp3chao opened this issue Nov 4, 2024 · 9 comments
Open

OnContextCreated may not always be called in renderer process #3823

cp3chao opened this issue Nov 4, 2024 · 9 comments
Labels
bug Bug report help wanted Issue resolution will likely require community contributions

Comments

@cp3chao
Copy link

cp3chao commented Nov 4, 2024

class CefClientApp : public CefApp,
public CefRenderProcessHandler {
public:
virtual CefRefPtr GetRenderProcessHandler()
override { return this; }

// CefRenderProcessHandler methods.
virtual void OnContextCreated(CefRefPtr<CefBrowser> browser,
	CefRefPtr<CefFrame> frame,
	CefRefPtr<CefV8Context> context) override;
     ........................................

}

the class CefClientApp inherits from Class CefRenderProcessHandler,I will register some methods in function OnContextCreated:

void CefClientApp::OnContextCreated(CefRefPtr browser,
CefRefPtr frame,
CefRefPtr context)
{
auto object = context->GetGlobal();
auto externalObj = CefV8Value::CreateObject(nullptr, nullptr);
object->SetValue("external", externalObj, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr cefV8Handler = new CefV8Client();
// the WebExtFunc:: is my defined functions list, the following method is registering the function
for (int i = 0; i < WebExtFunc::NameCount; i++)
{
externalObj->SetValue(WebExtFunc::Names[i], CefV8Value::CreateFunction(WebExtFunc::Names[i], cefV8Handler.get()), V8_PROPERTY_ATTRIBUTE_NONE);
}
}

A very random issue:
Sometimes it's difficult to reproduce even after opening the application many times, and sometimes it's easy to reproduce.
I opened the application multiple times and found that the function list registered under window.external was empty.
image

Expected behavior
Every time the application is opened, the function list in window.external can be registered normally in the renderer process.

Versions (please complete the following information):

  • OS: Windows 11 -11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz 3.00 GHz
  • CEF Version: cef_binary_128.4.9+g9840ad9+chromium-128.0.6613.120
@cp3chao cp3chao added the bug Bug report label Nov 4, 2024
@cp3chao
Copy link
Author

cp3chao commented Nov 4, 2024

@magreenblatt
Copy link
Collaborator

magreenblatt commented Nov 4, 2024 via email

@cp3chao
Copy link
Author

cp3chao commented Nov 5, 2024

Sometimes it's difficult to reproduce even after opening the application many times, and sometimes it's easy to reproduce.
What URL are you loading? Is it the same URL every time?

the URL is a website for our internal services. Yes, it is the same URL every time.

@magreenblatt
Copy link
Collaborator

OnContextCreated will only be called in cases where the frame source contains JavaScript code. When OnContextCreated is not called for you has your URL loaded correctly, and has it loaded/executed JavaScript code?

@cp3chao
Copy link
Author

cp3chao commented Nov 6, 2024

OnContextCreated will only be called in cases where the frame source contains JavaScript code. When OnContextCreated is not called for you has your URL loaded correctly, and has it loaded/executed JavaScript code?

Yes, the URL loaded correctly. The functions that should have been registered in Window. external in function OnContextCreated was not successfully registered, so I suspect that this OnContextCreated function has not been executed.

Functions registered in window.external under normal circumstances:

image

If there is a problem, this window.external is empty:
image

At present, the usage scenario of CEF is for login, and the operation is the same every time the application is opened. Normally, window.external has the function we registered, but in case of problems, it is empty. It's just strange.This problem occurs very randomly.

@magreenblatt
Copy link
Collaborator

magreenblatt commented Nov 6, 2024 via email

@cp3chao
Copy link
Author

cp3chao commented Nov 6, 2024

Do you always create window.external (every call to OnContextCreated) or is it conditional?

yes, when opening my application, the OnContextCreated function should be called, and the window.external should be created every call. So It's just strange that the window.external is empty sometimes.

@cp3chao
Copy link
Author

cp3chao commented Nov 12, 2024

Hi, @magreenblatt
Is there any update? thanks!

@magreenblatt
Copy link
Collaborator

I'll leave this issue open, but it's unlikely to be addressed without clear and reliable reproduction steps.

@magreenblatt magreenblatt changed the title the CefClientApp::OnContextCreated function doesn't seem to have been executed in the Renderer process sometimes OnContextCreated may not always be called in renderer process Nov 13, 2024
@magreenblatt magreenblatt added the help wanted Issue resolution will likely require community contributions label Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report help wanted Issue resolution will likely require community contributions
Projects
None yet
Development

No branches or pull requests

2 participants