OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/metrics/histogram.h" | 21 #include "base/metrics/histogram.h" |
22 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
24 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
25 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
26 #include "base/threading/sequenced_worker_pool.h" | 26 #include "base/threading/sequenced_worker_pool.h" |
27 #include "base/time/time.h" | 27 #include "base/time/time.h" |
28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/cookie_crypto_delegate.h" |
29 #include "content/public/browser/cookie_store_factory.h" | 30 #include "content/public/browser/cookie_store_factory.h" |
30 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 31 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
31 #include "net/cookies/canonical_cookie.h" | 32 #include "net/cookies/canonical_cookie.h" |
32 #include "net/cookies/cookie_constants.h" | 33 #include "net/cookies/cookie_constants.h" |
33 #include "net/cookies/cookie_util.h" | 34 #include "net/cookies/cookie_util.h" |
34 #include "sql/error_delegate_util.h" | 35 #include "sql/error_delegate_util.h" |
35 #include "sql/meta_table.h" | 36 #include "sql/meta_table.h" |
36 #include "sql/statement.h" | 37 #include "sql/statement.h" |
37 #include "sql/transaction.h" | 38 #include "sql/transaction.h" |
38 #include "third_party/sqlite/sqlite3.h" | 39 #include "third_party/sqlite/sqlite3.h" |
(...skipping 27 matching lines...) Expand all Loading... |
66 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), | 67 // disk on the BG runner every 30 seconds, 512 operations, or call to Flush(), |
67 // whichever occurs first. | 68 // whichever occurs first. |
68 class SQLitePersistentCookieStore::Backend | 69 class SQLitePersistentCookieStore::Backend |
69 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 70 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
70 public: | 71 public: |
71 Backend( | 72 Backend( |
72 const base::FilePath& path, | 73 const base::FilePath& path, |
73 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 74 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
74 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 75 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
75 bool restore_old_session_cookies, | 76 bool restore_old_session_cookies, |
76 quota::SpecialStoragePolicy* special_storage_policy) | 77 quota::SpecialStoragePolicy* special_storage_policy, |
| 78 scoped_ptr<CookieCryptoDelegate> crypto_delegate) |
77 : path_(path), | 79 : path_(path), |
78 num_pending_(0), | 80 num_pending_(0), |
79 force_keep_session_state_(false), | 81 force_keep_session_state_(false), |
80 initialized_(false), | 82 initialized_(false), |
81 corruption_detected_(false), | 83 corruption_detected_(false), |
82 restore_old_session_cookies_(restore_old_session_cookies), | 84 restore_old_session_cookies_(restore_old_session_cookies), |
83 special_storage_policy_(special_storage_policy), | 85 special_storage_policy_(special_storage_policy), |
84 num_cookies_read_(0), | 86 num_cookies_read_(0), |
85 client_task_runner_(client_task_runner), | 87 client_task_runner_(client_task_runner), |
86 background_task_runner_(background_task_runner), | 88 background_task_runner_(background_task_runner), |
87 num_priority_waiting_(0), | 89 num_priority_waiting_(0), |
88 total_priority_requests_(0) {} | 90 total_priority_requests_(0), |
| 91 crypto_(crypto_delegate.Pass()) {} |
89 | 92 |
90 // Creates or loads the SQLite database. | 93 // Creates or loads the SQLite database. |
91 void Load(const LoadedCallback& loaded_callback); | 94 void Load(const LoadedCallback& loaded_callback); |
92 | 95 |
93 // Loads cookies for the domain key (eTLD+1). | 96 // Loads cookies for the domain key (eTLD+1). |
94 void LoadCookiesForKey(const std::string& domain, | 97 void LoadCookiesForKey(const std::string& domain, |
95 const LoadedCallback& loaded_callback); | 98 const LoadedCallback& loaded_callback); |
96 | 99 |
97 // Batch a cookie addition. | 100 // Batch a cookie addition. |
98 void AddCookie(const net::CanonicalCookie& cc); | 101 void AddCookie(const net::CanonicalCookie& cc); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 // starting/completing priority loads or completing the total load). | 264 // starting/completing priority loads or completing the total load). |
262 base::Lock metrics_lock_; | 265 base::Lock metrics_lock_; |
263 int num_priority_waiting_; | 266 int num_priority_waiting_; |
264 // The total number of priority requests. | 267 // The total number of priority requests. |
265 int total_priority_requests_; | 268 int total_priority_requests_; |
266 // The time when |num_priority_waiting_| incremented to 1. | 269 // The time when |num_priority_waiting_| incremented to 1. |
267 base::Time current_priority_wait_start_; | 270 base::Time current_priority_wait_start_; |
268 // The cumulative duration of time when |num_priority_waiting_| was greater | 271 // The cumulative duration of time when |num_priority_waiting_| was greater |
269 // than 1. | 272 // than 1. |
270 base::TimeDelta priority_wait_duration_; | 273 base::TimeDelta priority_wait_duration_; |
| 274 // Class with functions that do cryptographic operations (for protecting |
| 275 // cookies stored persistently). |
| 276 scoped_ptr<CookieCryptoDelegate> crypto_; |
271 | 277 |
272 DISALLOW_COPY_AND_ASSIGN(Backend); | 278 DISALLOW_COPY_AND_ASSIGN(Backend); |
273 }; | 279 }; |
274 | 280 |
275 namespace { | 281 namespace { |
276 | 282 |
277 // Version number of the database. | 283 // Version number of the database. |
278 // | 284 // |
| 285 // Version 7 adds encrypted values. Old values will continue to be used but |
| 286 // all new values written will be encrypted on selected operating systems. New |
| 287 // records read by old clients will simply get an empty cookie value while old |
| 288 // records read by new clients will continue to operate with the unencrypted |
| 289 // version. New and old clients alike will always write/update records with |
| 290 // what they support. |
| 291 // |
279 // Version 6 adds cookie priorities. This allows developers to influence the | 292 // Version 6 adds cookie priorities. This allows developers to influence the |
280 // order in which cookies are evicted in order to meet domain cookie limits. | 293 // order in which cookies are evicted in order to meet domain cookie limits. |
281 // | 294 // |
282 // Version 5 adds the columns has_expires and is_persistent, so that the | 295 // Version 5 adds the columns has_expires and is_persistent, so that the |
283 // database can store session cookies as well as persistent cookies. Databases | 296 // database can store session cookies as well as persistent cookies. Databases |
284 // of version 5 are incompatible with older versions of code. If a database of | 297 // of version 5 are incompatible with older versions of code. If a database of |
285 // version 5 is read by older code, session cookies will be treated as normal | 298 // version 5 is read by older code, session cookies will be treated as normal |
286 // cookies. Currently, these fields are written, but not read anymore. | 299 // cookies. Currently, these fields are written, but not read anymore. |
287 // | 300 // |
288 // In version 4, we migrated the time epoch. If you open the DB with an older | 301 // In version 4, we migrated the time epoch. If you open the DB with an older |
289 // version on Mac or Linux, the times will look wonky, but the file will likely | 302 // version on Mac or Linux, the times will look wonky, but the file will likely |
290 // be usable. On Windows version 3 and 4 are the same. | 303 // be usable. On Windows version 3 and 4 are the same. |
291 // | 304 // |
292 // Version 3 updated the database to include the last access time, so we can | 305 // Version 3 updated the database to include the last access time, so we can |
293 // expire them in decreasing order of use when we've reached the maximum | 306 // expire them in decreasing order of use when we've reached the maximum |
294 // number of cookies. | 307 // number of cookies. |
295 const int kCurrentVersionNumber = 6; | 308 const int kCurrentVersionNumber = 7; |
296 const int kCompatibleVersionNumber = 5; | 309 const int kCompatibleVersionNumber = 5; |
297 | 310 |
298 // Possible values for the 'priority' column. | 311 // Possible values for the 'priority' column. |
299 enum DBCookiePriority { | 312 enum DBCookiePriority { |
300 kCookiePriorityLow = 0, | 313 kCookiePriorityLow = 0, |
301 kCookiePriorityMedium = 1, | 314 kCookiePriorityMedium = 1, |
302 kCookiePriorityHigh = 2, | 315 kCookiePriorityHigh = 2, |
303 }; | 316 }; |
304 | 317 |
305 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { | 318 DBCookiePriority CookiePriorityToDBCookiePriority(net::CookiePriority value) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 "host_key TEXT NOT NULL," | 375 "host_key TEXT NOT NULL," |
363 "name TEXT NOT NULL," | 376 "name TEXT NOT NULL," |
364 "value TEXT NOT NULL," | 377 "value TEXT NOT NULL," |
365 "path TEXT NOT NULL," | 378 "path TEXT NOT NULL," |
366 "expires_utc INTEGER NOT NULL," | 379 "expires_utc INTEGER NOT NULL," |
367 "secure INTEGER NOT NULL," | 380 "secure INTEGER NOT NULL," |
368 "httponly INTEGER NOT NULL," | 381 "httponly INTEGER NOT NULL," |
369 "last_access_utc INTEGER NOT NULL, " | 382 "last_access_utc INTEGER NOT NULL, " |
370 "has_expires INTEGER NOT NULL DEFAULT 1, " | 383 "has_expires INTEGER NOT NULL DEFAULT 1, " |
371 "persistent INTEGER NOT NULL DEFAULT 1," | 384 "persistent INTEGER NOT NULL DEFAULT 1," |
372 "priority INTEGER NOT NULL DEFAULT %d)", | 385 "priority INTEGER NOT NULL DEFAULT %d," |
| 386 "encrypted_value BLOB DEFAULT '')", |
373 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); | 387 CookiePriorityToDBCookiePriority(net::COOKIE_PRIORITY_DEFAULT))); |
374 if (!db->Execute(stmt.c_str())) | 388 if (!db->Execute(stmt.c_str())) |
375 return false; | 389 return false; |
376 } | 390 } |
377 | 391 |
378 // Older code created an index on creation_utc, which is already | 392 // Older code created an index on creation_utc, which is already |
379 // primary key for the table. | 393 // primary key for the table. |
380 if (!db->Execute("DROP INDEX IF EXISTS cookie_times")) | 394 if (!db->Execute("DROP INDEX IF EXISTS cookie_times")) |
381 return false; | 395 return false; |
382 | 396 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 } | 685 } |
672 | 686 |
673 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | 687 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( |
674 const std::set<std::string>& domains) { | 688 const std::set<std::string>& domains) { |
675 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 689 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
676 | 690 |
677 sql::Statement smt; | 691 sql::Statement smt; |
678 if (restore_old_session_cookies_) { | 692 if (restore_old_session_cookies_) { |
679 smt.Assign(db_->GetCachedStatement( | 693 smt.Assign(db_->GetCachedStatement( |
680 SQL_FROM_HERE, | 694 SQL_FROM_HERE, |
681 "SELECT creation_utc, host_key, name, value, path, expires_utc, " | 695 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " |
682 "secure, httponly, last_access_utc, has_expires, persistent, priority " | 696 "expires_utc, secure, httponly, last_access_utc, has_expires, " |
683 "FROM cookies WHERE host_key = ?")); | 697 "persistent, priority FROM cookies WHERE host_key = ?")); |
684 } else { | 698 } else { |
685 smt.Assign(db_->GetCachedStatement( | 699 smt.Assign(db_->GetCachedStatement( |
686 SQL_FROM_HERE, | 700 SQL_FROM_HERE, |
687 "SELECT creation_utc, host_key, name, value, path, expires_utc, " | 701 "SELECT creation_utc, host_key, name, value, encrypted_value, path, " |
688 "secure, httponly, last_access_utc, has_expires, persistent, priority " | 702 "expires_utc, secure, httponly, last_access_utc, has_expires, " |
689 "FROM cookies WHERE host_key = ? AND persistent = 1")); | 703 "persistent, priority FROM cookies WHERE host_key = ? " |
| 704 "AND persistent = 1")); |
690 } | 705 } |
691 if (!smt.is_valid()) { | 706 if (!smt.is_valid()) { |
692 smt.Clear(); // Disconnect smt_ref from db_. | 707 smt.Clear(); // Disconnect smt_ref from db_. |
693 meta_table_.Reset(); | 708 meta_table_.Reset(); |
694 db_.reset(); | 709 db_.reset(); |
695 return false; | 710 return false; |
696 } | 711 } |
697 | 712 |
698 std::vector<net::CanonicalCookie*> cookies; | 713 std::vector<net::CanonicalCookie*> cookies; |
699 std::set<std::string>::const_iterator it = domains.begin(); | 714 std::set<std::string>::const_iterator it = domains.begin(); |
700 for (; it != domains.end(); ++it) { | 715 for (; it != domains.end(); ++it) { |
701 smt.BindString(0, *it); | 716 smt.BindString(0, *it); |
702 while (smt.Step()) { | 717 while (smt.Step()) { |
| 718 std::string value; |
| 719 std::string encrypted_value = smt.ColumnString(4); |
| 720 if (!encrypted_value.empty() && crypto_.get()) { |
| 721 crypto_->DecryptString(encrypted_value, &value); |
| 722 } else { |
| 723 DCHECK(encrypted_value.empty()); |
| 724 value = smt.ColumnString(3); |
| 725 } |
703 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( | 726 scoped_ptr<net::CanonicalCookie> cc(new net::CanonicalCookie( |
704 // The "source" URL is not used with persisted cookies. | 727 // The "source" URL is not used with persisted cookies. |
705 GURL(), // Source | 728 GURL(), // Source |
706 smt.ColumnString(2), // name | 729 smt.ColumnString(2), // name |
707 smt.ColumnString(3), // value | 730 value, // value |
708 smt.ColumnString(1), // domain | 731 smt.ColumnString(1), // domain |
709 smt.ColumnString(4), // path | 732 smt.ColumnString(5), // path |
710 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 733 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
711 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc | 734 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc |
712 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc | 735 Time::FromInternalValue(smt.ColumnInt64(9)), // last_access_utc |
713 smt.ColumnInt(6) != 0, // secure | 736 smt.ColumnInt(7) != 0, // secure |
714 smt.ColumnInt(7) != 0, // httponly | 737 smt.ColumnInt(8) != 0, // httponly |
715 DBCookiePriorityToCookiePriority( | 738 DBCookiePriorityToCookiePriority( |
716 static_cast<DBCookiePriority>(smt.ColumnInt(11))))); // priority | 739 static_cast<DBCookiePriority>(smt.ColumnInt(12))))); // priority |
717 DLOG_IF(WARNING, | 740 DLOG_IF(WARNING, |
718 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; | 741 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; |
719 cookies_per_origin_[CookieOrigin(cc->Domain(), cc->IsSecure())]++; | 742 cookies_per_origin_[CookieOrigin(cc->Domain(), cc->IsSecure())]++; |
720 cookies.push_back(cc.release()); | 743 cookies.push_back(cc.release()); |
721 ++num_cookies_read_; | 744 ++num_cookies_read_; |
722 } | 745 } |
723 smt.Reset(true); | 746 smt.Reset(true); |
724 } | 747 } |
725 { | 748 { |
726 base::AutoLock locked(lock_); | 749 base::AutoLock locked(lock_); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 } | 852 } |
830 ++cur_version; | 853 ++cur_version; |
831 meta_table_.SetVersionNumber(cur_version); | 854 meta_table_.SetVersionNumber(cur_version); |
832 meta_table_.SetCompatibleVersionNumber( | 855 meta_table_.SetCompatibleVersionNumber( |
833 std::min(cur_version, kCompatibleVersionNumber)); | 856 std::min(cur_version, kCompatibleVersionNumber)); |
834 transaction.Commit(); | 857 transaction.Commit(); |
835 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", | 858 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV6", |
836 base::TimeTicks::Now() - start_time); | 859 base::TimeTicks::Now() - start_time); |
837 } | 860 } |
838 | 861 |
| 862 if (cur_version == 6) { |
| 863 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 864 sql::Transaction transaction(db_.get()); |
| 865 if (!transaction.Begin()) |
| 866 return false; |
| 867 // Alter the table to add empty "encrypted value" column. |
| 868 if (!db_->Execute("ALTER TABLE cookies " |
| 869 "ADD COLUMN encrypted_value BLOB DEFAULT ''")) { |
| 870 LOG(WARNING) << "Unable to update cookie database to version 7."; |
| 871 return false; |
| 872 } |
| 873 ++cur_version; |
| 874 meta_table_.SetVersionNumber(cur_version); |
| 875 meta_table_.SetCompatibleVersionNumber( |
| 876 std::min(cur_version, kCompatibleVersionNumber)); |
| 877 transaction.Commit(); |
| 878 UMA_HISTOGRAM_TIMES("Cookie.TimeDatabaseMigrationToV7", |
| 879 base::TimeTicks::Now() - start_time); |
| 880 } |
| 881 |
839 // Put future migration cases here. | 882 // Put future migration cases here. |
840 | 883 |
841 if (cur_version < kCurrentVersionNumber) { | 884 if (cur_version < kCurrentVersionNumber) { |
842 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); | 885 UMA_HISTOGRAM_COUNTS_100("Cookie.CorruptMetaTable", 1); |
843 | 886 |
844 meta_table_.Reset(); | 887 meta_table_.Reset(); |
845 db_.reset(new sql::Connection); | 888 db_.reset(new sql::Connection); |
846 if (!base::DeleteFile(path_, false) || | 889 if (!base::DeleteFile(path_, false) || |
847 !db_->Open(path_) || | 890 !db_->Open(path_) || |
848 !meta_table_.Init( | 891 !meta_table_.Init( |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
913 base::AutoLock locked(lock_); | 956 base::AutoLock locked(lock_); |
914 pending_.swap(ops); | 957 pending_.swap(ops); |
915 num_pending_ = 0; | 958 num_pending_ = 0; |
916 } | 959 } |
917 | 960 |
918 // Maybe an old timer fired or we are already Close()'ed. | 961 // Maybe an old timer fired or we are already Close()'ed. |
919 if (!db_.get() || ops.empty()) | 962 if (!db_.get() || ops.empty()) |
920 return; | 963 return; |
921 | 964 |
922 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 965 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
923 "INSERT INTO cookies (creation_utc, host_key, name, value, path, " | 966 "INSERT INTO cookies (creation_utc, host_key, name, value, " |
924 "expires_utc, secure, httponly, last_access_utc, has_expires, " | 967 "encrypted_value, path, expires_utc, secure, httponly, last_access_utc, " |
925 "persistent, priority) " | 968 "has_expires, persistent, priority) " |
926 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)")); | 969 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
927 if (!add_smt.is_valid()) | 970 if (!add_smt.is_valid()) |
928 return; | 971 return; |
929 | 972 |
930 sql::Statement update_access_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 973 sql::Statement update_access_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
931 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?")); | 974 "UPDATE cookies SET last_access_utc=? WHERE creation_utc=?")); |
932 if (!update_access_smt.is_valid()) | 975 if (!update_access_smt.is_valid()) |
933 return; | 976 return; |
934 | 977 |
935 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 978 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
936 "DELETE FROM cookies WHERE creation_utc=?")); | 979 "DELETE FROM cookies WHERE creation_utc=?")); |
937 if (!del_smt.is_valid()) | 980 if (!del_smt.is_valid()) |
938 return; | 981 return; |
939 | 982 |
940 sql::Transaction transaction(db_.get()); | 983 sql::Transaction transaction(db_.get()); |
941 if (!transaction.Begin()) | 984 if (!transaction.Begin()) |
942 return; | 985 return; |
943 | 986 |
944 for (PendingOperationsList::iterator it = ops.begin(); | 987 for (PendingOperationsList::iterator it = ops.begin(); |
945 it != ops.end(); ++it) { | 988 it != ops.end(); ++it) { |
946 // Free the cookies as we commit them to the database. | 989 // Free the cookies as we commit them to the database. |
947 scoped_ptr<PendingOperation> po(*it); | 990 scoped_ptr<PendingOperation> po(*it); |
948 switch (po->op()) { | 991 switch (po->op()) { |
949 case PendingOperation::COOKIE_ADD: | 992 case PendingOperation::COOKIE_ADD: |
950 cookies_per_origin_[ | 993 cookies_per_origin_[ |
951 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; | 994 CookieOrigin(po->cc().Domain(), po->cc().IsSecure())]++; |
952 add_smt.Reset(true); | 995 add_smt.Reset(true); |
953 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 996 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
954 add_smt.BindString(1, po->cc().Domain()); | 997 add_smt.BindString(1, po->cc().Domain()); |
955 add_smt.BindString(2, po->cc().Name()); | 998 add_smt.BindString(2, po->cc().Name()); |
956 add_smt.BindString(3, po->cc().Value()); | 999 if (crypto_.get()) { |
957 add_smt.BindString(4, po->cc().Path()); | 1000 std::string encrypted_value; |
958 add_smt.BindInt64(5, po->cc().ExpiryDate().ToInternalValue()); | 1001 add_smt.BindCString(3, ""); // value |
959 add_smt.BindInt(6, po->cc().IsSecure()); | 1002 crypto_->EncryptString(po->cc().Value(), &encrypted_value); |
960 add_smt.BindInt(7, po->cc().IsHttpOnly()); | 1003 // BindBlob() immediately makes an internal copy of the data. |
961 add_smt.BindInt64(8, po->cc().LastAccessDate().ToInternalValue()); | 1004 add_smt.BindBlob(4, encrypted_value.data(), |
962 add_smt.BindInt(9, po->cc().IsPersistent()); | 1005 static_cast<int>(encrypted_value.length())); |
| 1006 } else { |
| 1007 add_smt.BindString(3, po->cc().Value()); |
| 1008 add_smt.BindBlob(4, "", 0); // encrypted_value |
| 1009 } |
| 1010 add_smt.BindString(5, po->cc().Path()); |
| 1011 add_smt.BindInt64(6, po->cc().ExpiryDate().ToInternalValue()); |
| 1012 add_smt.BindInt(7, po->cc().IsSecure()); |
| 1013 add_smt.BindInt(8, po->cc().IsHttpOnly()); |
| 1014 add_smt.BindInt64(9, po->cc().LastAccessDate().ToInternalValue()); |
963 add_smt.BindInt(10, po->cc().IsPersistent()); | 1015 add_smt.BindInt(10, po->cc().IsPersistent()); |
| 1016 add_smt.BindInt(11, po->cc().IsPersistent()); |
964 add_smt.BindInt( | 1017 add_smt.BindInt( |
965 11, CookiePriorityToDBCookiePriority(po->cc().Priority())); | 1018 12, CookiePriorityToDBCookiePriority(po->cc().Priority())); |
966 if (!add_smt.Run()) | 1019 if (!add_smt.Run()) |
967 NOTREACHED() << "Could not add a cookie to the DB."; | 1020 NOTREACHED() << "Could not add a cookie to the DB."; |
968 break; | 1021 break; |
969 | 1022 |
970 case PendingOperation::COOKIE_UPDATEACCESS: | 1023 case PendingOperation::COOKIE_UPDATEACCESS: |
971 update_access_smt.Reset(true); | 1024 update_access_smt.Reset(true); |
972 update_access_smt.BindInt64(0, | 1025 update_access_smt.BindInt64(0, |
973 po->cc().LastAccessDate().ToInternalValue()); | 1026 po->cc().LastAccessDate().ToInternalValue()); |
974 update_access_smt.BindInt64(1, | 1027 update_access_smt.BindInt64(1, |
975 po->cc().CreationDate().ToInternalValue()); | 1028 po->cc().CreationDate().ToInternalValue()); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 LOG(WARNING) << "Failed to post task from " << origin.ToString() | 1194 LOG(WARNING) << "Failed to post task from " << origin.ToString() |
1142 << " to client_task_runner_."; | 1195 << " to client_task_runner_."; |
1143 } | 1196 } |
1144 } | 1197 } |
1145 | 1198 |
1146 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1199 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
1147 const base::FilePath& path, | 1200 const base::FilePath& path, |
1148 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1201 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
1149 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, | 1202 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
1150 bool restore_old_session_cookies, | 1203 bool restore_old_session_cookies, |
1151 quota::SpecialStoragePolicy* special_storage_policy) | 1204 quota::SpecialStoragePolicy* special_storage_policy, |
| 1205 scoped_ptr<CookieCryptoDelegate> crypto_delegate) |
1152 : backend_(new Backend(path, | 1206 : backend_(new Backend(path, |
1153 client_task_runner, | 1207 client_task_runner, |
1154 background_task_runner, | 1208 background_task_runner, |
1155 restore_old_session_cookies, | 1209 restore_old_session_cookies, |
1156 special_storage_policy)) { | 1210 special_storage_policy, |
| 1211 crypto_delegate.Pass())) { |
1157 } | 1212 } |
1158 | 1213 |
1159 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1214 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
1160 backend_->Load(loaded_callback); | 1215 backend_->Load(loaded_callback); |
1161 } | 1216 } |
1162 | 1217 |
1163 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1218 void SQLitePersistentCookieStore::LoadCookiesForKey( |
1164 const std::string& key, | 1219 const std::string& key, |
1165 const LoadedCallback& loaded_callback) { | 1220 const LoadedCallback& loaded_callback) { |
1166 backend_->LoadCookiesForKey(key, loaded_callback); | 1221 backend_->LoadCookiesForKey(key, loaded_callback); |
(...skipping 25 matching lines...) Expand all Loading... |
1192 // We release our reference to the Backend, though it will probably still have | 1247 // We release our reference to the Backend, though it will probably still have |
1193 // a reference if the background runner has not run Close() yet. | 1248 // a reference if the background runner has not run Close() yet. |
1194 } | 1249 } |
1195 | 1250 |
1196 net::CookieStore* CreatePersistentCookieStore( | 1251 net::CookieStore* CreatePersistentCookieStore( |
1197 const base::FilePath& path, | 1252 const base::FilePath& path, |
1198 bool restore_old_session_cookies, | 1253 bool restore_old_session_cookies, |
1199 quota::SpecialStoragePolicy* storage_policy, | 1254 quota::SpecialStoragePolicy* storage_policy, |
1200 net::CookieMonster::Delegate* cookie_monster_delegate, | 1255 net::CookieMonster::Delegate* cookie_monster_delegate, |
1201 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, | 1256 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner, |
1202 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner) { | 1257 const scoped_refptr<base::SequencedTaskRunner>& background_task_runner, |
| 1258 scoped_ptr<CookieCryptoDelegate> crypto_delegate) { |
1203 SQLitePersistentCookieStore* persistent_store = | 1259 SQLitePersistentCookieStore* persistent_store = |
1204 new SQLitePersistentCookieStore( | 1260 new SQLitePersistentCookieStore( |
1205 path, | 1261 path, |
1206 client_task_runner, | 1262 client_task_runner, |
1207 background_task_runner, | 1263 background_task_runner, |
1208 restore_old_session_cookies, | 1264 restore_old_session_cookies, |
1209 storage_policy); | 1265 storage_policy, |
| 1266 crypto_delegate.Pass()); |
1210 return new net::CookieMonster(persistent_store, cookie_monster_delegate); | 1267 return new net::CookieMonster(persistent_store, cookie_monster_delegate); |
1211 } | 1268 } |
1212 | 1269 |
1213 net::CookieStore* CreatePersistentCookieStore( | 1270 net::CookieStore* CreatePersistentCookieStore( |
1214 const base::FilePath& path, | 1271 const base::FilePath& path, |
1215 bool restore_old_session_cookies, | 1272 bool restore_old_session_cookies, |
1216 quota::SpecialStoragePolicy* storage_policy, | 1273 quota::SpecialStoragePolicy* storage_policy, |
1217 net::CookieMonster::Delegate* cookie_monster_delegate) { | 1274 net::CookieMonster::Delegate* cookie_monster_delegate, |
| 1275 scoped_ptr<CookieCryptoDelegate> crypto_delegate) { |
1218 return CreatePersistentCookieStore( | 1276 return CreatePersistentCookieStore( |
1219 path, | 1277 path, |
1220 restore_old_session_cookies, | 1278 restore_old_session_cookies, |
1221 storage_policy, | 1279 storage_policy, |
1222 cookie_monster_delegate, | 1280 cookie_monster_delegate, |
1223 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | 1281 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), |
1224 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( | 1282 BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
1225 BrowserThread::GetBlockingPool()->GetSequenceToken())); | 1283 BrowserThread::GetBlockingPool()->GetSequenceToken()), |
| 1284 crypto_delegate.Pass()); |
1226 } | 1285 } |
1227 | 1286 |
1228 } // namespace content | 1287 } // namespace content |
OLD | NEW |