• CEF框架:c++和JS交互走过的坑


    网上有许多教你如何进行使用JS调用C++的教程,但是大多数都是交代的不是十分清晰,这里主要讲我遇到的坑。主要以cefsimple来讲。

    我的目录大致为:

    cef1.png

    1如何开启一个多线程

    只有CEF框架中开启一个子线程,才能完成C++和JavaScript之间的交互,而开启线程只需要调用一下这个函数——CefExecuteProcess即可生成一个子线程。但是这个函数什么时候调用,和在哪里调用,和怎么调用,网上说的一点也不清晰。

    在cefsimple中cefsimple_win.cc中就存在了着这个方法,所以我们只需要修改一下这个方法即可

    //大概在48行左右
      // 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框架有许多子进程,这个方法主要是检测是否有子进程,如果有子进程,就执行子进程
      int exit_code = CefExecuteProcess(main_args, nullptr, sandbox_info);
    

    这个方法——CefExecuteProcess,可以先不看,

    CEF_GLOBAL int CefExecuteProcess(const CefMainArgs& args,
                                     CefRefPtr<CefApp> application,
                                     void* windows_sandbox_info) {
      const char* api_hash = cef_api_hash(0);
      if (strcmp(api_hash, CEF_API_HASH_PLATFORM)) {
        // The libcef API hash does not match the current header API hash.
        //当前API的hash值和当前进程的头APIhash不匹配,退出开启进程。
        NOTREACHED();
        return 0;
      }
    
      // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
    
      // Unverified params: application, windows_sandbox_info(一个检测信息)
    
    
      // Execute
      int _retval = cef_execute_process(&args, CefAppCppToC::Wrap(application),
                                        windows_sandbox_info);
    
      // Return type: simple
      return _retval;
    }
    

    重要的是这个方法的第二个参数是一个CefApp,也就是我们所要执行子进程对象,所以我们可以先不管它,直接创建子进程对象放上先。大概操作为如下:

    CefRefPtr<csubclassHandler> appa(new csubclassHandler);//csubclassHandler这个是我实现了CefApp的类
    int exit_code = CefExecuteProcess(main_args, appa.get() , nullptr);
    

    这就完成了开启线程的操作,是不是很简单,哈哈哈哈

    重写GetRenderProcessHandler()这个方法

    //subhandler.h_>
    
    #include "include/cef_app.h"
    
    class csubclassHandler :
    public CefApp,
    public CefRenderProcessHandler
    {
    
    public:
        csubclassHandler();
        ~csubclassHandler();
    
        /****/
       //这个方法一定要重写,不然的话browser对象不能收到JS发来的请求
        virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() OVERRIDE {
        return this;
      } 
        /***/
        
    
        void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE;
      private:
    	  IMPLEMENT_REFCOUNTING(csubclassHandler);
    };
    

    剩下的操作跟着官方文档和博客都能完成相应的操作。

    这是我的小demo:

    git@github.com:whllow/C-.git

  • 相关阅读:
    电脑磁盘分区助手:DiskGenius磁盘管理与数据恢复软件
    python安装第三方的包
    教你成为全栈工程师(Full Stack Developer) 四十五-一文读懂hadoop、hbase、hive、spark分布式系统架构
    北半球 自己动手做聊天机器人
    [08] 请求、会话、上下文属性比较
    [07] ServletContext上下文对象
    [06] Session实现机制以及和Cookie的区别
    [05] Session概要
    [04] Cookie概念和基本使用
    [03] Servlet继承关系和生命周期
  • 原文地址:https://www.cnblogs.com/whllow/p/13395981.html
Copyright © 2020-2023  润新知