Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
| 20 #include "base/task/cancelable_task_tracker.h" | 20 #include "base/task/cancelable_task_tracker.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "chrome/browser/predictors/loading_predictor_config.h" | 22 #include "chrome/browser/predictors/loading_predictor_config.h" |
| 23 #include "chrome/browser/predictors/resource_prefetch_common.h" | 23 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 24 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 24 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 25 #include "chrome/browser/predictors/resource_prefetcher.h" | 25 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 26 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | |
| 26 #include "components/history/core/browser/history_db_task.h" | 27 #include "components/history/core/browser/history_db_task.h" |
| 27 #include "components/history/core/browser/history_service_observer.h" | 28 #include "components/history/core/browser/history_service_observer.h" |
| 28 #include "components/history/core/browser/history_types.h" | 29 #include "components/history/core/browser/history_types.h" |
| 29 #include "components/keyed_service/core/keyed_service.h" | 30 #include "components/keyed_service/core/keyed_service.h" |
| 30 #include "components/precache/content/precache_manager.h" | 31 #include "components/precache/content/precache_manager.h" |
| 31 #include "content/public/common/resource_type.h" | 32 #include "content/public/common/resource_type.h" |
| 32 #include "url/gurl.h" | 33 #include "url/gurl.h" |
| 33 | 34 |
| 34 class PredictorsHandler; | 35 class PredictorsHandler; |
| 35 class Profile; | 36 class Profile; |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 const NavigationID& navigation_id, | 238 const NavigationID& navigation_id, |
| 238 const base::TimeTicks& first_contentful_paint); | 239 const base::TimeTicks& first_contentful_paint); |
| 239 | 240 |
| 240 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending | 241 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending |
| 241 // in flight. | 242 // in flight. |
| 242 void OnPrefetchingFinished( | 243 void OnPrefetchingFinished( |
| 243 const GURL& main_frame_url, | 244 const GURL& main_frame_url, |
| 244 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 245 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 245 | 246 |
| 246 // Returns true if prefetching data exists for the |main_frame_url|. | 247 // Returns true if prefetching data exists for the |main_frame_url|. |
| 247 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); | 248 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; |
| 248 | 249 |
| 249 // Returns true iff |resource| has sufficient confidence level and required | 250 // Returns true iff |resource| has sufficient confidence level and required |
| 250 // number of hits. | 251 // number of hits. |
| 251 bool IsResourcePrefetchable(const ResourceData& resource) const; | 252 bool IsResourcePrefetchable(const ResourceData& resource) const; |
| 252 | 253 |
| 253 // precache::PrecacheManager::Delegate: | 254 // precache::PrecacheManager::Delegate: |
| 254 void OnManifestFetched(const std::string& host, | 255 void OnManifestFetched(const std::string& host, |
| 255 const precache::PrecacheManifest& manifest) override; | 256 const precache::PrecacheManifest& manifest) override; |
| 256 | 257 |
| 257 // Sets the |observer| to be notified when the resource prefetch predictor | 258 // Sets the |observer| to be notified when the resource prefetch predictor |
| 258 // data changes. Previously registered observer will be discarded. Call | 259 // data changes. Previously registered observer will be discarded. Call |
| 259 // this with nullptr parameter to de-register observer. | 260 // this with nullptr parameter to de-register observer. |
| 260 void SetObserverForTesting(TestObserver* observer); | 261 void SetObserverForTesting(TestObserver* observer); |
| 261 | 262 |
| 262 private: | 263 private: |
| 263 // Starts prefetching if it is enabled for |origin| and prefetching data | 264 // Starts prefetching if it is enabled and prefetching data exists for the |
| 264 // exists for the |main_frame_url| either at the URL or at the host level. | 265 // |main_frame_url| either at the URL or at the host level. |
| 265 void StartPrefetching(const GURL& main_frame_url, HintOrigin origin); | 266 void StartPrefetching(const GURL& main_frame_url); |
| 266 | 267 |
| 267 // Stops prefetching that may be in progress corresponding to | 268 // Stops prefetching that may be in progress corresponding to |
| 268 // |main_frame_url|. | 269 // |main_frame_url|. |
| 269 void StopPrefetching(const GURL& main_frame_url); | 270 void StopPrefetching(const GURL& main_frame_url); |
| 270 | 271 |
| 271 friend class LoadingPredictor; | 272 friend class LoadingPredictor; |
| 272 friend class ::PredictorsHandler; | 273 friend class ::PredictorsHandler; |
| 273 friend class ResourcePrefetchPredictorTest; | 274 friend class ResourcePrefetchPredictorTest; |
| 274 friend class ResourcePrefetchPredictorBrowserTest; | 275 friend class ResourcePrefetchPredictorBrowserTest; |
| 275 | 276 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 302 PopulatePrefetcherRequest); | 303 PopulatePrefetcherRequest); |
| 303 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); | 304 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); |
| 304 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 305 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 305 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| 306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 307 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 307 TestPrecisionRecallHistograms); | 308 TestPrecisionRecallHistograms); |
| 308 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 309 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 309 TestPrefetchingDurationHistogram); | 310 TestPrefetchingDurationHistogram); |
| 310 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 311 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 311 TestRecordFirstContentfulPaint); | 312 TestRecordFirstContentfulPaint); |
| 313 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | |
| 314 TestCanPrefetchUrlForOrigin); | |
|
alexilin
2017/05/30 15:06:00
Does this test still exist?
Benoit L
2017/05/31 14:34:42
Done.
| |
| 312 | 315 |
| 313 enum InitializationState { | 316 enum InitializationState { |
| 314 NOT_INITIALIZED = 0, | 317 NOT_INITIALIZED = 0, |
| 315 INITIALIZING = 1, | 318 INITIALIZING = 1, |
| 316 INITIALIZED = 2 | 319 INITIALIZED = 2 |
| 317 }; | 320 }; |
| 318 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 321 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 319 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 322 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 320 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; | 323 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; |
| 321 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap; | 324 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 335 const std::string& mime_type); | 338 const std::string& mime_type); |
| 336 | 339 |
| 337 // Returns true if the request (should have a response in it) is "no-store". | 340 // Returns true if the request (should have a response in it) is "no-store". |
| 338 static bool IsNoStore(const net::URLRequest& request); | 341 static bool IsNoStore(const net::URLRequest& request); |
| 339 | 342 |
| 340 static void SetAllowPortInUrlsForTesting(bool state); | 343 static void SetAllowPortInUrlsForTesting(bool state); |
| 341 | 344 |
| 342 // Functions called on different network events pertaining to the loading of | 345 // Functions called on different network events pertaining to the loading of |
| 343 // main frame resource or sub resources. | 346 // main frame resource or sub resources. |
| 344 void OnMainFrameRequest(const URLRequestSummary& request); | 347 void OnMainFrameRequest(const URLRequestSummary& request); |
| 345 void OnMainFrameResponse(const URLRequestSummary& response); | |
| 346 void OnMainFrameRedirect(const URLRequestSummary& response); | 348 void OnMainFrameRedirect(const URLRequestSummary& response); |
| 347 void OnSubresourceResponse(const URLRequestSummary& response); | 349 void OnSubresourceResponse(const URLRequestSummary& response); |
| 348 void OnSubresourceRedirect(const URLRequestSummary& response); | 350 void OnSubresourceRedirect(const URLRequestSummary& response); |
| 349 | 351 |
| 350 // Called when onload completes for a navigation. We treat this point as the | 352 // Called when onload completes for a navigation. We treat this point as the |
| 351 // "completion" of the navigation. The resources requested by the page up to | 353 // "completion" of the navigation. The resources requested by the page up to |
| 352 // this point are the only ones considered for prefetching. | 354 // this point are the only ones considered for prefetching. |
| 353 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); | 355 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); |
| 354 | 356 |
| 355 // Returns true iff one of the following conditions is true | 357 // Returns true iff one of the following conditions is true |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 std::unique_ptr<PrefetchDataMap> host_data_map, | 390 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 389 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 391 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 390 std::unique_ptr<RedirectDataMap> host_redirect_data_map, | 392 std::unique_ptr<RedirectDataMap> host_redirect_data_map, |
| 391 std::unique_ptr<ManifestDataMap> manifest_data_map, | 393 std::unique_ptr<ManifestDataMap> manifest_data_map, |
| 392 std::unique_ptr<OriginDataMap> origin_data_map); | 394 std::unique_ptr<OriginDataMap> origin_data_map); |
| 393 | 395 |
| 394 // Called during initialization when history is read and the predictor | 396 // Called during initialization when history is read and the predictor |
| 395 // database has been read. | 397 // database has been read. |
| 396 void OnHistoryAndCacheLoaded(); | 398 void OnHistoryAndCacheLoaded(); |
| 397 | 399 |
| 398 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. | 400 // Cleanup inflight_navigations_, and prefetcher_stats_. |
| 399 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 401 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 400 | 402 |
| 401 // Deletes all URLs from the predictor database, the caches and removes all | 403 // Deletes all URLs from the predictor database, the caches and removes all |
| 402 // inflight navigations. | 404 // inflight navigations. |
| 403 void DeleteAllUrls(); | 405 void DeleteAllUrls(); |
| 404 | 406 |
| 405 // Deletes data for the input |urls| and their corresponding hosts from the | 407 // Deletes data for the input |urls| and their corresponding hosts from the |
| 406 // predictor database and caches. | 408 // predictor database and caches. |
| 407 void DeleteUrls(const history::URLRows& urls); | 409 void DeleteUrls(const history::URLRows& urls); |
| 408 | 410 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 466 | 468 |
| 467 // Used to connect to HistoryService or register for service loaded | 469 // Used to connect to HistoryService or register for service loaded |
| 468 // notificatioan. | 470 // notificatioan. |
| 469 void ConnectToHistoryService(); | 471 void ConnectToHistoryService(); |
| 470 | 472 |
| 471 // Used for testing to inject mock tables. | 473 // Used for testing to inject mock tables. |
| 472 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 474 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 473 tables_ = tables; | 475 tables_ = tables; |
| 474 } | 476 } |
| 475 | 477 |
| 478 // For testing. | |
| 479 void set_mock_resource_prefetcher_manager( | |
| 480 scoped_refptr<ResourcePrefetcherManager> prefetch_manager) { | |
| 481 prefetch_manager_ = prefetch_manager; | |
| 482 } | |
| 483 | |
| 476 Profile* const profile_; | 484 Profile* const profile_; |
| 477 TestObserver* observer_; | 485 TestObserver* observer_; |
| 478 const LoadingPredictorConfig config_; | 486 const LoadingPredictorConfig config_; |
| 479 InitializationState initialization_state_; | 487 InitializationState initialization_state_; |
| 480 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 488 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 481 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 489 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 482 base::CancelableTaskTracker history_lookup_consumer_; | 490 base::CancelableTaskTracker history_lookup_consumer_; |
| 483 | 491 |
| 484 // Copy of the data in the predictor tables. | 492 // Copy of the data in the predictor tables. |
| 485 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 493 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 486 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 494 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 487 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 495 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 488 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 496 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| 489 std::unique_ptr<ManifestDataMap> manifest_table_cache_; | 497 std::unique_ptr<ManifestDataMap> manifest_table_cache_; |
| 490 std::unique_ptr<OriginDataMap> origin_table_cache_; | 498 std::unique_ptr<OriginDataMap> origin_table_cache_; |
| 491 | 499 |
| 492 std::map<GURL, base::TimeTicks> inflight_prefetches_; | |
| 493 NavigationMap inflight_navigations_; | 500 NavigationMap inflight_navigations_; |
| 494 | 501 |
| 495 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> | 502 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> |
| 496 prefetcher_stats_; | 503 prefetcher_stats_; |
| 497 | 504 |
| 498 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 505 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 499 history_service_observer_; | 506 history_service_observer_; |
| 500 | 507 |
| 501 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 508 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 502 }; | 509 }; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 527 | 534 |
| 528 private: | 535 private: |
| 529 ResourcePrefetchPredictor* predictor_; | 536 ResourcePrefetchPredictor* predictor_; |
| 530 | 537 |
| 531 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 538 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 532 }; | 539 }; |
| 533 | 540 |
| 534 } // namespace predictors | 541 } // namespace predictors |
| 535 | 542 |
| 536 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 543 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |