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 81 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 { | |
|
pasko
2016/09/22 14:27:09
The name 'Navigation' looks a bit generic, so it s
alexilin
2016/09/22 16:48:19
I don't think that 'RedirectChain' is also a good
pasko
2016/09/26 12:28:15
I would also prefer moving it into the implementat
alexilin
2016/09/27 14:52:45
Renamed to 'PageRequestSummary' and moved into pri
| |
| 104 explicit Navigation(const GURL& initial_url); | |
| 105 ~Navigation(); | |
| 106 | |
| 107 GURL initial_url; | |
| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 155 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 147 LazilyInitializeEmpty); | 156 LazilyInitializeEmpty); |
| 148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 149 LazilyInitializeWithData); | 158 LazilyInitializeWithData); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 159 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 151 NavigationNotRecorded); | 160 NavigationNotRecorded); |
| 152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 162 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 163 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 155 NavigationUrlNotInDBAndDBFull); | 164 NavigationUrlNotInDBAndDBFull); |
| 165 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | |
| 166 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | |
| 156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 167 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 168 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 169 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 159 OnSubresourceResponse); | 170 OnSubresourceResponse); |
| 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 171 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 172 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 162 | 173 |
| 163 enum InitializationState { | 174 enum InitializationState { |
| 164 NOT_INITIALIZED = 0, | 175 NOT_INITIALIZED = 0, |
| 165 INITIALIZING = 1, | 176 INITIALIZING = 1, |
| 166 INITIALIZED = 2 | 177 INITIALIZED = 2 |
| 167 }; | 178 }; |
| 168 | 179 |
| 169 // Stores prefetching results. | 180 // Stores prefetching results. |
| 170 struct Result { | 181 struct Result { |
| 171 // Takes ownership of requests. | 182 // Takes ownership of requests. |
| 172 Result(PrefetchKeyType key_type, | 183 Result(PrefetchKeyType key_type, |
| 173 ResourcePrefetcher::RequestVector* requests); | 184 ResourcePrefetcher::RequestVector* requests); |
| 174 ~Result(); | 185 ~Result(); |
| 175 | 186 |
| 176 PrefetchKeyType key_type; | 187 PrefetchKeyType key_type; |
| 177 std::unique_ptr<ResourcePrefetcher::RequestVector> requests; | 188 std::unique_ptr<ResourcePrefetcher::RequestVector> requests; |
| 178 | 189 |
| 179 private: | 190 private: |
| 180 DISALLOW_COPY_AND_ASSIGN(Result); | 191 DISALLOW_COPY_AND_ASSIGN(Result); |
| 181 }; | 192 }; |
| 182 | 193 |
| 183 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; | 194 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; |
| 184 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 195 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 185 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > > | 196 |
| 186 NavigationMap; | 197 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 198 | |
| 199 typedef std::map<NavigationID, std::unique_ptr<Navigation>> NavigationMap; | |
| 187 typedef std::map<NavigationID, std::unique_ptr<Result>> ResultsMap; | 200 typedef std::map<NavigationID, std::unique_ptr<Result>> ResultsMap; |
| 188 | 201 |
| 189 // Returns true if the main page request is supported for prediction. | 202 // Returns true if the main page request is supported for prediction. |
| 190 static bool IsHandledMainPage(net::URLRequest* request); | 203 static bool IsHandledMainPage(net::URLRequest* request); |
| 191 | 204 |
| 192 // Returns true if the subresource request is supported for prediction. | 205 // Returns true if the subresource request is supported for prediction. |
| 193 static bool IsHandledSubresource(net::URLRequest* request, | 206 static bool IsHandledSubresource(net::URLRequest* request, |
| 194 content::ResourceType resource_type); | 207 content::ResourceType resource_type); |
| 195 | 208 |
| 196 // Returns true if the subresource has a supported type. | 209 // Returns true if the subresource has a supported type. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 void StartPrefetching(const NavigationID& navigation_id); | 244 void StartPrefetching(const NavigationID& navigation_id); |
| 232 | 245 |
| 233 // Stops prefetching that may be in progress corresponding to |navigation_id|. | 246 // Stops prefetching that may be in progress corresponding to |navigation_id|. |
| 234 void StopPrefetching(const NavigationID& navigation_id); | 247 void StopPrefetching(const NavigationID& navigation_id); |
| 235 | 248 |
| 236 // Starts initialization by posting a task to the DB thread to read the | 249 // Starts initialization by posting a task to the DB thread to read the |
| 237 // predictor database. | 250 // predictor database. |
| 238 void StartInitialization(); | 251 void StartInitialization(); |
| 239 | 252 |
| 240 // Callback for task to read predictor database. Takes ownership of | 253 // Callback for task to read predictor database. Takes ownership of |
| 241 // |url_data_map| and |host_data_map|. | 254 // all arguments. |
| 242 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 255 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 243 std::unique_ptr<PrefetchDataMap> host_data_map); | 256 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 257 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | |
| 258 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | |
| 244 | 259 |
| 245 // Called during initialization when history is read and the predictor | 260 // Called during initialization when history is read and the predictor |
| 246 // database has been read. | 261 // database has been read. |
| 247 void OnHistoryAndCacheLoaded(); | 262 void OnHistoryAndCacheLoaded(); |
| 248 | 263 |
| 249 // Removes data for navigations where the onload never fired. Will cleanup | 264 // Removes data for navigations where the onload never fired. Will cleanup |
| 250 // inflight_navigations_ and results_map_. | 265 // inflight_navigations_ and results_map_. |
| 251 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 266 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 252 | 267 |
| 253 // Deletes all URLs from the predictor database, the caches and removes all | 268 // Deletes all URLs from the predictor database, the caches and removes all |
| 254 // inflight navigations. | 269 // inflight navigations. |
| 255 void DeleteAllUrls(); | 270 void DeleteAllUrls(); |
| 256 | 271 |
| 257 // Deletes data for the input |urls| and their corresponding hosts from the | 272 // Deletes data for the input |urls| and their corresponding hosts from the |
| 258 // predictor database and caches. | 273 // predictor database and caches. |
| 259 void DeleteUrls(const history::URLRows& urls); | 274 void DeleteUrls(const history::URLRows& urls); |
| 260 | 275 |
| 261 // Callback for GetUrlVisitCountTask. | 276 // Callback for GetUrlVisitCountTask. |
| 262 void OnVisitCountLookup(size_t visit_count, | 277 void OnVisitCountLookup(size_t visit_count, |
| 263 const NavigationID& navigation_id, | 278 const NavigationID& navigation_id, |
| 264 const std::vector<URLRequestSummary>& requests); | 279 const Navigation& navigation); |
| 265 | 280 |
| 266 // Removes the oldest entry in the input |data_map|, also deleting it from the | 281 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 267 // predictor database. | 282 // predictor database. |
| 268 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 283 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 269 PrefetchDataMap* data_map); | 284 PrefetchDataMap* data_map); |
| 270 | 285 |
| 286 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | |
| 287 RedirectDataMap* data_map); | |
| 288 | |
| 271 // Merges resources in |new_resources| into the |data_map| and correspondingly | 289 // Merges resources in |new_resources| into the |data_map| and correspondingly |
| 272 // updates the predictor database. | 290 // updates the predictor database. Also calls LearnRedirect if relevant. |
| 273 void LearnNavigation(const std::string& key, | 291 void LearnNavigation(const std::string& key, |
| 274 PrefetchKeyType key_type, | 292 PrefetchKeyType key_type, |
| 275 const std::vector<URLRequestSummary>& new_resources, | 293 const std::vector<URLRequestSummary>& new_resources, |
| 276 size_t max_data_map_size, | 294 size_t max_data_map_size, |
| 277 PrefetchDataMap* data_map); | 295 PrefetchDataMap* data_map, |
| 296 const std::string& redirect_origin_key, | |
| 297 RedirectDataMap* redirect_map); | |
| 298 | |
| 299 // Updates information about final redirect destination for |key| in | |
| 300 // |redirect_map| and correspondingly updates the predictor database | |
| 301 void LearnRedirect(const std::string& key, | |
| 302 PrefetchKeyType key_type, | |
| 303 const std::string& final_redirect, | |
| 304 size_t max_redirect_map_size, | |
| 305 RedirectDataMap* redirect_map); | |
| 278 | 306 |
| 279 // Reports overall page load time. | 307 // Reports overall page load time. |
| 280 void ReportPageLoadTimeStats(base::TimeDelta plt) const; | 308 void ReportPageLoadTimeStats(base::TimeDelta plt) const; |
| 281 | 309 |
| 282 // Reports page load time for prefetched and not prefetched pages | 310 // Reports page load time for prefetched and not prefetched pages |
| 283 void ReportPageLoadTimePrefetchStats( | 311 void ReportPageLoadTimePrefetchStats( |
| 284 base::TimeDelta plt, | 312 base::TimeDelta plt, |
| 285 bool prefetched, | 313 bool prefetched, |
| 286 base::Callback<void(int)> report_network_type_callback, | 314 base::Callback<void(int)> report_network_type_callback, |
| 287 PrefetchKeyType key_type) const; | 315 PrefetchKeyType key_type) const; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 323 tables_ = tables; | 351 tables_ = tables; |
| 324 } | 352 } |
| 325 | 353 |
| 326 Profile* const profile_; | 354 Profile* const profile_; |
| 327 ResourcePrefetchPredictorConfig const config_; | 355 ResourcePrefetchPredictorConfig const config_; |
| 328 InitializationState initialization_state_; | 356 InitializationState initialization_state_; |
| 329 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 357 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 330 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 358 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 331 base::CancelableTaskTracker history_lookup_consumer_; | 359 base::CancelableTaskTracker history_lookup_consumer_; |
| 332 | 360 |
| 333 // Map of all the navigations in flight to their resource requests. | |
| 334 NavigationMap inflight_navigations_; | |
| 335 | |
| 336 // Copy of the data in the predictor tables. | 361 // Copy of the data in the predictor tables. |
| 337 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 362 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 338 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 363 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 364 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | |
| 365 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | |
| 339 | 366 |
| 367 NavigationMap inflight_navigations_; | |
| 340 ResultsMap results_map_; | 368 ResultsMap results_map_; |
| 341 | 369 |
| 342 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 370 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 343 history_service_observer_; | 371 history_service_observer_; |
| 344 | 372 |
| 345 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 373 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 346 }; | 374 }; |
| 347 | 375 |
| 348 } // namespace predictors | 376 } // namespace predictors |
| 349 | 377 |
| 350 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 378 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |