Chromium Code Reviews| Index: net/base/cookie_monster.cc |
| =================================================================== |
| --- net/base/cookie_monster.cc (revision 96178) |
| +++ net/base/cookie_monster.cc (working copy) |
| @@ -48,6 +48,7 @@ |
| #include <set> |
| #include "base/basictypes.h" |
| +#include "base/bind.h" |
| #include "base/format_macros.h" |
| #include "base/logging.h" |
| #include "base/memory/scoped_ptr.h" |
| @@ -419,6 +420,7 @@ |
| CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) |
| : initialized_(false), |
| + loaded_(false), |
| expiry_and_key_scheme_(expiry_and_key_default_), |
| store_(store), |
| last_access_threshold_( |
| @@ -434,6 +436,7 @@ |
| Delegate* delegate, |
| int last_access_threshold_milliseconds) |
| : initialized_(false), |
| + loaded_(false), |
| expiry_and_key_scheme_(expiry_and_key_default_), |
| store_(store), |
| last_access_threshold_(base::TimeDelta::FromMilliseconds( |
| @@ -567,6 +570,441 @@ |
| return (domain_string.empty() || domain_string[0] != '.'); |
| } |
| + |
| + |
| +void InvokeSetCookiesCallbackOnOtherThread( |
| + const CookieMonster::SetCookiesCallback& callback, |
| + bool success) { |
| + if (!callback.is_null()) |
| + callback.Run(success); |
| +} |
| + |
| +void InvokeGetCookieListCallbackOnOtherThread( |
| + const CookieMonster::GetCookieListCallback& callback, |
| + const CookieList& cookies) { |
| + if (!callback.is_null()) |
| + callback.Run(cookies); |
| +} |
| + |
| +void InvokeDeleteCallbackOnOtherThread( |
| + const CookieMonster::DeleteCallback& callback, |
| + int num_deleted) { |
| + if (!callback.is_null()) |
| + callback.Run(num_deleted); |
| +} |
| + |
| +void InvokeGetCookiesCallbackOnOtherThread( |
| + const CookieMonster::GetCookiesCallback& callback, |
| + const std::string& cookie) { |
| + if (!callback.is_null()) |
| + callback.Run(cookie); |
| +} |
| + |
| +void InvokeGetCookieInfoCallbackOnOtherThread( |
| + const CookieMonster::GetCookieInfoCallback& callback, |
| + const std::string& cookie_line, |
| + const std::vector<CookieStore::CookieInfo>& cookie_infos) { |
| + if (!callback.is_null()) { |
| + std::string line = cookie_line; |
| + std::vector<CookieStore::CookieInfo> info = cookie_infos; |
| + callback.Run(&line, &info); |
| + } |
| +} |
| + |
| +// Asynchronous CookieMonster API |
| + |
| +void CookieMonster::SetCookieWithDetailsAsync( |
| + const GURL& url, const std::string& name, const std::string& value, |
| + const std::string& domain, const std::string& path, |
| + const base::Time& expiration_time, bool secure, bool http_only, |
| + const SetCookiesCallback& callback) { |
| + GURL* gurl = new GURL(url); |
|
erikwright (departed)
2011/08/11 15:30:42
Who deletes this?
In general, 'new T' should neve
ycxiao
2011/08/12 02:35:24
Done.
|
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.name = name; |
| + parameters.value = value; |
| + parameters.domain = domain; |
| + parameters.path =path; |
| + parameters.expiration_time = expiration_time; |
| + parameters.secure = secure; |
| + parameters.http_only = http_only; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::SetCookieWithDetailsTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::GetAllCookiesTask, |
| + this, callback, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| + |
| +void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
| + const GURL& url, |
| + const CookieOptions& options, |
| + const GetCookieListCallback& callback) { |
| + GURL* gurl = new GURL(url); |
|
erikwright (departed)
2011/08/11 15:30:42
ditto here and below...
ycxiao
2011/08/12 02:35:24
Done.
|
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.options = options; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = |
| + base::Bind(&CookieMonster::GetAllCookiesForURLWithOptionsTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::GetAllCookiesForURLAsync( |
| + const GURL& url, const GetCookieListCallback& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::GetAllCookiesForURLTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DeleteAllAsync(bool sync_to_store, |
| + const DeleteCallback& callback) { |
| + CookieAPIParameters parameters; |
| + parameters.sync_to_store = sync_to_store; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::DeleteAllTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DeleteAllCreatedBetweenAsync( |
| + const Time& delete_begin, const Time& delete_end, |
| + bool sync_to_store, |
| + const DeleteCallback& callback) { |
| + CookieAPIParameters parameters; |
| + parameters.delete_begin = delete_begin; |
| + parameters.delete_end = delete_end; |
| + parameters.sync_to_store = sync_to_store; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::DeleteAllCreatedBetweenTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DeleteAllForHostAsync( |
| + const GURL& url, const DeleteCallback& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::DeleteAllForHostTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DeleteCanonicalCookieAsync( |
| + const CanonicalCookie& cookie, |
| + const DeleteCookieCallback& callback) { |
| + CanonicalCookie delete_cookie = cookie; |
| + CookieAPIParameters parameters; |
| + parameters.cookie = &delete_cookie; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::DeleteCanonicalCookieTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::SetCookieWithOptionsAsync( |
| + const GURL& url, |
| + const std::string& cookie_line, |
| + const CookieOptions& options, |
| + const SetCookiesCallback& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.cookie_line = cookie_line; |
| + parameters.options = options; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::SetCookieWithOptionsTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::GetCookiesWithOptionsAsync( |
| + const GURL& url, const CookieOptions& options, |
| + const GetCookiesCallback& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.options = options; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::GetCookiesWithOptionsTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::GetCookiesWithInfoAsync( |
| + const GURL& url, |
| + const CookieOptions& options, |
| + const GetCookieInfoCallback& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.options = options; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::GetCookiesWithInfoTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DeleteCookieAsync(const GURL& url, |
| + const std::string& cookie_name, |
| + const base::Closure& callback) { |
| + GURL* gurl = new GURL(url); |
| + CookieAPIParameters parameters; |
| + parameters.url = gurl; |
| + parameters.name = cookie_name; |
| + |
| + scoped_refptr<base::MessageLoopProxy> thread( |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + base::Closure task = base::Bind(&CookieMonster::DeleteCookieTask, |
| + this, callback, parameters, thread); |
| + DoCookieTask(task); |
| +} |
| + |
| +void CookieMonster::DoCookieTask(const base::Closure& task) { |
| + DCHECK(!task.is_null()); |
| + InitIfNecessary(); |
| + bool loaded = false; |
| + { |
| + base::AutoLock autolock(lock_); |
| + loaded = loaded_; |
| + } |
| + if (loaded_) { |
|
erikwright (departed)
2011/08/11 15:30:42
use the local variable!
ycxiao
2011/08/12 02:35:24
Done.
|
| + task.Run(); |
| + } else { |
| + base::AutoLock autolock(lock_); |
| + queue_.push_back(task); |
| + } |
| +} |
| + |
| +void CookieMonster::SetCookieWithDetailsTask( |
| + const SetCookiesCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + bool success = SetCookieWithDetails( |
| + *para.url, para.name, para.value, para.domain, para.path, |
| + para.expiration_time, para.secure, para.http_only); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(success); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread, |
| + callback, success)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::GetAllCookiesTask( |
| + const GetCookieListCallback& callback, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + if (!callback.is_null()) { |
| + CookieList cookies = GetAllCookies(); |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(cookies); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread, |
| + callback, cookies)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::GetAllCookiesForURLWithOptionsTask( |
| + const GetCookieListCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + if (!callback.is_null()) { |
| + CookieList cookies = GetAllCookiesForURLWithOptions( |
| + *para.url, para.options); |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(cookies); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread, |
| + callback, cookies)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::GetAllCookiesForURLTask( |
| + const GetCookieListCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + if (!callback.is_null()) { |
| + CookieList cookies = GetAllCookiesForURL(*para.url); |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(cookies); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread, |
| + callback, cookies)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::DeleteAllTask( |
| + const DeleteCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + int num_deleted = DeleteAll(para.sync_to_store); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(num_deleted); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread, |
| + callback, num_deleted)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::DeleteAllCreatedBetweenTask( |
| + const DeleteCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + int num_deleted = DeleteAllCreatedBetween( |
| + para.delete_begin, para.delete_end, para.sync_to_store); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(num_deleted); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread, |
| + callback, num_deleted)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::DeleteAllForHostTask( |
| + const DeleteCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + int num_deleted = DeleteAllForHost(*para.url); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(num_deleted); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread, |
| + callback, num_deleted)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::DeleteCanonicalCookieTask( |
| + const DeleteCookieCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + bool result = DeleteCanonicalCookie(*para.cookie); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(result); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread, |
| + callback, result)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::SetCookieWithOptionsTask( |
| + const SetCookiesCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + bool result = SetCookieWithOptions(*para.url, para.cookie_line, para.options); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(result); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread, |
| + callback, result)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::GetCookiesWithOptionsTask( |
| + const GetCookiesCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + std::string cookie = GetCookiesWithOptions(*para.url, para.options); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(cookie); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeGetCookiesCallbackOnOtherThread, |
| + callback, cookie)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::GetCookiesWithInfoTask( |
| + const GetCookieInfoCallback& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + if (!callback.is_null()) { |
| + std::string cookie_line; |
| + std::vector<CookieInfo> cookie_infos; |
| + GetCookiesWithInfo(*para.url, para.options, &cookie_line, &cookie_infos); |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(&cookie_line, &cookie_infos); |
| + } else { |
| + thread->PostTask( |
| + FROM_HERE, base::Bind(&InvokeGetCookieInfoCallbackOnOtherThread, |
| + callback, cookie_line, cookie_infos)); |
| + } |
| + } |
| +} |
| + |
| +void CookieMonster::DeleteCookieTask( |
| + const base::Closure& callback, |
| + const CookieAPIParameters& para, |
| + scoped_refptr<base::MessageLoopProxy> thread) { |
| + DeleteCookie(*para.url, para.name); |
| + if (!callback.is_null()) { |
| + if (thread->BelongsToCurrentThread()) { |
| + callback.Run(); |
| + } else { |
| + thread->PostTask(FROM_HERE, callback); |
| + } |
| + } |
| +} |
| + |
| bool CookieMonster::SetCookieWithDetails( |
| const GURL& url, const std::string& name, const std::string& value, |
| const std::string& domain, const std::string& path, |
| @@ -576,8 +1014,6 @@ |
| if (!HasCookieableScheme(url)) |
| return false; |
| - InitIfNecessary(); |
| - |
| Time creation_time = CurrentTime(); |
| last_time_seen_ = creation_time; |
| @@ -600,20 +1036,7 @@ |
| return SetCanonicalCookie(&cc, creation_time, options); |
| } |
| -void CookieMonster::SetCookieWithDetailsAsync( |
| - const GURL& url, const std::string& name, const std::string& value, |
| - const std::string& domain, const std::string& path, |
| - const base::Time& expiration_time, bool secure, bool http_only, |
| - const SetCookiesCallback& callback) { |
| - bool success_ = SetCookieWithDetails(url, name, value, domain, path, |
| - expiration_time, secure, http_only); |
| - if (!callback.is_null()) |
| - callback.Run(success_); |
| -} |
| - |
| -bool CookieMonster::InitializeFrom(CookieMonster* cookie_monster) { |
| - net::CookieList list = cookie_monster->GetAllCookies(); |
| - |
| +bool CookieMonster::InitializeFrom(const CookieList& list) { |
| base::AutoLock autolock(lock_); |
| InitIfNecessary(); |
| for (net::CookieList::const_iterator iter = list.begin(); |
| @@ -632,7 +1055,6 @@ |
| CookieList CookieMonster::GetAllCookies() { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| // This function is being called to scrape the cookie list for management UI |
| // or similar. We shouldn't show expired cookies in this list since it will |
| @@ -663,16 +1085,10 @@ |
| return cookie_list; |
| } |
| -void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| - if (!callback.is_null()) |
| - callback.Run(GetAllCookies()); |
| -} |
| - |
| CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
| const GURL& url, |
| const CookieOptions& options) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| std::vector<CanonicalCookie*> cookie_ptrs; |
| FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
| @@ -686,14 +1102,6 @@ |
| return cookies; |
| } |
| -void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
| - const GURL& url, |
| - const CookieOptions& options, |
| - const GetCookieListCallback& callback) { |
| - if (!callback.is_null()) |
| - callback.Run(GetAllCookiesForURLWithOptions(url, options)); |
| -} |
| - |
| CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
| CookieOptions options; |
| options.set_include_httponly(); |
| @@ -701,16 +1109,8 @@ |
| return GetAllCookiesForURLWithOptions(url, options); |
| } |
| -void CookieMonster::GetAllCookiesForURLAsync( |
| - const GURL& url, const GetCookieListCallback& callback) { |
| - if (!callback.is_null()) |
| - callback.Run(GetAllCookiesForURL(url)); |
| -} |
| - |
| int CookieMonster::DeleteAll(bool sync_to_store) { |
| base::AutoLock autolock(lock_); |
| - if (sync_to_store) |
| - InitIfNecessary(); |
| int num_deleted = 0; |
| for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| @@ -729,7 +1129,6 @@ |
| const Time& delete_end, |
| bool sync_to_store) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| int num_deleted = 0; |
| for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| @@ -747,19 +1146,8 @@ |
| return num_deleted; |
| } |
| -void CookieMonster::DeleteAllCreatedBetweenAsync( |
| - const Time& delete_begin, const Time& delete_end, |
| - bool sync_to_store, |
| - const DeleteCallback& callback) { |
| - int num_deleted = DeleteAllCreatedBetween( |
| - delete_begin, delete_end, sync_to_store); |
| - if (!callback.is_null()) |
| - callback.Run(num_deleted); |
| -} |
| - |
| int CookieMonster::DeleteAllForHost(const GURL& url) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| if (!HasCookieableScheme(url)) |
| return 0; |
| @@ -788,16 +1176,8 @@ |
| return num_deleted; |
| } |
| -void CookieMonster::DeleteAllForHostAsync( |
| - const GURL& url, const DeleteCallback& callback) { |
| - int num_deleted = DeleteAllForHost(url); |
| - if (!callback.is_null()) |
| - callback.Run(num_deleted); |
| -} |
| - |
| bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| its.first != its.second; ++its.first) { |
| @@ -810,14 +1190,6 @@ |
| return false; |
| } |
| -void CookieMonster::DeleteCanonicalCookieAsync( |
| - const CanonicalCookie& cookie, |
| - const DeleteCookieCallback& callback) { |
| - bool result = DeleteCanonicalCookie(cookie); |
| - if (!callback.is_null()) |
| - callback.Run(result); |
| -} |
| - |
| void CookieMonster::SetCookieableSchemes( |
| const char* schemes[], size_t num_schemes) { |
| base::AutoLock autolock(lock_); |
| @@ -866,25 +1238,12 @@ |
| return false; |
| } |
| - InitIfNecessary(); |
| - |
| return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| } |
| -void CookieMonster::SetCookieWithOptionsAsync( |
| - const GURL& url, |
| - const std::string& cookie_line, |
| - const CookieOptions& options, |
| - const SetCookiesCallback& callback) { |
| - bool result = SetCookieWithOptions(url, cookie_line, options); |
| - if (!callback.is_null()) |
| - callback.Run(result); |
| -} |
| - |
| std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
| const CookieOptions& options) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| if (!HasCookieableScheme(url)) |
| return std::string(); |
| @@ -904,14 +1263,6 @@ |
| return cookie_line; |
| } |
| -void CookieMonster::GetCookiesWithOptionsAsync( |
| - const GURL& url, const CookieOptions& options, |
| - const GetCookiesCallback& callback) { |
| - std::string cookie = GetCookiesWithOptions(url, options); |
| - if (!callback.is_null()) |
| - callback.Run(cookie); |
| -} |
| - |
| void CookieMonster::GetCookiesWithInfo(const GURL& url, |
| const CookieOptions& options, |
| std::string* cookie_line, |
| @@ -920,7 +1271,6 @@ |
| DCHECK(cookie_infos->empty()); |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| if (!HasCookieableScheme(url)) |
| return; |
| @@ -939,22 +1289,9 @@ |
| histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); |
| } |
| -void CookieMonster::GetCookiesWithInfoAsync( |
| - const GURL& url, |
| - const CookieOptions& options, |
| - const GetCookieInfoCallback& callback) { |
| - std::string cookie_line; |
| - std::vector<CookieInfo> cookie_infos; |
| - GetCookiesWithInfo(url, options, &cookie_line, &cookie_infos); |
| - |
| - if (!callback.is_null()) |
| - callback.Run(&cookie_line, &cookie_infos); |
| -} |
| - |
| void CookieMonster::DeleteCookie(const GURL& url, |
| const std::string& cookie_name) { |
| base::AutoLock autolock(lock_); |
| - InitIfNecessary(); |
| if (!HasCookieableScheme(url)) |
| return; |
| @@ -984,14 +1321,6 @@ |
| } |
| } |
| -void CookieMonster::DeleteCookieAsync(const GURL& url, |
| - const std::string& cookie_name, |
| - const base::Closure& callback) { |
| - DeleteCookie(url, cookie_name); |
| - if (!callback.is_null()) |
| - callback.Run(); |
| -} |
| - |
| CookieMonster* CookieMonster::GetCookieMonster() { |
| return this; |
| } |
| @@ -1003,6 +1332,8 @@ |
| bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
| const std::string& cookie_line, |
| const base::Time& creation_time) { |
| + // Only for CookieMonster unit test usage. |
| + DCHECK(!store_); |
| base::AutoLock autolock(lock_); |
| if (!HasCookieableScheme(url)) { |
| @@ -1016,17 +1347,22 @@ |
| void CookieMonster::InitStore() { |
| DCHECK(store_) << "Store must exist to initialize"; |
| + store_->Load(base::Bind(&CookieMonster::OnLoaded, this)); |
| +} |
| - TimeTicks beginning_time(TimeTicks::Now()); |
| +void CookieMonster::OnLoaded(const std::vector<CanonicalCookie*>& cookies) { |
| + StoreLoadedCookies(cookies); |
| + // Invoke the task queue of cookie request. |
| + InvokeQueue(); |
| +} |
| +void CookieMonster::StoreLoadedCookies( |
| + const std::vector<CanonicalCookie*>& cookies) { |
| // Initialize the store and sync in any saved persistent cookies. We don't |
| // care if it's expired, insert it so it can be garbage collected, removed, |
| // and sync'd. |
| - std::vector<CanonicalCookie*> cookies; |
| - // Reserve space for the maximum amount of cookies a database should have. |
| - // This prevents multiple vector growth / copies as we append cookies. |
| - cookies.reserve(kMaxCookies); |
| - store_->Load(&cookies); |
| + base::AutoLock autolock(lock_); |
| + TimeTicks beginning_time(TimeTicks::Now()); |
| // Avoid ever letting cookies with duplicate creation times into the store; |
| // that way we don't have to worry about what sections of code are safe |
| @@ -1069,6 +1405,22 @@ |
| histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
| } |
| +void CookieMonster::InvokeQueue() { |
| + while (true) { |
| + base::Closure request_task; |
| + { |
| + base::AutoLock autolock(lock_); |
| + if (queue_.empty()) { |
| + loaded_ = true; |
| + break; |
| + } |
| + request_task = queue_.front(); |
| + queue_.pop_front(); |
| + } |
| + request_task.Run(); |
| + } |
| +} |
| + |
| void CookieMonster::EnsureCookiesMapIsValid() { |
| lock_.AssertAcquired(); |
| @@ -2152,6 +2504,31 @@ |
| } |
| CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
| + const GURL& url, |
| + const ParsedCookie& pc) { |
| + if (!pc.IsValid()) { |
| + return NULL; |
| + } |
| + |
| + std::string domain_string; |
| + if (!GetCookieDomain(url, pc, &domain_string)) { |
| + return NULL; |
| + } |
| + std::string path_string = CanonPath(url, pc); |
| + std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); |
| + std::string mac_algorithm = pc.HasMACAlgorithm() ? |
| + pc.MACAlgorithm() : std::string(); |
| + Time creation_time = Time::Now(); |
| + Time expiration_time; |
| + if (pc.HasExpires()) |
| + expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires()); |
| + |
| + return (Create(url, pc.Name(), pc.Value(), domain_string, path_string, |
| + mac_key, mac_algorithm, creation_time, expiration_time, |
| + pc.IsSecure(), pc.IsHttpOnly())); |
| +} |
| + |
| +CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
| const GURL& url, |
| const std::string& name, |
| const std::string& value, |