TAdvWebBrowser Delete Cookies

How do I delete cookies using the various functions as documented here:

https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2cookiemanager?view=webview2-1.0.1210.39

I can see TAdvWebBrowser has a DeleteCookies function but the implementation seems to be empty?

Hi,

This is currently not exposed, but easily added using the following code snippet:

unit Unit33;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AdvCustomControl,
  AdvWebBrowser, AdvWebBrowser.Win;

const
  IID_ICoreWebView2CookieManagerGUID = '{177CD9E7-B6F5-451A-94A0-5D7A3A4C4141}';
  IID_ICoreWebView2CookieManager: TGUID = IID_ICoreWebView2CookieManagerGUID;

type
  ICoreWebView2CookieManager = interface
    [IID_ICoreWebView2CookieManagerGUID]
    procedure Placeholder_CreateCookie; safecall;
    procedure Placeholder_CopyCookie; safecall;
    procedure Placeholder_GetCookies; safecall;
    procedure Placeholder_AddOrUpdateCookie; safecall;
    procedure DeleteCookie; safecall;
    procedure DeleteCookies; safecall;
    procedure DeleteCookiesWithDomainAndPath; safecall;
    function DeleteAllCookies: HRESULT; stdcall;
  end;

  ICoreWebView2_2 = interface(ICoreWebView2)
    [IID_ICoreWebView2_2GUID]
    procedure Placeholder_add_WebResourceResponseReceived; safecall;
    procedure Placeholder_remove_WebResourceResponseReceived; safecall;
    function NavigateWithWebResourceRequest(request: ICoreWebView2WebResourceRequest): HRESULT; stdcall;
    procedure Placeholder_add_DOMContentLoaded; safecall;
    procedure Placeholder_remove_DOMContentLoaded; safecall;
    function get_CookieManager(var cookieManager: ICoreWebView2CookieManager): HRESULT; safecall;
    procedure Placeholder_get_Environment; safecall;
  end;

  TForm33 = class(TForm)
    AdvWebBrowser1: TAdvWebBrowser;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form33: TForm33;

implementation

{$R *.dfm}

procedure TForm33.Button1Click(Sender: TObject);
var
  w: ICoreWebView2;
  w2: ICoreWebView2_2;
  c: ICoreWebView2Controller;
  cm: ICoreWebView2CookieManager;
begin
  c := ICoreWebView2Controller(AdvWebBrowser1.NativeBrowser);
  if c.get_CoreWebView2(w) = S_OK then
  begin
    if w.QueryInterface(IID_ICoreWebView2_2, w2) = S_OK then
    begin
      if w2.get_CookieManager(cm) = S_OK then
      begin
        if cm.DeleteAllCookies = S_OK then
        begin
          ShowMessage('Cookies Deleted');
        end;
      end;
    end;
  end;
end;

procedure TForm33.FormCreate(Sender: TObject);
begin
  AdvWebBrowser1.Navigate('https://www.tmssoftware.com');
end;

end.

I haven't exposed all functionality, except the DeleteAllCookies.

1 Like

Thank you.