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