Chromium Code Reviews| 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 "chrome/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "chrome/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_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/metrics/histogram.h" | 20 #include "base/metrics/histogram.h" |
| 21 #include "base/string_util.h" | 21 #include "base/string_util.h" |
| 22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 23 #include "base/threading/thread.h" | 23 #include "base/threading/thread.h" |
| 24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
| 25 #include "base/time.h" | 25 #include "base/time.h" |
| 26 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 26 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| 27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/common/url_constants.h" | |
| 28 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
| 29 #include "net/base/registry_controlled_domain.h" | 30 #include "net/base/registry_controlled_domain.h" |
| 30 #include "sql/meta_table.h" | 31 #include "sql/meta_table.h" |
| 31 #include "sql/statement.h" | 32 #include "sql/statement.h" |
| 32 #include "sql/transaction.h" | 33 #include "sql/transaction.h" |
| 34 #include "webkit/quota/special_storage_policy.h" | |
| 33 | 35 |
| 34 using base::Time; | 36 using base::Time; |
| 35 using content::BrowserThread; | 37 using content::BrowserThread; |
| 36 | 38 |
| 37 // This class is designed to be shared between any calling threads and the | 39 // This class is designed to be shared between any calling threads and the |
| 38 // database thread. It batches operations and commits them on a timer. | 40 // database thread. It batches operations and commits them on a timer. |
| 39 // | 41 // |
| 40 // SQLitePersistentCookieStore::Load is called to load all cookies. It | 42 // SQLitePersistentCookieStore::Load is called to load all cookies. It |
| 41 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread | 43 // delegates to Backend::Load, which posts a Backend::LoadAndNotifyOnDBThread |
| 42 // task to the DB thread. This task calls Backend::ChainLoadCookies(), which | 44 // task to the DB thread. This task calls Backend::ChainLoadCookies(), which |
| 43 // repeatedly posts itself to the DB thread to load each eTLD+1's cookies in | 45 // repeatedly posts itself to the DB thread to load each eTLD+1's cookies in |
| 44 // separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is | 46 // separate tasks. When this is complete, Backend::CompleteLoadOnIOThread is |
| 45 // posted to the IO thread, which notifies the caller of | 47 // posted to the IO thread, which notifies the caller of |
| 46 // SQLitePersistentCookieStore::Load that the load is complete. | 48 // SQLitePersistentCookieStore::Load that the load is complete. |
| 47 // | 49 // |
| 48 // If a priority load request is invoked via SQLitePersistentCookieStore:: | 50 // If a priority load request is invoked via SQLitePersistentCookieStore:: |
| 49 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts | 51 // LoadCookiesForKey, it is delegated to Backend::LoadCookiesForKey, which posts |
| 50 // Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just | 52 // Backend::LoadKeyAndNotifyOnDBThread to the DB thread. That routine loads just |
| 51 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: | 53 // that single domain key (eTLD+1)'s cookies, and posts a Backend:: |
| 52 // CompleteLoadForKeyOnIOThread to the IO thread to notify the caller of | 54 // CompleteLoadForKeyOnIOThread to the IO thread to notify the caller of |
| 53 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. | 55 // SQLitePersistentCookieStore::LoadCookiesForKey that that load is complete. |
| 54 // | 56 // |
| 55 // Subsequent to loading, mutations may be queued by any thread using | 57 // Subsequent to loading, mutations may be queued by any thread using |
| 56 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to | 58 // AddCookie, UpdateCookieAccessTime, and DeleteCookie. These are flushed to |
| 57 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), | 59 // disk on the DB thread every 30 seconds, 512 operations, or call to Flush(), |
| 58 // whichever occurs first. | 60 // whichever occurs first. |
| 59 class SQLitePersistentCookieStore::Backend | 61 class SQLitePersistentCookieStore::Backend |
| 60 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { | 62 : public base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend> { |
| 61 public: | 63 public: |
| 62 Backend(const FilePath& path, bool restore_old_session_cookies) | 64 Backend(const FilePath& path, |
| 65 bool restore_old_session_cookies, | |
| 66 quota::SpecialStoragePolicy* special_storage_policy) | |
| 63 : path_(path), | 67 : path_(path), |
| 64 db_(NULL), | 68 db_(NULL), |
| 65 num_pending_(0), | 69 num_pending_(0), |
| 66 clear_local_state_on_exit_(false), | 70 clear_local_state_on_exit_(false), |
| 67 initialized_(false), | 71 initialized_(false), |
| 68 restore_old_session_cookies_(restore_old_session_cookies), | 72 restore_old_session_cookies_(restore_old_session_cookies), |
| 73 special_storage_policy_(special_storage_policy), | |
| 69 num_cookies_read_(0), | 74 num_cookies_read_(0), |
| 70 num_priority_waiting_(0), | 75 num_priority_waiting_(0), |
| 71 total_priority_requests_(0) { | 76 total_priority_requests_(0) { |
| 72 } | 77 } |
| 73 | 78 |
| 74 // Creates or loads the SQLite database. | 79 // Creates or loads the SQLite database. |
| 75 void Load(const LoadedCallback& loaded_callback); | 80 void Load(const LoadedCallback& loaded_callback); |
| 76 | 81 |
| 77 // Loads cookies for the domain key (eTLD+1). | 82 // Loads cookies for the domain key (eTLD+1). |
| 78 void LoadCookiesForKey(const std::string& domain, | 83 void LoadCookiesForKey(const std::string& domain, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 bool LoadCookiesForDomains(const std::set<std::string>& key); | 179 bool LoadCookiesForDomains(const std::set<std::string>& key); |
| 175 | 180 |
| 176 // Batch a cookie operation (add or delete) | 181 // Batch a cookie operation (add or delete) |
| 177 void BatchOperation(PendingOperation::OperationType op, | 182 void BatchOperation(PendingOperation::OperationType op, |
| 178 const net::CookieMonster::CanonicalCookie& cc); | 183 const net::CookieMonster::CanonicalCookie& cc); |
| 179 // Commit our pending operations to the database. | 184 // Commit our pending operations to the database. |
| 180 void Commit(); | 185 void Commit(); |
| 181 // Close() executed on the background thread. | 186 // Close() executed on the background thread. |
| 182 void InternalBackgroundClose(); | 187 void InternalBackgroundClose(); |
| 183 | 188 |
| 184 void DeleteSessionCookies(); | 189 void DeleteSessionCookiesOnStartup(); |
| 190 | |
| 191 void DeleteSessionCookiesOnShutdown(); | |
| 185 | 192 |
| 186 FilePath path_; | 193 FilePath path_; |
| 187 scoped_ptr<sql::Connection> db_; | 194 scoped_ptr<sql::Connection> db_; |
| 188 sql::MetaTable meta_table_; | 195 sql::MetaTable meta_table_; |
| 189 | 196 |
| 190 typedef std::list<PendingOperation*> PendingOperationsList; | 197 typedef std::list<PendingOperation*> PendingOperationsList; |
| 191 PendingOperationsList pending_; | 198 PendingOperationsList pending_; |
| 192 PendingOperationsList::size_type num_pending_; | 199 PendingOperationsList::size_type num_pending_; |
| 193 // True if the persistent store should be deleted upon destruction. | 200 // True if the persistent store should be deleted upon destruction. |
| 194 bool clear_local_state_on_exit_; | 201 bool clear_local_state_on_exit_; |
| 195 // Guard |cookies_|, |pending_|, |num_pending_|, |clear_local_state_on_exit_| | 202 // Guard |cookies_|, |pending_|, |num_pending_|, |clear_local_state_on_exit_|, |
| 203 // and |cookies_per_domain_|. | |
| 196 base::Lock lock_; | 204 base::Lock lock_; |
| 197 | 205 |
| 198 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce | 206 // Temporary buffer for cookies loaded from DB. Accumulates cookies to reduce |
| 199 // the number of messages sent to the IO thread. Sent back in response to | 207 // the number of messages sent to the IO thread. Sent back in response to |
| 200 // individual load requests for domain keys or when all loading completes. | 208 // individual load requests for domain keys or when all loading completes. |
| 201 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 209 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| 202 | 210 |
| 203 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. | 211 // Map of domain keys(eTLD+1) to domains/hosts that are to be loaded from DB. |
| 204 std::map<std::string, std::set<std::string> > keys_to_load_; | 212 std::map<std::string, std::set<std::string> > keys_to_load_; |
| 205 | 213 |
| 214 // Map of domain keys(eTLD+1) to number of cookies in the database. | |
| 215 std::map<std::string, int> cookies_per_domain_; | |
| 216 | |
| 206 // Indicates if DB has been initialized. | 217 // Indicates if DB has been initialized. |
| 207 bool initialized_; | 218 bool initialized_; |
| 208 | 219 |
| 209 // If false, we should filter out session cookies when reading the DB. | 220 // If false, we should filter out session cookies when reading the DB. |
| 210 bool restore_old_session_cookies_; | 221 bool restore_old_session_cookies_; |
| 211 | 222 |
| 223 // Storage Policy defining what data is deleted on shutdown. | |
| 224 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
| 225 | |
| 212 // The cumulative time spent loading the cookies on the DB thread. Incremented | 226 // The cumulative time spent loading the cookies on the DB thread. Incremented |
| 213 // and reported from the DB thread. | 227 // and reported from the DB thread. |
| 214 base::TimeDelta cookie_load_duration_; | 228 base::TimeDelta cookie_load_duration_; |
| 215 | 229 |
| 216 // The total number of cookies read. Incremented and reported on the DB | 230 // The total number of cookies read. Incremented and reported on the DB |
| 217 // thread. | 231 // thread. |
| 218 int num_cookies_read_; | 232 int num_cookies_read_; |
| 219 | 233 |
| 220 // Guards the following metrics-related properties (only accessed when | 234 // Guards the following metrics-related properties (only accessed when |
| 221 // starting/completing priority loads or completing the total load). | 235 // starting/completing priority loads or completing the total load). |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 558 if (load_success && keys_to_load_.size() > 0) { | 572 if (load_success && keys_to_load_.size() > 0) { |
| 559 BrowserThread::PostTask( | 573 BrowserThread::PostTask( |
| 560 BrowserThread::DB, FROM_HERE, | 574 BrowserThread::DB, FROM_HERE, |
| 561 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); | 575 base::Bind(&Backend::ChainLoadCookies, this, loaded_callback)); |
| 562 } else { | 576 } else { |
| 563 BrowserThread::PostTask( | 577 BrowserThread::PostTask( |
| 564 BrowserThread::IO, FROM_HERE, | 578 BrowserThread::IO, FROM_HERE, |
| 565 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, | 579 base::Bind(&SQLitePersistentCookieStore::Backend::CompleteLoadOnIOThread, |
| 566 this, loaded_callback, load_success)); | 580 this, loaded_callback, load_success)); |
| 567 if (load_success && !restore_old_session_cookies_) | 581 if (load_success && !restore_old_session_cookies_) |
| 568 DeleteSessionCookies(); | 582 DeleteSessionCookiesOnStartup(); |
| 569 } | 583 } |
| 570 } | 584 } |
| 571 | 585 |
| 572 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( | 586 bool SQLitePersistentCookieStore::Backend::LoadCookiesForDomains( |
| 573 const std::set<std::string>& domains) { | 587 const std::set<std::string>& domains) { |
| 574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 575 | 589 |
| 576 sql::Statement smt; | 590 sql::Statement smt; |
| 577 if (restore_old_session_cookies_) { | 591 if (restore_old_session_cookies_) { |
| 578 smt.Assign(db_->GetCachedStatement( | 592 smt.Assign(db_->GetCachedStatement( |
| 579 SQL_FROM_HERE, | 593 SQL_FROM_HERE, |
| 580 "SELECT creation_utc, host_key, name, value, path, expires_utc, " | 594 "SELECT creation_utc, host_key, name, value, path, expires_utc, " |
| 581 "secure, httponly, last_access_utc, has_expires, persistent " | 595 "secure, httponly, last_access_utc, has_expires, persistent " |
| 582 "FROM cookies WHERE host_key = ?")); | 596 "FROM cookies WHERE host_key = ?")); |
| 583 } else { | 597 } else { |
| 584 smt.Assign(db_->GetCachedStatement( | 598 smt.Assign(db_->GetCachedStatement( |
| 585 SQL_FROM_HERE, | 599 SQL_FROM_HERE, |
| 586 "SELECT creation_utc, host_key, name, value, path, expires_utc, " | 600 "SELECT creation_utc, host_key, name, value, path, expires_utc, " |
| 587 "secure, httponly, last_access_utc, has_expires, persistent " | 601 "secure, httponly, last_access_utc, has_expires, persistent " |
| 588 "FROM cookies WHERE host_key = ? AND persistent = 1")); | 602 "FROM cookies WHERE host_key = ? AND persistent = 1")); |
| 589 } | 603 } |
| 590 if (!smt.is_valid()) { | 604 if (!smt.is_valid()) { |
| 591 smt.Clear(); // Disconnect smt_ref from db_. | 605 smt.Clear(); // Disconnect smt_ref from db_. |
| 592 db_.reset(); | 606 db_.reset(); |
| 593 return false; | 607 return false; |
| 594 } | 608 } |
| 595 | 609 |
| 596 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 610 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 597 std::set<std::string>::const_iterator it = domains.begin(); | 611 std::set<std::string>::const_iterator it = domains.begin(); |
| 612 std::map<std::string, int> cookies_per_domain; | |
| 598 for (; it != domains.end(); ++it) { | 613 for (; it != domains.end(); ++it) { |
| 599 smt.BindString(0, *it); | 614 smt.BindString(0, *it); |
| 600 while (smt.Step()) { | 615 while (smt.Step()) { |
| 616 cookies_per_domain[*it]++; | |
| 601 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( | 617 scoped_ptr<net::CookieMonster::CanonicalCookie> cc( |
| 602 new net::CookieMonster::CanonicalCookie( | 618 new net::CookieMonster::CanonicalCookie( |
| 603 // The "source" URL is not used with persisted cookies. | 619 // The "source" URL is not used with persisted cookies. |
| 604 GURL(), // Source | 620 GURL(), // Source |
| 605 smt.ColumnString(2), // name | 621 smt.ColumnString(2), // name |
| 606 smt.ColumnString(3), // value | 622 smt.ColumnString(3), // value |
| 607 smt.ColumnString(1), // domain | 623 smt.ColumnString(1), // domain |
| 608 smt.ColumnString(4), // path | 624 smt.ColumnString(4), // path |
| 609 std::string(), // TODO(abarth): Persist mac_key | 625 std::string(), // TODO(abarth): Persist mac_key |
| 610 std::string(), // TODO(abarth): Persist mac_algorithm | 626 std::string(), // TODO(abarth): Persist mac_algorithm |
| 611 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc | 627 Time::FromInternalValue(smt.ColumnInt64(0)), // creation_utc |
| 612 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc | 628 Time::FromInternalValue(smt.ColumnInt64(5)), // expires_utc |
| 613 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc | 629 Time::FromInternalValue(smt.ColumnInt64(8)), // last_access_utc |
| 614 smt.ColumnInt(6) != 0, // secure | 630 smt.ColumnInt(6) != 0, // secure |
| 615 smt.ColumnInt(7) != 0, // httponly | 631 smt.ColumnInt(7) != 0, // httponly |
| 616 smt.ColumnInt(9) != 0, // has_expires | 632 smt.ColumnInt(9) != 0, // has_expires |
| 617 smt.ColumnInt(10) != 0)); // is_persistent | 633 smt.ColumnInt(10) != 0)); // is_persistent |
| 618 DLOG_IF(WARNING, | 634 DLOG_IF(WARNING, |
| 619 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; | 635 cc->CreationDate() > Time::Now()) << L"CreationDate too recent"; |
| 620 cookies.push_back(cc.release()); | 636 cookies.push_back(cc.release()); |
| 621 ++num_cookies_read_; | 637 ++num_cookies_read_; |
| 622 } | 638 } |
| 623 smt.Reset(true); | 639 smt.Reset(true); |
| 624 } | 640 } |
| 625 { | 641 { |
| 626 base::AutoLock locked(lock_); | 642 base::AutoLock locked(lock_); |
| 627 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); | 643 cookies_.insert(cookies_.end(), cookies.begin(), cookies.end()); |
| 644 std::map<std::string, int>::iterator domain; | |
| 645 for (domain = cookies_per_domain.begin(); | |
| 646 domain != cookies_per_domain.end(); ++domain) { | |
| 647 cookies_per_domain_[domain->first] += domain->second; | |
| 648 } | |
| 628 } | 649 } |
| 629 return true; | 650 return true; |
| 630 } | 651 } |
| 631 | 652 |
| 632 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { | 653 bool SQLitePersistentCookieStore::Backend::EnsureDatabaseVersion() { |
| 633 // Version check. | 654 // Version check. |
| 634 if (!meta_table_.Init( | 655 if (!meta_table_.Init( |
| 635 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { | 656 db_.get(), kCurrentVersionNumber, kCompatibleVersionNumber)) { |
| 636 return false; | 657 return false; |
| 637 } | 658 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 | 834 |
| 814 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 835 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
| 815 "DELETE FROM cookies WHERE creation_utc=?")); | 836 "DELETE FROM cookies WHERE creation_utc=?")); |
| 816 if (!del_smt.is_valid()) | 837 if (!del_smt.is_valid()) |
| 817 return; | 838 return; |
| 818 | 839 |
| 819 sql::Transaction transaction(db_.get()); | 840 sql::Transaction transaction(db_.get()); |
| 820 if (!transaction.Begin()) | 841 if (!transaction.Begin()) |
| 821 return; | 842 return; |
| 822 | 843 |
| 844 std::map<std::string, int> cookies_per_domain; | |
| 823 for (PendingOperationsList::iterator it = ops.begin(); | 845 for (PendingOperationsList::iterator it = ops.begin(); |
| 824 it != ops.end(); ++it) { | 846 it != ops.end(); ++it) { |
| 825 // Free the cookies as we commit them to the database. | 847 // Free the cookies as we commit them to the database. |
| 826 scoped_ptr<PendingOperation> po(*it); | 848 scoped_ptr<PendingOperation> po(*it); |
| 827 switch (po->op()) { | 849 switch (po->op()) { |
| 828 case PendingOperation::COOKIE_ADD: | 850 case PendingOperation::COOKIE_ADD: |
| 851 cookies_per_domain[po->cc().Domain()]++; | |
| 829 add_smt.Reset(true); | 852 add_smt.Reset(true); |
| 830 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 853 add_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
| 831 add_smt.BindString(1, po->cc().Domain()); | 854 add_smt.BindString(1, po->cc().Domain()); |
| 832 add_smt.BindString(2, po->cc().Name()); | 855 add_smt.BindString(2, po->cc().Name()); |
| 833 add_smt.BindString(3, po->cc().Value()); | 856 add_smt.BindString(3, po->cc().Value()); |
| 834 add_smt.BindString(4, po->cc().Path()); | 857 add_smt.BindString(4, po->cc().Path()); |
| 835 add_smt.BindInt64(5, po->cc().ExpiryDate().ToInternalValue()); | 858 add_smt.BindInt64(5, po->cc().ExpiryDate().ToInternalValue()); |
| 836 add_smt.BindInt(6, po->cc().IsSecure()); | 859 add_smt.BindInt(6, po->cc().IsSecure()); |
| 837 add_smt.BindInt(7, po->cc().IsHttpOnly()); | 860 add_smt.BindInt(7, po->cc().IsHttpOnly()); |
| 838 add_smt.BindInt64(8, po->cc().LastAccessDate().ToInternalValue()); | 861 add_smt.BindInt64(8, po->cc().LastAccessDate().ToInternalValue()); |
| 839 add_smt.BindInt(9, po->cc().DoesExpire()); | 862 add_smt.BindInt(9, po->cc().DoesExpire()); |
| 840 add_smt.BindInt(10, po->cc().IsPersistent()); | 863 add_smt.BindInt(10, po->cc().IsPersistent()); |
| 841 if (!add_smt.Run()) | 864 if (!add_smt.Run()) |
| 842 NOTREACHED() << "Could not add a cookie to the DB."; | 865 NOTREACHED() << "Could not add a cookie to the DB."; |
| 843 break; | 866 break; |
| 844 | 867 |
| 845 case PendingOperation::COOKIE_UPDATEACCESS: | 868 case PendingOperation::COOKIE_UPDATEACCESS: |
| 846 update_access_smt.Reset(true); | 869 update_access_smt.Reset(true); |
| 847 update_access_smt.BindInt64(0, | 870 update_access_smt.BindInt64(0, |
| 848 po->cc().LastAccessDate().ToInternalValue()); | 871 po->cc().LastAccessDate().ToInternalValue()); |
| 849 update_access_smt.BindInt64(1, | 872 update_access_smt.BindInt64(1, |
| 850 po->cc().CreationDate().ToInternalValue()); | 873 po->cc().CreationDate().ToInternalValue()); |
| 851 if (!update_access_smt.Run()) | 874 if (!update_access_smt.Run()) |
| 852 NOTREACHED() << "Could not update cookie last access time in the DB."; | 875 NOTREACHED() << "Could not update cookie last access time in the DB."; |
| 853 break; | 876 break; |
| 854 | 877 |
| 855 case PendingOperation::COOKIE_DELETE: | 878 case PendingOperation::COOKIE_DELETE: |
| 879 cookies_per_domain[po->cc().Domain()]--; | |
| 856 del_smt.Reset(true); | 880 del_smt.Reset(true); |
| 857 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); | 881 del_smt.BindInt64(0, po->cc().CreationDate().ToInternalValue()); |
| 858 if (!del_smt.Run()) | 882 if (!del_smt.Run()) |
| 859 NOTREACHED() << "Could not delete a cookie from the DB."; | 883 NOTREACHED() << "Could not delete a cookie from the DB."; |
| 860 break; | 884 break; |
| 861 | 885 |
| 862 default: | 886 default: |
| 863 NOTREACHED(); | 887 NOTREACHED(); |
| 864 break; | 888 break; |
| 865 } | 889 } |
| 866 } | 890 } |
| 867 bool succeeded = transaction.Commit(); | 891 bool succeeded = transaction.Commit(); |
| 868 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults", | 892 UMA_HISTOGRAM_ENUMERATION("Cookie.BackingStoreUpdateResults", |
| 869 succeeded ? 0 : 1, 2); | 893 succeeded ? 0 : 1, 2); |
| 894 if (succeeded) { | |
| 895 base::AutoLock locked(lock_); | |
| 896 std::map<std::string, int>::iterator domain; | |
| 897 for (domain = cookies_per_domain.begin(); | |
| 898 domain != cookies_per_domain.end(); ++domain) { | |
| 899 cookies_per_domain_[domain->first] += domain->second; | |
| 900 } | |
| 901 } | |
| 870 } | 902 } |
| 871 | 903 |
| 872 void SQLitePersistentCookieStore::Backend::Flush( | 904 void SQLitePersistentCookieStore::Backend::Flush( |
| 873 const base::Closure& callback) { | 905 const base::Closure& callback) { |
| 874 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); | 906 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 875 BrowserThread::PostTask( | 907 BrowserThread::PostTask( |
| 876 BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this)); | 908 BrowserThread::DB, FROM_HERE, base::Bind(&Backend::Commit, this)); |
| 877 if (!callback.is_null()) { | 909 if (!callback.is_null()) { |
| 878 // We want the completion task to run immediately after Commit() returns. | 910 // We want the completion task to run immediately after Commit() returns. |
| 879 // Posting it from here means there is less chance of another task getting | 911 // Posting it from here means there is less chance of another task getting |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 894 BrowserThread::DB, FROM_HERE, | 926 BrowserThread::DB, FROM_HERE, |
| 895 base::Bind(&Backend::InternalBackgroundClose, this)); | 927 base::Bind(&Backend::InternalBackgroundClose, this)); |
| 896 } | 928 } |
| 897 } | 929 } |
| 898 | 930 |
| 899 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { | 931 void SQLitePersistentCookieStore::Backend::InternalBackgroundClose() { |
| 900 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 932 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 901 // Commit any pending operations | 933 // Commit any pending operations |
| 902 Commit(); | 934 Commit(); |
| 903 | 935 |
| 936 if (!clear_local_state_on_exit_ && special_storage_policy_.get() && | |
| 937 special_storage_policy_->HasSessionOnlyOrigins()) { | |
| 938 DeleteSessionCookiesOnShutdown(); | |
| 939 } | |
| 940 | |
| 904 db_.reset(); | 941 db_.reset(); |
| 905 | 942 |
| 906 if (clear_local_state_on_exit_) | 943 if (clear_local_state_on_exit_) |
| 907 file_util::Delete(path_, false); | 944 file_util::Delete(path_, false); |
| 908 } | 945 } |
| 909 | 946 |
| 947 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnShutdown() { | |
| 948 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 949 | |
| 950 | |
| 951 sql::Statement del_smt(db_->GetCachedStatement(SQL_FROM_HERE, | |
| 952 "DELETE FROM cookies WHERE host_key=?")); | |
| 953 if (!del_smt.is_valid()) { | |
| 954 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 955 return; | |
| 956 } | |
| 957 | |
| 958 sql::Transaction transaction(db_.get()); | |
| 959 if (!transaction.Begin()) { | |
| 960 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 961 return; | |
| 962 } | |
| 963 | |
| 964 base::AutoLock locked(lock_); | |
| 965 std::map<std::string, int>::iterator domain; | |
| 966 for (domain = cookies_per_domain_.begin(); | |
| 967 domain != cookies_per_domain_.end(); ++domain) { | |
| 968 if (domain->second <= 0) { | |
| 969 DCHECK_EQ(0, domain->second); | |
| 970 continue; | |
| 971 } | |
| 972 | |
| 973 GURL url( | |
|
jochen (gone - plz use gerrit)
2012/05/24 11:04:03
compare to CookiesTreeModel::PopulateCookieInfoWit
erikwright (departed)
2012/05/24 17:36:59
Presumably this should be matching whoever defines
| |
| 974 std::string(chrome::kHttpScheme) + | |
| 975 content::kStandardSchemeSeparator + | |
| 976 (domain->first[0] == '.' ? domain->first.substr(1) : domain->first) + | |
| 977 "/"); | |
| 978 if (!special_storage_policy_->IsStorageSessionOnly(url)) | |
| 979 continue; | |
| 980 if (special_storage_policy_->IsStorageProtected(url)) | |
| 981 continue; | |
| 982 | |
| 983 del_smt.Reset(true); | |
| 984 del_smt.BindString(0, domain->first); | |
| 985 if (!del_smt.Run()) | |
| 986 NOTREACHED() << "Could not delete a cookie from the DB."; | |
| 987 } | |
| 988 | |
| 989 if (!transaction.Commit()) | |
| 990 LOG(WARNING) << "Unable to delete cookies on shutdown."; | |
| 991 } | |
| 992 | |
| 910 void SQLitePersistentCookieStore::Backend::SetClearLocalStateOnExit( | 993 void SQLitePersistentCookieStore::Backend::SetClearLocalStateOnExit( |
| 911 bool clear_local_state) { | 994 bool clear_local_state) { |
| 912 base::AutoLock locked(lock_); | 995 base::AutoLock locked(lock_); |
| 913 clear_local_state_on_exit_ = clear_local_state; | 996 clear_local_state_on_exit_ = clear_local_state; |
| 914 } | 997 } |
| 915 | 998 |
| 916 void SQLitePersistentCookieStore::Backend::DeleteSessionCookies() { | 999 void SQLitePersistentCookieStore::Backend::DeleteSessionCookiesOnStartup() { |
| 917 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 1000 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 918 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) | 1001 if (!db_->Execute("DELETE FROM cookies WHERE persistent == 0")) |
| 919 LOG(WARNING) << "Unable to delete session cookies."; | 1002 LOG(WARNING) << "Unable to delete session cookies."; |
| 920 } | 1003 } |
| 921 | 1004 |
| 922 SQLitePersistentCookieStore::SQLitePersistentCookieStore( | 1005 SQLitePersistentCookieStore::SQLitePersistentCookieStore( |
| 923 const FilePath& path, | 1006 const FilePath& path, |
| 924 bool restore_old_session_cookies) | 1007 bool restore_old_session_cookies, |
| 925 : backend_(new Backend(path, restore_old_session_cookies)) { | 1008 quota::SpecialStoragePolicy* storage_policy) |
| 1009 : backend_(new Backend(path, restore_old_session_cookies, storage_policy)) { | |
| 926 } | 1010 } |
| 927 | 1011 |
| 928 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { | 1012 void SQLitePersistentCookieStore::Load(const LoadedCallback& loaded_callback) { |
| 929 backend_->Load(loaded_callback); | 1013 backend_->Load(loaded_callback); |
| 930 } | 1014 } |
| 931 | 1015 |
| 932 void SQLitePersistentCookieStore::LoadCookiesForKey( | 1016 void SQLitePersistentCookieStore::LoadCookiesForKey( |
| 933 const std::string& key, | 1017 const std::string& key, |
| 934 const LoadedCallback& loaded_callback) { | 1018 const LoadedCallback& loaded_callback) { |
| 935 backend_->LoadCookiesForKey(key, loaded_callback); | 1019 backend_->LoadCookiesForKey(key, loaded_callback); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 967 } | 1051 } |
| 968 | 1052 |
| 969 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1053 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 970 if (backend_.get()) { | 1054 if (backend_.get()) { |
| 971 backend_->Close(); | 1055 backend_->Close(); |
| 972 // Release our reference, it will probably still have a reference if the | 1056 // Release our reference, it will probably still have a reference if the |
| 973 // background thread has not run Close() yet. | 1057 // background thread has not run Close() yet. |
| 974 backend_ = NULL; | 1058 backend_ = NULL; |
| 975 } | 1059 } |
| 976 } | 1060 } |
| OLD | NEW |