| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 | 8 |
| 9 #include "app/sql/meta_table.h" | 9 #include "app/sql/meta_table.h" |
| 10 #include "app/sql/statement.h" | 10 #include "app/sql/statement.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 FilePath path_; | 100 FilePath path_; |
| 101 scoped_ptr<sql::Connection> db_; | 101 scoped_ptr<sql::Connection> db_; |
| 102 sql::MetaTable meta_table_; | 102 sql::MetaTable meta_table_; |
| 103 | 103 |
| 104 typedef std::list<PendingOperation*> PendingOperationsList; | 104 typedef std::list<PendingOperation*> PendingOperationsList; |
| 105 PendingOperationsList pending_; | 105 PendingOperationsList pending_; |
| 106 PendingOperationsList::size_type num_pending_; | 106 PendingOperationsList::size_type num_pending_; |
| 107 // True if the persistent store should be deleted upon destruction. | 107 // True if the persistent store should be deleted upon destruction. |
| 108 bool clear_local_state_on_exit_; | 108 bool clear_local_state_on_exit_; |
| 109 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. | 109 // Guard |pending_|, |num_pending_| and |clear_local_state_on_exit_|. |
| 110 Lock lock_; | 110 base::Lock lock_; |
| 111 | 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(Backend); | 112 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 // Version number of the database. In version 4, we migrated the time epoch. | 115 // Version number of the database. In version 4, we migrated the time epoch. |
| 116 // If you open the DB with an older version on Mac or Linux, the times will | 116 // If you open the DB with an older version on Mac or Linux, the times will |
| 117 // look wonky, but the file will likely be usable. On Windows version 3 and 4 | 117 // look wonky, but the file will likely be usable. On Windows version 3 and 4 |
| 118 // are the same. | 118 // are the same. |
| 119 // | 119 // |
| 120 // Version 3 updated the database to include the last access time, so we can | 120 // Version 3 updated the database to include the last access time, so we can |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 static const int kCommitIntervalMs = 30 * 1000; | 299 static const int kCommitIntervalMs = 30 * 1000; |
| 300 // Commit right away if we have more than 512 outstanding operations. | 300 // Commit right away if we have more than 512 outstanding operations. |
| 301 static const size_t kCommitAfterBatchSize = 512; | 301 static const size_t kCommitAfterBatchSize = 512; |
| 302 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); | 302 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 303 | 303 |
| 304 // We do a full copy of the cookie here, and hopefully just here. | 304 // We do a full copy of the cookie here, and hopefully just here. |
| 305 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); | 305 scoped_ptr<PendingOperation> po(new PendingOperation(op, cc)); |
| 306 | 306 |
| 307 PendingOperationsList::size_type num_pending; | 307 PendingOperationsList::size_type num_pending; |
| 308 { | 308 { |
| 309 AutoLock locked(lock_); | 309 base::AutoLock locked(lock_); |
| 310 pending_.push_back(po.release()); | 310 pending_.push_back(po.release()); |
| 311 num_pending = ++num_pending_; | 311 num_pending = ++num_pending_; |
| 312 } | 312 } |
| 313 | 313 |
| 314 if (num_pending == 1) { | 314 if (num_pending == 1) { |
| 315 // We've gotten our first entry for this batch, fire off the timer. | 315 // We've gotten our first entry for this batch, fire off the timer. |
| 316 BrowserThread::PostDelayedTask( | 316 BrowserThread::PostDelayedTask( |
| 317 BrowserThread::DB, FROM_HERE, | 317 BrowserThread::DB, FROM_HERE, |
| 318 NewRunnableMethod(this, &Backend::Commit), kCommitIntervalMs); | 318 NewRunnableMethod(this, &Backend::Commit), kCommitIntervalMs); |
| 319 } else if (num_pending == kCommitAfterBatchSize) { | 319 } else if (num_pending == kCommitAfterBatchSize) { |
| 320 // We've reached a big enough batch, fire off a commit now. | 320 // We've reached a big enough batch, fire off a commit now. |
| 321 BrowserThread::PostTask( | 321 BrowserThread::PostTask( |
| 322 BrowserThread::DB, FROM_HERE, | 322 BrowserThread::DB, FROM_HERE, |
| 323 NewRunnableMethod(this, &Backend::Commit)); | 323 NewRunnableMethod(this, &Backend::Commit)); |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 void SQLitePersistentCookieStore::Backend::Commit() { | 327 void SQLitePersistentCookieStore::Backend::Commit() { |
| 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 329 | 329 |
| 330 PendingOperationsList ops; | 330 PendingOperationsList ops; |
| 331 { | 331 { |
| 332 AutoLock locked(lock_); | 332 base::AutoLock locked(lock_); |
| 333 pending_.swap(ops); | 333 pending_.swap(ops); |
| 334 num_pending_ = 0; | 334 num_pending_ = 0; |
| 335 } | 335 } |
| 336 | 336 |
| 337 // Maybe an old timer fired or we are already Close()'ed. | 337 // Maybe an old timer fired or we are already Close()'ed. |
| 338 if (!db_.get() || ops.empty()) | 338 if (!db_.get() || ops.empty()) |
| 339 return; | 339 return; |
| 340 | 340 |
| 341 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, | 341 sql::Statement add_smt(db_->GetCachedStatement(SQL_FROM_HERE, |
| 342 "INSERT INTO cookies (creation_utc, host_key, name, value, path, " | 342 "INSERT INTO cookies (creation_utc, host_key, name, value, path, " |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 Commit(); | 442 Commit(); |
| 443 | 443 |
| 444 db_.reset(); | 444 db_.reset(); |
| 445 | 445 |
| 446 if (clear_local_state_on_exit_) | 446 if (clear_local_state_on_exit_) |
| 447 file_util::Delete(path_, false); | 447 file_util::Delete(path_, false); |
| 448 } | 448 } |
| 449 | 449 |
| 450 void SQLitePersistentCookieStore::Backend::SetClearLocalStateOnExit( | 450 void SQLitePersistentCookieStore::Backend::SetClearLocalStateOnExit( |
| 451 bool clear_local_state) { | 451 bool clear_local_state) { |
| 452 AutoLock locked(lock_); | 452 base::AutoLock locked(lock_); |
| 453 clear_local_state_on_exit_ = clear_local_state; | 453 clear_local_state_on_exit_ = clear_local_state; |
| 454 } | 454 } |
| 455 SQLitePersistentCookieStore::SQLitePersistentCookieStore(const FilePath& path) | 455 SQLitePersistentCookieStore::SQLitePersistentCookieStore(const FilePath& path) |
| 456 : backend_(new Backend(path)) { | 456 : backend_(new Backend(path)) { |
| 457 } | 457 } |
| 458 | 458 |
| 459 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { | 459 SQLitePersistentCookieStore::~SQLitePersistentCookieStore() { |
| 460 if (backend_.get()) { | 460 if (backend_.get()) { |
| 461 backend_->Close(); | 461 backend_->Close(); |
| 462 // Release our reference, it will probably still have a reference if the | 462 // Release our reference, it will probably still have a reference if the |
| (...skipping 30 matching lines...) Expand all Loading... |
| 493 if (backend_.get()) | 493 if (backend_.get()) |
| 494 backend_->SetClearLocalStateOnExit(clear_local_state); | 494 backend_->SetClearLocalStateOnExit(clear_local_state); |
| 495 } | 495 } |
| 496 | 496 |
| 497 void SQLitePersistentCookieStore::Flush(Task* completion_task) { | 497 void SQLitePersistentCookieStore::Flush(Task* completion_task) { |
| 498 if (backend_.get()) | 498 if (backend_.get()) |
| 499 backend_->Flush(completion_task); | 499 backend_->Flush(completion_task); |
| 500 else if (completion_task) | 500 else if (completion_task) |
| 501 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 501 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 502 } | 502 } |
| OLD | NEW |