Cef初始化

2018-08-17  本文已影响229人  晓函


//进程开始的时候初始化CEF
bool InitCef(HINSTANCE hInstance){
    //cef初始化
    printf("InitCef\n");

    CefMainArgs main_args(hInstance);

    CefRefPtr<CCefClientApp>app(new CCefClientApp);// CefClientApp 继承了cefapp

    /*======渲染子进程 直接执行命令=========*/
    // CEF applications have multiple sub-processes (render, plugin, GPU, etc)
    // that share the same executable. This function checks the command-line and,
    // if this is a sub-process, executes the appropriate logic.
    //返回值:
    //     尝试执行CEF命令行参数,如果是cef渲染子进程,则执行参数成功,大于等于0。
    //     普通客户端进程由于没有cef命令行参数会直接返回-1
    int exit_code = CefExecuteProcess(main_args, app, NULL);
    if (exit_code >= 0)
    {
        printf("CefExecuteProcess exit code:%d\n", exit_code);
        // The sub-process has completed so return here.
        //子进程完成后会从这里返回
        return false;
    }

    /*========主窗口进程正常初始化=========*/
    // Specify CEF global settings here.
    CefSettings settings;
    CefSettingsTraits::init(&settings);
    settings.single_process = false;                //使用单进程模式
    settings.ignore_certificate_errors = true;      //忽略掉ssl证书验证错误
    settings.log_severity = LOGSEVERITY_ERROR;
    settings.no_sandbox = true;
    //multi_threaded_message_loop=true 这样设置的目的是使cef的browser ui线程和程序的线程分离,使用duilib的消息循环函数
    settings.multi_threaded_message_loop = true;
    settings.command_line_args_disabled = true;
    CefString(&settings.locale).FromASCII("zh-CN");

    TCHAR szSpecialPath[MAX_PATH] = { 0 };

    wstring cache_path = OS::GetAppDataPath() + L"MyCefExplorer\\Webkit\\";
    if (!cache_path.empty())
    {
        StringCbCopy(szSpecialPath, sizeof(szSpecialPath), cache_path.c_str());
        CefString(&settings.cache_path).FromString(szSpecialPath, sizeof(szSpecialPath) / 2, true);
    }


    //初始化
    CefInitialize(main_args, settings, app.get(), NULL);

    //屏蔽flash弹出的CMD,将本进程CMD路径设为当前路径下的dummy_cmd.empty文件
    wstring cmd_path = OS::GetCurrentPath()+ L"\\dummy_cmd.empty";
    SetEnvironmentVariable(L"ComSpec", cmd_path.c_str());

    return true;
}


int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{   
    //初始化CEF
    if (!InitCef(hInstance))
        return 0;

          ……
          各种初始化客户端主窗口的代码,省略...
          ……

         CefShutdown();
          ExitProcess(0);
          return 1;
}


上一篇下一篇

猜你喜欢

热点阅读