Page 1 of 1

Cookies on TabControl

Posted: Wed Feb 12, 2020 10:39 am
by w1ld32
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/CEF4Delph ... .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;

Re: Cookies on TabControl

Posted: Wed Feb 12, 2020 1:32 pm
by salvadordf
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.

Re: Cookies on TabControl

Posted: Thu Feb 13, 2020 7:59 am
by w1ld32
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"

Re: Cookies on TabControl

Posted: Thu Feb 13, 2020 8:18 am
by salvadordf
Use absolute paths like this :

Code: Select all

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

Re: Cookies on TabControl

Posted: Thu Feb 13, 2020 8:39 am
by w1ld32
Oh thank you it worked. I thought that when entering the address, root_cache will be substituted automatically.