CEF3: Add CefCookieManager::SetSupportedSchemes method which supports cookie storage for non-http(s) schemes (issue #567).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@577 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2012-04-03 22:14:28 +00:00
parent 825080f86d
commit 0b3ef6e2cd
8 changed files with 158 additions and 24 deletions

View File

@ -55,6 +55,14 @@ typedef struct _cef_cookie_manager_t {
///
cef_base_t base;
///
// Set the schemes supported by this manager. By default only "http" and
// "https" schemes are supported. Must be called before any cookies are
// accessed.
///
void (CEF_CALLBACK *set_supported_schemes)(struct _cef_cookie_manager_t* self,
cef_string_list_t schemes);
///
// Visit all cookies. The returned cookies are ordered by longest path, then
// by earliest creation date. Returns false (0) if cookies cannot be accessed.

View File

@ -39,6 +39,7 @@
#pragma once
#include "include/cef_base.h"
#include <vector>
class CefCookieVisitor;
@ -64,6 +65,14 @@ class CefCookieManager : public virtual CefBase {
/*--cef(optional_param=path)--*/
static CefRefPtr<CefCookieManager> CreateManager(const CefString& path);
///
// Set the schemes supported by this manager. By default only "http" and
// "https" schemes are supported. Must be called before any cookies are
// accessed.
///
/*--cef()--*/
virtual void SetSupportedSchemes(const std::vector<CefString>& schemes) =0;
///
// Visit all cookies. The returned cookies are ordered by longest path, then
// by earliest creation date. Returns false if cookies cannot be accessed.

View File

@ -85,6 +85,33 @@ void CefCookieManagerImpl::Initialize(const CefString& path) {
SetStoragePath(path);
}
void CefCookieManagerImpl::SetSupportedSchemes(
const std::vector<CefString>& schemes) {
if (CEF_CURRENTLY_ON_IOT()) {
if (schemes.empty())
return;
std::set<std::string> scheme_set;
std::vector<CefString>::const_iterator it = schemes.begin();
for (; it != schemes.end(); ++it)
scheme_set.insert(*it);
const char** arr = new const char*[scheme_set.size()];
std::set<std::string>::const_iterator it2 = scheme_set.begin();
for (int i = 0; it2 != scheme_set.end(); ++it2, ++i)
arr[i] = it2->c_str();
cookie_monster_->SetCookieableSchemes(arr, scheme_set.size());
delete [] arr;
} else {
// Execute on the IO thread.
CEF_POST_TASK(CEF_IOT,
base::Bind(&CefCookieManagerImpl::SetSupportedSchemes,
this, schemes));
}
}
bool CefCookieManagerImpl::VisitAllCookies(
CefRefPtr<CefCookieVisitor> visitor) {
if (CEF_CURRENTLY_ON_IOT()) {

View File

@ -19,6 +19,8 @@ class CefCookieManagerImpl : public CefCookieManager {
void Initialize(const CefString& path);
// CefCookieManager methods.
virtual void SetSupportedSchemes(const std::vector<CefString>& schemes)
OVERRIDE;
virtual bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor) OVERRIDE;
virtual bool VisitUrlCookies(const CefString& url, bool includeHttpOnly,
CefRefPtr<CefCookieVisitor> visitor) OVERRIDE;

View File

@ -12,6 +12,7 @@
#include "libcef_dll/cpptoc/cookie_manager_cpptoc.h"
#include "libcef_dll/ctocpp/cookie_visitor_ctocpp.h"
#include "libcef_dll/transfer_util.h"
// GLOBAL FUNCTIONS - Body may be edited by hand.
@ -43,6 +44,27 @@ CEF_EXPORT cef_cookie_manager_t* cef_cookie_manager_create_manager(
// MEMBER FUNCTIONS - Body may be edited by hand.
void CEF_CALLBACK cookie_manager_set_supported_schemes(
struct _cef_cookie_manager_t* self, cef_string_list_t schemes) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
DCHECK(self);
if (!self)
return;
// Verify param: schemes; type: string_vec_byref_const
DCHECK(schemes);
if (!schemes)
return;
// Translate param: schemes; type: string_vec_byref_const
std::vector<CefString> schemesList;
transfer_string_list_contents(schemes, schemesList);
// Execute
CefCookieManagerCppToC::Get(self)->SetSupportedSchemes(
schemesList);
}
int CEF_CALLBACK cookie_manager_visit_all_cookies(
struct _cef_cookie_manager_t* self,
struct _cef_cookie_visitor_t* visitor) {
@ -163,6 +185,7 @@ int CEF_CALLBACK cookie_manager_set_storage_path(
CefCookieManagerCppToC::CefCookieManagerCppToC(CefCookieManager* cls)
: CefCppToC<CefCookieManagerCppToC, CefCookieManager, cef_cookie_manager_t>(
cls) {
struct_.struct_.set_supported_schemes = cookie_manager_set_supported_schemes;
struct_.struct_.visit_all_cookies = cookie_manager_visit_all_cookies;
struct_.struct_.visit_url_cookies = cookie_manager_visit_url_cookies;
struct_.struct_.set_cookie = cookie_manager_set_cookie;

View File

@ -12,6 +12,7 @@
#include "libcef_dll/cpptoc/cookie_visitor_cpptoc.h"
#include "libcef_dll/ctocpp/cookie_manager_ctocpp.h"
#include "libcef_dll/transfer_util.h"
// STATIC METHODS - Body may be edited by hand.
@ -43,6 +44,28 @@ CefRefPtr<CefCookieManager> CefCookieManager::CreateManager(
// VIRTUAL METHODS - Body may be edited by hand.
void CefCookieManagerCToCpp::SetSupportedSchemes(
const std::vector<CefString>& schemes) {
if (CEF_MEMBER_MISSING(struct_, set_supported_schemes))
return;
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Translate param: schemes; type: string_vec_byref_const
cef_string_list_t schemesList = cef_string_list_alloc();
DCHECK(schemesList);
if (schemesList)
transfer_string_list_contents(schemes, schemesList);
// Execute
struct_->set_supported_schemes(struct_,
schemesList);
// Restore param:schemes; type: string_vec_byref_const
if (schemesList)
cef_string_list_free(schemesList);
}
bool CefCookieManagerCToCpp::VisitAllCookies(
CefRefPtr<CefCookieVisitor> visitor) {
if (CEF_MEMBER_MISSING(struct_, visit_all_cookies))

View File

@ -18,6 +18,7 @@
#pragma message("Warning: "__FILE__" may be accessed wrapper-side only")
#else // USING_CEF_SHARED
#include <vector>
#include "include/cef_cookie.h"
#include "include/capi/cef_cookie_capi.h"
#include "libcef_dll/ctocpp/ctocpp.h"
@ -34,6 +35,8 @@ class CefCookieManagerCToCpp
virtual ~CefCookieManagerCToCpp() {}
// CefCookieManager methods
virtual void SetSupportedSchemes(
const std::vector<CefString>& schemes) OVERRIDE;
virtual bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor) OVERRIDE;
virtual bool VisitUrlCookies(const CefString& url, bool includeHttpOnly,
CefRefPtr<CefCookieVisitor> visitor) OVERRIDE;

View File

@ -686,15 +686,11 @@ TEST(CookieTest, GetCookieManagerJS) {
namespace {
const char* kCookieHttpUrl1 = "http://cookie-tests/cookie1.html";
const char* kCookieHttpUrl2 = "http://cookie-tests/cookie2.html";
const char* kCookieHttpUrl3 = "http://cookie-tests/cookie3.html";
class CookieTestHttpHandler : public TestHandler {
class CookieTestSchemeHandler : public TestHandler {
public:
class SchemeHandler : public CefResourceHandler {
public:
explicit SchemeHandler(CookieTestHttpHandler* handler)
explicit SchemeHandler(CookieTestSchemeHandler* handler)
: handler_(handler),
offset_(0) {}
@ -702,15 +698,15 @@ class CookieTestHttpHandler : public TestHandler {
CefRefPtr<CefCallback> callback)
OVERRIDE {
std::string url = request->GetURL();
if (url == kCookieHttpUrl1) {
if (url == handler_->url1_) {
content_ = "<html><body>COOKIE TEST1</body></html>";
cookie_ = "name1=value1";
handler_->got_process_request1_.yes();
} else if (url == kCookieHttpUrl2) {
} else if (url == handler_->url2_) {
content_ = "<html><body>COOKIE TEST2</body></html>";
cookie_ = "name2=value2";
handler_->got_process_request2_.yes();
} else if (url == kCookieHttpUrl3) {
} else if (url == handler_->url3_) {
content_ = "<html><body>COOKIE TEST3</body></html>";
handler_->got_process_request3_.yes();
@ -768,7 +764,7 @@ class CookieTestHttpHandler : public TestHandler {
}
private:
CookieTestHttpHandler* handler_;
CookieTestSchemeHandler* handler_;
std::string content_;
size_t offset_;
std::string cookie_;
@ -778,7 +774,7 @@ class CookieTestHttpHandler : public TestHandler {
class SchemeHandlerFactory : public CefSchemeHandlerFactory {
public:
explicit SchemeHandlerFactory(CookieTestHttpHandler* handler)
explicit SchemeHandlerFactory(CookieTestSchemeHandler* handler)
: handler_(handler) {}
virtual CefRefPtr<CefResourceHandler> Create(
@ -787,7 +783,7 @@ class CookieTestHttpHandler : public TestHandler {
const CefString& scheme_name,
CefRefPtr<CefRequest> request) OVERRIDE {
std::string url = request->GetURL();
if (url == kCookieHttpUrl3) {
if (url == handler_->url3_) {
// Verify that the cookie was not passed in.
CefRequest::HeaderMap headerMap;
request->GetHeaderMap(headerMap);
@ -800,48 +796,64 @@ class CookieTestHttpHandler : public TestHandler {
}
private:
CookieTestHttpHandler* handler_;
CookieTestSchemeHandler* handler_;
IMPLEMENT_REFCOUNTING(SchemeHandlerFactory);
};
CookieTestHttpHandler() {}
CookieTestSchemeHandler(const std::string& scheme) : scheme_(scheme) {
url1_ = scheme + "://cookie-tests/cookie1.html";
url2_ = scheme + "://cookie-tests/cookie2.html";
url3_ = scheme + "://cookie-tests/cookie3.html";
}
virtual void RunTest() OVERRIDE {
// Create new in-memory managers.
manager1_ = CefCookieManager::CreateManager(CefString());
manager2_ = CefCookieManager::CreateManager(CefString());
if (scheme_ != "http") {
CefRegisterCustomScheme(scheme_, true, false, false);
std::vector<CefString> schemes;
schemes.push_back("http");
schemes.push_back("https");
schemes.push_back(scheme_);
manager1_->SetSupportedSchemes(schemes);
manager2_->SetSupportedSchemes(schemes);
}
// Register the scheme handler.
CefRegisterSchemeHandlerFactory("http", "cookie-tests",
CefRegisterSchemeHandlerFactory(scheme_, "cookie-tests",
new SchemeHandlerFactory(this));
// Create the browser
CreateBrowser(kCookieHttpUrl1);
CreateBrowser(url1_);
}
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode) OVERRIDE {
std::string url = frame->GetURL();
if (url == kCookieHttpUrl1) {
if (url == url1_) {
got_load_end1_.yes();
VerifyCookie(manager1_, url, "name1", "value1", got_cookie1_);
// Go to the next URL
frame->LoadURL(kCookieHttpUrl2);
} else if (url == kCookieHttpUrl2) {
frame->LoadURL(url2_);
} else if (url == url2_) {
got_load_end2_.yes();
VerifyCookie(manager2_, url, "name2", "value2", got_cookie2_);
// Go to the next URL
frame->LoadURL(kCookieHttpUrl3);
frame->LoadURL(url3_);
} else {
got_load_end3_.yes();
VerifyCookie(manager2_, url, "name2", "value2", got_cookie3_);
// Unregister the scheme handler.
CefRegisterSchemeHandlerFactory("http", "cookie-tests", NULL);
CefRegisterSchemeHandlerFactory(scheme_, "cookie-tests", NULL);
DestroyTest();
}
@ -850,7 +862,7 @@ class CookieTestHttpHandler : public TestHandler {
virtual CefRefPtr<CefCookieManager> GetCookieManager(
CefRefPtr<CefBrowser> browser,
const CefString& main_url) OVERRIDE {
if (main_url == kCookieHttpUrl1) {
if (main_url == url1_) {
// Return the first cookie manager.
got_cookie_manager1_.yes();
return manager1_;
@ -879,6 +891,11 @@ class CookieTestHttpHandler : public TestHandler {
}
}
std::string scheme_;
std::string url1_;
std::string url2_;
std::string url3_;
CefRefPtr<CefCookieManager> manager1_;
CefRefPtr<CefCookieManager> manager2_;
@ -899,9 +916,31 @@ class CookieTestHttpHandler : public TestHandler {
} // namespace
// Verify use of multiple cookie managers vis HTTP.
// Verify use of multiple cookie managers via HTTP.
TEST(CookieTest, GetCookieManagerHttp) {
CefRefPtr<CookieTestHttpHandler> handler = new CookieTestHttpHandler();
CefRefPtr<CookieTestSchemeHandler> handler =
new CookieTestSchemeHandler("http");
handler->ExecuteTest();
EXPECT_TRUE(handler->got_process_request1_);
EXPECT_TRUE(handler->got_process_request2_);
EXPECT_TRUE(handler->got_process_request3_);
EXPECT_FALSE(handler->got_create_cookie_);
EXPECT_TRUE(handler->got_process_request_cookie_);
EXPECT_TRUE(handler->got_cookie_manager1_);
EXPECT_TRUE(handler->got_cookie_manager2_);
EXPECT_TRUE(handler->got_load_end1_);
EXPECT_TRUE(handler->got_load_end2_);
EXPECT_TRUE(handler->got_load_end3_);
EXPECT_TRUE(handler->got_cookie1_);
EXPECT_TRUE(handler->got_cookie2_);
EXPECT_TRUE(handler->got_cookie3_);
}
// Verify use of multiple cookie managers via a custom scheme.
TEST(CookieTest, GetCookieManagerCustom) {
CefRefPtr<CookieTestSchemeHandler> handler =
new CookieTestSchemeHandler("ccustom");
handler->ExecuteTest();
EXPECT_TRUE(handler->got_process_request1_);