| 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/resource_prefetch_common.h" | 22 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 23 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 23 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 24 #include "chrome/browser/predictors/resource_prefetcher.h" | 24 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 25 #include "components/history/core/browser/history_db_task.h" | 25 #include "components/history/core/browser/history_db_task.h" |
| 26 #include "components/history/core/browser/history_service_observer.h" | 26 #include "components/history/core/browser/history_service_observer.h" |
| 27 #include "components/history/core/browser/history_types.h" | 27 #include "components/history/core/browser/history_types.h" |
| 28 #include "components/keyed_service/core/keyed_service.h" | 28 #include "components/keyed_service/core/keyed_service.h" |
| 29 #include "components/precache/content/precache_manager.h" |
| 29 #include "content/public/common/resource_type.h" | 30 #include "content/public/common/resource_type.h" |
| 30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 31 | 32 |
| 32 class PredictorsHandler; | 33 class PredictorsHandler; |
| 33 class Profile; | 34 class Profile; |
| 34 | 35 |
| 35 namespace net { | 36 namespace net { |
| 36 class URLRequest; | 37 class URLRequest; |
| 37 } | 38 } |
| 38 | 39 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // thread and the prefetchers on the IO thread. | 94 // thread and the prefetchers on the IO thread. |
| 94 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | 95 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
| 95 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 96 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
| 96 // | 97 // |
| 97 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 98 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
| 98 // recorded. Consider recording sub-frame responses independently or together | 99 // recorded. Consider recording sub-frame responses independently or together |
| 99 // with main frame. | 100 // with main frame. |
| 100 class ResourcePrefetchPredictor | 101 class ResourcePrefetchPredictor |
| 101 : public KeyedService, | 102 : public KeyedService, |
| 102 public history::HistoryServiceObserver, | 103 public history::HistoryServiceObserver, |
| 103 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { | 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, |
| 105 public precache::PrecacheManager::Delegate { |
| 104 public: | 106 public: |
| 105 // Stores the data that we need to get from the URLRequest. | 107 // Stores the data that we need to get from the URLRequest. |
| 106 struct URLRequestSummary { | 108 struct URLRequestSummary { |
| 107 URLRequestSummary(); | 109 URLRequestSummary(); |
| 108 URLRequestSummary(const URLRequestSummary& other); | 110 URLRequestSummary(const URLRequestSummary& other); |
| 109 ~URLRequestSummary(); | 111 ~URLRequestSummary(); |
| 110 | 112 |
| 111 NavigationID navigation_id; | 113 NavigationID navigation_id; |
| 112 GURL resource_url; | 114 GURL resource_url; |
| 113 content::ResourceType resource_type; | 115 content::ResourceType resource_type; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 const GURL& main_frame_url, | 219 const GURL& main_frame_url, |
| 218 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); | 220 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats); |
| 219 | 221 |
| 220 // Returns true if prefetching data exists for the |main_frame_url|. | 222 // Returns true if prefetching data exists for the |main_frame_url|. |
| 221 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); | 223 virtual bool IsUrlPrefetchable(const GURL& main_frame_url); |
| 222 | 224 |
| 223 // Returns true iff |resource| has sufficient confidence level and required | 225 // Returns true iff |resource| has sufficient confidence level and required |
| 224 // number of hits. | 226 // number of hits. |
| 225 bool IsResourcePrefetchable(const ResourceData& resource) const; | 227 bool IsResourcePrefetchable(const ResourceData& resource) const; |
| 226 | 228 |
| 229 // precache::PrecacheManager::Delegate: |
| 230 void OnManifestFetched(const std::string& host, |
| 231 const precache::PrecacheManifest& manifest) override; |
| 232 |
| 227 // Sets the |observer| to be notified when the resource prefetch predictor | 233 // Sets the |observer| to be notified when the resource prefetch predictor |
| 228 // data changes. Previously registered observer will be discarded. Call | 234 // data changes. Previously registered observer will be discarded. Call |
| 229 // this with nullptr parameter to de-register observer. | 235 // this with nullptr parameter to de-register observer. |
| 230 void SetObserverForTesting(TestObserver* observer); | 236 void SetObserverForTesting(TestObserver* observer); |
| 231 | 237 |
| 232 private: | 238 private: |
| 233 friend class ::PredictorsHandler; | 239 friend class ::PredictorsHandler; |
| 234 friend class ResourcePrefetchPredictorTest; | 240 friend class ResourcePrefetchPredictorTest; |
| 235 friend class ResourcePrefetchPredictorBrowserTest; | 241 friend class ResourcePrefetchPredictorBrowserTest; |
| 236 | 242 |
| 237 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 243 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 238 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 244 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 239 LazilyInitializeEmpty); | 245 LazilyInitializeEmpty); |
| 240 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 246 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 241 LazilyInitializeWithData); | 247 LazilyInitializeWithData); |
| 242 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 248 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 243 NavigationNotRecorded); | 249 NavigationNotRecorded); |
| 244 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 250 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 245 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 251 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 246 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 252 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 247 NavigationUrlNotInDBAndDBFull); | 253 NavigationUrlNotInDBAndDBFull); |
| 248 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | 254 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); |
| 249 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | 255 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); |
| 256 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostNotInDB); |
| 257 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostInDB); |
| 258 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 259 ManifestHostNotInDBAndDBFull); |
| 250 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 260 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 251 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 261 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 252 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 262 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 253 OnSubresourceResponse); | 263 OnSubresourceResponse); |
| 254 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 264 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 255 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 265 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 256 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 266 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 257 PopulatePrefetcherRequest); | 267 PopulatePrefetcherRequest); |
| 258 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 268 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 259 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 269 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 const PageRequestSummary& summary); | 363 const PageRequestSummary& summary); |
| 354 | 364 |
| 355 // Removes the oldest entry in the input |data_map|, also deleting it from the | 365 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 356 // predictor database. | 366 // predictor database. |
| 357 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 367 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 358 PrefetchDataMap* data_map); | 368 PrefetchDataMap* data_map); |
| 359 | 369 |
| 360 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | 370 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, |
| 361 RedirectDataMap* data_map); | 371 RedirectDataMap* data_map); |
| 362 | 372 |
| 373 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map); |
| 374 |
| 363 // Merges resources in |new_resources| into the |data_map| and correspondingly | 375 // Merges resources in |new_resources| into the |data_map| and correspondingly |
| 364 // updates the predictor database. Also calls LearnRedirect if relevant. | 376 // updates the predictor database. Also calls LearnRedirect if relevant. |
| 365 void LearnNavigation(const std::string& key, | 377 void LearnNavigation(const std::string& key, |
| 366 PrefetchKeyType key_type, | 378 PrefetchKeyType key_type, |
| 367 const std::vector<URLRequestSummary>& new_resources, | 379 const std::vector<URLRequestSummary>& new_resources, |
| 368 size_t max_data_map_size, | 380 size_t max_data_map_size, |
| 369 PrefetchDataMap* data_map, | 381 PrefetchDataMap* data_map, |
| 370 const std::string& key_before_redirects, | 382 const std::string& key_before_redirects, |
| 371 RedirectDataMap* redirect_map); | 383 RedirectDataMap* redirect_map); |
| 372 | 384 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 | 465 |
| 454 private: | 466 private: |
| 455 ResourcePrefetchPredictor* predictor_; | 467 ResourcePrefetchPredictor* predictor_; |
| 456 | 468 |
| 457 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 469 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 458 }; | 470 }; |
| 459 | 471 |
| 460 } // namespace predictors | 472 } // namespace predictors |
| 461 | 473 |
| 462 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 474 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |