| 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 27 matching lines...) Expand all Loading... |
| 124 private: | 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(TestDatabaseConnection); | 125 DISALLOW_COPY_AND_ASSIGN(TestDatabaseConnection); |
| 126 }; | 126 }; |
| 127 | 127 |
| 128 } // namespace | 128 } // namespace |
| 129 | 129 |
| 130 class IndexedDBDispatcherHostTest : public testing::Test { | 130 class IndexedDBDispatcherHostTest : public testing::Test { |
| 131 public: | 131 public: |
| 132 IndexedDBDispatcherHostTest() | 132 IndexedDBDispatcherHostTest() |
| 133 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 133 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 134 idb_thread_(new base::Thread("IndexedDB")), | 134 special_storage_policy_( |
| 135 io_task_runner_( | 135 base::MakeRefCounted<MockSpecialStoragePolicy>()), |
| 136 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), | 136 quota_manager_(base::MakeRefCounted<MockQuotaManager>( |
| 137 idb_task_runner_(idb_thread_->Start() ? idb_thread_->task_runner() | 137 false /*is_incognito*/, |
| 138 : nullptr), | 138 browser_context_.GetPath(), |
| 139 request_context_getter_( | 139 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), |
| 140 new net::TestURLRequestContextGetter(io_task_runner_)), | 140 base::ThreadTaskRunnerHandle::Get().get(), |
| 141 special_storage_policy_(new MockSpecialStoragePolicy()), | 141 special_storage_policy_)), |
| 142 quota_manager_(new MockQuotaManager(false, | 142 context_impl_(base::MakeRefCounted<IndexedDBContextImpl>( |
| 143 browser_context_.GetPath(), | 143 CreateAndReturnTempDir(&temp_dir_), |
| 144 io_task_runner_, | 144 special_storage_policy_, |
| 145 idb_task_runner_, | 145 quota_manager_->proxy())), |
| 146 special_storage_policy_)), | 146 host_(new IndexedDBDispatcherHost( |
| 147 quota_manager_proxy_(quota_manager_->proxy()), | 147 kFakeProcessId, |
| 148 context_impl_( | 148 base::MakeRefCounted<net::TestURLRequestContextGetter>( |
| 149 new IndexedDBContextImpl(CreateAndReturnTempDir(&temp_dir_), | 149 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
| 150 special_storage_policy_.get(), | 150 context_impl_, |
| 151 quota_manager_proxy_.get(), | 151 ChromeBlobStorageContext::GetFor(&browser_context_))) { |
| 152 idb_task_runner_.get())), | 152 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, |
| 153 blob_storage_(ChromeBlobStorageContext::GetFor(&browser_context_)), | |
| 154 host_(new IndexedDBDispatcherHost(kFakeProcessId, | |
| 155 request_context_getter_.get(), | |
| 156 context_impl_.get(), | |
| 157 blob_storage_.get())) { | |
| 158 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypePersistent, | |
| 159 kTemporaryQuota); | 153 kTemporaryQuota); |
| 160 quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, | |
| 161 kPersistantQuota); | |
| 162 } | 154 } |
| 163 | 155 |
| 164 void TearDown() override { | 156 void TearDown() override { |
| 165 host_.reset(); | 157 host_.reset(); |
| 166 // In order for idb_thread_.Stop() to not cause thread/taskrunner checking | |
| 167 // errors, the handles must be deref'd before we join threads. This ensures | |
| 168 // classes that require destruction on the idb thread can be destructed | |
| 169 // correctly before scheduling on the the idb thread task runner turns into | |
| 170 // a no-op after thread join. | |
| 171 blob_storage_ = nullptr; | |
| 172 context_impl_ = nullptr; | 158 context_impl_ = nullptr; |
| 173 quota_manager_proxy_ = nullptr; | |
| 174 quota_manager_ = nullptr; | 159 quota_manager_ = nullptr; |
| 175 special_storage_policy_ = nullptr; | 160 RunAllBlockingPoolTasksUntilIdle(); |
| 176 request_context_getter_ = nullptr; | |
| 177 // This will run the idb task runner until idle, then join the threads. | |
| 178 idb_thread_->Stop(); | |
| 179 // File are leaked if this doesn't return true. | 161 // File are leaked if this doesn't return true. |
| 180 ASSERT_TRUE(temp_dir_.Delete()); | 162 ASSERT_TRUE(temp_dir_.Delete()); |
| 181 } | 163 } |
| 182 | 164 |
| 183 void SetUp() override { | 165 void SetUp() override { |
| 184 ASSERT_TRUE(idb_task_runner_); | |
| 185 FactoryAssociatedRequest request = | 166 FactoryAssociatedRequest request = |
| 186 ::mojo::MakeIsolatedRequest(&idb_mojo_factory_); | 167 ::mojo::MakeIsolatedRequest(&idb_mojo_factory_); |
| 187 host_->AddBinding(std::move(request)); | 168 host_->AddBinding(std::move(request)); |
| 188 } | 169 } |
| 189 | 170 |
| 190 protected: | 171 protected: |
| 191 TestBrowserThreadBundle thread_bundle_; | 172 TestBrowserThreadBundle thread_bundle_; |
| 192 TestBrowserContext browser_context_; | 173 TestBrowserContext browser_context_; |
| 193 | 174 |
| 194 base::ScopedTempDir temp_dir_; | 175 base::ScopedTempDir temp_dir_; |
| 195 std::unique_ptr<base::Thread> idb_thread_; | |
| 196 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 197 scoped_refptr<base::SingleThreadTaskRunner> idb_task_runner_; | |
| 198 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
| 199 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; | 176 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_; |
| 200 scoped_refptr<MockQuotaManager> quota_manager_; | 177 scoped_refptr<MockQuotaManager> quota_manager_; |
| 201 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_; | |
| 202 scoped_refptr<IndexedDBContextImpl> context_impl_; | 178 scoped_refptr<IndexedDBContextImpl> context_impl_; |
| 203 scoped_refptr<ChromeBlobStorageContext> blob_storage_; | |
| 204 std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread> | 179 std::unique_ptr<IndexedDBDispatcherHost, BrowserThread::DeleteOnIOThread> |
| 205 host_; | 180 host_; |
| 206 FactoryAssociatedPtr idb_mojo_factory_; | 181 FactoryAssociatedPtr idb_mojo_factory_; |
| 207 | 182 |
| 208 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest); | 183 DISALLOW_COPY_AND_ASSIGN(IndexedDBDispatcherHostTest); |
| 209 }; | 184 }; |
| 210 | 185 |
| 211 TEST_F(IndexedDBDispatcherHostTest, CloseConnectionBeforeUpgrade) { | 186 TEST_F(IndexedDBDispatcherHostTest, CloseConnectionBeforeUpgrade) { |
| 212 const int64_t kDBVersion = 1; | 187 const int64_t kDBVersion = 1; |
| 213 const int64_t kTransactionId = 1; | 188 const int64_t kTransactionId = 1; |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 content::IndexedDBKey(base::UTF8ToUTF16("hello")), | 411 content::IndexedDBKey(base::UTF8ToUTF16("hello")), |
| 437 blink::kWebIDBPutModeAddOnly, | 412 blink::kWebIDBPutModeAddOnly, |
| 438 std::vector<content::IndexedDBIndexKeys>(), | 413 std::vector<content::IndexedDBIndexKeys>(), |
| 439 put_callbacks->CreateInterfacePtrAndBind()); | 414 put_callbacks->CreateInterfacePtrAndBind()); |
| 440 connection.database->Commit(kTransactionId); | 415 connection.database->Commit(kTransactionId); |
| 441 loop.Run(); | 416 loop.Run(); |
| 442 } | 417 } |
| 443 } | 418 } |
| 444 | 419 |
| 445 } // namespace content | 420 } // namespace content |
| OLD | NEW |