| 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: |
| 264 // Return whether the predictor has data about a URL and can prefetch it |
| 265 // for a given origin with the current configuration. |
| 266 bool CanPrefetchUrlForOrigin(const GURL& url, HintOrigin origin) const; |
| 267 |
| 263 // Starts prefetching if it is enabled for |origin| and prefetching data | 268 // Starts prefetching if it is enabled for |origin| and prefetching data |
| 264 // exists for the |main_frame_url| either at the URL or at the host level. | 269 // exists for the |main_frame_url| either at the URL or at the host level. |
| 265 void StartPrefetching(const GURL& main_frame_url, HintOrigin origin); | 270 void StartPrefetching(const GURL& main_frame_url, HintOrigin origin); |
| 266 | 271 |
| 267 // Stops prefetching that may be in progress corresponding to | 272 // Stops prefetching that may be in progress corresponding to |
| 268 // |main_frame_url|. | 273 // |main_frame_url|. |
| 269 void StopPrefetching(const GURL& main_frame_url); | 274 void StopPrefetching(const GURL& main_frame_url); |
| 270 | 275 |
| 271 friend class LoadingPredictor; | 276 friend class LoadingPredictor; |
| 272 friend class ::PredictorsHandler; | 277 friend class ::PredictorsHandler; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 302 PopulatePrefetcherRequest); | 307 PopulatePrefetcherRequest); |
| 303 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); | 308 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); |
| 304 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 309 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 305 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 310 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| 306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 311 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 307 TestPrecisionRecallHistograms); | 312 TestPrecisionRecallHistograms); |
| 308 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 313 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 309 TestPrefetchingDurationHistogram); | 314 TestPrefetchingDurationHistogram); |
| 310 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 315 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 311 TestRecordFirstContentfulPaint); | 316 TestRecordFirstContentfulPaint); |
| 317 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 318 TestCanPrefetchUrlForOrigin); |
| 312 | 319 |
| 313 enum InitializationState { | 320 enum InitializationState { |
| 314 NOT_INITIALIZED = 0, | 321 NOT_INITIALIZED = 0, |
| 315 INITIALIZING = 1, | 322 INITIALIZING = 1, |
| 316 INITIALIZED = 2 | 323 INITIALIZED = 2 |
| 317 }; | 324 }; |
| 318 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 325 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 319 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 326 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 320 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; | 327 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; |
| 321 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap; | 328 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 | 473 |
| 467 // Used to connect to HistoryService or register for service loaded | 474 // Used to connect to HistoryService or register for service loaded |
| 468 // notificatioan. | 475 // notificatioan. |
| 469 void ConnectToHistoryService(); | 476 void ConnectToHistoryService(); |
| 470 | 477 |
| 471 // Used for testing to inject mock tables. | 478 // Used for testing to inject mock tables. |
| 472 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 479 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 473 tables_ = tables; | 480 tables_ = tables; |
| 474 } | 481 } |
| 475 | 482 |
| 483 // For testing. |
| 484 void set_mock_resource_prefetcher_manager( |
| 485 scoped_refptr<ResourcePrefetcherManager> prefetch_manager) { |
| 486 prefetch_manager_ = prefetch_manager; |
| 487 } |
| 488 |
| 476 Profile* const profile_; | 489 Profile* const profile_; |
| 477 TestObserver* observer_; | 490 TestObserver* observer_; |
| 478 const LoadingPredictorConfig config_; | 491 const LoadingPredictorConfig config_; |
| 479 InitializationState initialization_state_; | 492 InitializationState initialization_state_; |
| 480 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 493 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 481 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 494 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 482 base::CancelableTaskTracker history_lookup_consumer_; | 495 base::CancelableTaskTracker history_lookup_consumer_; |
| 483 | 496 |
| 484 // Copy of the data in the predictor tables. | 497 // Copy of the data in the predictor tables. |
| 485 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 498 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 | 540 |
| 528 private: | 541 private: |
| 529 ResourcePrefetchPredictor* predictor_; | 542 ResourcePrefetchPredictor* predictor_; |
| 530 | 543 |
| 531 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 544 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 532 }; | 545 }; |
| 533 | 546 |
| 534 } // namespace predictors | 547 } // namespace predictors |
| 535 | 548 |
| 536 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 549 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |