6

我用 TChromium 创建了一个浏览器。TChromium 是动态创建的。Facebook 访问(登录) 在流程结束时,组件被销毁。再次创建组件时会出现问题 他继续上一个会话(登录)。我需要清理所有缓存和 cookie。(强制注销)

在代码下方,我通过以下方式创建组件:

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

我销毁它并像这样释放内存:

FreeAndNil(Chromium)

我应该怎么办?

4

2 回答 2

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;
于 2012-09-04T22:26:39.617 回答
1

TLama 您的帮助对我的项目具有决定性意义。我很感谢您分享您的经验。

用这段代码解决了我的问题:

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;

拥抱

于 2012-09-07T06:35:59.887 回答