1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/net/sqlite_persistent_cookie_store.h"
12 #include "base/basictypes.h"
13 #include "base/bind.h"
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/location.h"
18 #include "base/logging.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/metrics/histogram.h"
23 #include "base/sequenced_task_runner.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h"
26 #include "base/synchronization/lock.h"
27 #include "base/threading/sequenced_worker_pool.h"
28 #include "base/time/time.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/cookie_store_factory.h"
31 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
32 #include "net/cookies/canonical_cookie.h"
33 #include "net/cookies/cookie_constants.h"
34 #include "net/cookies/cookie_util.h"
35 #include "net/extras/sqlite/cookie_crypto_delegate.h"
36 #include "sql/error_delegate_util.h"
37 #include "sql/meta_table.h"
38 #include "sql/statement.h"
39 #include "sql/transaction.h"
40 #include "storage/browser/quota/special_storage_policy.h"
41 #include "third_party/sqlite/sqlite3.h"
48 // The persistent cookie store is loaded into memory on eTLD at a time. This
49 // variable controls the delay between loading eTLDs, so as to not overload the
50 // CPU or I/O with these low priority requests immediately after start up.
51 const int kLoadDelayMilliseconds
= 0;
57 // This class is designed to be shared between any client thread and the
58 // background task runner. It batches operations and commits them on a timer.
60 // SQLitePersistentCookieStore::Load is called to load all cookies. It
61 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread
62 // task to the background runner. This task calls Backend::ChainLoadCookies(),
63 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies
64 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is
65 // posted to the client runner, which notifies the caller of
66 // SQLitePersistentCookieStore::Load that the load is complete.
68 // If a priority load request is invoked via SQLitePersistentCookieStore::
69 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts
70 // Backend::LoadKeyAndNotifyOnDBThread to the BG runner. That routine loads just
71 // that single domain key (eTLD+1)'s cookies, and posts a Backend::
72 // CompleteLoadForKeyOnIOThread to the client runner to notify the caller of
73 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete.
75 // Subsequent to loading, mutations may be queued by any thread using
76 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to
77 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(),
78 // whichever occurs first.
79 class SQLitePersistentCookieStore::Backend
80 : public base::RefCountedThreadSafe
<SQLitePersistentCookieStore::Backend
> {
83 const base::FilePath
& path
,
84 const scoped_refptr
<base::SequencedTaskRunner
>& client_task_runner
,
85 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
86 bool restore_old_session_cookies
,
87 storage::SpecialStoragePolicy
* special_storage_policy
,
88 net::CookieCryptoDelegate
* crypto_delegate
)
91 force_keep_session_state_(false),
93 corruption_detected_(false),
94 restore_old_session_cookies_(restore_old_session_cookies
),
95 special_storage_policy_(special_storage_policy
),
97 client_task_runner_(client_task_runner
),
98 background_task_runner_(background_task_runner
),
99 num_priority_waiting_(0),
100 total_priority_requests_(0),
101 crypto_(crypto_delegate
) {}
103 // Creates or loads the SQLite database.
104 void Load(const LoadedCallback
& loaded_callback
);
106 // Loads cookies for the domain key (eTLD+1).
107 void LoadCookiesForKey(const std::string
& domain
,
108 const LoadedCallback
& loaded_callback
);
110 // Steps through all results of |smt|, makes a cookie from each, and adds the
111 // cookie to |cookies|. This method also updates |cookies_per_origin_| and
112 // |num_cookies_read_|.
113 void MakeCookiesFromSQLStatement(std::vector
<net::CanonicalCookie
*>* cookies
,
114 sql::Statement
* statement
);
116 // Batch a cookie addition.
117 void AddCookie(const net::CanonicalCookie
& cc
);
119 // Batch a cookie access time update.
120 void UpdateCookieAccessTime(const net::CanonicalCookie
& cc
);
122 // Batch a cookie deletion.
123 void DeleteCookie(const net::CanonicalCookie
& cc
);
125 // Commit pending operations as soon as possible.
126 void Flush(const base::Closure
& callback
);
128 // Commit any pending operations and close the database. This must be called
129 // before the object is destructed.
132 void SetForceKeepSessionState();
135 friend class base::RefCountedThreadSafe
<SQLitePersistentCookieStore::Backend
>;
137 // You should call Close() before destructing this object.
139 DCHECK(!db_
.get()) << "Close should have already been called.";
140 DCHECK(num_pending_
== 0 && pending_
.empty());
143 // Database upgrade statements.
144 bool EnsureDatabaseVersion();
146 class PendingOperation
{
154 PendingOperation(OperationType op
, const net::CanonicalCookie
& cc
)
155 : op_(op
), cc_(cc
) { }
157 OperationType
op() const { return op_
; }
158 const net::CanonicalCookie
& cc() const { return cc_
; }
162 net::CanonicalCookie cc_
;
166 // Creates or loads the SQLite database on background runner.
167 void LoadAndNotifyInBackground(const LoadedCallback
& loaded_callback
,
168 const base::Time
& posted_at
);
170 // Loads cookies for the domain key (eTLD+1) on background runner.
171 void LoadKeyAndNotifyInBackground(const std::string
& domains
,
172 const LoadedCallback
& loaded_callback
,
173 const base::Time
& posted_at
);
175 // Notifies the CookieMonster when loading completes for a specific domain key
176 // or for all domain keys. Triggers the callback and passes it all cookies
177 // that have been loaded from DB since last IO notification.
178 void Notify(const LoadedCallback
& loaded_callback
, bool load_success
);
180 // Sends notification when the entire store is loaded, and reports metrics
181 // for the total time to load and aggregated results from any priority loads
183 void CompleteLoadInForeground(const LoadedCallback
& loaded_callback
,
186 // Sends notification when a single priority load completes. Updates priority
187 // load metric data. The data is sent only after the final load completes.
188 void CompleteLoadForKeyInForeground(const LoadedCallback
& loaded_callback
,
190 const base::Time
& requested_at
);
192 // Sends all metrics, including posting a ReportMetricsInBackground task.
193 // Called after all priority and regular loading is complete.
194 void ReportMetrics();
196 // Sends background-runner owned metrics (i.e., the combined duration of all
198 void ReportMetricsInBackground();
200 // Initialize the data base.
201 bool InitializeDatabase();
203 // Loads cookies for the next domain key from the DB, then either reschedules
204 // itself or schedules the provided callback to run on the client runner (if
205 // all domains are loaded).
206 void ChainLoadCookies(const LoadedCallback
& loaded_callback
);
208 // Load all cookies for a set of domains/hosts
209 bool LoadCookiesForDomains(const std::set
<std::string
>& key
);
211 // Batch a cookie operation (add or delete)
212 void BatchOperation(PendingOperation::OperationType op
,
213 const net::CanonicalCookie
& cc
);
214 // Commit our pending operations to the database.
216 // Close() executed on the background runner.
217 void InternalBackgroundClose();
219 void DeleteSessionCookiesOnStartup();
221 void DeleteSessionCookiesOnShutdown();
223 void DatabaseErrorCallback(int error
, sql::Statement
* stmt
);
226 void PostBackgroundTask(const tracked_objects::Location
& origin
,
227 const base::Closure
& task
);
228 void PostClientTask(const tracked_objects::Location
& origin
,
229 const base::Closure
& task
);
231 // Shared code between the different load strategies to be used after all
232 // cookies have been loaded.
233 void FinishedLoadingCookies(const LoadedCallback
& loaded_callback
,
236 base::FilePath path_
;
237 scoped_ptr
<sql::Connection
> db_
;
238 sql::MetaTable meta_table_
;
240 typedef std::list
<PendingOperation
*> PendingOperationsList
;
241 PendingOperationsList pending_
;
242 PendingOperationsList::size_type num_pending_
;
243 // True if the persistent store should skip delete on exit rules.
244 bool force_keep_session_state_
;
245 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_|
248 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce
249 // the number of messages sent to the client runner. Sent back in response to
250 // individual load requests for domain keys or when all loading completes.
251 std::vector
<net::CanonicalCookie
*> cookies_
;
253 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB.
254 std::map
<std::string
, std::set
<std::string
> > keys_to_load_
;
256 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the
258 typedef std::pair
<std::string
, bool> CookieOrigin
;
259 typedef std::map
<CookieOrigin
, int> CookiesPerOriginMap
;
260 CookiesPerOriginMap cookies_per_origin_
;
262 // Indicates if DB has been initialized.
265 // Indicates if the kill-database callback has been scheduled.
266 bool corruption_detected_
;
268 // If false, we should filter out session cookies when reading the DB.
269 bool restore_old_session_cookies_
;
271 // Policy defining what data is deleted on shutdown.
272 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
274 // The cumulative time spent loading the cookies on the background runner.
275 // Incremented and reported from the background runner.
276 base::TimeDelta cookie_load_duration_
;
278 // The total number of cookies read. Incremented and reported on the
279 // background runner.
280 int num_cookies_read_
;
282 scoped_refptr
<base::SequencedTaskRunner
> client_task_runner_
;
283 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner_
;
285 // Guards the following metrics-related properties (only accessed when
286 // starting/completing priority loads or completing the total load).
287 base::Lock metrics_lock_
;
288 int num_priority_waiting_
;
289 // The total number of priority requests.
290 int total_priority_requests_
;
291 // The time when |num_priority_waiting_| incremented to 1.
292 base::Time current_priority_wait_start_
;
293 // The cumulative duration of time when |num_priority_waiting_| was greater
295 base::TimeDelta priority_wait_duration_
;
296 // Class with functions that do cryptographic operations (for protecting
297 // cookies stored persistently).
300 net::CookieCryptoDelegate
* crypto_
;
302 DISALLOW_COPY_AND_ASSIGN(Backend
);
307 // Version number of the database.
309 // Version 8 adds "first-party only" cookies.
311 // Version 7 adds encrypted values. Old values will continue to be used but
312 // all new values written will be encrypted on selected operating systems. New
313 // records read by old clients will simply get an empty cookie value while old
314 // records read by new clients will continue to operate with the unencrypted
315 // version. New and old clients alike will always write/update records with
316 // what they support.
318 // Version 6 adds cookie priorities. This allows developers to influence the
319 // order in which cookies are evicted in order to meet domain cookie limits.
321 // Version 5 adds the columns has_expires and is_persistent, so that the
322 // database can store session cookies as well as persistent cookies. Databases
323 // of version 5 are incompatible with older versions of code. If a database of
324 // version 5 is read by older code, session cookies will be treated as normal
325 // cookies. Currently, these fields are written, but not read anymore.
327 // In version 4, we migrated the time epoch. If you open the DB with an older
328 // version on Mac or Linux, the times will look wonky, but the file will likely
329 // be usable. On Windows version 3 and 4 are the same.
331 // Version 3 updated the database to include the last access time, so we can
332 // expire them in decreasing order of use when we've reached the maximum
333 // number of cookies.
334 const int kCurrentVersionNumber
= 8;
335 const int kCompatibleVersionNumber
= 5;
337 // Possible values for the 'priority' column.
338 enum DBCookiePriority
{
339 kCookiePriorityLow
= 0,
340 kCookiePriorityMedium
= 1,
341 kCookiePriorityHigh
= 2,
344 DBCookiePriority
CookiePriorityToDBCookiePriority(net::CookiePriority value
) {
346 case net::COOKIE_PRIORITY_LOW
:
347 return kCookiePriorityLow
;
348 case net::COOKIE_PRIORITY_MEDIUM
:
349 return kCookiePriorityMedium
;
350 case net::COOKIE_PRIORITY_HIGH
:
351 return kCookiePriorityHigh
;
355 return kCookiePriorityMedium
;
358 net::CookiePriority
DBCookiePriorityToCookiePriority(DBCookiePriority value
) {
360 case kCookiePriorityLow
:
361 return net::COOKIE_PRIORITY_LOW
;
362 case kCookiePriorityMedium
:
363 return net::COOKIE_PRIORITY_MEDIUM
;
364 case kCookiePriorityHigh
:
365 return net::COOKIE_PRIORITY_HIGH
;
369 return net::COOKIE_PRIORITY_DEFAULT
;
372 // Increments a specified TimeDelta by the duration between this object's
373 // constructor and destructor. Not thread safe. Multiple instances may be
374 // created with the same delta instance as long as their lifetimes are nested.
375 // The shortest lived instances have no impact.
376 class IncrementTimeDelta
{
378 explicit IncrementTimeDelta(base::TimeDelta
* delta
) :
380 original_value_(*delta
),
381 start_(base::Time::Now()) {}
383 ~IncrementTimeDelta() {
384 *delta_
= original_value_
+ base::Time::Now() - start_
;
388 base::TimeDelta
* delta_
;
389 base::TimeDelta original_value_
;
392 DISALLOW_COPY_AND_ASSIGN(IncrementTimeDelta
);
395 // Initializes the cookies table, returning true on success.
396 bool InitTable(sql::Connection
* db
) {
397 if (!db
->DoesTableExist("cookies")) {
398 std::string
stmt(base::StringPrintf(
399 "CREATE TABLE cookies ("
400 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY,"
401 "host_key TEXT NOT NULL,"
402 "name TEXT NOT NULL,"
403 "value TEXT NOT NULL,"
404 "path TEXT NOT NULL,"
405 "expires_utc INTEGER NOT NULL,"
406 "secure INTEGER NOT NULL,"
407 "httponly INTEGER NOT NULL,"
408 "last_access_utc INTEGER NOT NULL, "
409 "has_expires INTEGER NOT NULL DEFAULT 1, "
410 "persistent INTEGER NOT NULL DEFAULT 1,"
411 "priority INTEGER NOT NULL DEFAULT %d,"
412 "encrypted_value BLOB DEFAULT '',"
413 "firstpartyonly INTEGER NOT NULL DEFAULT 0)",
414 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT
)));
415 if (!db
->Execute(stmt
.c_str()))
419 // Older code created an index on creation_utc, which is already
420 // primary key for the table.
421 if (!db
->Execute("DROP INDEX IF EXISTS cookie_times"))
424 if (!db
->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)"))
432 void SQLitePersistentCookieStore::Backend::Load(
433 const LoadedCallback
& loaded_callback
) {
434 // This function should be called only once per instance.
436 PostBackgroundTask(FROM_HERE
, base::Bind(
437 &Backend::LoadAndNotifyInBackground
, this,
438 loaded_callback
, base::Time::Now()));
441 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey(
442 const std::string
& key
,
443 const LoadedCallback
& loaded_callback
) {
445 base::AutoLock
locked(metrics_lock_
);
446 if (num_priority_waiting_
== 0)
447 current_priority_wait_start_
= base::Time::Now();
448 num_priority_waiting_
++;
449 total_priority_requests_
++;
452 PostBackgroundTask(FROM_HERE
, base::Bind(
453 &Backend::LoadKeyAndNotifyInBackground
,
454 this, key
, loaded_callback
, base::Time::Now()));
457 void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground(
458 const LoadedCallback
& loaded_callback
, const base::Time
& posted_at
) {
459 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
460 IncrementTimeDelta
increment(&cookie_load_duration_
);
462 UMA_HISTOGRAM_CUSTOM_TIMES(
463 "Cookie.TimeLoadDBQueueWait",
464 base::Time::Now() - posted_at
,
465 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
468 if (!InitializeDatabase()) {
469 PostClientTask(FROM_HERE
, base::Bind(
470 &Backend::CompleteLoadInForeground
, this, loaded_callback
, false));
472 ChainLoadCookies(loaded_callback
);
476 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground(
477 const std::string
& key
,
478 const LoadedCallback
& loaded_callback
,
479 const base::Time
& posted_at
) {
480 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
481 IncrementTimeDelta
increment(&cookie_load_duration_
);
483 UMA_HISTOGRAM_CUSTOM_TIMES(
484 "Cookie.TimeKeyLoadDBQueueWait",
485 base::Time::Now() - posted_at
,
486 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
489 bool success
= false;
490 if (InitializeDatabase()) {
491 std::map
<std::string
, std::set
<std::string
> >::iterator
492 it
= keys_to_load_
.find(key
);
493 if (it
!= keys_to_load_
.end()) {
494 success
= LoadCookiesForDomains(it
->second
);
495 keys_to_load_
.erase(it
);
501 PostClientTask(FROM_HERE
, base::Bind(
502 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground
,
503 this, loaded_callback
, success
, posted_at
));
506 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground(
507 const LoadedCallback
& loaded_callback
,
509 const::Time
& requested_at
) {
510 DCHECK(client_task_runner_
->RunsTasksOnCurrentThread());
512 UMA_HISTOGRAM_CUSTOM_TIMES(
513 "Cookie.TimeKeyLoadTotalWait",
514 base::Time::Now() - requested_at
,
515 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
518 Notify(loaded_callback
, load_success
);
521 base::AutoLock
locked(metrics_lock_
);
522 num_priority_waiting_
--;
523 if (num_priority_waiting_
== 0) {
524 priority_wait_duration_
+=
525 base::Time::Now() - current_priority_wait_start_
;
531 void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() {
532 UMA_HISTOGRAM_CUSTOM_TIMES(
534 cookie_load_duration_
,
535 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
539 void SQLitePersistentCookieStore::Backend::ReportMetrics() {
540 PostBackgroundTask(FROM_HERE
, base::Bind(
541 &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground
, this));
544 base::AutoLock
locked(metrics_lock_
);
545 UMA_HISTOGRAM_CUSTOM_TIMES(
546 "Cookie.PriorityBlockingTime",
547 priority_wait_duration_
,
548 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
551 UMA_HISTOGRAM_COUNTS_100(
552 "Cookie.PriorityLoadCount",
553 total_priority_requests_
);
555 UMA_HISTOGRAM_COUNTS_10000(
556 "Cookie.NumberOfLoadedCookies",
561 void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground(
562 const LoadedCallback
& loaded_callback
, bool load_success
) {
563 Notify(loaded_callback
, load_success
);
569 void SQLitePersistentCookieStore::Backend::Notify(
570 const LoadedCallback
& loaded_callback
,
572 DCHECK(client_task_runner_
->RunsTasksOnCurrentThread());
574 std::vector
<net::CanonicalCookie
*> cookies
;
576 base::AutoLock
locked(lock_
);
577 cookies
.swap(cookies_
);
580 loaded_callback
.Run(cookies
);
583 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() {
584 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
586 if (initialized_
|| corruption_detected_
) {
587 // Return false if we were previously initialized but the DB has since been
588 // closed, or if corruption caused a database reset during initialization.
592 base::Time start
= base::Time::Now();
594 const base::FilePath dir
= path_
.DirName();
595 if (!base::PathExists(dir
) && !base::CreateDirectory(dir
)) {
600 if (base::GetFileSize(path_
, &db_size
))
601 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size
/ 1024 );
603 db_
.reset(new sql::Connection
);
604 db_
->set_histogram_tag("Cookie");
606 // Unretained to avoid a ref loop with |db_|.
607 db_
->set_error_callback(
608 base::Bind(&SQLitePersistentCookieStore::Backend::DatabaseErrorCallback
,
609 base::Unretained(this)));
611 if (!db_
->Open(path_
)) {
612 NOTREACHED() << "Unable to open cookie DB.";
613 if (corruption_detected_
)
620 if (!EnsureDatabaseVersion() || !InitTable(db_
.get())) {
621 NOTREACHED() << "Unable to open cookie DB.";
622 if (corruption_detected_
)
629 UMA_HISTOGRAM_CUSTOM_TIMES(
630 "Cookie.TimeInitializeDB",
631 base::Time::Now() - start
,
632 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
635 start
= base::Time::Now();
637 // Retrieve all the domains
638 sql::Statement
smt(db_
->GetUniqueStatement(
639 "SELECT DISTINCT host_key FROM cookies"));
641 if (!smt
.is_valid()) {
642 if (corruption_detected_
)
649 std::vector
<std::string
> host_keys
;
651 host_keys
.push_back(smt
.ColumnString(0));
653 UMA_HISTOGRAM_CUSTOM_TIMES(
654 "Cookie.TimeLoadDomains",
655 base::Time::Now() - start
,
656 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
659 base::Time start_parse
= base::Time::Now();
661 // Build a map of domain keys (always eTLD+1) to domains.
662 for (size_t idx
= 0; idx
< host_keys
.size(); ++idx
) {
663 const std::string
& domain
= host_keys
[idx
];
665 net::registry_controlled_domains::GetDomainAndRegistry(
667 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
669 keys_to_load_
[key
].insert(domain
);
672 UMA_HISTOGRAM_CUSTOM_TIMES(
673 "Cookie.TimeParseDomains",
674 base::Time::Now() - start_parse
,
675 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
678 UMA_HISTOGRAM_CUSTOM_TIMES(
679 "Cookie.TimeInitializeDomainMap",
680 base::Time::Now() - start
,
681 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
688 void SQLitePersistentCookieStore::Backend::ChainLoadCookies(
689 const LoadedCallback
& loaded_callback
) {
690 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
691 IncrementTimeDelta
increment(&cookie_load_duration_
);
693 bool load_success
= true;
696 // Close() has been called on this store.
697 load_success
= false;
698 } else if (keys_to_load_
.size() > 0) {
699 // Load cookies for the first domain key.
700 std::map
<std::string
, std::set
<std::string
> >::iterator
701 it
= keys_to_load_
.begin();
702 load_success
= LoadCookiesForDomains(it
->second
);
703 keys_to_load_
.erase(it
);
706 // If load is successful and there are more domain keys to be loaded,
707 // then post a background task to continue chain-load;
708 // Otherwise notify on client runner.
709 if (load_success
&& keys_to_load_
.size() > 0) {
710 bool success
= background_task_runner_
->PostDelayedTask(
712 base::Bind(&Backend::ChainLoadCookies
, this, loaded_callback
),
713 base::TimeDelta::FromMilliseconds(kLoadDelayMilliseconds
));
715 LOG(WARNING
) << "Failed to post task from " << FROM_HERE
.ToString()
716 << " to background_task_runner_.";
719 FinishedLoadingCookies(loaded_callback
, load_success
);
723 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains(
724 const std::set
<std::string
>& domains
) {
725 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
728 if (restore_old_session_cookies_
) {
729 smt
.Assign(db_
->GetCachedStatement(
731 "SELECT creation_utc, host_key, name, value, encrypted_value, path, "
732 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, "
733 "has_expires, persistent, priority FROM cookies WHERE host_key = ?"));
735 smt
.Assign(db_
->GetCachedStatement(
737 "SELECT creation_utc, host_key, name, value, encrypted_value, path, "
738 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, "
739 "has_expires, persistent, priority FROM cookies WHERE host_key = ? "
740 "AND persistent = 1"));
742 if (!smt
.is_valid()) {
743 smt
.Clear(); // Disconnect smt_ref from db_.
749 std::vector
<net::CanonicalCookie
*> cookies
;
750 std::set
<std::string
>::const_iterator it
= domains
.begin();
751 for (; it
!= domains
.end(); ++it
) {
752 smt
.BindString(0, *it
);
753 MakeCookiesFromSQLStatement(&cookies
, &smt
);
757 base::AutoLock
locked(lock_
);
758 cookies_
.insert(cookies_
.end(), cookies
.begin(), cookies
.end());
763 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement(
764 std::vector
<net::CanonicalCookie
*>* cookies
,
765 sql::Statement
* statement
) {
766 sql::Statement
& smt
= *statement
;
769 std::string encrypted_value
= smt
.ColumnString(4);
770 if (!encrypted_value
.empty() && crypto_
) {
771 crypto_
->DecryptString(encrypted_value
, &value
);
773 DCHECK(encrypted_value
.empty());
774 value
= smt
.ColumnString(3);
776 scoped_ptr
<net::CanonicalCookie
> cc(new net::CanonicalCookie(
777 // The "source" URL is not used with persisted cookies.
779 smt
.ColumnString(2), // name
781 smt
.ColumnString(1), // domain
782 smt
.ColumnString(5), // path
783 Time::FromInternalValue(smt
.ColumnInt64(0)), // creation_utc
784 Time::FromInternalValue(smt
.ColumnInt64(6)), // expires_utc
785 Time::FromInternalValue(smt
.ColumnInt64(10)), // last_access_utc
786 smt
.ColumnInt(7) != 0, // secure
787 smt
.ColumnInt(8) != 0, // httponly
788 smt
.ColumnInt(9) != 0, // firstpartyonly
789 DBCookiePriorityToCookiePriority(
790 static_cast<DBCookiePriority
>(smt
.ColumnInt(13))))); // priority
791 DLOG_IF(WARNING
, cc
->CreationDate() > Time::Now())
792 << L
"CreationDate too recent";
793 cookies_per_origin_
[CookieOrigin(cc
->Domain(), cc
->IsSecure())]++;
794 cookies
->push_back(cc
.release());
799 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() {
801 if (!meta_table_
.Init(
802 db_
.get(), kCurrentVersionNumber
, kCompatibleVersionNumber
)) {
806 if (meta_table_
.GetCompatibleVersionNumber() > kCurrentVersionNumber
) {
807 LOG(WARNING
) << "Cookie database is too new.";
811 int cur_version
= meta_table_
.GetVersionNumber();
812 if (cur_version
== 2) {
813 sql::Transaction
transaction(db_
.get());
814 if (!transaction
.Begin())
816 if (!db_
->Execute("ALTER TABLE cookies ADD COLUMN last_access_utc "
817 "INTEGER DEFAULT 0") ||
818 !db_
->Execute("UPDATE cookies SET last_access_utc = creation_utc")) {
819 LOG(WARNING
) << "Unable to update cookie database to version 3.";
823 meta_table_
.SetVersionNumber(cur_version
);
824 meta_table_
.SetCompatibleVersionNumber(
825 std::min(cur_version
, kCompatibleVersionNumber
));
826 transaction
.Commit();
829 if (cur_version
== 3) {
830 // The time epoch changed for Mac & Linux in this version to match Windows.
831 // This patch came after the main epoch change happened, so some
832 // developers have "good" times for cookies added by the more recent
833 // versions. So we have to be careful to only update times that are under
834 // the old system (which will appear to be from before 1970 in the new
835 // system). The magic number used below is 1970 in our time units.
836 sql::Transaction
transaction(db_
.get());
839 ignore_result(db_
->Execute(
841 "SET creation_utc = creation_utc + 11644473600000000 "
843 "(SELECT rowid FROM cookies WHERE "
844 "creation_utc > 0 AND creation_utc < 11644473600000000)"));
845 ignore_result(db_
->Execute(
847 "SET expires_utc = expires_utc + 11644473600000000 "
849 "(SELECT rowid FROM cookies WHERE "
850 "expires_utc > 0 AND expires_utc < 11644473600000000)"));
851 ignore_result(db_
->Execute(
853 "SET last_access_utc = last_access_utc + 11644473600000000 "
855 "(SELECT rowid FROM cookies WHERE "
856 "last_access_utc > 0 AND last_access_utc < 11644473600000000)"));
859 meta_table_
.SetVersionNumber(cur_version
);
860 transaction
.Commit();
863 if (cur_version
== 4) {
864 const base::TimeTicks start_time
= base::TimeTicks::Now();
865 sql::Transaction
transaction(db_
.get());
866 if (!transaction
.Begin())
868 if (!db_
->Execute("ALTER TABLE cookies "
869 "ADD COLUMN has_expires INTEGER DEFAULT 1") ||
870 !db_
->Execute("ALTER TABLE cookies "
871 "ADD COLUMN persistent INTEGER DEFAULT 1")) {
872 LOG(WARNING
) << "Unable to update cookie database to version 5.";
876 meta_table_
.SetVersionNumber(cur_version
);
877 meta_table_
.SetCompatibleVersionNumber(
878 std::min(cur_version
, kCompatibleVersionNumber
));
879 transaction
.Commit();
880 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV5",
881 base::TimeTicks::Now() - start_time
);
884 if (cur_version
== 5) {
885 const base::TimeTicks start_time
= base::TimeTicks::Now();
886 sql::Transaction
transaction(db_
.get());
887 if (!transaction
.Begin())
889 // Alter the table to add the priority column with a default value.
890 std::string
stmt(base::StringPrintf(
891 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d",
892 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT
)));
893 if (!db_
->Execute(stmt
.c_str())) {
894 LOG(WARNING
) << "Unable to update cookie database to version 6.";
898 meta_table_
.SetVersionNumber(cur_version
);
899 meta_table_
.SetCompatibleVersionNumber(
900 std::min(cur_version
, kCompatibleVersionNumber
));
901 transaction
.Commit();
902 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6",
903 base::TimeTicks::Now() - start_time
);
906 if (cur_version
== 6) {
907 const base::TimeTicks start_time
= base::TimeTicks::Now();
908 sql::Transaction
transaction(db_
.get());
909 if (!transaction
.Begin())
911 // Alter the table to add empty "encrypted value" column.
912 if (!db_
->Execute("ALTER TABLE cookies "
913 "ADD COLUMN encrypted_value BLOB DEFAULT ''")) {
914 LOG(WARNING
) << "Unable to update cookie database to version 7.";
918 meta_table_
.SetVersionNumber(cur_version
);
919 meta_table_
.SetCompatibleVersionNumber(
920 std::min(cur_version
, kCompatibleVersionNumber
));
921 transaction
.Commit();
922 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV7",
923 base::TimeTicks::Now() - start_time
);
926 if (cur_version
== 7) {
927 const base::TimeTicks start_time
= base::TimeTicks::Now();
928 sql::Transaction
transaction(db_
.get());
929 if (!transaction
.Begin())
931 // Alter the table to add a 'firstpartyonly' column.
933 "ALTER TABLE cookies "
934 "ADD COLUMN firstpartyonly INTEGER DEFAULT 0")) {
935 LOG(WARNING
) << "Unable to update cookie database to version 8.";
939 meta_table_
.SetVersionNumber(cur_version
);
940 meta_table_
.SetCompatibleVersionNumber(
941 std::min(cur_version
, kCompatibleVersionNumber
));
942 transaction
.Commit();
943 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8",
944 base::TimeTicks::Now() - start_time
);
947 // Put future migration cases here.
949 if (cur_version
< kCurrentVersionNumber
) {
950 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1);
953 db_
.reset(new sql::Connection
);
954 if (!base::DeleteFile(path_
, false) ||
957 db_
.get(), kCurrentVersionNumber
, kCompatibleVersionNumber
)) {
958 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTableRecoveryFailed", 1);
959 NOTREACHED() << "Unable to reset the cookie DB.";
969 void SQLitePersistentCookieStore::Backend::AddCookie(
970 const net::CanonicalCookie
& cc
) {
971 BatchOperation(PendingOperation::COOKIE_ADD
, cc
);
974 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime(
975 const net::CanonicalCookie
& cc
) {
976 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS
, cc
);
979 void SQLitePersistentCookieStore::Backend::DeleteCookie(
980 const net::CanonicalCookie
& cc
) {
981 BatchOperation(PendingOperation::COOKIE_DELETE
, cc
);
984 void SQLitePersistentCookieStore::Backend::BatchOperation(
985 PendingOperation::OperationType op
,
986 const net::CanonicalCookie
& cc
) {
987 // Commit every 30 seconds.
988 static const int kCommitIntervalMs
= 30 * 1000;
989 // Commit right away if we have more than 512 outstanding operations.
990 static const size_t kCommitAfterBatchSize
= 512;
991 DCHECK(!background_task_runner_
->RunsTasksOnCurrentThread());
993 // We do a full copy of the cookie here, and hopefully just here.
994 scoped_ptr
<PendingOperation
> po(new PendingOperation(op
, cc
));
996 PendingOperationsList::size_type num_pending
;
998 base::AutoLock
locked(lock_
);
999 pending_
.push_back(po
.release());
1000 num_pending
= ++num_pending_
;
1003 if (num_pending
== 1) {
1004 // We've gotten our first entry for this batch, fire off the timer.
1005 if (!background_task_runner_
->PostDelayedTask(
1006 FROM_HERE
, base::Bind(&Backend::Commit
, this),
1007 base::TimeDelta::FromMilliseconds(kCommitIntervalMs
))) {
1008 NOTREACHED() << "background_task_runner_ is not running.";
1010 } else if (num_pending
== kCommitAfterBatchSize
) {
1011 // We've reached a big enough batch, fire off a commit now.
1012 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::Commit
, this));
1016 void SQLitePersistentCookieStore::Backend::Commit() {
1017 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1019 PendingOperationsList ops
;
1021 base::AutoLock
locked(lock_
);
1026 // Maybe an old timer fired or we are already Close()'ed.
1027 if (!db_
.get() || ops
.empty())
1030 sql::Statement
add_smt(db_
->GetCachedStatement(
1032 "INSERT INTO cookies (creation_utc, host_key, name, value, "
1033 "encrypted_value, path, expires_utc, secure, httponly, firstpartyonly, "
1034 "last_access_utc, has_expires, persistent, priority) "
1035 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));
1036 if (!add_smt
.is_valid())
1039 sql::Statement
update_access_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
1040 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?"));
1041 if (!update_access_smt
.is_valid())
1044 sql::Statement
del_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
1045 "DELETE FROM cookies WHERE creation_utc=?"));
1046 if (!del_smt
.is_valid())
1049 sql::Transaction
transaction(db_
.get());
1050 if (!transaction
.Begin())
1053 for (PendingOperationsList::iterator it
= ops
.begin();
1054 it
!= ops
.end(); ++it
) {
1055 // Free the cookies as we commit them to the database.
1056 scoped_ptr
<PendingOperation
> po(*it
);
1058 case PendingOperation::COOKIE_ADD
:
1059 cookies_per_origin_
[
1060 CookieOrigin(po
->cc().Domain(), po
->cc().IsSecure())]++;
1061 add_smt
.Reset(true);
1062 add_smt
.BindInt64(0, po
->cc().CreationDate().ToInternalValue());
1063 add_smt
.BindString(1, po
->cc().Domain());
1064 add_smt
.BindString(2, po
->cc().Name());
1066 std::string encrypted_value
;
1067 add_smt
.BindCString(3, ""); // value
1068 crypto_
->EncryptString(po
->cc().Value(), &encrypted_value
);
1069 // BindBlob() immediately makes an internal copy of the data.
1070 add_smt
.BindBlob(4, encrypted_value
.data(),
1071 static_cast<int>(encrypted_value
.length()));
1073 add_smt
.BindString(3, po
->cc().Value());
1074 add_smt
.BindBlob(4, "", 0); // encrypted_value
1076 add_smt
.BindString(5, po
->cc().Path());
1077 add_smt
.BindInt64(6, po
->cc().ExpiryDate().ToInternalValue());
1078 add_smt
.BindInt(7, po
->cc().IsSecure());
1079 add_smt
.BindInt(8, po
->cc().IsHttpOnly());
1080 add_smt
.BindInt(9, po
->cc().IsFirstPartyOnly());
1081 add_smt
.BindInt64(10, po
->cc().LastAccessDate().ToInternalValue());
1082 add_smt
.BindInt(11, po
->cc().IsPersistent());
1083 add_smt
.BindInt(12, po
->cc().IsPersistent());
1085 CookiePriorityToDBCookiePriority(po
->cc().Priority()));
1087 NOTREACHED() << "Could not add a cookie to the DB.";
1090 case PendingOperation::COOKIE_UPDATEACCESS
:
1091 update_access_smt
.Reset(true);
1092 update_access_smt
.BindInt64(0,
1093 po
->cc().LastAccessDate().ToInternalValue());
1094 update_access_smt
.BindInt64(1,
1095 po
->cc().CreationDate().ToInternalValue());
1096 if (!update_access_smt
.Run())
1097 NOTREACHED() << "Could not update cookie last access time in the DB.";
1100 case PendingOperation::COOKIE_DELETE
:
1101 cookies_per_origin_
[
1102 CookieOrigin(po
->cc().Domain(), po
->cc().IsSecure())]--;
1103 del_smt
.Reset(true);
1104 del_smt
.BindInt64(0, po
->cc().CreationDate().ToInternalValue());
1106 NOTREACHED() << "Could not delete a cookie from the DB.";
1114 bool succeeded
= transaction
.Commit();
1115 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults",
1116 succeeded
? 0 : 1, 2);
1119 void SQLitePersistentCookieStore::Backend::Flush(
1120 const base::Closure
& callback
) {
1121 DCHECK(!background_task_runner_
->RunsTasksOnCurrentThread());
1122 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::Commit
, this));
1124 if (!callback
.is_null()) {
1125 // We want the completion task to run immediately after Commit() returns.
1126 // Posting it from here means there is less chance of another task getting
1127 // onto the message queue first, than if we posted it from Commit() itself.
1128 PostBackgroundTask(FROM_HERE
, callback
);
1132 // Fire off a close message to the background runner. We could still have a
1133 // pending commit timer or Load operations holding references on us, but if/when
1134 // this fires we will already have been cleaned up and it will be ignored.
1135 void SQLitePersistentCookieStore::Backend::Close() {
1136 if (background_task_runner_
->RunsTasksOnCurrentThread()) {
1137 InternalBackgroundClose();
1139 // Must close the backend on the background runner.
1140 PostBackgroundTask(FROM_HERE
,
1141 base::Bind(&Backend::InternalBackgroundClose
, this));
1145 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() {
1146 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1147 // Commit any pending operations
1150 if (!force_keep_session_state_
&& special_storage_policy_
.get() &&
1151 special_storage_policy_
->HasSessionOnlyOrigins()) {
1152 DeleteSessionCookiesOnShutdown();
1155 meta_table_
.Reset();
1159 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() {
1160 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1165 if (!special_storage_policy_
.get())
1168 sql::Statement
del_smt(db_
->GetCachedStatement(
1169 SQL_FROM_HERE
, "DELETE FROM cookies WHERE host_key=? AND secure=?"));
1170 if (!del_smt
.is_valid()) {
1171 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1175 sql::Transaction
transaction(db_
.get());
1176 if (!transaction
.Begin()) {
1177 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1181 for (CookiesPerOriginMap::iterator it
= cookies_per_origin_
.begin();
1182 it
!= cookies_per_origin_
.end(); ++it
) {
1183 if (it
->second
<= 0) {
1184 DCHECK_EQ(0, it
->second
);
1187 const GURL
url(net::cookie_util::CookieOriginToURL(it
->first
.first
,
1189 if (!url
.is_valid() || !special_storage_policy_
->IsStorageSessionOnly(url
))
1192 del_smt
.Reset(true);
1193 del_smt
.BindString(0, it
->first
.first
);
1194 del_smt
.BindInt(1, it
->first
.second
);
1196 NOTREACHED() << "Could not delete a cookie from the DB.";
1199 if (!transaction
.Commit())
1200 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1203 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback(
1205 sql::Statement
* stmt
) {
1206 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1208 if (!sql::IsErrorCatastrophic(error
))
1211 // TODO(shess): Running KillDatabase() multiple times should be
1213 if (corruption_detected_
)
1216 corruption_detected_
= true;
1218 // Don't just do the close/delete here, as we are being called by |db| and
1219 // that seems dangerous.
1220 // TODO(shess): Consider just calling RazeAndClose() immediately.
1221 // db_ may not be safe to reset at this point, but RazeAndClose()
1222 // would cause the stack to unwind safely with errors.
1223 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::KillDatabase
, this));
1226 void SQLitePersistentCookieStore::Backend::KillDatabase() {
1227 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1230 // This Backend will now be in-memory only. In a future run we will recreate
1231 // the database. Hopefully things go better then!
1232 bool success
= db_
->RazeAndClose();
1233 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success
);
1234 meta_table_
.Reset();
1239 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() {
1240 base::AutoLock
locked(lock_
);
1241 force_keep_session_state_
= true;
1244 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() {
1245 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1246 if (!db_
->Execute("DELETE FROM cookies WHERE persistent == 0"))
1247 LOG(WARNING
) << "Unable to delete session cookies.";
1250 void SQLitePersistentCookieStore::Backend::PostBackgroundTask(
1251 const tracked_objects::Location
& origin
, const base::Closure
& task
) {
1252 if (!background_task_runner_
->PostTask(origin
, task
)) {
1253 LOG(WARNING
) << "Failed to post task from " << origin
.ToString()
1254 << " to background_task_runner_.";
1258 void SQLitePersistentCookieStore::Backend::PostClientTask(
1259 const tracked_objects::Location
& origin
, const base::Closure
& task
) {
1260 if (!client_task_runner_
->PostTask(origin
, task
)) {
1261 LOG(WARNING
) << "Failed to post task from " << origin
.ToString()
1262 << " to client_task_runner_.";
1266 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies(
1267 const LoadedCallback
& loaded_callback
,
1269 PostClientTask(FROM_HERE
, base::Bind(&Backend::CompleteLoadInForeground
, this,
1270 loaded_callback
, success
));
1271 if (success
&& !restore_old_session_cookies_
)
1272 DeleteSessionCookiesOnStartup();
1275 SQLitePersistentCookieStore::SQLitePersistentCookieStore(
1276 const base::FilePath
& path
,
1277 const scoped_refptr
<base::SequencedTaskRunner
>& client_task_runner
,
1278 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
1279 bool restore_old_session_cookies
,
1280 storage::SpecialStoragePolicy
* special_storage_policy
,
1281 net::CookieCryptoDelegate
* crypto_delegate
)
1282 : backend_(new Backend(path
,
1284 background_task_runner
,
1285 restore_old_session_cookies
,
1286 special_storage_policy
,
1290 void SQLitePersistentCookieStore::Load(const LoadedCallback
& loaded_callback
) {
1291 backend_
->Load(loaded_callback
);
1294 void SQLitePersistentCookieStore::LoadCookiesForKey(
1295 const std::string
& key
,
1296 const LoadedCallback
& loaded_callback
) {
1297 backend_
->LoadCookiesForKey(key
, loaded_callback
);
1300 void SQLitePersistentCookieStore::AddCookie(const net::CanonicalCookie
& cc
) {
1301 backend_
->AddCookie(cc
);
1304 void SQLitePersistentCookieStore::UpdateCookieAccessTime(
1305 const net::CanonicalCookie
& cc
) {
1306 backend_
->UpdateCookieAccessTime(cc
);
1309 void SQLitePersistentCookieStore::DeleteCookie(const net::CanonicalCookie
& cc
) {
1310 backend_
->DeleteCookie(cc
);
1313 void SQLitePersistentCookieStore::SetForceKeepSessionState() {
1314 backend_
->SetForceKeepSessionState();
1317 void SQLitePersistentCookieStore::Flush(const base::Closure
& callback
) {
1318 backend_
->Flush(callback
);
1321 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() {
1323 // We release our reference to the Backend, though it will probably still have
1324 // a reference if the background runner has not run Close() yet.
1327 CookieStoreConfig::CookieStoreConfig()
1328 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES
),
1329 crypto_delegate(NULL
) {
1330 // Default to an in-memory cookie store.
1333 CookieStoreConfig::CookieStoreConfig(
1334 const base::FilePath
& path
,
1335 SessionCookieMode session_cookie_mode
,
1336 storage::SpecialStoragePolicy
* storage_policy
,
1337 net::CookieMonsterDelegate
* cookie_delegate
)
1339 session_cookie_mode(session_cookie_mode
),
1340 storage_policy(storage_policy
),
1341 cookie_delegate(cookie_delegate
),
1342 crypto_delegate(NULL
) {
1343 CHECK(!path
.empty() || session_cookie_mode
== EPHEMERAL_SESSION_COOKIES
);
1346 CookieStoreConfig::~CookieStoreConfig() {
1349 net::CookieStore
* CreateCookieStore(const CookieStoreConfig
& config
) {
1350 net::CookieMonster
* cookie_monster
= NULL
;
1352 if (config
.path
.empty()) {
1353 // Empty path means in-memory store.
1354 cookie_monster
= new net::CookieMonster(NULL
, config
.cookie_delegate
.get());
1356 scoped_refptr
<base::SequencedTaskRunner
> client_task_runner
=
1357 config
.client_task_runner
;
1358 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
=
1359 config
.background_task_runner
;
1361 if (!client_task_runner
.get()) {
1362 client_task_runner
=
1363 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
);
1366 if (!background_task_runner
.get()) {
1367 background_task_runner
=
1368 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
1369 BrowserThread::GetBlockingPool()->GetSequenceToken());
1372 SQLitePersistentCookieStore
* persistent_store
=
1373 new SQLitePersistentCookieStore(
1376 background_task_runner
,
1377 (config
.session_cookie_mode
==
1378 CookieStoreConfig::RESTORED_SESSION_COOKIES
),
1379 config
.storage_policy
.get(),
1380 config
.crypto_delegate
);
1383 new net::CookieMonster(persistent_store
, config
.cookie_delegate
.get());
1384 if ((config
.session_cookie_mode
==
1385 CookieStoreConfig::PERSISTANT_SESSION_COOKIES
) ||
1386 (config
.session_cookie_mode
==
1387 CookieStoreConfig::RESTORED_SESSION_COOKIES
)) {
1388 cookie_monster
->SetPersistSessionCookies(true);
1392 return cookie_monster
;
1395 } // namespace content