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/command_line.h"
16 #include "base/files/file_path.h"
17 #include "base/files/file_util.h"
18 #include "base/location.h"
19 #include "base/logging.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "base/metrics/field_trial.h"
23 #include "base/metrics/histogram.h"
24 #include "base/sequenced_task_runner.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/synchronization/lock.h"
28 #include "base/threading/sequenced_worker_pool.h"
29 #include "base/time/time.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/cookie_crypto_delegate.h"
32 #include "content/public/browser/cookie_store_factory.h"
33 #include "content/public/common/content_switches.h"
34 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
35 #include "net/cookies/canonical_cookie.h"
36 #include "net/cookies/cookie_constants.h"
37 #include "net/cookies/cookie_util.h"
38 #include "sql/error_delegate_util.h"
39 #include "sql/meta_table.h"
40 #include "sql/statement.h"
41 #include "sql/transaction.h"
42 #include "storage/browser/quota/special_storage_policy.h"
43 #include "third_party/sqlite/sqlite3.h"
50 // The persistent cookie store is loaded into memory on eTLD at a time. This
51 // variable controls the delay between loading eTLDs, so as to not overload the
52 // CPU or I/O with these low priority requests immediately after start up.
53 const int kLoadDelayMilliseconds
= 200;
59 // This class is designed to be shared between any client thread and the
60 // background task runner. It batches operations and commits them on a timer.
62 // SQLitePersistentCookieStore::Load is called to load all cookies. It
63 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread
64 // task to the background runner. This task calls Backend::ChainLoadCookies(),
65 // which repeatedly posts itself to the BG runner to load each eTLD+1's cookies
66 // in separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is
67 // posted to the client runner, which notifies the caller of
68 // SQLitePersistentCookieStore::Load that the load is complete.
70 // If a priority load request is invoked via SQLitePersistentCookieStore::
71 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts
72 // Backend::LoadKeyAndNotifyOnDBThread to the BG runner. That routine loads just
73 // that single domain key (eTLD+1)'s cookies, and posts a Backend::
74 // CompleteLoadForKeyOnIOThread to the client runner to notify the caller of
75 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete.
77 // Subsequent to loading, mutations may be queued by any thread using
78 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to
79 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(),
80 // whichever occurs first.
81 class SQLitePersistentCookieStore::Backend
82 : public base::RefCountedThreadSafe
<SQLitePersistentCookieStore::Backend
> {
85 const base::FilePath
& path
,
86 const scoped_refptr
<base::SequencedTaskRunner
>& client_task_runner
,
87 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
88 bool restore_old_session_cookies
,
89 storage::SpecialStoragePolicy
* special_storage_policy
,
90 CookieCryptoDelegate
* crypto_delegate
)
93 force_keep_session_state_(false),
95 corruption_detected_(false),
96 restore_old_session_cookies_(restore_old_session_cookies
),
97 special_storage_policy_(special_storage_policy
),
99 client_task_runner_(client_task_runner
),
100 background_task_runner_(background_task_runner
),
101 num_priority_waiting_(0),
102 total_priority_requests_(0),
103 crypto_(crypto_delegate
) {}
105 // Creates or loads the SQLite database.
106 void Load(const LoadedCallback
& loaded_callback
);
108 // Loads cookies for the domain key (eTLD+1).
109 void LoadCookiesForKey(const std::string
& domain
,
110 const LoadedCallback
& loaded_callback
);
112 // Steps through all results of |smt|, makes a cookie from each, and adds the
113 // cookie to |cookies|. This method also updates |cookies_per_origin_| and
114 // |num_cookies_read_|.
115 void MakeCookiesFromSQLStatement(std::vector
<net::CanonicalCookie
*>* cookies
,
116 sql::Statement
* statement
);
118 // Batch a cookie addition.
119 void AddCookie(const net::CanonicalCookie
& cc
);
121 // Batch a cookie access time update.
122 void UpdateCookieAccessTime(const net::CanonicalCookie
& cc
);
124 // Batch a cookie deletion.
125 void DeleteCookie(const net::CanonicalCookie
& cc
);
127 // Commit pending operations as soon as possible.
128 void Flush(const base::Closure
& callback
);
130 // Commit any pending operations and close the database. This must be called
131 // before the object is destructed.
134 void SetForceKeepSessionState();
137 friend class base::RefCountedThreadSafe
<SQLitePersistentCookieStore::Backend
>;
139 // You should call Close() before destructing this object.
141 DCHECK(!db_
.get()) << "Close should have already been called.";
142 DCHECK(num_pending_
== 0 && pending_
.empty());
145 // Database upgrade statements.
146 bool EnsureDatabaseVersion();
148 class PendingOperation
{
156 PendingOperation(OperationType op
, const net::CanonicalCookie
& cc
)
157 : op_(op
), cc_(cc
) { }
159 OperationType
op() const { return op_
; }
160 const net::CanonicalCookie
& cc() const { return cc_
; }
164 net::CanonicalCookie cc_
;
168 // Creates or loads the SQLite database on background runner.
169 void LoadAndNotifyInBackground(const LoadedCallback
& loaded_callback
,
170 const base::Time
& posted_at
);
172 // Loads cookies for the domain key (eTLD+1) on background runner.
173 void LoadKeyAndNotifyInBackground(const std::string
& domains
,
174 const LoadedCallback
& loaded_callback
,
175 const base::Time
& posted_at
);
177 // Notifies the CookieMonster when loading completes for a specific domain key
178 // or for all domain keys. Triggers the callback and passes it all cookies
179 // that have been loaded from DB since last IO notification.
180 void Notify(const LoadedCallback
& loaded_callback
, bool load_success
);
182 // Sends notification when the entire store is loaded, and reports metrics
183 // for the total time to load and aggregated results from any priority loads
185 void CompleteLoadInForeground(const LoadedCallback
& loaded_callback
,
188 // Sends notification when a single priority load completes. Updates priority
189 // load metric data. The data is sent only after the final load completes.
190 void CompleteLoadForKeyInForeground(const LoadedCallback
& loaded_callback
,
192 const base::Time
& requested_at
);
194 // Sends all metrics, including posting a ReportMetricsInBackground task.
195 // Called after all priority and regular loading is complete.
196 void ReportMetrics();
198 // Sends background-runner owned metrics (i.e., the combined duration of all
200 void ReportMetricsInBackground();
202 // Initialize the data base.
203 bool InitializeDatabase();
205 // Loads cookies for the next domain key from the DB, then either reschedules
206 // itself or schedules the provided callback to run on the client runner (if
207 // all domains are loaded).
208 void ChainLoadCookies(const LoadedCallback
& loaded_callback
);
210 // Load all cookies for a set of domains/hosts
211 bool LoadCookiesForDomains(const std::set
<std::string
>& key
);
213 // Batch a cookie operation (add or delete)
214 void BatchOperation(PendingOperation::OperationType op
,
215 const net::CanonicalCookie
& cc
);
216 // Commit our pending operations to the database.
218 // Close() executed on the background runner.
219 void InternalBackgroundClose();
221 void DeleteSessionCookiesOnStartup();
223 void DeleteSessionCookiesOnShutdown();
225 void DatabaseErrorCallback(int error
, sql::Statement
* stmt
);
228 void PostBackgroundTask(const tracked_objects::Location
& origin
,
229 const base::Closure
& task
);
230 void PostClientTask(const tracked_objects::Location
& origin
,
231 const base::Closure
& task
);
233 // Shared code between the different load strategies to be used after all
234 // cookies have been loaded.
235 void FinishedLoadingCookies(const LoadedCallback
& loaded_callback
,
238 base::FilePath path_
;
239 scoped_ptr
<sql::Connection
> db_
;
240 sql::MetaTable meta_table_
;
242 typedef std::list
<PendingOperation
*> PendingOperationsList
;
243 PendingOperationsList pending_
;
244 PendingOperationsList::size_type num_pending_
;
245 // True if the persistent store should skip delete on exit rules.
246 bool force_keep_session_state_
;
247 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_|
250 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce
251 // the number of messages sent to the client runner. Sent back in response to
252 // individual load requests for domain keys or when all loading completes.
253 std::vector
<net::CanonicalCookie
*> cookies_
;
255 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB.
256 std::map
<std::string
, std::set
<std::string
> > keys_to_load_
;
258 // Map of (domain keys(eTLD+1), is secure cookie) to number of cookies in the
260 typedef std::pair
<std::string
, bool> CookieOrigin
;
261 typedef std::map
<CookieOrigin
, int> CookiesPerOriginMap
;
262 CookiesPerOriginMap cookies_per_origin_
;
264 // Indicates if DB has been initialized.
267 // Indicates if the kill-database callback has been scheduled.
268 bool corruption_detected_
;
270 // If false, we should filter out session cookies when reading the DB.
271 bool restore_old_session_cookies_
;
273 // Policy defining what data is deleted on shutdown.
274 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy_
;
276 // The cumulative time spent loading the cookies on the background runner.
277 // Incremented and reported from the background runner.
278 base::TimeDelta cookie_load_duration_
;
280 // The total number of cookies read. Incremented and reported on the
281 // background runner.
282 int num_cookies_read_
;
284 scoped_refptr
<base::SequencedTaskRunner
> client_task_runner_
;
285 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner_
;
287 // Guards the following metrics-related properties (only accessed when
288 // starting/completing priority loads or completing the total load).
289 base::Lock metrics_lock_
;
290 int num_priority_waiting_
;
291 // The total number of priority requests.
292 int total_priority_requests_
;
293 // The time when |num_priority_waiting_| incremented to 1.
294 base::Time current_priority_wait_start_
;
295 // The cumulative duration of time when |num_priority_waiting_| was greater
297 base::TimeDelta priority_wait_duration_
;
298 // Class with functions that do cryptographic operations (for protecting
299 // cookies stored persistently).
302 CookieCryptoDelegate
* crypto_
;
304 DISALLOW_COPY_AND_ASSIGN(Backend
);
309 // Version number of the database.
311 // Version 8 adds "first-party only" cookies.
313 // Version 7 adds encrypted values. Old values will continue to be used but
314 // all new values written will be encrypted on selected operating systems. New
315 // records read by old clients will simply get an empty cookie value while old
316 // records read by new clients will continue to operate with the unencrypted
317 // version. New and old clients alike will always write/update records with
318 // what they support.
320 // Version 6 adds cookie priorities. This allows developers to influence the
321 // order in which cookies are evicted in order to meet domain cookie limits.
323 // Version 5 adds the columns has_expires and is_persistent, so that the
324 // database can store session cookies as well as persistent cookies. Databases
325 // of version 5 are incompatible with older versions of code. If a database of
326 // version 5 is read by older code, session cookies will be treated as normal
327 // cookies. Currently, these fields are written, but not read anymore.
329 // In version 4, we migrated the time epoch. If you open the DB with an older
330 // version on Mac or Linux, the times will look wonky, but the file will likely
331 // be usable. On Windows version 3 and 4 are the same.
333 // Version 3 updated the database to include the last access time, so we can
334 // expire them in decreasing order of use when we've reached the maximum
335 // number of cookies.
336 const int kCurrentVersionNumber
= 8;
337 const int kCompatibleVersionNumber
= 5;
339 // Possible values for the 'priority' column.
340 enum DBCookiePriority
{
341 kCookiePriorityLow
= 0,
342 kCookiePriorityMedium
= 1,
343 kCookiePriorityHigh
= 2,
346 DBCookiePriority
CookiePriorityToDBCookiePriority(net::CookiePriority value
) {
348 case net::COOKIE_PRIORITY_LOW
:
349 return kCookiePriorityLow
;
350 case net::COOKIE_PRIORITY_MEDIUM
:
351 return kCookiePriorityMedium
;
352 case net::COOKIE_PRIORITY_HIGH
:
353 return kCookiePriorityHigh
;
357 return kCookiePriorityMedium
;
360 net::CookiePriority
DBCookiePriorityToCookiePriority(DBCookiePriority value
) {
362 case kCookiePriorityLow
:
363 return net::COOKIE_PRIORITY_LOW
;
364 case kCookiePriorityMedium
:
365 return net::COOKIE_PRIORITY_MEDIUM
;
366 case kCookiePriorityHigh
:
367 return net::COOKIE_PRIORITY_HIGH
;
371 return net::COOKIE_PRIORITY_DEFAULT
;
374 // Increments a specified TimeDelta by the duration between this object's
375 // constructor and destructor. Not thread safe. Multiple instances may be
376 // created with the same delta instance as long as their lifetimes are nested.
377 // The shortest lived instances have no impact.
378 class IncrementTimeDelta
{
380 explicit IncrementTimeDelta(base::TimeDelta
* delta
) :
382 original_value_(*delta
),
383 start_(base::Time::Now()) {}
385 ~IncrementTimeDelta() {
386 *delta_
= original_value_
+ base::Time::Now() - start_
;
390 base::TimeDelta
* delta_
;
391 base::TimeDelta original_value_
;
394 DISALLOW_COPY_AND_ASSIGN(IncrementTimeDelta
);
397 // Initializes the cookies table, returning true on success.
398 bool InitTable(sql::Connection
* db
) {
399 if (!db
->DoesTableExist("cookies")) {
400 std::string
stmt(base::StringPrintf(
401 "CREATE TABLE cookies ("
402 "creation_utc INTEGER NOT NULL UNIQUE PRIMARY KEY,"
403 "host_key TEXT NOT NULL,"
404 "name TEXT NOT NULL,"
405 "value TEXT NOT NULL,"
406 "path TEXT NOT NULL,"
407 "expires_utc INTEGER NOT NULL,"
408 "secure INTEGER NOT NULL,"
409 "httponly INTEGER NOT NULL,"
410 "last_access_utc INTEGER NOT NULL, "
411 "has_expires INTEGER NOT NULL DEFAULT 1, "
412 "persistent INTEGER NOT NULL DEFAULT 1,"
413 "priority INTEGER NOT NULL DEFAULT %d,"
414 "encrypted_value BLOB DEFAULT '',"
415 "firstpartyonly INTEGER NOT NULL DEFAULT 0)",
416 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT
)));
417 if (!db
->Execute(stmt
.c_str()))
421 // Older code created an index on creation_utc, which is already
422 // primary key for the table.
423 if (!db
->Execute("DROP INDEX IF EXISTS cookie_times"))
426 if (!db
->Execute("CREATE INDEX IF NOT EXISTS domain ON cookies(host_key)"))
434 void SQLitePersistentCookieStore::Backend::Load(
435 const LoadedCallback
& loaded_callback
) {
436 // This function should be called only once per instance.
438 PostBackgroundTask(FROM_HERE
, base::Bind(
439 &Backend::LoadAndNotifyInBackground
, this,
440 loaded_callback
, base::Time::Now()));
443 void SQLitePersistentCookieStore::Backend::LoadCookiesForKey(
444 const std::string
& key
,
445 const LoadedCallback
& loaded_callback
) {
447 base::AutoLock
locked(metrics_lock_
);
448 if (num_priority_waiting_
== 0)
449 current_priority_wait_start_
= base::Time::Now();
450 num_priority_waiting_
++;
451 total_priority_requests_
++;
454 PostBackgroundTask(FROM_HERE
, base::Bind(
455 &Backend::LoadKeyAndNotifyInBackground
,
456 this, key
, loaded_callback
, base::Time::Now()));
459 void SQLitePersistentCookieStore::Backend::LoadAndNotifyInBackground(
460 const LoadedCallback
& loaded_callback
, const base::Time
& posted_at
) {
461 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
462 IncrementTimeDelta
increment(&cookie_load_duration_
);
464 UMA_HISTOGRAM_CUSTOM_TIMES(
465 "Cookie.TimeLoadDBQueueWait",
466 base::Time::Now() - posted_at
,
467 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
470 if (!InitializeDatabase()) {
471 PostClientTask(FROM_HERE
, base::Bind(
472 &Backend::CompleteLoadInForeground
, this, loaded_callback
, false));
474 ChainLoadCookies(loaded_callback
);
478 void SQLitePersistentCookieStore::Backend::LoadKeyAndNotifyInBackground(
479 const std::string
& key
,
480 const LoadedCallback
& loaded_callback
,
481 const base::Time
& posted_at
) {
482 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
483 IncrementTimeDelta
increment(&cookie_load_duration_
);
485 UMA_HISTOGRAM_CUSTOM_TIMES(
486 "Cookie.TimeKeyLoadDBQueueWait",
487 base::Time::Now() - posted_at
,
488 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
491 bool success
= false;
492 if (InitializeDatabase()) {
493 std::map
<std::string
, std::set
<std::string
> >::iterator
494 it
= keys_to_load_
.find(key
);
495 if (it
!= keys_to_load_
.end()) {
496 success
= LoadCookiesForDomains(it
->second
);
497 keys_to_load_
.erase(it
);
503 PostClientTask(FROM_HERE
, base::Bind(
504 &SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground
,
505 this, loaded_callback
, success
, posted_at
));
508 void SQLitePersistentCookieStore::Backend::CompleteLoadForKeyInForeground(
509 const LoadedCallback
& loaded_callback
,
511 const::Time
& requested_at
) {
512 DCHECK(client_task_runner_
->RunsTasksOnCurrentThread());
514 UMA_HISTOGRAM_CUSTOM_TIMES(
515 "Cookie.TimeKeyLoadTotalWait",
516 base::Time::Now() - requested_at
,
517 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
520 Notify(loaded_callback
, load_success
);
523 base::AutoLock
locked(metrics_lock_
);
524 num_priority_waiting_
--;
525 if (num_priority_waiting_
== 0) {
526 priority_wait_duration_
+=
527 base::Time::Now() - current_priority_wait_start_
;
533 void SQLitePersistentCookieStore::Backend::ReportMetricsInBackground() {
534 UMA_HISTOGRAM_CUSTOM_TIMES(
536 cookie_load_duration_
,
537 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
541 void SQLitePersistentCookieStore::Backend::ReportMetrics() {
542 PostBackgroundTask(FROM_HERE
, base::Bind(
543 &SQLitePersistentCookieStore::Backend::ReportMetricsInBackground
, this));
546 base::AutoLock
locked(metrics_lock_
);
547 UMA_HISTOGRAM_CUSTOM_TIMES(
548 "Cookie.PriorityBlockingTime",
549 priority_wait_duration_
,
550 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
553 UMA_HISTOGRAM_COUNTS_100(
554 "Cookie.PriorityLoadCount",
555 total_priority_requests_
);
557 UMA_HISTOGRAM_COUNTS_10000(
558 "Cookie.NumberOfLoadedCookies",
563 void SQLitePersistentCookieStore::Backend::CompleteLoadInForeground(
564 const LoadedCallback
& loaded_callback
, bool load_success
) {
565 Notify(loaded_callback
, load_success
);
571 void SQLitePersistentCookieStore::Backend::Notify(
572 const LoadedCallback
& loaded_callback
,
574 DCHECK(client_task_runner_
->RunsTasksOnCurrentThread());
576 std::vector
<net::CanonicalCookie
*> cookies
;
578 base::AutoLock
locked(lock_
);
579 cookies
.swap(cookies_
);
582 loaded_callback
.Run(cookies
);
585 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() {
586 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
588 if (initialized_
|| corruption_detected_
) {
589 // Return false if we were previously initialized but the DB has since been
590 // closed, or if corruption caused a database reset during initialization.
594 base::Time start
= base::Time::Now();
596 const base::FilePath dir
= path_
.DirName();
597 if (!base::PathExists(dir
) && !base::CreateDirectory(dir
)) {
602 if (base::GetFileSize(path_
, &db_size
))
603 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size
/ 1024 );
605 db_
.reset(new sql::Connection
);
606 db_
->set_histogram_tag("Cookie");
608 // Unretained to avoid a ref loop with |db_|.
609 db_
->set_error_callback(
610 base::Bind(&SQLitePersistentCookieStore::Backend::DatabaseErrorCallback
,
611 base::Unretained(this)));
613 if (!db_
->Open(path_
)) {
614 NOTREACHED() << "Unable to open cookie DB.";
615 if (corruption_detected_
)
622 if (!EnsureDatabaseVersion() || !InitTable(db_
.get())) {
623 NOTREACHED() << "Unable to open cookie DB.";
624 if (corruption_detected_
)
631 UMA_HISTOGRAM_CUSTOM_TIMES(
632 "Cookie.TimeInitializeDB",
633 base::Time::Now() - start
,
634 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
637 start
= base::Time::Now();
639 // Retrieve all the domains
640 sql::Statement
smt(db_
->GetUniqueStatement(
641 "SELECT DISTINCT host_key FROM cookies"));
643 if (!smt
.is_valid()) {
644 if (corruption_detected_
)
651 std::vector
<std::string
> host_keys
;
653 host_keys
.push_back(smt
.ColumnString(0));
655 UMA_HISTOGRAM_CUSTOM_TIMES(
656 "Cookie.TimeLoadDomains",
657 base::Time::Now() - start
,
658 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
661 base::Time start_parse
= base::Time::Now();
663 // Build a map of domain keys (always eTLD+1) to domains.
664 for (size_t idx
= 0; idx
< host_keys
.size(); ++idx
) {
665 const std::string
& domain
= host_keys
[idx
];
667 net::registry_controlled_domains::GetDomainAndRegistry(
669 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
671 keys_to_load_
[key
].insert(domain
);
674 UMA_HISTOGRAM_CUSTOM_TIMES(
675 "Cookie.TimeParseDomains",
676 base::Time::Now() - start_parse
,
677 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
680 UMA_HISTOGRAM_CUSTOM_TIMES(
681 "Cookie.TimeInitializeDomainMap",
682 base::Time::Now() - start
,
683 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
690 void SQLitePersistentCookieStore::Backend::ChainLoadCookies(
691 const LoadedCallback
& loaded_callback
) {
692 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
693 IncrementTimeDelta
increment(&cookie_load_duration_
);
695 bool load_success
= true;
698 // Close() has been called on this store.
699 load_success
= false;
700 } else if (keys_to_load_
.size() > 0) {
701 // Load cookies for the first domain key.
702 std::map
<std::string
, std::set
<std::string
> >::iterator
703 it
= keys_to_load_
.begin();
704 load_success
= LoadCookiesForDomains(it
->second
);
705 keys_to_load_
.erase(it
);
708 // If load is successful and there are more domain keys to be loaded,
709 // then post a background task to continue chain-load;
710 // Otherwise notify on client runner.
711 if (load_success
&& keys_to_load_
.size() > 0) {
712 bool success
= background_task_runner_
->PostDelayedTask(
714 base::Bind(&Backend::ChainLoadCookies
, this, loaded_callback
),
715 base::TimeDelta::FromMilliseconds(kLoadDelayMilliseconds
));
717 LOG(WARNING
) << "Failed to post task from " << FROM_HERE
.ToString()
718 << " to background_task_runner_.";
721 FinishedLoadingCookies(loaded_callback
, load_success
);
725 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains(
726 const std::set
<std::string
>& domains
) {
727 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
730 if (restore_old_session_cookies_
) {
731 smt
.Assign(db_
->GetCachedStatement(
733 "SELECT creation_utc, host_key, name, value, encrypted_value, path, "
734 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, "
735 "has_expires, persistent, priority FROM cookies WHERE host_key = ?"));
737 smt
.Assign(db_
->GetCachedStatement(
739 "SELECT creation_utc, host_key, name, value, encrypted_value, path, "
740 "expires_utc, secure, httponly, firstpartyonly, last_access_utc, "
741 "has_expires, persistent, priority FROM cookies WHERE host_key = ? "
742 "AND persistent = 1"));
744 if (!smt
.is_valid()) {
745 smt
.Clear(); // Disconnect smt_ref from db_.
751 std::vector
<net::CanonicalCookie
*> cookies
;
752 std::set
<std::string
>::const_iterator it
= domains
.begin();
753 for (; it
!= domains
.end(); ++it
) {
754 smt
.BindString(0, *it
);
755 MakeCookiesFromSQLStatement(&cookies
, &smt
);
759 base::AutoLock
locked(lock_
);
760 cookies_
.insert(cookies_
.end(), cookies
.begin(), cookies
.end());
765 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement(
766 std::vector
<net::CanonicalCookie
*>* cookies
,
767 sql::Statement
* statement
) {
768 sql::Statement
& smt
= *statement
;
771 std::string encrypted_value
= smt
.ColumnString(4);
772 if (!encrypted_value
.empty() && crypto_
) {
773 crypto_
->DecryptString(encrypted_value
, &value
);
775 DCHECK(encrypted_value
.empty());
776 value
= smt
.ColumnString(3);
778 scoped_ptr
<net::CanonicalCookie
> cc(new net::CanonicalCookie(
779 // The "source" URL is not used with persisted cookies.
781 smt
.ColumnString(2), // name
783 smt
.ColumnString(1), // domain
784 smt
.ColumnString(5), // path
785 Time::FromInternalValue(smt
.ColumnInt64(0)), // creation_utc
786 Time::FromInternalValue(smt
.ColumnInt64(6)), // expires_utc
787 Time::FromInternalValue(smt
.ColumnInt64(10)), // last_access_utc
788 smt
.ColumnInt(7) != 0, // secure
789 smt
.ColumnInt(8) != 0, // httponly
790 smt
.ColumnInt(9) != 0, // firstpartyonly
791 DBCookiePriorityToCookiePriority(
792 static_cast<DBCookiePriority
>(smt
.ColumnInt(13))))); // priority
793 DLOG_IF(WARNING
, cc
->CreationDate() > Time::Now())
794 << L
"CreationDate too recent";
795 cookies_per_origin_
[CookieOrigin(cc
->Domain(), cc
->IsSecure())]++;
796 cookies
->push_back(cc
.release());
801 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() {
803 if (!meta_table_
.Init(
804 db_
.get(), kCurrentVersionNumber
, kCompatibleVersionNumber
)) {
808 if (meta_table_
.GetCompatibleVersionNumber() > kCurrentVersionNumber
) {
809 LOG(WARNING
) << "Cookie database is too new.";
813 int cur_version
= meta_table_
.GetVersionNumber();
814 if (cur_version
== 2) {
815 sql::Transaction
transaction(db_
.get());
816 if (!transaction
.Begin())
818 if (!db_
->Execute("ALTER TABLE cookies ADD COLUMN last_access_utc "
819 "INTEGER DEFAULT 0") ||
820 !db_
->Execute("UPDATE cookies SET last_access_utc = creation_utc")) {
821 LOG(WARNING
) << "Unable to update cookie database to version 3.";
825 meta_table_
.SetVersionNumber(cur_version
);
826 meta_table_
.SetCompatibleVersionNumber(
827 std::min(cur_version
, kCompatibleVersionNumber
));
828 transaction
.Commit();
831 if (cur_version
== 3) {
832 // The time epoch changed for Mac & Linux in this version to match Windows.
833 // This patch came after the main epoch change happened, so some
834 // developers have "good" times for cookies added by the more recent
835 // versions. So we have to be careful to only update times that are under
836 // the old system (which will appear to be from before 1970 in the new
837 // system). The magic number used below is 1970 in our time units.
838 sql::Transaction
transaction(db_
.get());
841 ignore_result(db_
->Execute(
843 "SET creation_utc = creation_utc + 11644473600000000 "
845 "(SELECT rowid FROM cookies WHERE "
846 "creation_utc > 0 AND creation_utc < 11644473600000000)"));
847 ignore_result(db_
->Execute(
849 "SET expires_utc = expires_utc + 11644473600000000 "
851 "(SELECT rowid FROM cookies WHERE "
852 "expires_utc > 0 AND expires_utc < 11644473600000000)"));
853 ignore_result(db_
->Execute(
855 "SET last_access_utc = last_access_utc + 11644473600000000 "
857 "(SELECT rowid FROM cookies WHERE "
858 "last_access_utc > 0 AND last_access_utc < 11644473600000000)"));
861 meta_table_
.SetVersionNumber(cur_version
);
862 transaction
.Commit();
865 if (cur_version
== 4) {
866 const base::TimeTicks start_time
= base::TimeTicks::Now();
867 sql::Transaction
transaction(db_
.get());
868 if (!transaction
.Begin())
870 if (!db_
->Execute("ALTER TABLE cookies "
871 "ADD COLUMN has_expires INTEGER DEFAULT 1") ||
872 !db_
->Execute("ALTER TABLE cookies "
873 "ADD COLUMN persistent INTEGER DEFAULT 1")) {
874 LOG(WARNING
) << "Unable to update cookie database to version 5.";
878 meta_table_
.SetVersionNumber(cur_version
);
879 meta_table_
.SetCompatibleVersionNumber(
880 std::min(cur_version
, kCompatibleVersionNumber
));
881 transaction
.Commit();
882 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV5",
883 base::TimeTicks::Now() - start_time
);
886 if (cur_version
== 5) {
887 const base::TimeTicks start_time
= base::TimeTicks::Now();
888 sql::Transaction
transaction(db_
.get());
889 if (!transaction
.Begin())
891 // Alter the table to add the priority column with a default value.
892 std::string
stmt(base::StringPrintf(
893 "ALTER TABLE cookies ADD COLUMN priority INTEGER DEFAULT %d",
894 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT
)));
895 if (!db_
->Execute(stmt
.c_str())) {
896 LOG(WARNING
) << "Unable to update cookie database to version 6.";
900 meta_table_
.SetVersionNumber(cur_version
);
901 meta_table_
.SetCompatibleVersionNumber(
902 std::min(cur_version
, kCompatibleVersionNumber
));
903 transaction
.Commit();
904 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6",
905 base::TimeTicks::Now() - start_time
);
908 if (cur_version
== 6) {
909 const base::TimeTicks start_time
= base::TimeTicks::Now();
910 sql::Transaction
transaction(db_
.get());
911 if (!transaction
.Begin())
913 // Alter the table to add empty "encrypted value" column.
914 if (!db_
->Execute("ALTER TABLE cookies "
915 "ADD COLUMN encrypted_value BLOB DEFAULT ''")) {
916 LOG(WARNING
) << "Unable to update cookie database to version 7.";
920 meta_table_
.SetVersionNumber(cur_version
);
921 meta_table_
.SetCompatibleVersionNumber(
922 std::min(cur_version
, kCompatibleVersionNumber
));
923 transaction
.Commit();
924 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV7",
925 base::TimeTicks::Now() - start_time
);
928 if (cur_version
== 7) {
929 const base::TimeTicks start_time
= base::TimeTicks::Now();
930 sql::Transaction
transaction(db_
.get());
931 if (!transaction
.Begin())
933 // Alter the table to add a 'firstpartyonly' column.
935 "ALTER TABLE cookies "
936 "ADD COLUMN firstpartyonly INTEGER DEFAULT 0")) {
937 LOG(WARNING
) << "Unable to update cookie database to version 8.";
941 meta_table_
.SetVersionNumber(cur_version
);
942 meta_table_
.SetCompatibleVersionNumber(
943 std::min(cur_version
, kCompatibleVersionNumber
));
944 transaction
.Commit();
945 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV8",
946 base::TimeTicks::Now() - start_time
);
949 // Put future migration cases here.
951 if (cur_version
< kCurrentVersionNumber
) {
952 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1);
955 db_
.reset(new sql::Connection
);
956 if (!base::DeleteFile(path_
, false) ||
959 db_
.get(), kCurrentVersionNumber
, kCompatibleVersionNumber
)) {
960 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTableRecoveryFailed", 1);
961 NOTREACHED() << "Unable to reset the cookie DB.";
971 void SQLitePersistentCookieStore::Backend::AddCookie(
972 const net::CanonicalCookie
& cc
) {
973 BatchOperation(PendingOperation::COOKIE_ADD
, cc
);
976 void SQLitePersistentCookieStore::Backend::UpdateCookieAccessTime(
977 const net::CanonicalCookie
& cc
) {
978 BatchOperation(PendingOperation::COOKIE_UPDATEACCESS
, cc
);
981 void SQLitePersistentCookieStore::Backend::DeleteCookie(
982 const net::CanonicalCookie
& cc
) {
983 BatchOperation(PendingOperation::COOKIE_DELETE
, cc
);
986 void SQLitePersistentCookieStore::Backend::BatchOperation(
987 PendingOperation::OperationType op
,
988 const net::CanonicalCookie
& cc
) {
989 // Commit every 30 seconds.
990 static const int kCommitIntervalMs
= 30 * 1000;
991 // Commit right away if we have more than 512 outstanding operations.
992 static const size_t kCommitAfterBatchSize
= 512;
993 DCHECK(!background_task_runner_
->RunsTasksOnCurrentThread());
995 // We do a full copy of the cookie here, and hopefully just here.
996 scoped_ptr
<PendingOperation
> po(new PendingOperation(op
, cc
));
998 PendingOperationsList::size_type num_pending
;
1000 base::AutoLock
locked(lock_
);
1001 pending_
.push_back(po
.release());
1002 num_pending
= ++num_pending_
;
1005 if (num_pending
== 1) {
1006 // We've gotten our first entry for this batch, fire off the timer.
1007 if (!background_task_runner_
->PostDelayedTask(
1008 FROM_HERE
, base::Bind(&Backend::Commit
, this),
1009 base::TimeDelta::FromMilliseconds(kCommitIntervalMs
))) {
1010 NOTREACHED() << "background_task_runner_ is not running.";
1012 } else if (num_pending
== kCommitAfterBatchSize
) {
1013 // We've reached a big enough batch, fire off a commit now.
1014 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::Commit
, this));
1018 void SQLitePersistentCookieStore::Backend::Commit() {
1019 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1021 PendingOperationsList ops
;
1023 base::AutoLock
locked(lock_
);
1028 // Maybe an old timer fired or we are already Close()'ed.
1029 if (!db_
.get() || ops
.empty())
1032 sql::Statement
add_smt(db_
->GetCachedStatement(
1034 "INSERT INTO cookies (creation_utc, host_key, name, value, "
1035 "encrypted_value, path, expires_utc, secure, httponly, firstpartyonly, "
1036 "last_access_utc, has_expires, persistent, priority) "
1037 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"));
1038 if (!add_smt
.is_valid())
1041 sql::Statement
update_access_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
1042 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?"));
1043 if (!update_access_smt
.is_valid())
1046 sql::Statement
del_smt(db_
->GetCachedStatement(SQL_FROM_HERE
,
1047 "DELETE FROM cookies WHERE creation_utc=?"));
1048 if (!del_smt
.is_valid())
1051 sql::Transaction
transaction(db_
.get());
1052 if (!transaction
.Begin())
1055 for (PendingOperationsList::iterator it
= ops
.begin();
1056 it
!= ops
.end(); ++it
) {
1057 // Free the cookies as we commit them to the database.
1058 scoped_ptr
<PendingOperation
> po(*it
);
1060 case PendingOperation::COOKIE_ADD
:
1061 cookies_per_origin_
[
1062 CookieOrigin(po
->cc().Domain(), po
->cc().IsSecure())]++;
1063 add_smt
.Reset(true);
1064 add_smt
.BindInt64(0, po
->cc().CreationDate().ToInternalValue());
1065 add_smt
.BindString(1, po
->cc().Domain());
1066 add_smt
.BindString(2, po
->cc().Name());
1068 std::string encrypted_value
;
1069 add_smt
.BindCString(3, ""); // value
1070 crypto_
->EncryptString(po
->cc().Value(), &encrypted_value
);
1071 // BindBlob() immediately makes an internal copy of the data.
1072 add_smt
.BindBlob(4, encrypted_value
.data(),
1073 static_cast<int>(encrypted_value
.length()));
1075 add_smt
.BindString(3, po
->cc().Value());
1076 add_smt
.BindBlob(4, "", 0); // encrypted_value
1078 add_smt
.BindString(5, po
->cc().Path());
1079 add_smt
.BindInt64(6, po
->cc().ExpiryDate().ToInternalValue());
1080 add_smt
.BindInt(7, po
->cc().IsSecure());
1081 add_smt
.BindInt(8, po
->cc().IsHttpOnly());
1082 add_smt
.BindInt(9, po
->cc().IsFirstPartyOnly());
1083 add_smt
.BindInt64(10, po
->cc().LastAccessDate().ToInternalValue());
1084 add_smt
.BindInt(11, po
->cc().IsPersistent());
1085 add_smt
.BindInt(12, po
->cc().IsPersistent());
1087 CookiePriorityToDBCookiePriority(po
->cc().Priority()));
1089 NOTREACHED() << "Could not add a cookie to the DB.";
1092 case PendingOperation::COOKIE_UPDATEACCESS
:
1093 update_access_smt
.Reset(true);
1094 update_access_smt
.BindInt64(0,
1095 po
->cc().LastAccessDate().ToInternalValue());
1096 update_access_smt
.BindInt64(1,
1097 po
->cc().CreationDate().ToInternalValue());
1098 if (!update_access_smt
.Run())
1099 NOTREACHED() << "Could not update cookie last access time in the DB.";
1102 case PendingOperation::COOKIE_DELETE
:
1103 cookies_per_origin_
[
1104 CookieOrigin(po
->cc().Domain(), po
->cc().IsSecure())]--;
1105 del_smt
.Reset(true);
1106 del_smt
.BindInt64(0, po
->cc().CreationDate().ToInternalValue());
1108 NOTREACHED() << "Could not delete a cookie from the DB.";
1116 bool succeeded
= transaction
.Commit();
1117 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults",
1118 succeeded
? 0 : 1, 2);
1121 void SQLitePersistentCookieStore::Backend::Flush(
1122 const base::Closure
& callback
) {
1123 DCHECK(!background_task_runner_
->RunsTasksOnCurrentThread());
1124 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::Commit
, this));
1126 if (!callback
.is_null()) {
1127 // We want the completion task to run immediately after Commit() returns.
1128 // Posting it from here means there is less chance of another task getting
1129 // onto the message queue first, than if we posted it from Commit() itself.
1130 PostBackgroundTask(FROM_HERE
, callback
);
1134 // Fire off a close message to the background runner. We could still have a
1135 // pending commit timer or Load operations holding references on us, but if/when
1136 // this fires we will already have been cleaned up and it will be ignored.
1137 void SQLitePersistentCookieStore::Backend::Close() {
1138 if (background_task_runner_
->RunsTasksOnCurrentThread()) {
1139 InternalBackgroundClose();
1141 // Must close the backend on the background runner.
1142 PostBackgroundTask(FROM_HERE
,
1143 base::Bind(&Backend::InternalBackgroundClose
, this));
1147 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() {
1148 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1149 // Commit any pending operations
1152 if (!force_keep_session_state_
&& special_storage_policy_
.get() &&
1153 special_storage_policy_
->HasSessionOnlyOrigins()) {
1154 DeleteSessionCookiesOnShutdown();
1157 meta_table_
.Reset();
1161 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() {
1162 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1167 if (!special_storage_policy_
.get())
1170 sql::Statement
del_smt(db_
->GetCachedStatement(
1171 SQL_FROM_HERE
, "DELETE FROM cookies WHERE host_key=? AND secure=?"));
1172 if (!del_smt
.is_valid()) {
1173 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1177 sql::Transaction
transaction(db_
.get());
1178 if (!transaction
.Begin()) {
1179 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1183 for (CookiesPerOriginMap::iterator it
= cookies_per_origin_
.begin();
1184 it
!= cookies_per_origin_
.end(); ++it
) {
1185 if (it
->second
<= 0) {
1186 DCHECK_EQ(0, it
->second
);
1189 const GURL
url(net::cookie_util::CookieOriginToURL(it
->first
.first
,
1191 if (!url
.is_valid() || !special_storage_policy_
->IsStorageSessionOnly(url
))
1194 del_smt
.Reset(true);
1195 del_smt
.BindString(0, it
->first
.first
);
1196 del_smt
.BindInt(1, it
->first
.second
);
1198 NOTREACHED() << "Could not delete a cookie from the DB.";
1201 if (!transaction
.Commit())
1202 LOG(WARNING
) << "Unable to delete cookies on shutdown.";
1205 void SQLitePersistentCookieStore::Backend::DatabaseErrorCallback(
1207 sql::Statement
* stmt
) {
1208 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1210 if (!sql::IsErrorCatastrophic(error
))
1213 // TODO(shess): Running KillDatabase() multiple times should be
1215 if (corruption_detected_
)
1218 corruption_detected_
= true;
1220 // Don't just do the close/delete here, as we are being called by |db| and
1221 // that seems dangerous.
1222 // TODO(shess): Consider just calling RazeAndClose() immediately.
1223 // db_ may not be safe to reset at this point, but RazeAndClose()
1224 // would cause the stack to unwind safely with errors.
1225 PostBackgroundTask(FROM_HERE
, base::Bind(&Backend::KillDatabase
, this));
1228 void SQLitePersistentCookieStore::Backend::KillDatabase() {
1229 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1232 // This Backend will now be in-memory only. In a future run we will recreate
1233 // the database. Hopefully things go better then!
1234 bool success
= db_
->RazeAndClose();
1235 UMA_HISTOGRAM_BOOLEAN("Cookie.KillDatabaseResult", success
);
1236 meta_table_
.Reset();
1241 void SQLitePersistentCookieStore::Backend::SetForceKeepSessionState() {
1242 base::AutoLock
locked(lock_
);
1243 force_keep_session_state_
= true;
1246 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() {
1247 DCHECK(background_task_runner_
->RunsTasksOnCurrentThread());
1248 if (!db_
->Execute("DELETE FROM cookies WHERE persistent == 0"))
1249 LOG(WARNING
) << "Unable to delete session cookies.";
1252 void SQLitePersistentCookieStore::Backend::PostBackgroundTask(
1253 const tracked_objects::Location
& origin
, const base::Closure
& task
) {
1254 if (!background_task_runner_
->PostTask(origin
, task
)) {
1255 LOG(WARNING
) << "Failed to post task from " << origin
.ToString()
1256 << " to background_task_runner_.";
1260 void SQLitePersistentCookieStore::Backend::PostClientTask(
1261 const tracked_objects::Location
& origin
, const base::Closure
& task
) {
1262 if (!client_task_runner_
->PostTask(origin
, task
)) {
1263 LOG(WARNING
) << "Failed to post task from " << origin
.ToString()
1264 << " to client_task_runner_.";
1268 void SQLitePersistentCookieStore::Backend::FinishedLoadingCookies(
1269 const LoadedCallback
& loaded_callback
,
1271 PostClientTask(FROM_HERE
, base::Bind(&Backend::CompleteLoadInForeground
, this,
1272 loaded_callback
, success
));
1273 if (success
&& !restore_old_session_cookies_
)
1274 DeleteSessionCookiesOnStartup();
1277 SQLitePersistentCookieStore::SQLitePersistentCookieStore(
1278 const base::FilePath
& path
,
1279 const scoped_refptr
<base::SequencedTaskRunner
>& client_task_runner
,
1280 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
1281 bool restore_old_session_cookies
,
1282 storage::SpecialStoragePolicy
* special_storage_policy
,
1283 CookieCryptoDelegate
* crypto_delegate
)
1284 : backend_(new Backend(path
,
1286 background_task_runner
,
1287 restore_old_session_cookies
,
1288 special_storage_policy
,
1292 void SQLitePersistentCookieStore::Load(const LoadedCallback
& loaded_callback
) {
1293 backend_
->Load(loaded_callback
);
1296 void SQLitePersistentCookieStore::LoadCookiesForKey(
1297 const std::string
& key
,
1298 const LoadedCallback
& loaded_callback
) {
1299 backend_
->LoadCookiesForKey(key
, loaded_callback
);
1302 void SQLitePersistentCookieStore::AddCookie(const net::CanonicalCookie
& cc
) {
1303 backend_
->AddCookie(cc
);
1306 void SQLitePersistentCookieStore::UpdateCookieAccessTime(
1307 const net::CanonicalCookie
& cc
) {
1308 backend_
->UpdateCookieAccessTime(cc
);
1311 void SQLitePersistentCookieStore::DeleteCookie(const net::CanonicalCookie
& cc
) {
1312 backend_
->DeleteCookie(cc
);
1315 void SQLitePersistentCookieStore::SetForceKeepSessionState() {
1316 backend_
->SetForceKeepSessionState();
1319 void SQLitePersistentCookieStore::Flush(const base::Closure
& callback
) {
1320 backend_
->Flush(callback
);
1323 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() {
1325 // We release our reference to the Backend, though it will probably still have
1326 // a reference if the background runner has not run Close() yet.
1329 CookieStoreConfig::CookieStoreConfig()
1330 : session_cookie_mode(EPHEMERAL_SESSION_COOKIES
),
1331 crypto_delegate(NULL
) {
1332 // Default to an in-memory cookie store.
1335 CookieStoreConfig::CookieStoreConfig(
1336 const base::FilePath
& path
,
1337 SessionCookieMode session_cookie_mode
,
1338 storage::SpecialStoragePolicy
* storage_policy
,
1339 net::CookieMonsterDelegate
* cookie_delegate
)
1341 session_cookie_mode(session_cookie_mode
),
1342 storage_policy(storage_policy
),
1343 cookie_delegate(cookie_delegate
),
1344 crypto_delegate(NULL
) {
1345 CHECK(!path
.empty() || session_cookie_mode
== EPHEMERAL_SESSION_COOKIES
);
1348 CookieStoreConfig::~CookieStoreConfig() {
1351 net::CookieStore
* CreateCookieStore(const CookieStoreConfig
& config
) {
1352 net::CookieMonster
* cookie_monster
= NULL
;
1354 if (config
.path
.empty()) {
1355 // Empty path means in-memory store.
1356 cookie_monster
= new net::CookieMonster(NULL
, config
.cookie_delegate
.get());
1358 scoped_refptr
<base::SequencedTaskRunner
> client_task_runner
=
1359 config
.client_task_runner
;
1360 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
=
1361 config
.background_task_runner
;
1363 if (!client_task_runner
.get()) {
1364 client_task_runner
=
1365 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
);
1368 if (!background_task_runner
.get()) {
1369 background_task_runner
=
1370 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner(
1371 BrowserThread::GetBlockingPool()->GetSequenceToken());
1374 SQLitePersistentCookieStore
* persistent_store
=
1375 new SQLitePersistentCookieStore(
1378 background_task_runner
,
1379 (config
.session_cookie_mode
==
1380 CookieStoreConfig::RESTORED_SESSION_COOKIES
),
1381 config
.storage_policy
.get(),
1382 config
.crypto_delegate
);
1385 new net::CookieMonster(persistent_store
, config
.cookie_delegate
.get());
1386 if ((config
.session_cookie_mode
==
1387 CookieStoreConfig::PERSISTANT_SESSION_COOKIES
) ||
1388 (config
.session_cookie_mode
==
1389 CookieStoreConfig::RESTORED_SESSION_COOKIES
)) {
1390 cookie_monster
->SetPersistSessionCookies(true);
1394 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1395 switches::kEnableFileCookies
)) {
1396 cookie_monster
->SetEnableFileScheme(true);
1399 return cookie_monster
;
1402 } // namespace content