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 learned from a single navigation. | |
| 104 struct PageRequestSummary { | |
|
pasko
2016/10/24 17:59:38
struct vs. class:
the style guide says: "structs
alexilin
2016/10/25 11:38:09
I have another opinion. PageRequestSummary should
pasko
2016/10/25 14:05:40
OK, you are right, it is indeed common that non-tr
alexilin
2016/10/25 15:10:58
Done.
| |
| 105 explicit PageRequestSummary(const GURL& main_frame_url); | |
| 106 PageRequestSummary(const PageRequestSummary& other); | |
|
pasko
2016/10/24 17:59:38
hm, just noticed: public copy-constructor is error
alexilin
2016/10/25 11:38:09
It will be hard to avoid this because of following
pasko
2016/10/25 14:05:40
Oh, makes sense, thank you for explaining. I forgo
alexilin
2016/10/25 15:10:58
Done.
| |
| 107 ~PageRequestSummary(); | |
| 108 | |
| 109 GURL main_frame_url; | |
| 110 GURL initial_url; | |
| 111 | |
| 112 // Stores all subresource requests within a single navigation, from initial | |
| 113 // main frame request to navigation completion. | |
| 114 std::vector<URLRequestSummary> subresource_requests; | |
| 115 }; | |
| 116 | |
| 117 // An interface used to notify clients (observers) of this object that data in | |
| 118 // the resource prefetch predictor has changed. All methods need to be | |
| 119 // called on the UI thread. | |
| 120 class TestObserver { | |
| 121 public: | |
| 122 virtual ~TestObserver(); | |
| 123 | |
| 124 virtual void OnNavigationLearned(size_t url_visit_count, | |
| 125 const PageRequestSummary& summary) {} | |
|
pasko
2016/10/24 17:59:38
I think a single mock would be enough to hook into
alexilin
2016/10/25 11:38:09
IIUC, you suggested to move all implementation det
pasko
2016/10/25 14:05:40
Oops. I forgot that the de-registration logic stil
alexilin
2016/10/25 15:10:58
Everything done except the last one.
Discussed off
| |
| 126 | |
| 127 protected: | |
| 128 // Observer is tied to a single ResourcePrefetchPredictor for its entire | |
|
pasko
2016/10/24 17:59:38
it is not perfectly clear what 'its' refers to, Re
alexilin
2016/10/25 11:38:09
Done.
| |
| 129 // lifetime. However it could be displaced by another observer and then it | |
| 130 // won't receive any notifications. | |
| 131 explicit TestObserver(ResourcePrefetchPredictor* predictor); | |
| 132 | |
| 133 private: | |
| 134 friend class ResourcePrefetchPredictor; | |
| 135 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | |
| 136 SetObserverForTesting); | |
| 137 | |
| 138 void ResetResourcePrefetchPredictor(); | |
| 139 | |
| 140 ResourcePrefetchPredictor* predictor_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 143 }; | |
| 144 | |
| 103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, | 145 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 104 Profile* profile); | 146 Profile* profile); |
| 105 ~ResourcePrefetchPredictor() override; | 147 ~ResourcePrefetchPredictor() override; |
| 106 | 148 |
| 107 // Thread safe. | 149 // Thread safe. |
| 108 static bool ShouldRecordRequest(net::URLRequest* request, | 150 static bool ShouldRecordRequest(net::URLRequest* request, |
| 109 content::ResourceType resource_type); | 151 content::ResourceType resource_type); |
| 110 static bool ShouldRecordResponse(net::URLRequest* response); | 152 static bool ShouldRecordResponse(net::URLRequest* response); |
| 111 static bool ShouldRecordRedirect(net::URLRequest* response); | 153 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 112 | 154 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the | 167 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the |
| 126 // predictor of main frame and resource requests. Should only be called if the | 168 // predictor of main frame and resource requests. Should only be called if the |
| 127 // corresponding Should* functions return true. | 169 // corresponding Should* functions return true. |
| 128 void RecordURLRequest(const URLRequestSummary& request); | 170 void RecordURLRequest(const URLRequestSummary& request); |
| 129 void RecordURLResponse(const URLRequestSummary& response); | 171 void RecordURLResponse(const URLRequestSummary& response); |
| 130 void RecordURLRedirect(const URLRequestSummary& response); | 172 void RecordURLRedirect(const URLRequestSummary& response); |
| 131 | 173 |
| 132 // Called when the main frame of a page completes loading. | 174 // Called when the main frame of a page completes loading. |
| 133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 175 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
| 134 | 176 |
| 177 // Starts prefetching if it is enabled and prefetching data exists for the | |
| 178 // |main_frame_url| either at the URL or at the host level. | |
| 179 void StartPrefetching(const GURL& main_frame_url); | |
| 180 | |
| 181 // Stops prefetching that may be in progress corresponding to | |
| 182 // |main_frame_url|. | |
| 183 void StopPrefetching(const GURL& main_frame_url); | |
| 184 | |
| 135 private: | 185 private: |
| 136 friend class ::PredictorsHandler; | 186 friend class ::PredictorsHandler; |
| 137 friend class ResourcePrefetchPredictorTest; | 187 friend class ResourcePrefetchPredictorTest; |
| 138 | 188 |
| 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 189 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 190 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 141 LazilyInitializeEmpty); | 191 LazilyInitializeEmpty); |
| 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 192 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 143 LazilyInitializeWithData); | 193 LazilyInitializeWithData); |
| 144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 194 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 145 NavigationNotRecorded); | 195 NavigationNotRecorded); |
| 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 196 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
| 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 197 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
| 148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 198 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 149 NavigationUrlNotInDBAndDBFull); | 199 NavigationUrlNotInDBAndDBFull); |
| 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | 200 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | 201 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); |
| 152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 202 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
| 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 203 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
| 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 204 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 155 OnSubresourceResponse); | 205 OnSubresourceResponse); |
| 156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 206 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
| 157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | 207 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); |
| 158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 208 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 159 PopulatePrefetcherRequest); | 209 PopulatePrefetcherRequest); |
| 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 210 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
| 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 211 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
| 212 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | |
| 213 SetObserverForTesting); | |
| 162 | 214 |
| 163 enum InitializationState { | 215 enum InitializationState { |
| 164 NOT_INITIALIZED = 0, | 216 NOT_INITIALIZED = 0, |
| 165 INITIALIZING = 1, | 217 INITIALIZING = 1, |
| 166 INITIALIZED = 2 | 218 INITIALIZED = 2 |
| 167 }; | 219 }; |
| 168 | 220 |
| 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; | 221 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; |
| 212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; | 222 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; |
| 213 | 223 |
| 214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 224 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
| 215 NavigationMap; | 225 NavigationMap; |
| 216 | 226 |
| 217 // Returns true if the main page request is supported for prediction. | 227 // Returns true if the main page request is supported for prediction. |
| 218 static bool IsHandledMainPage(net::URLRequest* request); | 228 static bool IsHandledMainPage(net::URLRequest* request); |
| 219 | 229 |
| 220 // Returns true if the subresource request is supported for prediction. | 230 // 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. | 264 // prefetched. |
| 255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); | 265 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); |
| 256 | 266 |
| 257 // Returns true iff the |data_map| contains PrefetchData that can be used | 267 // 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 | 268 // for a |main_frame_key| and fills |urls| with resources that need to be |
| 259 // prefetched. | 269 // prefetched. |
| 260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, | 270 bool PopulatePrefetcherRequest(const std::string& main_frame_key, |
| 261 const PrefetchDataMap& data_map, | 271 const PrefetchDataMap& data_map, |
| 262 std::vector<GURL>* urls); | 272 std::vector<GURL>* urls); |
| 263 | 273 |
| 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 | 274 // Starts initialization by posting a task to the DB thread to read the |
| 274 // predictor database. | 275 // predictor database. |
| 275 void StartInitialization(); | 276 void StartInitialization(); |
| 276 | 277 |
| 277 // Callback for task to read predictor database. Takes ownership of | 278 // Callback for task to read predictor database. Takes ownership of |
| 278 // all arguments. | 279 // all arguments. |
| 279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, | 280 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, |
| 280 std::unique_ptr<PrefetchDataMap> host_data_map, | 281 std::unique_ptr<PrefetchDataMap> host_data_map, |
| 281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, | 282 std::unique_ptr<RedirectDataMap> url_redirect_data_map, |
| 282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); | 283 std::unique_ptr<RedirectDataMap> host_redirect_data_map); |
| 283 | 284 |
| 284 // Called during initialization when history is read and the predictor | 285 // Called during initialization when history is read and the predictor |
| 285 // database has been read. | 286 // database has been read. |
| 286 void OnHistoryAndCacheLoaded(); | 287 void OnHistoryAndCacheLoaded(); |
| 287 | 288 |
| 288 // Removes data for navigations where the onload never fired. Will cleanup | 289 // Removes data for navigations where the onload never fired. Will cleanup |
| 289 // inflight_navigations_. | 290 // inflight_navigations_. |
| 290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 291 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 291 | 292 |
| 292 // Deletes all URLs from the predictor database, the caches and removes all | 293 // Deletes all URLs from the predictor database, the caches and removes all |
| 293 // inflight navigations. | 294 // inflight navigations. |
| 294 void DeleteAllUrls(); | 295 void DeleteAllUrls(); |
| 295 | 296 |
| 296 // Deletes data for the input |urls| and their corresponding hosts from the | 297 // Deletes data for the input |urls| and their corresponding hosts from the |
| 297 // predictor database and caches. | 298 // predictor database and caches. |
| 298 void DeleteUrls(const history::URLRows& urls); | 299 void DeleteUrls(const history::URLRows& urls); |
| 299 | 300 |
| 300 // Callback for GetUrlVisitCountTask. | 301 // Callback for GetUrlVisitCountTask. |
| 301 void OnVisitCountLookup(size_t visit_count, | 302 void OnVisitCountLookup(size_t url_visit_count, |
| 302 const NavigationID& navigation_id, | |
| 303 const PageRequestSummary& summary); | 303 const PageRequestSummary& summary); |
| 304 | 304 |
| 305 // Removes the oldest entry in the input |data_map|, also deleting it from the | 305 // Removes the oldest entry in the input |data_map|, also deleting it from the |
| 306 // predictor database. | 306 // predictor database. |
| 307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, | 307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, |
| 308 PrefetchDataMap* data_map); | 308 PrefetchDataMap* data_map); |
| 309 | 309 |
| 310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, | 310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, |
| 311 RedirectDataMap* data_map); | 311 RedirectDataMap* data_map); |
| 312 | 312 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 | 349 |
| 350 // Used to connect to HistoryService or register for service loaded | 350 // Used to connect to HistoryService or register for service loaded |
| 351 // notificatioan. | 351 // notificatioan. |
| 352 void ConnectToHistoryService(); | 352 void ConnectToHistoryService(); |
| 353 | 353 |
| 354 // Used for testing to inject mock tables. | 354 // Used for testing to inject mock tables. |
| 355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
| 356 tables_ = tables; | 356 tables_ = tables; |
| 357 } | 357 } |
| 358 | 358 |
| 359 // Sets the |observer| to be notified when the resource prefetch predictor | |
| 360 // data changes. Previously registered observer will be discarded. | |
| 361 void SetObserverForTesting(TestObserver* observer); | |
| 362 | |
| 359 Profile* const profile_; | 363 Profile* const profile_; |
| 364 TestObserver* observer_; | |
| 360 ResourcePrefetchPredictorConfig const config_; | 365 ResourcePrefetchPredictorConfig const config_; |
| 361 InitializationState initialization_state_; | 366 InitializationState initialization_state_; |
| 362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 367 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 368 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 364 base::CancelableTaskTracker history_lookup_consumer_; | 369 base::CancelableTaskTracker history_lookup_consumer_; |
| 365 | 370 |
| 366 // Copy of the data in the predictor tables. | 371 // Copy of the data in the predictor tables. |
| 367 std::unique_ptr<PrefetchDataMap> url_table_cache_; | 372 std::unique_ptr<PrefetchDataMap> url_table_cache_; |
| 368 std::unique_ptr<PrefetchDataMap> host_table_cache_; | 373 std::unique_ptr<PrefetchDataMap> host_table_cache_; |
| 369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; | 374 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; |
| 370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; | 375 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; |
| 371 | 376 |
| 372 NavigationMap inflight_navigations_; | 377 NavigationMap inflight_navigations_; |
| 373 | 378 |
| 374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | 379 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> |
| 375 history_service_observer_; | 380 history_service_observer_; |
| 376 | 381 |
| 377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 382 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 378 }; | 383 }; |
| 379 | 384 |
| 380 } // namespace predictors | 385 } // namespace predictors |
| 381 | 386 |
| 382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 387 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |