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