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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 class ResourcePrefetcherManager; | 88 class ResourcePrefetcherManager; |
89 | 89 |
90 // Contains logic for learning what can be prefetched and for kicking off | 90 // Contains logic for learning what can be prefetched and for kicking off |
91 // speculative prefetching. | 91 // speculative prefetching. |
92 // - The class is a profile keyed service owned by the profile. | 92 // - The class is a profile keyed service owned by the profile. |
93 // - All the non-static methods of this class need to be called on the UI | 93 // - All the non-static methods of this class need to be called on the UI |
94 // thread. | 94 // thread. |
95 // | 95 // |
96 // The overall flow of the resource prefetching algorithm is as follows: | 96 // The overall flow of the resource prefetching algorithm is as follows: |
97 // | 97 // |
98 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and | 98 // * LoadingPredictorObserver - Listens for URL requests, responses and |
99 // redirects (client-side redirects are not supported) on the IO thread (via | 99 // redirects (client-side redirects are not supported) on the IO thread (via |
100 // ResourceDispatcherHostDelegate) and posts tasks to the | 100 // ResourceDispatcherHostDelegate) and posts tasks to the |
101 // ResourcePrefetchPredictor on the UI thread. This is owned by the | 101 // ResourcePrefetchPredictor on the UI thread. This is owned by the |
102 // ProfileIOData for the profile. | 102 // ProfileIOData for the profile. |
103 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data | 103 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data |
104 // to a sql database. Runs entirely on the DB thread. Owned by the | 104 // to a sql database. Runs entirely on the DB thread. Owned by the |
105 // PredictorDatabase. | 105 // PredictorDatabase. |
106 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in | 106 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in |
107 // the UI thread through the ResourcePrefetchPredictorObserver and persists | 107 // the UI thread through the LoadingPredictorObserver and persists |
108 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It | 108 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It |
109 // initiates resource prefetching using the ResourcePrefetcherManager. Owned | 109 // initiates resource prefetching using the ResourcePrefetcherManager. Owned |
110 // by profile. | 110 // by profile. |
111 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the | 111 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the |
112 // prefetching on the IO thread. The manager is owned by the | 112 // prefetching on the IO thread. The manager is owned by the |
113 // ResourcePrefetchPredictor and interfaces between the predictor on the UI | 113 // ResourcePrefetchPredictor and interfaces between the predictor on the UI |
114 // thread and the prefetchers on the IO thread. | 114 // thread and the prefetchers on the IO thread. |
115 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | 115 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
116 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 116 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
117 // | 117 // |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 212 |
213 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 213 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
214 Profile* profile); | 214 Profile* profile); |
215 ~ResourcePrefetchPredictor() override; | 215 ~ResourcePrefetchPredictor() override; |
216 | 216 |
217 // Starts initialization by posting a task to the DB thread to read the | 217 // Starts initialization by posting a task to the DB thread to read the |
218 // predictor database. Virtual for testing. | 218 // predictor database. Virtual for testing. |
219 virtual void StartInitialization(); | 219 virtual void StartInitialization(); |
220 virtual void Shutdown(); | 220 virtual void Shutdown(); |
221 | 221 |
222 // Thread safe. | |
223 static bool ShouldRecordRequest(net::URLRequest* request, | |
224 content::ResourceType resource_type); | |
225 static bool ShouldRecordResponse(net::URLRequest* response); | |
226 static bool ShouldRecordRedirect(net::URLRequest* response); | |
227 | |
228 // Determines the resource type from the declared one, falling back to MIME | 222 // Determines the resource type from the declared one, falling back to MIME |
229 // type detection when it is not explicit. | 223 // type detection when it is not explicit. |
230 static content::ResourceType GetResourceType( | 224 static content::ResourceType GetResourceType( |
231 content::ResourceType resource_type, | 225 content::ResourceType resource_type, |
232 const std::string& mime_type); | 226 const std::string& mime_type); |
233 | 227 |
234 // Determines the ResourceType from the mime type, defaulting to the | 228 // Determines the ResourceType from the mime type, defaulting to the |
235 // |fallback| if the ResourceType could not be determined. | 229 // |fallback| if the ResourceType could not be determined. |
236 static content::ResourceType GetResourceTypeFromMimeType( | 230 static content::ResourceType GetResourceTypeFromMimeType( |
237 const std::string& mime_type, | 231 const std::string& mime_type, |
238 content::ResourceType fallback); | 232 content::ResourceType fallback); |
239 | 233 |
240 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the | 234 // 'LoadingPredictorObserver' calls the below functions to inform the |
241 // predictor of main frame and resource requests. Should only be called if the | 235 // predictor of main frame and resource requests. Should only be called if the |
242 // corresponding Should* functions return true. | 236 // corresponding Should* functions return true. |
243 void RecordURLRequest(const URLRequestSummary& request); | 237 void RecordURLRequest(const URLRequestSummary& request); |
244 void RecordURLResponse(const URLRequestSummary& response); | 238 void RecordURLResponse(const URLRequestSummary& response); |
245 void RecordURLRedirect(const URLRequestSummary& response); | 239 void RecordURLRedirect(const URLRequestSummary& response); |
246 | 240 |
247 // Called when the main frame of a page completes loading. | 241 // Called when the main frame of a page completes loading. |
248 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 242 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
249 | 243 |
250 // Called after the main frame's first contentful paint. | 244 // Called after the main frame's first contentful paint. |
(...skipping 27 matching lines...) Expand all Loading... | |
278 // Starts prefetching if it is enabled and prefetching data exists for the | 272 // Starts prefetching if it is enabled and prefetching data exists for the |
279 // |main_frame_url| either at the URL or at the host level. | 273 // |main_frame_url| either at the URL or at the host level. |
280 void StartPrefetching(const GURL& main_frame_url); | 274 void StartPrefetching(const GURL& main_frame_url); |
281 | 275 |
282 // Stops prefetching that may be in progress corresponding to | 276 // Stops prefetching that may be in progress corresponding to |
283 // |main_frame_url|. | 277 // |main_frame_url|. |
284 void StopPrefetching(const GURL& main_frame_url); | 278 void StopPrefetching(const GURL& main_frame_url); |
285 | 279 |
286 friend class LoadingPredictor; | 280 friend class LoadingPredictor; |
287 friend class ::PredictorsHandler; | 281 friend class ::PredictorsHandler; |
282 friend class LoadingDataCollectorTest; | |
Benoit L
2017/06/02 12:43:59
Is this needed?
trevordixon
2017/06/06 13:08:04
No longer.
| |
288 friend class ResourcePrefetchPredictorTest; | 283 friend class ResourcePrefetchPredictorTest; |
289 friend class ResourcePrefetchPredictorBrowserTest; | 284 friend class ResourcePrefetchPredictorBrowserTest; |
290 | 285 |
291 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 286 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
292 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 287 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
293 LazilyInitializeEmpty); | 288 LazilyInitializeEmpty); |
294 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 289 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
295 LazilyInitializeWithData); | 290 LazilyInitializeWithData); |
296 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 291 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
297 NavigationLowHistoryCount); | 292 NavigationLowHistoryCount); |
298 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); | 293 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); |
299 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); | 294 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); |
300 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 295 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
301 NavigationUrlNotInDBAndDBFull); | 296 NavigationUrlNotInDBAndDBFull); |
302 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); | 297 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); |
303 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); | 298 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); |
304 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostNotInDB); | 299 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostNotInDB); |
305 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostInDB); | 300 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, ManifestHostInDB); |
306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 301 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
307 ManifestHostNotInDBAndDBFull); | 302 ManifestHostNotInDBAndDBFull); |
308 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 303 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
309 ManifestUnusedRemoved); | 304 ManifestUnusedRemoved); |
310 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); | 305 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); |
311 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); | 306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); |
312 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 307 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
313 OnSubresourceResponse); | 308 OnSubresourceResponse); |
314 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); | 309 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); |
315 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); | |
316 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 310 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
317 PopulatePrefetcherRequest); | 311 PopulatePrefetcherRequest); |
318 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); | 312 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, PopulateFromManifest); |
319 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); | 313 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); |
320 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); | 314 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); |
321 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 315 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
322 TestPrecisionRecallHistograms); | 316 TestPrecisionRecallHistograms); |
323 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 317 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
324 TestPrefetchingDurationHistogram); | 318 TestPrefetchingDurationHistogram); |
325 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 319 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
326 TestRecordFirstContentfulPaint); | 320 TestRecordFirstContentfulPaint); |
327 | 321 |
328 enum InitializationState { | 322 enum InitializationState { |
329 NOT_INITIALIZED = 0, | 323 NOT_INITIALIZED = 0, |
330 INITIALIZING = 1, | 324 INITIALIZING = 1, |
331 INITIALIZED = 2 | 325 INITIALIZED = 2 |
332 }; | 326 }; |
333 typedef GlowplugKeyValueData<PrefetchData, internal::LastVisitTimeCompare> | 327 typedef GlowplugKeyValueData<PrefetchData, internal::LastVisitTimeCompare> |
334 PrefetchDataMap; | 328 PrefetchDataMap; |
335 typedef GlowplugKeyValueData<RedirectData, internal::LastVisitTimeCompare> | 329 typedef GlowplugKeyValueData<RedirectData, internal::LastVisitTimeCompare> |
336 RedirectDataMap; | 330 RedirectDataMap; |
337 typedef GlowplugKeyValueData<precache::PrecacheManifest, | 331 typedef GlowplugKeyValueData<precache::PrecacheManifest, |
338 internal::ManifestCompare> | 332 internal::ManifestCompare> |
339 ManifestDataMap; | 333 ManifestDataMap; |
340 typedef GlowplugKeyValueData<OriginData, internal::LastVisitTimeCompare> | 334 typedef GlowplugKeyValueData<OriginData, internal::LastVisitTimeCompare> |
341 OriginDataMap; | 335 OriginDataMap; |
342 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> | 336 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> |
343 NavigationMap; | 337 NavigationMap; |
344 | 338 |
345 // Returns true if the main page request is supported for prediction. | |
346 static bool IsHandledMainPage(net::URLRequest* request); | |
347 | |
348 // Returns true if the subresource request is supported for prediction. | |
349 static bool IsHandledSubresource(net::URLRequest* request, | |
350 content::ResourceType resource_type); | |
351 | |
352 // Returns true if the subresource has a supported type. | |
353 static bool IsHandledResourceType(content::ResourceType resource_type, | |
354 const std::string& mime_type); | |
355 | |
356 // Returns true if the request (should have a response in it) is "no-store". | 339 // Returns true if the request (should have a response in it) is "no-store". |
357 static bool IsNoStore(const net::URLRequest& request); | 340 static bool IsNoStore(const net::URLRequest& request); |
358 | 341 |
359 static void SetAllowPortInUrlsForTesting(bool state); | |
360 | |
361 // Functions called on different network events pertaining to the loading of | 342 // Functions called on different network events pertaining to the loading of |
362 // main frame resource or sub resources. | 343 // main frame resource or sub resources. |
363 void OnMainFrameRequest(const URLRequestSummary& request); | 344 void OnMainFrameRequest(const URLRequestSummary& request); |
364 void OnMainFrameRedirect(const URLRequestSummary& response); | 345 void OnMainFrameRedirect(const URLRequestSummary& response); |
365 void OnSubresourceResponse(const URLRequestSummary& response); | 346 void OnSubresourceResponse(const URLRequestSummary& response); |
366 void OnSubresourceRedirect(const URLRequestSummary& response); | 347 void OnSubresourceRedirect(const URLRequestSummary& response); |
367 | 348 |
368 // Called when onload completes for a navigation. We treat this point as the | 349 // Called when onload completes for a navigation. We treat this point as the |
369 // "completion" of the navigation. The resources requested by the page up to | 350 // "completion" of the navigation. The resources requested by the page up to |
370 // this point are the only ones considered for prefetching. | 351 // this point are the only ones considered for prefetching. |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
521 | 502 |
522 private: | 503 private: |
523 ResourcePrefetchPredictor* predictor_; | 504 ResourcePrefetchPredictor* predictor_; |
524 | 505 |
525 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 506 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
526 }; | 507 }; |
527 | 508 |
528 } // namespace predictors | 509 } // namespace predictors |
529 | 510 |
530 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 511 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
OLD | NEW |