6

I created a browser with TChromium. The TChromium is created dynamically. Facebook access (login) At the end of the process, the component is destroyed. The problem occurs when the component is created again He continues with the previous session (login). I need to clean all cache and cookies. (Force Log out)

Below the code I create the component by:

var
   Chromium: TChromium;
begin
   try
     Chromium := TChromium.Create(nil);
     Chromium.SetParentComponent(Form1);
     Chromium.Align := alClient;
     chromium.Browser.MainFrame.LoadUrl('www.facebook.com');

I destroy it and release memory like this:

FreeAndNil(Chromium)

What should I do?

braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

5

DCEF1:

To delete cookies in DCEF1 wrapper there's the DeleteCookies function in ICefCookieManager manager interface. However, I've tried the following code to delete all cookies, but it always failed to me:

procedure TForm1.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  if not CookieManager.DeleteCookies('', '') then
    ShowMessage('DeleteCookies failed!');
end;

Fortunately, there is another option to delete cookies using this cookie manager. Visit all of them and in the visitor function assign True to the deleteCookie output parameter. Be careful with getting cookie manager, it's created the first time you navigate somewhere (the GetGlobalManager class function is unsafe, it's not properly handled for unexpected result), so be sure you'll use this code after navigation:

procedure TForm1.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  CookieManager.VisitAllCookiesProc(
    function(const name, value, domain, path: ustring; secure, httponly,
      hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
      count, total: Integer; out deleteCookie: Boolean): Boolean
    begin
      deleteCookie := True;
      ShowMessage('A cookie from domain ' + domain + ' will be unmercifully ' +
        'deleted!');
    end
  );
end;

DCEF3:

In DCEF3 wrapper you can use the following. Credit goes to Eric Santos:

type
  CefTask = class(TCefTaskOwn)
    procedure Execute; override;
  end;

procedure CefTask.Execute;
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.Global;
  CookieManager.DeleteCookies('', '');
end;

procedure ClearCookies;
var
  Task: CefTask;
begin
  Task := CefTask.Create;
  CefPostTask(TID_IO, Task);
end;
Community
  • 1
  • 1
TLama
  • 75,147
  • 17
  • 214
  • 392
  • Hello @TLama! You use what version of dcef? – Junior Miguel Vieira Sep 05 '12 at 05:58
  • Hi, I'm using one of the most recent ones from SVN, revision 147. – TLama Sep 05 '12 at 06:02
  • You can get it from the URL [`mentioned here`](http://code.google.com/p/delphichromiumembedded/source/checkout) but you will need to use some of the SVN clients to get it, like [`TortoiseSVN`](http://tortoisesvn.net/) or similar (e.g. [`some portable`](http://stackoverflow.com/q/693498/960757) SVN client). Otherwise you would have to download all the files from the [`source trunk tree`](http://code.google.com/p/delphichromiumembedded/source/browse/#svn%2Ftrunk). – TLama Sep 05 '12 at 06:12
  • Okay and you? I downloaded the source with SVN and not yet tested. When you inform test. Grateful for help – Junior Miguel Vieira Sep 05 '12 at 23:20
  • I'm curious if won't be needed to clear the cache as well (except cookies), but something tells me that it should be enough. I've tested this with StackOverflow and it worked fine. Let me know after when you test it, please. Thanks! – TLama Sep 05 '12 at 23:32
  • @Christian, add [`ceflib`](http://code.google.com/p/delphichromiumembedded/source/browse/trunk/src/ceflib.pas?r=145) to your uses clause. If you already have it, then update Chromium to a recent version (what I would suggest in any case). Anyway, there's a brand [`new version`](http://code.google.com/p/dcef3/) of Chromium, but I haven't time to check what's new there. – TLama Sep 14 '12 at 12:35
  • I just checked again, this class only exists in DCEF3, **not r306**. I'm switching to DCEF3... – Christian Sep 14 '12 at 13:58
  • @TLama Can you update answer for how to use DeleteCookies() in DCEF3? –  Sep 10 '13 at 22:26
  • @Eric, if I'd know how, I would post an answer to [`your question`](http://stackoverflow.com/q/18493820/960757), but unfortunately I don't, and I'm busy right now to study this topic deeper. Sorry. – TLama Sep 10 '13 at 22:30
  • @Eric, thanks! I've kept the original `DeleteCookies` option, because there was originally a bug in DCEF1 wrapper. `ICefCookieManager::DeleteCookies` should work, and maybe already works in recent DCEF1 version (I don't know, but I hope :-). Just a small hint, you don't need to implement methods where you just call `inherited` because they do effectively nothing but calling it's ancestor's methods. – TLama Sep 18 '13 at 12:52
  • @TLama Actually I don't need this now. Because I use Indy. I've perfected it now. Each thread syncs with global cookiemanager. :) –  Sep 18 '13 at 12:54
1

TLama your help was decisive for my project. I am grateful for sharing your experience.

Solved my problem with this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  CookieManager: ICefCookieManager;
begin
  CookieManager := TCefCookieManagerRef.GetGlobalManager;
  CookieManager.VisitAllCookiesProc(
    function(const name, value, domain, path: ustring; secure, httponly,
      hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
      count, total: Integer; out deleteCookie: Boolean): Boolean
    begin
      deleteCookie := True;
      ShowMessage('A cookie from domain ' + domain + ' will be unmercifully ' +
        'deleted!');
    end
  );
end;

hug