OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/cache_storage/cache_storage_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
19 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 19 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
20 #include "content/browser/fileapi/mock_url_request_delegate.h" | 20 #include "content/browser/fileapi/mock_url_request_delegate.h" |
21 #include "content/browser/quota/mock_quota_manager_proxy.h" | 21 #include "content/browser/quota/mock_quota_manager_proxy.h" |
22 #include "content/common/cache_storage/cache_storage_types.h" | 22 #include "content/common/cache_storage/cache_storage_types.h" |
23 #include "content/common/service_worker/service_worker_types.h" | 23 #include "content/common/service_worker/service_worker_types.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/common/referrer.h" | 25 #include "content/public/common/referrer.h" |
| 26 #include "content/public/test/mock_special_storage_policy.h" |
26 #include "content/public/test/test_browser_context.h" | 27 #include "content/public/test/test_browser_context.h" |
27 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
28 #include "net/base/test_completion_callback.h" | 29 #include "net/base/test_completion_callback.h" |
29 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
30 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
31 #include "net/url_request/url_request_job_factory_impl.h" | 32 #include "net/url_request/url_request_job_factory_impl.h" |
32 #include "storage/browser/blob/blob_data_builder.h" | 33 #include "storage/browser/blob/blob_data_builder.h" |
33 #include "storage/browser/blob/blob_data_handle.h" | 34 #include "storage/browser/blob/blob_data_handle.h" |
34 #include "storage/browser/blob/blob_data_snapshot.h" | 35 #include "storage/browser/blob/blob_data_snapshot.h" |
35 #include "storage/browser/blob/blob_storage_context.h" | 36 #include "storage/browser/blob/blob_storage_context.h" |
36 #include "storage/browser/blob/blob_url_request_job_factory.h" | 37 #include "storage/browser/blob/blob_url_request_job_factory.h" |
37 #include "storage/browser/quota/quota_manager_proxy.h" | 38 #include "storage/browser/quota/quota_manager_proxy.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 | 40 |
40 namespace content { | 41 namespace content { |
41 | 42 |
42 namespace { | 43 namespace { |
43 const char kTestData[] = "Hello World"; | 44 const char kTestData[] = "Hello World"; |
| 45 const char kOrigin[] = "http://example.com"; |
44 | 46 |
45 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns | 47 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns |
46 // the memory. | 48 // the memory. |
47 scoped_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( | 49 scoped_ptr<storage::BlobProtocolHandler> CreateMockBlobProtocolHandler( |
48 storage::BlobStorageContext* blob_storage_context) { | 50 storage::BlobStorageContext* blob_storage_context) { |
49 // The FileSystemContext and thread task runner are not actually used but a | 51 // The FileSystemContext and thread task runner are not actually used but a |
50 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. | 52 // task runner is needed to avoid a DCHECK in BlobURLRequestJob ctor. |
51 return make_scoped_ptr(new storage::BlobProtocolHandler( | 53 return make_scoped_ptr(new storage::BlobProtocolHandler( |
52 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); | 54 blob_storage_context, NULL, base::ThreadTaskRunnerHandle::Get().get())); |
53 } | 55 } |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 callback_error_(CACHE_STORAGE_OK), | 259 callback_error_(CACHE_STORAGE_OK), |
258 callback_closed_(false) {} | 260 callback_closed_(false) {} |
259 | 261 |
260 void SetUp() override { | 262 void SetUp() override { |
261 ChromeBlobStorageContext* blob_storage_context = | 263 ChromeBlobStorageContext* blob_storage_context = |
262 ChromeBlobStorageContext::GetFor(&browser_context_); | 264 ChromeBlobStorageContext::GetFor(&browser_context_); |
263 // Wait for chrome_blob_storage_context to finish initializing. | 265 // Wait for chrome_blob_storage_context to finish initializing. |
264 base::RunLoop().RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
265 blob_storage_context_ = blob_storage_context->context(); | 267 blob_storage_context_ = blob_storage_context->context(); |
266 | 268 |
| 269 if (!MemoryOnly()) |
| 270 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 271 |
| 272 quota_policy_ = new MockSpecialStoragePolicy; |
| 273 mock_quota_manager_ = new MockQuotaManager( |
| 274 MemoryOnly() /* is incognito */, temp_dir_.path(), |
| 275 base::ThreadTaskRunnerHandle::Get().get(), |
| 276 base::ThreadTaskRunnerHandle::Get().get(), quota_policy_.get()); |
| 277 mock_quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, |
| 278 1024 * 1024 * 100); |
| 279 |
267 quota_manager_proxy_ = new MockQuotaManagerProxy( | 280 quota_manager_proxy_ = new MockQuotaManagerProxy( |
268 nullptr, base::ThreadTaskRunnerHandle::Get().get()); | 281 mock_quota_manager_.get(), base::ThreadTaskRunnerHandle::Get().get()); |
269 | 282 |
270 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 283 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
271 url_request_job_factory_->SetProtocolHandler( | 284 url_request_job_factory_->SetProtocolHandler( |
272 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 285 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); |
273 | 286 |
274 net::URLRequestContext* url_request_context = | 287 net::URLRequestContext* url_request_context = |
275 browser_context_.GetRequestContext()->GetURLRequestContext(); | 288 browser_context_.GetRequestContext()->GetURLRequestContext(); |
276 | 289 |
277 url_request_context->set_job_factory(url_request_job_factory_.get()); | 290 url_request_context->set_job_factory(url_request_job_factory_.get()); |
278 | 291 |
279 CreateRequests(blob_storage_context); | 292 CreateRequests(blob_storage_context); |
280 | 293 |
281 if (!MemoryOnly()) | |
282 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
283 base::FilePath path = MemoryOnly() ? base::FilePath() : temp_dir_.path(); | |
284 | |
285 cache_ = make_scoped_refptr(new TestCacheStorageCache( | 294 cache_ = make_scoped_refptr(new TestCacheStorageCache( |
286 GURL("http://example.com"), path, browser_context_.GetRequestContext(), | 295 GURL(kOrigin), temp_dir_.path(), browser_context_.GetRequestContext(), |
287 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr())); | 296 quota_manager_proxy_, blob_storage_context->context()->AsWeakPtr())); |
288 } | 297 } |
289 | 298 |
290 void TearDown() override { | 299 void TearDown() override { |
291 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); | 300 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
292 base::RunLoop().RunUntilIdle(); | 301 base::RunLoop().RunUntilIdle(); |
293 } | 302 } |
294 | 303 |
295 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 304 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
296 ServiceWorkerHeaderMap headers; | 305 ServiceWorkerHeaderMap headers; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 EXPECT_FALSE(Keys()); | 516 EXPECT_FALSE(Keys()); |
508 } | 517 } |
509 | 518 |
510 virtual bool MemoryOnly() { return false; } | 519 virtual bool MemoryOnly() { return false; } |
511 | 520 |
512 protected: | 521 protected: |
513 base::ScopedTempDir temp_dir_; | 522 base::ScopedTempDir temp_dir_; |
514 TestBrowserContext browser_context_; | 523 TestBrowserContext browser_context_; |
515 TestBrowserThreadBundle browser_thread_bundle_; | 524 TestBrowserThreadBundle browser_thread_bundle_; |
516 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 525 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 526 scoped_refptr<MockSpecialStoragePolicy> quota_policy_; |
| 527 scoped_refptr<MockQuotaManager> mock_quota_manager_; |
517 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | 528 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
518 storage::BlobStorageContext* blob_storage_context_; | 529 storage::BlobStorageContext* blob_storage_context_; |
519 | 530 |
520 scoped_refptr<TestCacheStorageCache> cache_; | 531 scoped_refptr<TestCacheStorageCache> cache_; |
521 | 532 |
522 ServiceWorkerFetchRequest body_request_; | 533 ServiceWorkerFetchRequest body_request_; |
523 ServiceWorkerResponse body_response_; | 534 ServiceWorkerResponse body_response_; |
524 ServiceWorkerFetchRequest no_body_request_; | 535 ServiceWorkerFetchRequest no_body_request_; |
525 ServiceWorkerResponse no_body_response_; | 536 ServiceWorkerResponse no_body_response_; |
526 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 537 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); | 940 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); |
930 sum_delta += quota_manager_proxy_->last_notified_delta(); | 941 sum_delta += quota_manager_proxy_->last_notified_delta(); |
931 | 942 |
932 EXPECT_TRUE(Delete(no_body_request_)); | 943 EXPECT_TRUE(Delete(no_body_request_)); |
933 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); | 944 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); |
934 sum_delta += quota_manager_proxy_->last_notified_delta(); | 945 sum_delta += quota_manager_proxy_->last_notified_delta(); |
935 | 946 |
936 EXPECT_EQ(0, sum_delta); | 947 EXPECT_EQ(0, sum_delta); |
937 } | 948 } |
938 | 949 |
| 950 TEST_P(CacheStorageCacheTestP, PutObeysQuotaLimits) { |
| 951 mock_quota_manager_->SetQuota(GURL(kOrigin), storage::kStorageTypeTemporary, |
| 952 0); |
| 953 EXPECT_FALSE(Put(no_body_request_, no_body_response_)); |
| 954 EXPECT_EQ(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED, callback_error_); |
| 955 } |
| 956 |
939 TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) { | 957 TEST_F(CacheStorageCacheMemoryOnlyTest, MemoryBackedSize) { |
940 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 958 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
941 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); | 959 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
942 EXPECT_LT(0, cache_->MemoryBackedSize()); | 960 EXPECT_LT(0, cache_->MemoryBackedSize()); |
943 int64_t no_body_size = cache_->MemoryBackedSize(); | 961 int64_t no_body_size = cache_->MemoryBackedSize(); |
944 | 962 |
945 EXPECT_TRUE(Delete(no_body_request_)); | 963 EXPECT_TRUE(Delete(no_body_request_)); |
946 EXPECT_EQ(0, cache_->MemoryBackedSize()); | 964 EXPECT_EQ(0, cache_->MemoryBackedSize()); |
947 | 965 |
948 EXPECT_TRUE(Put(body_request_, body_response_)); | 966 EXPECT_TRUE(Put(body_request_, body_response_)); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1016 EXPECT_EQ(1, sequence_out); | 1034 EXPECT_EQ(1, sequence_out); |
1017 close_loop2->Run(); | 1035 close_loop2->Run(); |
1018 EXPECT_EQ(2, sequence_out); | 1036 EXPECT_EQ(2, sequence_out); |
1019 } | 1037 } |
1020 | 1038 |
1021 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, | 1039 INSTANTIATE_TEST_CASE_P(CacheStorageCacheTest, |
1022 CacheStorageCacheTestP, | 1040 CacheStorageCacheTestP, |
1023 ::testing::Values(false, true)); | 1041 ::testing::Values(false, true)); |
1024 | 1042 |
1025 } // namespace content | 1043 } // namespace content |
OLD | NEW |