cef -- CefClient

/*--cef(source=client,no_debugct_check)--*/
class CefClient : public virtual CefBase {
 public:
  ///
  // Return the handler for context menus. If no handler is provided the default
  // implementation will be used.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetContextMenuHandler() {
    return NULL;
  }

  ///
  // Return the handler for dialogs. If no handler is provided the default
  // implementation will be used.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetDialogHandler() {
    return NULL;
  }

  ///
  // Return the handler for browser display state events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetDisplayHandler() {
    return NULL;
  }

  ///
  // Return the handler for download events. If no handler is returned downloads
  // will not be allowed.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetDownloadHandler() {
    return NULL;
  }

  ///
  // Return the handler for drag events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetDragHandler() {
    return NULL;
  }

  ///
  // Return the handler for find result events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetFindHandler() {
    return NULL;
  }

  ///
  // Return the handler for focus events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetFocusHandler() {
    return NULL;
  }

  ///
  // Return the handler for geolocation permissions requests. If no handler is
  // provided geolocation access will be denied by default.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetGeolocationHandler() {
    return NULL;
  }

  ///
  // Return the handler for JavaScript dialogs. If no handler is provided the
  // default implementation will be used.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetJSDialogHandler() {
    return NULL;
  }

  ///
  // Return the handler for keyboard events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetKeyboardHandler() {
    return NULL;
  }

  ///
  // Return the handler for browser life span events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetLifeSpanHandler() {
    return NULL;
  }

  ///
  // Return the handler for browser load status events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetLoadHandler() {
    return NULL;
  }

  ///
  // Return the handler for off-screen rendering events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetRenderHandler() {
    return NULL;
  }

  ///
  // Return the handler for browser request events.
  ///
  /*--cef()--*/
  virtual CefRefPtr GetRequestHandler() {
    return NULL;
  }

  ///
  // Called when a new message is received from a different process. Return true
  // if the message was handled or false otherwise. Do not keep a reference to
  // or attempt to access the message outside of this callback.
  ///
  /*--cef()--*/
  virtual bool OnProcessMessageReceived(CefRefPtr browser,
                                        CefProcessId source_process,
                                        CefRefPtr message) {
    return false;
  }
};

CefClient接口提供对特定于浏览器实例的回调的访问。一个CefClient实例可以在任意数量的浏览器之间共享。重要的回调包括:

  • Handlers for things like browser life span, context menus, dialogs, display notifications, drag events, focus events, keyboard events and more. The majority of handlers are optional. See the documentation in cef_client.h for the side effects, if any, of not implementing a specific handler.

所有的Handler,例如浏览器的生命周期,上下文菜单,对话框,显示通知,拖动事件,焦点事件,键盘事件等。大多数处理程序是可选的。请参阅cef_client.h中的文档,以了解不实施特定处理程序的副作用(如果有)。

  • OnProcessMessageReceived which is called when an IPC message is received from the render process. See the “Inter-Process Communication” section for more information.

从渲染过程中接收到IPC消息时调用的OnProcessMessageReceived。有关更多信息,请参见“进程间通信”部分。

特定浏览器实例,实际上,指的是以下过程产生的浏览器实例:

    CefRefPtr browser_view = CefBrowserView::CreateBrowserView(
        handler, url, browser_settings, nullptr, nullptr,
        new SimpleBrowserViewDelegate());
// 或
    CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,

通过上述两种方式创建的浏览器实例,是一个概念上的实例,并不是指你能看得到的浏览器的窗口,窗口只是浏览器实例的宿主而已。而浏览器中发生的事件,例如:生命周期的变化,对话框等,都只会通过CefClient中返回的各种类型Handler以及这些Handler接口实例提供的方法回调。

在CefClient中各种回调的事件,本质上发生的地方是渲染进程。因为每当一个浏览器实例(不是浏览器进程)创建的时候,会有一个对应的渲染进程创建(也可能由于配置,而共用一个,这里先认为默认多个一对一)。渲染进程中发生的各种V8事件、下载事件,显示事件等触发后,会通过进程间通讯给到浏览器进程,然后在浏览器进程中找到与之相关的CefClient,然后从CefClient中找到对应的Handler,回调Handler对应的方法。

也就是说,将在渲染进程发生的事件,用在浏览器进程中的CefClient一定的抽象映射,而不是直接在浏览器进程处理器中进行,因为一个浏览器进程可能会创建多个渲染进程,让CefClient作为中间层避免耦合。

CefClient实例与浏览器实例可以不是一一对应的,多个浏览器实例可以共享一个CefClient,如此一来我们也可以总结关于CefClient的一点:非必要情况,不要编写具有状态的CefClient。

重写CefClient

class CCefClientHandler : public CefClient,public CefDisplayHandler,public CefLifeSpanHandler,public CefLoadHandler,public CefRequestHandler, public CefContextMenuHandler
{
public:
    CCefClientHandler();
    ~CCefClientHandler();

    // CefClient methods:
    virtual CefRefPtr GetDisplayHandler() override;

    virtual CefRefPtr GetLifeSpanHandler() override;
    
    virtual CefRefPtr GetLoadHandler() override;

    virtual CefRefPtr GetRequestHandler() override;

    virtual CefRefPtr GetContextMenuHandler() override{
        return this;
    }

    // ----------------CefDisplayHandler methods:-------------------
    virtual void OnTitleChange(CefRefPtr browser,const CefString& title) override;

    //---------------- CefLifeSpanHandler methods:----------------------------
    virtual bool OnBeforePopup(CefRefPtr browser, CefRefPtr frame, const CefString& target_url, const CefString& target_frame_name,
        CefLifeSpanHandler::WindowOpenDisposition target_disposition, bool user_gesture, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr& client, CefBrowserSettings& settings, bool* no_javascript_access) override;

    virtual void OnAfterCreated(CefRefPtr browser) override;
    virtual bool DoClose(CefRefPtr browser) override;
    virtual void OnBeforeClose(CefRefPtr browser) override;

    // ----------------CefLoadHandler methods:---------------------------
    virtual void OnLoadStart(CefRefPtr browser, CefRefPtr frame) override;
    virtual void OnLoadEnd(CefRefPtr browser,CefRefPtr frame,int httpStatusCode) override;
    virtual void OnLoadError(CefRefPtr browser,CefRefPtr frame,ErrorCode errorCode,const CefString& errorText,const CefString& failedUrl) override;
    //-----------------

    //菜单处理  
    virtual void OnBeforeContextMenu(CefRefPtr browser, CefRefPtr frame,CefRefPtr params, CefRefPtr model) override;

    virtual bool OnContextMenuCommand(CefRefPtr browser, CefRefPtr frame,CefRefPtr params, int command_id, EventFlags event_flags) override;

    void CloseHostBrowser(CefRefPtrbrowser, bool force_close) ;
    // Request that all existing browser windows close.
    void CloseAllBrowsers(bool force_close);

    bool IsClosing() const;


    void ShowDevelopTools(CefRefPtr browser,const CefPoint& inspect_element_at);
    void CloseDevelopTools(CefRefPtr browser);
};

你可能感兴趣的:(cef)