CEF调试Render进程

首先我们按照CefSample的Demo去模仿自己的工程,然后在通过SimpleApp继承了CefRenderProcessHandler,并且重写了CefRenderProcessHandler的部分函数,结果发现CefSample关于Render进程的代码都没有调用。额。。。怎么回事呢?哦,对了Cef是多进程的,所以我们调试Cef Browser 进程的时候当然不会调用关于Render进程的代码(跨进程了)。所以我们需要把Render进程附加到VS调试器。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

此时理论上就可以调试Cef Render进程了。额。。。结果还是不能?这就奇怪,试着在
CRenderApp::OnContextCreated 弹出MessageBox,结果还是没有弹出来。说明Render进程根本就没有调用我们这里的Render实现的代码。是哪里问题呢???肯定和多进程有关系,于是继续看看CefSample的例子,一步步的整理。此时突然发现CefExecuteProcess,这个函数会调用起子进程。但是只有是Browser进程的时候exit_code才返回-1,所以下面这段代码在如果是Render进程的时候就直接返回了,根本不会调用到我们重写Render进程的地方。

int exit_code = CefExecuteProcess(main_args, NULL, sandbox_info);
	if (exit_code >= 0) {
		// The sub-process has completed so return here.
		return exit_code;
	}

此时有点懵逼了,那么我们该怎么处理Render进程的消息和事件呢???不过可以确定问题就在CefExecuteProcess,所以在仔细查看一下这个函数原型。

///
// This function should be called from the application entry point function to
// execute a secondary process. It can be used to run secondary processes from
// the browser client executable (default behavior) or from a separate
// executable specified by the CefSettings.browser_subprocess_path value. If
// called for the browser process (identified by no "type" command-line value)
// it will return immediately with a value of -1. If called for a recognized
// secondary process it will block until the process should exit and then return
// the process exit code. The |application| parameter may be empty. The
// |windows_sandbox_info| parameter is only used on Windows and may be NULL (see
// cef_sandbox_win.h for details).
///
/*--cef(api_hash_check,optional_param=application,
        optional_param=windows_sandbox_info)--*/
int CefExecuteProcess(const CefMainArgs& args,
                      CefRefPtr<CefApp> application,
                      void* windows_sandbox_info);

有么有新发现?第二个参数是CefApp 而CefApp就是我们重写Browser进程,Render进程的基类。所以既然我们想调试Render进程,那么试着尝试把Render进程App参数传入进去。代码如下:

CefRefPtr<CefApp> g_app = NULL;

bool ExecuteCefProcess(HINSTANCE hist)
{
	CefEnableHighDPISupport();

	CefMainArgs cef_main_args(hist);
	CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
	command_line->InitFromString(::GetCommandLineW());

	//通过启动参数判断进程类型,如果没有type则是Browser进程,type说明可以查看CefExecuteProcess函数说明
	if (!command_line->HasSwitch("type"))
	{
		g_app = new CBrowserApp();
	}

	//如果有type并且value=renderer则为Render进程
	const std::string& process_type = command_line->GetSwitchValue("type");//RenderProcess
	if (process_type == "renderer")
	{
		g_app = new CRenderApp();
	}

	//启动子进程
	int nexit_code = CefExecuteProcess(cef_main_args, g_app.get(), NULL);
	if (nexit_code >= 0)
	{
		return false;
	}
	return true;
}

此时我们在附加进程,调试一下add函数试试。结果如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CAir2/article/details/85282252
CEF
今日推荐