CEF3 筆記二(常用類的介紹)
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。
// 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; }
CefClient 類介紹
CefClient: 回撥管理類,該類的物件作為引數可以被傳遞給CefCreateBrowser() 或者 CefCreateBrowserSync() 函式。該類的主要介面如下:
// 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; }
CefClient 中返回的回撥類包括:
CefContextMenuHandler,回撥類,主要用於處理 Context Menu 事件。
CefDialogHandler,回撥類,主要用來處理對話方塊事件。
CefDisplayHandler,回撥類,處理與頁面狀態相關的事件,如頁面載入情況的變化,位址列變化,標題變化等事件。
CefDownloadHandler,回撥類,主要用來處理檔案下載。
CefFocusHandler,回撥類,主要用來處理焦點事件。
CefGeolocationHandler,回撥類,用於申請 geolocation 許可權。
CefJSDialogHandler,回撥類,主要用來處理 JS 對話方塊事件。
CefKeyboardHandler,回撥類,主要用來處理鍵盤輸入事件。
CefLifeSpanHandler,回撥類,主要用來處理與瀏覽器生命週期相關的事件,與瀏覽器物件的建立、銷燬以及彈出框的管理。
CefLoadHandler,回撥類,主要用來處理瀏覽器頁面載入狀態的變化,如頁面載入開始,完成,出錯等。
CefRenderHandler,回撥類,主要用來處在在視窗渲染功能被關閉的情況下的事件。
CefRequestHandler,回撥類,主要用來處理與瀏覽器請求相關的的事件,如資源的的載入,重定向等。