Different cookie handlers for two instances of chrome in one form
I am using DCEF3 on Delphi XE3.
Task . Create different cookie stores for different TChromium instances.
Problem . I create different ICefCookieManager instances for each TChromium instance and return to the GetCookieManager event.
Sample code:
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Chromium1: TChromium;
Chromium2: TChromium;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Chromium1GetCookieManager(Sender: TObject;
const browser: ICefBrowser; const mainUrl: ustring;
out Result: ICefCookieManager);
procedure Chromium2GetCookieManager(Sender: TObject;
const browser: ICefBrowser; const mainUrl: ustring;
out Result: ICefCookieManager);
private
{ Private declarations }
CookieManager: ICefCookieManager;
CookieManager2: ICefCookieManager;
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure TForm1.FormCreate(Sender: TObject);
begin
CookieManager:=TCefCookieManagerRef.New('C:\Cookies\1');
CookieManager2:=TCefCookieManagerRef.New('C:\Cookies\2');
end;
procedure TForm1.Chromium1GetCookieManager(Sender: TObject;
const browser: ICefBrowser; const mainUrl: ustring;
out Result: ICefCookieManager);
begin
Result:=CookieManager;
end;
procedure TForm1.Chromium2GetCookieManager(Sender: TObject;
const browser: ICefBrowser; const mainUrl: ustring;
out Result: ICefCookieManager);
begin
Result:=CookieManager2;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Chromium1.Load('http://somesite.com');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Chromium2.Load('http://somesite.com');
end;
If I add an event ShowMessage('First')
to Chromium1GetCookieManager
and ShowMessage('Second')
to Chromium2GetCookieManager
, the message "First" is always displayed, regardless of whether I press button1 or button2.
In other words, Chromium1GetCookieManager is called for both instances.
source to share
Problem solved thanks to everyone.
How to solve a problem?
Just turn off the cefsingleprocess variable in your project1.dpr file.
Example:
program Project1;
uses
Forms,
cefvcl,
ceflib,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
CefSingleProcess := False;//this what i'm talking about
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
source to share