Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Separate sessions in tabbed CEF browser

Post Reply
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Separate sessions in tabbed CEF browser

Post 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 ?
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Separate sessions in tabbed CEF browser

Post 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
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Re: Separate sessions in tabbed CEF browser

Post by michal@dorfin.waw.pl »

Great help!
Thank You very much!
michal@dorfin.waw.pl
Posts: 41
Joined: Sun Feb 05, 2017 8:53 am

Re: Separate sessions in tabbed CEF browser

Post 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 ?
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Separate sessions in tabbed CEF browser

Post 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;
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Separate sessions in tabbed CEF browser

Post 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.
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Separate sessions in tabbed CEF browser

Post 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.
dilfich
Posts: 330
Joined: Thu Nov 30, 2017 1:17 am

Re: Separate sessions in tabbed CEF browser

Post 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?
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Separate sessions in tabbed CEF browser

Post 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 ;)
Post Reply