Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Correct way of sharing data between processes

Post Reply
flydev
Posts: 1
Joined: Thu Oct 06, 2022 6:02 pm

Correct way of sharing data between processes

Post by flydev »

Hello,

I am just asking myself if I am using the correct (best) way to share data between the browser instance and process logic.

Example from a working application I have in prod. I am using mORMot under the hood, with WebSockets to receive data from a server:

Code: Select all

procedure TUptimeStatusCallback.NotifyUptimeStatus(const msg: RawUtf8);
var copyDataStruct : TCopyDataStruct;
begin
  copyDataStruct.dwData := MSG_NEW_CAPTORS;
  copyDataStruct.cbData := Length(msg) * SizeOf(AnsiChar);
  copyDataStruct.lpData := PAnsiChar(msg);
  PostMessage(frmMain.Handle, MSG_NEW_CAPTORS, 0, LPARAM(@copyDataStruct)); // send data to main form, should I send it directly to the browser ?
end;
Then on received message, I set `window.json_data` with `ExecuteJavascriptCode`. I have a doubt as when I read the example JSSharedMemoryProcMessage, messages are handled in `GlobalCEFApp_OnProcessMessageReceived`, but I cannot manage to update `window.json_data` from `GlobalCEFApp_OnProcessMessageReceived`.

Code: Select all

procedure TfrmMain.GetNewCaptors(var aMessage: TMessage);
var p : PCopyDataStruct;
    S: RawUtf8;
begin
  // data is a large JSON string
  p := PCopyDataStruct(aMessage.lParam);
  if (p <> nil) and (p^.dwData = MSG_NEW_CAPTORS) then
  begin
    var S2: TRawByteStringBuffer;
    S2.Append(PAnsiChar(p^.lpData), p^.cbData);
    S2.AsText(S);
    var code := FormatUtf8('window.json_data=''%'';', [S]);
    Chromium1.ExecuteJavaScript(code, '/', 0);
  end;
end;
Everything works, but is there any advice on what I am doing wrong ?
User avatar
salvadordf
Posts: 4057
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Correct way of sharing data between processes

Post by salvadordf »

Hi,

The JavaScript integration is one of the most complex things in CEF.

In case you haven't read this already please take a moment to read it :
https://bitbucket.org/chromiumembedded/cef/wiki/JavaScriptIntegration.md

I would also recommend that you read these code comments :
https://github.com/salvadordf/CEF4Delphi/blob/031eb58ad13aa96c51e837401ef43a5d3c5da244/demos/Delphi_VCL/JavaScript/JSExtension/uJSExtension.pas#L122

The official CEF documentation about TCefSharedProcessMessageBuilderRef is in the CEF code comments and here :
https://cef-builds.spotifycdn.com/docs/107.1/classCefSharedProcessMessageBuilder.html

See the JSSimpleWindowBinding demo to know how to set a value in "window.json_data".
Open the JSEval demo and see the RenderProcessHandler_OnProcessMessageReceivedEvent procedure to know how to get an instance of ICefv8Context in order to set "window.json_data".
Post Reply