| 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 "components/precache/content/precache_manager.h" | 5 #include "components/precache/content/precache_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); | 236 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); |
| 237 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); | 237 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); |
| 238 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); | 238 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); |
| 239 | 239 |
| 240 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 240 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 241 | 241 |
| 242 factory_.SetFakeResponse( | 242 factory_.SetFakeResponse( |
| 243 GURL(kEvilManifestURL), "", | 243 GURL(kEvilManifestURL), "", |
| 244 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 244 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 245 | 245 |
| 246 ASSERT_TRUE(precache_database_->GetLastPrecacheTimestamp().is_null()); |
| 247 |
| 246 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); | 248 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); |
| 247 EXPECT_TRUE(precache_manager_->IsPrecaching()); | 249 EXPECT_TRUE(precache_manager_->IsPrecaching()); |
| 248 | 250 |
| 249 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. | 251 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. |
| 250 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 252 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 251 EXPECT_TRUE(precache_callback_.was_on_done_called()); | 253 EXPECT_TRUE(precache_callback_.was_on_done_called()); |
| 252 | 254 |
| 255 // The LastPrecacheTimestamp has been set. |
| 256 EXPECT_FALSE(precache_database_->GetLastPrecacheTimestamp().is_null()); |
| 257 |
| 253 std::multiset<GURL> expected_requested_urls; | 258 std::multiset<GURL> expected_requested_urls; |
| 254 expected_requested_urls.insert(GURL(kConfigURL)); | 259 expected_requested_urls.insert(GURL(kConfigURL)); |
| 255 expected_requested_urls.insert(GURL(kEvilManifestURL)); | 260 expected_requested_urls.insert(GURL(kEvilManifestURL)); |
| 256 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 261 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 257 } | 262 } |
| 258 | 263 |
| 259 TEST_F(PrecacheManagerTest, | 264 TEST_F(PrecacheManagerTest, |
| 260 StartAndCancelPrecachingBeforeUnfinishedWorkRetrieved) { | 265 StartAndCancelPrecachingBeforeUnfinishedWorkRetrieved) { |
| 261 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 266 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 262 | 267 |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 | 503 |
| 499 // A fetch for the same URL as the expired precache was served from the cache, | 504 // A fetch for the same URL as the expired precache was served from the cache, |
| 500 // but it isn't reported as saved bytes because it had expired in the precache | 505 // but it isn't reported as saved bytes because it had expired in the precache |
| 501 // history. | 506 // history. |
| 502 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), | 507 precache_manager_->RecordStatsForFetch(GURL("http://old-fetch.com"), GURL(), |
| 503 base::TimeDelta(), kCurrentTime, 1000, | 508 base::TimeDelta(), kCurrentTime, 1000, |
| 504 true); | 509 true); |
| 505 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; | 510 expected_histogram_count_map["Precache.Fetch.TimeToComplete"]++; |
| 506 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; | 511 expected_histogram_count_map["Precache.Latency.NonPrefetch"]++; |
| 507 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; | 512 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"]++; |
| 513 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 1; |
| 508 | 514 |
| 509 base::RunLoop().RunUntilIdle(); | 515 base::RunLoop().RunUntilIdle(); |
| 510 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 516 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 511 ContainerEq(expected_histogram_count_map)); | 517 ContainerEq(expected_histogram_count_map)); |
| 512 | 518 |
| 513 // The other precaches should not have expired, so the following fetches from | 519 // The other precaches should not have expired, so the following fetches from |
| 514 // the cache should count as saved bytes. | 520 // the cache should count as saved bytes. |
| 515 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), GURL()
, | 521 precache_manager_->RecordStatsForFetch(GURL("http://recent-fetch.com"), GURL()
, |
| 516 base::TimeDelta(), kCurrentTime, 1000, | 522 base::TimeDelta(), kCurrentTime, 1000, |
| 517 true); | 523 true); |
| 518 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), | 524 precache_manager_->RecordStatsForFetch(GURL("http://yesterday-fetch.com"), |
| 519 GURL(), base::TimeDelta(), kCurrentTime, | 525 GURL(), base::TimeDelta(), kCurrentTime, |
| 520 1000, true); | 526 1000, true); |
| 521 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; | 527 expected_histogram_count_map["Precache.Latency.NonPrefetch"] += 2; |
| 522 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; | 528 expected_histogram_count_map["Precache.Latency.NonPrefetch.NonTopHosts"] += 2; |
| 523 expected_histogram_count_map["Precache.Saved"] += 2; | 529 expected_histogram_count_map["Precache.Saved"] += 2; |
| 530 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; |
| 524 | 531 |
| 525 base::RunLoop().RunUntilIdle(); | 532 base::RunLoop().RunUntilIdle(); |
| 526 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 533 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 527 ContainerEq(expected_histogram_count_map)); | 534 ContainerEq(expected_histogram_count_map)); |
| 528 } | 535 } |
| 529 | 536 |
| 530 } // namespace precache | 537 } // namespace precache |
| OLD | NEW |