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> | |
| 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_db_task.h" | |
| 25 #include "components/history/core/browser/history_service_observer.h" | 26 #include "components/history/core/browser/history_service_observer.h" |
| 26 #include "components/history/core/browser/history_types.h" | 27 #include "components/history/core/browser/history_types.h" |
| 27 #include "components/keyed_service/core/keyed_service.h" | 28 #include "components/keyed_service/core/keyed_service.h" |
| 28 #include "content/public/common/resource_type.h" | 29 #include "content/public/common/resource_type.h" |
| 29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 30 | 31 |
| 31 class PredictorsHandler; | 32 class PredictorsHandler; |
| 32 class Profile; | 33 class Profile; |
| 33 | 34 |
| 34 namespace net { | 35 namespace net { |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 140 LazilyInitializeEmpty); | 141 LazilyInitializeEmpty); |
| 141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 142 LazilyInitializeWithData); | 143 LazilyInitializeWithData); |
| 143 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 144 NavigationNotRecorded); | 145 NavigationNotRecorded); |
| 145 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 148 NavigationUrlNotInDBAndDBFull); | 149 NavigationUrlNotInDBAndDBFull); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | |
| 149 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 152 OnSubresourceResponse); | 155 OnSubresourceResponse); |
| 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 155 | 158 |
| 156 enum InitializationState { | 159 enum InitializationState { |
| 157 NOT_INITIALIZED = 0, | 160 NOT_INITIALIZED = 0, |
| 158 INITIALIZING = 1, | 161 INITIALIZING = 1, |
| 159 INITIALIZED = 2 | 162 INITIALIZED = 2 |
| 160 }; | 163 }; |
| 161 | 164 |
| 165 // Stores information about inflight navigations. | |
| 166 struct PageRequestSummary { | |
| 167 explicit PageRequestSummary(const GURL& initial_url); | |
| 168 ~PageRequestSummary(); | |
| 169 | |
| 170 GURL initial_url; | |
| 171 | |
| 172 // Stores all subresources requests within a single navigation, from initial | |
| 173 // main frame request to navigation completion. | |
| 174 std::vector<URLRequestSummary> subresource_requests; | |
| 175 }; | |
| 176 | |
| 177 // Used to fetch the visit count for a URL from the History database. | |
| 178 class GetUrlVisitCountTask : public history::HistoryDBTask { | |
|
pasko
2016/09/27 16:21:27
this class is an implementation detail behind reso
alexilin
2016/09/27 18:11:08
This declaration is in private section of Resource
pasko
2016/09/29 18:08:54
Ah. Thank you for this detailed explanation! What
pasko
2016/09/29 18:25:55
On the other hand .. URLRequestSummary is public a
| |
| 179 public: | |
| 180 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; | |
| 181 typedef ResourcePrefetchPredictor::PageRequestSummary PageRequestSummary; | |
| 182 typedef base::Callback<void(size_t, // Visit count. | |
| 183 const NavigationID&, | |
| 184 const PageRequestSummary&)> | |
| 185 VisitInfoCallback; | |
| 186 | |
| 187 GetUrlVisitCountTask(const NavigationID& navigation_id, | |
| 188 std::unique_ptr<PageRequestSummary> summary, | |
| 189 VisitInfoCallback callback); | |
| 190 | |
| 191 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 192 history::HistoryDatabase* db) override; | |
| 193 | |
| 194 void DoneRunOnMainThread() override; | |
| 195 | |
| 196 private: | |
| 197 ~GetUrlVisitCountTask() override; | |
| 198 | |
| 199 int visit_count_; | |
| 200 NavigationID navigation_id_; | |
| 201 std::unique_ptr<PageRequestSummary> summary_; | |
| 202 VisitInfoCallback callback_; | |
| 203 | |
| 204 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask); | |
| 205 }; | |
| 206 | |
| 162 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; | 207 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; |
| 163 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 208 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 164 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > > | 209 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 210 | |
| 211 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | |
| 165 NavigationMap; | 212 NavigationMap; |
| 166 | 213 |
| 167 // Returns true if the main page request is supported for prediction. | 214 // Returns true if the main page request is supported for prediction. |
| 168 static bool IsHandledMainPage(net::URLRequest* request); | 215 static bool IsHandledMainPage(net::URLRequest* request); |
| 169 | 216 |
| 170 // Returns true if the subresource request is supported for prediction. | 217 // Returns true if the subresource request is supported for prediction. |
| 171 static bool IsHandledSubresource(net::URLRequest* request, | 218 static bool IsHandledSubresource(net::URLRequest* request, |
| 172 content::ResourceType resource_type); | 219 content::ResourceType resource_type); |
| 173 | 220 |
| 174 // Returns true if the subresource has a supported type. | 221 // Returns true if the subresource has a supported type. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 void StartPrefetching(const NavigationID& navigation_id); | 256 void StartPrefetching(const NavigationID& navigation_id); |
| 210 | 257 |
| 211 // Stops prefetching that may be in progress corresponding to |navigation_id|. | 258 // Stops prefetching that may be in progress corresponding to |navigation_id|. |
| 212 void StopPrefetching(const NavigationID& navigation_id); | 259 void StopPrefetching(const NavigationID& navigation_id); |
| 213 | 260 |
| 214 // Starts initialization by posting a task to the DB thread to read the | 261 // Starts initialization by posting a task to the DB thread to read the |
| 215 // predictor database. | 262 // predictor database. |
| 216 void StartInitialization(); | 263 void StartInitialization(); |
| 217 | 264 |
| 218 // Callback for task to read predictor database. Takes ownership of | 265 // Callback for task to read predictor database. Takes ownership of |
| 219 // |url_data_map| and |host_data_map|. | 266 // all arguments. |
| 220 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 267 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 221 std::unique_ptr<PrefetchDataMap> host_data_map); | 268 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 269 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | |
| 270 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | |
| 222 | 271 |
| 223 // Called during initialization when history is read and the predictor | 272 // Called during initialization when history is read and the predictor |
| 224 // database has been read. | 273 // database has been read. |
| 225 void OnHistoryAndCacheLoaded(); | 274 void OnHistoryAndCacheLoaded(); |
| 226 | 275 |
| 227 // Removes data for navigations where the onload never fired. Will cleanup | 276 // Removes data for navigations where the onload never fired. Will cleanup |
| 228 // inflight_navigations_. | 277 // inflight_navigations_. |
| 229 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 278 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 230 | 279 |
| 231 // Deletes all URLs from the predictor database, the caches and removes all | 280 // Deletes all URLs from the predictor database, the caches and removes all |
| 232 // inflight navigations. | 281 // inflight navigations. |
| 233 void DeleteAllUrls(); | 282 void DeleteAllUrls(); |
| 234 | 283 |
| 235 // Deletes data for the input |urls| and their corresponding hosts from the | 284 // Deletes data for the input |urls| and their corresponding hosts from the |
| 236 // predictor database and caches. | 285 // predictor database and caches. |
| 237 void DeleteUrls(const history::URLRows& urls); | 286 void DeleteUrls(const history::URLRows& urls); |
| 238 | 287 |
| 239 // Callback for GetUrlVisitCountTask. | 288 // Callback for GetUrlVisitCountTask. |
| 240 void OnVisitCountLookup(size_t visit_count, | 289 void OnVisitCountLookup(size_t visit_count, |
| 241 const NavigationID& navigation_id, | 290 const NavigationID& navigation_id, |
| 242 const std::vector<URLRequestSummary>& requests); | 291 const PageRequestSummary& summary); |
| 243 | 292 |
| 244 // Removes the oldest entry in the input |data_map|, also deleting it from the | 293 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 245 // predictor database. | 294 // predictor database. |
| 246 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 295 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 247 PrefetchDataMap* data_map); | 296 PrefetchDataMap* data_map); |
| 248 | 297 |
| 298 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | |
| 299 RedirectDataMap* data_map); | |
| 300 | |
| 249 // Merges resources in |new_resources| into the |data_map| and correspondingly | 301 // Merges resources in |new_resources| into the |data_map| and correspondingly |
| 250 // updates the predictor database. | 302 // updates the predictor database. Also calls LearnRedirect if relevant. |
| 251 void LearnNavigation(const std::string& key, | 303 void LearnNavigation(const std::string& key, |
| 252 PrefetchKeyType key_type, | 304 PrefetchKeyType key_type, |
| 253 const std::vector<URLRequestSummary>& new_resources, | 305 const std::vector<URLRequestSummary>& new_resources, |
| 254 size_t max_data_map_size, | 306 size_t max_data_map_size, |
| 255 PrefetchDataMap* data_map); | 307 PrefetchDataMap* data_map, |
| 308 const std::string& key_before_redirects, | |
| 309 RedirectDataMap* redirect_map); | |
| 310 | |
| 311 // Updates information about final redirect destination for |key| in | |
| 312 // |redirect_map| and correspondingly updates the predictor database | |
|
pasko
2016/09/27 16:21:27
nit: .
alexilin
2016/09/27 18:11:08
Done.
| |
| 313 void LearnRedirect(const std::string& key, | |
| 314 PrefetchKeyType key_type, | |
| 315 const std::string& final_redirect, | |
| 316 size_t max_redirect_map_size, | |
| 317 RedirectDataMap* redirect_map); | |
| 256 | 318 |
| 257 // Reports overall page load time. | 319 // Reports overall page load time. |
| 258 void ReportPageLoadTimeStats(base::TimeDelta plt) const; | 320 void ReportPageLoadTimeStats(base::TimeDelta plt) const; |
| 259 | 321 |
| 260 // Reports page load time for prefetched and not prefetched pages | 322 // Reports page load time for prefetched and not prefetched pages |
| 261 void ReportPageLoadTimePrefetchStats( | 323 void ReportPageLoadTimePrefetchStats( |
| 262 base::TimeDelta plt, | 324 base::TimeDelta plt, |
| 263 bool prefetched, | 325 bool prefetched, |
| 264 base::Callback<void(int)> report_network_type_callback, | 326 base::Callback<void(int)> report_network_type_callback, |
| 265 PrefetchKeyType key_type) const; | 327 PrefetchKeyType key_type) const; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 282 tables_ = tables; | 344 tables_ = tables; |
| 283 } | 345 } |
| 284 | 346 |
| 285 Profile* const profile_; | 347 Profile* const profile_; |
| 286 ResourcePrefetchPredictorConfig const config_; | 348 ResourcePrefetchPredictorConfig const config_; |
| 287 InitializationState initialization_state_; | 349 InitializationState initialization_state_; |
| 288 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 350 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 289 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 351 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 290 base::CancelableTaskTracker history_lookup_consumer_; | 352 base::CancelableTaskTracker history_lookup_consumer_; |
| 291 | 353 |
| 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. | 354 // Copy of the data in the predictor tables. |
| 296 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 355 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 297 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 356 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 357 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | |
| 358 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | |
| 359 | |
| 360 NavigationMap inflight_navigations_; | |
| 298 | 361 |
| 299 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 362 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 300 history_service_observer_; | 363 history_service_observer_; |
| 301 | 364 |
| 302 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 365 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 303 }; | 366 }; |
| 304 | 367 |
| 305 } // namespace predictors | 368 } // namespace predictors |
| 306 | 369 |
| 307 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 370 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |