| 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/resource_prefetch_common.h" | 23 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 23 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 24 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 24 #include "chrome/browser/predictors/resource_prefetcher.h" | 25 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 25 #include "components/history/core/browser/history_db_task.h" | 26 #include "components/history/core/browser/history_db_task.h" |
| 26 #include "components/history/core/browser/history_service_observer.h" | 27 #include "components/history/core/browser/history_service_observer.h" |
| 27 #include "components/history/core/browser/history_types.h" | 28 #include "components/history/core/browser/history_types.h" |
| 28 #include "components/keyed_service/core/keyed_service.h" | 29 #include "components/keyed_service/core/keyed_service.h" |
| 29 #include "components/precache/content/precache_manager.h" | 30 #include "components/precache/content/precache_manager.h" |
| 30 #include "content/public/common/resource_type.h" | 31 #include "content/public/common/resource_type.h" |
| 31 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // prefetching on the IO thread. The manager is owned by the | 97 // prefetching on the IO thread. The manager is owned by the |
| 97 // ResourcePrefetchPredictor and interfaces between the predictor on the UI | 98 // ResourcePrefetchPredictor and interfaces between the predictor on the UI |
| 98 // thread and the prefetchers on the IO thread. | 99 // thread and the prefetchers on the IO thread. |
| 99 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | 100 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
| 100 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 101 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
| 101 // | 102 // |
| 102 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 103 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
| 103 // recorded. Consider recording sub-frame responses independently or together | 104 // recorded. Consider recording sub-frame responses independently or together |
| 104 // with main frame. | 105 // with main frame. |
| 105 class ResourcePrefetchPredictor | 106 class ResourcePrefetchPredictor |
| 106 : public KeyedService, | 107 : public history::HistoryServiceObserver, |
| 107 public history::HistoryServiceObserver, | |
| 108 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, | 108 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, |
| 109 public precache::PrecacheManager::Delegate { | 109 public precache::PrecacheManager::Delegate { |
| 110 public: | 110 public: |
| 111 // Data collected for origin-based prediction, for a single origin during a | 111 // Data collected for origin-based prediction, for a single origin during a |
| 112 // page load (see PageRequestSummary). | 112 // page load (see PageRequestSummary). |
| 113 struct OriginRequestSummary { | 113 struct OriginRequestSummary { |
| 114 OriginRequestSummary(); | 114 OriginRequestSummary(); |
| 115 OriginRequestSummary(const OriginRequestSummary& other); | 115 OriginRequestSummary(const OriginRequestSummary& other); |
| 116 ~OriginRequestSummary(); | 116 ~OriginRequestSummary(); |
| 117 | 117 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // in the middle. | 188 // in the middle. |
| 189 enum class RedirectStatus { | 189 enum class RedirectStatus { |
| 190 NO_REDIRECT, | 190 NO_REDIRECT, |
| 191 NO_REDIRECT_BUT_PREDICTED, | 191 NO_REDIRECT_BUT_PREDICTED, |
| 192 REDIRECT_NOT_PREDICTED, | 192 REDIRECT_NOT_PREDICTED, |
| 193 REDIRECT_WRONG_PREDICTED, | 193 REDIRECT_WRONG_PREDICTED, |
| 194 REDIRECT_CORRECTLY_PREDICTED, | 194 REDIRECT_CORRECTLY_PREDICTED, |
| 195 MAX | 195 MAX |
| 196 }; | 196 }; |
| 197 | 197 |
| 198 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, | 198 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
| 199 Profile* profile); | 199 Profile* profile); |
| 200 ~ResourcePrefetchPredictor() override; | 200 ~ResourcePrefetchPredictor() override; |
| 201 | 201 |
| 202 // Starts initialization by posting a task to the DB thread to read the | 202 // Starts initialization by posting a task to the DB thread to read the |
| 203 // predictor database. | 203 // predictor database. |
| 204 void StartInitialization(); | 204 void StartInitialization(); |
| 205 void Shutdown(); |
| 205 | 206 |
| 206 // Thread safe. | 207 // Thread safe. |
| 207 static bool ShouldRecordRequest(net::URLRequest* request, | 208 static bool ShouldRecordRequest(net::URLRequest* request, |
| 208 content::ResourceType resource_type); | 209 content::ResourceType resource_type); |
| 209 static bool ShouldRecordResponse(net::URLRequest* response); | 210 static bool ShouldRecordResponse(net::URLRequest* response); |
| 210 static bool ShouldRecordRedirect(net::URLRequest* response); | 211 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 211 | 212 |
| 212 // Determines the resource type from the declared one, falling back to MIME | 213 // Determines the resource type from the declared one, falling back to MIME |
| 213 // type detection when it is not explicit. | 214 // type detection when it is not explicit. |
| 214 static content::ResourceType GetResourceType( | 215 static content::ResourceType GetResourceType( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 229 void RecordURLRedirect(const URLRequestSummary& response); | 230 void RecordURLRedirect(const URLRequestSummary& response); |
| 230 | 231 |
| 231 // Called when the main frame of a page completes loading. | 232 // Called when the main frame of a page completes loading. |
| 232 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 233 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 233 | 234 |
| 234 // Called after the main frame's first contentful paint. | 235 // Called after the main frame's first contentful paint. |
| 235 void RecordFirstContentfulPaint( | 236 void RecordFirstContentfulPaint( |
| 236 const NavigationID& navigation_id, | 237 const NavigationID& navigation_id, |
| 237 const base::TimeTicks& first_contentful_paint); | 238 const base::TimeTicks& first_contentful_paint); |
| 238 | 239 |
| 239 // Starts prefetching if it is enabled for |origin| and prefetching data | |
| 240 // exists for the |main_frame_url| either at the URL or at the host level. | |
| 241 void StartPrefetching(const GURL& main_frame_url, PrefetchOrigin origin); | |
| 242 | |
| 243 // Stops prefetching that may be in progress corresponding to | |
| 244 // |main_frame_url|. | |
| 245 void StopPrefetching(const GURL& main_frame_url); | |
| 246 | |
| 247 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending | 240 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending |
| 248 // in flight. | 241 // in flight. |
| 249 void OnPrefetchingFinished( | 242 void OnPrefetchingFinished( |
| 250 const GURL& main_frame_url, | 243 const GURL& main_frame_url, |
| 251 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 244 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 252 | 245 |
| 253 // Returns true if prefetching data exists for the |main_frame_url|. | 246 // Returns true if prefetching data exists for the |main_frame_url|. |
| 254 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); | 247 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); |
| 255 | 248 |
| 256 // Returns true iff |resource| has sufficient confidence level and required | 249 // Returns true iff |resource| has sufficient confidence level and required |
| 257 // number of hits. | 250 // number of hits. |
| 258 bool IsResourcePrefetchable(const ResourceData& resource) const; | 251 bool IsResourcePrefetchable(const ResourceData& resource) const; |
| 259 | 252 |
| 260 // precache::PrecacheManager::Delegate: | 253 // precache::PrecacheManager::Delegate: |
| 261 void OnManifestFetched(const std::string& host, | 254 void OnManifestFetched(const std::string& host, |
| 262 const precache::PrecacheManifest& manifest) override; | 255 const precache::PrecacheManifest& manifest) override; |
| 263 | 256 |
| 264 // Sets the |observer| to be notified when the resource prefetch predictor | 257 // Sets the |observer| to be notified when the resource prefetch predictor |
| 265 // data changes. Previously registered observer will be discarded. Call | 258 // data changes. Previously registered observer will be discarded. Call |
| 266 // this with nullptr parameter to de-register observer. | 259 // this with nullptr parameter to de-register observer. |
| 267 void SetObserverForTesting(TestObserver* observer); | 260 void SetObserverForTesting(TestObserver* observer); |
| 268 | 261 |
| 269 private: | 262 private: |
| 263 // 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. |
| 265 void StartPrefetching(const GURL& main_frame_url, HintOrigin origin); |
| 266 |
| 267 // Stops prefetching that may be in progress corresponding to |
| 268 // |main_frame_url|. |
| 269 void StopPrefetching(const GURL& main_frame_url); |
| 270 |
| 271 friend class LoadingPredictor; |
| 270 friend class ::PredictorsHandler; | 272 friend class ::PredictorsHandler; |
| 271 friend class ResourcePrefetchPredictorTest; | 273 friend class ResourcePrefetchPredictorTest; |
| 272 friend class ResourcePrefetchPredictorBrowserTest; | 274 friend class ResourcePrefetchPredictorBrowserTest; |
| 273 | 275 |
| 274 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 276 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 275 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 277 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 276 LazilyInitializeEmpty); | 278 LazilyInitializeEmpty); |
| 277 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 279 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 278 LazilyInitializeWithData); | 280 LazilyInitializeWithData); |
| 279 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 281 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 332 |
| 331 // Returns true if the subresource has a supported type. | 333 // Returns true if the subresource has a supported type. |
| 332 static bool IsHandledResourceType(content::ResourceType resource_type, | 334 static bool IsHandledResourceType(content::ResourceType resource_type, |
| 333 const std::string& mime_type); | 335 const std::string& mime_type); |
| 334 | 336 |
| 335 // Returns true if the request (should have a response in it) is "no-store". | 337 // Returns true if the request (should have a response in it) is "no-store". |
| 336 static bool IsNoStore(const net::URLRequest& request); | 338 static bool IsNoStore(const net::URLRequest& request); |
| 337 | 339 |
| 338 static void SetAllowPortInUrlsForTesting(bool state); | 340 static void SetAllowPortInUrlsForTesting(bool state); |
| 339 | 341 |
| 340 // KeyedService methods override. | |
| 341 void Shutdown() override; | |
| 342 | |
| 343 // Functions called on different network events pertaining to the loading of | 342 // Functions called on different network events pertaining to the loading of |
| 344 // main frame resource or sub resources. | 343 // main frame resource or sub resources. |
| 345 void OnMainFrameRequest(const URLRequestSummary& request); | 344 void OnMainFrameRequest(const URLRequestSummary& request); |
| 346 void OnMainFrameResponse(const URLRequestSummary& response); | 345 void OnMainFrameResponse(const URLRequestSummary& response); |
| 347 void OnMainFrameRedirect(const URLRequestSummary& response); | 346 void OnMainFrameRedirect(const URLRequestSummary& response); |
| 348 void OnSubresourceResponse(const URLRequestSummary& response); | 347 void OnSubresourceResponse(const URLRequestSummary& response); |
| 349 void OnSubresourceRedirect(const URLRequestSummary& response); | 348 void OnSubresourceRedirect(const URLRequestSummary& response); |
| 350 | 349 |
| 351 // Called when onload completes for a navigation. We treat this point as the | 350 // Called when onload completes for a navigation. We treat this point as the |
| 352 // "completion" of the navigation. The resources requested by the page up to | 351 // "completion" of the navigation. The resources requested by the page up to |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 // notificatioan. | 468 // notificatioan. |
| 470 void ConnectToHistoryService(); | 469 void ConnectToHistoryService(); |
| 471 | 470 |
| 472 // Used for testing to inject mock tables. | 471 // Used for testing to inject mock tables. |
| 473 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 472 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 474 tables_ = tables; | 473 tables_ = tables; |
| 475 } | 474 } |
| 476 | 475 |
| 477 Profile* const profile_; | 476 Profile* const profile_; |
| 478 TestObserver* observer_; | 477 TestObserver* observer_; |
| 479 ResourcePrefetchPredictorConfig const config_; | 478 const LoadingPredictorConfig config_; |
| 480 InitializationState initialization_state_; | 479 InitializationState initialization_state_; |
| 481 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 480 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 482 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 481 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 483 base::CancelableTaskTracker history_lookup_consumer_; | 482 base::CancelableTaskTracker history_lookup_consumer_; |
| 484 | 483 |
| 485 // Copy of the data in the predictor tables. | 484 // Copy of the data in the predictor tables. |
| 486 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 485 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 487 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 486 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 488 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 487 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 489 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 488 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 | 527 |
| 529 private: | 528 private: |
| 530 ResourcePrefetchPredictor* predictor_; | 529 ResourcePrefetchPredictor* predictor_; |
| 531 | 530 |
| 532 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 531 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 533 }; | 532 }; |
| 534 | 533 |
| 535 } // namespace predictors | 534 } // namespace predictors |
| 536 | 535 |
| 537 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 536 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |