天天看點

CEF3 筆記

CEF(Chromium Embedded Framework) 是什麼?

CEF 的官網介紹的很簡潔:A simple framework for embedding chromium browser windows in other applications. 具體地說就是一個可以将浏覽器功能(頁面渲染,JS 執行)嵌入到其他應用程式的架構。

如果你對上面這句話不是特别了解,可以看看這篇文章對 CEF 的介紹。如果你對 CEF 的底層感興趣,建議你去這裡,這裡,這裡,還有這裡看看。

CEF 的應用場景

CEF 作為嵌入式浏覽器架構最适合的應用場景應該是 HTML 頁面渲染,是以很多程式都基于 CEF 來為應用程式提供 HTML 頁面渲染的功能,如有道筆記,Evernote,GitHub Window Client,Q+,Adobe Brackets 等。此外還有一些基于 Web 的桌面應用也使用了 CEF,更多的應用你可以 Google 一下。

CEF 的好處和優點

基于 Chrome,開源,穩定,跨平台,性能高,新特性多,支援很多 HTML5 特性,等等等。

CEF 的缺陷

太太太太大。。。。動辄幾十M的動态庫,一個小程式打包後也有20多M,受不了 ;-(

CEF3 作為一個基于 Chromium 的嵌入式浏覽器架構為開發者提供了幾個基本的接口類來完成一些基本功能。

CefApp 類介紹

CefApp: 與程序,指令行參數,代理,資源管理相關的回調類,用于讓 CEF3 的調用者們定制自己的邏輯。與該類相關的幾個函數如下:

int CefExecuteProcess(const CefMainArgs& args, CefRefPtr<CefApp> application);      
bool CefInitialize(const CefMainArgs& args, const CefSettings& settings,
                   CefRefPtr<CefApp> application);      
CefExecuteProcess() 和 CefInitialize() 都可以将 CefApp 類的一個對象做為參數傳遞進來。

另外,CefApp 的主要接口如下,其中 OnBeforeCommandLineProcessing 函數在你的程式啟動 CEF 和 Chromium 之前給你提供了修改指令行參數的機會;
OnRegisterCustomSchemes 用于注冊自定義 schemes,剩下的接口用于傳回相關回調函數的 Handler。      
CEF3 筆記
// Provides an opportunity to view and/or modify command-line arguments before
  // processing by CEF and Chromium. The |process_type| value will be empty for
  // the browser process. Do not keep a reference to the CefCommandLine object
  // passed to this method. The CefSettings.command_line_args_disabled value
  // can be used to start with an empty command-line object. Any values
  // specified in CefSettings that equate to command-line arguments will be set
  // before this method is called. Be cautious when using this method to modify
  // command-line arguments for non-browser processes as this may result in
  // undefined behavior including crashes.
  virtual void OnBeforeCommandLineProcessing(
      const CefString& process_type,
      CefRefPtr<CefCommandLine> command_line) {
  }

  // Provides an opportunity to register custom schemes. Do not keep a reference
  // to the |registrar| object. This method is called on the main thread for
  // each process and the registered schemes should be the same across all
  // processes.
  virtual void OnRegisterCustomSchemes(
      CefRefPtr<CefSchemeRegistrar> registrar) {
  }

  // Return the handler for resource bundle events. If
  // CefSettings.pack_loading_disabled is true a handler must be returned. If no
  // handler is returned resources will be loaded from pack files. This method
  // is called by the browser and render processes on multiple threads.
  virtual CefRefPtr<CefResourceBundleHandler> GetResourceBundleHandler() {
    return NULL;
  }

  // Return the handler for functionality specific to the browser process. This
  // method is called on multiple threads in the browser process.
  virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() {
    return NULL;
  }

  // Return the handler for functionality specific to the render process. This
  // method is called on the render process main thread.
  virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() {
    return NULL;
  }      
CEF3 筆記

CefClient 類介紹

CefClient: 回調管理類,該類的對象作為參數可以被傳遞給CefCreateBrowser() 或者 CefCreateBrowserSync() 函數。該類的主要接口如下:

CEF3 筆記
// Return the handler for context menus. If no handler is provided the default
  // implementation will be used.
  virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() {
    return NULL;
  }

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

  // Return the handler for browser display state events.
  virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() {
    return NULL;
  }

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

  // Return the handler for focus events.
  virtual CefRefPtr<CefFocusHandler> GetFocusHandler() {
    return NULL;
  }

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

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

  // Return the handler for keyboard events.
  virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() {
    return NULL;
  }

  // Return the handler for browser life span events.
  virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() {
    return NULL;
  }

  // Return the handler for browser load status events.
  virtual CefRefPtr<CefLoadHandler> GetLoadHandler() {
    return NULL;
  }

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

  // Return the handler for browser request events.
  virtual CefRefPtr<CefRequestHandler> 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.
  virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
                                        CefProcessId source_process,
                                        CefRefPtr<CefProcessMessage> message) {
    return false;
  }      
