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.

Changing the cache

Post Reply
amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

Changing the cache

Post by amiran6543 »

Hello, tell me how you can apply different cookies to one component of Tchrome, for example, I have 2 buttons, clicking opens a page with 1 cache and clicking on another page with another cache.

For example
if not(Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext)) then Timer1.Enabled := True;

procedure TForm1.Timer1Timer(Sender: TObject);
var
TempContext : ICefRequestContext;
TempCache:string;
begin
Timer1.Enabled := False;
TempCache := GlobalCEFApp.RootCache + '\root_cache\user1';
TempContext := TCefRequestContextRef.New(TempCache, '', '', False, False, False) ;
form2.Chromium1.LoadURL('https://ya.ru/');
if not(Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext)) and not(Chromium1.Initialized) then
Timer1.Enabled := True
end;
All the same, only 1 path is applied, it does not react to the rest!
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Changing the cache

Post by salvadordf »

Hi,

The directory structure must be like this :
  • GlobalCEFApp.RootCache must be a writable directory that will be the parent for all the cache directories.
  • Each independent browser will use a different subdirectory of GlobalCEFApp.RootCache.
The result could be something like this :
  • Root cache directory : c:\MyApp\RootCache
  • Cache directory for the first independent browser : c:\MyApp\RootCache\cache1
  • Cache directory for the second independent browser : c:\MyApp\RootCache\cache2
Perhaps the issue that you have is caused by the timer logic. I would suggest that you use the same solution as the MDIBrowser demo.

Use the GlobalCEFApp.OnContextInitialized event and the TForm.OnShow event to enable the interface when GlobalCEFApp.GlobalContextInitialized is true.

After that, you can call TChromium.CreateBrowser without timers.
amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

Re: Changing the cache

Post by amiran6543 »

Thanks to salvadordf, but even in this case it doesn't work, you have to close and reopen the program then it works, how can you implement it without restarting the program?

Code: Select all

procedure TForm1.Image1Click(Sender: TObject);
var
  i: Integer;
  TempContext : ICefRequestContext;
TempCache:string;
begin
 TempCache := GlobalCEFApp.RootCache + '\root_cache\user1';
             TempContext := TCefRequestContextRef.New(TempCache, '', '', False, False, False) ;
         Chromium1.LoadURL('https://ya.ru/');
       Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
         end;

procedure TForm1.Image2Click(Sender: TObject);
var
  i: Integer;
  TempContext : ICefRequestContext;
TempCache:string;
begin
 TempCache := GlobalCEFApp.RootCache + '\root_cache\user2';
             TempContext := TCefRequestContextRef.New(TempCache, '', '', False, False, False) ;
         Chromium1.LoadURL('https://ya.ru/');
       Chromium1.CreateBrowser(CEFWindowParent1, '', TempContext);
         end;

amiran6543
Posts: 15
Joined: Sun Dec 05, 2021 12:07 pm

Re: Changing the cache

Post by amiran6543 »

I still haven't found an answer, how to change the cache right while the program is running?
:(
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Changing the cache

Post by salvadordf »

CEF only allows us to set the cache directory when we call TChromium.CreateBrowser.

That means that each browser has the same cache directory during its lifetime. If we need to use a browser with a different cache directory we have to create a new browser.

If your application needs to switch between several independent browsers you can create a new browser and then hide or destroy the previous browser.

You can use a TPageControl with hidden tabs to do all that. See the TabbedBrowser2 demo.
Post Reply