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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

about set cookies

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

Re: about set cookies

Post by salvadordf »

Hi,

Cookies are usually stored in the cache directory and you need to set a cache directory to save the cookies.

Most of the demos have the same cache for all the browsers they might create and you can set the cache directory by adding this code line before the GlobalCEFApp.StartMainProcess call in the DPR file.

Code: Select all

GlobalCEFApp.cache := 'c:\MyCacheDirectory';
For the MDIBrowser demo you can set an individual cache directory for each child form in the TChildForm.FormShow procedure, inside the uChildForm.pas file. Modify the context creation with this :

Code: Select all

TempContext := TCefRequestContextRef.New('c:\MyCacheDirectoryForThisChildForm', '', False, False, False, False)
Then run the demo and check the "Create a new request context for new browsers" checkbox before creating child forms.

If you need to store the cookies in a different directory than the cache you will need to add this to the DPR file :

Code: Select all

GlobalCEFApp.cookies := 'c:\MyCookiesDirectory';
In the case of the MDIBrowser, you would also need to add the cookies directory to the browser creation in the TChildForm.FormShow procedure :

Code: Select all

Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext, 'c:\MyCookiesDirectoryForThisChildForm');
PS : Make sure your app has write privileges in the cache and cookies directories.
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: about set cookies

Post by X11 »

Hi!

Code: Select all

GlobalCEFApp.cookies := 'c:\MyCookiesDirectory';

[dcc32 Error] uMiniBrowser.pas(958): E2003 Undeclared identifier: 'cookies'


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

Re: about set cookies

Post by salvadordf »

Hi,

CEF removed the possibility to set a different directory for the cookies recently.

The cookies are saved inside the "cache" directory now.
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: about set cookies

Post by X11 »

Thanx.
I set the path http://prntscr.com/oju98z

But directory is empty http://prntscr.com/oju9ql
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: about set cookies

Post by salvadordf »

Check that the user running your application can create and modify files inside the cache directory.
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: about set cookies

Post by X11 »

Yes.
I can create this directiry. And delete, and edit.
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: about set cookies

Post by salvadordf »

I'm sorry. I meant that the user account in your Windows system must have write privileges in that directory.

Try one of the demos (MiniBroser or SimpleBrowser2) and add this code line before the GlobalCEFApp.StartMainProcess call in the DPR file :

Code: Select all

GlobalCEFApp.cache := 'cache';
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: about set cookies

Post by X11 »

I meant that the user account in your Windows system must have write privileges in that directory.
I understood you.
I execute the program under the user from the administrator's right and I have the right to write to this folder.

Dir is empty
http://prntscr.com/ojwodt

Code: Select all

procedure TMiniBrowserFrm.FormShow(Sender: TObject);
begin
  ShowStatusText('Initializing browser. Please wait...');

  // WebRTC's IP leaking can lowered/avoided by setting these preferences
  // To test this go to https://www.browserleaks.com/webrtc
  Chromium1.WebRTCIPHandlingPolicy := hpDisableNonProxiedUDP;
  Chromium1.WebRTCMultipleRoutes   := STATE_DISABLED;
  Chromium1.WebRTCNonproxiedUDP    := STATE_DISABLED;
//  GlobalCEFApp.PersistSessionCookies := True;
  GlobalCEFApp.Cache := 'cache';//ExtractFilePath(Application.ExeName) + 'Cache\' ;
  GlobalCEFApp.UserDataPath := 'UserData';// ExtractFilePath(Application.ExeName) + 'UserData\';


  // GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser
  // If it's not initialized yet, we use a simple timer to create the browser later.
  if not(Chromium1.CreateBrowser(CEFWindowParent1, '')) then Timer1.Enabled := True;
end;
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: about set cookies

Post by X11 »

I understood.
It is necessary to set the parameters befor the line

Code: Select all

if GlobalCEFApp.StartMainProcess then
So.

That's right:

Code: Select all

  GlobalCEFApp.cache := 'cache';
  GlobalCEFApp.StartMainProcess;
And it is NOT right:

Code: Select all

  GlobalCEFApp.StartMainProcess;
  GlobalCEFApp.cache := 'cache';
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: about set cookies

Post by salvadordf »

That's correct.

Most of the GlobalCEFApp properties need to be set before the GlobalCEFApp.StartMainProcess call because they are used in the CEF initialization inside the GlobalCEFApp.StartMainProcess function.

Just one more thing. GlobalCEFApp.StartMainProcess must be in in a if..then clause unless the application uses a different EXE for the subprocesses.

If your application uses the same EXE for all the CEF processes then the DPR file should have something similar to this :

Code: Select all

  GlobalCEFApp := TCefApplication.Create;
  
  // Set your GlobalCEFApp properties here.

  if GlobalCEFApp.StartMainProcess then
    begin
      Application.Initialize;
      {$IFDEF DELPHI11_UP}
      Application.MainFormOnTaskbar := True;
      {$ENDIF}
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end;

  DestroyGlobalCEFApp;
Post Reply