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