CEF3 筆記

CefClient 中傳回的回調類包括:

CefContextMenuHandler,回調類,主要用于處理 Context Menu 事件。

CefDialogHandler,回調類,主要用來處理對話框事件。

CefDisplayHandler,回調類,處理與頁面狀态相關的事件,如頁面加載情況的變化,位址欄變化,标題變化等事件。

CefDownloadHandler,回調類,主要用來處理檔案下載下傳。

CefFocusHandler,回調類,主要用來處理焦點事件。

CefGeolocationHandler,回調類,用于申請 geolocation 權限。

CefJSDialogHandler,回調類,主要用來處理 JS 對話框事件。

CefKeyboardHandler,回調類,主要用來處理鍵盤輸入事件。

CefLifeSpanHandler,回調類,主要用來處理與浏覽器生命周期相關的事件,與浏覽器對象的建立、銷毀以及彈出框的管理。

CefLoadHandler,回調類,主要用來處理浏覽器頁面加載狀态的變化,如頁面加載開始,完成,出錯等。

CefRenderHandler,回調類,主要用來處在在視窗渲染功能被關閉的情況下的事件。

CefRequestHandler,回調類,主要用來處理與浏覽器請求相關的的事件,如資源的的加載,重定向等。

CefBrowserHost 類介紹

CefBrowserHost: 該類在浏覽器視窗來看代表了 browser 程序,同時也暴露了與浏覽器視窗相關的接口,該類的方法隻能在 browser 程序中調用,但可以在 browser 程序的任意線程中被調用。該類的主要方法如下:

  • 建立浏覽器對象。

需要傳入的參數包括 CefWindowInfo 對象,CefClient 對象,預設的 URL, 以及浏覽器啟動時參數設定。

public static bool CreateBrowser(const CefWindowInfo& windowInfo,
                       CefRefPtr<CefClient> client, const CefString& url,
                       const CefBrowserSettings& settings);      
public static CefRefPtr<CefBrowser> CreateBrowserSync(
                  const CefWindowInfo& windowInfo,
                  CefRefPtr< CefClient > client,
                  const CefString& url, const CefBrowserSettings& settings);      
  • 請求關閉浏覽器對象。該函數被調用是會觸發 JS 'onbeforeunload' 事件,如果參數 force_close為 false,并且提供了 onbeforeunload 事件的回調函數,則提示使用者是否關閉浏覽器,此時使用者可以選取取消操作。如果 force_close為 true,則直接關閉浏覽器。
public virtual void CloseBrowser(bool force_close)= 0;      
  • 擷取浏覽器對象(在 CefBrowser 類中可以通過調用 GetHost() 擷取與之對應的 CefBrowserHost)
public virtual CefRefPtr< CefBrowser > GetBrowser()= 0;      
  • 擷取 CefClient 對象
public virtual CefRefPtr< CefClient > GetClient()= 0;      
  • 擷取該浏覽器對象的視窗句柄,如果是彈出視窗,則傳回 NULL。
public virtual CefWindowHandle GetOpenerWindowHandle()= 0;      

CefBrowser 類介紹

CefBrowser: 該類代表一個浏覽器對象,在 browser 程序中該類的方法可以被任意線程調用。在 render 程序中隻能在主線程被調用。該類的主要方法包括:

CEF3 筆記
// Returns the browser host object. This method can only be called in the
  // browser process.
  virtual CefRefPtr<CefBrowserHost> GetHost() =0;

  // Returns true if the browser can navigate backwards.
  virtual bool CanGoBack() =0;

  // Navigate backwards.
  virtual void GoBack() =0;

  // Returns true if the browser can navigate forwards.
  virtual bool CanGoForward() =0;

  // Navigate forwards.
  virtual void GoForward() =0;

  // Returns true if the browser is currently loading.
  virtual bool IsLoading() =0;

  // Reload the current page.
  virtual void Reload() =0;

  // Reload the current page ignoring any cached data.
  virtual void ReloadIgnoreCache() =0;

  // Stop loading the page.
  virtual void StopLoad() =0;

  // Returns the globally unique identifier for this browser.
  virtual int GetIdentifier() =0;

  // Returns true if this object is pointing to the same handle as |that|
  // object.
  virtual bool IsSame(CefRefPtr<CefBrowser> that) =0;

  // Returns true if the window is a popup window.
  virtual bool IsPopup() =0;

  // Returns true if a document has been loaded in the browser.
  virtual bool HasDocument() =0;

  // Returns the main (top-level) frame for the browser window.
  virtual CefRefPtr<CefFrame> GetMainFrame() =0;

  // Returns the focused frame for the browser window.
  virtual CefRefPtr<CefFrame> GetFocusedFrame() =0;

  // Returns the frame with the specified identifier, or NULL if not found.
  virtual CefRefPtr<CefFrame> GetFrame(int64 identifier) =0;

  // Returns the frame with the specified name, or NULL if not found.
  virtual CefRefPtr<CefFrame> GetFrame(const CefString& name) =0;

  // Returns the number of frames that currently exist.
  virtual size_t GetFrameCount() =0;

  // Returns the identifiers of all existing frames.
  virtual void GetFrameIdentifiers(std::vector<int64>& identifiers) =0;

  // Returns the names of all existing frames.
  virtual void GetFrameNames(std::vector<CefString>& names) =0;

  // Send a message to the specified |target_process|. Returns true if the
  // message was sent successfully.
  virtual bool SendProcessMessage(CefProcessId target_process,
                                  CefRefPtr<CefProcessMessage> message) =0;      
