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> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
|
Benoit L
2016/09/26 12:51:14
Is it still needed?
alexilin
2016/09/26 15:38:28
Done.
| |
| 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_service_observer.h" | 25 #include "components/history/core/browser/history_service_observer.h" |
| 26 #include "components/history/core/browser/history_types.h" | 26 #include "components/history/core/browser/history_types.h" |
| 27 #include "components/keyed_service/core/keyed_service.h" | 27 #include "components/keyed_service/core/keyed_service.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 | 92 |
| 93 bool has_validators; | 93 bool has_validators; |
| 94 bool always_revalidate; | 94 bool always_revalidate; |
| 95 | 95 |
| 96 // Initializes a |URLRequestSummary| from a |URLRequest| response. | 96 // Initializes a |URLRequestSummary| from a |URLRequest| response. |
| 97 // Returns true for success. | 97 // Returns true for success. |
| 98 static bool SummarizeResponse(const net::URLRequest& request, | 98 static bool SummarizeResponse(const net::URLRequest& request, |
| 99 URLRequestSummary* summary); | 99 URLRequestSummary* summary); |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 // Stores information about inflight navigations. | |
| 103 struct Navigation { | |
| 104 explicit Navigation(const GURL& initial_url); | |
| 105 ~Navigation(); | |
| 106 | |
| 107 GURL initial_url; | |
|
Benoit L
2016/09/26 12:51:14
nit: Can you add a comment to specify whether this
alexilin
2016/09/26 15:38:28
Done.
| |
| 108 std::vector<URLRequestSummary> subresource_requests; | |
| 109 }; | |
| 110 | |
| 102 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, | 111 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 103 Profile* profile); | 112 Profile* profile); |
| 104 ~ResourcePrefetchPredictor() override; | 113 ~ResourcePrefetchPredictor() override; |
| 105 | 114 |
| 106 // Thread safe. | 115 // Thread safe. |
| 107 static bool ShouldRecordRequest(net::URLRequest* request, | 116 static bool ShouldRecordRequest(net::URLRequest* request, |
| 108 content::ResourceType resource_type); | 117 content::ResourceType resource_type); |
| 109 static bool ShouldRecordResponse(net::URLRequest* response); | 118 static bool ShouldRecordResponse(net::URLRequest* response); |
| 110 static bool ShouldRecordRedirect(net::URLRequest* response); | 119 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 111 | 120 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 140 LazilyInitializeEmpty); | 149 LazilyInitializeEmpty); |
| 141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 142 LazilyInitializeWithData); | 151 LazilyInitializeWithData); |
| 143 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 144 NavigationNotRecorded); | 153 NavigationNotRecorded); |
| 145 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 155 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 148 NavigationUrlNotInDBAndDBFull); | 157 NavigationUrlNotInDBAndDBFull); |
| 158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | |
| 159 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | |
| 149 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 162 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 152 OnSubresourceResponse); | 163 OnSubresourceResponse); |
| 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 164 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 165 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 155 | 166 |
| 156 enum InitializationState { | 167 enum InitializationState { |
| 157 NOT_INITIALIZED = 0, | 168 NOT_INITIALIZED = 0, |
| 158 INITIALIZING = 1, | 169 INITIALIZING = 1, |
| 159 INITIALIZED = 2 | 170 INITIALIZED = 2 |
| 160 }; | 171 }; |
| 161 | 172 |
| 162 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; | 173 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; |
| 163 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 174 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 164 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > > | 175 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 165 NavigationMap; | 176 |
| 177 typedef std::map<NavigationID, std::unique_ptr<Navigation>> NavigationMap; | |
| 166 | 178 |
| 167 // Returns true if the main page request is supported for prediction. | 179 // Returns true if the main page request is supported for prediction. |
| 168 static bool IsHandledMainPage(net::URLRequest* request); | 180 static bool IsHandledMainPage(net::URLRequest* request); |
| 169 | 181 |
| 170 // Returns true if the subresource request is supported for prediction. | 182 // Returns true if the subresource request is supported for prediction. |
| 171 static bool IsHandledSubresource(net::URLRequest* request, | 183 static bool IsHandledSubresource(net::URLRequest* request, |
| 172 content::ResourceType resource_type); | 184 content::ResourceType resource_type); |
| 173 | 185 |
| 174 // Returns true if the subresource has a supported type. | 186 // Returns true if the subresource has a supported type. |
| 175 static bool IsHandledResourceType(content::ResourceType resource_type, | 187 static bool IsHandledResourceType(content::ResourceType resource_type, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 void StartPrefetching(const NavigationID& navigation_id); | 221 void StartPrefetching(const NavigationID& navigation_id); |
| 210 | 222 |
| 211 // Stops prefetching that may be in progress corresponding to |navigation_id|. | 223 // Stops prefetching that may be in progress corresponding to |navigation_id|. |
| 212 void StopPrefetching(const NavigationID& navigation_id); | 224 void StopPrefetching(const NavigationID& navigation_id); |
| 213 | 225 |
| 214 // Starts initialization by posting a task to the DB thread to read the | 226 // Starts initialization by posting a task to the DB thread to read the |
| 215 // predictor database. | 227 // predictor database. |
| 216 void StartInitialization(); | 228 void StartInitialization(); |
| 217 | 229 |
| 218 // Callback for task to read predictor database. Takes ownership of | 230 // Callback for task to read predictor database. Takes ownership of |
| 219 // |url_data_map| and |host_data_map|. | 231 // all arguments. |
| 220 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 232 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 221 std::unique_ptr<PrefetchDataMap> host_data_map); | 233 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 234 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | |
| 235 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | |
| 222 | 236 |
| 223 // Called during initialization when history is read and the predictor | 237 // Called during initialization when history is read and the predictor |
| 224 // database has been read. | 238 // database has been read. |
| 225 void OnHistoryAndCacheLoaded(); | 239 void OnHistoryAndCacheLoaded(); |
| 226 | 240 |
| 227 // Removes data for navigations where the onload never fired. Will cleanup | 241 // Removes data for navigations where the onload never fired. Will cleanup |
| 228 // inflight_navigations_. | 242 // inflight_navigations_. |
| 229 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 243 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 230 | 244 |
| 231 // Deletes all URLs from the predictor database, the caches and removes all | 245 // Deletes all URLs from the predictor database, the caches and removes all |
| 232 // inflight navigations. | 246 // inflight navigations. |
| 233 void DeleteAllUrls(); | 247 void DeleteAllUrls(); |
| 234 | 248 |
| 235 // Deletes data for the input |urls| and their corresponding hosts from the | 249 // Deletes data for the input |urls| and their corresponding hosts from the |
| 236 // predictor database and caches. | 250 // predictor database and caches. |
| 237 void DeleteUrls(const history::URLRows& urls); | 251 void DeleteUrls(const history::URLRows& urls); |
| 238 | 252 |
| 239 // Callback for GetUrlVisitCountTask. | 253 // Callback for GetUrlVisitCountTask. |
| 240 void OnVisitCountLookup(size_t visit_count, | 254 void OnVisitCountLookup(size_t visit_count, |
| 241 const NavigationID& navigation_id, | 255 const NavigationID& navigation_id, |
| 242 const std::vector<URLRequestSummary>& requests); | 256 const Navigation& navigation); |
| 243 | 257 |
| 244 // Removes the oldest entry in the input |data_map|, also deleting it from the | 258 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 245 // predictor database. | 259 // predictor database. |
| 246 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 260 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 247 PrefetchDataMap* data_map); | 261 PrefetchDataMap* data_map); |
| 248 | 262 |
| 263 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | |
| 264 RedirectDataMap* data_map); | |
| 265 | |
| 249 // Merges resources in |new_resources| into the |data_map| and correspondingly | 266 // Merges resources in |new_resources| into the |data_map| and correspondingly |
| 250 // updates the predictor database. | 267 // updates the predictor database. Also calls LearnRedirect if relevant. |
| 251 void LearnNavigation(const std::string& key, | 268 void LearnNavigation(const std::string& key, |
| 252 PrefetchKeyType key_type, | 269 PrefetchKeyType key_type, |
| 253 const std::vector<URLRequestSummary>& new_resources, | 270 const std::vector<URLRequestSummary>& new_resources, |
| 254 size_t max_data_map_size, | 271 size_t max_data_map_size, |
| 255 PrefetchDataMap* data_map); | 272 PrefetchDataMap* data_map, |
| 273 const std::string& redirect_origin_key, | |
| 274 RedirectDataMap* redirect_map); | |
| 275 | |
| 276 // Updates information about final redirect destination for |key| in | |
| 277 // |redirect_map| and correspondingly updates the predictor database | |
| 278 void LearnRedirect(const std::string& key, | |
| 279 PrefetchKeyType key_type, | |
| 280 const std::string& final_redirect, | |
| 281 size_t max_redirect_map_size, | |
| 282 RedirectDataMap* redirect_map); | |
| 256 | 283 |
| 257 // Reports overall page load time. | 284 // Reports overall page load time. |
| 258 void ReportPageLoadTimeStats(base::TimeDelta plt) const; | 285 void ReportPageLoadTimeStats(base::TimeDelta plt) const; |
| 259 | 286 |
| 260 // Reports page load time for prefetched and not prefetched pages | 287 // Reports page load time for prefetched and not prefetched pages |
| 261 void ReportPageLoadTimePrefetchStats( | 288 void ReportPageLoadTimePrefetchStats( |
| 262 base::TimeDelta plt, | 289 base::TimeDelta plt, |
| 263 bool prefetched, | 290 bool prefetched, |
| 264 base::Callback<void(int)> report_network_type_callback, | 291 base::Callback<void(int)> report_network_type_callback, |
| 265 PrefetchKeyType key_type) const; | 292 PrefetchKeyType key_type) const; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 282 tables_ = tables; | 309 tables_ = tables; |
| 283 } | 310 } |
| 284 | 311 |
| 285 Profile* const profile_; | 312 Profile* const profile_; |
| 286 ResourcePrefetchPredictorConfig const config_; | 313 ResourcePrefetchPredictorConfig const config_; |
| 287 InitializationState initialization_state_; | 314 InitializationState initialization_state_; |
| 288 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 315 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 289 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 316 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 290 base::CancelableTaskTracker history_lookup_consumer_; | 317 base::CancelableTaskTracker history_lookup_consumer_; |
| 291 | 318 |
| 292 // Map of all the navigations in flight to their resource requests. | |
| 293 NavigationMap inflight_navigations_; | |
| 294 | |
| 295 // Copy of the data in the predictor tables. | 319 // Copy of the data in the predictor tables. |
| 296 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 320 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 297 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 321 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 322 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | |
| 323 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | |
| 324 | |
| 325 NavigationMap inflight_navigations_; | |
| 298 | 326 |
| 299 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 327 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 300 history_service_observer_; | 328 history_service_observer_; |
| 301 | 329 |
| 302 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 330 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 303 }; | 331 }; |
| 304 | 332 |
| 305 } // namespace predictors | 333 } // namespace predictors |
| 306 | 334 |
| 307 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 335 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |