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> |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 // | 97 // |
| 98 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 98 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
| 99 // recorded. Consider recording sub-frame responses independently or together | 99 // recorded. Consider recording sub-frame responses independently or together |
| 100 // with main frame. | 100 // with main frame. |
| 101 class ResourcePrefetchPredictor | 101 class ResourcePrefetchPredictor |
| 102 : public KeyedService, | 102 : public KeyedService, |
| 103 public history::HistoryServiceObserver, | 103 public history::HistoryServiceObserver, |
| 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, | 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, |
| 105 public precache::PrecacheManager::Delegate { | 105 public precache::PrecacheManager::Delegate { |
| 106 public: | 106 public: |
| 107 struct OriginRequestSummary { | |
|
alexilin
2017/04/10 14:58:28
Do we really need this struct?
Can't we derive the
Benoit L
2017/04/11 09:04:05
You're right, this is not strictly necessary.
This
alexilin
2017/04/11 09:42:19
Sgtm. Anyway, we would have to reconstruct this ma
| |
| 108 OriginRequestSummary(); | |
| 109 OriginRequestSummary(const OriginRequestSummary& other); | |
| 110 ~OriginRequestSummary(); | |
| 111 | |
| 112 GURL origin; | |
| 113 bool always_access_network; | |
| 114 bool accessed_network; | |
| 115 int first_occurrence; | |
| 116 }; | |
| 117 | |
| 107 // Stores the data that we need to get from the URLRequest. | 118 // Stores the data that we need to get from the URLRequest. |
| 108 struct URLRequestSummary { | 119 struct URLRequestSummary { |
| 109 URLRequestSummary(); | 120 URLRequestSummary(); |
| 110 URLRequestSummary(const URLRequestSummary& other); | 121 URLRequestSummary(const URLRequestSummary& other); |
| 111 ~URLRequestSummary(); | 122 ~URLRequestSummary(); |
| 112 | 123 |
| 113 NavigationID navigation_id; | 124 NavigationID navigation_id; |
| 114 GURL resource_url; | 125 GURL resource_url; |
| 115 content::ResourceType resource_type; | 126 content::ResourceType resource_type; |
| 116 net::RequestPriority priority; | 127 net::RequestPriority priority; |
| 117 | 128 |
| 118 // Only for responses. | 129 // Only for responses. |
| 119 std::string mime_type; | 130 std::string mime_type; |
| 120 bool was_cached; | 131 bool was_cached; |
| 121 GURL redirect_url; // Empty unless request was redirected to a valid url. | 132 GURL redirect_url; // Empty unless request was redirected to a valid url. |
| 122 | 133 |
| 123 bool has_validators; | 134 bool has_validators; |
| 124 bool always_revalidate; | 135 bool always_revalidate; |
| 136 bool is_no_store; | |
| 137 bool network_accessed; | |
| 125 | 138 |
| 126 // Initializes a |URLRequestSummary| from a |URLRequest| response. | 139 // Initializes a |URLRequestSummary| from a |URLRequest| response. |
| 127 // Returns true for success. Note: NavigationID is NOT initialized | 140 // Returns true for success. Note: NavigationID is NOT initialized |
| 128 // by this function. | 141 // by this function. |
| 129 static bool SummarizeResponse(const net::URLRequest& request, | 142 static bool SummarizeResponse(const net::URLRequest& request, |
| 130 URLRequestSummary* summary); | 143 URLRequestSummary* summary); |
| 131 }; | 144 }; |
| 132 | 145 |
| 133 // Stores the data learned from a single navigation. | 146 // Stores the data learned from a single navigation. |
| 134 struct PageRequestSummary { | 147 struct PageRequestSummary { |
| 135 explicit PageRequestSummary(const GURL& main_frame_url); | 148 explicit PageRequestSummary(const GURL& main_frame_url); |
| 136 PageRequestSummary(const PageRequestSummary& other); | 149 PageRequestSummary(const PageRequestSummary& other); |
| 137 ~PageRequestSummary(); | 150 ~PageRequestSummary(); |
| 138 | 151 |
| 139 GURL main_frame_url; | 152 GURL main_frame_url; |
| 140 GURL initial_url; | 153 GURL initial_url; |
| 141 | 154 |
| 142 // Stores all subresource requests within a single navigation, from initial | 155 // Stores all subresource requests within a single navigation, from initial |
| 143 // main frame request to navigation completion. | 156 // main frame request to navigation completion. |
| 144 std::vector<URLRequestSummary> subresource_requests; | 157 std::vector<URLRequestSummary> subresource_requests; |
| 158 // Map of origin -> OriginRequestSummary. Only one instance of each origin | |
| 159 // is kept per navigation, but the summary is updated several times. | |
| 160 std::map<GURL, OriginRequestSummary> origins; | |
| 145 }; | 161 }; |
| 146 | 162 |
| 147 // Stores a result of prediction. Essentially, |subresource_urls| is main | 163 // Stores a result of prediction. Essentially, |subresource_urls| is main |
| 148 // result and other fields are used for diagnosis and histograms reporting. | 164 // result and other fields are used for diagnosis and histograms reporting. |
| 149 struct Prediction { | 165 struct Prediction { |
| 150 Prediction(); | 166 Prediction(); |
| 151 Prediction(const Prediction& other); | 167 Prediction(const Prediction& other); |
| 152 ~Prediction(); | 168 ~Prediction(); |
| 153 | 169 |
| 154 bool is_host; | 170 bool is_host; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 TestPrefetchingDurationHistogram); | 289 TestPrefetchingDurationHistogram); |
| 274 | 290 |
| 275 enum InitializationState { | 291 enum InitializationState { |
| 276 NOT_INITIALIZED = 0, | 292 NOT_INITIALIZED = 0, |
| 277 INITIALIZING = 1, | 293 INITIALIZING = 1, |
| 278 INITIALIZED = 2 | 294 INITIALIZED = 2 |
| 279 }; | 295 }; |
| 280 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 296 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 281 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 297 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 282 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; | 298 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; |
| 299 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap; | |
| 283 | 300 |
| 284 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 301 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
| 285 NavigationMap; | 302 NavigationMap; |
| 286 | 303 |
| 287 // Returns true if the main page request is supported for prediction. | 304 // Returns true if the main page request is supported for prediction. |
| 288 static bool IsHandledMainPage(net::URLRequest* request); | 305 static bool IsHandledMainPage(net::URLRequest* request); |
| 289 | 306 |
| 290 // Returns true if the subresource request is supported for prediction. | 307 // Returns true if the subresource request is supported for prediction. |
| 291 static bool IsHandledSubresource(net::URLRequest* request, | 308 static bool IsHandledSubresource(net::URLRequest* request, |
| 292 content::ResourceType resource_type); | 309 content::ResourceType resource_type); |
| 293 | 310 |
| 294 // Returns true if the subresource has a supported type. | 311 // Returns true if the subresource has a supported type. |
| 295 static bool IsHandledResourceType(content::ResourceType resource_type, | 312 static bool IsHandledResourceType(content::ResourceType resource_type, |
| 296 const std::string& mime_type); | 313 const std::string& mime_type); |
| 297 | 314 |
| 298 // Returns true if the request (should have a response in it) is "no-store". | 315 // Returns true if the request (should have a response in it) is "no-store". |
| 299 static bool IsNoStore(const net::URLRequest* request); | 316 static bool IsNoStore(const net::URLRequest& request); |
| 300 | 317 |
| 301 // Returns true iff |redirect_data_map| contains confident redirect endpoint | 318 // Returns true iff |redirect_data_map| contains confident redirect endpoint |
| 302 // for |entry_point| and assigns it to the |redirect_endpoint|. | 319 // for |entry_point| and assigns it to the |redirect_endpoint|. |
| 303 static bool GetRedirectEndpoint(const std::string& entry_point, | 320 static bool GetRedirectEndpoint(const std::string& entry_point, |
| 304 const RedirectDataMap& redirect_data_map, | 321 const RedirectDataMap& redirect_data_map, |
| 305 std::string* redirect_endpoint); | 322 std::string* redirect_endpoint); |
| 306 | 323 |
| 307 static void SetAllowPortInUrlsForTesting(bool state); | 324 static void SetAllowPortInUrlsForTesting(bool state); |
| 308 | 325 |
| 309 // KeyedService methods override. | 326 // KeyedService methods override. |
| 310 void Shutdown() override; | 327 void Shutdown() override; |
| 311 | 328 |
| 312 // Functions called on different network events pertaining to the loading of | 329 // Functions called on different network events pertaining to the loading of |
| 313 // main frame resource or sub resources. | 330 // main frame resource or sub resources. |
| 314 void OnMainFrameRequest(const URLRequestSummary& request); | 331 void OnMainFrameRequest(const URLRequestSummary& request); |
| 315 void OnMainFrameResponse(const URLRequestSummary& response); | 332 void OnMainFrameResponse(const URLRequestSummary& response); |
| 316 void OnMainFrameRedirect(const URLRequestSummary& response); | 333 void OnMainFrameRedirect(const URLRequestSummary& response); |
| 317 void OnSubresourceResponse(const URLRequestSummary& response); | 334 void OnSubresourceResponse(const URLRequestSummary& response); |
| 335 void OnSubresourceRedirect(const URLRequestSummary& response); | |
| 318 | 336 |
| 319 // Called when onload completes for a navigation. We treat this point as the | 337 // Called when onload completes for a navigation. We treat this point as the |
| 320 // "completion" of the navigation. The resources requested by the page up to | 338 // "completion" of the navigation. The resources requested by the page up to |
| 321 // this point are the only ones considered for prefetching. | 339 // this point are the only ones considered for prefetching. |
| 322 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); | 340 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); |
| 323 | 341 |
| 324 // Returns true iff there is PrefetchData that can be used for a | 342 // Returns true iff there is PrefetchData that can be used for a |
| 325 // |main_frame_url| and fills |prediction| with resources that need to be | 343 // |main_frame_url| and fills |prediction| with resources that need to be |
| 326 // prefetched. |prediction| pointer may be equal nullptr to get return value | 344 // prefetched. |prediction| pointer may be equal nullptr to get return value |
| 327 // only. | 345 // only. |
| 328 bool GetPrefetchData(const GURL& main_frame_url, | 346 bool GetPrefetchData(const GURL& main_frame_url, |
| 329 Prediction* prediction) const; | 347 Prediction* prediction) const; |
| 330 | 348 |
| 331 // Returns true iff the |data_map| contains PrefetchData that can be used | 349 // Returns true iff the |data_map| contains PrefetchData that can be used |
| 332 // for a |main_frame_key| and fills |urls| with resources that need to be | 350 // for a |main_frame_key| and fills |urls| with resources that need to be |
| 333 // prefetched. |urls| pointer may be equal nullptr to get return value only. | 351 // prefetched. |urls| pointer may be equal nullptr to get return value only. |
| 334 bool PopulatePrefetcherRequest(const std::string& main_frame_key, | 352 bool PopulatePrefetcherRequest(const std::string& main_frame_key, |
| 335 const PrefetchDataMap& data_map, | 353 const PrefetchDataMap& data_map, |
| 336 std::vector<GURL>* urls) const; | 354 std::vector<GURL>* urls) const; |
| 337 | 355 |
| 338 // Callback for task to read predictor database. Takes ownership of | 356 // Callback for task to read predictor database. Takes ownership of |
| 339 // all arguments. | 357 // all arguments. |
| 340 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 358 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 341 std::unique_ptr<PrefetchDataMap> host_data_map, | 359 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 342 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 360 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 343 std::unique_ptr<RedirectDataMap> host_redirect_data_map, | 361 std::unique_ptr<RedirectDataMap> host_redirect_data_map, |
| 344 std::unique_ptr<ManifestDataMap> manifest_data_map); | 362 std::unique_ptr<ManifestDataMap> manifest_data_map, |
| 363 std::unique_ptr<OriginDataMap> origin_data_map); | |
| 345 | 364 |
| 346 // Called during initialization when history is read and the predictor | 365 // Called during initialization when history is read and the predictor |
| 347 // database has been read. | 366 // database has been read. |
| 348 void OnHistoryAndCacheLoaded(); | 367 void OnHistoryAndCacheLoaded(); |
| 349 | 368 |
| 350 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. | 369 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. |
| 351 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 370 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 352 | 371 |
| 353 // Deletes all URLs from the predictor database, the caches and removes all | 372 // Deletes all URLs from the predictor database, the caches and removes all |
| 354 // inflight navigations. | 373 // inflight navigations. |
| 355 void DeleteAllUrls(); | 374 void DeleteAllUrls(); |
| 356 | 375 |
| 357 // Deletes data for the input |urls| and their corresponding hosts from the | 376 // Deletes data for the input |urls| and their corresponding hosts from the |
| 358 // predictor database and caches. | 377 // predictor database and caches. |
| 359 void DeleteUrls(const history::URLRows& urls); | 378 void DeleteUrls(const history::URLRows& urls); |
| 360 | 379 |
| 361 // Callback for GetUrlVisitCountTask. | 380 // Callback for GetUrlVisitCountTask. |
| 362 void OnVisitCountLookup(size_t url_visit_count, | 381 void OnVisitCountLookup(size_t url_visit_count, |
| 363 const PageRequestSummary& summary); | 382 const PageRequestSummary& summary); |
| 364 | 383 |
| 365 // Removes the oldest entry in the input |data_map|, also deleting it from the | 384 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 366 // predictor database. | 385 // predictor database. |
| 367 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 386 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 368 PrefetchDataMap* data_map); | 387 PrefetchDataMap* data_map); |
| 369 | 388 |
| 370 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | 389 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, |
| 371 RedirectDataMap* data_map); | 390 RedirectDataMap* data_map); |
| 372 | 391 |
| 373 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map); | 392 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map); |
| 393 void RemoveOldestEntryInOriginDataMap(OriginDataMap* data_map); | |
| 374 | 394 |
| 375 // Merges resources in |new_resources| into the |data_map| and correspondingly | 395 // Merges resources in |new_resources| into the |data_map| and correspondingly |
| 376 // updates the predictor database. Also calls LearnRedirect if relevant. | 396 // updates the predictor database. Also calls LearnRedirect if relevant. |
| 377 void LearnNavigation(const std::string& key, | 397 void LearnNavigation(const std::string& key, |
| 378 PrefetchKeyType key_type, | 398 PrefetchKeyType key_type, |
| 379 const std::vector<URLRequestSummary>& new_resources, | 399 const std::vector<URLRequestSummary>& new_resources, |
| 380 size_t max_data_map_size, | 400 size_t max_data_map_size, |
| 381 PrefetchDataMap* data_map, | 401 PrefetchDataMap* data_map, |
| 382 const std::string& key_before_redirects, | 402 const std::string& key_before_redirects, |
| 383 RedirectDataMap* redirect_map); | 403 RedirectDataMap* redirect_map); |
| 384 | 404 |
| 385 // Updates information about final redirect destination for |key| in | 405 // Updates information about final redirect destination for |key| in |
| 386 // |redirect_map| and correspondingly updates the predictor database. | 406 // |redirect_map| and correspondingly updates the predictor database. |
| 387 void LearnRedirect(const std::string& key, | 407 void LearnRedirect(const std::string& key, |
| 388 PrefetchKeyType key_type, | 408 PrefetchKeyType key_type, |
| 389 const std::string& final_redirect, | 409 const std::string& final_redirect, |
| 390 size_t max_redirect_map_size, | 410 size_t max_redirect_map_size, |
| 391 RedirectDataMap* redirect_map); | 411 RedirectDataMap* redirect_map); |
| 392 | 412 |
| 413 void LearnOrigins(const std::string& host, | |
| 414 const std::map<GURL, OriginRequestSummary>& summaries, | |
| 415 size_t max_data_map_size, | |
| 416 OriginDataMap* data_map); | |
| 417 | |
| 393 // Reports database readiness metric defined as percentage of navigated hosts | 418 // Reports database readiness metric defined as percentage of navigated hosts |
| 394 // found in DB for last X entries in history. | 419 // found in DB for last X entries in history. |
| 395 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const; | 420 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const; |
| 396 | 421 |
| 397 // history::HistoryServiceObserver: | 422 // history::HistoryServiceObserver: |
| 398 void OnURLsDeleted(history::HistoryService* history_service, | 423 void OnURLsDeleted(history::HistoryService* history_service, |
| 399 bool all_history, | 424 bool all_history, |
| 400 bool expired, | 425 bool expired, |
| 401 const history::URLRows& deleted_rows, | 426 const history::URLRows& deleted_rows, |
| 402 const std::set<GURL>& favicon_urls) override; | 427 const std::set<GURL>& favicon_urls) override; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 419 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 444 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 420 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 445 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 421 base::CancelableTaskTracker history_lookup_consumer_; | 446 base::CancelableTaskTracker history_lookup_consumer_; |
| 422 | 447 |
| 423 // Copy of the data in the predictor tables. | 448 // Copy of the data in the predictor tables. |
| 424 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 449 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 425 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 450 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 426 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 451 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 427 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 452 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| 428 std::unique_ptr<ManifestDataMap> manifest_table_cache_; | 453 std::unique_ptr<ManifestDataMap> manifest_table_cache_; |
| 454 std::unique_ptr<OriginDataMap> origin_table_cache_; | |
| 429 | 455 |
| 430 std::map<GURL, base::TimeTicks> inflight_prefetches_; | 456 std::map<GURL, base::TimeTicks> inflight_prefetches_; |
| 431 NavigationMap inflight_navigations_; | 457 NavigationMap inflight_navigations_; |
| 432 | 458 |
| 433 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> | 459 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> |
| 434 prefetcher_stats_; | 460 prefetcher_stats_; |
| 435 | 461 |
| 436 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 462 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 437 history_service_observer_; | 463 history_service_observer_; |
| 438 | 464 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 465 | 491 |
| 466 private: | 492 private: |
| 467 ResourcePrefetchPredictor* predictor_; | 493 ResourcePrefetchPredictor* predictor_; |
| 468 | 494 |
| 469 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 495 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 470 }; | 496 }; |
| 471 | 497 |
| 472 } // namespace predictors | 498 } // namespace predictors |
| 473 | 499 |
| 474 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 500 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |