Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
| 23 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "components/history/core/browser/history_constants.h" | 25 #include "components/history/core/browser/history_constants.h" |
| 26 #include "components/history/core/browser/history_service.h" | 26 #include "components/history/core/browser/history_service.h" |
| 27 #include "components/history/core/browser/history_types.h" | 27 #include "components/history/core/browser/history_types.h" |
| 28 #include "components/precache/core/precache_database.h" | 28 #include "components/precache/core/precache_database.h" |
| 29 #include "components/precache/core/precache_switches.h" | 29 #include "components/precache/core/precache_switches.h" |
| 30 #include "components/precache/core/proto/unfinished_work.pb.h" | 30 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/storage_partition.h" | |
| 32 #include "content/public/test/test_browser_context.h" | 33 #include "content/public/test/test_browser_context.h" |
| 33 #include "content/public/test/test_browser_thread_bundle.h" | 34 #include "content/public/test/test_browser_thread_bundle.h" |
| 35 #include "net/base/test_completion_callback.h" | |
| 36 #include "net/disk_cache/simple/simple_backend_impl.h" | |
| 34 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
| 35 #include "net/http/http_response_info.h" | 38 #include "net/http/http_response_info.h" |
| 36 #include "net/http/http_status_code.h" | 39 #include "net/http/http_status_code.h" |
| 40 #include "net/test/gtest_util.h" | |
| 37 #include "net/url_request/test_url_fetcher_factory.h" | 41 #include "net/url_request/test_url_fetcher_factory.h" |
| 38 #include "net/url_request/url_request_status.h" | 42 #include "net/url_request/url_request_status.h" |
| 39 #include "net/url_request/url_request_test_util.h" | 43 #include "net/url_request/url_request_test_util.h" |
| 40 #include "testing/gmock/include/gmock/gmock.h" | 44 #include "testing/gmock/include/gmock/gmock.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
| 42 #include "url/gurl.h" | 46 #include "url/gurl.h" |
| 43 | 47 |
| 44 namespace precache { | 48 namespace precache { |
| 45 | 49 |
| 46 namespace { | 50 namespace { |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 // should still complete normally in this case. | 189 // should still complete normally in this case. |
| 186 factory_.SetFakeResponse(GURL(kConfigURL), "", | 190 factory_.SetFakeResponse(GURL(kConfigURL), "", |
| 187 net::HTTP_INTERNAL_SERVER_ERROR, | 191 net::HTTP_INTERNAL_SERVER_ERROR, |
| 188 net::URLRequestStatus::FAILED); | 192 net::URLRequestStatus::FAILED); |
| 189 precache_manager_.reset( | 193 precache_manager_.reset( |
| 190 new PrecacheManagerUnderTest( | 194 new PrecacheManagerUnderTest( |
| 191 &browser_context_, nullptr /* sync_service */, | 195 &browser_context_, nullptr /* sync_service */, |
| 192 &history_service_, db_path, std::move(precache_database))); | 196 &history_service_, db_path, std::move(precache_database))); |
| 193 base::RunLoop().RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
| 194 | 198 |
| 199 // Allow caches of any size to be used. | |
| 200 precache_manager_->min_cache_size_bytes_ = 0; | |
| 201 | |
| 195 info_.headers = new net::HttpResponseHeaders(""); | 202 info_.headers = new net::HttpResponseHeaders(""); |
| 196 } | 203 } |
| 197 | 204 |
| 198 void RecordStatsForPrecacheFetch(const GURL& url, | 205 void RecordStatsForPrecacheFetch(const GURL& url, |
| 199 const std::string& referrer_host, | 206 const std::string& referrer_host, |
| 200 const base::TimeDelta& latency, | 207 const base::TimeDelta& latency, |
| 201 const base::Time& fetch_time, | 208 const base::Time& fetch_time, |
| 202 const net::HttpResponseInfo& info, | 209 const net::HttpResponseInfo& info, |
| 203 int64_t size) { | 210 int64_t size) { |
| 204 precache_manager_->RecordStatsForFetch(url, GURL(referrer_host), latency, | 211 precache_manager_->RecordStatsForFetch(url, GURL(referrer_host), latency, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. | 249 base::RunLoop().RunUntilIdle(); // For PrecacheFetcher. |
| 243 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 250 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 244 EXPECT_TRUE(precache_callback_.was_on_done_called()); | 251 EXPECT_TRUE(precache_callback_.was_on_done_called()); |
| 245 | 252 |
| 246 std::multiset<GURL> expected_requested_urls; | 253 std::multiset<GURL> expected_requested_urls; |
| 247 expected_requested_urls.insert(GURL(kConfigURL)); | 254 expected_requested_urls.insert(GURL(kConfigURL)); |
| 248 expected_requested_urls.insert(GURL(kGoodManifestURL)); | 255 expected_requested_urls.insert(GURL(kGoodManifestURL)); |
| 249 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 256 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 250 } | 257 } |
| 251 | 258 |
| 259 TEST_F(PrecacheManagerTest, StartPrecachingWithGoodSizedCache) { | |
| 260 precache_manager_->min_cache_size_bytes_ = 1; | |
| 261 | |
| 262 // Let's store something in the cache so we pass the min_cache_size_bytes_ | |
| 263 // threshold. | |
| 264 disk_cache::Backend* cache_backend; | |
| 265 { | |
| 266 // Get the CacheBackend. | |
| 267 net::TestCompletionCallback cb; | |
| 268 net::HttpCache* cache = | |
| 269 content::BrowserContext::GetDefaultStoragePartition(&browser_context_) | |
| 270 ->GetURLRequestContext() | |
| 271 ->GetURLRequestContext() | |
| 272 ->http_transaction_factory() | |
| 273 ->GetCache(); | |
| 274 ASSERT_NE(nullptr, cache); | |
| 275 int rv = cache->GetBackend(&cache_backend, cb.callback()); | |
| 276 ASSERT_THAT(cb.GetResult(rv), net::test::IsOk()); | |
| 277 ASSERT_NE(nullptr, cache_backend); | |
| 278 ASSERT_EQ(cache_backend, cache->GetCurrentBackend()); | |
|
twifkak
2016/11/17 21:52:04
(nit/opt) These asserts just verify that the test
jamartin
2016/11/18 03:55:32
Done.
| |
| 279 } | |
| 280 disk_cache::Entry* entry = nullptr; | |
| 281 { | |
| 282 // Create a cache Entry. | |
| 283 net::TestCompletionCallback cb; | |
| 284 int rv = | |
| 285 content::BrowserContext::GetDefaultStoragePartition(&browser_context_) | |
| 286 ->GetURLRequestContext() | |
| 287 ->GetURLRequestContext() | |
| 288 ->http_transaction_factory() | |
| 289 ->GetCache() | |
| 290 ->GetCurrentBackend() | |
|
twifkak
2016/11/17 21:52:04
Use cache_backend from above?
jamartin
2016/11/18 03:55:32
Done.
| |
| 291 ->CreateEntry("key", &entry, cb.callback()); | |
| 292 ASSERT_THAT(cb.GetResult(rv), net::test::IsOk()); | |
| 293 ASSERT_NE(nullptr, entry); | |
| 294 } | |
| 295 { | |
| 296 // Store some data in the cache Entry. | |
| 297 const std::string data(precache_manager_->min_cache_size_bytes_, 'a'); | |
| 298 scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); | |
| 299 net::TestCompletionCallback cb; | |
| 300 int rv = entry->WriteData(0, 0, buffer.get(), buffer->size(), cb.callback(), | |
| 301 false); | |
| 302 entry->Close(); | |
| 303 ASSERT_EQ(buffer->size(), cb.GetResult(rv)); | |
| 304 } | |
| 305 { | |
| 306 // Make sure everything went according to plan. | |
| 307 net::TestCompletionCallback cb; | |
| 308 int rv = cache_backend->CalculateSizeOfAllEntries(cb.callback()); | |
| 309 ASSERT_LE(precache_manager_->min_cache_size_bytes_, cb.GetResult(rv)); | |
| 310 } | |
| 311 EXPECT_FALSE(precache_manager_->IsPrecaching()); | |
| 312 | |
| 313 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); | |
| 314 base::RunLoop().RunUntilIdle(); | |
| 315 | |
| 316 EXPECT_TRUE(precache_manager_->IsPrecaching()); | |
| 317 // Now it should be waiting for the top hosts. | |
| 318 } | |
| 319 | |
| 320 TEST_F(PrecacheManagerTest, StartPrecachingStopsOnSmallCaches) { | |
| 321 // We don't have any entry in the cache, so the reported cache_size = 0 and | |
| 322 // thus it will fall below the threshold of 1. | |
| 323 precache_manager_->min_cache_size_bytes_ = 1; | |
| 324 EXPECT_FALSE(precache_manager_->IsPrecaching()); | |
| 325 | |
| 326 precache_manager_->StartPrecaching(precache_callback_.GetCallback()); | |
| 327 base::RunLoop().RunUntilIdle(); | |
| 328 | |
| 329 EXPECT_FALSE(precache_manager_->IsPrecaching()); | |
| 330 EXPECT_TRUE(precache_callback_.was_on_done_called()); | |
| 331 EXPECT_TRUE(url_callback_.requested_urls().empty()); | |
| 332 } | |
| 333 | |
| 252 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { | 334 TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { |
| 253 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 335 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
| 254 new PrecacheUnfinishedWork()); | 336 new PrecacheUnfinishedWork()); |
| 255 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); | 337 unfinished_work->add_top_host()->set_hostname("evil-manifest.com"); |
| 256 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); | 338 unfinished_work->set_start_time(base::Time::Now().ToInternalValue()); |
| 257 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); | 339 precache_database_->SaveUnfinishedWork(std::move(unfinished_work)); |
| 258 | 340 |
| 259 EXPECT_FALSE(precache_manager_->IsPrecaching()); | 341 EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| 260 | 342 |
| 261 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK, | 343 factory_.SetFakeResponse(GURL(kConfigURL), "", net::HTTP_OK, |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 563 expected_histogram_count_map["Precache.Saved"] += 2; | 645 expected_histogram_count_map["Precache.Saved"] += 2; |
| 564 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; | 646 expected_histogram_count_map["Precache.TimeSinceLastPrecache"] += 2; |
| 565 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; | 647 expected_histogram_count_map["Precache.Saved.Freshness"] = 2; |
| 566 | 648 |
| 567 base::RunLoop().RunUntilIdle(); | 649 base::RunLoop().RunUntilIdle(); |
| 568 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), | 650 EXPECT_THAT(histograms_.GetTotalCountsForPrefix("Precache."), |
| 569 ContainerEq(expected_histogram_count_map)); | 651 ContainerEq(expected_histogram_count_map)); |
| 570 } | 652 } |
| 571 | 653 |
| 572 } // namespace precache | 654 } // namespace precache |
| OLD | NEW |