Cookies on TabControl

Post Reply
w1ld32
Posts: 3
Joined: Wed Feb 12, 2020 10:28 am

Cookies on TabControl

Post by w1ld32 » Wed Feb 12, 2020 10:39 am

Hi everyone. (I will say this right away, I write through a translator, there is very little information in Russian on CEF4). There was already a topic about cookies, but this construction does not work now (cef4delphi also uses the setstoragepath function to set a new path for storing cookies :
https://github.com/salvadordf/CEF4Delphi/blob/dbc4900358d3ac344f38e298c145f44ebb635abb/source/uCEFChromium.pas#L1213), there is no SetSoragePath function. And I need each tab of the TabControl component to use its own Cookies. In the main function, I set the storage location (GlobalCEFApp.Cache : = ' cef\'+CurUser+'\cache';) if you change the CurUser here, then the Cookies also change, how do you do this during the execution of the program?

This bundle doesn't work either

Code: Select all

GlobalCEFApp.RootCache := 'C:\Program Files (x86)\Embarcadero\Studio\20.0\lib\CEF4Delphi-master\bin\cef';

Code: Select all

procedure TChildForm.FormShow(Sender: TObject);
var
  TempContext : ICefRequestContext;
begin
  try
     if MainForm.NewContextChk.Checked then begin
      TempContext := TCefRequestContextRef.New('\User3\cache', '', True, True, True);
     end else
      TempContext := nil;
    Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
  finally
    TempContext := nil;
  end;
end;

User avatar
salvadordf
Posts: 2112
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cookies on TabControl

Post by salvadordf » Wed Feb 12, 2020 1:32 pm

Hi,

The directory used when you create the ICefRequestContext instances must be a subdirectory of GlobalCEFApp.RootCache.

For example, if you set GlobalCEFApp.RootCache with this value :

Code: Select all

GlobalCEFApp.RootCache := 'c:\root_cache'
Then you can use these cache directories when you call TCefRequestContextRef.New :
c:\root_cache\cache1
c:\root_cache\cache2
c:\root_cache\cache3
c:\root_cache\cache4
c:\root_cache\cache5

The Windows user running your application must have write privileges in the cache directories.
The GlobalCEFApp.RootCache property must be set before the GlobalCEFApp.StartMainProcess call in the DPR file.

w1ld32
Posts: 3
Joined: Wed Feb 12, 2020 10:28 am

Re: Cookies on TabControl

Post by w1ld32 » Thu Feb 13, 2020 7:59 am

Thanks for the answer. My attempts have not yet yielded a positive result. Added to the main process :

Code: Select all

begin
  CreateGlobalCEFApp;
  GlobalCEFApp.RootCache := 'c:\root_cache'; // - dir root for example
  if GlobalCEFApp.StartMainProcess then
    begin
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end;

  DestroyGlobalCEFApp;
end.
In the main form :

Code: Select all

procedure CreateGlobalCEFApp;
  var CurUser: string;
 begin
  CurUser := 'User1';    
  GlobalCEFApp                  := TCefApplication.Create;
  GlobalCEFApp.IgnoreCertificateErrors := True;
  GlobalCEFApp.Cache :=  'C:\root_cache\'+CurUser+'\cache';
  GlobalCEFApp.userDataPath  :=  'C:\root_cache\'+CurUser+'\userData';
  GlobalCEFApp.PersistSessionCookies := true;
  GlobalCEFApp.singleProcess := false;
  GlobalCEFApp.MultiThreadedMessageLoop := true;
  GlobalCEFApp.fastUnload := false;
  GlobalCEFApp.OnContextInitialized := GlobalCEFApp_OnContextInitialized;
end;
and when opening a new window, I pass the following:

Code: Select all

procedure TChildForm.FormShow(Sender: TObject);
var
  TempContext : ICefRequestContext;
begin
  try
    if MainForm.NewContextChk.Checked then begin
      TempContext := TCefRequestContextRef.New('User2\cache', '', True, True, True);
      ShowMessage(TempContext.CachePath);
     end else
      TempContext :=TCefRequestContextRef.New('User1\cache', '', True, True, True); // nil;

    Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
  finally
    TempContext := nil;
  end;
end;
Image
directory cache
However, the first window opens with cookies, and the second does not. Now I'm experimenting on the demo "MDIBrowser"
You do not have the required permissions to view the files attached to this post.

User avatar
salvadordf
Posts: 2112
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Cookies on TabControl

Post by salvadordf » Thu Feb 13, 2020 8:18 am

Use absolute paths like this :

Code: Select all

TCefRequestContextRef.New('C:\root_cache\User1\cache', '', True, True, True);

w1ld32
Posts: 3
Joined: Wed Feb 12, 2020 10:28 am

Re: Cookies on TabControl

Post by w1ld32 » Thu Feb 13, 2020 8:39 am

Oh thank you it worked. I thought that when entering the address, root_cache will be substituted automatically.

Post Reply