1
0
mirror of https://github.com/salvadordf/CEF4Delphi.git synced 2025-07-02 22:26:53 +02:00

Moved all JSON functions to the TCEFJson class

- Added TCEFJson.SaveToFile and TCEFJson.LoadFromFile functions
- Added more code comments to DOMVisitor
- Replaced all the code to save the browser preferences in TChromiumCore with the new TCEFJson functions
This commit is contained in:
Salvador Díaz Fau
2020-07-17 12:56:43 +02:00
parent 2b963f06ba
commit dceb2299e3
7 changed files with 231 additions and 339 deletions

View File

@ -406,16 +406,6 @@ type
function UpdatePreference(const aBrowser: ICefBrowser; const aName : ustring; const aValue : TStringList) : boolean; overload;
function UpdateStringListPref(const aBrowser: ICefBrowser; const aName, aValue : ustring) : boolean;
procedure HandleDictionary(const aDict : ICefDictionaryValue; var aResultSL : TStringList; const aRoot : string);
procedure HandleNull(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleBool(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleInteger(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleDouble(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleString(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleBinary(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleList(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
procedure HandleInvalid(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
function ExecuteUpdateZoomStepTask(aInc : boolean) : boolean;
function ExecuteUpdateZoomPctTask(aInc : boolean) : boolean;
function ExecuteReadZoomTask : boolean;
@ -1104,7 +1094,7 @@ uses
uCEFDownloadImageCallBack, uCEFCookieManager, uCEFRequestContextHandler,
uCEFCookieVisitor, uCEFSetCookieCallback, uCEFResourceRequestHandler,
uCEFMediaObserver, uCEFMediaRouteCreateCallback ,uCEFDevToolsMessageObserver,
uCEFMediaSinkDeviceInfoCallback;
uCEFMediaSinkDeviceInfoCallback, uCEFJson;
constructor TChromiumCore.Create(AOwner: TComponent);
begin
@ -4099,262 +4089,13 @@ begin
end;
end;
procedure TChromiumCore.HandleNull(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : -null-')
else
aResultSL.Add('-null-');
end;
procedure TChromiumCore.HandleBool(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : ' + BoolToStr(aValue.GetBool, true))
else
aResultSL.Add(BoolToStr(aValue.GetBool, true));
end;
procedure TChromiumCore.HandleInteger(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : ' + IntToStr(aValue.GetInt))
else
aResultSL.Add(IntToStr(aValue.GetInt));
end;
procedure TChromiumCore.HandleDouble(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : ' + FloatToStr(aValue.GetDouble))
else
aResultSL.Add(FloatToStr(aValue.GetDouble));
end;
procedure TChromiumCore.HandleString(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : ' + aValue.GetString)
else
aResultSL.Add(aValue.GetString);
end;
procedure TChromiumCore.HandleBinary(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : -binary-')
else
aResultSL.Add('-binary-');
end;
procedure TChromiumCore.HandleList(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey, TempResult : string;
i, j : integer;
TempList : ICefListValue;
TempValue : ICefValue;
TempSL : TStringList;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
TempList := aValue.GetList;
TempSL := TStringList.Create;
i := 0;
j := TempList.GetSize;
TempResult := '(' + inttostr(j) + '){';
while (i < j) do
begin
TempValue := TempList.GetValue(i);
case TempValue.GetType of
VTYPE_NULL : TempResult := TempResult + '-null-,';
VTYPE_BOOL : TempResult := TempResult + BoolToStr(TempValue.GetBool, true) + ',';
VTYPE_INT : TempResult := TempResult + IntToStr(TempValue.GetInt) + ',';
VTYPE_DOUBLE : TempResult := TempResult + FloatToStr(TempValue.GetDouble) + ',';
VTYPE_STRING : TempResult := TempResult + TempValue.GetString + ',';
VTYPE_BINARY : TempResult := TempResult + '-binary-,';
VTYPE_DICTIONARY :
begin
TempSL.Clear;
HandleDictionary(TempValue.GetDictionary, TempSL, '');
TempResult := TempResult + TempSL.CommaText + ',';
end;
VTYPE_LIST :
begin
TempSL.Clear;
HandleList(TempValue, TempSL, '', '');
TempResult := TempResult + TempSL.CommaText + ',';
end;
else TempResult := TempResult + '-invalid-,';
end;
inc(i);
end;
i := length(TempResult);
if (i > 0) and (TempResult[i] = ',') then TempResult := copy(TempResult, 1, pred(i));
TempResult := TempResult + '}';
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : ' + TempResult)
else
aResultSL.Add(TempResult);
TempSL.Free;
end;
procedure TChromiumCore.HandleInvalid(const aValue : ICefValue; var aResultSL : TStringList; const aRoot, aKey : string);
var
TempKey : string;
begin
if (aRoot <> '') then
TempKey := aRoot + '.' + aKey
else
TempKey := aKey;
if (length(TempKey) > 0) then
aResultSL.Add(TempKey + ' : -invalid-')
else
aResultSL.Add('-invalid-');
end;
procedure TChromiumCore.HandleDictionary(const aDict : ICefDictionaryValue; var aResultSL : TStringList; const aRoot : string);
var
TempKeys : TStringList;
i, j : integer;
TempValue : ICefValue;
TempNewKey : string;
begin
TempKeys := nil;
try
try
if (aDict <> nil) then
begin
TempKeys := TStringList.Create;
aDict.GetKeys(TempKeys);
i := 0;
j := TempKeys.Count;
while (i < j) do
begin
TempValue := aDict.GetValue(TempKeys[i]);
case TempValue.GetType of
VTYPE_NULL : HandleNull(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_BOOL : HandleBool(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_INT : HandleInteger(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_DOUBLE : HandleDouble(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_STRING : HandleString(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_BINARY : HandleBinary(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_LIST : HandleList(TempValue, aResultSL, aRoot, TempKeys[i]);
VTYPE_DICTIONARY :
begin
if (length(aRoot) > 0) then
TempNewKey := aRoot + '.' + TempKeys[i]
else
TempNewKey := TempKeys[i];
HandleDictionary(TempValue.GetDictionary, aResultSL, TempNewKey);
end;
else
HandleInvalid(TempValue, aResultSL, aRoot, TempKeys[i]);
end;
inc(i);
end;
end;
except
on e : exception do
if CustomExceptionHandler('TChromiumCore.HandleDictionary', e) then raise;
end;
finally
if (TempKeys <> nil) then TempKeys.Free;
end;
end;
function TChromiumCore.doSavePreferences : boolean;
{$IFDEF MSWINDOWS}
var
TempDict : ICefDictionaryValue;
TempPrefs : TStringList;
{$ENDIF}
begin
Result := False;
{$IFDEF MSWINDOWS}
TempPrefs := nil;
Result := Initialized and
TCEFJson.SaveToFile(Browser.Host.RequestContext.GetAllPreferences(True), FPrefsFileName);
try
try
if Initialized then
begin
TempPrefs := TStringList.Create;
TempDict := Browser.Host.RequestContext.GetAllPreferences(True);
HandleDictionary(TempDict, TempPrefs, '');
TempPrefs.SaveToFile(FPrefsFileName);
Result := True;
end;
except
on e : exception do
if CustomExceptionHandler('TChromiumCore.Internal_SavePreferences', e) then raise;
end;
finally
SendCompMessage(CEF_PREFERENCES_SAVED, Ord(Result));
if (TempPrefs <> nil) then FreeAndNil(TempPrefs);
end;
{$IFDEF MSWINDOWS}
SendCompMessage(CEF_PREFERENCES_SAVED, Ord(Result));
{$ENDIF}
end;