Add CefSetCookiePath() and CefSetStoragePath() functions for changing cookie and localStorage locations while CEF is running (issue #347).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@353 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2011-11-02 19:50:00 +00:00
parent 7a4cb9dd20
commit 28adf8908a
14 changed files with 474 additions and 134 deletions

View File

@ -389,6 +389,14 @@ bool CefSetCookie(const CefString& url, const CefCookie& cookie);
/*--cef()--*/
bool CefDeleteCookies(const CefString& url, const CefString& cookie_name);
///
// Sets the directory path that will be used for storing cookie data. If |path|
// is empty data will be stored in memory only. By default the cookie path is
// the same as the cache path. Returns false if cookies cannot be accessed.
///
/*--cef()--*/
bool CefSetCookiePath(const CefString& path);
typedef cef_storage_type_t CefStorageType;
@ -396,8 +404,9 @@ typedef cef_storage_type_t CefStorageType;
// Visit storage of the specified type. If |origin| is non-empty only data
// matching that origin will be visited. If |key| is non-empty only data
// matching that key will be visited. Otherwise, all data for the storage
// type will be visited. Returns false if the storage cannot be accessed.
// Origin should be of the form scheme://domain.
// type will be visited. Origin should be of the form scheme://domain. If no
// origin is specified only data currently in memory will be returned. Returns
// false if the storage cannot be accessed.
///
/*--cef()--*/
bool CefVisitStorage(CefStorageType type, const CefString& origin,
@ -423,6 +432,16 @@ bool CefSetStorage(CefStorageType type, const CefString& origin,
bool CefDeleteStorage(CefStorageType type, const CefString& origin,
const CefString& key);
///
// Sets the directory path that will be used for storing data of the specified
// type. Currently only the ST_LOCALSTORAGE type is supported by this method.
// If |path| is empty data will be stored in memory only. By default the storage
// path is the same as the cache path. Returns false if the storage cannot be
// accessed.
///
/*--cef()--*/
bool CefSetStoragePath(CefStorageType type, const CefString& path);
///
// Interface defining the reference count implementation methods. All framework

View File

@ -326,12 +326,20 @@ CEF_EXPORT int cef_set_cookie(const cef_string_t* url,
CEF_EXPORT int cef_delete_cookies(const cef_string_t* url,
const cef_string_t* cookie_name);
///
// Sets the directory path that will be used for storing cookie data. If |path|
// is NULL data will be stored in memory only. By default the cookie path is the
// same as the cache path. Returns false (0) if cookies cannot be accessed.
///
CEF_EXPORT int cef_set_cookie_path(const cef_string_t* path);
///
// Visit storage of the specified type. If |origin| is non-NULL only data
// matching that origin will be visited. If |key| is non-NULL only data matching
// that key will be visited. Otherwise, all data for the storage type will be
// visited. Returns false (0) if the storage cannot be accessed. Origin should
// be of the form scheme://domain.
// visited. Origin should be of the form scheme://domain. If no origin is
// specified only data currently in memory will be returned. Returns false (0)
// if the storage cannot be accessed.
///
CEF_EXPORT int cef_visit_storage(enum cef_storage_type_t type,
const cef_string_t* origin, const cef_string_t* key,
@ -355,6 +363,16 @@ CEF_EXPORT int cef_set_storage(enum cef_storage_type_t type,
CEF_EXPORT int cef_delete_storage(enum cef_storage_type_t type,
const cef_string_t* origin, const cef_string_t* key);
///
// Sets the directory path that will be used for storing data of the specified
// type. Currently only the ST_LOCALSTORAGE type is supported by this function.
// If |path| is NULL data will be stored in memory only. By default the storage
// path is the same as the cache path. Returns false (0) if the storage cannot
// be accessed.
///
CEF_EXPORT int cef_set_storage_path(enum cef_storage_type_t type,
const cef_string_t* path);
typedef struct _cef_base_t
{
// Size of the data structure.

View File

@ -86,14 +86,8 @@ void BrowserRequestContext::Init(
NOTREACHED() << "The cache_path directory could not be created";
}
scoped_refptr<BrowserPersistentCookieStore> persistent_store;
if (cache_path_valid) {
const FilePath& cookie_path = cache_path.AppendASCII("Cookies");
persistent_store = new BrowserPersistentCookieStore(cookie_path);
}
SetCookieStoragePath(cache_path);
storage_.set_cookie_store(
new net::CookieMonster(persistent_store.get(), NULL));
storage_.set_origin_bound_cert_service(new net::OriginBoundCertService(
new net::DefaultOriginBoundCertStore(NULL)));
@ -222,8 +216,34 @@ bool BrowserRequestContext::AcceptAllCookies() {
return accept_all_cookies_;
}
void BrowserRequestContext::SetCookieStoragePath(const FilePath& path) {
REQUIRE_IOT();
if (cookie_store() && ((cookie_store_path_.empty() && path.empty()) ||
cookie_store_path_ == path)) {
// The path has not changed so don't do anything.
return;
}
scoped_refptr<BrowserPersistentCookieStore> persistent_store;
if (!path.empty()) {
if (file_util::CreateDirectory(path)) {
const FilePath& cookie_path = path.AppendASCII("Cookies");
persistent_store = new BrowserPersistentCookieStore(cookie_path);
} else {
NOTREACHED() << "The cookie storage directory could not be created";
}
}
// Set the new cookie store that will be used for all new requests. The old
// cookie store, if any, will be automatically flushed and closed when no
// longer referenced.
storage_.set_cookie_store(
new net::CookieMonster(persistent_store.get(), NULL));
cookie_store_path_ = path;
}
const std::string& BrowserRequestContext::GetUserAgent(
const GURL& url) const {
return webkit_glue::GetUserAgent(url);
}

View File

@ -39,6 +39,11 @@ class BrowserRequestContext : public net::URLRequestContext {
void SetAcceptAllCookies(bool accept_all_cookies);
bool AcceptAllCookies();
// Set the path used for cookie storage. If |path| is empty memory only
// storage will be used. If the old cookie data is being stored on disk it
// will be flushed and closed.
void SetCookieStoragePath(const FilePath& path);
webkit_blob::BlobStorageController* blob_storage_controller() const {
return blob_storage_controller_.get();
}
@ -55,6 +60,7 @@ class BrowserRequestContext : public net::URLRequestContext {
scoped_ptr<webkit_blob::BlobStorageController> blob_storage_controller_;
scoped_refptr<fileapi::FileSystemContext> file_system_context_;
scoped_ptr<net::URLSecurityManager> url_security_manager_;
FilePath cookie_store_path_;
bool accept_all_cookies_;
};

View File

@ -1061,6 +1061,7 @@ class CookieGetter : public base::RefCountedThreadSafe<CookieGetter> {
}
void Get(const GURL& url) {
REQUIRE_IOT();
net::CookieStore* cookie_store =
_Context->request_context()->cookie_store();
if (cookie_store) {

View File

@ -184,6 +184,17 @@ void IOT_VisitUrlCookies(const GURL& url, bool includeHttpOnly,
base::Bind(&VisitCookiesCallback::Run, callback.get()));
}
void IOT_SetCookiePath(const CefString& path)
{
REQUIRE_IOT();
FilePath cookie_path;
if (!path.empty())
cookie_path = FilePath(path);
_Context->request_context()->SetCookieStoragePath(cookie_path);
}
// Used in multi-threaded message loop mode to observe shutdown of the UI
// thread.
class DestructionObserver : public MessageLoop::DestructionObserver
@ -206,9 +217,13 @@ void UIT_VisitStorage(int64 namespace_id, const CefString& origin,
REQUIRE_UIT();
DOMStorageContext* context = _Context->storage_context();
// Allow storage to be allocated for localStorage so that on-disk data, if
// any, will be available.
bool allocation_allowed = (namespace_id == kLocalStorageNamespaceId);
DOMStorageNamespace* ns =
context->GetStorageNamespace(namespace_id, false);
context->GetStorageNamespace(namespace_id, allocation_allowed);
if (!ns)
return;
@ -217,7 +232,7 @@ void UIT_VisitStorage(int64 namespace_id, const CefString& origin,
if (!origin.empty()) {
// Visit only the area with the specified origin.
DOMStorageArea* area = ns->GetStorageArea(origin, false);
DOMStorageArea* area = ns->GetStorageArea(origin, allocation_allowed);
if (area)
areas.push_back(area);
} else {
@ -317,6 +332,20 @@ void UIT_VisitStorage(int64 namespace_id, const CefString& origin,
}
}
void UIT_SetStoragePath(int64 namespace_id, const CefString& path)
{
REQUIRE_UIT();
if (namespace_id != kLocalStorageNamespaceId)
return;
FilePath file_path;
if (!path.empty())
file_path = FilePath(path);
_Context->storage_context()->SetLocalStoragePath(file_path);
}
} // anonymous
bool CefInitialize(const CefSettings& settings)
@ -621,6 +650,24 @@ bool CefDeleteCookies(const CefString& url, const CefString& cookie_name)
return true;
}
bool CefSetCookiePath(const CefString& path)
{
// Verify that the context is in a valid state.
if (!CONTEXT_STATE_VALID()) {
NOTREACHED() << "context not valid";
return false;
}
if (CefThread::CurrentlyOn(CefThread::IO)) {
IOT_SetCookiePath(path);
} else {
CefThread::PostTask(CefThread::IO, FROM_HERE,
base::Bind(&IOT_SetCookiePath, path));
}
return true;
}
bool CefVisitStorage(CefStorageType type, const CefString& origin,
const CefString& key,
CefRefPtr<CefStorageVisitor> visitor)
@ -718,6 +765,10 @@ bool CefDeleteStorage(CefStorageType type, const CefString& origin,
DOMStorageContext* context = _Context->storage_context();
// Allow storage to be allocated for localStorage so that on-disk data, if
// any, will be available.
bool allocation_allowed = (namespace_id == kLocalStorageNamespaceId);
if (origin.empty()) {
// Delete all storage for the namespace.
if (namespace_id == kLocalStorageNamespaceId)
@ -730,7 +781,7 @@ bool CefDeleteStorage(CefStorageType type, const CefString& origin,
context->DeleteLocalStorageForOrigin(origin);
} else {
DOMStorageArea* area =
context->GetStorageArea(namespace_id, origin, false);
context->GetStorageArea(namespace_id, origin, allocation_allowed);
if (area) {
// Calling Clear() is necessary to remove the data from the namespace.
area->Clear();
@ -739,7 +790,8 @@ bool CefDeleteStorage(CefStorageType type, const CefString& origin,
}
} else {
// Delete the specified key.
DOMStorageArea* area = context->GetStorageArea(namespace_id, origin, false);
DOMStorageArea* area =
context->GetStorageArea(namespace_id, origin, allocation_allowed);
if (area)
area->RemoveItem(key);
}
@ -747,6 +799,32 @@ bool CefDeleteStorage(CefStorageType type, const CefString& origin,
return true;
}
bool CefSetStoragePath(CefStorageType type, const CefString& path)
{
// Verify that the context is in a valid state.
if (!CONTEXT_STATE_VALID()) {
NOTREACHED() << "context not valid";
return false;
}
int64 namespace_id;
if (type == ST_LOCALSTORAGE) {
namespace_id = kLocalStorageNamespaceId;
} else {
NOTREACHED() << "invalid type";
return false;
}
if (CefThread::CurrentlyOn(CefThread::UI)) {
UIT_SetStoragePath(namespace_id, path);
} else {
CefThread::PostTask(CefThread::UI, FROM_HERE,
base::Bind(&UIT_SetStoragePath, namespace_id, path));
}
return true;
}
// CefContext

View File

@ -99,6 +99,17 @@ void DOMStorageContext::DeleteSessionStorageNamespace(int64 namespace_id) {
storage_namespace_map_.erase(iter);
}
void DOMStorageContext::DeleteLocalStorageNamespace() {
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
StorageNamespaceMap::iterator iter =
storage_namespace_map_.find(kLocalStorageNamespaceId);
if (iter == storage_namespace_map_.end())
return;
DCHECK(iter->second->dom_storage_type() == DOM_STORAGE_LOCAL);
delete iter->second;
storage_namespace_map_.erase(iter);
}
DOMStorageNamespace* DOMStorageContext::GetStorageNamespace(
int64 id, bool allocation_allowed) {
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
@ -203,6 +214,25 @@ void DOMStorageContext::DeleteAllLocalStorageFiles() {
}
}
void DOMStorageContext::SetLocalStoragePath(
const FilePath& local_storage_path) {
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
if ((local_storage_path.empty() && local_storage_path_.empty()) ||
local_storage_path == local_storage_path_)
return;
// Make sure that we don't swap out a database that's currently being accessed
// by unloading all of the databases temporarily.
PurgeMemory(kLocalStorageNamespaceId);
// Delete the current namespace, if any. It will be recreated using the new
// path when needed.
DeleteLocalStorageNamespace();
local_storage_path_ = local_storage_path;
}
DOMStorageNamespace* DOMStorageContext::CreateLocalStorage() {
FilePath dir_path;
if (!local_storage_path_.empty())

View File

@ -46,6 +46,9 @@ class DOMStorageContext {
// Called on WebKit thread when a session storage namespace can be deleted.
void DeleteSessionStorageNamespace(int64 namespace_id);
// Called on WebKit thread when the local storage namespace can be deleted.
void DeleteLocalStorageNamespace();
// Get a namespace from an id. What's returned is owned by this class. If
// allocation_allowed is true, then this function will create the storage
// namespace if it hasn't been already.
@ -72,6 +75,10 @@ class DOMStorageContext {
// Deletes all local storage files.
void DeleteAllLocalStorageFiles();
// Sets the path that will be used for local storage. If |local_storage_path|
// is empty in-memory storage will be used.
void SetLocalStoragePath(const FilePath& local_storage_path);
// The local storage directory.
static const FilePath::CharType kLocalStorageDirectory[];

View File

@ -283,14 +283,24 @@ CEF_EXPORT int cef_delete_cookies(const cef_string_t* url,
{
CefString urlStr, cookieNameStr;
if(url)
if (url)
urlStr = url;
if(cookie_name)
if (cookie_name)
cookieNameStr = cookie_name;
return CefDeleteCookies(urlStr, cookieNameStr);
}
CEF_EXPORT int cef_set_cookie_path(const cef_string_t* path)
{
CefString pathStr;
if (path)
pathStr = path;
return CefSetCookiePath(pathStr);
}
CEF_EXPORT int cef_visit_storage(enum cef_storage_type_t type,
const cef_string_t* origin, const cef_string_t* key,
struct _cef_storage_visitor_t* visitor)
@ -334,3 +344,14 @@ CEF_EXPORT int cef_delete_storage(enum cef_storage_type_t type,
return CefDeleteStorage(type, origin, key);
}
CEF_EXPORT int cef_set_storage_path(enum cef_storage_type_t type,
const cef_string_t* path)
{
CefString pathStr;
if (path)
pathStr = path;
return CefSetStoragePath(type, pathStr);
}

View File

@ -205,6 +205,11 @@ bool CefDeleteCookies(const CefString& url, const CefString& cookie_name)
true : false;
}
bool CefSetCookiePath(const CefString& path)
{
return cef_set_cookie_path(path.GetStruct()) ? true : false;
}
bool CefVisitStorage(CefStorageType type, const CefString& origin,
const CefString& key,
CefRefPtr<CefStorageVisitor> visitor)
@ -226,3 +231,8 @@ bool CefDeleteStorage(CefStorageType type, const CefString& origin,
return cef_delete_storage(type, origin.GetStruct(), key.GetStruct()) ?
true : false;
}
bool CefSetStoragePath(CefStorageType type, const CefString& path)
{
return cef_set_storage_path(type, path.GetStruct()) ? true : false;
}

View File

@ -2,10 +2,12 @@
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "base/scoped_temp_dir.h"
#include "base/synchronization/waitable_event.h"
#include "include/cef.h"
#include "include/cef_runnable.h"
#include "base/synchronization/waitable_event.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "test_suite.h"
#include <vector>
namespace {
@ -61,125 +63,123 @@ public:
IMPLEMENT_REFCOUNTING(TestVisitor);
};
// Create a test cookie. If |withDomain| is true a domain cookie will be
// created, otherwise a host cookie will be created.
void CreateCookie(CefCookie& cookie, bool withDomain,
base::WaitableEvent& event)
{
CefString(&cookie.name).FromASCII("my_cookie");
CefString(&cookie.value).FromASCII("My Value");
if (withDomain)
CefString(&cookie.domain).FromASCII(kTestDomain);
CefString(&cookie.path).FromASCII(kTestPath);
cookie.has_expires = true;
cookie.expires.year = 2200;
cookie.expires.month = 4;
cookie.expires.day_of_week = 5;
cookie.expires.day_of_month = 11;
CookieVector cookies;
cookies.push_back(cookie);
// Set the cookie.
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Set, kTestUrl, &cookies,
&event));
event.Wait();
}
// Retrieve the test cookie. If |withDomain| is true check that the cookie
// is a domain cookie, otherwise a host cookie. if |deleteCookies| is true
// the cookie will be deleted when it's retrieved.
void GetCookie(const CefCookie& cookie, bool withDomain,
base::WaitableEvent& event, bool deleteCookies)
{
CookieVector cookies;
// Get the cookie and delete it.
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, deleteCookies, &event)));
event.Wait();
EXPECT_EQ((CookieVector::size_type)1, cookies.size());
const CefCookie& cookie_read = cookies[0];
EXPECT_EQ(CefString(&cookie_read.name), "my_cookie");
EXPECT_EQ(CefString(&cookie_read.value), "My Value");
if (withDomain)
EXPECT_EQ(CefString(&cookie_read.domain), ".www.test.com");
else
EXPECT_EQ(CefString(&cookie_read.domain), kTestDomain);
EXPECT_EQ(CefString(&cookie_read.path), kTestPath);
EXPECT_TRUE(cookie_read.has_expires);
EXPECT_EQ(cookie.expires.year, cookie_read.expires.year);
EXPECT_EQ(cookie.expires.month, cookie_read.expires.month);
EXPECT_EQ(cookie.expires.day_of_week, cookie_read.expires.day_of_week);
EXPECT_EQ(cookie.expires.day_of_month, cookie_read.expires.day_of_month);
EXPECT_EQ(cookie.expires.hour, cookie_read.expires.hour);
EXPECT_EQ(cookie.expires.minute, cookie_read.expires.minute);
EXPECT_EQ(cookie.expires.second, cookie_read.expires.second);
EXPECT_EQ(cookie.expires.millisecond, cookie_read.expires.millisecond);
}
// Verify that no cookies exist. If |withUrl| is true it will only check for
// cookies matching the URL.
void VerifyNoCookies(base::WaitableEvent& event, bool withUrl)
{
CookieVector cookies;
// Verify that the cookie has been deleted.
if (withUrl) {
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, false, &event)));
} else {
EXPECT_TRUE(CefVisitAllCookies(new TestVisitor(&cookies, false, &event)));
}
event.Wait();
EXPECT_EQ((CookieVector::size_type)0, cookies.size());
}
// Delete all system cookies.
void DeleteAllCookies(base::WaitableEvent& event)
{
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Delete, CefString(),
CefString(), &event));
event.Wait();
}
} // anonymous
// Test creation of a domain cookie.
TEST(CookieTest, DomainCookie)
{
base::WaitableEvent event(false, false);
CefCookie cookie;
CefString(&cookie.name).FromASCII("my_cookie");
CefString(&cookie.value).FromASCII("My Value");
CefString(&cookie.domain).FromASCII(kTestDomain);
CefString(&cookie.path).FromASCII(kTestPath);
cookie.has_expires = true;
cookie.expires.year = 2200;
cookie.expires.month = 4;
cookie.expires.day_of_week = 5;
cookie.expires.day_of_month = 11;
CookieVector cookies;
cookies.push_back(cookie);
// Set the cookie.
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Set, kTestUrl, &cookies,
&event));
event.Wait();
cookies.clear();
// Get the cookie and delete it.
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, true, &event)));
event.Wait();
// Create a domain cookie.
CreateCookie(cookie, true, event);
EXPECT_EQ((CookieVector::size_type)1, cookies.size());
const CefCookie& cookie_read = cookies[0];
EXPECT_EQ(CefString(&cookie_read.name), "my_cookie");
EXPECT_EQ(CefString(&cookie_read.value), "My Value");
EXPECT_EQ(CefString(&cookie_read.domain), ".www.test.com");
EXPECT_EQ(CefString(&cookie_read.path), kTestPath);
EXPECT_TRUE(cookie_read.has_expires);
EXPECT_EQ(cookie.expires.year, cookie_read.expires.year);
EXPECT_EQ(cookie.expires.month, cookie_read.expires.month);
EXPECT_EQ(cookie.expires.day_of_week, cookie_read.expires.day_of_week);
EXPECT_EQ(cookie.expires.day_of_month, cookie_read.expires.day_of_month);
EXPECT_EQ(cookie.expires.hour, cookie_read.expires.hour);
EXPECT_EQ(cookie.expires.minute, cookie_read.expires.minute);
EXPECT_EQ(cookie.expires.second, cookie_read.expires.second);
EXPECT_EQ(cookie.expires.millisecond, cookie_read.expires.millisecond);
// Retrieve, verify and delete the domain cookie.
GetCookie(cookie, true, event, true);
cookies.clear();
// Verify that the cookie has been deleted.
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, false, &event)));
event.Wait();
EXPECT_EQ((CookieVector::size_type)0, cookies.size());
// Verify that the cookie was deleted.
VerifyNoCookies(event, true);
}
// Test creation of a host cookie.
TEST(CookieTest, HostCookie)
{
base::WaitableEvent event(false, false);
CefCookie cookie;
// Create a host cookie.
CefCookie cookie;
CefString(&cookie.name).FromASCII("my_cookie");
CefString(&cookie.value).FromASCII("My Value");
CefString(&cookie.path).FromASCII(kTestPath);
cookie.has_expires = true;
cookie.expires.year = 2200;
cookie.expires.month = 4;
cookie.expires.day_of_week = 5;
cookie.expires.day_of_month = 11;
CreateCookie(cookie, false, event);
CookieVector cookies;
cookies.push_back(cookie);
// Set the cookie.
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Set, kTestUrl, &cookies,
&event));
event.Wait();
cookies.clear();
// Get the cookie.
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, false, &event)));
event.Wait();
// Retrieve, verify and delete the host cookie.
GetCookie(cookie, false, event, true);
EXPECT_EQ((CookieVector::size_type)1, cookies.size());
const CefCookie& cookie_read = cookies[0];
EXPECT_EQ(CefString(&cookie_read.name), "my_cookie");
EXPECT_EQ(CefString(&cookie_read.value), "My Value");
EXPECT_EQ(CefString(&cookie_read.domain), kTestDomain);
EXPECT_EQ(CefString(&cookie_read.path), kTestPath);
EXPECT_TRUE(cookie_read.has_expires);
EXPECT_EQ(cookie.expires.year, cookie_read.expires.year);
EXPECT_EQ(cookie.expires.month, cookie_read.expires.month);
EXPECT_EQ(cookie.expires.day_of_week, cookie_read.expires.day_of_week);
EXPECT_EQ(cookie.expires.day_of_month, cookie_read.expires.day_of_month);
EXPECT_EQ(cookie.expires.hour, cookie_read.expires.hour);
EXPECT_EQ(cookie.expires.minute, cookie_read.expires.minute);
EXPECT_EQ(cookie.expires.second, cookie_read.expires.second);
EXPECT_EQ(cookie.expires.millisecond, cookie_read.expires.millisecond);
cookies.clear();
// Delete the cookie.
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Delete, kTestUrl,
CefString("my_cookie"), &event));
event.Wait();
// Verify that the cookie has been deleted.
EXPECT_TRUE(CefVisitUrlCookies(kTestUrl, false,
new TestVisitor(&cookies, false, &event)));
event.Wait();
EXPECT_EQ((CookieVector::size_type)0, cookies.size());
// Verify that the cookie was deleted.
VerifyNoCookies(event, true);
}
// Test creation of multiple cookies.
@ -365,13 +365,52 @@ TEST(CookieTest, AllCookies)
cookies.clear();
// Delete all of the system cookies.
CefPostTask(TID_IO, NewCefRunnableFunction(IOT_Delete, CefString(),
CefString(), &event));
event.Wait();
DeleteAllCookies(event);
// Verify that all system cookies have been deleted.
EXPECT_TRUE(CefVisitAllCookies(new TestVisitor(&cookies, false, &event)));
event.Wait();
EXPECT_EQ((CookieVector::size_type)0, cookies.size());
VerifyNoCookies(event, false);
}
TEST(CookieTest, ChangeDirectory)
{
base::WaitableEvent event(false, false);
CefCookie cookie;
std::string cache_path;
CefTestSuite::GetCachePath(cache_path);
ScopedTempDir temp_dir;
// Create a new temporary directory.
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
// Delete all of the system cookies.
DeleteAllCookies(event);
// Set the new temporary directory as the storage location.
EXPECT_TRUE(CefSetCookiePath(temp_dir.path().value()));
// Verify that no cookies exist.
VerifyNoCookies(event, true);
// Create a domain cookie.
CreateCookie(cookie, true, event);
// Retrieve and verify the domain cookie.
GetCookie(cookie, true, event, false);
// Restore the original storage location.
EXPECT_TRUE(CefSetCookiePath(cache_path));
// Verify that no cookies exist.
VerifyNoCookies(event, true);
// Set the new temporary directory as the storage location.
EXPECT_TRUE(CefSetCookiePath(temp_dir.path().value()));
// Retrieve and verify the domain cookie that was set previously.
GetCookie(cookie, true, event, false);
// Restore the original storage location.
EXPECT_TRUE(CefSetCookiePath(cache_path));
}

