我正在尝试将fpcef3渲染处理程序实现为子进程:
通过参考fpcef3的github仓库提供的示例,我已经成功创建了一个渲染处理程序子进程:
Program subprocess;
{$mode objfpc}{$H+}
Uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
cef3lib, cef3types, cef3api, Handler;
Var
Args : TCefMainArgs;
begin
CefLoadLibrary;
CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
{$IFDEF WINDOWS}
Args.instance := HINSTANCE();
Halt(cef_execute_process(@Args, nil, nil));
{$ELSE}
Args.argc := argc;
Args.argv := argv;
Halt(cef_execute_process(@Args, nil, nil));
{$ENDIF}
end.
TCustomRenderProcessHandler
与examples子目录中JavaScript示例提供的处理程序完全相同:
Unit Handler;
{$MODE objfpc}{$H+}
(*
* Everything in here is called from a render process, so there is no access to GUI and all the
* data of the main process.
*)
Interface
Uses
Classes, SysUtils,
cef3types, cef3intf, cef3ref, cef3own, cef3lib;
Type
{ Custom handler for the render process }
TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
protected
// Test Window Bindings
procedure OnContextCreated(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context); override;
// Test Extension
procedure OnWebKitInitialized; override;
end;
TMyHandler = class(TCefv8HandlerOwn)
protected
function Execute(const name: ustring; const obj: ICefv8Value;
const arguments: ICefv8ValueArray; var retval: ICefv8Value;
var exception: ustring): Boolean; override;
end;
Implementation
Var
mystr : String;
{ TMyHandler }
function TMyHandler.Execute(const name : ustring; const obj : ICefv8Value;
const arguments : ICefv8ValueArray; var retval : ICefv8Value;
var exception : ustring) : Boolean;
begin
// return a value
//retval := TCefv8ValueRef.NewString('TMyHandler');
retval := TCefv8ValueRef.NewDate(Now);
Result := True;
end;
{ TCustomRenderProcessHandler }
procedure TCustomRenderProcessHandler.OnContextCreated(const browser : ICefBrowser;
const frame : ICefFrame; const context : ICefv8Context);
Var
myWin : ICefv8Value;
args : ICefv8ValueArray;
begin
myWin := context.GetGlobal;
mystr := 'a test string';
SetLength(args, 1);
args[0] := TCefv8ValueRef.NewString(mystr);
myWin.SetValueByKey('myval', args[0], []);
end;
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
Var
Code: ustring;
begin
Code :=
'var cef;'+
'if (!cef)'+
' cef = {};'+
'if (!cef.test)'+
' cef.test = {};'+
'(function() {'+
' cef.test.__defineGetter__(''test_param'', function() {'+
' native function GetTestParam();'+
' return GetTestParam();'+
' });'+
' cef.test.__defineSetter__(''test_param'', function(b) {'+
' native function SetTestParam();'+
' if(b) SetTestParam(b);'+
' });'+
' cef.test.test_object = function() {'+
' native function GetTestObject();'+
' return GetTestObject();'+
' };'+
'})();';
CefRegisterExtension('example/v8', Code, TMyHandler.Create as ICefv8Handler);
end;
end.
最后,在我的主进程的主表单中,我提供了子进程的路径:
procedure TForm1.FormCreate(Sender: TObject);
begin
CefSingleProcess := False;
//CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
CefBrowserSubprocessPath := 'C:\Users\aludin\fpCEF3-master\Examples\SubProcess\subprocess64.exe'
end;
当我启动主应用程序时,Chromium浏览器正确显示,但是处理程序未被调用(但我可以看到子进程已启动)。 我在初始化处理程序时做错了什么?
谢谢您的帮助!