Page 1 of 1

global variant in delphi function registered in js

Posted: Wed May 08, 2019 12:57 pm
by coater
why js cannot obtain new value of global variant?
click button2 always show '' even button1 is clicked;

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls,Generics.Collections,
System.StrUtils, Masks,

uCEFWindowParent, uCEFChromium, uCEFConstants, uCEFApplication, uCEFInterfaces,
uCEFTypes, uCEFProcessMessage, uCEFMiscFunctions, uCEFSchemeRegistrar, uCEFRenderProcessHandler,
uCEFv8Handler, uCEFDomVisitor, uCEFDomNode, uCEFTask;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
CEFWindowParent1: TCEFWindowParent;
Chromium1: TChromium;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
myvariant:string;
procedure CreateGlobalCEFApp; //initialize
implementation
uses unit2, uCEFStringMultimap, DateUtils; // TCefStringMultimapOwn : uCEFStringMultimap
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
myvariant:='Unita1';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//
Chromium1.Browser.MainFrame.ExecuteJavaScript('TMyExtension.StringNameToVariant("1")', Chromium1.browser.MainFrame.GetURL (), 0 );

end;

procedure GlobalCEFApp_OnWebKitInitialized;
begin

// Registering the extension. Read this document for more details :
// https://bitbucket.org/chromiumembedded/ ... gration.md
TCefRTTIExtension.Register('MyJavaExtension', TMyExtension);
end;

procedure CreateGlobalCEFApp; //use , uCEFApplication
begin

GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.OnWebKitInitialized := GlobalCEFApp_OnWebKitInitialized;

end;


end.
----------

unit Unit2;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TMyExtension = class
class function StringNameToVariant(const AStringName : string): string;
end;

implementation
uses unit1;
class function TMyExtension.StringNameToVariant(const AStringName : string): string;
begin
Result:=myvariant;
showmessage(myvariant); //not 'Unita1' but ''(beginning value)
end;
end.

Re: global variant in delphi function registered in js

Posted: Wed May 08, 2019 1:19 pm
by salvadordf
Hi,

myvariant is declared in Unit1 which is used in the browser process but JavaScript and the extension is executed in the render process.

Read this for more information about the multi-process architecture in Chromium :
http://www.chromium.org/developers/desi ... chitecture
https://bitbucket.org/chromiumembedded/ ... ntegration

Re: global variant in delphi function registered in js

Posted: Fri May 10, 2019 2:48 am
by coater
Thank you very much!
If it is possible to use global variant in is situation? use message?(but variant name may be different)

Re: global variant in delphi function registered in js

Posted: Fri May 10, 2019 7:45 am
by salvadordf
coater wrote: Fri May 10, 2019 2:48 am Thank you very much!
If it is possible to use global variant in is situation? use message?(but variant name may be different)
It's not possible to use a variable declared in a different process. You will read different values.

If you need to share some information you can :
  • Set a value in TCefProcessMessageRef.ArgumentList and send that message to another process. Several demos use those arguments to pass information between processes but there's a limit of a few KB in size https://github.com/salvadordf/CEF4Delph ... l.pas#L379
  • Use a database protected by a mutex.
  • Use WebSockets or any other way to send custom IPC messages

Re: global variant in delphi function registered in js

Posted: Sat May 11, 2019 3:27 pm
by coater
Thank you very much for your kind help!
If I use message to obtain value of the variant, How can I wait and continue until the message was send back(so I can obtain the value of variant)?
keen for your help, thank you!

Re: global variant in delphi function registered in js

Posted: Sat May 11, 2019 3:50 pm
by salvadordf
If you use a message then you need to split your work in several pieces. Each piece would do a part of the work, send a message to the other process and the other process would reply with another message or executing some other JavaScript function.

If your app can't split the work in several phases easily then use a database protected by a mutex.

Re: global variant in delphi function registered in js

Posted: Mon May 13, 2019 8:22 am
by coater
Thank you very much!
Hope there would be some examples in the future, thank you!

Re: global variant in delphi function registered in js

Posted: Wed Jul 10, 2019 1:39 pm
by coater
1. glbal variant:
var

hMutex:THandle; //


2.
procedure TFrm.FormCreate(Sender: TObject);
begin
hMutex:=CreateMutex(nil,false,'Myjs'); //success

end;

3.

javaextension:
class function TMyJavaExtension.FileExistsByJs(const Afile : string): boolean;
var i:integer;
begin
Result:=false;

showmessage(inttostr( WaitForSingleObject(hMutex,INFINITE)));// no message, ----but old cef4 showed message
if WaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0 then
begin
Result:=True;
ReleaseMutex(hMutex);

end else Result:=False;

end;
4.class function TMyJavaExtension.StrV(const AStringName : string): string;
var i:integer;

begin

Result:=inttostr(hMutex); //show '0' after run 'alert(MyJavaExtension.StrV('txt'))'
end;

Re: global variant in delphi function registered in js

Posted: Wed Jul 10, 2019 1:49 pm
by salvadordf
Use process messages to send information between the browser and render processes.

The DOMVisitor, JSRTTIExtension and JSExtension demos show you how to send those messages.

Re: global variant in delphi function registered in js

Posted: Wed Jul 10, 2019 2:13 pm
by coater
:roll:
I think out another way:

Before the use of the function:
var a js variant and set it's value befor using the function. :D

Why new cef4 does't show dialog by showmessage() in jsextention procedure? but old can