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 "chrome/browser/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
6 | 6 |
7 #include <iostream> | 7 #include <iostream> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 using testing::SetArgPointee; | 34 using testing::SetArgPointee; |
35 using testing::StrictMock; | 35 using testing::StrictMock; |
36 using testing::UnorderedElementsAre; | 36 using testing::UnorderedElementsAre; |
37 | 37 |
38 namespace predictors { | 38 namespace predictors { |
39 | 39 |
40 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 40 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
41 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 41 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
42 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; | 42 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; |
43 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; | 43 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; |
| 44 using ManifestDataMap = ResourcePrefetchPredictorTables::ManifestDataMap; |
44 | 45 |
45 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( | 46 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
46 const char* headers) { | 47 const char* headers) { |
47 return make_scoped_refptr(new net::HttpResponseHeaders( | 48 return make_scoped_refptr(new net::HttpResponseHeaders( |
48 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); | 49 net::HttpUtil::AssembleRawHeaders(headers, strlen(headers)))); |
49 } | 50 } |
50 | 51 |
51 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { | 52 class EmptyURLRequestDelegate : public net::URLRequest::Delegate { |
52 void OnResponseStarted(net::URLRequest* request, int net_error) override {} | 53 void OnResponseStarted(net::URLRequest* request, int net_error) override {} |
53 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} | 54 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {} |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 private: | 124 private: |
124 net::HttpResponseInfo response_info_; | 125 net::HttpResponseInfo response_info_; |
125 std::string mime_type_; | 126 std::string mime_type_; |
126 }; | 127 }; |
127 | 128 |
128 class MockResourcePrefetchPredictorTables | 129 class MockResourcePrefetchPredictorTables |
129 : public ResourcePrefetchPredictorTables { | 130 : public ResourcePrefetchPredictorTables { |
130 public: | 131 public: |
131 MockResourcePrefetchPredictorTables() { } | 132 MockResourcePrefetchPredictorTables() { } |
132 | 133 |
133 MOCK_METHOD4(GetAllData, | 134 MOCK_METHOD5(GetAllData, |
134 void(PrefetchDataMap* url_data_map, | 135 void(PrefetchDataMap* url_data_map, |
135 PrefetchDataMap* host_data_map, | 136 PrefetchDataMap* host_data_map, |
136 RedirectDataMap* url_redirect_data_map, | 137 RedirectDataMap* url_redirect_data_map, |
137 RedirectDataMap* host_redirect_data_map)); | 138 RedirectDataMap* host_redirect_data_map, |
| 139 ManifestDataMap* manifest_data_map)); |
138 MOCK_METHOD4(UpdateData, | 140 MOCK_METHOD4(UpdateData, |
139 void(const PrefetchData& url_data, | 141 void(const PrefetchData& url_data, |
140 const PrefetchData& host_data, | 142 const PrefetchData& host_data, |
141 const RedirectData& url_redirect_data, | 143 const RedirectData& url_redirect_data, |
142 const RedirectData& host_redirect_data)); | 144 const RedirectData& host_redirect_data)); |
143 MOCK_METHOD2(DeleteResourceData, | 145 MOCK_METHOD2(DeleteResourceData, |
144 void(const std::vector<std::string>& urls, | 146 void(const std::vector<std::string>& urls, |
145 const std::vector<std::string>& hosts)); | 147 const std::vector<std::string>& hosts)); |
146 MOCK_METHOD2(DeleteSingleResourceDataPoint, | 148 MOCK_METHOD2(DeleteSingleResourceDataPoint, |
147 void(const std::string& key, PrefetchKeyType key_type)); | 149 void(const std::string& key, PrefetchKeyType key_type)); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 std::unique_ptr<TestingProfile> profile_; | 252 std::unique_ptr<TestingProfile> profile_; |
251 net::TestURLRequestContext url_request_context_; | 253 net::TestURLRequestContext url_request_context_; |
252 | 254 |
253 std::unique_ptr<ResourcePrefetchPredictor> predictor_; | 255 std::unique_ptr<ResourcePrefetchPredictor> predictor_; |
254 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables>> mock_tables_; | 256 scoped_refptr<StrictMock<MockResourcePrefetchPredictorTables>> mock_tables_; |
255 | 257 |
256 PrefetchDataMap test_url_data_; | 258 PrefetchDataMap test_url_data_; |
257 PrefetchDataMap test_host_data_; | 259 PrefetchDataMap test_host_data_; |
258 RedirectDataMap test_url_redirect_data_; | 260 RedirectDataMap test_url_redirect_data_; |
259 RedirectDataMap test_host_redirect_data_; | 261 RedirectDataMap test_host_redirect_data_; |
| 262 ManifestDataMap test_manifest_data_; |
260 PrefetchData empty_resource_data_; | 263 PrefetchData empty_resource_data_; |
261 RedirectData empty_redirect_data_; | 264 RedirectData empty_redirect_data_; |
262 | 265 |
263 MockURLRequestJobFactory url_request_job_factory_; | 266 MockURLRequestJobFactory url_request_job_factory_; |
264 EmptyURLRequestDelegate url_request_delegate_; | 267 EmptyURLRequestDelegate url_request_delegate_; |
265 | 268 |
266 std::unique_ptr<base::HistogramTester> histogram_tester_; | 269 std::unique_ptr<base::HistogramTester> histogram_tester_; |
267 }; | 270 }; |
268 | 271 |
269 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() | 272 ResourcePrefetchPredictorTest::ResourcePrefetchPredictorTest() |
(...skipping 16 matching lines...) Expand all Loading... |
286 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( | 289 EXPECT_TRUE(HistoryServiceFactory::GetForProfile( |
287 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); | 290 profile_.get(), ServiceAccessType::EXPLICIT_ACCESS)); |
288 // Initialize the predictor with empty data. | 291 // Initialize the predictor with empty data. |
289 ResetPredictor(); | 292 ResetPredictor(); |
290 EXPECT_EQ(predictor_->initialization_state_, | 293 EXPECT_EQ(predictor_->initialization_state_, |
291 ResourcePrefetchPredictor::NOT_INITIALIZED); | 294 ResourcePrefetchPredictor::NOT_INITIALIZED); |
292 EXPECT_CALL(*mock_tables_.get(), | 295 EXPECT_CALL(*mock_tables_.get(), |
293 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 296 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
294 Pointee(ContainerEq(PrefetchDataMap())), | 297 Pointee(ContainerEq(PrefetchDataMap())), |
295 Pointee(ContainerEq(RedirectDataMap())), | 298 Pointee(ContainerEq(RedirectDataMap())), |
296 Pointee(ContainerEq(RedirectDataMap())))); | 299 Pointee(ContainerEq(RedirectDataMap())), |
| 300 Pointee(ContainerEq(ManifestDataMap())))); |
297 InitializePredictor(); | 301 InitializePredictor(); |
298 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); | 302 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); |
299 EXPECT_EQ(predictor_->initialization_state_, | 303 EXPECT_EQ(predictor_->initialization_state_, |
300 ResourcePrefetchPredictor::INITIALIZED); | 304 ResourcePrefetchPredictor::INITIALIZED); |
301 | 305 |
302 url_request_context_.set_job_factory(&url_request_job_factory_); | 306 url_request_context_.set_job_factory(&url_request_job_factory_); |
303 | 307 |
304 histogram_tester_.reset(new base::HistogramTester()); | 308 histogram_tester_.reset(new base::HistogramTester()); |
305 } | 309 } |
306 | 310 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 | 498 |
495 // Tests that the history and the db tables data are loaded correctly. | 499 // Tests that the history and the db tables data are loaded correctly. |
496 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { | 500 TEST_F(ResourcePrefetchPredictorTest, LazilyInitializeWithData) { |
497 AddUrlToHistory("http://www.google.com/", 4); | 501 AddUrlToHistory("http://www.google.com/", 4); |
498 AddUrlToHistory("http://www.yahoo.com/", 2); | 502 AddUrlToHistory("http://www.yahoo.com/", 2); |
499 | 503 |
500 EXPECT_CALL(*mock_tables_.get(), | 504 EXPECT_CALL(*mock_tables_.get(), |
501 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 505 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
502 Pointee(ContainerEq(PrefetchDataMap())), | 506 Pointee(ContainerEq(PrefetchDataMap())), |
503 Pointee(ContainerEq(RedirectDataMap())), | 507 Pointee(ContainerEq(RedirectDataMap())), |
504 Pointee(ContainerEq(RedirectDataMap())))) | 508 Pointee(ContainerEq(RedirectDataMap())), |
| 509 Pointee(ContainerEq(ManifestDataMap())))) |
505 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), | 510 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), |
506 SetArgPointee<1>(test_host_data_), | 511 SetArgPointee<1>(test_host_data_), |
507 SetArgPointee<2>(test_url_redirect_data_), | 512 SetArgPointee<2>(test_url_redirect_data_), |
508 SetArgPointee<3>(test_host_redirect_data_))); | 513 SetArgPointee<3>(test_host_redirect_data_), |
| 514 SetArgPointee<4>(test_manifest_data_))); |
509 | 515 |
510 ResetPredictor(); | 516 ResetPredictor(); |
511 InitializePredictor(); | 517 InitializePredictor(); |
512 | 518 |
513 // Test that the internal variables correctly initialized. | 519 // Test that the internal variables correctly initialized. |
514 EXPECT_EQ(predictor_->initialization_state_, | 520 EXPECT_EQ(predictor_->initialization_state_, |
515 ResourcePrefetchPredictor::INITIALIZED); | 521 ResourcePrefetchPredictor::INITIALIZED); |
516 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); | 522 EXPECT_TRUE(predictor_->inflight_navigations_.empty()); |
517 | 523 |
518 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); | 524 EXPECT_EQ(test_url_data_, *predictor_->url_table_cache_); |
519 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); | 525 EXPECT_EQ(test_host_data_, *predictor_->host_table_cache_); |
520 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_); | 526 EXPECT_EQ(test_url_redirect_data_, *predictor_->url_redirect_table_cache_); |
521 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_); | 527 EXPECT_EQ(test_host_redirect_data_, *predictor_->host_redirect_table_cache_); |
| 528 EXPECT_EQ(test_manifest_data_, *predictor_->manifest_table_cache_); |
522 } | 529 } |
523 | 530 |
524 // Single navigation but history count is low, so should not record. | 531 // Single navigation but history count is low, so should not record. |
525 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { | 532 TEST_F(ResourcePrefetchPredictorTest, NavigationNotRecorded) { |
526 const int kVisitCount = 1; | 533 const int kVisitCount = 1; |
527 AddUrlToHistory("https://www.google.com", kVisitCount); | 534 AddUrlToHistory("https://www.google.com", kVisitCount); |
528 | 535 |
529 URLRequestSummary main_frame = | 536 URLRequestSummary main_frame = |
530 CreateURLRequestSummary(1, "http://www.google.com"); | 537 CreateURLRequestSummary(1, "http://www.google.com"); |
531 predictor_->RecordURLRequest(main_frame); | 538 predictor_->RecordURLRequest(main_frame); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 // Tests that navigation is recorded correctly for URL already present in | 667 // Tests that navigation is recorded correctly for URL already present in |
661 // the database cache. | 668 // the database cache. |
662 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { | 669 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlInDB) { |
663 const int kVisitCount = 4; | 670 const int kVisitCount = 4; |
664 AddUrlToHistory("http://www.google.com", kVisitCount); | 671 AddUrlToHistory("http://www.google.com", kVisitCount); |
665 | 672 |
666 EXPECT_CALL(*mock_tables_.get(), | 673 EXPECT_CALL(*mock_tables_.get(), |
667 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 674 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
668 Pointee(ContainerEq(PrefetchDataMap())), | 675 Pointee(ContainerEq(PrefetchDataMap())), |
669 Pointee(ContainerEq(RedirectDataMap())), | 676 Pointee(ContainerEq(RedirectDataMap())), |
670 Pointee(ContainerEq(RedirectDataMap())))) | 677 Pointee(ContainerEq(RedirectDataMap())), |
| 678 Pointee(ContainerEq(ManifestDataMap())))) |
671 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), | 679 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), |
672 SetArgPointee<1>(test_host_data_))); | 680 SetArgPointee<1>(test_host_data_))); |
673 ResetPredictor(); | 681 ResetPredictor(); |
674 InitializePredictor(); | 682 InitializePredictor(); |
675 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); | 683 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); |
676 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); | 684 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); |
677 | 685 |
678 URLRequestSummary main_frame = CreateURLRequestSummary( | 686 URLRequestSummary main_frame = CreateURLRequestSummary( |
679 1, "http://www.google.com", "http://www.google.com", | 687 1, "http://www.google.com", "http://www.google.com", |
680 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); | 688 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 | 773 |
766 // Tests that a URL is deleted before another is added if the cache is full. | 774 // Tests that a URL is deleted before another is added if the cache is full. |
767 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { | 775 TEST_F(ResourcePrefetchPredictorTest, NavigationUrlNotInDBAndDBFull) { |
768 const int kVisitCount = 4; | 776 const int kVisitCount = 4; |
769 AddUrlToHistory("http://www.nike.com/", kVisitCount); | 777 AddUrlToHistory("http://www.nike.com/", kVisitCount); |
770 | 778 |
771 EXPECT_CALL(*mock_tables_.get(), | 779 EXPECT_CALL(*mock_tables_.get(), |
772 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 780 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
773 Pointee(ContainerEq(PrefetchDataMap())), | 781 Pointee(ContainerEq(PrefetchDataMap())), |
774 Pointee(ContainerEq(RedirectDataMap())), | 782 Pointee(ContainerEq(RedirectDataMap())), |
775 Pointee(ContainerEq(RedirectDataMap())))) | 783 Pointee(ContainerEq(RedirectDataMap())), |
| 784 Pointee(ContainerEq(ManifestDataMap())))) |
776 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), | 785 .WillOnce(DoAll(SetArgPointee<0>(test_url_data_), |
777 SetArgPointee<1>(test_host_data_))); | 786 SetArgPointee<1>(test_host_data_))); |
778 ResetPredictor(); | 787 ResetPredictor(); |
779 InitializePredictor(); | 788 InitializePredictor(); |
780 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); | 789 EXPECT_EQ(3U, predictor_->url_table_cache_->size()); |
781 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); | 790 EXPECT_EQ(2U, predictor_->host_table_cache_->size()); |
782 | 791 |
783 URLRequestSummary main_frame = CreateURLRequestSummary( | 792 URLRequestSummary main_frame = CreateURLRequestSummary( |
784 1, "http://www.nike.com", "http://www.nike.com", | 793 1, "http://www.nike.com", "http://www.nike.com", |
785 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); | 794 content::RESOURCE_TYPE_MAIN_FRAME, net::MEDIUM, std::string(), false); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
885 // Tests that redirect is recorded correctly for URL already present in | 894 // Tests that redirect is recorded correctly for URL already present in |
886 // the database cache. | 895 // the database cache. |
887 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { | 896 TEST_F(ResourcePrefetchPredictorTest, RedirectUrlInDB) { |
888 const int kVisitCount = 7; | 897 const int kVisitCount = 7; |
889 AddUrlToHistory("https://facebook.com/google", kVisitCount); | 898 AddUrlToHistory("https://facebook.com/google", kVisitCount); |
890 | 899 |
891 EXPECT_CALL(*mock_tables_.get(), | 900 EXPECT_CALL(*mock_tables_.get(), |
892 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), | 901 GetAllData(Pointee(ContainerEq(PrefetchDataMap())), |
893 Pointee(ContainerEq(PrefetchDataMap())), | 902 Pointee(ContainerEq(PrefetchDataMap())), |
894 Pointee(ContainerEq(RedirectDataMap())), | 903 Pointee(ContainerEq(RedirectDataMap())), |
895 Pointee(ContainerEq(RedirectDataMap())))) | 904 Pointee(ContainerEq(RedirectDataMap())), |
| 905 Pointee(ContainerEq(ManifestDataMap())))) |
896 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_), | 906 .WillOnce(DoAll(SetArgPointee<2>(test_url_redirect_data_), |
897 SetArgPointee<3>(test_host_redirect_data_))); | 907 SetArgPointee<3>(test_host_redirect_data_))); |
898 ResetPredictor(); | 908 ResetPredictor(); |
899 InitializePredictor(); | 909 InitializePredictor(); |
900 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size()); | 910 EXPECT_EQ(3U, predictor_->url_redirect_table_cache_->size()); |
901 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size()); | 911 EXPECT_EQ(2U, predictor_->host_redirect_table_cache_->size()); |
902 | 912 |
903 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); | 913 URLRequestSummary fb1 = CreateURLRequestSummary(1, "http://fb.com/google"); |
904 predictor_->RecordURLRequest(fb1); | 914 predictor_->RecordURLRequest(fb1); |
905 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); | 915 EXPECT_EQ(1U, predictor_->inflight_navigations_.size()); |
(...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1709 predictor_->host_table_cache_->insert( | 1719 predictor_->host_table_cache_->insert( |
1710 std::make_pair(google.primary_key(), google)); | 1720 std::make_pair(google.primary_key(), google)); |
1711 | 1721 |
1712 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); | 1722 predictor_->StartPrefetching(GURL(main_frame_url), PrefetchOrigin::EXTERNAL); |
1713 predictor_->StopPrefetching(GURL(main_frame_url)); | 1723 predictor_->StopPrefetching(GURL(main_frame_url)); |
1714 histogram_tester_->ExpectTotalCount( | 1724 histogram_tester_->ExpectTotalCount( |
1715 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); | 1725 internal::kResourcePrefetchPredictorPrefetchingDurationHistogram, 1); |
1716 } | 1726 } |
1717 | 1727 |
1718 } // namespace predictors | 1728 } // namespace predictors |
OLD | NEW |