View File

@ -2,9 +2,11 @@
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "base/scoped_temp_dir.h"
#include "include/cef.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "test_handler.h"
#include "test_suite.h"
namespace {
@ -111,11 +113,22 @@ public:
IMPLEMENT_REFCOUNTING(StorageVisitor);
};
StorageTestHandler(CefStorageType type)
: type_(type), nav_(0) {}
StorageTestHandler(CefStorageType type, bool expectKeysSet, bool leaveKeysSet)
: type_(type),
expect_keys_set_(expectKeysSet),
leave_keys_set_(leaveKeysSet),
nav_(0) {}
virtual void RunTest() OVERRIDE
{
// Verify the key status.
CefVisitStorage(type_, kOrigin, "",
new StorageVisitor(this, "startupvisit",
StorageVisitor::VisitKey,
&got_cpp_startupvisit_fail_,
&got_cpp_startupvisit_fail_,
expect_keys_set_?2:0));
std::stringstream ss;
std::string func = (type_==ST_LOCALSTORAGE?"localStorage":"sessionStorage");
@ -293,6 +306,19 @@ public:
// Verify JS read after navigation.
frame->LoadURL(kNav2);
} else {
if (!leave_keys_set_) {
// Delete all values by origin.
CefDeleteStorage(type_, kOrigin, "");
}
// Verify the key status.
CefVisitStorage(type_, kOrigin, "",
new StorageVisitor(this, "shutdownvisit",
StorageVisitor::VisitKey,
&got_cpp_shutdownvisit_fail_,
&got_cpp_shutdownvisit_fail_,
leave_keys_set_?2:0));
DestroyTest();
}
}
@ -308,8 +334,11 @@ public:
}
CefStorageType type_;
bool expect_keys_set_;
bool leave_keys_set_;
int nav_;
TrackCallback got_cpp_startupvisit_fail_;
TrackCallback got_cpp_all_read1_;
TrackCallback got_cpp_all_read2_;
TrackCallback got_cpp_origin_read1_;
@ -340,13 +369,20 @@ public:
TrackCallback got_cpp_all_reset2d_;
TrackCallback got_js_read1_;
TrackCallback got_js_read2_;
TrackCallback got_cpp_shutdownvisit_fail_;
};
void StorageTest(CefStorageType type)
void StorageTest(CefStorageType type, bool expectKeysSet, bool leaveKeysSet)
{
CefRefPtr<StorageTestHandler> handler = new StorageTestHandler(type);
CefRefPtr<StorageTestHandler> handler =
new StorageTestHandler(type, expectKeysSet, leaveKeysSet);
handler->ExecuteTest();
if (expectKeysSet)
EXPECT_TRUE(handler->got_cpp_startupvisit_fail_);
else
EXPECT_FALSE(handler->got_cpp_startupvisit_fail_);
EXPECT_TRUE(handler->got_cpp_all_read1_);
EXPECT_TRUE(handler->got_cpp_all_read2_);
EXPECT_TRUE(handler->got_cpp_origin_read1_);
@ -377,6 +413,11 @@ void StorageTest(CefStorageType type)
EXPECT_TRUE(handler->got_cpp_all_reset2d_);
EXPECT_TRUE(handler->got_js_read1_);
EXPECT_TRUE(handler->got_js_read2_);
if (leaveKeysSet)
EXPECT_TRUE(handler->got_cpp_shutdownvisit_fail_);
else
EXPECT_FALSE(handler->got_cpp_shutdownvisit_fail_);
}
} // namespace
@ -384,11 +425,46 @@ void StorageTest(CefStorageType type)
// Test localStorage.
TEST(StorageTest, Local)
{
StorageTest(ST_LOCALSTORAGE);
StorageTest(ST_LOCALSTORAGE, false, false);
}
// Test sessionStorage.
TEST(StorageTest, Session)
{
StorageTest(ST_SESSIONSTORAGE);
StorageTest(ST_SESSIONSTORAGE, false, false);
}
// Test changing the localStorage directory.
TEST(StorageTest, LocalChangeDirectory)
{
std::string cache_path;
CefTestSuite::GetCachePath(cache_path);
ScopedTempDir temp_dir;
// Create a new temporary directory.
EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
// Set the new temporary directory as the storage location.
EXPECT_TRUE(CefSetStoragePath(ST_LOCALSTORAGE, temp_dir.path().value()));
// Run the test leaving behind the set keys.
StorageTest(ST_LOCALSTORAGE, false, true);
// Restore the original storage location.
EXPECT_TRUE(CefSetStoragePath(ST_LOCALSTORAGE, cache_path));
// Run the test. It will fail if the set keys exist in the original storage
// location.
StorageTest(ST_LOCALSTORAGE, false, false);
// Set the new temporary directory as the storage location.
EXPECT_TRUE(CefSetStoragePath(ST_LOCALSTORAGE, temp_dir.path().value()));
// Run the test verifying that the keys set previously still exist in the
// temporary directory.
StorageTest(ST_LOCALSTORAGE, true, false);
// Restore the original storage directory.
EXPECT_TRUE(CefSetStoragePath(ST_LOCALSTORAGE, cache_path));
}

View File

@ -20,9 +20,9 @@ void CefTestSuite::Initialize() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch("cache_path")) {
std::string cache_path;
if (GetCachePath(cache_path)) {
// Set the cache_path value.
std::string cache_path = command_line->GetSwitchValueASCII("cache_path");
CefString(&settings.cache_path).FromASCII(cache_path.c_str());
}
@ -37,3 +37,16 @@ void CefTestSuite::Shutdown() {
CefShutdown();
TestSuite::Shutdown();
}
// static
bool CefTestSuite::GetCachePath(std::string& path) {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch("cache_path")) {
// Set the cache_path value.
path = command_line->GetSwitchValueASCII("cache_path");
return true;
}
return false;
}

View File

@ -11,6 +11,8 @@ class CefTestSuite : public TestSuite {
public:
CefTestSuite(int argc, char** argv);
static bool GetCachePath(std::string& path);
protected:
virtual void Initialize();
virtual void Shutdown();