1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-06-22 22:17:48 +02:00

Delete the cache and cookies directories in a thread

This commit is contained in:
Salvador Díaz Fau
2018-09-18 15:19:44 +02:00
parent e6d4d7bce5
commit 833d50134a
2 changed files with 116 additions and 36 deletions

View File

@ -251,10 +251,10 @@ type
function ExecuteProcess(const aApp : ICefApp) : integer; function ExecuteProcess(const aApp : ICefApp) : integer;
procedure InitializeSettings(var aSettings : TCefSettings); procedure InitializeSettings(var aSettings : TCefSettings);
function InitializeLibrary(const aApp : ICefApp) : boolean; function InitializeLibrary(const aApp : ICefApp) : boolean;
procedure RenameAndDeleteDir(const aDirectory : string);
function MultiExeProcessing : boolean; function MultiExeProcessing : boolean;
function SingleExeProcessing : boolean; function SingleExeProcessing : boolean;
function CheckCEFLibrary : boolean; function CheckCEFLibrary : boolean;
procedure DeleteDirContents(const aDirectory : string);
procedure RegisterWidevineCDM; procedure RegisterWidevineCDM;
function FindFlashDLL(var aFileName : string) : boolean; function FindFlashDLL(var aFileName : string) : boolean;
procedure ShowErrorMessageDlg(const aError : string); virtual; procedure ShowErrorMessageDlg(const aError : string); virtual;
@ -409,6 +409,16 @@ type
constructor Create(const aCookies : string; aPersistSessionCookies : boolean); constructor Create(const aCookies : string; aPersistSessionCookies : boolean);
end; end;
TCEFDirectoryDeleterThread = class(TThread)
protected
FDirectory : string;
procedure Execute; override;
public
constructor Create(const aDirectory : string);
end;
var var
GlobalCEFApp : TCefApplication = nil; GlobalCEFApp : TCefApplication = nil;
@ -1050,8 +1060,8 @@ begin
try try
if (aApp <> nil) then if (aApp <> nil) then
begin begin
if FDeleteCache then DeleteDirContents(FCache); if FDeleteCache then RenameAndDeleteDir(FCache);
if FDeleteCookies then DeleteDirContents(FCookies); if FDeleteCookies then RenameAndDeleteDir(FCookies);
RegisterWidevineCDM; RegisterWidevineCDM;
@ -1079,39 +1089,44 @@ begin
end; end;
end; end;
procedure TCefApplication.DeleteDirContents(const aDirectory : string); procedure TCefApplication.RenameAndDeleteDir(const aDirectory : string);
{$IFNDEF DELPHI14_UP}
var var
TempRec : TSearchRec; TempOldDir, TempNewDir : string;
{$ENDIF} i : integer;
TempThread : TCEFDirectoryDeleterThread;
begin begin
try try
if (length(aDirectory) > 0) and DirectoryExists(aDirectory) then if (length(aDirectory) = 0) or not(DirectoryExists(aDirectory)) then exit;
TempOldDir := ExcludeTrailingPathDelimiter(aDirectory);
if (Pos(PathDelim, TempOldDir, 1) > 0) and
(length(ExtractFileName(TempOldDir)) > 0) then
begin begin
{$IFDEF DELPHI14_UP} i := 0;
TDirectory.Delete(aDirectory, True);
{$ELSE} repeat
if (FindFirst(aDirectory + '\*', faAnyFile, TempRec) = 0) then inc(i);
TempNewDir := TempOldDir + '(' + inttostr(i) + ')';
until not(DirectoryExists(TempNewDir));
if MoveFileW(PWideChar(TempOldDir + chr(0)), PWideChar(TempNewDir + chr(0))) then
begin begin
try TempThread := TCEFDirectoryDeleterThread.Create(TempNewDir);
repeat {$IFDEF DELPHI14_UP}
if ((TempRec.Attr and faDirectory) <> 0) then TempThread.Start;
begin {$ELSE}
if (TempRec.Name <> '.') and (TempRec.Name <> '..') then TempThread.Resume;
DeleteDirContents(aDirectory + '\' + TempRec.Name) {$ENDIF}
end end
else else
DeleteFile(aDirectory + '\' + TempRec.Name); DeleteDirContents(aDirectory);
until (FindNext(TempRec) <> 0); end
finally else
FindClose(TempRec); DeleteDirContents(aDirectory);
end;
end;
{$ENDIF}
end;
except except
on e : exception do on e : exception do
if CustomExceptionHandler('TCefApplication.DeleteDirContents', e) then raise; if CustomExceptionHandler('TCefApplication.RenameAndDeleteDir', e) then raise;
end; end;
end; end;
@ -2286,4 +2301,30 @@ begin
end; end;
end; end;
// TCEFDirectoryDeleterThread
constructor TCEFDirectoryDeleterThread.Create(const aDirectory : string);
begin
inherited Create(True);
FDirectory := aDirectory;
FreeOnTerminate := True;
end;
procedure TCEFDirectoryDeleterThread.Execute;
begin
try
{$IFDEF DELPHI14_UP}
TDirectory.Delete(FDirectory, True);
{$ELSE}
if DeleteDirContents(FDirectory) then RemoveDir(FDirectory);
{$ENDIF}
except
on e : exception do
if CustomExceptionHandler('TCEFDirectoryDeleterThread.Execute', e) then raise;
end;
end;

View File

@ -52,9 +52,9 @@ interface
uses uses
{$IFDEF DELPHI16_UP} {$IFDEF DELPHI16_UP}
{$IFDEF MSWINDOWS}WinApi.Windows, WinApi.ActiveX,{$ENDIF} System.Classes, System.SysUtils, System.UITypes, System.Math, {$IFDEF MSWINDOWS}WinApi.Windows, WinApi.ActiveX,{$ENDIF} System.IOUtils, System.Classes, System.SysUtils, System.UITypes, System.Math,
{$ELSE} {$ELSE}
{$IFDEF MSWINDOWS}Windows, ActiveX,{$ENDIF} Classes, SysUtils, Controls, Graphics, Math, {$IFDEF MSWINDOWS}Windows, ActiveX,{$ENDIF} {$IFDEF DELPHI14_UP}IOUtils,{$ENDIF} Classes, SysUtils, Controls, Graphics, Math,
{$ENDIF} {$ENDIF}
uCEFTypes, uCEFInterfaces, uCEFLibFunctions, uCEFResourceHandler, uCEFTypes, uCEFInterfaces, uCEFLibFunctions, uCEFResourceHandler,
uCEFRegisterCDMCallback; uCEFRegisterCDMCallback;
@ -221,6 +221,8 @@ procedure LogicalToDevice(var aRect : TCEFRect; const aDeviceScaleFactor : doubl
function GetScreenDPI : integer; function GetScreenDPI : integer;
function GetDeviceScaleFactor : single; function GetDeviceScaleFactor : single;
function DeleteDirContents(const aDirectory : string) : boolean;
implementation implementation
uses uses
@ -1814,4 +1816,41 @@ begin
Result := GetScreenDPI / 96; Result := GetScreenDPI / 96;
end; end;
function DeleteDirContents(const aDirectory : string) : boolean;
var
TempRec : TSearchRec;
TempPath : string;
begin
Result := True;
try
if (length(aDirectory) > 0) and
DirectoryExists(aDirectory) and
(FindFirst(aDirectory + '\*', faAnyFile, TempRec) = 0) then
try
repeat
TempPath := aDirectory + PathDelim + TempRec.Name;
if ((TempRec.Attr and faDirectory) <> 0) then
begin
if (TempRec.Name <> '.') and (TempRec.Name <> '..') then
begin
if DeleteDirContents(TempPath) then
Result := RemoveDir(TempPath) and Result
else
Result := False;
end;
end
else
Result := DeleteFile(TempPath) and Result;
until (FindNext(TempRec) <> 0) or not(Result);
finally
FindClose(TempRec);
end;
except
on e : exception do
if CustomExceptionHandler('DeleteDirContents', e) then raise;
end;
end;
end. end.