Page 1 of 1

Separate sessions in tabbed CEF browser

Posted: Tue May 30, 2023 6:15 am
by michal@dorfin.waw.pl
I have tabbed CEF browser. Every tab has it's own TChromium component. Is there any way to separate sessions between tabs so that user can use different logins on the same site.
Scenario:
1. user logs in into web app with LOGIN1
2. user opens another tab - starts the same web app in it, and this web app doesn't "recognize" that the user is logged in - so user can log in using LOGIN2

Is there any way in CEF to acomplish this task within the same application ?

Re: Separate sessions in tabbed CEF browser

Posted: Tue May 30, 2023 8:07 am
by salvadordf
Hi,

Yes. The MDIBrowser has all the code to create several independent browsers.

You have to create a new request context with a different cache directory like this :
https://github.com/salvadordf/CEF4Delphi/blob/9e7da7a03e92f4367b4cbf86f1ff3a56c581afdc/demos/Delphi_VCL/MDIBrowser/uChildForm.pas#L225

Then use that context as a parameter in TChromium.CreateBrowser like this :
https://github.com/salvadordf/CEF4Delphi/blob/9e7da7a03e92f4367b4cbf86f1ff3a56c581afdc/demos/Delphi_VCL/MDIBrowser/uChildForm.pas#L242

Re: Separate sessions in tabbed CEF browser

Posted: Tue May 30, 2023 9:34 am
by michal@dorfin.waw.pl
Great help!
Thank You very much!

Re: Separate sessions in tabbed CEF browser

Posted: Tue May 30, 2023 2:22 pm
by michal@dorfin.waw.pl
One more thing.
Can I safely share this context between TChromium components ?

Let's say I would like to do this:

Code: Select all

myGlobalContext := TCefRequestContextRef.New('', '', '', False, False, False, Chromium1.ReqContextHandler)

Chromium1.CreateBrowser(CEFWindowParent1, '', myGlobalContext);
...
Chromium2.CreateBrowser(CEFWindowParent2, '', myGlobalContext);
...
Chromium3.CreateBrowser(CEFWindowParent3, '', myGlobalContext);
If this is wrong - how can I do this safely ?

Re: Separate sessions in tabbed CEF browser

Posted: Tue May 30, 2023 3:08 pm
by salvadordf
I think it should be OK to use the context like that.

If that doesn't work then call TChromium.ShareRequestContext

If you already have a browser and you need to create a new browser using the same context then do this :

Code: Select all

var
  TempContext : ICefRequestContext;
begin 
  ExistingBrowser.ShareRequestContext(TempContext, NewBrowser.ReqContextHandler);
  NewBrowser.CreateBrowser(NewWindowParent, '', TempContext);
end;

Re: Separate sessions in tabbed CEF browser

Posted: Sat Jun 03, 2023 1:29 pm
by dilfich

Code: Select all

TempContext:   array [0..MaxARR] of ICefRequestContext;
I wonder too, is this approach not correct?
After all, if you do according to the proposed scheme, there will be no access to the context of a particular browser.

Re: Separate sessions in tabbed CEF browser

Posted: Sat Jun 03, 2023 3:37 pm
by salvadordf
I use the TChromium.ShareRequestContext method in BriskBard and it works fine. I haven't tried to save the ICefRequestContext instances but I guess it should work.

Re: Separate sessions in tabbed CEF browser

Posted: Mon Jun 05, 2023 12:00 pm
by dilfich

Code: Select all

myGlobalContext := TCefRequestContextRef.New('', '', '', False, False, False, Chromium1.ReqContextHandler);

Chromium1.CreateBrowser(CEFWindowParent1, '', myGlobalContext);

myGlobalContext:= nil;
myGlobalContext := TCefRequestContextRef.New('', '', '', False, False, False, Chromium1.ReqContextHandler);

Chromium2.CreateBrowser(CEFWindowParent2, '', myGlobalContext);

myGlobalContext:= nil;
myGlobalContext := TCefRequestContextRef.New('', '', '', False, False, False, Chromium1.ReqContextHandler);

Chromium3.CreateBrowser(CEFWindowParent3, '', myGlobalContext);
Isn't it easier this way? :)

If you don't save it, then how to use context references for any interaction?

Code: Select all

 
  try
   TempCallback := TCefCloseAllConnectionsCompletionCallback.Create(nil);
   TempContext[FCefID].CloseAllConnections(TempCallback);
  finally
   TempCallback := nil;
  end;
And if you need to save it, how is it more correct?

Re: Separate sessions in tabbed CEF browser

Posted: Tue Jun 06, 2023 2:54 pm
by salvadordf
dilfich wrote: Mon Jun 05, 2023 12:00 pm And if you need to save it, how is it more correct?
Both are correct if they work ;)