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> |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 : path_(path), | 68 : path_(path), |
69 db_(NULL), | 69 db_(NULL), |
70 num_pending_(0), | 70 num_pending_(0), |
71 force_keep_session_state_(false), | 71 force_keep_session_state_(false), |
72 initialized_(false), | 72 initialized_(false), |
73 restore_old_session_cookies_(restore_old_session_cookies), | 73 restore_old_session_cookies_(restore_old_session_cookies), |
74 clear_on_exit_policy_(clear_on_exit_policy), | 74 clear_on_exit_policy_(clear_on_exit_policy), |
75 num_cookies_read_(0), | 75 num_cookies_read_(0), |
76 num_priority_waiting_(0), | 76 num_priority_waiting_(0), |
77 total_priority_requests_(0) { | 77 total_priority_requests_(0) { |
78 error_delegate_ = | |
79 new KillDatabaseErrorDelegate(this, GetErrorHandlerForCookieDb()); | |
80 } | 78 } |
81 | 79 |
82 // Creates or loads the SQLite database. | 80 // Creates or loads the SQLite database. |
83 void Load(const LoadedCallback& loaded_callback); | 81 void Load(const LoadedCallback& loaded_callback); |
84 | 82 |
85 // Loads cookies for the domain key (eTLD+1). | 83 // Loads cookies for the domain key (eTLD+1). |
86 void LoadCookiesForKey(const std::string& domain, | 84 void LoadCookiesForKey(const std::string& domain, |
87 const LoadedCallback& loaded_callback); | 85 const LoadedCallback& loaded_callback); |
88 | 86 |
89 // Batch a cookie addition. | 87 // Batch a cookie addition. |
(...skipping 14 matching lines...) Expand all Loading... |
104 | 102 |
105 void SetForceKeepSessionState(); | 103 void SetForceKeepSessionState(); |
106 | 104 |
107 private: | 105 private: |
108 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 106 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
109 | 107 |
110 class KillDatabaseErrorDelegate : public sql::ErrorDelegate { | 108 class KillDatabaseErrorDelegate : public sql::ErrorDelegate { |
111 public: | 109 public: |
112 KillDatabaseErrorDelegate(Backend* backend, | 110 KillDatabaseErrorDelegate(Backend* backend, |
113 sql::ErrorDelegate* wrapped_delegate); | 111 sql::ErrorDelegate* wrapped_delegate); |
| 112 |
| 113 virtual ~KillDatabaseErrorDelegate() {} |
| 114 |
114 // ErrorDelegate implementation. | 115 // ErrorDelegate implementation. |
115 virtual int OnError(int error, | 116 virtual int OnError(int error, |
116 sql::Connection* connection, | 117 sql::Connection* connection, |
117 sql::Statement* stmt) OVERRIDE; | 118 sql::Statement* stmt) OVERRIDE; |
118 | 119 |
119 void reset_backend() { | |
120 backend_ = NULL; | |
121 } | |
122 | |
123 protected: | |
124 virtual ~KillDatabaseErrorDelegate() {} | |
125 | |
126 private: | 120 private: |
127 | 121 |
128 // Do not increment the count on Backend, as that would create a circular | 122 // Do not increment the count on Backend, as that would create a circular |
129 // reference (Backend -> Connection -> ErrorDelegate -> Backend). Instead, | 123 // reference (Backend -> Connection -> ErrorDelegate -> Backend). |
130 // Backend will call reset_backend() when it is going away. | |
131 Backend* backend_; | 124 Backend* backend_; |
132 scoped_refptr<sql::ErrorDelegate> wrapped_delegate_; | 125 scoped_ptr<sql::ErrorDelegate> wrapped_delegate_; |
133 | 126 |
134 DISALLOW_COPY_AND_ASSIGN(KillDatabaseErrorDelegate); | 127 DISALLOW_COPY_AND_ASSIGN(KillDatabaseErrorDelegate); |
135 }; | 128 }; |
136 | 129 |
137 // You should call Close() before destructing this object. | 130 // You should call Close() before destructing this object. |
138 ~Backend() { | 131 ~Backend() { |
139 if (error_delegate_.get()) { | |
140 error_delegate_->reset_backend(); | |
141 error_delegate_ = NULL; | |
142 } | |
143 DCHECK(!db_.get()) << "Close should have already been called."; | 132 DCHECK(!db_.get()) << "Close should have already been called."; |
144 DCHECK(num_pending_ == 0 && pending_.empty()); | 133 DCHECK(num_pending_ == 0 && pending_.empty()); |
145 } | 134 } |
146 | 135 |
147 // Database upgrade statements. | 136 // Database upgrade statements. |
148 bool EnsureDatabaseVersion(); | 137 bool EnsureDatabaseVersion(); |
149 | 138 |
150 class PendingOperation { | 139 class PendingOperation { |
151 public: | 140 public: |
152 typedef enum { | 141 typedef enum { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 void InternalBackgroundClose(); | 209 void InternalBackgroundClose(); |
221 | 210 |
222 void DeleteSessionCookiesOnStartup(); | 211 void DeleteSessionCookiesOnStartup(); |
223 | 212 |
224 void DeleteSessionCookiesOnShutdown(); | 213 void DeleteSessionCookiesOnShutdown(); |
225 | 214 |
226 void KillDatabase(); | 215 void KillDatabase(); |
227 | 216 |
228 FilePath path_; | 217 FilePath path_; |
229 scoped_ptr<sql::Connection> db_; | 218 scoped_ptr<sql::Connection> db_; |
230 scoped_refptr<KillDatabaseErrorDelegate> error_delegate_; | |
231 sql::MetaTable meta_table_; | 219 sql::MetaTable meta_table_; |
232 | 220 |
233 typedef std::list<PendingOperation*> PendingOperationsList; | 221 typedef std::list<PendingOperation*> PendingOperationsList; |
234 PendingOperationsList pending_; | 222 PendingOperationsList pending_; |
235 PendingOperationsList::size_type num_pending_; | 223 PendingOperationsList::size_type num_pending_; |
236 // True if the persistent store should skip delete on exit rules. | 224 // True if the persistent store should skip delete on exit rules. |
237 bool force_keep_session_state_; | 225 bool force_keep_session_state_; |
238 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| | 226 // Guard |cookies_|, |pending_|, |num_pending_|, |force_keep_session_state_| |
239 base::Lock lock_; | 227 base::Lock lock_; |
240 | 228 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 // guess at how to handle them. | 352 // guess at how to handle them. |
365 break; | 353 break; |
366 } | 354 } |
367 | 355 |
368 if (delete_db && backend_) { | 356 if (delete_db && backend_) { |
369 // Don't just do the close/delete here, as we are being called by |db| and | 357 // Don't just do the close/delete here, as we are being called by |db| and |
370 // that seems dangerous. | 358 // that seems dangerous. |
371 MessageLoop::current()->PostTask( | 359 MessageLoop::current()->PostTask( |
372 FROM_HERE, base::Bind(&Backend::KillDatabase, backend_)); | 360 FROM_HERE, base::Bind(&Backend::KillDatabase, backend_)); |
373 | 361 |
374 // Avoid being called more than once. There should still be a reference to | 362 // Avoid being called more than once. This will destroy the |
375 // this ErrorDelegate in the backend, but just in case don't refer to any | 363 // KillDatabaseErrorDelegate. Do not refer to any members from here forward. |
376 // members from here forward. | 364 connection->set_error_delegate(wrapped_delegate_.release()); |
377 connection->set_error_delegate(wrapped_delegate_.get()); | |
378 } | 365 } |
379 | 366 |
380 return error; | 367 return error; |
381 } | 368 } |
382 | 369 |
383 // Version number of the database. | 370 // Version number of the database. |
384 // | 371 // |
385 // Version 5 adds the columns has_expires and is_persistent, so that the | 372 // Version 5 adds the columns has_expires and is_persistent, so that the |
386 // database can store session cookies as well as persistent cookies. Databases | 373 // database can store session cookies as well as persistent cookies. Databases |
387 // of version 5 are incompatible with older versions of code. If a database of | 374 // of version 5 are incompatible with older versions of code. If a database of |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 return false; | 607 return false; |
621 } | 608 } |
622 | 609 |
623 int64 db_size = 0; | 610 int64 db_size = 0; |
624 if (file_util::GetFileSize(path_, &db_size)) { | 611 if (file_util::GetFileSize(path_, &db_size)) { |
625 base::ThreadRestrictions::ScopedAllowIO allow_io; | 612 base::ThreadRestrictions::ScopedAllowIO allow_io; |
626 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size / 1024 ); | 613 UMA_HISTOGRAM_COUNTS("Cookie.DBSizeInKB", db_size / 1024 ); |
627 } | 614 } |
628 | 615 |
629 db_.reset(new sql::Connection); | 616 db_.reset(new sql::Connection); |
630 db_->set_error_delegate(error_delegate_.get()); | 617 db_->set_error_delegate( |
| 618 new KillDatabaseErrorDelegate(this, GetErrorHandlerForCookieDb())); |
631 | 619 |
632 if (!db_->Open(path_)) { | 620 if (!db_->Open(path_)) { |
633 NOTREACHED() << "Unable to open cookie DB."; | 621 NOTREACHED() << "Unable to open cookie DB."; |
634 db_.reset(); | 622 db_.reset(); |
635 return false; | 623 return false; |
636 } | 624 } |
637 | 625 |
638 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { | 626 if (!EnsureDatabaseVersion() || !InitTable(db_.get())) { |
639 NOTREACHED() << "Unable to open cookie DB."; | 627 NOTREACHED() << "Unable to open cookie DB."; |
640 db_.reset(); | 628 db_.reset(); |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1172 } | 1160 } |
1173 | 1161 |
1174 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1162 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
1175 if (backend_.get()) { | 1163 if (backend_.get()) { |
1176 backend_->Close(); | 1164 backend_->Close(); |
1177 // Release our reference, it will probably still have a reference if the | 1165 // Release our reference, it will probably still have a reference if the |
1178 // background thread has not run Close() yet. | 1166 // background thread has not run Close() yet. |
1179 backend_ = NULL; | 1167 backend_ = NULL; |
1180 } | 1168 } |
1181 } | 1169 } |
OLD | NEW |