Chromium Code Reviews| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/auto_reset.h" | 8 #include "base/auto_reset.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/sequenced_task_runner_handle.h" |
| 18 #include "content/browser/indexed_db/indexed_db_connection.h" | 18 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 19 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 19 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 20 #include "content/browser/indexed_db/indexed_db_data_format_version.h" | 20 #include "content/browser/indexed_db/indexed_db_data_format_version.h" |
| 21 #include "content/browser/indexed_db/indexed_db_factory_impl.h" | 21 #include "content/browser/indexed_db/indexed_db_factory_impl.h" |
| 22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" | 22 #include "content/browser/indexed_db/mock_indexed_db_callbacks.h" |
| 23 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" | 23 #include "content/browser/indexed_db/mock_indexed_db_database_callbacks.h" |
| 24 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 25 #include "content/public/test/test_utils.h" | |
| 25 #include "storage/browser/test/mock_quota_manager_proxy.h" | 26 #include "storage/browser/test/mock_quota_manager_proxy.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" | 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc eption.h" |
| 28 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" | 29 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBTypes.h" |
| 29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 30 #include "url/origin.h" | 31 #include "url/origin.h" |
| 31 | 32 |
| 32 using base::ASCIIToUTF16; | 33 using base::ASCIIToUTF16; |
| 33 using url::Origin; | 34 using url::Origin; |
| 34 | 35 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 65 private: | 66 private: |
| 66 ~MockIDBFactory() override {} | 67 ~MockIDBFactory() override {} |
| 67 | 68 |
| 68 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); | 69 DISALLOW_COPY_AND_ASSIGN(MockIDBFactory); |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 } // namespace | 72 } // namespace |
| 72 | 73 |
| 73 class IndexedDBFactoryTest : public testing::Test { | 74 class IndexedDBFactoryTest : public testing::Test { |
| 74 public: | 75 public: |
| 76 IndexedDBFactoryTest() | |
| 77 : quota_manager_proxy_( | |
| 78 base::MakeRefCounted<MockQuotaManagerProxy>(nullptr, nullptr)) {} | |
| 79 | |
| 75 void SetUp() override { | 80 void SetUp() override { |
| 76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 81 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 77 task_runner_ = new base::TestSimpleTaskRunner(); | 82 context_ = base::MakeRefCounted<IndexedDBContextImpl>( |
| 78 quota_manager_proxy_ = new MockQuotaManagerProxy(nullptr, nullptr); | |
| 79 context_ = new IndexedDBContextImpl( | |
| 80 temp_dir_.GetPath(), nullptr /* special_storage_policy */, | 83 temp_dir_.GetPath(), nullptr /* special_storage_policy */, |
| 81 quota_manager_proxy_.get(), task_runner_.get()); | 84 quota_manager_proxy_.get()); |
| 82 idb_factory_ = new MockIDBFactory(context_.get()); | |
| 83 } | 85 } |
| 84 | 86 |
| 85 void TearDown() override { | 87 void TearDown() override { |
| 86 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 88 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
| 87 } | 89 } |
| 88 | 90 |
| 89 protected: | 91 protected: |
| 90 IndexedDBFactoryTest() {} | |
| 91 MockIDBFactory* factory() const { return idb_factory_.get(); } | |
| 92 void clear_factory() { idb_factory_ = nullptr; } | |
| 93 IndexedDBContextImpl* context() const { return context_.get(); } | 92 IndexedDBContextImpl* context() const { return context_.get(); } |
| 94 | 93 |
| 95 private: | 94 private: |
| 95 TestBrowserThreadBundle thread_bundle_; | |
| 96 | |
| 96 base::ScopedTempDir temp_dir_; | 97 base::ScopedTempDir temp_dir_; |
| 97 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 98 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
| 98 scoped_refptr<IndexedDBContextImpl> context_; | 99 scoped_refptr<IndexedDBContextImpl> context_; |
| 99 scoped_refptr<MockIDBFactory> idb_factory_; | |
| 100 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | |
| 101 TestBrowserThreadBundle thread_bundle_; | |
| 102 | 100 |
| 103 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); | 101 DISALLOW_COPY_AND_ASSIGN(IndexedDBFactoryTest); |
| 104 }; | 102 }; |
| 105 | 103 |
| 106 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { | 104 TEST_F(IndexedDBFactoryTest, BackingStoreLifetime) { |
| 107 const Origin origin1(GURL("http://localhost:81")); | 105 context()->TaskRunner()->PostTask( |
| 108 const Origin origin2(GURL("http://localhost:82")); | 106 FROM_HERE, |
| 107 base::BindOnce( | |
| 108 [](IndexedDBContextImpl* context) { | |
| 109 scoped_refptr<MockIDBFactory> factory = | |
| 110 base::MakeRefCounted<MockIDBFactory>(context); | |
| 109 | 111 |
| 110 base::ScopedTempDir temp_directory; | 112 const Origin origin1(GURL("http://localhost:81")); |
| 111 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 113 const Origin origin2(GURL("http://localhost:82")); |
| 112 scoped_refptr<IndexedDBBackingStore> disk_store1 = | |
| 113 factory()->TestOpenBackingStore(origin1, temp_directory.GetPath()); | |
| 114 | 114 |
| 115 scoped_refptr<IndexedDBBackingStore> disk_store2 = | 115 scoped_refptr<IndexedDBBackingStore> disk_store1 = |
| 116 factory()->TestOpenBackingStore(origin1, temp_directory.GetPath()); | 116 factory->TestOpenBackingStore(origin1, context->data_path()); |
| 117 EXPECT_EQ(disk_store1.get(), disk_store2.get()); | |
| 118 | 117 |
| 119 scoped_refptr<IndexedDBBackingStore> disk_store3 = | 118 scoped_refptr<IndexedDBBackingStore> disk_store2 = |
| 120 factory()->TestOpenBackingStore(origin2, temp_directory.GetPath()); | 119 factory->TestOpenBackingStore(origin1, context->data_path()); |
| 120 EXPECT_EQ(disk_store1.get(), disk_store2.get()); | |
| 121 | 121 |
| 122 factory()->TestCloseBackingStore(disk_store1.get()); | 122 scoped_refptr<IndexedDBBackingStore> disk_store3 = |
| 123 factory()->TestCloseBackingStore(disk_store3.get()); | 123 factory->TestOpenBackingStore(origin2, context->data_path()); |
| 124 | 124 |
| 125 EXPECT_FALSE(disk_store1->HasOneRef()); | 125 factory->TestCloseBackingStore(disk_store1.get()); |
| 126 EXPECT_FALSE(disk_store2->HasOneRef()); | 126 factory->TestCloseBackingStore(disk_store3.get()); |
| 127 EXPECT_TRUE(disk_store3->HasOneRef()); | |
| 128 | 127 |
| 129 disk_store2 = nullptr; | 128 EXPECT_FALSE(disk_store1->HasOneRef()); |
| 130 EXPECT_TRUE(disk_store1->HasOneRef()); | 129 EXPECT_FALSE(disk_store2->HasOneRef()); |
| 130 EXPECT_TRUE(disk_store3->HasOneRef()); | |
| 131 | |
| 132 disk_store2 = nullptr; | |
| 133 EXPECT_TRUE(disk_store1->HasOneRef()); | |
| 134 }, | |
| 135 base::Unretained(context()))); | |
| 136 | |
| 137 RunAllBlockingPoolTasksUntilIdle(); | |
| 131 } | 138 } |
| 132 | 139 |
| 133 TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) { | 140 TEST_F(IndexedDBFactoryTest, BackingStoreLazyClose) { |
| 134 const Origin origin(GURL("http://localhost:81")); | 141 context()->TaskRunner()->PostTask( |
| 142 FROM_HERE, | |
| 143 base::BindOnce( | |
| 144 [](IndexedDBContextImpl* context) { | |
| 135 | 145 |
| 136 base::ScopedTempDir temp_directory; | 146 scoped_refptr<MockIDBFactory> factory = |
| 137 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 147 base::MakeRefCounted<MockIDBFactory>(context); |
| 138 scoped_refptr<IndexedDBBackingStore> store = | |
| 139 factory()->TestOpenBackingStore(origin, temp_directory.GetPath()); | |
| 140 | 148 |
| 141 // Give up the local refptr so that the factory has the only | 149 const Origin origin(GURL("http://localhost:81")); |
| 142 // outstanding reference. | |
| 143 IndexedDBBackingStore* store_ptr = store.get(); | |
| 144 store = nullptr; | |
| 145 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); | |
| 146 factory()->TestReleaseBackingStore(store_ptr, false); | |
| 147 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); | |
| 148 | 150 |
| 149 factory()->TestOpenBackingStore(origin, temp_directory.GetPath()); | 151 scoped_refptr<IndexedDBBackingStore> store = |
| 150 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); | 152 factory->TestOpenBackingStore(origin, context->data_path()); |
| 151 factory()->TestReleaseBackingStore(store_ptr, false); | |
| 152 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); | |
| 153 | 153 |
| 154 // Take back a ref ptr and ensure that the actual close | 154 // Give up the local refptr so that the factory has the only |
| 155 // stops a running timer. | 155 // outstanding reference. |
| 156 store = store_ptr; | 156 IndexedDBBackingStore* store_ptr = store.get(); |
| 157 factory()->TestCloseBackingStore(store_ptr); | 157 store = nullptr; |
| 158 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); | 158 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); |
| 159 factory->TestReleaseBackingStore(store_ptr, false); | |
| 160 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); | |
| 161 | |
| 162 factory->TestOpenBackingStore(origin, context->data_path()); | |
| 163 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); | |
| 164 factory->TestReleaseBackingStore(store_ptr, false); | |
| 165 EXPECT_TRUE(store_ptr->close_timer()->IsRunning()); | |
| 166 | |
| 167 // Take back a ref ptr and ensure that the actual close | |
| 168 // stops a running timer. | |
| 169 store = store_ptr; | |
| 170 factory->TestCloseBackingStore(store_ptr); | |
| 171 EXPECT_FALSE(store_ptr->close_timer()->IsRunning()); | |
| 172 }, | |
| 173 base::Unretained(context()))); | |
| 174 | |
| 175 RunAllBlockingPoolTasksUntilIdle(); | |
| 159 } | 176 } |
| 160 | 177 |
| 161 TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { | 178 TEST_F(IndexedDBFactoryTest, MemoryBackingStoreLifetime) { |
| 162 const Origin origin1(GURL("http://localhost:81")); | 179 context()->TaskRunner()->PostTask( |
| 163 const Origin origin2(GURL("http://localhost:82")); | 180 FROM_HERE, |
| 181 base::BindOnce( | |
| 182 [](IndexedDBContextImpl* context) { | |
| 164 | 183 |
| 165 scoped_refptr<IndexedDBBackingStore> mem_store1 = | 184 scoped_refptr<MockIDBFactory> factory = |
| 166 factory()->TestOpenBackingStore(origin1, base::FilePath()); | 185 base::MakeRefCounted<MockIDBFactory>(context); |
| 167 | 186 |
| 168 scoped_refptr<IndexedDBBackingStore> mem_store2 = | 187 const Origin origin1(GURL("http://localhost:81")); |
| 169 factory()->TestOpenBackingStore(origin1, base::FilePath()); | 188 const Origin origin2(GURL("http://localhost:82")); |
| 170 EXPECT_EQ(mem_store1.get(), mem_store2.get()); | |
| 171 | 189 |
| 172 scoped_refptr<IndexedDBBackingStore> mem_store3 = | 190 scoped_refptr<IndexedDBBackingStore> mem_store1 = |
| 173 factory()->TestOpenBackingStore(origin2, base::FilePath()); | 191 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 174 | 192 |
| 175 factory()->TestCloseBackingStore(mem_store1.get()); | 193 scoped_refptr<IndexedDBBackingStore> mem_store2 = |
| 176 factory()->TestCloseBackingStore(mem_store3.get()); | 194 factory->TestOpenBackingStore(origin1, base::FilePath()); |
| 195 EXPECT_EQ(mem_store1.get(), mem_store2.get()); | |
| 177 | 196 |
| 178 EXPECT_FALSE(mem_store1->HasOneRef()); | 197 scoped_refptr<IndexedDBBackingStore> mem_store3 = |
| 179 EXPECT_FALSE(mem_store2->HasOneRef()); | 198 factory->TestOpenBackingStore(origin2, base::FilePath()); |
| 180 EXPECT_FALSE(mem_store3->HasOneRef()); | |
| 181 | 199 |
| 182 clear_factory(); | 200 factory->TestCloseBackingStore(mem_store1.get()); |
| 183 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 | 201 factory->TestCloseBackingStore(mem_store3.get()); |
| 184 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 | |
| 185 EXPECT_TRUE(mem_store3->HasOneRef()); | |
| 186 | 202 |
| 187 mem_store2 = nullptr; | 203 EXPECT_FALSE(mem_store1->HasOneRef()); |
| 188 EXPECT_TRUE(mem_store1->HasOneRef()); | 204 EXPECT_FALSE(mem_store2->HasOneRef()); |
| 205 EXPECT_FALSE(mem_store3->HasOneRef()); | |
| 206 | |
| 207 factory = nullptr; | |
| 208 EXPECT_FALSE(mem_store1->HasOneRef()); // mem_store1 and 2 | |
| 209 EXPECT_FALSE(mem_store2->HasOneRef()); // mem_store1 and 2 | |
| 210 EXPECT_TRUE(mem_store3->HasOneRef()); | |
| 211 | |
| 212 mem_store2 = nullptr; | |
| 213 EXPECT_TRUE(mem_store1->HasOneRef()); | |
| 214 }, | |
| 215 base::Unretained(context()))); | |
| 216 | |
| 217 RunAllBlockingPoolTasksUntilIdle(); | |
| 189 } | 218 } |
| 190 | 219 |
| 191 TEST_F(IndexedDBFactoryTest, RejectLongOrigins) { | 220 TEST_F(IndexedDBFactoryTest, RejectLongOrigins) { |
| 192 base::ScopedTempDir temp_directory; | 221 context()->TaskRunner()->PostTask( |
| 193 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 222 FROM_HERE, |
| 194 const base::FilePath base_path = temp_directory.GetPath(); | 223 base::BindOnce( |
| 224 [](IndexedDBContextImpl* context) { | |
| 225 base::FilePath temp_dir = context->data_path().DirName(); | |
| 226 int limit = base::GetMaximumPathComponentLength(temp_dir); | |
| 227 EXPECT_GT(limit, 0); | |
| 195 | 228 |
| 196 int limit = base::GetMaximumPathComponentLength(base_path); | 229 scoped_refptr<MockIDBFactory> factory = |
| 197 EXPECT_GT(limit, 0); | 230 base::MakeRefCounted<MockIDBFactory>(context); |
| 198 | 231 |
| 199 std::string origin(limit + 1, 'x'); | 232 std::string origin(limit + 1, 'x'); |
| 200 Origin too_long_origin(GURL("http://" + origin + ":81/")); | 233 Origin too_long_origin(GURL("http://" + origin + ":81/")); |
| 201 scoped_refptr<IndexedDBBackingStore> diskStore1 = | 234 scoped_refptr<IndexedDBBackingStore> diskStore1 = |
| 202 factory()->TestOpenBackingStore(too_long_origin, base_path); | 235 factory->TestOpenBackingStore(too_long_origin, |
| 203 EXPECT_FALSE(diskStore1.get()); | 236 context->data_path()); |
| 237 EXPECT_FALSE(diskStore1.get()); | |
| 204 | 238 |
| 205 Origin ok_origin(GURL("http://someorigin.com:82/")); | 239 Origin ok_origin(GURL("http://someorigin.com:82/")); |
| 206 scoped_refptr<IndexedDBBackingStore> diskStore2 = | 240 scoped_refptr<IndexedDBBackingStore> diskStore2 = |
| 207 factory()->TestOpenBackingStore(ok_origin, base_path); | 241 factory->TestOpenBackingStore(ok_origin, context->data_path()); |
| 208 EXPECT_TRUE(diskStore2.get()); | 242 EXPECT_TRUE(diskStore2.get()); |
| 209 // We need a manual close or Windows can't delete the temp directory. | 243 // We need a manual close or Windows can't delete the temp |
| 210 factory()->TestCloseBackingStore(diskStore2.get()); | 244 // directory. |
| 245 factory->TestCloseBackingStore(diskStore2.get()); | |
| 246 }, | |
| 247 base::Unretained(context()))); | |
| 248 | |
| 249 RunAllBlockingPoolTasksUntilIdle(); | |
| 211 } | 250 } |
| 212 | 251 |
| 213 class DiskFullFactory : public IndexedDBFactoryImpl { | 252 class DiskFullFactory : public IndexedDBFactoryImpl { |
| 214 public: | 253 public: |
| 215 explicit DiskFullFactory(IndexedDBContextImpl* context) | 254 explicit DiskFullFactory(IndexedDBContextImpl* context) |
| 216 : IndexedDBFactoryImpl(context) {} | 255 : IndexedDBFactoryImpl(context) {} |
| 217 | 256 |
| 218 private: | 257 private: |
| 219 ~DiskFullFactory() override {} | 258 ~DiskFullFactory() override {} |
| 220 scoped_refptr<IndexedDBBackingStore> OpenBackingStore( | 259 scoped_refptr<IndexedDBBackingStore> OpenBackingStore( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 231 | 270 |
| 232 DISALLOW_COPY_AND_ASSIGN(DiskFullFactory); | 271 DISALLOW_COPY_AND_ASSIGN(DiskFullFactory); |
| 233 }; | 272 }; |
| 234 | 273 |
| 235 class LookingForQuotaErrorMockCallbacks : public IndexedDBCallbacks { | 274 class LookingForQuotaErrorMockCallbacks : public IndexedDBCallbacks { |
| 236 public: | 275 public: |
| 237 LookingForQuotaErrorMockCallbacks() | 276 LookingForQuotaErrorMockCallbacks() |
| 238 : IndexedDBCallbacks(nullptr, | 277 : IndexedDBCallbacks(nullptr, |
| 239 url::Origin(), | 278 url::Origin(), |
| 240 nullptr, | 279 nullptr, |
| 241 base::ThreadTaskRunnerHandle::Get()), | 280 base::SequencedTaskRunnerHandle::Get()), |
| 242 error_called_(false) {} | 281 error_called_(false) {} |
| 243 void OnError(const IndexedDBDatabaseError& error) override { | 282 void OnError(const IndexedDBDatabaseError& error) override { |
| 244 error_called_ = true; | 283 error_called_ = true; |
| 245 EXPECT_EQ(blink::kWebIDBDatabaseExceptionQuotaError, error.code()); | 284 EXPECT_EQ(blink::kWebIDBDatabaseExceptionQuotaError, error.code()); |
| 246 } | 285 } |
| 247 bool error_called() const { return error_called_; } | 286 bool error_called() const { return error_called_; } |
| 248 | 287 |
| 249 private: | 288 private: |
| 250 ~LookingForQuotaErrorMockCallbacks() override {} | 289 ~LookingForQuotaErrorMockCallbacks() override {} |
| 251 bool error_called_; | 290 bool error_called_; |
| 252 | 291 |
| 253 DISALLOW_COPY_AND_ASSIGN(LookingForQuotaErrorMockCallbacks); | 292 DISALLOW_COPY_AND_ASSIGN(LookingForQuotaErrorMockCallbacks); |
| 254 }; | 293 }; |
| 255 | 294 |
| 256 TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) { | 295 TEST_F(IndexedDBFactoryTest, QuotaErrorOnDiskFull) { |
| 257 const Origin origin(GURL("http://localhost:81")); | |
| 258 base::ScopedTempDir temp_directory; | |
| 259 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | |
| 260 | |
| 261 scoped_refptr<DiskFullFactory> factory = new DiskFullFactory(context()); | |
| 262 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = | 296 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks = |
| 263 new LookingForQuotaErrorMockCallbacks; | 297 base::MakeRefCounted<LookingForQuotaErrorMockCallbacks>(); |
| 264 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = | 298 scoped_refptr<IndexedDBDatabaseCallbacks> dummy_database_callbacks = |
| 265 new IndexedDBDatabaseCallbacks(nullptr, nullptr); | 299 base::MakeRefCounted<IndexedDBDatabaseCallbacks>(nullptr, nullptr); |
| 266 const base::string16 name(ASCIIToUTF16("name")); | 300 |
| 267 std::unique_ptr<IndexedDBPendingConnection> connection( | 301 context()->TaskRunner()->PostTask( |
| 268 base::MakeUnique<IndexedDBPendingConnection>( | 302 FROM_HERE, |
| 269 callbacks, dummy_database_callbacks, 0 /* child_process_id */, | 303 base::BindOnce( |
| 270 2 /* transaction_id */, 1 /* version */)); | 304 [](IndexedDBContextImpl* context, |
| 271 factory->Open(name, std::move(connection), nullptr /* request_context */, | 305 scoped_refptr<LookingForQuotaErrorMockCallbacks> callbacks, |
| 272 origin, temp_directory.GetPath()); | 306 scoped_refptr<IndexedDBDatabaseCallbacks> |
| 273 EXPECT_TRUE(callbacks->error_called()); | 307 dummy_database_callbacks) { |
| 308 | |
| 309 const Origin origin(GURL("http://localhost:81")); | |
| 310 scoped_refptr<DiskFullFactory> factory = | |
| 311 base::MakeRefCounted<DiskFullFactory>(context); | |
| 312 const base::string16 name(ASCIIToUTF16("name")); | |
| 313 std::unique_ptr<IndexedDBPendingConnection> connection( | |
| 314 base::MakeUnique<IndexedDBPendingConnection>( | |
| 315 callbacks, dummy_database_callbacks, | |
| 316 0 /* child_process_id */, 2 /* transaction_id */, | |
| 317 1 /* version */)); | |
| 318 factory->Open(name, std::move(connection), | |
| 319 nullptr /* request_context */, origin, | |
| 320 context->data_path()); | |
| 321 EXPECT_TRUE(callbacks->error_called()); | |
| 322 }, | |
| 323 base::Unretained(context()), std::move(callbacks), | |
| 324 std::move(dummy_database_callbacks))); | |
| 325 RunAllBlockingPoolTasksUntilIdle(); | |
| 274 } | 326 } |
| 275 | 327 |
| 276 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { | 328 TEST_F(IndexedDBFactoryTest, BackingStoreReleasedOnForcedClose) { |
| 277 const Origin origin(GURL("http://localhost:81")); | 329 context()->TaskRunner()->PostTask( |
| 278 | 330 FROM_HERE, |
| 279 base::ScopedTempDir temp_directory; | 331 base::BindOnce( |
| 280 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 332 [](IndexedDBContextImpl* context, |
| 281 | 333 scoped_refptr<MockIndexedDBCallbacks> callbacks, |
| 282 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 334 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks) { |
| 283 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 335 |
| 284 new MockIndexedDBDatabaseCallbacks()); | 336 scoped_refptr<MockIDBFactory> factory = |
| 285 const int64_t transaction_id = 1; | 337 base::MakeRefCounted<MockIDBFactory>(context); |
| 286 std::unique_ptr<IndexedDBPendingConnection> connection( | 338 |
| 287 base::MakeUnique<IndexedDBPendingConnection>( | 339 const Origin origin(GURL("http://localhost:81")); |
| 288 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | 340 const int64_t transaction_id = 1; |
| 289 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); | 341 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 290 factory()->Open(ASCIIToUTF16("db"), std::move(connection), | 342 base::MakeUnique<IndexedDBPendingConnection>( |
| 291 nullptr /* request_context */, origin, | 343 callbacks, db_callbacks, 0 /* child_process_id */, |
| 292 temp_directory.GetPath()); | 344 transaction_id, |
| 293 | 345 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 294 EXPECT_TRUE(callbacks->connection()); | 346 factory->Open(ASCIIToUTF16("db"), std::move(connection), |
| 295 | 347 nullptr /* request_context */, origin, |
| 296 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 348 context->data_path()); |
| 297 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 349 |
| 298 | 350 EXPECT_TRUE(callbacks->connection()); |
| 299 callbacks->connection()->ForceClose(); | 351 |
| 300 | 352 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 301 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 353 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); |
| 302 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 354 |
| 355 callbacks->connection()->ForceClose(); | |
| 356 | |
| 357 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); | |
| 358 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); | |
| 359 }, | |
| 360 base::Unretained(context()), | |
| 361 base::MakeRefCounted<MockIndexedDBCallbacks>(), | |
| 362 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>())); | |
| 363 RunAllBlockingPoolTasksUntilIdle(); | |
| 303 } | 364 } |
| 304 | 365 |
| 305 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { | 366 TEST_F(IndexedDBFactoryTest, BackingStoreReleaseDelayedOnClose) { |
| 306 const Origin origin(GURL("http://localhost:81")); | 367 context()->TaskRunner()->PostTask( |
| 307 | 368 FROM_HERE, |
| 308 base::ScopedTempDir temp_directory; | 369 base::BindOnce( |
| 309 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 370 [](IndexedDBContextImpl* context, |
| 310 | 371 scoped_refptr<MockIndexedDBCallbacks> callbacks, |
| 311 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 372 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks) { |
| 312 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 373 |
| 313 new MockIndexedDBDatabaseCallbacks()); | 374 scoped_refptr<MockIDBFactory> factory = |
| 314 const int64_t transaction_id = 1; | 375 base::MakeRefCounted<MockIDBFactory>(context); |
| 315 std::unique_ptr<IndexedDBPendingConnection> connection( | 376 |
| 316 base::MakeUnique<IndexedDBPendingConnection>( | 377 const Origin origin(GURL("http://localhost:81")); |
| 317 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | 378 const int64_t transaction_id = 1; |
| 318 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); | 379 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 319 factory()->Open(ASCIIToUTF16("db"), std::move(connection), | 380 base::MakeUnique<IndexedDBPendingConnection>( |
| 320 nullptr /* request_context */, origin, | 381 callbacks, db_callbacks, 0 /* child_process_id */, |
| 321 temp_directory.GetPath()); | 382 transaction_id, |
| 322 | 383 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 323 EXPECT_TRUE(callbacks->connection()); | 384 factory->Open(ASCIIToUTF16("db"), std::move(connection), |
| 324 IndexedDBBackingStore* store = | 385 nullptr /* request_context */, origin, |
| 325 callbacks->connection()->database()->backing_store(); | 386 context->data_path()); |
| 326 EXPECT_FALSE(store->HasOneRef()); // Factory and database. | 387 |
| 327 | 388 EXPECT_TRUE(callbacks->connection()); |
| 328 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 389 IndexedDBBackingStore* store = |
| 329 callbacks->connection()->Close(); | 390 callbacks->connection()->database()->backing_store(); |
| 330 EXPECT_TRUE(store->HasOneRef()); // Factory. | 391 EXPECT_FALSE(store->HasOneRef()); // Factory and database. |
| 331 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 392 |
| 332 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); | 393 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 333 EXPECT_TRUE(store->close_timer()->IsRunning()); | 394 callbacks->connection()->Close(); |
| 334 | 395 EXPECT_TRUE(store->HasOneRef()); // Factory. |
| 335 // Take a ref so it won't be destroyed out from under the test. | 396 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 336 scoped_refptr<IndexedDBBackingStore> store_ref = store; | 397 EXPECT_TRUE(factory->IsBackingStorePendingClose(origin)); |
| 337 // Now simulate shutdown, which should stop the timer. | 398 EXPECT_TRUE(store->close_timer()->IsRunning()); |
| 338 factory()->ContextDestroyed(); | 399 |
| 339 EXPECT_TRUE(store->HasOneRef()); // Local. | 400 // Take a ref so it won't be destroyed out from under the test. |
| 340 EXPECT_FALSE(store->close_timer()->IsRunning()); | 401 scoped_refptr<IndexedDBBackingStore> store_ref = store; |
| 341 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 402 // Now simulate shutdown, which should stop the timer. |
| 342 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 403 factory->ContextDestroyed(); |
| 404 EXPECT_TRUE(store->HasOneRef()); // Local. | |
| 405 EXPECT_FALSE(store->close_timer()->IsRunning()); | |
| 406 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); | |
| 407 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); | |
| 408 }, | |
| 409 base::Unretained(context()), | |
| 410 base::MakeRefCounted<MockIndexedDBCallbacks>(), | |
| 411 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>())); | |
| 412 RunAllBlockingPoolTasksUntilIdle(); | |
| 343 } | 413 } |
| 344 | 414 |
| 345 TEST_F(IndexedDBFactoryTest, DeleteDatabaseClosesBackingStore) { | 415 TEST_F(IndexedDBFactoryTest, DeleteDatabaseClosesBackingStore) { |
| 346 const Origin origin(GURL("http://localhost:81")); | 416 context()->TaskRunner()->PostTask( |
| 347 | 417 FROM_HERE, |
| 348 base::ScopedTempDir temp_directory; | 418 base::BindOnce( |
| 349 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 419 [](IndexedDBContextImpl* context, |
| 350 | 420 scoped_refptr<MockIndexedDBCallbacks> callbacks) { |
| 351 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 421 |
| 352 | 422 scoped_refptr<MockIDBFactory> factory = |
| 353 const bool expect_connection = false; | 423 base::MakeRefCounted<MockIDBFactory>(context); |
| 354 scoped_refptr<MockIndexedDBCallbacks> callbacks( | 424 |
| 355 new MockIndexedDBCallbacks(expect_connection)); | 425 const Origin origin(GURL("http://localhost:81")); |
| 356 factory()->DeleteDatabase(ASCIIToUTF16("db"), nullptr /* request_context */, | 426 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); |
| 357 callbacks, origin, temp_directory.GetPath(), | 427 |
| 358 false /* force_close */); | 428 factory->DeleteDatabase( |
| 359 | 429 ASCIIToUTF16("db"), nullptr /* request_context */, callbacks, |
| 360 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 430 origin, context->data_path(), false /* force_close */); |
| 361 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); | 431 |
| 362 | 432 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 363 // Now simulate shutdown, which should stop the timer. | 433 EXPECT_TRUE(factory->IsBackingStorePendingClose(origin)); |
| 364 factory()->ContextDestroyed(); | 434 |
| 365 | 435 // Now simulate shutdown, which should stop the timer. |
| 366 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 436 factory->ContextDestroyed(); |
| 367 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 437 |
| 438 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); | |
| 439 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); | |
| 440 | |
| 441 }, | |
| 442 base::Unretained(context()), | |
| 443 base::MakeRefCounted<MockIndexedDBCallbacks>( | |
| 444 false /*expect_connection*/))); | |
| 445 RunAllBlockingPoolTasksUntilIdle(); | |
| 368 } | 446 } |
| 369 | 447 |
| 370 TEST_F(IndexedDBFactoryTest, GetDatabaseNamesClosesBackingStore) { | 448 TEST_F(IndexedDBFactoryTest, GetDatabaseNamesClosesBackingStore) { |
| 371 const Origin origin(GURL("http://localhost:81")); | 449 context()->TaskRunner()->PostTask( |
| 372 | 450 FROM_HERE, |
| 373 base::ScopedTempDir temp_directory; | 451 base::BindOnce( |
| 374 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 452 [](IndexedDBContextImpl* context, |
| 375 | 453 scoped_refptr<MockIndexedDBCallbacks> callbacks) { |
| 376 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 454 scoped_refptr<MockIDBFactory> factory = |
| 377 | 455 base::MakeRefCounted<MockIDBFactory>(context); |
| 378 const bool expect_connection = false; | 456 |
| 379 scoped_refptr<MockIndexedDBCallbacks> callbacks( | 457 const Origin origin(GURL("http://localhost:81")); |
| 380 new MockIndexedDBCallbacks(expect_connection)); | 458 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); |
| 381 factory()->GetDatabaseNames(callbacks, origin, temp_directory.GetPath(), | 459 |
| 382 nullptr /* request_context */); | 460 factory->GetDatabaseNames(callbacks, origin, context->data_path(), |
| 383 | 461 nullptr /* request_context */); |
| 384 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 462 |
| 385 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); | 463 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 386 | 464 EXPECT_TRUE(factory->IsBackingStorePendingClose(origin)); |
| 387 // Now simulate shutdown, which should stop the timer. | 465 |
| 388 factory()->ContextDestroyed(); | 466 // Now simulate shutdown, which should stop the timer. |
| 389 | 467 factory->ContextDestroyed(); |
| 390 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 468 |
| 391 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 469 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); |
| 470 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); | |
| 471 | |
| 472 }, | |
| 473 base::Unretained(context()), | |
| 474 base::MakeRefCounted<MockIndexedDBCallbacks>( | |
| 475 false /*expect_connection*/))); | |
| 476 RunAllBlockingPoolTasksUntilIdle(); | |
| 392 } | 477 } |
| 393 | 478 |
| 394 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) { | 479 TEST_F(IndexedDBFactoryTest, ForceCloseReleasesBackingStore) { |
| 395 const Origin origin(GURL("http://localhost:81")); | 480 context()->TaskRunner()->PostTask( |
| 396 | 481 FROM_HERE, |
| 397 base::ScopedTempDir temp_directory; | 482 base::BindOnce( |
| 398 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 483 [](IndexedDBContextImpl* context, |
| 399 | 484 scoped_refptr<MockIndexedDBCallbacks> callbacks, |
| 400 scoped_refptr<MockIndexedDBCallbacks> callbacks(new MockIndexedDBCallbacks()); | 485 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks) { |
| 401 scoped_refptr<MockIndexedDBDatabaseCallbacks> db_callbacks( | 486 scoped_refptr<MockIDBFactory> factory = |
| 402 new MockIndexedDBDatabaseCallbacks()); | 487 base::MakeRefCounted<MockIDBFactory>(context); |
| 403 const int64_t transaction_id = 1; | 488 |
| 404 std::unique_ptr<IndexedDBPendingConnection> connection( | 489 const Origin origin(GURL("http://localhost:81")); |
| 405 base::MakeUnique<IndexedDBPendingConnection>( | 490 const int64_t transaction_id = 1; |
| 406 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | 491 std::unique_ptr<IndexedDBPendingConnection> connection( |
| 407 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); | 492 base::MakeUnique<IndexedDBPendingConnection>( |
| 408 factory()->Open(ASCIIToUTF16("db"), std::move(connection), | 493 callbacks, db_callbacks, 0 /* child_process_id */, |
| 409 nullptr /* request_context */, origin, | 494 transaction_id, |
| 410 temp_directory.GetPath()); | 495 IndexedDBDatabaseMetadata::DEFAULT_VERSION)); |
| 411 | 496 factory->Open(ASCIIToUTF16("db"), std::move(connection), |
| 412 EXPECT_TRUE(callbacks->connection()); | 497 nullptr /* request_context */, origin, |
| 413 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 498 context->data_path()); |
| 414 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 499 |
| 415 | 500 EXPECT_TRUE(callbacks->connection()); |
| 416 callbacks->connection()->Close(); | 501 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 417 | 502 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); |
| 418 EXPECT_TRUE(factory()->IsBackingStoreOpen(origin)); | 503 |
| 419 EXPECT_TRUE(factory()->IsBackingStorePendingClose(origin)); | 504 callbacks->connection()->Close(); |
| 420 | 505 |
| 421 factory()->ForceClose(origin); | 506 EXPECT_TRUE(factory->IsBackingStoreOpen(origin)); |
| 422 | 507 EXPECT_TRUE(factory->IsBackingStorePendingClose(origin)); |
| 423 EXPECT_FALSE(factory()->IsBackingStoreOpen(origin)); | 508 |
| 424 EXPECT_FALSE(factory()->IsBackingStorePendingClose(origin)); | 509 factory->ForceClose(origin); |
| 425 | 510 |
| 426 // Ensure it is safe if the store is not open. | 511 EXPECT_FALSE(factory->IsBackingStoreOpen(origin)); |
| 427 factory()->ForceClose(origin); | 512 EXPECT_FALSE(factory->IsBackingStorePendingClose(origin)); |
| 513 | |
| 514 // Ensure it is safe if the store is not open. | |
| 515 factory->ForceClose(origin); | |
| 516 | |
| 517 }, | |
| 518 base::Unretained(context()), | |
| 519 base::MakeRefCounted<MockIndexedDBCallbacks>(), | |
| 520 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>())); | |
| 521 RunAllBlockingPoolTasksUntilIdle(); | |
| 428 } | 522 } |
| 429 | 523 |
| 430 class UpgradeNeededCallbacks : public MockIndexedDBCallbacks { | 524 class UpgradeNeededCallbacks : public MockIndexedDBCallbacks { |
| 431 public: | 525 public: |
| 432 UpgradeNeededCallbacks() {} | 526 UpgradeNeededCallbacks() {} |
| 433 | 527 |
| 434 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, | 528 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, |
| 435 const IndexedDBDatabaseMetadata& metadata) override { | 529 const IndexedDBDatabaseMetadata& metadata) override { |
| 436 EXPECT_TRUE(connection_.get()); | 530 EXPECT_TRUE(connection_.get()); |
| 437 EXPECT_FALSE(connection.get()); | 531 EXPECT_FALSE(connection.get()); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 462 | 556 |
| 463 private: | 557 private: |
| 464 ~ErrorCallbacks() override {} | 558 ~ErrorCallbacks() override {} |
| 465 bool saw_error_; | 559 bool saw_error_; |
| 466 | 560 |
| 467 DISALLOW_COPY_AND_ASSIGN(ErrorCallbacks); | 561 DISALLOW_COPY_AND_ASSIGN(ErrorCallbacks); |
| 468 }; | 562 }; |
| 469 | 563 |
| 470 TEST_F(IndexedDBFactoryTest, DatabaseFailedOpen) { | 564 TEST_F(IndexedDBFactoryTest, DatabaseFailedOpen) { |
| 471 const Origin origin(GURL("http://localhost:81")); | 565 const Origin origin(GURL("http://localhost:81")); |
| 566 const base::string16 db_name(ASCIIToUTF16("db")); | |
| 567 const int64_t transaction_id = 1; | |
| 472 | 568 |
| 473 base::ScopedTempDir temp_directory; | 569 // These objects are retained across posted tasks, so despite being used |
| 474 ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); | 570 // exclusively on the IDB sequence. |
| 475 | 571 |
| 476 const base::string16 db_name(ASCIIToUTF16("db")); | 572 // Created and used on IDB sequence. |
| 477 const int64_t db_version = 2; | 573 scoped_refptr<MockIDBFactory> factory; |
| 478 const int64_t transaction_id = 1; | 574 // Created on IO thread, used on IDB sequence. |
| 479 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks( | 575 scoped_refptr<UpgradeNeededCallbacks> upgrade_callbacks = |
| 480 new MockIndexedDBDatabaseCallbacks()); | 576 base::MakeRefCounted<UpgradeNeededCallbacks>(); |
| 481 | 577 |
| 482 // Open at version 2, then close. | 578 context()->TaskRunner()->PostTask( |
| 483 { | 579 FROM_HERE, |
| 484 scoped_refptr<UpgradeNeededCallbacks> callbacks( | 580 base::BindOnce( |
| 485 new UpgradeNeededCallbacks()); | 581 [](IndexedDBContextImpl* context, |
| 486 std::unique_ptr<IndexedDBPendingConnection> connection( | 582 scoped_refptr<MockIDBFactory>* factory, |
| 487 base::MakeUnique<IndexedDBPendingConnection>( | 583 scoped_refptr<UpgradeNeededCallbacks>* upgrade_callbacks, |
| 488 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | 584 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks, |
| 489 db_version)); | 585 const base::string16& db_name, int64_t transaction_id, |
| 490 factory()->Open(db_name, std::move(connection), | 586 const Origin& origin) { |
| 491 nullptr /* request_context */, origin, | |
| 492 temp_directory.GetPath()); | |
| 493 EXPECT_TRUE(factory()->IsDatabaseOpen(origin, db_name)); | |
| 494 | 587 |
| 495 // Pump the message loop so the upgrade transaction can run. | 588 *factory = base::MakeRefCounted<MockIDBFactory>(context); |
| 496 base::RunLoop().RunUntilIdle(); | |
| 497 EXPECT_TRUE(callbacks->connection()); | |
| 498 callbacks->connection()->database()->Commit( | |
| 499 callbacks->connection()->GetTransaction(transaction_id)); | |
| 500 | 589 |
| 501 callbacks->connection()->Close(); | 590 // Open at version 2. |
| 502 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); | 591 const int64_t db_version = 2; |
| 503 } | 592 (*factory)->Open( |
| 593 db_name, | |
| 594 base::MakeUnique<IndexedDBPendingConnection>( | |
| 595 *upgrade_callbacks, db_callbacks, 0 /* child_process_id */, | |
| 596 transaction_id, db_version), | |
| 597 nullptr /* request_context */, origin, context->data_path()); | |
| 504 | 598 |
| 505 // Open at version < 2, which will fail; ensure factory doesn't retain | 599 EXPECT_TRUE((*factory)->IsDatabaseOpen(origin, db_name)); |
| 506 // the database object. | 600 }, |
| 507 { | 601 base::Unretained(context()), base::Unretained(&factory), |
| 508 scoped_refptr<ErrorCallbacks> callbacks(new ErrorCallbacks()); | 602 base::Unretained(&upgrade_callbacks), |
| 509 std::unique_ptr<IndexedDBPendingConnection> connection( | 603 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>(), db_name, |
| 510 base::MakeUnique<IndexedDBPendingConnection>( | 604 transaction_id, origin)); |
| 511 callbacks, db_callbacks, 0 /* child_process_id */, transaction_id, | |
| 512 db_version - 1)); | |
| 513 factory()->Open(db_name, std::move(connection), | |
| 514 nullptr /* request_context */, origin, | |
| 515 temp_directory.GetPath()); | |
| 516 EXPECT_TRUE(callbacks->saw_error()); | |
| 517 EXPECT_FALSE(factory()->IsDatabaseOpen(origin, db_name)); | |
| 518 } | |
| 519 | 605 |
| 520 // Terminate all pending-close timers. | 606 // Pump the message loop so the upgrade transaction can run. |
| 521 factory()->ForceClose(origin); | 607 RunAllBlockingPoolTasksUntilIdle(); |
| 608 | |
| 609 context()->TaskRunner()->PostTask( | |
| 610 FROM_HERE, | |
| 611 base::BindOnce( | |
| 612 [](IndexedDBContextImpl* context, | |
| 613 scoped_refptr<MockIDBFactory> factory, | |
| 614 scoped_refptr<UpgradeNeededCallbacks> upgrade_callbacks, | |
| 615 scoped_refptr<ErrorCallbacks> failed_open_callbacks, | |
| 616 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks, | |
| 617 const base::string16& db_name, int64_t transaction_id, | |
| 618 const Origin& origin) { | |
| 619 // Close the connection. | |
| 620 { | |
| 621 EXPECT_TRUE(upgrade_callbacks->connection()); | |
| 622 upgrade_callbacks->connection()->database()->Commit( | |
| 623 upgrade_callbacks->connection()->GetTransaction( | |
| 624 transaction_id)); | |
| 625 upgrade_callbacks->connection()->Close(); | |
| 626 EXPECT_FALSE(factory->IsDatabaseOpen(origin, db_name)); | |
| 627 } | |
| 628 | |
| 629 // Open at version < 2, which will fail; ensure factory doesn't | |
| 630 // retain the database object. | |
| 631 { | |
| 632 const int64_t db_version = 1; | |
| 633 std::unique_ptr<IndexedDBPendingConnection> connection( | |
| 634 base::MakeUnique<IndexedDBPendingConnection>( | |
| 635 failed_open_callbacks, db_callbacks, | |
| 636 0 /* child_process_id */, transaction_id, db_version)); | |
| 637 factory->Open(db_name, std::move(connection), | |
| 638 nullptr /* request_context */, origin, | |
| 639 context->data_path()); | |
| 640 EXPECT_TRUE(failed_open_callbacks->saw_error()); | |
| 641 EXPECT_FALSE(factory->IsDatabaseOpen(origin, db_name)); | |
| 642 } | |
| 643 | |
| 644 // Terminate all pending-close timers. | |
| 645 factory->ForceClose(origin); | |
| 646 }, | |
| 647 base::Unretained(context()), std::move(factory), | |
| 648 std::move(upgrade_callbacks), base::MakeRefCounted<ErrorCallbacks>(), | |
| 649 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>(), db_name, | |
| 650 transaction_id, origin)); | |
| 651 | |
| 652 RunAllBlockingPoolTasksUntilIdle(); | |
|
dmurph
2017/07/07 18:28:59
I'd be a little more comfortable if you do somethi
jsbell
2017/07/07 21:35:44
Per gab@ (email thread to scheduler-dev@) the sche
| |
| 522 } | 653 } |
| 523 | 654 |
| 524 namespace { | 655 namespace { |
| 525 | 656 |
| 526 class DataLossCallbacks final : public MockIndexedDBCallbacks { | 657 class DataLossCallbacks final : public MockIndexedDBCallbacks { |
| 527 public: | 658 public: |
| 528 blink::WebIDBDataLoss data_loss() const { return data_loss_; } | 659 blink::WebIDBDataLoss data_loss() const { return data_loss_; } |
| 529 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, | 660 void OnSuccess(std::unique_ptr<IndexedDBConnection> connection, |
| 530 const IndexedDBDatabaseMetadata& metadata) override { | 661 const IndexedDBDatabaseMetadata& metadata) override { |
| 531 if (!connection_) | 662 if (!connection_) |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 545 private: | 676 private: |
| 546 ~DataLossCallbacks() final {} | 677 ~DataLossCallbacks() final {} |
| 547 blink::WebIDBDataLoss data_loss_ = blink::kWebIDBDataLossNone; | 678 blink::WebIDBDataLoss data_loss_ = blink::kWebIDBDataLossNone; |
| 548 }; | 679 }; |
| 549 | 680 |
| 550 TEST_F(IndexedDBFactoryTest, DataFormatVersion) { | 681 TEST_F(IndexedDBFactoryTest, DataFormatVersion) { |
| 551 auto try_open = [this](const Origin& origin, | 682 auto try_open = [this](const Origin& origin, |
| 552 const IndexedDBDataFormatVersion& version) { | 683 const IndexedDBDataFormatVersion& version) { |
| 553 base::AutoReset<IndexedDBDataFormatVersion> override_version( | 684 base::AutoReset<IndexedDBDataFormatVersion> override_version( |
| 554 &IndexedDBDataFormatVersion::GetMutableCurrentForTesting(), version); | 685 &IndexedDBDataFormatVersion::GetMutableCurrentForTesting(), version); |
| 555 auto db_callbacks = base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>(); | 686 |
| 556 auto callbacks = base::MakeRefCounted<DataLossCallbacks>(); | 687 // These objects are retained across posted tasks, so despite being used |
| 688 // exclusively on the IDB sequence. | |
| 689 | |
| 690 // Created and used on IDB sequence. | |
| 691 scoped_refptr<MockIDBFactory> factory; | |
| 692 // Created on IO thread, used on IDB sequence. | |
| 693 scoped_refptr<DataLossCallbacks> callbacks = | |
| 694 base::MakeRefCounted<DataLossCallbacks>(); | |
| 695 | |
| 557 const int64_t transaction_id = 1; | 696 const int64_t transaction_id = 1; |
| 558 factory()->Open(ASCIIToUTF16("test_db"), | 697 blink::WebIDBDataLoss result; |
| 559 base::MakeUnique<IndexedDBPendingConnection>( | 698 |
| 560 callbacks, db_callbacks, 0 /* child_process_id */, | 699 context()->TaskRunner()->PostTask( |
| 561 transaction_id, 1 /* version */), | 700 FROM_HERE, |
| 562 nullptr /* request_context */, origin, | 701 base::BindOnce( |
| 563 context()->data_path()); | 702 [](IndexedDBContextImpl* context, |
| 564 base::RunLoop().RunUntilIdle(); | 703 scoped_refptr<MockIDBFactory>* factory, |
| 565 auto* connection = callbacks->connection(); | 704 scoped_refptr<DataLossCallbacks>* callbacks, |
| 566 EXPECT_TRUE(connection); | 705 scoped_refptr<IndexedDBDatabaseCallbacks> db_callbacks, |
| 567 connection->database()->Commit(connection->GetTransaction(transaction_id)); | 706 const Origin& origin, int64_t transaction_id) { |
| 568 connection->Close(); | 707 *factory = base::MakeRefCounted<MockIDBFactory>(context); |
| 569 factory()->ForceClose(origin); | 708 (*factory)->Open( |
| 570 return callbacks->data_loss(); | 709 ASCIIToUTF16("test_db"), |
| 710 base::MakeUnique<IndexedDBPendingConnection>( | |
| 711 *callbacks, db_callbacks, 0 /* child_process_id */, | |
| 712 transaction_id, 1 /* version */), | |
| 713 nullptr /* request_context */, origin, context->data_path()); | |
| 714 }, | |
| 715 base::Unretained(context()), base::Unretained(&factory), | |
| 716 base::Unretained(&callbacks), | |
| 717 base::MakeRefCounted<MockIndexedDBDatabaseCallbacks>(), origin, | |
| 718 transaction_id)); | |
| 719 RunAllBlockingPoolTasksUntilIdle(); | |
| 720 context()->TaskRunner()->PostTask( | |
| 721 FROM_HERE, | |
| 722 base::BindOnce( | |
| 723 [](scoped_refptr<MockIDBFactory> factory, | |
| 724 scoped_refptr<DataLossCallbacks> callbacks, const Origin& origin, | |
| 725 int64_t transaction_id, blink::WebIDBDataLoss* result) { | |
| 726 auto* connection = callbacks->connection(); | |
| 727 EXPECT_TRUE(connection); | |
| 728 connection->database()->Commit( | |
| 729 connection->GetTransaction(transaction_id)); | |
| 730 connection->Close(); | |
| 731 factory->ForceClose(origin); | |
| 732 *result = callbacks->data_loss(); | |
| 733 }, | |
| 734 std::move(factory), std::move(callbacks), origin, transaction_id, | |
| 735 base::Unretained(&result))); | |
| 736 RunAllBlockingPoolTasksUntilIdle(); | |
| 737 return result; | |
| 571 }; | 738 }; |
| 572 | 739 |
| 573 using blink::kWebIDBDataLossNone; | 740 using blink::kWebIDBDataLossNone; |
| 574 using blink::kWebIDBDataLossTotal; | 741 using blink::kWebIDBDataLossTotal; |
| 575 static const struct { | 742 static const struct { |
| 576 const char* origin; | 743 const char* origin; |
| 577 IndexedDBDataFormatVersion open_version_1; | 744 IndexedDBDataFormatVersion open_version_1; |
| 578 IndexedDBDataFormatVersion open_version_2; | 745 IndexedDBDataFormatVersion open_version_2; |
| 579 blink::WebIDBDataLoss expected_data_loss; | 746 blink::WebIDBDataLoss expected_data_loss; |
| 580 } kTestCases[] = { | 747 } kTestCases[] = { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 592 SCOPED_TRACE(test.origin); | 759 SCOPED_TRACE(test.origin); |
| 593 const Origin origin(GURL(test.origin)); | 760 const Origin origin(GURL(test.origin)); |
| 594 ASSERT_EQ(kWebIDBDataLossNone, try_open(origin, test.open_version_1)); | 761 ASSERT_EQ(kWebIDBDataLossNone, try_open(origin, test.open_version_1)); |
| 595 EXPECT_EQ(test.expected_data_loss, try_open(origin, test.open_version_2)); | 762 EXPECT_EQ(test.expected_data_loss, try_open(origin, test.open_version_2)); |
| 596 } | 763 } |
| 597 } | 764 } |
| 598 | 765 |
| 599 } // namespace | 766 } // namespace |
| 600 | 767 |
| 601 } // namespace content | 768 } // namespace content |
| OLD | NEW |