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.

Recreate browser with new context

Post Reply
andrelfc
Posts: 4
Joined: Wed Mar 13, 2024 2:11 pm
Location: Brasil

Recreate browser with new context

Post by andrelfc »

Hello,

I'm using Chromium to retrieve fiscal notes from a government website. This process requires a digital certificate, which I've implemented successfully. However, I need to switch certificates to retrieve notes from different accounts. When I try to destroy the browser and recreate it with a new context to handle different certificates, I encounter an access violation error.

I need assistance with managing browser contexts effectively. I want to destroy and recreate them to seamlessly change certificates between sessions.

Here's the existing creation code:

Code: Select all

    if GlobalCEFApp = nil then
         CreateGlobalCEFApp;

    inherited Create(AOwner);

   pvTimerInicializacao := TTimer.Create(Self);
   pvTimerInicializacao.Enabled := False;
   pvTimerInicializacao.Interval := 300;
   pvTimerInicializacao.OnTimer := pvTimerInicializacaoTimer;
   CreateBrowser(pvCEFWindow, pvWindowName, TCefRequestContextRef.New('', '', '', False, False ,False))
Here's the existing timer event handler:

Code: Select all

procedure TConsisaChromium.pvTimerInicializacaoTimer(Sender: TObject);
begin
    pvTimerInicializacao.Enabled := False;
    if not(inherited CreateBrowser(pvCEFWindow, pvWindowName)) then
        pvTimerInicializacao.Enabled := True;
end;
Here's the existing destroy code:

Code: Select all

    CloseAllConnections(False);

     while not(pvConexoesFinalizadas) do
         Application.ProcessMessages;

     CloseBrowser(True);

     while not(pvBrowserDestruido) do
         Application.ProcessMessages;

     if pvDevTools then
         CloseDevTools;
User avatar
salvadordf
Posts: 4059
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Recreate browser with new context

Post by salvadordf »

Hi,

Use the code from the MDIBrowser demo as a reference for your application.

It's not necessary to use a timer to create the browser. The first CreateBrowser call with the TCefRequestContextRef instance will be enough if you make sure GlobalCEFApp.Initialized is true before trying to create any browser.

The TabbedBrowser2 demo uses the GlobalCEFApp.OnContextInitialized and TForm.OnShow events to enable the user interface when GlobalCEFApp.Initialized is true.

Use a different cache directory if you want to make sure the sessions are independent but all the cache directories must be a subdirectory of GlobalCEFApp.RootCache like this :

Code: Select all

RootCache 
     |
     |--> Cache0001
     |--> Cache0002
     |--> Cache0003
See the MDIBrowser demo : https://github.com/salvadordf/CEF4Delphi/blob/16639d5ea5d5c6d5fddaa4c771504abb4fbb7641/demos/Delphi_VCL/MDIBrowser/uChildForm.pas#L177

Use the code in the MDIBrowser demo to destroy all the browsers correctly before closing the main form. Read the code comments about the destruction steps :
https://github.com/salvadordf/CEF4Delphi/blob/16639d5ea5d5c6d5fddaa4c771504abb4fbb7641/demos/Delphi_VCL/MDIBrowser/uMainForm.pas#L73
https://github.com/salvadordf/CEF4Delphi/blob/16639d5ea5d5c6d5fddaa4c771504abb4fbb7641/demos/Delphi_VCL/MDIBrowser/uChildForm.pas#L66

Avoid the Application.ProcessMessages calls.
andrelfc
Posts: 4
Joined: Wed Mar 13, 2024 2:11 pm
Location: Brasil

Re: Recreate browser with new context

Post by andrelfc »

I'm using an in-memory cache. When I switch to a physical cache, the webpage detects my browser as a bot and block me on CAPTCHAs.
andrelfc
Posts: 4
Joined: Wed Mar 13, 2024 2:11 pm
Location: Brasil

Re: Recreate browser with new context

Post by andrelfc »

Using MDIBrowser demo to access the page, even with the incognito checked it works as i need, i've tryed to implement passing a new context with cache folder empty every CreateBrowser call but when i call CreateBrowser for the seccond time my application got Access Violation.
User avatar
salvadordf
Posts: 4059
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Recreate browser with new context

Post by salvadordf »

Try enabling the Chrome runtime mode in the MDIBrowser demo and set different cache directories for each new session.

Set GlobalCEFApp.ChromeRuntime to True and destroy CEFWindowParent1 in FormCloseQuery as you can see in the MiniBrowser demo :
https://github.com/salvadordf/CEF4Delphi/blob/16639d5ea5d5c6d5fddaa4c771504abb4fbb7641/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L324
https://github.com/salvadordf/CEF4Delphi/blob/16639d5ea5d5c6d5fddaa4c771504abb4fbb7641/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas#L1407

Perhaps this way you will avoid the CAPTCHAs.
Post Reply