CEF3 筆記

CefFrame 類介紹

CefFrame: 表示浏覽器視窗中的一個 frame,在 browser 程序中該類的方法可以被任意線程調用(簡單了解就是 Frame 辨別一個頁面,通過該類開發者可以加載某一URL 或者一段 HTML 代碼,擷取頁面的源碼和文本,URL,V8 執行上下文,通路頁面中的 DOM)。該類的主要方法如下:

CEF3 筆記
// True if this object is currently attached to a valid frame.
  virtual bool IsValid() =0;

  // Execute undo in this frame.
  virtual void Undo() =0;

  // Execute redo in this frame.
  virtual void Redo() =0;

  // Execute cut in this frame.
  virtual void Cut() =0;

  // Execute copy in this frame.
  virtual void Copy() =0;

  // Execute paste in this frame.
  virtual void Paste() =0;

  // Execute delete in this frame.
  virtual void Delete() =0;

  // Execute select all in this frame.
  virtual void SelectAll() =0;

  // Save this frame's HTML source to a temporary file and open it in the
  // default text viewing application. This method can only be called from the
  // browser process.
  virtual void ViewSource() =0;

  // Retrieve this frame's HTML source as a string sent to the specified
  // visitor.
  virtual void GetSource(CefRefPtr<CefStringVisitor> visitor) =0;

  // Retrieve this frame's display text as a string sent to the specified
  // visitor.
  virtual void GetText(CefRefPtr<CefStringVisitor> visitor) =0;

  // Load the request represented by the |request| object.
  virtual void LoadRequest(CefRefPtr<CefRequest> request) =0;

  // Load the specified |url|.
  virtual void LoadURL(const CefString& url) =0;

  // Load the contents of |string_val| with the specified dummy |url|. |url|
  // should have a standard scheme (for example, http scheme) or behaviors like
  // link clicks and web security restrictions may not behave as expected.
  virtual void LoadString(const CefString& string_val,
                          const CefString& url) =0;

  // Execute a string of JavaScript code in this frame. The |script_url|
  // parameter is the URL where the script in question can be found, if any.
  // The renderer may request this URL to show the developer the source of the
  // error.  The |start_line| parameter is the base line number to use for error
  // reporting.
  virtual void ExecuteJavaScript(const CefString& code,
                                 const CefString& script_url,
                                 int start_line) =0;

  // Returns true if this is the main (top-level) frame.
  virtual bool IsMain() =0;

  // Returns true if this is the focused frame.
  virtual bool IsFocused() =0;

  // Returns the name for this frame. If the frame has an assigned name (for
  // example, set via the iframe "name" attribute) then that value will be
  // returned. Otherwise a unique name will be constructed based on the frame
  // parent hierarchy. The main (top-level) frame will always have an empty name
  // value.
  virtual CefString GetName() =0;

  // Returns the globally unique identifier for this frame.
  virtual int64 GetIdentifier() =0;

  // Returns the parent of this frame or NULL if this is the main (top-level)
  // frame.
  virtual CefRefPtr<CefFrame> GetParent() =0;

  // Returns the URL currently loaded in this frame.
  virtual CefString GetURL() =0;

  // Returns the browser that this frame belongs to.
  virtual CefRefPtr<CefBrowser> GetBrowser() =0;

  // Get the V8 context associated with the frame. This method can only be
  // called from the render process.
  virtual CefRefPtr<CefV8Context> GetV8Context() =0;
  
  // Visit the DOM document. This method can only be called from the render
  // process.
  virtual void VisitDOM(CefRefPtr<CefDOMVisitor> visitor) =0;      
cef