| 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_ = | 78 error_delegate_ = new KillDatabaseErrorDelegate(this); |
| 79 new KillDatabaseErrorDelegate(this, GetErrorHandlerForCookieDb()); | |
| 80 } | 79 } |
| 81 | 80 |
| 82 // Creates or loads the SQLite database. | 81 // Creates or loads the SQLite database. |
| 83 void Load(const LoadedCallback& loaded_callback); | 82 void Load(const LoadedCallback& loaded_callback); |
| 84 | 83 |
| 85 // Loads cookies for the domain key (eTLD+1). | 84 // Loads cookies for the domain key (eTLD+1). |
| 86 void LoadCookiesForKey(const std::string& domain, | 85 void LoadCookiesForKey(const std::string& domain, |
| 87 const LoadedCallback& loaded_callback); | 86 const LoadedCallback& loaded_callback); |
| 88 | 87 |
| 89 // Batch a cookie addition. | 88 // Batch a cookie addition. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 102 // before the object is destructed. | 101 // before the object is destructed. |
| 103 void Close(); | 102 void Close(); |
| 104 | 103 |
| 105 void SetForceKeepSessionState(); | 104 void SetForceKeepSessionState(); |
| 106 | 105 |
| 107 private: | 106 private: |
| 108 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; | 107 friend class base::RefCountedThreadSafe<SQLitePersistentCookieStore::Backend>; |
| 109 | 108 |
| 110 class KillDatabaseErrorDelegate : public sql::ErrorDelegate { | 109 class KillDatabaseErrorDelegate : public sql::ErrorDelegate { |
| 111 public: | 110 public: |
| 112 KillDatabaseErrorDelegate(Backend* backend, | 111 explicit KillDatabaseErrorDelegate(Backend* backend); |
| 113 sql::ErrorDelegate* wrapped_delegate); | |
| 114 // ErrorDelegate implementation. | 112 // ErrorDelegate implementation. |
| 115 virtual int OnError(int error, | 113 virtual int OnError(int error, |
| 116 sql::Connection* connection, | 114 sql::Connection* connection, |
| 117 sql::Statement* stmt) OVERRIDE; | 115 sql::Statement* stmt) OVERRIDE; |
| 118 | 116 |
| 119 void reset_backend() { | 117 void reset_backend() { |
| 120 backend_ = NULL; | 118 backend_ = NULL; |
| 121 } | 119 } |
| 122 | 120 |
| 123 protected: | 121 protected: |
| 124 virtual ~KillDatabaseErrorDelegate() {} | 122 virtual ~KillDatabaseErrorDelegate() {} |
| 125 | 123 |
| 126 private: | 124 private: |
| 127 | 125 |
| 128 // Do not increment the count on Backend, as that would create a circular | 126 // Do not increment the count on Backend, as that would create a circular |
| 129 // reference (Backend -> Connection -> ErrorDelegate -> Backend). Instead, | 127 // reference (Backend -> Connection -> ErrorDelegate -> Backend). Instead, |
| 130 // Backend will call reset_backend() when it is going away. | 128 // Backend will call reset_backend() when it is going away. |
| 131 Backend* backend_; | 129 Backend* backend_; |
| 132 scoped_refptr<sql::ErrorDelegate> wrapped_delegate_; | |
| 133 | 130 |
| 134 DISALLOW_COPY_AND_ASSIGN(KillDatabaseErrorDelegate); | 131 DISALLOW_COPY_AND_ASSIGN(KillDatabaseErrorDelegate); |
| 135 }; | 132 }; |
| 136 | 133 |
| 137 // You should call Close() before destructing this object. | 134 // You should call Close() before destructing this object. |
| 138 ~Backend() { | 135 ~Backend() { |
| 139 if (error_delegate_.get()) { | 136 if (error_delegate_.get()) { |
| 140 error_delegate_->reset_backend(); | 137 error_delegate_->reset_backend(); |
| 141 error_delegate_ = NULL; | 138 error_delegate_ = NULL; |
| 142 } | 139 } |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 // The time when |num_priority_waiting_| incremented to 1. | 275 // The time when |num_priority_waiting_| incremented to 1. |
| 279 base::Time current_priority_wait_start_; | 276 base::Time current_priority_wait_start_; |
| 280 // The cumulative duration of time when |num_priority_waiting_| was greater | 277 // The cumulative duration of time when |num_priority_waiting_| was greater |
| 281 // than 1. | 278 // than 1. |
| 282 base::TimeDelta priority_wait_duration_; | 279 base::TimeDelta priority_wait_duration_; |
| 283 | 280 |
| 284 DISALLOW_COPY_AND_ASSIGN(Backend); | 281 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 285 }; | 282 }; |
| 286 | 283 |
| 287 SQLitePersistentCookieStore::Backend::KillDatabaseErrorDelegate:: | 284 SQLitePersistentCookieStore::Backend::KillDatabaseErrorDelegate:: |
| 288 KillDatabaseErrorDelegate(Backend* backend, | 285 KillDatabaseErrorDelegate(Backend* backend) |
| 289 sql::ErrorDelegate* wrapped_delegate) | 286 : backend_(backend) { |
| 290 : backend_(backend), | |
| 291 wrapped_delegate_(wrapped_delegate) { | |
| 292 } | 287 } |
| 293 | 288 |
| 294 int SQLitePersistentCookieStore::Backend::KillDatabaseErrorDelegate::OnError( | 289 int SQLitePersistentCookieStore::Backend::KillDatabaseErrorDelegate::OnError( |
| 295 int error, sql::Connection* connection, sql::Statement* stmt) { | 290 int error, sql::Connection* connection, sql::Statement* stmt) { |
| 296 if (wrapped_delegate_.get()) | |
| 297 error = wrapped_delegate_->OnError(error, connection, stmt); | |
| 298 | |
| 299 bool delete_db = false; | 291 bool delete_db = false; |
| 300 | 292 |
| 301 switch (error) { | 293 switch (error) { |
| 302 case SQLITE_DONE: | 294 case SQLITE_DONE: |
| 303 case SQLITE_OK: | 295 case SQLITE_OK: |
| 304 // Theoretically, the wrapped delegate might have resolved the error, and | 296 // Theoretically, the wrapped delegate might have resolved the error, and |
| 305 // we would end up here. | 297 // we would end up here. |
| 306 break; | 298 break; |
| 307 | 299 |
| 308 case SQLITE_CORRUPT: | 300 case SQLITE_CORRUPT: |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 | 359 |
| 368 if (delete_db && backend_) { | 360 if (delete_db && backend_) { |
| 369 // Don't just do the close/delete here, as we are being called by |db| and | 361 // Don't just do the close/delete here, as we are being called by |db| and |
| 370 // that seems dangerous. | 362 // that seems dangerous. |
| 371 MessageLoop::current()->PostTask( | 363 MessageLoop::current()->PostTask( |
| 372 FROM_HERE, base::Bind(&Backend::KillDatabase, backend_)); | 364 FROM_HERE, base::Bind(&Backend::KillDatabase, backend_)); |
| 373 | 365 |
| 374 // Avoid being called more than once. There should still be a reference to | 366 // Avoid being called more than once. There should still be a reference to |
| 375 // this ErrorDelegate in the backend, but just in case don't refer to any | 367 // this ErrorDelegate in the backend, but just in case don't refer to any |
| 376 // members from here forward. | 368 // members from here forward. |
| 377 connection->set_error_delegate(wrapped_delegate_.get()); | 369 connection->remove_error_delegate(this); |
| 378 } | 370 } |
| 379 | 371 |
| 380 return error; | 372 return error; |
| 381 } | 373 } |
| 382 | 374 |
| 383 // Version number of the database. | 375 // Version number of the database. |
| 384 // | 376 // |
| 385 // Version 5 adds the columns has_expires and is_persistent, so that the | 377 // 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 | 378 // 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 | 379 // of version 5 are incompatible with older versions of code. If a database of |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 } | 1164 } |
| 1173 | 1165 |
| 1174 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 1166 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 1175 if (backend_.get()) { | 1167 if (backend_.get()) { |
| 1176 backend_->Close(); | 1168 backend_->Close(); |
| 1177 // Release our reference, it will probably still have a reference if the | 1169 // Release our reference, it will probably still have a reference if the |
| 1178 // background thread has not run Close() yet. | 1170 // background thread has not run Close() yet. |
| 1179 backend_ = NULL; | 1171 backend_ = NULL; |
| 1180 } | 1172 } |
| 1181 } | 1173 } |
| OLD | NEW |