Pass browser-specific data in OnRenderProcessThreadCreated

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Pass browser-specific data in OnRenderProcessThreadCreated

Postby Willox » Thu Mar 05, 2020 2:16 pm

Hi, I've been running into some limitations with keeping persistent state in a browser's render process.

My goal is to maintain a list of JavaScript methods to be registered either on-demand (MyApp::AddMethod) or on page-load in CefApp::OnContextCreated.

At the moment (pre-site-isolation), I store a list of these methods in my renderer process so that each call to CefApp::OnContextCreated after the original MyApp::AddMethod will immediately re-add all of the methods to the newly created context. This means the functions are always available to JavaScript when loading a new page.

Now, site-isolation messes with this setup because the browser can end up with a new render-process. This render-process will contain an empty list instead of the list of methods that have been created.

I'm happy to co-operate with the site-isolation model, but I'm struggling to find a way to pass this state into the newly created processes in a synchronous manner. I'm not really interested in an asynchronous solution where the render process will send an IPC message to the browser requesting the list.

CefRenderProcessHandler::OnRenderThreadCreated looks like a perfect solution for this as I could pass my list into extra_info and maintain it in the browser process instead of the render process. Sadly, CefBrowserProcessHandler::OnRenderProcessThreadCreated has no information on which browser the render-process is being created for.

My questions:
Is there some part of the API I have completely overlooked that can solve this?
Can CefBrowserProcessHandler::OnRenderProcessThreadCreated be modified to know which browser the render-process is for?
Willox
Newbie
 
Posts: 4
Joined: Thu Mar 05, 2020 1:54 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby magreenblatt » Thu Mar 05, 2020 2:22 pm

You should use the |extra_info| parameter to CreateBrowser which will be passed to CefRenderProcessHandler::OnBrowserCreated in the renderer process.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby Willox » Thu Mar 05, 2020 2:26 pm

magreenblatt wrote:You should use the |extra_info| parameter to CreateBrowser which will be passed to CefRenderProcessHandler::OnBrowserCreated in the renderer process.


The issue with this is that these functions can be added while the browser already exists. I can't change this behaviour as it comes from users and not my own code.

I believe it would be invalid to change the contents of the CefDictionaryValue extra_info points to at a later point in time?
Willox
Newbie
 
Posts: 4
Joined: Thu Mar 05, 2020 1:54 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby magreenblatt » Thu Mar 05, 2020 2:30 pm

Correct, you can't change |extra_info| after the browser is created. But you can send a message to the renderer process with any changes. See here for details.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby Willox » Thu Mar 05, 2020 2:57 pm

I see, there can be one renderer for multiple browsers. That further confuses things.

I already use IPC currently to create the methods. They're created over IPC and kept persistent between page loads using CefRenderProcessHandler::OnContextCreated.

The problem I'm facing is that when site-isolation kicks in and gives me a new renderer-process (and thus, a new instance of my CefApp), the state is lost.

I can easily keep this state in my browser process, but I have no way to hand it to the new renderer-process in the form of an 'extra_info'.

If there's no solution for this I will probably have to keep these method registrations global and have them shared between all of my contexts. This is undesirable but may work.
Willox
Newbie
 
Posts: 4
Joined: Thu Mar 05, 2020 1:54 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby magreenblatt » Thu Mar 05, 2020 3:11 pm

The easiest and most reliable approach is to use the message router instead of adding a bunch of JS functions on the window object. The client can then use window.cefQuery to make async requests that are handled by the main/browser process. You can define a JSON or URI scheme to represent the different capabilities that your application exposes via cefQuery. If necessary (e.g. for backwards compatibility) you can further wrap those cefQuery calls in JS functions, and return an error or Exception if they call something that they don't have access to (again, as determined by the browser process).
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby Willox » Thu Mar 05, 2020 3:21 pm

Thanks for the help. As I said, I can't really change this behaviour for some very old compatibility reasons.

If anybody else has similar issues, my approach will probably be to send my registration IPCs (BrowserIdentifier + Method Names) to every render-process, as well as including all of that data in CefBrowserProcessHandler::OnRenderProcessThreadCreated's extra_info. The render-processes will each be able to know which methods are exposed to which browsers at all times.
Willox
Newbie
 
Posts: 4
Joined: Thu Mar 05, 2020 1:54 pm

Re: Pass browser-specific data in OnRenderProcessThreadCreat

Postby amaitland » Thu Mar 05, 2020 3:28 pm

You can probably disable site isolation trials to simplify things, at least in the short term.

https://bitbucket.org/chromiumembedded/ ... t-52241733
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1291
Joined: Wed Jan 14, 2015 2:35 am


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 39 guests