码迷,mamicode.com
首页 > 编程语言 > 详细

CEF3 获取Cookie例子 CefCookieManager C++

时间:2017-06-09 00:49:27      阅读:3772      评论:0      收藏:0      [点我收藏+]

标签:nbsp   included   自动   led   httponly   dom   数据   currently   let   

首先从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;

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);

  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 }

 

结束!

CEF3 获取Cookie例子 CefCookieManager C++

标签:nbsp   included   自动   led   httponly   dom   数据   currently   let   

原文地址:http://www.cnblogs.com/wcctml/p/6965349.html

(1)
(2)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!