gpt4 book ai didi

delphi - 如何在 freepascal/lazarus 的子进程中实现 cef3 渲染进程处理程序

转载 作者:行者123 更新时间:2023-12-03 15:55:57 47 4
gpt4 key购买 nike

我正在尝试将 fpcef3 渲染进程处理程序实现为子进程:

遵循 fpcef3 github repo 上提供的示例,我已经成功创建了一个渲染进程处理程序子进程:

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 与示例子目录中的 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;

当我启动主应用程序时, Chrome 浏览器会正确显示,但是处理程序不会被调用(但是我可以看到子进程已启动)。初始化处理程序时我做错了什么?

感谢您的帮助!

最佳答案

好吧,终于弄清楚了,在花了这么多时间之后,我想在这里添加一些咒语。

在深入研究了 fpcef3 源代码并单步执行主进程后,我意识到您需要创建一个 ICefApp 应用程序实例并将自定义渲染进程处理程序分配给该应用程序。因此,为了简化我的生活并避免实现 ICefApp 接口(interface),我“劫持”了主进程内部使用的类。更新后的子流程实现现在由以下代码给出:

Program subprocess;

{$mode objfpc}{$H+}

Uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
cef3lib, cef3types, cef3api, cef3own, cef3intf, Handler;

Var
Args : TCefMainArgs;
app : ICefApp;

begin
CefLoadLibrary;
CefRenderProcessHandler := TCustomRenderProcessHandler.Create;
app := TInternalApp.Create;
{$IFDEF WINDOWS}
Args.instance := HINSTANCE();

Halt(cef_execute_process(@Args, CefGetData(app), nil));
{$ELSE}
Args.argc := argc;
Args.argv := argv;

Halt(cef_execute_process(@Args, CefGetData(app), nil));
{$ENDIF}
end.

现在,ICefApp 实例将使用分配给 CefRenderProcessHandler 的渲染进程处理程序。最后请注意如何修改 cef_execute_process() 以将应用程序作为附加参数。

关于delphi - 如何在 freepascal/lazarus 的子进程中实现 cef3 渲染进程处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39431751/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com