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 "net/extras/sqlite/sqlite_persistent_cookie_store.h" | 5 #include "net/extras/sqlite/sqlite_persistent_cookie_store.h" |
6 | 6 |
| 7 #include <iterator> |
7 #include <map> | 8 #include <map> |
8 #include <memory> | 9 #include <memory> |
9 #include <set> | 10 #include <set> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/callback.h" | 13 #include "base/callback.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
15 #include "base/location.h" | 16 #include "base/location.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 102 |
102 // Creates or loads the SQLite database. | 103 // Creates or loads the SQLite database. |
103 void Load(const LoadedCallback& loaded_callback); | 104 void Load(const LoadedCallback& loaded_callback); |
104 | 105 |
105 // Loads cookies for the domain key (eTLD+1). | 106 // Loads cookies for the domain key (eTLD+1). |
106 void LoadCookiesForKey(const std::string& domain, | 107 void LoadCookiesForKey(const std::string& domain, |
107 const LoadedCallback& loaded_callback); | 108 const LoadedCallback& loaded_callback); |
108 | 109 |
109 // Steps through all results of |smt|, makes a cookie from each, and adds the | 110 // Steps through all results of |smt|, makes a cookie from each, and adds the |
110 // cookie to |cookies|. This method also updates |num_cookies_read_|. | 111 // cookie to |cookies|. This method also updates |num_cookies_read_|. |
111 void MakeCookiesFromSQLStatement(std::vector<CanonicalCookie*>* cookies, | 112 void MakeCookiesFromSQLStatement( |
112 sql::Statement* statement); | 113 std::vector<std::unique_ptr<CanonicalCookie>>* cookies, |
| 114 sql::Statement* statement); |
113 | 115 |
114 // Batch a cookie addition. | 116 // Batch a cookie addition. |
115 void AddCookie(const CanonicalCookie& cc); | 117 void AddCookie(const CanonicalCookie& cc); |
116 | 118 |
117 // Batch a cookie access time update. | 119 // Batch a cookie access time update. |
118 void UpdateCookieAccessTime(const CanonicalCookie& cc); | 120 void UpdateCookieAccessTime(const CanonicalCookie& cc); |
119 | 121 |
120 // Batch a cookie deletion. | 122 // Batch a cookie deletion. |
121 void DeleteCookie(const CanonicalCookie& cc); | 123 void DeleteCookie(const CanonicalCookie& cc); |
122 | 124 |
123 // Commit pending operations as soon as possible. | 125 // Commit pending operations as soon as possible. |
124 void Flush(const base::Closure& callback); | 126 void Flush(const base::Closure& callback); |
125 | 127 |
126 // Commit any pending operations and close the database. This must be called | 128 // Commit any pending operations and close the database. This must be called |
127 // before the object is destructed. | 129 // before the object is destructed. |
128 void Close(const base::Closure& callback); | 130 void Close(const base::Closure& callback); |
129 | 131 |
130 // Post background delete of all cookies that match |cookies|. | 132 // Post background delete of all cookies that match |cookies|. |
131 void DeleteAllInList(const std::list<CookieOrigin>& cookies); | 133 void DeleteAllInList(const std::list<CookieOrigin>& cookies); |
132 | 134 |
133 private: | 135 private: |
134 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 136 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
135 | 137 |
136 // You should call Close() before destructing this object. | 138 // You should call Close() before destructing this object. |
137 ~Backend() { | 139 ~Backend() { |
138 DCHECK(!db_.get()) << "Close should have already been called."; | 140 DCHECK(!db_.get()) << "Close should have already been called."; |
139 DCHECK_EQ(0u, num_pending_); | 141 DCHECK_EQ(0u, num_pending_); |
140 DCHECK(pending_.empty()); | 142 DCHECK(pending_.empty()); |
141 | |
142 for (CanonicalCookie* cookie : cookies_) { | |
143 delete cookie; | |
144 } | |
145 } | 143 } |
146 | 144 |
147 // Database upgrade statements. | 145 // Database upgrade statements. |
148 bool EnsureDatabaseVersion(); | 146 bool EnsureDatabaseVersion(); |
149 | 147 |
150 class PendingOperation { | 148 class PendingOperation { |
151 public: | 149 public: |
152 enum OperationType { | 150 enum OperationType { |
153 COOKIE_ADD, | 151 COOKIE_ADD, |
154 COOKIE_UPDATEACCESS, | 152 COOKIE_UPDATEACCESS, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 | 245 |
248 typedef std::list<PendingOperation*> PendingOperationsList; | 246 typedef std::list<PendingOperation*> PendingOperationsList; |
249 PendingOperationsList pending_; | 247 PendingOperationsList pending_; |
250 PendingOperationsList::size_type num_pending_; | 248 PendingOperationsList::size_type num_pending_; |
251 // Guard |cookies_|, |pending_|, |num_pending_|. | 249 // Guard |cookies_|, |pending_|, |num_pending_|. |
252 base::Lock lock_; | 250 base::Lock lock_; |
253 | 251 |
254 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 252 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
255 // the number of messages sent to the client runner. Sent back in response to | 253 // the number of messages sent to the client runner. Sent back in response to |
256 // individual load requests for domain keys or when all loading completes. | 254 // individual load requests for domain keys or when all loading completes. |
257 // Ownership of the cookies in this vector is transferred to the client in | 255 std::vector<std::unique_ptr<CanonicalCookie>> cookies_; |
258 // response to individual load requests or when all loading completes. | |
259 std::vector<CanonicalCookie*> cookies_; | |
260 | 256 |
261 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 257 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
262 std::map<std::string, std::set<std::string>> keys_to_load_; | 258 std::map<std::string, std::set<std::string>> keys_to_load_; |
263 | 259 |
264 // Indicates if DB has been initialized. | 260 // Indicates if DB has been initialized. |
265 bool initialized_; | 261 bool initialized_; |
266 | 262 |
267 // Indicates if the kill-database callback has been scheduled. | 263 // Indicates if the kill-database callback has been scheduled. |
268 bool corruption_detected_; | 264 bool corruption_detected_; |
269 | 265 |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 | 609 |
614 if (load_success) | 610 if (load_success) |
615 ReportMetrics(); | 611 ReportMetrics(); |
616 } | 612 } |
617 | 613 |
618 void SQLitePersistentCookieStore::Backend::Notify( | 614 void SQLitePersistentCookieStore::Backend::Notify( |
619 const LoadedCallback& loaded_callback, | 615 const LoadedCallback& loaded_callback, |
620 bool load_success) { | 616 bool load_success) { |
621 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); | 617 DCHECK(client_task_runner_->RunsTasksOnCurrentThread()); |
622 | 618 |
623 std::vector<CanonicalCookie*> cookies; | 619 std::vector<std::unique_ptr<CanonicalCookie>> cookies; |
624 { | 620 { |
625 base::AutoLock locked(lock_); | 621 base::AutoLock locked(lock_); |
626 cookies.swap(cookies_); | 622 cookies.swap(cookies_); |
627 } | 623 } |
628 | 624 |
629 loaded_callback.Run(cookies); | 625 loaded_callback.Run(std::move(cookies)); |
630 } | 626 } |
631 | 627 |
632 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { | 628 bool SQLitePersistentCookieStore::Backend::InitializeDatabase() { |
633 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); | 629 DCHECK(background_task_runner_->RunsTasksOnCurrentThread()); |
634 | 630 |
635 if (initialized_ || corruption_detected_) { | 631 if (initialized_ || corruption_detected_) { |
636 // Return false if we were previously initialized but the DB has since been | 632 // Return false if we were previously initialized but the DB has since been |
637 // closed, or if corruption caused a database reset during initialization. | 633 // closed, or if corruption caused a database reset during initialization. |
638 return db_ != NULL; | 634 return db_ != NULL; |
639 } | 635 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " | 781 "has_expires, persistent, priority FROM cookies WHERE host_key = ? " |
786 "AND persistent = 1")); | 782 "AND persistent = 1")); |
787 } | 783 } |
788 if (!smt.is_valid()) { | 784 if (!smt.is_valid()) { |
789 smt.Clear(); // Disconnect smt_ref from db_. | 785 smt.Clear(); // Disconnect smt_ref from db_. |
790 meta_table_.Reset(); | 786 meta_table_.Reset(); |
791 db_.reset(); | 787 db_.reset(); |
792 return false; | 788 return false; |
793 } | 789 } |
794 | 790 |
795 std::vector<CanonicalCookie*> cookies; | 791 std::vector<std::unique_ptr<CanonicalCookie>> cookies; |
796 std::set<std::string>::const_iterator it = domains.begin(); | 792 std::set<std::string>::const_iterator it = domains.begin(); |
797 for (; it != domains.end(); ++it) { | 793 for (; it != domains.end(); ++it) { |
798 smt.BindString(0, *it); | 794 smt.BindString(0, *it); |
799 MakeCookiesFromSQLStatement(&cookies, &smt); | 795 MakeCookiesFromSQLStatement(&cookies, &smt); |
800 smt.Reset(true); | 796 smt.Reset(true); |
801 } | 797 } |
802 { | 798 { |
803 base::AutoLock locked(lock_); | 799 base::AutoLock locked(lock_); |
804 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); | 800 std::move(cookies.begin(), cookies.end(), std::back_inserter(cookies_)); |
805 } | 801 } |
806 return true; | 802 return true; |
807 } | 803 } |
808 | 804 |
809 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( | 805 void SQLitePersistentCookieStore::Backend::MakeCookiesFromSQLStatement( |
810 std::vector<CanonicalCookie*>* cookies, | 806 std::vector<std::unique_ptr<CanonicalCookie>>* cookies, |
811 sql::Statement* statement) { | 807 sql::Statement* statement) { |
812 sql::Statement& smt = *statement; | 808 sql::Statement& smt = *statement; |
813 while (smt.Step()) { | 809 while (smt.Step()) { |
814 std::string value; | 810 std::string value; |
815 std::string encrypted_value = smt.ColumnString(4); | 811 std::string encrypted_value = smt.ColumnString(4); |
816 if (!encrypted_value.empty() && crypto_) { | 812 if (!encrypted_value.empty() && crypto_) { |
817 if (!crypto_->DecryptString(encrypted_value, &value)) | 813 if (!crypto_->DecryptString(encrypted_value, &value)) |
818 continue; | 814 continue; |
819 } else { | 815 } else { |
820 value = smt.ColumnString(3); | 816 value = smt.ColumnString(3); |
821 } | 817 } |
822 std::unique_ptr<CanonicalCookie> cc(CanonicalCookie::Create( | 818 std::unique_ptr<CanonicalCookie> cc(CanonicalCookie::Create( |
823 smt.ColumnString(2), // name | 819 smt.ColumnString(2), // name |
824 value, // value | 820 value, // value |
825 smt.ColumnString(1), // domain | 821 smt.ColumnString(1), // domain |
826 smt.ColumnString(5), // path | 822 smt.ColumnString(5), // path |
827 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 823 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
828 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc | 824 Time::FromInternalValue(smt.ColumnInt64(6)), // expires_utc |
829 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc | 825 Time::FromInternalValue(smt.ColumnInt64(10)), // last_access_utc |
830 smt.ColumnInt(7) != 0, // secure | 826 smt.ColumnInt(7) != 0, // secure |
831 smt.ColumnInt(8) != 0, // http_only | 827 smt.ColumnInt(8) != 0, // http_only |
832 DBCookieSameSiteToCookieSameSite( | 828 DBCookieSameSiteToCookieSameSite( |
833 static_cast<DBCookieSameSite>(smt.ColumnInt(9))), // samesite | 829 static_cast<DBCookieSameSite>(smt.ColumnInt(9))), // samesite |
834 DBCookiePriorityToCookiePriority( | 830 DBCookiePriorityToCookiePriority( |
835 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority | 831 static_cast<DBCookiePriority>(smt.ColumnInt(13))))); // priority |
836 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) | 832 DLOG_IF(WARNING, cc->CreationDate() > Time::Now()) |
837 << L"CreationDate too recent"; | 833 << L"CreationDate too recent"; |
838 cookies->push_back(cc.release()); | 834 cookies->push_back(std::move(cc)); |
839 ++num_cookies_read_; | 835 ++num_cookies_read_; |
840 } | 836 } |
841 } | 837 } |
842 | 838 |
843 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 839 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
844 // Version check. | 840 // Version check. |
845 if (!meta_table_.Init(db_.get(), kCurrentVersionNumber, | 841 if (!meta_table_.Init(db_.get(), kCurrentVersionNumber, |
846 kCompatibleVersionNumber)) { | 842 kCompatibleVersionNumber)) { |
847 return false; | 843 return false; |
848 } | 844 } |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1395 // Backend::InternalBackgroundClose() yet. | 1391 // Backend::InternalBackgroundClose() yet. |
1396 backend_ = nullptr; | 1392 backend_ = nullptr; |
1397 } | 1393 } |
1398 } | 1394 } |
1399 | 1395 |
1400 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1396 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
1401 DCHECK(!loaded_callback.is_null()); | 1397 DCHECK(!loaded_callback.is_null()); |
1402 if (backend_) | 1398 if (backend_) |
1403 backend_->Load(loaded_callback); | 1399 backend_->Load(loaded_callback); |
1404 else | 1400 else |
1405 loaded_callback.Run(std::vector<CanonicalCookie*>()); | 1401 loaded_callback.Run(std::vector<std::unique_ptr<CanonicalCookie>>()); |
1406 } | 1402 } |
1407 | 1403 |
1408 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1404 void SQLitePersistentCookieStore::LoadCookiesForKey( |
1409 const std::string& key, | 1405 const std::string& key, |
1410 const LoadedCallback& loaded_callback) { | 1406 const LoadedCallback& loaded_callback) { |
1411 DCHECK(!loaded_callback.is_null()); | 1407 DCHECK(!loaded_callback.is_null()); |
1412 if (backend_) | 1408 if (backend_) |
1413 backend_->LoadCookiesForKey(key, loaded_callback); | 1409 backend_->LoadCookiesForKey(key, loaded_callback); |
1414 else | 1410 else |
1415 loaded_callback.Run(std::vector<CanonicalCookie*>()); | 1411 loaded_callback.Run(std::vector<std::unique_ptr<CanonicalCookie>>()); |
1416 } | 1412 } |
1417 | 1413 |
1418 void SQLitePersistentCookieStore::AddCookie(const CanonicalCookie& cc) { | 1414 void SQLitePersistentCookieStore::AddCookie(const CanonicalCookie& cc) { |
1419 if (backend_) | 1415 if (backend_) |
1420 backend_->AddCookie(cc); | 1416 backend_->AddCookie(cc); |
1421 } | 1417 } |
1422 | 1418 |
1423 void SQLitePersistentCookieStore::UpdateCookieAccessTime( | 1419 void SQLitePersistentCookieStore::UpdateCookieAccessTime( |
1424 const CanonicalCookie& cc) { | 1420 const CanonicalCookie& cc) { |
1425 if (backend_) | 1421 if (backend_) |
(...skipping 12 matching lines...) Expand all Loading... |
1438 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { | 1434 void SQLitePersistentCookieStore::Flush(const base::Closure& callback) { |
1439 if (backend_) | 1435 if (backend_) |
1440 backend_->Flush(callback); | 1436 backend_->Flush(callback); |
1441 } | 1437 } |
1442 | 1438 |
1443 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1439 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
1444 Close(base::Closure()); | 1440 Close(base::Closure()); |
1445 } | 1441 } |
1446 | 1442 |
1447 } // namespace net | 1443 } // namespace net |
OLD | NEW |