| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/location.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "content/browser/dom_storage/dom_storage_area.h" | 15 #include "content/browser/dom_storage/dom_storage_area.h" |
| 14 #include "content/browser/dom_storage/dom_storage_database.h" | 16 #include "content/browser/dom_storage/dom_storage_database.h" |
| 15 #include "content/browser/dom_storage/dom_storage_database_adapter.h" | 17 #include "content/browser/dom_storage/dom_storage_database_adapter.h" |
| 16 #include "content/browser/dom_storage/dom_storage_task_runner.h" | 18 #include "content/browser/dom_storage/dom_storage_task_runner.h" |
| 17 #include "content/browser/dom_storage/local_storage_database_adapter.h" | 19 #include "content/browser/dom_storage/local_storage_database_adapter.h" |
| 18 #include "content/common/dom_storage/dom_storage_types.h" | 20 #include "content/common/dom_storage/dom_storage_types.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 38 const base::string16 kValue; | 40 const base::string16 kValue; |
| 39 const base::string16 kKey2; | 41 const base::string16 kKey2; |
| 40 const base::string16 kValue2; | 42 const base::string16 kValue2; |
| 41 | 43 |
| 42 // Method used in the CommitTasks test case. | 44 // Method used in the CommitTasks test case. |
| 43 void InjectedCommitSequencingTask1( | 45 void InjectedCommitSequencingTask1( |
| 44 const scoped_refptr<DOMStorageArea>& area) { | 46 const scoped_refptr<DOMStorageArea>& area) { |
| 45 // At this point the StartCommitTimer task has run and | 47 // At this point the StartCommitTimer task has run and |
| 46 // the OnCommitTimer task is queued. We want to inject after | 48 // the OnCommitTimer task is queued. We want to inject after |
| 47 // that. | 49 // that. |
| 48 base::MessageLoop::current()->PostTask( | 50 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 49 FROM_HERE, | 51 FROM_HERE, |
| 50 base::Bind(&DOMStorageAreaTest::InjectedCommitSequencingTask2, | 52 base::Bind(&DOMStorageAreaTest::InjectedCommitSequencingTask2, |
| 51 base::Unretained(this), | 53 base::Unretained(this), area)); |
| 52 area)); | |
| 53 } | 54 } |
| 54 | 55 |
| 55 void InjectedCommitSequencingTask2( | 56 void InjectedCommitSequencingTask2( |
| 56 const scoped_refptr<DOMStorageArea>& area) { | 57 const scoped_refptr<DOMStorageArea>& area) { |
| 57 // At this point the OnCommitTimer has run. | 58 // At this point the OnCommitTimer has run. |
| 58 // Verify that it put a commit in flight. | 59 // Verify that it put a commit in flight. |
| 59 EXPECT_EQ(1, area->commit_batches_in_flight_); | 60 EXPECT_EQ(1, area->commit_batches_in_flight_); |
| 60 EXPECT_FALSE(area->commit_batch_.get()); | 61 EXPECT_FALSE(area->commit_batch_.get()); |
| 61 EXPECT_TRUE(area->HasUncommittedChanges()); | 62 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 62 // Make additional change and verify that a new commit batch | 63 // Make additional change and verify that a new commit batch |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 167 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 167 ASSERT_TRUE(old_value.is_null()); | 168 ASSERT_TRUE(old_value.is_null()); |
| 168 | 169 |
| 169 // Check that saving a value has still left us without a backing database. | 170 // Check that saving a value has still left us without a backing database. |
| 170 EXPECT_EQ(NULL, area->backing_.get()); | 171 EXPECT_EQ(NULL, area->backing_.get()); |
| 171 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); | 172 EXPECT_FALSE(base::PathExists(kExpectedOriginFilePath)); |
| 172 } | 173 } |
| 173 | 174 |
| 174 // This should set up a DOMStorageArea that is correctly backed to disk. | 175 // This should set up a DOMStorageArea that is correctly backed to disk. |
| 175 { | 176 { |
| 176 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 177 scoped_refptr<DOMStorageArea> area( |
| 177 kOrigin, | 178 new DOMStorageArea(kOrigin, temp_dir.path(), |
| 178 temp_dir.path(), | 179 new MockDOMStorageTaskRunner( |
| 179 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | 180 base::ThreadTaskRunnerHandle::Get().get()))); |
| 180 | 181 |
| 181 EXPECT_TRUE(area->backing_.get()); | 182 EXPECT_TRUE(area->backing_.get()); |
| 182 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( | 183 DOMStorageDatabase* database = static_cast<LocalStorageDatabaseAdapter*>( |
| 183 area->backing_.get())->db_.get(); | 184 area->backing_.get())->db_.get(); |
| 184 EXPECT_FALSE(database->IsOpen()); | 185 EXPECT_FALSE(database->IsOpen()); |
| 185 EXPECT_FALSE(area->is_initial_import_done_); | 186 EXPECT_FALSE(area->is_initial_import_done_); |
| 186 | 187 |
| 187 // Inject an in-memory db to speed up the test. | 188 // Inject an in-memory db to speed up the test. |
| 188 // We will verify that something is written into the database but not | 189 // We will verify that something is written into the database but not |
| 189 // that a file is written to disk - DOMStorageDatabase unit tests cover | 190 // that a file is written to disk - DOMStorageDatabase unit tests cover |
| (...skipping 24 matching lines...) Expand all Loading... |
| 214 EXPECT_EQ(1u, values.size()); | 215 EXPECT_EQ(1u, values.size()); |
| 215 EXPECT_EQ(kValue, values[kKey].string()); | 216 EXPECT_EQ(kValue, values[kKey].string()); |
| 216 } | 217 } |
| 217 } | 218 } |
| 218 | 219 |
| 219 TEST_F(DOMStorageAreaTest, CommitTasks) { | 220 TEST_F(DOMStorageAreaTest, CommitTasks) { |
| 220 base::ScopedTempDir temp_dir; | 221 base::ScopedTempDir temp_dir; |
| 221 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 222 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 222 | 223 |
| 223 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 224 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 224 kOrigin, | 225 kOrigin, temp_dir.path(), |
| 225 temp_dir.path(), | 226 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 226 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | |
| 227 // Inject an in-memory db to speed up the test. | 227 // Inject an in-memory db to speed up the test. |
| 228 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 228 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 229 | 229 |
| 230 // Unrelated to commits, but while we're here, see that querying Length() | 230 // Unrelated to commits, but while we're here, see that querying Length() |
| 231 // causes the backing database to be opened and presumably read from. | 231 // causes the backing database to be opened and presumably read from. |
| 232 EXPECT_FALSE(area->is_initial_import_done_); | 232 EXPECT_FALSE(area->is_initial_import_done_); |
| 233 EXPECT_EQ(0u, area->Length()); | 233 EXPECT_EQ(0u, area->Length()); |
| 234 EXPECT_TRUE(area->is_initial_import_done_); | 234 EXPECT_TRUE(area->is_initial_import_done_); |
| 235 | 235 |
| 236 DOMStorageValuesMap values; | 236 DOMStorageValuesMap values; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 area->backing_->ReadAllValues(&values); | 293 area->backing_->ReadAllValues(&values); |
| 294 EXPECT_EQ(2u, values.size()); | 294 EXPECT_EQ(2u, values.size()); |
| 295 EXPECT_EQ(kValue, values[kKey].string()); | 295 EXPECT_EQ(kValue, values[kKey].string()); |
| 296 EXPECT_EQ(kValue2, values[kKey2].string()); | 296 EXPECT_EQ(kValue2, values[kKey2].string()); |
| 297 } | 297 } |
| 298 | 298 |
| 299 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { | 299 TEST_F(DOMStorageAreaTest, CommitChangesAtShutdown) { |
| 300 base::ScopedTempDir temp_dir; | 300 base::ScopedTempDir temp_dir; |
| 301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 301 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 302 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 302 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 303 kOrigin, | 303 kOrigin, temp_dir.path(), |
| 304 temp_dir.path(), | 304 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 305 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | |
| 306 | 305 |
| 307 // Inject an in-memory db to speed up the test and also to verify | 306 // Inject an in-memory db to speed up the test and also to verify |
| 308 // the final changes are commited in it's dtor. | 307 // the final changes are commited in it's dtor. |
| 309 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( | 308 static_cast<LocalStorageDatabaseAdapter*>(area->backing_.get())->db_.reset( |
| 310 new VerifyChangesCommittedDatabase()); | 309 new VerifyChangesCommittedDatabase()); |
| 311 | 310 |
| 312 DOMStorageValuesMap values; | 311 DOMStorageValuesMap values; |
| 313 base::NullableString16 old_value; | 312 base::NullableString16 old_value; |
| 314 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); | 313 EXPECT_TRUE(area->SetItem(kKey, kValue, &old_value)); |
| 315 EXPECT_TRUE(area->HasUncommittedChanges()); | 314 EXPECT_TRUE(area->HasUncommittedChanges()); |
| 316 area->backing_->ReadAllValues(&values); | 315 area->backing_->ReadAllValues(&values); |
| 317 EXPECT_TRUE(values.empty()); // not committed yet | 316 EXPECT_TRUE(values.empty()); // not committed yet |
| 318 area->Shutdown(); | 317 area->Shutdown(); |
| 319 base::MessageLoop::current()->RunUntilIdle(); | 318 base::MessageLoop::current()->RunUntilIdle(); |
| 320 EXPECT_TRUE(area->HasOneRef()); | 319 EXPECT_TRUE(area->HasOneRef()); |
| 321 EXPECT_FALSE(area->backing_.get()); | 320 EXPECT_FALSE(area->backing_.get()); |
| 322 // The VerifyChangesCommittedDatabase destructor verifies values | 321 // The VerifyChangesCommittedDatabase destructor verifies values |
| 323 // were committed. | 322 // were committed. |
| 324 } | 323 } |
| 325 | 324 |
| 326 TEST_F(DOMStorageAreaTest, DeleteOrigin) { | 325 TEST_F(DOMStorageAreaTest, DeleteOrigin) { |
| 327 base::ScopedTempDir temp_dir; | 326 base::ScopedTempDir temp_dir; |
| 328 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 327 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 329 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 328 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 330 kOrigin, | 329 kOrigin, temp_dir.path(), |
| 331 temp_dir.path(), | 330 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 332 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | |
| 333 | 331 |
| 334 // This test puts files on disk. | 332 // This test puts files on disk. |
| 335 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( | 333 base::FilePath db_file_path = static_cast<LocalStorageDatabaseAdapter*>( |
| 336 area->backing_.get())->db_->file_path(); | 334 area->backing_.get())->db_->file_path(); |
| 337 base::FilePath db_journal_file_path = | 335 base::FilePath db_journal_file_path = |
| 338 DOMStorageDatabase::GetJournalFilePath(db_file_path); | 336 DOMStorageDatabase::GetJournalFilePath(db_file_path); |
| 339 | 337 |
| 340 // Nothing bad should happen when invoked w/o any files on disk. | 338 // Nothing bad should happen when invoked w/o any files on disk. |
| 341 area->DeleteOrigin(); | 339 area->DeleteOrigin(); |
| 342 EXPECT_FALSE(base::PathExists(db_file_path)); | 340 EXPECT_FALSE(base::PathExists(db_file_path)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 EXPECT_TRUE(base::PathExists(db_file_path)); | 379 EXPECT_TRUE(base::PathExists(db_file_path)); |
| 382 area->Shutdown(); | 380 area->Shutdown(); |
| 383 base::MessageLoop::current()->RunUntilIdle(); | 381 base::MessageLoop::current()->RunUntilIdle(); |
| 384 EXPECT_FALSE(base::PathExists(db_file_path)); | 382 EXPECT_FALSE(base::PathExists(db_file_path)); |
| 385 } | 383 } |
| 386 | 384 |
| 387 TEST_F(DOMStorageAreaTest, PurgeMemory) { | 385 TEST_F(DOMStorageAreaTest, PurgeMemory) { |
| 388 base::ScopedTempDir temp_dir; | 386 base::ScopedTempDir temp_dir; |
| 389 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 387 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 390 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( | 388 scoped_refptr<DOMStorageArea> area(new DOMStorageArea( |
| 391 kOrigin, | 389 kOrigin, temp_dir.path(), |
| 392 temp_dir.path(), | 390 new MockDOMStorageTaskRunner(base::ThreadTaskRunnerHandle::Get().get()))); |
| 393 new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()))); | |
| 394 | 391 |
| 395 // Inject an in-memory db to speed up the test. | 392 // Inject an in-memory db to speed up the test. |
| 396 area->backing_.reset(new LocalStorageDatabaseAdapter()); | 393 area->backing_.reset(new LocalStorageDatabaseAdapter()); |
| 397 | 394 |
| 398 // Unowned ptrs we use to verify that 'purge' has happened. | 395 // Unowned ptrs we use to verify that 'purge' has happened. |
| 399 DOMStorageDatabase* original_backing = | 396 DOMStorageDatabase* original_backing = |
| 400 static_cast<LocalStorageDatabaseAdapter*>( | 397 static_cast<LocalStorageDatabaseAdapter*>( |
| 401 area->backing_.get())->db_.get(); | 398 area->backing_.get())->db_.get(); |
| 402 DOMStorageMap* original_map = area->map_.get(); | 399 DOMStorageMap* original_map = area->map_.get(); |
| 403 | 400 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 base::TimeDelta::FromMilliseconds(750))); | 525 base::TimeDelta::FromMilliseconds(750))); |
| 529 EXPECT_EQ(base::TimeDelta(), | 526 EXPECT_EQ(base::TimeDelta(), |
| 530 rate_limiter.ComputeDelayNeeded( | 527 rate_limiter.ComputeDelayNeeded( |
| 531 base::TimeDelta::FromMilliseconds(1500))); | 528 base::TimeDelta::FromMilliseconds(1500))); |
| 532 EXPECT_EQ(base::TimeDelta(), | 529 EXPECT_EQ(base::TimeDelta(), |
| 533 rate_limiter.ComputeDelayNeeded( | 530 rate_limiter.ComputeDelayNeeded( |
| 534 base::TimeDelta::FromDays(1))); | 531 base::TimeDelta::FromDays(1))); |
| 535 } | 532 } |
| 536 | 533 |
| 537 } // namespace content | 534 } // namespace content |
| OLD | NEW |