| 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/glowplug_key_value_data.h" | 22 #include "chrome/browser/predictors/glowplug_key_value_data.h" |
| 23 #include "chrome/browser/predictors/loading_predictor_config.h" | 23 #include "chrome/browser/predictors/loading_predictor_config.h" |
| 24 #include "chrome/browser/predictors/resource_prefetch_common.h" | 24 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 25 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 25 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 26 #include "chrome/browser/predictors/resource_prefetcher.h" | 26 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 27 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 27 #include "components/history/core/browser/history_db_task.h" | 28 #include "components/history/core/browser/history_db_task.h" |
| 28 #include "components/history/core/browser/history_service_observer.h" | 29 #include "components/history/core/browser/history_service_observer.h" |
| 29 #include "components/history/core/browser/history_types.h" | 30 #include "components/history/core/browser/history_types.h" |
| 30 #include "components/keyed_service/core/keyed_service.h" | 31 #include "components/keyed_service/core/keyed_service.h" |
| 31 #include "components/precache/content/precache_manager.h" | 32 #include "components/precache/content/precache_manager.h" |
| 32 #include "content/public/common/resource_type.h" | 33 #include "content/public/common/resource_type.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 class PredictorsHandler; | 36 class PredictorsHandler; |
| 36 class Profile; | 37 class Profile; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 REDIRECT_WRONG_PREDICTED, | 208 REDIRECT_WRONG_PREDICTED, |
| 208 REDIRECT_CORRECTLY_PREDICTED, | 209 REDIRECT_CORRECTLY_PREDICTED, |
| 209 MAX | 210 MAX |
| 210 }; | 211 }; |
| 211 | 212 |
| 212 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 213 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
| 213 Profile* profile); | 214 Profile* profile); |
| 214 ~ResourcePrefetchPredictor() override; | 215 ~ResourcePrefetchPredictor() override; |
| 215 | 216 |
| 216 // Starts initialization by posting a task to the DB thread to read the | 217 // Starts initialization by posting a task to the DB thread to read the |
| 217 // predictor database. | 218 // predictor database. Virtual for testing. |
| 218 void StartInitialization(); | 219 virtual void StartInitialization(); |
| 219 void Shutdown(); | 220 virtual void Shutdown(); |
| 220 | 221 |
| 221 // Thread safe. | 222 // Thread safe. |
| 222 static bool ShouldRecordRequest(net::URLRequest* request, | 223 static bool ShouldRecordRequest(net::URLRequest* request, |
| 223 content::ResourceType resource_type); | 224 content::ResourceType resource_type); |
| 224 static bool ShouldRecordResponse(net::URLRequest* response); | 225 static bool ShouldRecordResponse(net::URLRequest* response); |
| 225 static bool ShouldRecordRedirect(net::URLRequest* response); | 226 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 226 | 227 |
| 227 // Determines the resource type from the declared one, falling back to MIME | 228 // Determines the resource type from the declared one, falling back to MIME |
| 228 // type detection when it is not explicit. | 229 // type detection when it is not explicit. |
| 229 static content::ResourceType GetResourceType( | 230 static content::ResourceType GetResourceType( |
| (...skipping 21 matching lines...) Expand all Loading... |
| 251 const NavigationID& navigation_id, | 252 const NavigationID& navigation_id, |
| 252 const base::TimeTicks& first_contentful_paint); | 253 const base::TimeTicks& first_contentful_paint); |
| 253 | 254 |
| 254 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending | 255 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending |
| 255 // in flight. | 256 // in flight. |
| 256 void OnPrefetchingFinished( | 257 void OnPrefetchingFinished( |
| 257 const GURL& main_frame_url, | 258 const GURL& main_frame_url, |
| 258 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 259 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 259 | 260 |
| 260 // Returns true if prefetching data exists for the |main_frame_url|. | 261 // Returns true if prefetching data exists for the |main_frame_url|. |
| 261 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); | 262 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; |
| 262 | 263 |
| 263 // Returns true iff |resource| has sufficient confidence level and required | 264 // Returns true iff |resource| has sufficient confidence level and required |
| 264 // number of hits. | 265 // number of hits. |
| 265 bool IsResourcePrefetchable(const ResourceData& resource) const; | 266 bool IsResourcePrefetchable(const ResourceData& resource) const; |
| 266 | 267 |
| 267 // precache::PrecacheManager::Delegate: | 268 // precache::PrecacheManager::Delegate: |
| 268 void OnManifestFetched(const std::string& host, | 269 void OnManifestFetched(const std::string& host, |
| 269 const precache::PrecacheManifest& manifest) override; | 270 const precache::PrecacheManifest& manifest) override; |
| 270 | 271 |
| 271 // Sets the |observer| to be notified when the resource prefetch predictor | 272 // Sets the |observer| to be notified when the resource prefetch predictor |
| 272 // data changes. Previously registered observer will be discarded. Call | 273 // data changes. Previously registered observer will be discarded. Call |
| 273 // this with nullptr parameter to de-register observer. | 274 // this with nullptr parameter to de-register observer. |
| 274 void SetObserverForTesting(TestObserver* observer); | 275 void SetObserverForTesting(TestObserver* observer); |
| 275 | 276 |
| 276 private: | 277 private: |
| 277 // Starts prefetching if it is enabled for |origin| and prefetching data | 278 // Starts prefetching if it is enabled and prefetching data exists for the |
| 278 // exists for the |main_frame_url| either at the URL or at the host level. | 279 // |main_frame_url| either at the URL or at the host level. |
| 279 void StartPrefetching(const GURL& main_frame_url, HintOrigin origin); | 280 void StartPrefetching(const GURL& main_frame_url); |
| 280 | 281 |
| 281 // Stops prefetching that may be in progress corresponding to | 282 // Stops prefetching that may be in progress corresponding to |
| 282 // |main_frame_url|. | 283 // |main_frame_url|. |
| 283 void StopPrefetching(const GURL& main_frame_url); | 284 void StopPrefetching(const GURL& main_frame_url); |
| 284 | 285 |
| 285 friend class LoadingPredictor; | 286 friend class LoadingPredictor; |
| 286 friend class ::PredictorsHandler; | 287 friend class ::PredictorsHandler; |
| 287 friend class ResourcePrefetchPredictorTest; | 288 friend class ResourcePrefetchPredictorTest; |
| 288 friend class ResourcePrefetchPredictorBrowserTest; | 289 friend class ResourcePrefetchPredictorBrowserTest; |
| 289 | 290 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 const std::string& mime_type); | 354 const std::string& mime_type); |
| 354 | 355 |
| 355 // Returns true if the request (should have a response in it) is "no-store". | 356 // Returns true if the request (should have a response in it) is "no-store". |
| 356 static bool IsNoStore(const net::URLRequest& request); | 357 static bool IsNoStore(const net::URLRequest& request); |
| 357 | 358 |
| 358 static void SetAllowPortInUrlsForTesting(bool state); | 359 static void SetAllowPortInUrlsForTesting(bool state); |
| 359 | 360 |
| 360 // Functions called on different network events pertaining to the loading of | 361 // Functions called on different network events pertaining to the loading of |
| 361 // main frame resource or sub resources. | 362 // main frame resource or sub resources. |
| 362 void OnMainFrameRequest(const URLRequestSummary& request); | 363 void OnMainFrameRequest(const URLRequestSummary& request); |
| 363 void OnMainFrameResponse(const URLRequestSummary& response); | |
| 364 void OnMainFrameRedirect(const URLRequestSummary& response); | 364 void OnMainFrameRedirect(const URLRequestSummary& response); |
| 365 void OnSubresourceResponse(const URLRequestSummary& response); | 365 void OnSubresourceResponse(const URLRequestSummary& response); |
| 366 void OnSubresourceRedirect(const URLRequestSummary& response); | 366 void OnSubresourceRedirect(const URLRequestSummary& response); |
| 367 | 367 |
| 368 // Called when onload completes for a navigation. We treat this point as the | 368 // Called when onload completes for a navigation. We treat this point as the |
| 369 // "completion" of the navigation. The resources requested by the page up to | 369 // "completion" of the navigation. The resources requested by the page up to |
| 370 // this point are the only ones considered for prefetching. | 370 // this point are the only ones considered for prefetching. |
| 371 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); | 371 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); |
| 372 | 372 |
| 373 // Returns true iff one of the following conditions is true | 373 // Returns true iff one of the following conditions is true |
| (...skipping 24 matching lines...) Expand all Loading... |
| 398 // prefetched. |urls| may be nullptr to get the return value only. | 398 // prefetched. |urls| may be nullptr to get the return value only. |
| 399 bool PopulateFromManifest(const std::string& manifest_host, | 399 bool PopulateFromManifest(const std::string& manifest_host, |
| 400 std::vector<GURL>* urls) const; | 400 std::vector<GURL>* urls) const; |
| 401 | 401 |
| 402 void InitializeOnDBThread(); | 402 void InitializeOnDBThread(); |
| 403 | 403 |
| 404 // Called during initialization when history is read and the predictor | 404 // Called during initialization when history is read and the predictor |
| 405 // database has been read. | 405 // database has been read. |
| 406 void OnHistoryAndCacheLoaded(); | 406 void OnHistoryAndCacheLoaded(); |
| 407 | 407 |
| 408 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. | 408 // Cleanup inflight_navigations_, and prefetcher_stats_. |
| 409 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 409 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 410 | 410 |
| 411 // Deletes all URLs from the predictor database, the caches and removes all | 411 // Deletes all URLs from the predictor database, the caches and removes all |
| 412 // inflight navigations. | 412 // inflight navigations. |
| 413 void DeleteAllUrls(); | 413 void DeleteAllUrls(); |
| 414 | 414 |
| 415 // Deletes data for the input |urls| and their corresponding hosts from the | 415 // Deletes data for the input |urls| and their corresponding hosts from the |
| 416 // predictor database and caches. | 416 // predictor database and caches. |
| 417 void DeleteUrls(const history::URLRows& urls); | 417 void DeleteUrls(const history::URLRows& urls); |
| 418 | 418 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 | 456 |
| 457 // Used to connect to HistoryService or register for service loaded | 457 // Used to connect to HistoryService or register for service loaded |
| 458 // notificatioan. | 458 // notificatioan. |
| 459 void ConnectToHistoryService(); | 459 void ConnectToHistoryService(); |
| 460 | 460 |
| 461 // Used for testing to inject mock tables. | 461 // Used for testing to inject mock tables. |
| 462 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 462 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 463 tables_ = tables; | 463 tables_ = tables; |
| 464 } | 464 } |
| 465 | 465 |
| 466 // For testing. |
| 467 void set_mock_resource_prefetcher_manager( |
| 468 scoped_refptr<ResourcePrefetcherManager> prefetch_manager) { |
| 469 prefetch_manager_ = prefetch_manager; |
| 470 } |
| 471 |
| 466 Profile* const profile_; | 472 Profile* const profile_; |
| 467 TestObserver* observer_; | 473 TestObserver* observer_; |
| 468 const LoadingPredictorConfig config_; | 474 const LoadingPredictorConfig config_; |
| 469 InitializationState initialization_state_; | 475 InitializationState initialization_state_; |
| 470 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 476 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 471 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 477 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 472 base::CancelableTaskTracker history_lookup_consumer_; | 478 base::CancelableTaskTracker history_lookup_consumer_; |
| 473 | 479 |
| 474 std::unique_ptr<PrefetchDataMap> url_resource_data_; | 480 std::unique_ptr<PrefetchDataMap> url_resource_data_; |
| 475 std::unique_ptr<PrefetchDataMap> host_resource_data_; | 481 std::unique_ptr<PrefetchDataMap> host_resource_data_; |
| 476 std::unique_ptr<RedirectDataMap> url_redirect_data_; | 482 std::unique_ptr<RedirectDataMap> url_redirect_data_; |
| 477 std::unique_ptr<RedirectDataMap> host_redirect_data_; | 483 std::unique_ptr<RedirectDataMap> host_redirect_data_; |
| 478 std::unique_ptr<ManifestDataMap> manifest_data_; | 484 std::unique_ptr<ManifestDataMap> manifest_data_; |
| 479 std::unique_ptr<OriginDataMap> origin_data_; | 485 std::unique_ptr<OriginDataMap> origin_data_; |
| 480 | 486 |
| 481 std::map<GURL, base::TimeTicks> inflight_prefetches_; | |
| 482 NavigationMap inflight_navigations_; | 487 NavigationMap inflight_navigations_; |
| 483 | 488 |
| 484 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> | 489 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> |
| 485 prefetcher_stats_; | 490 prefetcher_stats_; |
| 486 | 491 |
| 487 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 492 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 488 history_service_observer_; | 493 history_service_observer_; |
| 489 | 494 |
| 490 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 495 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 491 }; | 496 }; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 516 | 521 |
| 517 private: | 522 private: |
| 518 ResourcePrefetchPredictor* predictor_; | 523 ResourcePrefetchPredictor* predictor_; |
| 519 | 524 |
| 520 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 525 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 521 }; | 526 }; |
| 522 | 527 |
| 523 } // namespace predictors | 528 } // namespace predictors |
| 524 | 529 |
| 525 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 530 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |