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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 | 93 |
| 94 bool has_validators; | 94 bool has_validators; |
| 95 bool always_revalidate; | 95 bool always_revalidate; |
| 96 | 96 |
| 97 // Initializes a |URLRequestSummary| from a |URLRequest| response. | 97 // Initializes a |URLRequestSummary| from a |URLRequest| response. |
| 98 // Returns true for success. | 98 // Returns true for success. |
| 99 static bool SummarizeResponse(const net::URLRequest& request, | 99 static bool SummarizeResponse(const net::URLRequest& request, |
| 100 URLRequestSummary* summary); | 100 URLRequestSummary* summary); |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 // Stores the data is learned from single navigation. | |
|
Benoit L
2016/10/21 17:16:24
nit: Stores the data learned from a single navigat
alexilin
2016/10/21 18:37:15
Done.
| |
| 104 struct PageRequestSummary { | |
| 105 explicit PageRequestSummary(const GURL& main_frame_url); | |
| 106 ~PageRequestSummary(); | |
| 107 | |
| 108 GURL main_frame_url; | |
| 109 GURL initial_url; | |
| 110 | |
| 111 // Stores all subresource requests within a single navigation, from initial | |
| 112 // main frame request to navigation completion. | |
| 113 std::vector<URLRequestSummary> subresource_requests; | |
| 114 }; | |
| 115 | |
| 103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, | 116 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 104 Profile* profile); | 117 Profile* profile); |
| 105 ~ResourcePrefetchPredictor() override; | 118 ~ResourcePrefetchPredictor() override; |
| 106 | 119 |
| 107 // Thread safe. | 120 // Thread safe. |
| 108 static bool ShouldRecordRequest(net::URLRequest* request, | 121 static bool ShouldRecordRequest(net::URLRequest* request, |
| 109 content::ResourceType resource_type); | 122 content::ResourceType resource_type); |
| 110 static bool ShouldRecordResponse(net::URLRequest* response); | 123 static bool ShouldRecordResponse(net::URLRequest* response); |
| 111 static bool ShouldRecordRedirect(net::URLRequest* response); | 124 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 112 | 125 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the | 138 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the |
| 126 // predictor of main frame and resource requests. Should only be called if the | 139 // predictor of main frame and resource requests. Should only be called if the |
| 127 // corresponding Should* functions return true. | 140 // corresponding Should* functions return true. |
| 128 void RecordURLRequest(const URLRequestSummary& request); | 141 void RecordURLRequest(const URLRequestSummary& request); |
| 129 void RecordURLResponse(const URLRequestSummary& response); | 142 void RecordURLResponse(const URLRequestSummary& response); |
| 130 void RecordURLRedirect(const URLRequestSummary& response); | 143 void RecordURLRedirect(const URLRequestSummary& response); |
| 131 | 144 |
| 132 // Called when the main frame of a page completes loading. | 145 // Called when the main frame of a page completes loading. |
| 133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 146 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 134 | 147 |
| 148 // Starts prefetching if it is enabled and prefetching data exists for the | |
| 149 // NavigationID either at the URL or at the host level. | |
| 150 void StartPrefetching(const GURL& main_frame_url); | |
| 151 | |
| 152 // Stops prefetching that may be in progress corresponding to |navigation_id|. | |
|
pasko
2016/10/21 16:47:34
huh, I did not notice this during one of the previ
alexilin
2016/10/21 18:37:15
Done.
| |
| 153 void StopPrefetching(const GURL& main_frame_url); | |
| 154 | |
| 135 private: | 155 private: |
| 136 friend class ::PredictorsHandler; | 156 friend class ::PredictorsHandler; |
| 137 friend class ResourcePrefetchPredictorTest; | 157 friend class ResourcePrefetchPredictorTest; |
| 138 | 158 |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 159 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 141 LazilyInitializeEmpty); | 161 LazilyInitializeEmpty); |
| 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 162 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 143 LazilyInitializeWithData); | 163 LazilyInitializeWithData); |
| 144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 164 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 145 NavigationNotRecorded); | 165 NavigationNotRecorded); |
| 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 166 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 167 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 168 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 149 NavigationUrlNotInDBAndDBFull); | 169 NavigationUrlNotInDBAndDBFull); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | 170 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | 171 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); |
| 152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 172 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 173 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 174 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 155 OnSubresourceResponse); | 175 OnSubresourceResponse); |
| 156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 176 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 177 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 178 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 159 PopulatePrefetcherRequest); | 179 PopulatePrefetcherRequest); |
| 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 180 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 181 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| 162 | 182 |
| 183 // An interface used to notify clients (observers) of this object that data in | |
| 184 // the resource prefetch predictor has changed. Register the observer via | |
| 185 // ResourcePrefetchPredictor::SetObserverForTesting. | |
|
Benoit L
2016/10/21 17:16:24
Can you add a comment specifying on which thread t
alexilin
2016/10/21 18:37:14
Done.
| |
| 186 class TestObserver { | |
| 187 public: | |
| 188 virtual ~TestObserver() {} | |
| 189 virtual void OnNavigationLearned(size_t url_visit_count, | |
| 190 const PageRequestSummary& summary) {} | |
| 191 | |
| 192 protected: | |
| 193 TestObserver() = default; | |
| 194 | |
| 195 private: | |
| 196 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 197 }; | |
| 198 | |
| 163 enum InitializationState { | 199 enum InitializationState { |
| 164 NOT_INITIALIZED = 0, | 200 NOT_INITIALIZED = 0, |
| 165 INITIALIZING = 1, | 201 INITIALIZING = 1, |
| 166 INITIALIZED = 2 | 202 INITIALIZED = 2 |
| 167 }; | 203 }; |
| 168 | 204 |
| 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; | 205 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 206 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 213 | 207 |
| 214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 208 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
| 215 NavigationMap; | 209 NavigationMap; |
| 216 | 210 |
| 217 // Returns true if the main page request is supported for prediction. | 211 // Returns true if the main page request is supported for prediction. |
| 218 static bool IsHandledMainPage(net::URLRequest* request); | 212 static bool IsHandledMainPage(net::URLRequest* request); |
| 219 | 213 |
| 220 // Returns true if the subresource request is supported for prediction. | 214 // 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. | 248 // prefetched. |
| 255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); | 249 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); |
| 256 | 250 |
| 257 // Returns true iff the |data_map| contains PrefetchData that can be used | 251 // 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 | 252 // for a |main_frame_key| and fills |urls| with resources that need to be |
| 259 // prefetched. | 253 // prefetched. |
| 260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, | 254 bool PopulatePrefetcherRequest(const std::string& main_frame_key, |
| 261 const PrefetchDataMap& data_map, | 255 const PrefetchDataMap& data_map, |
| 262 std::vector<GURL>* urls); | 256 std::vector<GURL>* urls); |
| 263 | 257 |
| 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 | 258 // Starts initialization by posting a task to the DB thread to read the |
| 274 // predictor database. | 259 // predictor database. |
| 275 void StartInitialization(); | 260 void StartInitialization(); |
| 276 | 261 |
| 277 // Callback for task to read predictor database. Takes ownership of | 262 // Callback for task to read predictor database. Takes ownership of |
| 278 // all arguments. | 263 // all arguments. |
| 279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 264 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 280 std::unique_ptr<PrefetchDataMap> host_data_map, | 265 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 266 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | 267 std::unique_ptr<RedirectDataMap> host_redirect_data_map); |
| 283 | 268 |
| 284 // Called during initialization when history is read and the predictor | 269 // Called during initialization when history is read and the predictor |
| 285 // database has been read. | 270 // database has been read. |
| 286 void OnHistoryAndCacheLoaded(); | 271 void OnHistoryAndCacheLoaded(); |
| 287 | 272 |
| 288 // Removes data for navigations where the onload never fired. Will cleanup | 273 // Removes data for navigations where the onload never fired. Will cleanup |
| 289 // inflight_navigations_. | 274 // inflight_navigations_. |
| 290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 275 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 291 | 276 |
| 292 // Deletes all URLs from the predictor database, the caches and removes all | 277 // Deletes all URLs from the predictor database, the caches and removes all |
| 293 // inflight navigations. | 278 // inflight navigations. |
| 294 void DeleteAllUrls(); | 279 void DeleteAllUrls(); |
| 295 | 280 |
| 296 // Deletes data for the input |urls| and their corresponding hosts from the | 281 // Deletes data for the input |urls| and their corresponding hosts from the |
| 297 // predictor database and caches. | 282 // predictor database and caches. |
| 298 void DeleteUrls(const history::URLRows& urls); | 283 void DeleteUrls(const history::URLRows& urls); |
| 299 | 284 |
| 300 // Callback for GetUrlVisitCountTask. | 285 // Callback for GetUrlVisitCountTask. |
| 301 void OnVisitCountLookup(size_t visit_count, | 286 void OnVisitCountLookup(size_t url_visit_count, |
| 302 const NavigationID& navigation_id, | |
| 303 const PageRequestSummary& summary); | 287 const PageRequestSummary& summary); |
| 304 | 288 |
| 305 // Removes the oldest entry in the input |data_map|, also deleting it from the | 289 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 306 // predictor database. | 290 // predictor database. |
| 307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 291 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 308 PrefetchDataMap* data_map); | 292 PrefetchDataMap* data_map); |
| 309 | 293 |
| 310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | 294 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, |
| 311 RedirectDataMap* data_map); | 295 RedirectDataMap* data_map); |
| 312 | 296 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 | 333 |
| 350 // Used to connect to HistoryService or register for service loaded | 334 // Used to connect to HistoryService or register for service loaded |
| 351 // notificatioan. | 335 // notificatioan. |
| 352 void ConnectToHistoryService(); | 336 void ConnectToHistoryService(); |
| 353 | 337 |
| 354 // Used for testing to inject mock tables. | 338 // Used for testing to inject mock tables. |
| 355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 339 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 356 tables_ = tables; | 340 tables_ = tables; |
| 357 } | 341 } |
| 358 | 342 |
| 343 // Sets the |observer| to be notified when the resource prefetch predictor | |
| 344 // data changes. This observer needs to be removed before it is destructed by | |
| 345 // calling this function with nullptr parameter. | |
| 346 void SetObserverForTesting(TestObserver* observer); | |
|
Benoit L
2016/10/21 17:16:24
Stupid question: can we use a unique_ptr<> to hold
pasko
2016/10/21 17:38:22
Not stupid at all.
It's hard to say for me withou
alexilin
2016/10/21 18:37:15
Well, it's not great to transfer the observer owne
Benoit L
2016/10/24 02:13:56
Ok, thank you for the clarification. Observers usu
| |
| 347 | |
| 359 Profile* const profile_; | 348 Profile* const profile_; |
| 349 TestObserver* observer_; | |
| 360 ResourcePrefetchPredictorConfig const config_; | 350 ResourcePrefetchPredictorConfig const config_; |
| 361 InitializationState initialization_state_; | 351 InitializationState initialization_state_; |
| 362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 352 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 353 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 364 base::CancelableTaskTracker history_lookup_consumer_; | 354 base::CancelableTaskTracker history_lookup_consumer_; |
| 365 | 355 |
| 366 // Copy of the data in the predictor tables. | 356 // Copy of the data in the predictor tables. |
| 367 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 357 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 368 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 358 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 359 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 360 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| 371 | 361 |
| 372 NavigationMap inflight_navigations_; | 362 NavigationMap inflight_navigations_; |
| 373 | 363 |
| 374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 364 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 375 history_service_observer_; | 365 history_service_observer_; |
| 376 | 366 |
| 377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 367 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 378 }; | 368 }; |
| 379 | 369 |
| 380 } // namespace predictors | 370 } // namespace predictors |
| 381 | 371 |
| 382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 372 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |