Skip to content

Commit

Permalink
Moved all JSON functions to the TCEFJson class
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
salvadordf committed Jul 17, 2020
1 parent 2b963f0 commit dceb229
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 339 deletions.
17 changes: 11 additions & 6 deletions demos/Delphi_VCL/DOMVisitor/uDOMVisitor.pas
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ implementation
// TChromium.OnConsoleMessage event and we identify the right message thanks to
// the preamble in the message.

// This demos also uses DevTool methods to change the "value" attribute of an
// This demo also uses DevTool methods to change the "value" attribute of an
// INPUT HTML element. Each method is called using the
// TChromium.ExecuteDevToolsMethod function and the results are received in the
// TChromium.OnDevToolsMethodResult event.
Expand Down Expand Up @@ -288,14 +288,19 @@ procedure SimpleNodeSearch(const aDocument: ICefDomDocument; const aFrame : ICef
// known preamble that will be used to identify the message in the
// TChromium.OnConsoleMessage event.

// CEF has some known issues with ICefDomNode.GetValue and ICefDomNode.SetValue
// Use JavaScript if you need to get or set the value of HTML elements.
// NOTE : In case you try to read or write node values using the CEF API
// you should know that ICefDomNode.GetValue and ICefDomNode.SetValue
// only work in text nodes. ICefDomNode.GetElementAttribute returns
// the attribute value specified in the HTML and not the current value.

// It's recommended that you use JavaScript or DevTools methods if
// you need to get or set the value of HTML elements.
// For example, if you want to use the "console trick" and you want
// to get the value of the search box in our forum you would have to
// execute this JavaScript code :
// console.log("DOMVISITOR" + document.getElementById("keywords").value);

TempMessage := 'name:' + quotedstr(TempNode.Name);
TempMessage := 'name:' + TempNode.Name;
TempJSCode := 'console.log("' + CONSOLE_MSG_PREAMBLE + TempMessage + '");';
aFrame.ExecuteJavaScript(TempJSCode, 'about:blank', 0);
end;
Expand Down Expand Up @@ -531,9 +536,9 @@ procedure TDOMVisitorFrm.Chromium1ConsoleMessage(Sender: TObject;
MsgContents := copy(message, succ(length(CONSOLE_MSG_PREAMBLE)), length(message));

if (length(MsgContents) = 0) then
MsgContents := 'The INPUT node has no value'
MsgContents := 'There was an error reading the search box information'
else
MsgContents := 'INPUT node value : ' + quotedstr(MsgContents);
MsgContents := 'Search box information: ' + quotedstr(MsgContents);

PostMessage(Handle, MINIBROWSER_SHOWMESSAGE, 0, 0);
end;
Expand Down
269 changes: 5 additions & 264 deletions source/uCEFChromiumCore.pas
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,6 @@ TChromiumCore = class(TComponent, IChromiumEvents)
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;
Expand Down Expand Up @@ -1104,7 +1094,7 @@ implementation
uCEFDownloadImageCallBack, uCEFCookieManager, uCEFRequestContextHandler,
uCEFCookieVisitor, uCEFSetCookieCallback, uCEFResourceRequestHandler,
uCEFMediaObserver, uCEFMediaRouteCreateCallback ,uCEFDevToolsMessageObserver,
uCEFMediaSinkDeviceInfoCallback;
uCEFMediaSinkDeviceInfoCallback, uCEFJson;

constructor TChromiumCore.Create(AOwner: TComponent);
begin
Expand Down Expand Up @@ -4099,262 +4089,13 @@ function TChromiumCore.UpdateStringListPref(const aBrowser: ICefBrowser; const a
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;

Expand Down
8 changes: 4 additions & 4 deletions source/uCEFDevToolsMessageObserver.pas
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ implementation
{$ELSE}
SysUtils,
{$ENDIF}
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser;
uCEFMiscFunctions, uCEFLibFunctions, uCEFBrowser, uCEFJson;


// ************************************************************
Expand All @@ -108,7 +108,7 @@ function cef_on_dev_tools_message( self : PCefDevToolsMessageObserv

if (TempObject <> nil) and (TempObject is TCEFDevToolsMessageObserverOwn) then
try
TempValue := CefParseJson(message_, message_size);
TempValue := TCEFJson.Parse(message_, message_size);
TCEFDevToolsMessageObserverOwn(TempObject).OnDevToolsMessage(TCefBrowserRef.UnWrap(browser),
TempValue,
TempHandled);
Expand All @@ -133,7 +133,7 @@ procedure cef_on_dev_tools_method_result( self : PCefDevToolsMessage

if (TempObject <> nil) and (TempObject is TCEFDevToolsMessageObserverOwn) then
try
TempValue := CefParseJson(result, result_size);
TempValue := TCEFJson.Parse(result, result_size);
TCEFDevToolsMessageObserverOwn(TempObject).OnDevToolsMethodResult(TCefBrowserRef.UnWrap(browser),
message_id,
success <> 0,
Expand All @@ -156,7 +156,7 @@ procedure cef_on_dev_tools_event( self : PCefDevToolsMessageObserver

if (TempObject <> nil) and (TempObject is TCEFDevToolsMessageObserverOwn) then
try
TempValue := CefParseJson(params, params_size);
TempValue := TCEFJson.Parse(params, params_size);
TCEFDevToolsMessageObserverOwn(TempObject).OnDevToolsEvent(TCefBrowserRef.UnWrap(browser),
CefString(method),
TempValue);
Expand Down
Loading

0 comments on commit dceb229

Please sign in to comment.