| 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 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 class PredictorsHandler; | 32 class PredictorsHandler; |
| 33 class Profile; | 33 class Profile; |
| 34 | 34 |
| 35 namespace net { | 35 namespace net { |
| 36 class URLRequest; | 36 class URLRequest; |
| 37 } | 37 } |
| 38 | 38 |
| 39 namespace predictors { | 39 namespace predictors { |
| 40 | 40 |
| 41 class TestObserver; |
| 41 class ResourcePrefetcherManager; | 42 class ResourcePrefetcherManager; |
| 42 | 43 |
| 43 // Contains logic for learning what can be prefetched and for kicking off | 44 // Contains logic for learning what can be prefetched and for kicking off |
| 44 // speculative prefetching. | 45 // speculative prefetching. |
| 45 // - The class is a profile keyed service owned by the profile. | 46 // - The class is a profile keyed service owned by the profile. |
| 46 // - All the non-static methods of this class need to be called on the UI | 47 // - All the non-static methods of this class need to be called on the UI |
| 47 // thread. | 48 // thread. |
| 48 // | 49 // |
| 49 // The overall flow of the resource prefetching algorithm is as follows: | 50 // The overall flow of the resource prefetching algorithm is as follows: |
| 50 // | 51 // |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 94 |
| 94 bool has_validators; | 95 bool has_validators; |
| 95 bool always_revalidate; | 96 bool always_revalidate; |
| 96 | 97 |
| 97 // Initializes a |URLRequestSummary| from a |URLRequest| response. | 98 // Initializes a |URLRequestSummary| from a |URLRequest| response. |
| 98 // Returns true for success. | 99 // Returns true for success. |
| 99 static bool SummarizeResponse(const net::URLRequest& request, | 100 static bool SummarizeResponse(const net::URLRequest& request, |
| 100 URLRequestSummary* summary); | 101 URLRequestSummary* summary); |
| 101 }; | 102 }; |
| 102 | 103 |
| 104 // Stores the data learned from a single navigation. |
| 105 struct PageRequestSummary { |
| 106 explicit PageRequestSummary(const GURL& main_frame_url); |
| 107 PageRequestSummary(const PageRequestSummary& other); |
| 108 ~PageRequestSummary(); |
| 109 |
| 110 GURL main_frame_url; |
| 111 GURL initial_url; |
| 112 |
| 113 // Stores all subresource requests within a single navigation, from initial |
| 114 // main frame request to navigation completion. |
| 115 std::vector<URLRequestSummary> subresource_requests; |
| 116 }; |
| 117 |
| 103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, | 118 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 104 Profile* profile); | 119 Profile* profile); |
| 105 ~ResourcePrefetchPredictor() override; | 120 ~ResourcePrefetchPredictor() override; |
| 106 | 121 |
| 107 // Thread safe. | 122 // Thread safe. |
| 108 static bool ShouldRecordRequest(net::URLRequest* request, | 123 static bool ShouldRecordRequest(net::URLRequest* request, |
| 109 content::ResourceType resource_type); | 124 content::ResourceType resource_type); |
| 110 static bool ShouldRecordResponse(net::URLRequest* response); | 125 static bool ShouldRecordResponse(net::URLRequest* response); |
| 111 static bool ShouldRecordRedirect(net::URLRequest* response); | 126 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 112 | 127 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the | 140 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the |
| 126 // predictor of main frame and resource requests. Should only be called if the | 141 // predictor of main frame and resource requests. Should only be called if the |
| 127 // corresponding Should* functions return true. | 142 // corresponding Should* functions return true. |
| 128 void RecordURLRequest(const URLRequestSummary& request); | 143 void RecordURLRequest(const URLRequestSummary& request); |
| 129 void RecordURLResponse(const URLRequestSummary& response); | 144 void RecordURLResponse(const URLRequestSummary& response); |
| 130 void RecordURLRedirect(const URLRequestSummary& response); | 145 void RecordURLRedirect(const URLRequestSummary& response); |
| 131 | 146 |
| 132 // Called when the main frame of a page completes loading. | 147 // Called when the main frame of a page completes loading. |
| 133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 148 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 134 | 149 |
| 150 // Starts prefetching if it is enabled and prefetching data exists for the |
| 151 // |main_frame_url| either at the URL or at the host level. |
| 152 void StartPrefetching(const GURL& main_frame_url); |
| 153 |
| 154 // Stops prefetching that may be in progress corresponding to |
| 155 // |main_frame_url|. |
| 156 void StopPrefetching(const GURL& main_frame_url); |
| 157 |
| 158 // Sets the |observer| to be notified when the resource prefetch predictor |
| 159 // data changes. Previously registered observer will be discarded. Call |
| 160 // this with nullptr parameter to de-register observer. |
| 161 void SetObserverForTesting(TestObserver* observer); |
| 162 |
| 135 private: | 163 private: |
| 136 friend class ::PredictorsHandler; | 164 friend class ::PredictorsHandler; |
| 137 friend class ResourcePrefetchPredictorTest; | 165 friend class ResourcePrefetchPredictorTest; |
| 138 | 166 |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 167 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 168 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 141 LazilyInitializeEmpty); | 169 LazilyInitializeEmpty); |
| 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 170 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 143 LazilyInitializeWithData); | 171 LazilyInitializeWithData); |
| 144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 172 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 159 PopulatePrefetcherRequest); | 187 PopulatePrefetcherRequest); |
| 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 188 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 189 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| 162 | 190 |
| 163 enum InitializationState { | 191 enum InitializationState { |
| 164 NOT_INITIALIZED = 0, | 192 NOT_INITIALIZED = 0, |
| 165 INITIALIZING = 1, | 193 INITIALIZING = 1, |
| 166 INITIALIZED = 2 | 194 INITIALIZED = 2 |
| 167 }; | 195 }; |
| 168 | 196 |
| 169 // Stores information about inflight navigations. | |
| 170 struct PageRequestSummary { | |
| 171 explicit PageRequestSummary(const GURL& initial_url); | |
| 172 ~PageRequestSummary(); | |
| 173 | |
| 174 GURL initial_url; | |
| 175 | |
| 176 // Stores all subresources requests within a single navigation, from initial | |
| 177 // main frame request to navigation completion. | |
| 178 std::vector<URLRequestSummary> subresource_requests; | |
| 179 }; | |
| 180 | |
| 181 // Used to fetch the visit count for a URL from the History database. | |
| 182 class GetUrlVisitCountTask : public history::HistoryDBTask { | |
| 183 public: | |
| 184 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary; | |
| 185 typedef ResourcePrefetchPredictor::PageRequestSummary PageRequestSummary; | |
| 186 typedef base::Callback<void(size_t, // Visit count. | |
| 187 const NavigationID&, | |
| 188 const PageRequestSummary&)> | |
| 189 VisitInfoCallback; | |
| 190 | |
| 191 GetUrlVisitCountTask(const NavigationID& navigation_id, | |
| 192 std::unique_ptr<PageRequestSummary> summary, | |
| 193 VisitInfoCallback callback); | |
| 194 | |
| 195 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 196 history::HistoryDatabase* db) override; | |
| 197 | |
| 198 void DoneRunOnMainThread() override; | |
| 199 | |
| 200 private: | |
| 201 ~GetUrlVisitCountTask() override; | |
| 202 | |
| 203 int visit_count_; | |
| 204 NavigationID navigation_id_; | |
| 205 std::unique_ptr<PageRequestSummary> summary_; | |
| 206 VisitInfoCallback callback_; | |
| 207 | |
| 208 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask); | |
| 209 }; | |
| 210 | |
| 211 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; | 197 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 198 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 213 | 199 |
| 214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 200 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
| 215 NavigationMap; | 201 NavigationMap; |
| 216 | 202 |
| 217 // Returns true if the main page request is supported for prediction. | 203 // Returns true if the main page request is supported for prediction. |
| 218 static bool IsHandledMainPage(net::URLRequest* request); | 204 static bool IsHandledMainPage(net::URLRequest* request); |
| 219 | 205 |
| 220 // Returns true if the subresource request is supported for prediction. | 206 // Returns true if the subresource request is supported for prediction. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 // prefetched. | 240 // prefetched. |
| 255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); | 241 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); |
| 256 | 242 |
| 257 // Returns true iff the |data_map| contains PrefetchData that can be used | 243 // Returns true iff the |data_map| contains PrefetchData that can be used |
| 258 // for a |main_frame_key| and fills |urls| with resources that need to be | 244 // for a |main_frame_key| and fills |urls| with resources that need to be |
| 259 // prefetched. | 245 // prefetched. |
| 260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, | 246 bool PopulatePrefetcherRequest(const std::string& main_frame_key, |
| 261 const PrefetchDataMap& data_map, | 247 const PrefetchDataMap& data_map, |
| 262 std::vector<GURL>* urls); | 248 std::vector<GURL>* urls); |
| 263 | 249 |
| 264 public: | |
| 265 // Starts prefetching if it is enabled and prefetching data exists for the | |
| 266 // NavigationID either at the URL or at the host level. | |
| 267 void StartPrefetching(const GURL& main_frame_url); | |
| 268 | |
| 269 // Stops prefetching that may be in progress corresponding to |navigation_id|. | |
| 270 void StopPrefetching(const GURL& main_frame_url); | |
| 271 | |
| 272 private: | |
| 273 // Starts initialization by posting a task to the DB thread to read the | 250 // Starts initialization by posting a task to the DB thread to read the |
| 274 // predictor database. | 251 // predictor database. |
| 275 void StartInitialization(); | 252 void StartInitialization(); |
| 276 | 253 |
| 277 // Callback for task to read predictor database. Takes ownership of | 254 // Callback for task to read predictor database. Takes ownership of |
| 278 // all arguments. | 255 // all arguments. |
| 279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 256 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 280 std::unique_ptr<PrefetchDataMap> host_data_map, | 257 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 258 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | 259 std::unique_ptr<RedirectDataMap> host_redirect_data_map); |
| 283 | 260 |
| 284 // Called during initialization when history is read and the predictor | 261 // Called during initialization when history is read and the predictor |
| 285 // database has been read. | 262 // database has been read. |
| 286 void OnHistoryAndCacheLoaded(); | 263 void OnHistoryAndCacheLoaded(); |
| 287 | 264 |
| 288 // Removes data for navigations where the onload never fired. Will cleanup | 265 // Removes data for navigations where the onload never fired. Will cleanup |
| 289 // inflight_navigations_. | 266 // inflight_navigations_. |
| 290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 267 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 291 | 268 |
| 292 // Deletes all URLs from the predictor database, the caches and removes all | 269 // Deletes all URLs from the predictor database, the caches and removes all |
| 293 // inflight navigations. | 270 // inflight navigations. |
| 294 void DeleteAllUrls(); | 271 void DeleteAllUrls(); |
| 295 | 272 |
| 296 // Deletes data for the input |urls| and their corresponding hosts from the | 273 // Deletes data for the input |urls| and their corresponding hosts from the |
| 297 // predictor database and caches. | 274 // predictor database and caches. |
| 298 void DeleteUrls(const history::URLRows& urls); | 275 void DeleteUrls(const history::URLRows& urls); |
| 299 | 276 |
| 300 // Callback for GetUrlVisitCountTask. | 277 // Callback for GetUrlVisitCountTask. |
| 301 void OnVisitCountLookup(size_t visit_count, | 278 void OnVisitCountLookup(size_t url_visit_count, |
| 302 const NavigationID& navigation_id, | |
| 303 const PageRequestSummary& summary); | 279 const PageRequestSummary& summary); |
| 304 | 280 |
| 305 // 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 |
| 306 // predictor database. | 282 // predictor database. |
| 307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 283 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 308 PrefetchDataMap* data_map); | 284 PrefetchDataMap* data_map); |
| 309 | 285 |
| 310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | 286 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, |
| 311 RedirectDataMap* data_map); | 287 RedirectDataMap* data_map); |
| 312 | 288 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 // Used to connect to HistoryService or register for service loaded | 326 // Used to connect to HistoryService or register for service loaded |
| 351 // notificatioan. | 327 // notificatioan. |
| 352 void ConnectToHistoryService(); | 328 void ConnectToHistoryService(); |
| 353 | 329 |
| 354 // Used for testing to inject mock tables. | 330 // Used for testing to inject mock tables. |
| 355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 331 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 356 tables_ = tables; | 332 tables_ = tables; |
| 357 } | 333 } |
| 358 | 334 |
| 359 Profile* const profile_; | 335 Profile* const profile_; |
| 336 TestObserver* observer_; |
| 360 ResourcePrefetchPredictorConfig const config_; | 337 ResourcePrefetchPredictorConfig const config_; |
| 361 InitializationState initialization_state_; | 338 InitializationState initialization_state_; |
| 362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 339 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 340 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 364 base::CancelableTaskTracker history_lookup_consumer_; | 341 base::CancelableTaskTracker history_lookup_consumer_; |
| 365 | 342 |
| 366 // Copy of the data in the predictor tables. | 343 // Copy of the data in the predictor tables. |
| 367 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 344 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 368 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 345 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 346 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 347 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| 371 | 348 |
| 372 NavigationMap inflight_navigations_; | 349 NavigationMap inflight_navigations_; |
| 373 | 350 |
| 374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 351 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 375 history_service_observer_; | 352 history_service_observer_; |
| 376 | 353 |
| 377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 354 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 378 }; | 355 }; |
| 379 | 356 |
| 357 // An interface used to notify that data in the ResourcePrefetchPredictor |
| 358 // has changed. All methods are invoked on the UI thread. |
| 359 class TestObserver { |
| 360 public: |
| 361 // De-registers itself from |predictor_| on destruction. |
| 362 virtual ~TestObserver(); |
| 363 |
| 364 virtual void OnNavigationLearned( |
| 365 size_t url_visit_count, |
| 366 const ResourcePrefetchPredictor::PageRequestSummary& summary) {} |
| 367 |
| 368 protected: |
| 369 // |predictor| must be non-NULL and has to outlive the TestObserver. |
| 370 // Also the predictor must not have a TestObserver set. |
| 371 explicit TestObserver(ResourcePrefetchPredictor* predictor); |
| 372 |
| 373 private: |
| 374 ResourcePrefetchPredictor* predictor_; |
| 375 |
| 376 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 377 }; |
| 378 |
| 380 } // namespace predictors | 379 } // namespace predictors |
| 381 | 380 |
| 382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 381 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |