I'm having some issues when converting a CEF1 project to CEF3, using the latest Delphi Chromium Embedded code in the repo (3.1750.1738). To be brief, I used to have an extension that I would register. Then on the html side, I would use a native call to register a javascript object with the Delphi host. It would store the object and the object's context for later calls. But this doesn't seem to work with DCEF3. I can get the object and it's context. But later when I call context.Enter
, it fails. I am wondering if this has to do with changes in the architecture between cef1 and cef3. I already had to change up the extension a bit to register it in OnWebkitIniitalized
.
Here's my code:
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, cefvcl, ceflib, StdCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
Guest: ICefv8Value;
GuestContext: ICefv8Context;
Browser: TChromium;
procedure BrowserConsoleMessage(Sender: TObject; const browser: ICefBrowser;
const message, source: ustring; line: Integer; out Result: Boolean);
public
function ExtensionExecute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean;
end;
var
MainForm: TMainForm;
BrowserExt: TCefv8HandlerOwn;
implementation
{$R *.dfm}
const
code =
'var cef;'+
'if (!cef)'+
' cef = {};'+
'if (!cef.nes)'+
' cef.nes = {};'+
'(function() {'+
' cef.nes.callNative = function() {'+
' native function callNative();'+
' return callNative.apply(this, arguments);'+
' };'+
'})();';
type
TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
procedure OnWebKitInitialized; override;
end;
TExtension = class(TCefv8HandlerOwn)
protected
function Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean; override;
public
Host: TMainForm;
end;
function TExtension.Execute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean;
begin
Result := Host.ExtensionExecute(name, obj, arguments, retval, exception);
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
o: ICefv8Value;
begin
if GuestContext.Enter then
begin
o := Guest.GetValueByKey('foo');
OutputDebugString(PChar('foo found now: ' + BoolToStr(Assigned(o), True)));
GuestContext.Exit;
end
else
OutputDebugString('GuestContext.Enter failed');
end;
function TMainForm.ExtensionExecute(const name: ustring; const obj: ICefv8Value;
const arguments: TCefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean;
var
o: ICefv8Value;
begin
OutputDebugStringW(PWideChar('Execute: ' + name));
if name = 'callNative' then
begin
GuestContext := TCefv8ContextRef.Current;
Guest := arguments[0];
o := Guest.GetValueByKey('foo');
OutputDebugString(PChar('foo found: ' + BoolToStr(Assigned(o), True)));
end;
Result := True;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Browser := TChromium.Create(Self);
Browser.Options.JavascriptDomPaste := STATE_DISABLED;
Browser.Options.CaretBrowsing := STATE_DISABLED;
Browser.OnConsoleMessage := BrowserConsoleMessage;
Browser.Color := clWhite;
Browser.Align := alClient;
Browser.DefaultUrl := 'http://localhost/test2/home.html';
Browser.Parent := Self;
BrowserExt := TExtension.Create;
TExtension(BrowserExt).Host := Self;
Browser.Load(Browser.DefaultUrl);
end;
procedure TMainForm.BrowserConsoleMessage(Sender: TObject;
const browser: ICefBrowser; const message, source: ustring; line: Integer;
out Result: Boolean);
begin
OutputDebugStringW(PWideChar('[line ' + IntToStr(Line) + '] ' + message));
end;
{ TCustomRenderProcessHandler }
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
inherited;
CefRegisterExtension('v8/nes', code, BrowserExt as ICefV8Handler);
end;
initialization
CefRemoteDebuggingPort := 9000;
CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;
end.
And the javascript code:
console.log('startup');
var guest = {foo:function() {
console.log('foo called');
return 42;
}};
if (typeof(cef) !== "undefined") {
console.log('registering guest');
cef.nes.callNative(guest);
}
else {
console.log('no cef');
}
console.log('startup done');
TMainForm.ExtensionExecute
is triggered and callNative happens. But later when I do Button1Click, the context Enter always returns False.
This is due to the CEF3 architecture changing to multi-thread/process. You can no longer just call code directly but must pass messages back and forth using Browser.SendProcessMessage and handle them with OnProcessMessage handlers.