CEF 獲取Cookie例子 CefCookieManager C++

簡介

Chromium Embedded Framework (CEF)是由 Marshall Greenblatt 在2008年創辦的開源項目,致力於基於Google Chromium項目開發一個Web控件。 CEF目前已支持多種編程語言和操做系統,能方便地集成到現有或者新的應用程序中,設計上,它追求高性能的同時,也追求易於使用,它的基本框架經過原生庫提供C和C++的編程接口,這些接口將宿主程序與Chromium與WebKit的實現細節隔離,能讓瀏覽器與應用程序無縫集成,並支持自定義插件、協議、Javascript對象與擴展。宿主程序還能根據須要控制資源加載、頁面跳轉、上下文菜單、打印等等。這些好處都是在支持Google Chrome同等效率與HTML5技術可用的基本上提供的。如今不少主流的客戶端都使用了CEF來顯示Web頁面:網易雲音樂、QQ、豌豆莢等(安裝目錄下能夠找到libcef.dll)。chrome

正文

首先從cef_cookie.h 源碼中看到CefCookieManager 這個類:編程

  // Visit all cookies on the IO thread. The returned cookies are ordered by
  // longest path, then by earliest creation date. Returns false if cookies
  // cannot be accessed.
  ///
  /*--cef()--*/
  virtual bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor) =0;

  ///
  // Visit a subset of cookies on the IO thread. The results are filtered by the
  // given url scheme, host, domain and path. If |includeHttpOnly| is true
  // HTTP-only cookies will also be included in the results. The returned
  // cookies are ordered by longest path, then by earliest creation date.
  // Returns false if cookies cannot be accessed.
  ///
  /*--cef()--*/
  virtual bool VisitUrlCookies(const CefString& url,
                               bool includeHttpOnly,
                               CefRefPtr<CefCookieVisitor> visitor) =0;

 

 1 class CefCookieVisitor : public virtual CefBase {
 2  public:
 3   ///
 4   // Method that will be called once for each cookie. |count| is the 0-based
 5   // index for the current cookie. |total| is the total number of cookies.
 6   // Set |deleteCookie| to true to delete the cookie currently being visited.
 7   // Return false to stop visiting cookies. This method may never be called if
 8   // no cookies are found.
 9   ///
10   /*--cef()--*/
11   virtual bool Visit(const CefCookie& cookie, int count, int total,
12                      bool& deleteCookie) =0;
13 };

 

能夠經過VisitAllCookies獲取全部cookies;VisitUrlCookies獲取域名下的全部cookies。瀏覽器

看到VisitUrlCookies的參數是CefCookieVisitor;因此實現一個類用於回調讀取cookies;cookie

class CCookieVisitor : public CefCookieVisitor
{
public:
    CCookieVisitor() {};
    ~CCookieVisitor() {};

    bool Visit(const CefCookie& cookie, int count, int total,
        bool& deleteCookie);
    //這是一個宏 
    //全部的框架類從CefBase繼承,實例指針由CefRefPtr管理,CefRefPtr經過調用AddRef()和Release()方法自動管理引用計數。
    IMPLEMENT_REFCOUNTING(CookieVisitor);
};        
//做爲類的成員變量
CefRefPtr<CCookieVisitor> m_CookieVisitor; m_CookieVisitor(new CCookieVisitor());

 //如下代碼執行 即回調Visit框架

 CefRefPtr<CefCookieManager> cefCookieManager = CefCookieManager::GetGlobalManager(nullptr);dom

  if (cefCookieManager)
  {
    cefCookieManager->VisitUrlCookies(url ,true , m_visitor);
  }編程語言

 

回調進行讀取,count爲當前cookie total爲總數。具體看CefCookieVisitor的註釋,接下來即可以Visit讀取到數據性能

 1 bool CookieVisitor::Visit(const CefCookie & cookie, int count, int total, bool & deleteCookie)
 2 {
 3     if (count == total)
 4     {
 5         return false;
 6     }
 7     if (cookie.name.str && cookie.value.str)
 8     {
 9         string strName = cookie.name.str;
10         string strValue = cookie.value.str;
11     }
12     return true;
13 }

 

備註:CEF遠程調試地址 chrome://inspect/#devices url

結束!spa

相關文章
相關標籤/搜索