在将CEF1项目转换为CEF3时,我遇到了一些问题,使用了repo中最新的Delphi Chromium Embedded代码(3.1750.1738)。简而言之,我曾经有一个我会注册的扩展名。然后在html方面,我将使用本机调用向Delphi主机注册javascript对象。它将存储对象和对象的上下文以供以后调用。但这似乎不适用于DCEF3。我可以得到对象和它的上下文。但是后来当我打电话给context.Enter
时,它失败了。我想知道这是否与cef1和cef3之间架构的变化有关。我已经不得不更改扩展名以在OnWebkitIniitalized
中注册它。
这是我的代码:
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.
和javascript代码:
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
被触发并且callNative发生。但是后来当我执行Button1Click时,上下文Enter总是返回False。
答案 0 :(得分:1)
这是由于CEF3架构改为多线程/进程。您不能再直接调用代码,而必须使用Browser.SendProcessMessage来回传递消息,并使用OnProcessMessage处理程序处理它们。