| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <cstddef> | 6 #include <cstddef> |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 struct ResourceSummary { | 82 struct ResourceSummary { |
| 83 ResourceSummary() | 83 ResourceSummary() |
| 84 : version(0), | 84 : version(0), |
| 85 is_no_store(false), | 85 is_no_store(false), |
| 86 is_external(false), | 86 is_external(false), |
| 87 is_observable(true), | 87 is_observable(true), |
| 88 is_prohibited(false) { | 88 is_prohibited(false) { |
| 89 request.before_first_contentful_paint = true; | 89 request.before_first_contentful_paint = true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 ResourcePrefetchPredictor::URLRequestSummary request; | 92 URLRequestSummary request; |
| 93 // Allows to update HTTP ETag. | 93 // Allows to update HTTP ETag. |
| 94 size_t version; | 94 size_t version; |
| 95 // True iff "Cache-control: no-store" header is present. | 95 // True iff "Cache-control: no-store" header is present. |
| 96 bool is_no_store; | 96 bool is_no_store; |
| 97 // True iff a request for this resource must be ignored by the custom handler. | 97 // True iff a request for this resource must be ignored by the custom handler. |
| 98 bool is_external; | 98 bool is_external; |
| 99 // True iff the LearningObserver must observe this resource. | 99 // True iff the LearningObserver must observe this resource. |
| 100 bool is_observable; | 100 bool is_observable; |
| 101 // A request with |is_prohibited| set to true makes the test that originates | 101 // A request with |is_prohibited| set to true makes the test that originates |
| 102 // the request fail. | 102 // the request fail. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 144 |
| 145 void Wait() { run_loop_.Run(); } | 145 void Wait() { run_loop_.Run(); } |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 content::BrowsingDataRemover* remover_; | 148 content::BrowsingDataRemover* remover_; |
| 149 base::RunLoop run_loop_; | 149 base::RunLoop run_loop_; |
| 150 | 150 |
| 151 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverObserver); | 151 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverObserver); |
| 152 }; | 152 }; |
| 153 | 153 |
| 154 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 155 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 156 | |
| 157 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { | 154 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { |
| 158 std::stable_sort(subresources->begin(), subresources->end(), | 155 std::stable_sort(subresources->begin(), subresources->end(), |
| 159 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 156 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 160 return x.resource_url < y.resource_url; | 157 return x.resource_url < y.resource_url; |
| 161 }); | 158 }); |
| 162 subresources->erase( | 159 subresources->erase( |
| 163 std::unique(subresources->begin(), subresources->end(), | 160 std::unique(subresources->begin(), subresources->end(), |
| 164 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 161 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 165 return x.resource_url == y.resource_url; | 162 return x.resource_url == y.resource_url; |
| 166 }), | 163 }), |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 return resource_url; | 230 return resource_url; |
| 234 } | 231 } |
| 235 | 232 |
| 236 } // namespace | 233 } // namespace |
| 237 | 234 |
| 238 // Helper class to track and allow waiting for a single OnNavigationLearned | 235 // Helper class to track and allow waiting for a single OnNavigationLearned |
| 239 // event. The information provided by this event is also used to verify that | 236 // event. The information provided by this event is also used to verify that |
| 240 // ResourcePrefetchPredictor works as expected. | 237 // ResourcePrefetchPredictor works as expected. |
| 241 class LearningObserver : public TestObserver { | 238 class LearningObserver : public TestObserver { |
| 242 public: | 239 public: |
| 243 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 244 | |
| 245 LearningObserver(ResourcePrefetchPredictor* predictor, | 240 LearningObserver(ResourcePrefetchPredictor* predictor, |
| 246 const size_t expected_url_visit_count, | 241 const size_t expected_url_visit_count, |
| 247 const PageRequestSummary& expected_summary, | 242 const PageRequestSummary& expected_summary, |
| 248 bool match_navigation_id, | 243 bool match_navigation_id, |
| 249 bool match_before_first_contentful_paint) | 244 bool match_before_first_contentful_paint) |
| 250 : TestObserver(predictor), | 245 : TestObserver(predictor), |
| 251 url_visit_count_(expected_url_visit_count), | 246 url_visit_count_(expected_url_visit_count), |
| 252 summary_(expected_summary), | 247 summary_(expected_summary), |
| 253 match_navigation_id_(match_navigation_id), | 248 match_navigation_id_(match_navigation_id), |
| 254 match_before_first_contentful_paint_( | 249 match_before_first_contentful_paint_( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 311 |
| 317 private: | 312 private: |
| 318 base::RunLoop run_loop_; | 313 base::RunLoop run_loop_; |
| 319 GURL main_frame_url_; | 314 GURL main_frame_url_; |
| 320 | 315 |
| 321 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); | 316 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); |
| 322 }; | 317 }; |
| 323 | 318 |
| 324 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 319 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
| 325 protected: | 320 protected: |
| 326 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 321 using URLRequestSummary = URLRequestSummary; |
| 327 | 322 |
| 328 void SetUpCommandLine(base::CommandLine* command_line) override { | 323 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 329 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); | 324 command_line->AppendSwitchASCII("force-fieldtrials", "trial/group"); |
| 330 std::string parameter = base::StringPrintf( | 325 std::string parameter = base::StringPrintf( |
| 331 "trial.group:%s/%s", kModeParamName, kExternalPrefetchingMode); | 326 "trial.group:%s/%s", kModeParamName, kExternalPrefetchingMode); |
| 332 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); | 327 command_line->AppendSwitchASCII("force-fieldtrial-params", parameter); |
| 333 std::string enabled_feature = base::StringPrintf( | 328 std::string enabled_feature = base::StringPrintf( |
| 334 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); | 329 "%s<trial", kSpeculativeResourcePrefetchingFeatureName); |
| 335 command_line->AppendSwitchASCII("enable-features", enabled_feature); | 330 command_line->AppendSwitchASCII("enable-features", enabled_feature); |
| 336 } | 331 } |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 net::RequestPriority priority) { | 463 net::RequestPriority priority) { |
| 469 auto pair_and_whether_inserted = | 464 auto pair_and_whether_inserted = |
| 470 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 465 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
| 471 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 466 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
| 472 << " was inserted twice"; | 467 << " was inserted twice"; |
| 473 ResourceSummary* resource = &pair_and_whether_inserted.first->second; | 468 ResourceSummary* resource = &pair_and_whether_inserted.first->second; |
| 474 resource->request.resource_url = resource_url; | 469 resource->request.resource_url = resource_url; |
| 475 resource->request.resource_type = resource_type; | 470 resource->request.resource_type = resource_type; |
| 476 resource->request.priority = priority; | 471 resource->request.priority = priority; |
| 477 resource->request.has_validators = true; | 472 resource->request.has_validators = true; |
| 473 resource->request.request_url = resource_url; |
| 478 return resource; | 474 return resource; |
| 479 } | 475 } |
| 480 | 476 |
| 481 ResourceSummary* AddExternalResource(const GURL& resource_url, | 477 ResourceSummary* AddExternalResource(const GURL& resource_url, |
| 482 content::ResourceType resource_type, | 478 content::ResourceType resource_type, |
| 483 net::RequestPriority priority) { | 479 net::RequestPriority priority) { |
| 484 auto* resource = AddResource(resource_url, resource_type, priority); | 480 auto* resource = AddResource(resource_url, resource_type, priority); |
| 485 resource->is_external = true; | 481 resource->is_external = true; |
| 486 return resource; | 482 return resource; |
| 487 } | 483 } |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 AddResourcesFromSubresourceHtml(); | 997 AddResourcesFromSubresourceHtml(); |
| 1002 | 998 |
| 1003 NavigateToURLAndCheckSubresources(initial_url); | 999 NavigateToURLAndCheckSubresources(initial_url); |
| 1004 ClearCache(); | 1000 ClearCache(); |
| 1005 NavigateToURLAndCheckSubresources(initial_url); | 1001 NavigateToURLAndCheckSubresources(initial_url); |
| 1006 ClearCache(); | 1002 ClearCache(); |
| 1007 NavigateToURLAndCheckPrefetching(initial_url); | 1003 NavigateToURLAndCheckPrefetching(initial_url); |
| 1008 } | 1004 } |
| 1009 | 1005 |
| 1010 } // namespace predictors | 1006 } // namespace predictors |
| OLD | NEW |