Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(743)

Unified Diff: net/base/cookie_monster.cc

Issue 7598001: Remove the old synchronous CookieMonster API. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/cookie_monster.cc
===================================================================
--- net/base/cookie_monster.cc (revision 96178)
+++ net/base/cookie_monster.cc (working copy)
@@ -48,10 +48,12 @@
#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"
#include "base/message_loop.h"
+#include "base/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
@@ -419,6 +421,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 +437,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 +571,656 @@
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);
+ }
+}
+
+// Task classes for queueing the coming request.
+
+class CookieMonsterTask
+ : public base::RefCountedThreadSafe<CookieMonsterTask> {
+ public:
+ virtual void Run() = 0;
+ void InvokeCallback(base::Closure callback);
erikwright (departed) 2011/08/12 16:00:34 InvokeCallback and cookie_monster -> protected
ycxiao 2011/08/12 17:51:25 Done.
+ CookieMonster* cookie_monster() {
+ return cookie_monster_;
+ }
+
+ protected:
+ CookieMonsterTask();
erikwright (departed) 2011/08/12 16:00:34 Add constructor param for cookie_monster_, initial
ycxiao 2011/08/12 17:51:25 Done.
+ virtual ~CookieMonsterTask();
+
+ CookieMonster* cookie_monster_;
erikwright (departed) 2011/08/12 16:00:34 cookie_monster_, thread_ -> private
ycxiao 2011/08/12 17:51:25 Done.
+ scoped_refptr<base::MessageLoopProxy> thread_;
+ friend class base::RefCountedThreadSafe<CookieMonsterTask>;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
+};
+
+CookieMonsterTask::CookieMonsterTask()
+ : thread_(base::MessageLoopProxy::CreateForCurrentThread()) { }
+
+CookieMonsterTask::~CookieMonsterTask() { }
+
+void CookieMonsterTask::InvokeCallback(base::Closure callback) {
+ if (!callback.is_null()) {
+ if (thread_->BelongsToCurrentThread()) {
+ callback.Run();
+ } else {
+ thread_->PostTask(FROM_HERE, callback);
+ }
+ }
+}
+
+// Task class for SetCookieWithDetails call.
+class SetCookieWithDetailsTask : public CookieMonsterTask {
+ public:
+ SetCookieWithDetailsTask(
+ CookieMonster* cookie_monster,
+ 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 CookieMonster::SetCookiesCallback& callback);
+
+ virtual ~SetCookieWithDetailsTask() { }
erikwright (departed) 2011/08/12 16:00:34 protected or no declaration. (For each subclass.)
ycxiao 2011/08/12 17:51:25 Done.
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ std::string name_;
+ std::string value_;
+ std::string domain_;
+ std::string path_;
+ base::Time expiration_time_;
+ bool secure_;
+ bool http_only_;
+ CookieMonster::SetCookiesCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
+};
+
+SetCookieWithDetailsTask::SetCookieWithDetailsTask(
erikwright (departed) 2011/08/12 16:00:34 I think it's reasonable to inline the constructor
ycxiao 2011/08/12 17:51:25 Done.
+ CookieMonster* cookie_monster,
+ 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 CookieMonster::SetCookiesCallback& callback)
+ : url_(url),
+ name_(name),
+ value_(value),
+ domain_(domain),
+ path_(path),
+ expiration_time_(expiration_time),
+ secure_(secure),
+ http_only_(http_only),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void SetCookieWithDetailsTask::Run() {
+ bool success = this->cookie_monster()->
+ SetCookieWithDetails(url_, name_, value_, domain_, path_,
+ expiration_time_, secure_, http_only_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
+ callback_, success));
+ }
+}
+
+// Task class for GetAllCookies call.
+class GetAllCookiesTask : public CookieMonsterTask {
+ public:
+ GetAllCookiesTask(CookieMonster* cookie_monster,
+ const CookieMonster::GetCookieListCallback& callback);
+ virtual ~GetAllCookiesTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ CookieMonster::GetCookieListCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
+};
+
+GetAllCookiesTask::GetAllCookiesTask(
+ CookieMonster* cookie_monster,
+ const CookieMonster::GetCookieListCallback& callback)
+ : callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void GetAllCookiesTask::Run() {
+ if (!callback_.is_null()) {
+ CookieList cookies = this->cookie_monster()->GetAllCookies();
+ this->InvokeCallback(base::Bind(&InvokeGetCookieListCallbackOnOtherThread,
+ callback_, cookies));
+ }
+}
+
+// Task class for GetAllCookiesForURLWithOptions call.
+class GetAllCookiesForURLWithOptionsTask : public CookieMonsterTask {
+ public:
+ GetAllCookiesForURLWithOptionsTask(
+ CookieMonster* cookie_monster,
+ const GURL& url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookieListCallback& callback);
+
+ virtual ~GetAllCookiesForURLWithOptionsTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ CookieOptions options_;
+ CookieMonster::GetCookieListCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
+};
+
+GetAllCookiesForURLWithOptionsTask::GetAllCookiesForURLWithOptionsTask(
+ CookieMonster* cookie_monster,
+ const GURL& url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookieListCallback& callback)
+ : url_(url),
+ options_(options),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void GetAllCookiesForURLWithOptionsTask::Run() {
+ if (!callback_.is_null()) {
+ CookieList cookies = this->cookie_monster()->
+ GetAllCookiesForURLWithOptions(url_, options_);
+ this->InvokeCallback(base::Bind(&InvokeGetCookieListCallbackOnOtherThread,
+ callback_, cookies));
+ }
+}
+
+// Task class for DeleteAll call.
+class DeleteAllTask : public CookieMonsterTask {
+ public:
+ DeleteAllTask(CookieMonster* cookie_monster,
+ bool sync_to_store,
+ const CookieMonster::DeleteCallback& callback);
+
+ virtual ~DeleteAllTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ bool sync_to_store_;
+ CookieMonster::DeleteCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
+};
+
+DeleteAllTask::DeleteAllTask(CookieMonster* cookie_monster,
+ bool sync_to_store,
erikwright (departed) 2011/08/12 16:00:34 With BrowsingDataCookieHelper, do we still need sy
ycxiao 2011/08/12 17:51:25 The CookieMonster Unittest use DeleteAllAsync(fals
+ const CookieMonster::DeleteCallback& callback)
+ : sync_to_store_(sync_to_store),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void DeleteAllTask::Run() {
+ int num_deleted = this->cookie_monster()->DeleteAll(sync_to_store_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread,
+ callback_, num_deleted));
+ }
+}
+
+// Task class for DeleteAllCreatedBetween call.
+class DeleteAllCreatedBetweenTask : public CookieMonsterTask {
+ public:
+ DeleteAllCreatedBetweenTask(
+ CookieMonster* cookie_monster,
+ const Time& delete_begin,
+ const Time& delete_end,
+ bool sync_to_store,
+ const CookieMonster::DeleteCallback& callback);
+
+ virtual ~DeleteAllCreatedBetweenTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ Time delete_begin_;
+ Time delete_end_;
+ bool sync_to_store_;
+ CookieMonster::DeleteCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
+};
+
+DeleteAllCreatedBetweenTask::DeleteAllCreatedBetweenTask(
+ CookieMonster* cookie_monster,
+ const Time& delete_begin,
+ const Time& delete_end,
+ bool sync_to_store,
+ const CookieMonster::DeleteCallback& callback)
+ : delete_begin_(delete_begin),
+ delete_end_(delete_end),
+ sync_to_store_(sync_to_store),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void DeleteAllCreatedBetweenTask::Run() {
+ int num_deleted = this->cookie_monster()->
+ DeleteAllCreatedBetween(delete_begin_, delete_end_, sync_to_store_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread,
+ callback_, num_deleted));
+ }
+}
+
+// Task class for DeleteAllForHost call.
+class DeleteAllForHostTask : public CookieMonsterTask {
+ public:
+ DeleteAllForHostTask(CookieMonster* cookie_monster,
+ const GURL& url,
+ const CookieMonster::DeleteCallback& callback);
+
+ virtual ~DeleteAllForHostTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ CookieMonster::DeleteCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
+};
+
+DeleteAllForHostTask::DeleteAllForHostTask(
+ CookieMonster* cookie_monster,
+ const GURL& url,
+ const CookieMonster::DeleteCallback& callback)
+ : url_(url),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void DeleteAllForHostTask::Run() {
+ int num_deleted = this->cookie_monster()->DeleteAllForHost(url_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread,
+ callback_, num_deleted));
+ }
+}
+
+// Task class for DeleteCanonicalCookie call.
+class DeleteCanonicalCookieTask : public CookieMonsterTask {
+ public:
+ DeleteCanonicalCookieTask(
+ CookieMonster* cookie_monster,
+ const CookieMonster::CanonicalCookie& cookie,
+ const CookieMonster::DeleteCookieCallback& callback);
+ virtual ~DeleteCanonicalCookieTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ CookieMonster::CanonicalCookie cookie_;
+ CookieMonster::DeleteCookieCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
+};
+
+DeleteCanonicalCookieTask::DeleteCanonicalCookieTask(
+ CookieMonster* cookie_monster,
+ const CookieMonster::CanonicalCookie& cookie,
+ const CookieMonster::DeleteCookieCallback& callback)
+ : cookie_(cookie),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void DeleteCanonicalCookieTask::Run() {
+ bool result = this->cookie_monster()->DeleteCanonicalCookie(cookie_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
+ callback_, result));
+ }
+}
+
+// Task class for SetCookieWithOptions call.
+class SetCookieWithOptionsTask : public CookieMonsterTask {
+ public:
+ SetCookieWithOptionsTask(CookieMonster* cookie_monster,
+ const GURL& url,
+ const std::string& cookie_line,
+ const CookieOptions& options,
+ const CookieMonster::SetCookiesCallback& callback);
+ virtual ~SetCookieWithOptionsTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ std::string cookie_line_;
+ CookieOptions options_;
+ CookieMonster::SetCookiesCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
+};
+
+SetCookieWithOptionsTask::SetCookieWithOptionsTask(
+ CookieMonster* cookie_monster,
+ const GURL& url,
+ const std::string& cookie_line,
+ const CookieOptions& options,
+ const CookieMonster::SetCookiesCallback& callback)
+ : url_(url),
+ cookie_line_(cookie_line),
+ options_(options),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void SetCookieWithOptionsTask::Run() {
+ bool result = this->cookie_monster()->
+ SetCookieWithOptions(url_, cookie_line_, options_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
+ callback_, result));
+ }
+}
+
+// Task class for GetCookiesWithOptions call.
+class GetCookiesWithOptionsTask : public CookieMonsterTask {
+ public:
+ GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
+ GURL url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookiesCallback& callback);
+ virtual ~GetCookiesWithOptionsTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ CookieOptions options_;
+ CookieMonster::GetCookiesCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
+};
+
+GetCookiesWithOptionsTask::GetCookiesWithOptionsTask(
+ CookieMonster* cookie_monster,
+ GURL url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookiesCallback& callback)
+ : url_(url),
+ options_(options),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void GetCookiesWithOptionsTask::Run() {
+ std::string cookie = this->cookie_monster()->
+ GetCookiesWithOptions(url_, options_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(base::Bind(&InvokeGetCookiesCallbackOnOtherThread,
+ callback_, cookie));
+ }
+}
+
+// Task class for GetCookiesWithInfo call.
+class GetCookiesWithInfoTask : public CookieMonsterTask {
+ public:
+ GetCookiesWithInfoTask(CookieMonster* cookie_monster,
+ GURL url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookieInfoCallback& callback);
+ virtual ~GetCookiesWithInfoTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ CookieOptions options_;
+ CookieMonster::GetCookieInfoCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(GetCookiesWithInfoTask);
+};
+
+GetCookiesWithInfoTask::GetCookiesWithInfoTask(
+ CookieMonster* cookie_monster,
+ GURL url,
+ const CookieOptions& options,
+ const CookieMonster::GetCookieInfoCallback& callback)
+ : url_(url),
+ options_(options),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void GetCookiesWithInfoTask::Run() {
+ if (!callback_.is_null()) {
+ std::string cookie_line;
+ std::vector<CookieMonster::CookieInfo> cookie_infos;
+ this->cookie_monster()->
+ GetCookiesWithInfo(url_, options_, &cookie_line, &cookie_infos);
+ this->InvokeCallback(base::Bind(&InvokeGetCookieInfoCallbackOnOtherThread,
+ callback_, cookie_line, cookie_infos));
+ }
+}
+
+// Task class for DeleteCookie call.
+class DeleteCookieTask : public CookieMonsterTask {
+ public:
+ DeleteCookieTask(CookieMonster* cookie_monster,
+ GURL url,
+ const std::string& cookie_name,
+ const base::Closure& callback);
+ virtual ~DeleteCookieTask() { }
+
+ virtual void Run() OVERRIDE;
+
+ private:
+ GURL url_;
+ std::string cookie_name_;
+ base::Closure callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
+};
+
+DeleteCookieTask::DeleteCookieTask(CookieMonster* cookie_monster,
+ GURL url,
+ const std::string& cookie_name,
+ const base::Closure& callback)
+ : url_(url),
+ cookie_name_(cookie_name),
+ callback_(callback) {
+ cookie_monster_ = cookie_monster;
+}
+
+void DeleteCookieTask::Run() {
+ this->cookie_monster()->DeleteCookie(url_, cookie_name_);
+ if (!callback_.is_null()) {
+ this->InvokeCallback(callback_);
+ }
+}
+
+// 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) {
+ scoped_refptr<SetCookieWithDetailsTask> task =
+ new SetCookieWithDetailsTask(this, url, name, value, domain, path,
+ expiration_time, secure, http_only,
+ callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
+ scoped_refptr<GetAllCookiesTask> task =
+ new GetAllCookiesTask(this, callback);
+
+ DoCookieTask(task);
+}
+
+
+void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
+ const GURL& url,
+ const CookieOptions& options,
+ const GetCookieListCallback& callback) {
+ scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
+ new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::GetAllCookiesForURLAsync(
+ const GURL& url, const GetCookieListCallback& callback) {
+ CookieOptions options;
+ options.set_include_httponly();
+ scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
+ new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DeleteAllAsync(bool sync_to_store,
+ const DeleteCallback& callback) {
+ scoped_refptr<DeleteAllTask> task =
+ new DeleteAllTask(this, sync_to_store, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DeleteAllCreatedBetweenAsync(
+ const Time& delete_begin, const Time& delete_end,
+ bool sync_to_store,
+ const DeleteCallback& callback) {
+ scoped_refptr<DeleteAllCreatedBetweenTask> task =
+ new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
+ sync_to_store, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DeleteAllForHostAsync(
+ const GURL& url, const DeleteCallback& callback) {
+ scoped_refptr<DeleteAllForHostTask> task =
+ new DeleteAllForHostTask(this, url, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DeleteCanonicalCookieAsync(
+ const CanonicalCookie& cookie,
+ const DeleteCookieCallback& callback) {
+ scoped_refptr<DeleteCanonicalCookieTask> task =
+ new DeleteCanonicalCookieTask(this, cookie, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::SetCookieWithOptionsAsync(
+ const GURL& url,
+ const std::string& cookie_line,
+ const CookieOptions& options,
+ const SetCookiesCallback& callback) {
+ scoped_refptr<SetCookieWithOptionsTask> task =
+ new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::GetCookiesWithOptionsAsync(
+ const GURL& url,
+ const CookieOptions& options,
+ const GetCookiesCallback& callback) {
+ scoped_refptr<GetCookiesWithOptionsTask> task =
+ new GetCookiesWithOptionsTask(this, url, options, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::GetCookiesWithInfoAsync(
+ const GURL& url,
+ const CookieOptions& options,
+ const GetCookieInfoCallback& callback) {
+ scoped_refptr<GetCookiesWithInfoTask> task =
+ new GetCookiesWithInfoTask(this, url, options, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DeleteCookieAsync(const GURL& url,
+ const std::string& cookie_name,
+ const base::Closure& callback) {
+ scoped_refptr<DeleteCookieTask> task =
+ new DeleteCookieTask(this, url, cookie_name, callback);
+
+ DoCookieTask(task);
+}
+
+void CookieMonster::DoCookieTask(scoped_refptr<CookieMonsterTask> task_item) {
+ base::Closure task = base::Bind(&CookieMonsterTask::Run, task_item.get());
+ DCHECK(!task.is_null());
erikwright (departed) 2011/08/12 16:00:34 Remove the DCHECK, and perhaps move the bind to li
ycxiao 2011/08/12 17:51:25 Done.
+ InitIfNecessary();
+ bool loaded = false;
+ {
+ base::AutoLock autolock(lock_);
+ loaded = loaded_;
+ }
+ if (loaded) {
+ task.Run();
+ } else {
+ base::AutoLock autolock(lock_);
+ queue_.push(task);
+ }
+}
+
bool CookieMonster::SetCookieWithDetails(
const GURL& url, const std::string& name, const std::string& value,
const std::string& domain, const std::string& path,
@@ -576,8 +1230,6 @@
if (!HasCookieableScheme(url))
return false;
- InitIfNecessary();
-
Time creation_time = CurrentTime();
last_time_seen_ = creation_time;
@@ -600,20 +1252,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 +1271,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 +1301,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 +1318,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 +1325,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 +1345,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 +1362,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 +1392,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 +1406,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 +1454,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 +1479,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 +1487,6 @@
DCHECK(cookie_infos->empty());
base::AutoLock autolock(lock_);
- InitIfNecessary();
if (!HasCookieableScheme(url))
return;
@@ -939,22 +1505,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 +1537,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 +1548,7 @@
bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
const std::string& cookie_line,
const base::Time& creation_time) {
+ DCHECK(!store_) << "This method is only to be used by unit-tests.";
base::AutoLock autolock(lock_);
if (!HasCookieableScheme(url)) {
@@ -1016,17 +1562,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 +1620,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();
+ }
+ request_task.Run();
+ }
+}
+
void CookieMonster::EnsureCookiesMapIsValid() {
lock_.AssertAcquired();
@@ -2152,6 +2719,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,

Powered by Google App Engine
This is Rietveld 408576698