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_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 cache_manager_->EnumerateCaches( | 198 cache_manager_->EnumerateCaches( |
199 origin, base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, | 199 origin, base::Bind(&CacheStorageManagerTest::StringsAndErrorCallback, |
200 base::Unretained(this), base::Unretained(&loop))); | 200 base::Unretained(this), base::Unretained(&loop))); |
201 loop.Run(); | 201 loop.Run(); |
202 | 202 |
203 return callback_error_ == CACHE_STORAGE_OK; | 203 return callback_error_ == CACHE_STORAGE_OK; |
204 } | 204 } |
205 | 205 |
206 bool StorageMatch(const GURL& origin, | 206 bool StorageMatch(const GURL& origin, |
207 const std::string& cache_name, | 207 const std::string& cache_name, |
208 const GURL& url) { | 208 const GURL& url, |
209 std::unique_ptr<ServiceWorkerFetchRequest> request( | 209 const CacheStorageCacheQueryParams& match_params = |
210 new ServiceWorkerFetchRequest()); | 210 CacheStorageCacheQueryParams()) { |
211 request->url = url; | 211 ServiceWorkerFetchRequest request; |
| 212 request.url = url; |
| 213 return StorageMatchWithRequest(origin, cache_name, request, match_params); |
| 214 } |
| 215 |
| 216 bool StorageMatchWithRequest( |
| 217 const GURL& origin, |
| 218 const std::string& cache_name, |
| 219 const ServiceWorkerFetchRequest& request, |
| 220 const CacheStorageCacheQueryParams& match_params = |
| 221 CacheStorageCacheQueryParams()) { |
| 222 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = |
| 223 base::MakeUnique<ServiceWorkerFetchRequest>(request); |
| 224 |
212 base::RunLoop loop; | 225 base::RunLoop loop; |
213 cache_manager_->MatchCache( | 226 cache_manager_->MatchCache( |
214 origin, cache_name, std::move(request), | 227 origin, cache_name, std::move(unique_request), match_params, |
215 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 228 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
216 base::Unretained(this), base::Unretained(&loop))); | 229 base::Unretained(this), base::Unretained(&loop))); |
217 loop.Run(); | 230 loop.Run(); |
218 | 231 |
219 return callback_error_ == CACHE_STORAGE_OK; | 232 return callback_error_ == CACHE_STORAGE_OK; |
220 } | 233 } |
221 | 234 |
222 bool StorageMatchAll(const GURL& origin, const GURL& url) { | 235 bool StorageMatchAll(const GURL& origin, |
223 std::unique_ptr<ServiceWorkerFetchRequest> request( | 236 const GURL& url, |
224 new ServiceWorkerFetchRequest()); | 237 const CacheStorageCacheQueryParams& match_params = |
225 request->url = url; | 238 CacheStorageCacheQueryParams()) { |
| 239 ServiceWorkerFetchRequest request; |
| 240 request.url = url; |
| 241 return StorageMatchAllWithRequest(origin, request, match_params); |
| 242 } |
| 243 |
| 244 bool StorageMatchAllWithRequest( |
| 245 const GURL& origin, |
| 246 const ServiceWorkerFetchRequest& request, |
| 247 const CacheStorageCacheQueryParams& match_params = |
| 248 CacheStorageCacheQueryParams()) { |
| 249 std::unique_ptr<ServiceWorkerFetchRequest> unique_request = |
| 250 base::MakeUnique<ServiceWorkerFetchRequest>(request); |
226 base::RunLoop loop; | 251 base::RunLoop loop; |
227 cache_manager_->MatchAllCaches( | 252 cache_manager_->MatchAllCaches( |
228 origin, std::move(request), | 253 origin, std::move(unique_request), match_params, |
229 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 254 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
230 base::Unretained(this), base::Unretained(&loop))); | 255 base::Unretained(this), base::Unretained(&loop))); |
231 loop.Run(); | 256 loop.Run(); |
232 | 257 |
233 return callback_error_ == CACHE_STORAGE_OK; | 258 return callback_error_ == CACHE_STORAGE_OK; |
234 } | 259 } |
235 | 260 |
236 bool CachePut(CacheStorageCache* cache, const GURL& url) { | 261 bool CachePut(CacheStorageCache* cache, const GURL& url) { |
237 return CachePutWithStatusCode(cache, url, 200); | 262 ServiceWorkerFetchRequest request; |
| 263 request.url = url; |
| 264 |
| 265 return CachePutWithStatusCode(cache, request, 200); |
| 266 } |
| 267 |
| 268 bool CachePutWithRequestAndHeaders( |
| 269 CacheStorageCache* cache, |
| 270 const ServiceWorkerFetchRequest& request, |
| 271 const ServiceWorkerHeaderMap& response_headers) { |
| 272 return CachePutWithStatusCode(cache, request, 200, response_headers); |
238 } | 273 } |
239 | 274 |
240 bool CachePutWithStatusCode(CacheStorageCache* cache, | 275 bool CachePutWithStatusCode(CacheStorageCache* cache, |
241 const GURL& url, | 276 const ServiceWorkerFetchRequest& request, |
242 int status_code) { | 277 int status_code, |
243 ServiceWorkerFetchRequest request; | 278 const ServiceWorkerHeaderMap& response_headers = |
244 request.url = url; | 279 ServiceWorkerHeaderMap()) { |
245 | |
246 std::unique_ptr<storage::BlobDataBuilder> blob_data( | 280 std::unique_ptr<storage::BlobDataBuilder> blob_data( |
247 new storage::BlobDataBuilder(base::GenerateGUID())); | 281 new storage::BlobDataBuilder(base::GenerateGUID())); |
248 blob_data->AppendData(url.spec()); | 282 blob_data->AppendData(request.url.spec()); |
249 | 283 |
250 std::unique_ptr<storage::BlobDataHandle> blob_handle = | 284 std::unique_ptr<storage::BlobDataHandle> blob_handle = |
251 blob_storage_context_->AddFinishedBlob(blob_data.get()); | 285 blob_storage_context_->AddFinishedBlob(blob_data.get()); |
252 ServiceWorkerResponse response( | 286 ServiceWorkerResponse response( |
253 url, status_code, "OK", blink::WebServiceWorkerResponseTypeDefault, | 287 request.url, status_code, "OK", |
254 ServiceWorkerHeaderMap(), blob_handle->uuid(), url.spec().size(), | 288 blink::WebServiceWorkerResponseTypeDefault, response_headers, |
255 GURL(), blink::WebServiceWorkerResponseErrorUnknown, base::Time(), | 289 blob_handle->uuid(), request.url.spec().size(), GURL(), |
| 290 blink::WebServiceWorkerResponseErrorUnknown, base::Time(), |
256 false /* is_in_cache_storage */, | 291 false /* is_in_cache_storage */, |
257 std::string() /* cache_storage_cache_name */, | 292 std::string() /* cache_storage_cache_name */, |
258 ServiceWorkerHeaderList() /* cors_exposed_header_names */); | 293 ServiceWorkerHeaderList() /* cors_exposed_header_names */); |
259 | 294 |
260 CacheStorageBatchOperation operation; | 295 CacheStorageBatchOperation operation; |
261 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; | 296 operation.operation_type = CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT; |
262 operation.request = request; | 297 operation.request = request; |
263 operation.response = response; | 298 operation.response = response; |
264 | 299 |
265 base::RunLoop loop; | 300 base::RunLoop loop; |
266 cache->BatchOperation( | 301 cache->BatchOperation( |
267 std::vector<CacheStorageBatchOperation>(1, operation), | 302 std::vector<CacheStorageBatchOperation>(1, operation), |
268 base::Bind(&CacheStorageManagerTest::CachePutCallback, | 303 base::Bind(&CacheStorageManagerTest::CachePutCallback, |
269 base::Unretained(this), base::Unretained(&loop))); | 304 base::Unretained(this), base::Unretained(&loop))); |
270 loop.Run(); | 305 loop.Run(); |
271 | 306 |
272 return callback_error_ == CACHE_STORAGE_OK; | 307 return callback_error_ == CACHE_STORAGE_OK; |
273 } | 308 } |
274 | 309 |
275 bool CacheMatch(CacheStorageCache* cache, const GURL& url) { | 310 bool CacheMatch(CacheStorageCache* cache, const GURL& url) { |
276 std::unique_ptr<ServiceWorkerFetchRequest> request( | 311 std::unique_ptr<ServiceWorkerFetchRequest> request( |
277 new ServiceWorkerFetchRequest()); | 312 new ServiceWorkerFetchRequest()); |
278 request->url = url; | 313 request->url = url; |
279 base::RunLoop loop; | 314 base::RunLoop loop; |
280 cache->Match(std::move(request), | 315 cache->Match(std::move(request), CacheStorageCacheQueryParams(), |
281 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, | 316 base::Bind(&CacheStorageManagerTest::CacheMatchCallback, |
282 base::Unretained(this), base::Unretained(&loop))); | 317 base::Unretained(this), base::Unretained(&loop))); |
283 loop.Run(); | 318 loop.Run(); |
284 | 319 |
285 return callback_error_ == CACHE_STORAGE_OK; | 320 return callback_error_ == CACHE_STORAGE_OK; |
286 } | 321 } |
287 | 322 |
288 CacheStorage* CacheStorageForOrigin(const GURL& origin) { | 323 CacheStorage* CacheStorageForOrigin(const GURL& origin) { |
289 return cache_manager_->FindOrCreateCacheStorage(origin); | 324 return cache_manager_->FindOrCreateCacheStorage(origin); |
290 } | 325 } |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 | 627 |
593 // Do some busy work on a different cache to move the cache pool threads | 628 // Do some busy work on a different cache to move the cache pool threads |
594 // along and trigger the bug. | 629 // along and trigger the bug. |
595 EXPECT_TRUE(Open(origin1_, "bar")); | 630 EXPECT_TRUE(Open(origin1_, "bar")); |
596 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), kTestURL)); | 631 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), kTestURL)); |
597 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), kTestURL)); | 632 EXPECT_TRUE(CacheMatch(callback_cache_handle_->value(), kTestURL)); |
598 } | 633 } |
599 | 634 |
600 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { | 635 TEST_P(CacheStorageManagerTestP, StorageMatchAllEntryExistsTwice) { |
601 EXPECT_TRUE(Open(origin1_, "foo")); | 636 EXPECT_TRUE(Open(origin1_, "foo")); |
602 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_handle_->value(), | 637 ServiceWorkerFetchRequest request; |
603 GURL("http://example.com/foo"), 200)); | 638 request.url = GURL("http://example.com/foo"); |
| 639 EXPECT_TRUE( |
| 640 CachePutWithStatusCode(callback_cache_handle_->value(), request, 200)); |
604 EXPECT_TRUE(Open(origin1_, "bar")); | 641 EXPECT_TRUE(Open(origin1_, "bar")); |
605 EXPECT_TRUE(CachePutWithStatusCode(callback_cache_handle_->value(), | 642 EXPECT_TRUE( |
606 GURL("http://example.com/foo"), 201)); | 643 CachePutWithStatusCode(callback_cache_handle_->value(), request, 201)); |
607 | 644 |
608 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); | 645 EXPECT_TRUE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
609 | 646 |
610 // The caches need to be searched in order of creation, so verify that the | 647 // The caches need to be searched in order of creation, so verify that the |
611 // response came from the first cache. | 648 // response came from the first cache. |
612 EXPECT_EQ(200, callback_cache_handle_response_->status_code); | 649 EXPECT_EQ(200, callback_cache_handle_response_->status_code); |
613 } | 650 } |
614 | 651 |
615 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) { | 652 TEST_P(CacheStorageManagerTestP, StorageMatchInOneOfMany) { |
616 EXPECT_TRUE(Open(origin1_, "foo")); | 653 EXPECT_TRUE(Open(origin1_, "foo")); |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); | 959 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); |
923 } | 960 } |
924 | 961 |
925 TEST_P(CacheStorageManagerTestP, SizeThenCloseStorageAccessed) { | 962 TEST_P(CacheStorageManagerTestP, SizeThenCloseStorageAccessed) { |
926 EXPECT_EQ(0, GetSizeThenCloseAllCaches(origin1_)); | 963 EXPECT_EQ(0, GetSizeThenCloseAllCaches(origin1_)); |
927 // GetSizeThenCloseAllCaches is not part of the web API and should not notify | 964 // GetSizeThenCloseAllCaches is not part of the web API and should not notify |
928 // the quota manager of an access. | 965 // the quota manager of an access. |
929 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); | 966 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_accessed_count()); |
930 } | 967 } |
931 | 968 |
| 969 TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreSearch) { |
| 970 EXPECT_TRUE(Open(origin1_, "foo")); |
| 971 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 972 GURL("http://example.com/foo?bar"))); |
| 973 |
| 974 EXPECT_FALSE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"))); |
| 975 |
| 976 CacheStorageCacheQueryParams match_params; |
| 977 match_params.ignore_search = true; |
| 978 EXPECT_TRUE(StorageMatch(origin1_, "foo", GURL("http://example.com/foo"), |
| 979 match_params)); |
| 980 } |
| 981 |
| 982 TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreMethod) { |
| 983 GURL url = GURL("http://example.com/foo"); |
| 984 EXPECT_TRUE(Open(origin1_, "foo")); |
| 985 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), url)); |
| 986 |
| 987 ServiceWorkerFetchRequest post_request; |
| 988 post_request.url = url; |
| 989 post_request.method = "POST"; |
| 990 EXPECT_FALSE(StorageMatchWithRequest(origin1_, "foo", post_request)); |
| 991 |
| 992 CacheStorageCacheQueryParams match_params; |
| 993 match_params.ignore_method = true; |
| 994 EXPECT_TRUE( |
| 995 StorageMatchWithRequest(origin1_, "foo", post_request, match_params)); |
| 996 } |
| 997 |
| 998 TEST_P(CacheStorageManagerTestP, StorageMatch_IgnoreVary) { |
| 999 GURL url = GURL("http://example.com/foo"); |
| 1000 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1001 |
| 1002 ServiceWorkerFetchRequest request; |
| 1003 request.url = url; |
| 1004 request.headers["vary_foo"] = "foo"; |
| 1005 |
| 1006 ServiceWorkerHeaderMap response_headers; |
| 1007 response_headers["vary"] = "vary_foo"; |
| 1008 |
| 1009 EXPECT_TRUE(CachePutWithRequestAndHeaders(callback_cache_handle_->value(), |
| 1010 request, response_headers)); |
| 1011 EXPECT_TRUE(StorageMatchWithRequest(origin1_, "foo", request)); |
| 1012 |
| 1013 request.headers["vary_foo"] = "bar"; |
| 1014 EXPECT_FALSE(StorageMatchWithRequest(origin1_, "foo", request)); |
| 1015 |
| 1016 CacheStorageCacheQueryParams match_params; |
| 1017 match_params.ignore_vary = true; |
| 1018 EXPECT_TRUE(StorageMatchWithRequest(origin1_, "foo", request, match_params)); |
| 1019 } |
| 1020 |
| 1021 TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreSearch) { |
| 1022 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1023 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), |
| 1024 GURL("http://example.com/foo?bar"))); |
| 1025 |
| 1026 EXPECT_FALSE(StorageMatchAll(origin1_, GURL("http://example.com/foo"))); |
| 1027 |
| 1028 CacheStorageCacheQueryParams match_params; |
| 1029 match_params.ignore_search = true; |
| 1030 EXPECT_TRUE( |
| 1031 StorageMatchAll(origin1_, GURL("http://example.com/foo"), match_params)); |
| 1032 } |
| 1033 |
| 1034 TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreMethod) { |
| 1035 GURL url = GURL("http://example.com/foo"); |
| 1036 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1037 EXPECT_TRUE(CachePut(callback_cache_handle_->value(), url)); |
| 1038 |
| 1039 ServiceWorkerFetchRequest post_request; |
| 1040 post_request.url = url; |
| 1041 post_request.method = "POST"; |
| 1042 EXPECT_FALSE(StorageMatchAllWithRequest(origin1_, post_request)); |
| 1043 |
| 1044 CacheStorageCacheQueryParams match_params; |
| 1045 match_params.ignore_method = true; |
| 1046 EXPECT_TRUE(StorageMatchAllWithRequest(origin1_, post_request, match_params)); |
| 1047 } |
| 1048 |
| 1049 TEST_P(CacheStorageManagerTestP, StorageMatchAll_IgnoreVary) { |
| 1050 GURL url = GURL("http://example.com/foo"); |
| 1051 EXPECT_TRUE(Open(origin1_, "foo")); |
| 1052 |
| 1053 ServiceWorkerFetchRequest request; |
| 1054 request.url = url; |
| 1055 request.headers["vary_foo"] = "foo"; |
| 1056 |
| 1057 ServiceWorkerHeaderMap response_headers; |
| 1058 response_headers["vary"] = "vary_foo"; |
| 1059 |
| 1060 EXPECT_TRUE(CachePutWithRequestAndHeaders(callback_cache_handle_->value(), |
| 1061 request, response_headers)); |
| 1062 EXPECT_TRUE(StorageMatchAllWithRequest(origin1_, request)); |
| 1063 |
| 1064 request.headers["vary_foo"] = "bar"; |
| 1065 EXPECT_FALSE(StorageMatchAllWithRequest(origin1_, request)); |
| 1066 |
| 1067 CacheStorageCacheQueryParams match_params; |
| 1068 match_params.ignore_vary = true; |
| 1069 EXPECT_TRUE(StorageMatchAllWithRequest(origin1_, request, match_params)); |
| 1070 } |
| 1071 |
932 class CacheStorageMigrationTest : public CacheStorageManagerTest { | 1072 class CacheStorageMigrationTest : public CacheStorageManagerTest { |
933 protected: | 1073 protected: |
934 CacheStorageMigrationTest() : cache1_("foo"), cache2_("bar") {} | 1074 CacheStorageMigrationTest() : cache1_("foo"), cache2_("bar") {} |
935 | 1075 |
936 void SetUp() override { | 1076 void SetUp() override { |
937 CacheStorageManagerTest::SetUp(); | 1077 CacheStorageManagerTest::SetUp(); |
938 | 1078 |
939 // Populate a cache, then move it to the "legacy" location | 1079 // Populate a cache, then move it to the "legacy" location |
940 // so that tests can verify the results of migration. | 1080 // so that tests can verify the results of migration. |
941 legacy_path_ = CacheStorageManager::ConstructLegacyOriginPath( | 1081 legacy_path_ = CacheStorageManager::ConstructLegacyOriginPath( |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1299 | 1439 |
1300 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, | 1440 INSTANTIATE_TEST_CASE_P(CacheStorageManagerTests, |
1301 CacheStorageManagerTestP, | 1441 CacheStorageManagerTestP, |
1302 ::testing::Values(false, true)); | 1442 ::testing::Values(false, true)); |
1303 | 1443 |
1304 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, | 1444 INSTANTIATE_TEST_CASE_P(CacheStorageQuotaClientTests, |
1305 CacheStorageQuotaClientTestP, | 1445 CacheStorageQuotaClientTestP, |
1306 ::testing::Values(false, true)); | 1446 ::testing::Values(false, true)); |
1307 | 1447 |
1308 } // namespace content | 1448 } // namespace content |
OLD | NEW |