| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/indexed_db/indexed_db_dispatcher_host.h" | 5 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/utf_offset_string_conversions.h" | 14 #include "base/strings/utf_offset_string_conversions.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "content/browser/browser_thread_impl.h" | 17 #include "content/browser/browser_thread_impl.h" |
| 18 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 18 #include "content/browser/indexed_db/indexed_db_callbacks.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_database_callbacks.h" | 20 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" |
| 21 #include "content/browser/indexed_db/indexed_db_factory.h" | 21 #include "content/browser/indexed_db/indexed_db_factory.h" |
| 22 #include "content/browser/indexed_db/indexed_db_pending_connection.h" | 22 #include "content/browser/indexed_db/indexed_db_pending_connection.h" |
| 23 #include "content/browser/indexed_db/mock_mojo_indexed_db_callbacks.h" | 23 #include "content/browser/indexed_db/mock_mojo_indexed_db_callbacks.h" |
| 24 #include "content/browser/indexed_db/mock_mojo_indexed_db_database_callbacks.h" | 24 #include "content/browser/indexed_db/mock_mojo_indexed_db_database_callbacks.h" |
| 25 #include "content/common/indexed_db/indexed_db.mojom.h" | 25 #include "content/common/indexed_db/indexed_db.mojom.h" |
| 26 #include "content/public/test/test_browser_context.h" | 26 #include "content/public/test/test_browser_context.h" |
| 27 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 28 #include "content/public/test/test_utils.h" |
| 28 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 29 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 29 #include "mojo/public/cpp/bindings/strong_associated_binding.h" | 30 #include "mojo/public/cpp/bindings/strong_associated_binding.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
| 31 #include "storage/browser/test/mock_quota_manager.h" | 32 #include "storage/browser/test/mock_quota_manager.h" |
| 32 #include "storage/browser/test/mock_quota_manager_proxy.h" | 33 #include "storage/browser/test/mock_quota_manager_proxy.h" |
| 33 #include "storage/browser/test/mock_special_storage_policy.h" | 34 #include "storage/browser/test/mock_special_storage_policy.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" | 37 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseExc
eption.h" |
| 37 #include "url/origin.h" | 38 #include "url/origin.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 MATCHER_P(MatchesIDBKey, key, "") { | 77 MATCHER_P(MatchesIDBKey, key, "") { |
| 77 return arg.Equals(key); | 78 return arg.Equals(key); |
| 78 } | 79 } |
| 79 | 80 |
| 80 typedef void (base::Closure::*ClosureRunFcn)() const &; | 81 typedef void (base::Closure::*ClosureRunFcn)() const &; |
| 81 | 82 |
| 82 static const char kDatabaseName[] = "db"; | 83 static const char kDatabaseName[] = "db"; |
| 83 static const char kOrigin[] = "https://www.example.com"; | 84 static const char kOrigin[] = "https://www.example.com"; |
| 84 static const int kFakeProcessId = 2; | 85 static const int kFakeProcessId = 2; |
| 85 static const int64_t kTemporaryQuota = 50 * 1024 * 1024; | 86 static const int64_t kTemporaryQuota = 50 * 1024 * 1024; |
| 86 static const int64_t kPersistantQuota = 50 * 1024 * 1024; | |
| 87 | 87 |
| 88 base::FilePath CreateAndReturnTempDir(base::ScopedTempDir* temp_dir) { | 88 base::FilePath CreateAndReturnTempDir(base::ScopedTempDir* temp_dir) { |
| 89 CHECK(temp_dir->CreateUniqueTempDir()); | 89 CHECK(temp_dir->CreateUniqueTempDir()); |
| 90 return temp_dir->GetPath(); | 90 return temp_dir->GetPath(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Stores data specific to a connection. | 93 // Stores data specific to a connection. |
| 94 struct TestDatabaseConnection { | 94 struct TestDatabaseConnection { |
| 95 TestDatabaseConnection(url::Origin origin, | 95 TestDatabaseConnection(url::Origin origin, |
| 96 base::string16 db_name, | 96 base::string16 db_name, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 *status_out = status; | 131 *status_out = status; |
| 132 callback.Run(); | 132 callback.Run(); |
| 133 } | 133 } |
| 134 | 134 |
| 135 } // namespace | 135 } // namespace |
| 136 | 136 |
| 137 class IndexedDBDispatcherHostTest : public testing::Test { | 137 class IndexedDBDispatcherHostTest : public testing::Test { |
| 138 public: | 138 public: |
| 139 IndexedDBDispatcherHostTest() | 139 IndexedDBDispatcherHostTest() |
| 140 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 140 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 141 idb_thread_(new base::Thread("IndexedDB")), | 141 special_storage_policy_( |
| 142 io_task_runner_( | 142 base::MakeRefCounted<MockSpecialStoragePolicy>()), |
| 143 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | 143 quota_manager_(base::MakeRefCounted<MockQuotaManager>( |
| 144 idb_task_runner_(idb_thread_->Start() ? idb_thread_->task_runner() | 144 false /*is_incognito*/, |
| 145 : nullptr), | 145 browser_context_.GetPath(), |
| 146 request_context_getter_( | 146 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), |
| 147 new net::TestURLRequestContextGetter(io_task_runner_)), | 147 base::ThreadTaskRunnerHandle::Get().get(), |
| 148 special_storage_policy_(new MockSpecialStoragePolicy()), | 148 special_storage_policy_)), |
| 149 quota_manager_(new MockQuotaManager(false, | 149 context_impl_(base::MakeRefCounted<IndexedDBContextImpl>( |
| 150 browser_context_.GetPath(), | 150 CreateAndReturnTempDir(&temp_dir_), |
| 151 io_task_runner_, | 151 special_storage_policy_, |
| 152 idb_task_runner_, | 152 quota_manager_->proxy())), |
| 153 special_storage_policy_)), | 153 host_(new IndexedDBDispatcherHost( |
| 154 quota_manager_proxy_(quota_manager_->proxy()), | 154 kFakeProcessId, |
| 155 context_impl_( | 155 base::MakeRefCounted<net::TestURLRequestContextGetter>( |
| 156 new IndexedDBContextImpl(CreateAndReturnTempDir(&temp_dir_), | 156 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
| 157 special_storage_policy_.get(), | 157 context_impl_, |
| 158 quota_manager_proxy_.get(), | 158 ChromeBlobStorageContext::GetFor(&browser_context_))) { |
| 159 idb_task_runner_.get())), | 159 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, |
| 160 blob_storage_(ChromeBlobStorageContext::GetFor(&browser_context_)), | |
| 161 host_(new IndexedDBDispatcherHost(kFakeProcessId, | |
| 162 request_context_getter_.get(), | |
| 163 context_impl_.get(), | |
| 164 blob_storage_.get())) { | |
| 165 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypePersistent, | |
| 166 kTemporaryQuota); | 160 kTemporaryQuota); |
| 167 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, | |
| 168 kPersistantQuota); | |
| 169 } | 161 } |
| 170 | 162 |
| 171 void TearDown() override { | 163 void TearDown() override { |
| 172 host_.reset(); | 164 host_.reset(); |
| 173 // In order for idb_thread_.Stop() to not cause thread/taskrunner checking | |
| 174 // errors, the handles must be deref'd before we join threads. This ensures | |
| 175 // classes that require destruction on the idb thread can be destructed | |
| 176 // correctly before scheduling on the the idb thread task runner turns into | |
| 177 // a no-op after thread join. | |
| 178 blob_storage_ = nullptr; | |
| 179 context_impl_ = nullptr; | 165 context_impl_ = nullptr; |
| 180 quota_manager_proxy_ = nullptr; | |
| 181 quota_manager_ = nullptr; | 166 quota_manager_ = nullptr; |
| 182 special_storage_policy_ = nullptr; | 167 RunAllBlockingPoolTasksUntilIdle(); |
| 183 request_context_getter_ = nullptr; | |
| 184 // This will run the idb task runner until idle, then join the threads. | |
| 185 idb_thread_->Stop(); | |
| 186 // File are leaked if this doesn't return true. | 168 // File are leaked if this doesn't return true. |
| 187 ASSERT_TRUE(temp_dir_.Delete()); | 169 ASSERT_TRUE(temp_dir_.Delete()); |
| 188 } | 170 } |
| 189 | 171 |
| 190 void SetUp() override { | 172 void SetUp() override { |
| 191 ASSERT_TRUE(idb_task_runner_); | |
| 192 FactoryAssociatedRequest request = | 173 FactoryAssociatedRequest request = |
| 193 ::mojo::MakeIsolatedRequest(&idb_mojo_factory_); | 174 ::mojo::MakeIsolatedRequest(&idb_mojo_factory_); |
| 194 host_->AddBinding(std::move(request)); | 175 host_->AddBinding(std::move(request)); |
| 195 } | 176 } |
| 196 | 177 |
| 197 protected: | 178 protected: |
| 198 TestBrowserThreadBundle thread_bundle_; | 179 TestBrowserThreadBundle thread_bundle_; |
| 199 TestBrowserContext browser_context_; | 180 TestBrowserContext browser_context_; |
| 200 | 181 |
| 201 base::ScopedTempDir temp_dir_; | 182 base::ScopedTempDir temp_dir_; |
| 202 std::unique_ptr<base::Thread> idb_thread_; | |
| 203 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 204 scoped_refptr<base::SingleThreadTaskRunner> idb_task_runner_; | |
| 205 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
| 206 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; | 183 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; |
| 207 scoped_refptr<MockQuotaManager> quota_manager_; | 184 scoped_refptr<MockQuotaManager> quota_manager_; |
| 208 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_; | |
| 209 scoped_refptr<IndexedDBContextImpl> context_impl_; | 185 scoped_refptr<IndexedDBContextImpl> context_impl_; |
| 210 scoped_refptr<ChromeBlobStorageContext> blob_storage_; | |
| 211 std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread> | 186 std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread> |
| 212 host_; | 187 host_; |
| 213 FactoryAssociatedPtr idb_mojo_factory_; | 188 FactoryAssociatedPtr idb_mojo_factory_; |
| 214 | 189 |
| 215 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest); | 190 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest); |
| 216 }; | 191 }; |
| 217 | 192 |
| 218 TEST_F(IndexedDBDispatcherHostTest, CloseConnectionBeforeUpgrade) { | 193 TEST_F(IndexedDBDispatcherHostTest, CloseConnectionBeforeUpgrade) { |
| 219 const int64_t kDBVersion = 1; | 194 const int64_t kDBVersion = 1; |
| 220 const int64_t kTransactionId = 1; | 195 const int64_t kTransactionId = 1; |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 idb_mojo_factory_->AbortTransactionsForDatabase( | 787 idb_mojo_factory_->AbortTransactionsForDatabase( |
| 813 origin, base::BindOnce(&StatusCallback, std::move(quit_closure), | 788 origin, base::BindOnce(&StatusCallback, std::move(quit_closure), |
| 814 &callback_result)); | 789 &callback_result)); |
| 815 | 790 |
| 816 loop.Run(); | 791 loop.Run(); |
| 817 } | 792 } |
| 818 EXPECT_EQ(::indexed_db::mojom::Status::OK, callback_result); | 793 EXPECT_EQ(::indexed_db::mojom::Status::OK, callback_result); |
| 819 } | 794 } |
| 820 | 795 |
| 821 } // namespace content | 796 } // namespace content |
| OLD | NEW |