Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.h

Issue 2796783004: predictors: Add origin learning. (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // 97 //
98 // TODO(zhenw): Currently only main frame requests/redirects/responses are 98 // TODO(zhenw): Currently only main frame requests/redirects/responses are
99 // recorded. Consider recording sub-frame responses independently or together 99 // recorded. Consider recording sub-frame responses independently or together
100 // with main frame. 100 // with main frame.
101 class ResourcePrefetchPredictor 101 class ResourcePrefetchPredictor
102 : public KeyedService, 102 : public KeyedService,
103 public history::HistoryServiceObserver, 103 public history::HistoryServiceObserver,
104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>,
105 public precache::PrecacheManager::Delegate { 105 public precache::PrecacheManager::Delegate {
106 public: 106 public:
107 struct OriginRequestSummary {
alexilin 2017/04/12 13:53:08 Oh, and a comment to this class would be nice.
108 OriginRequestSummary();
109 OriginRequestSummary(const OriginRequestSummary& other);
110 ~OriginRequestSummary();
111
112 GURL origin;
113 bool always_access_network;
114 bool accessed_network;
115 int first_occurrence;
116 };
117
107 // Stores the data that we need to get from the URLRequest. 118 // Stores the data that we need to get from the URLRequest.
108 struct URLRequestSummary { 119 struct URLRequestSummary {
109 URLRequestSummary(); 120 URLRequestSummary();
110 URLRequestSummary(const URLRequestSummary& other); 121 URLRequestSummary(const URLRequestSummary& other);
111 ~URLRequestSummary(); 122 ~URLRequestSummary();
112 123
113 NavigationID navigation_id; 124 NavigationID navigation_id;
114 GURL resource_url; 125 GURL resource_url;
126 GURL request_url; // URL after all redirects.
alexilin 2017/04/12 13:44:05 I'd say it's last issued URL comparing that resour
115 content::ResourceType resource_type; 127 content::ResourceType resource_type;
116 net::RequestPriority priority; 128 net::RequestPriority priority;
117 129
118 // Only for responses. 130 // Only for responses.
119 std::string mime_type; 131 std::string mime_type;
120 bool was_cached; 132 bool was_cached;
121 GURL redirect_url; // Empty unless request was redirected to a valid url. 133 GURL redirect_url; // Empty unless request was redirected to a valid url.
122 134
123 bool has_validators; 135 bool has_validators;
124 bool always_revalidate; 136 bool always_revalidate;
137 bool is_no_store;
138 bool network_accessed;
125 139
126 // Initializes a |URLRequestSummary| from a |URLRequest| response. 140 // Initializes a |URLRequestSummary| from a |URLRequest| response.
127 // Returns true for success. Note: NavigationID is NOT initialized 141 // Returns true for success. Note: NavigationID is NOT initialized
128 // by this function. 142 // by this function.
129 static bool SummarizeResponse(const net::URLRequest& request, 143 static bool SummarizeResponse(const net::URLRequest& request,
130 URLRequestSummary* summary); 144 URLRequestSummary* summary);
131 }; 145 };
132 146
133 // Stores the data learned from a single navigation. 147 // Stores the data learned from a single navigation.
134 struct PageRequestSummary { 148 struct PageRequestSummary {
135 explicit PageRequestSummary(const GURL& main_frame_url); 149 explicit PageRequestSummary(const GURL& main_frame_url);
136 PageRequestSummary(const PageRequestSummary& other); 150 PageRequestSummary(const PageRequestSummary& other);
137 ~PageRequestSummary(); 151 ~PageRequestSummary();
138 152
139 GURL main_frame_url; 153 GURL main_frame_url;
140 GURL initial_url; 154 GURL initial_url;
141 155
142 // Stores all subresource requests within a single navigation, from initial 156 // Stores all subresource requests within a single navigation, from initial
143 // main frame request to navigation completion. 157 // main frame request to navigation completion.
144 std::vector<URLRequestSummary> subresource_requests; 158 std::vector<URLRequestSummary> subresource_requests;
159 // Map of origin -> OriginRequestSummary. Only one instance of each origin
160 // is kept per navigation, but the summary is updated several times.
161 std::map<GURL, OriginRequestSummary> origins;
145 }; 162 };
146 163
147 // Stores a result of prediction. Essentially, |subresource_urls| is main 164 // Stores a result of prediction. Essentially, |subresource_urls| is main
148 // result and other fields are used for diagnosis and histograms reporting. 165 // result and other fields are used for diagnosis and histograms reporting.
149 struct Prediction { 166 struct Prediction {
150 Prediction(); 167 Prediction();
151 Prediction(const Prediction& other); 168 Prediction(const Prediction& other);
152 ~Prediction(); 169 ~Prediction();
153 170
154 bool is_host; 171 bool is_host;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 TestPrefetchingDurationHistogram); 290 TestPrefetchingDurationHistogram);
274 291
275 enum InitializationState { 292 enum InitializationState {
276 NOT_INITIALIZED = 0, 293 NOT_INITIALIZED = 0,
277 INITIALIZING = 1, 294 INITIALIZING = 1,
278 INITIALIZED = 2 295 INITIALIZED = 2
279 }; 296 };
280 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 297 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
281 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; 298 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
282 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; 299 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap;
300 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap;
283 301
284 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> 302 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>>
285 NavigationMap; 303 NavigationMap;
286 304
287 // Returns true if the main page request is supported for prediction. 305 // Returns true if the main page request is supported for prediction.
288 static bool IsHandledMainPage(net::URLRequest* request); 306 static bool IsHandledMainPage(net::URLRequest* request);
289 307
290 // Returns true if the subresource request is supported for prediction. 308 // Returns true if the subresource request is supported for prediction.
291 static bool IsHandledSubresource(net::URLRequest* request, 309 static bool IsHandledSubresource(net::URLRequest* request,
292 content::ResourceType resource_type); 310 content::ResourceType resource_type);
293 311
294 // Returns true if the subresource has a supported type. 312 // Returns true if the subresource has a supported type.
295 static bool IsHandledResourceType(content::ResourceType resource_type, 313 static bool IsHandledResourceType(content::ResourceType resource_type,
296 const std::string& mime_type); 314 const std::string& mime_type);
297 315
298 // Returns true if the request (should have a response in it) is "no-store". 316 // Returns true if the request (should have a response in it) is "no-store".
299 static bool IsNoStore(const net::URLRequest* request); 317 static bool IsNoStore(const net::URLRequest& request);
300 318
301 // Returns true iff |redirect_data_map| contains confident redirect endpoint 319 // Returns true iff |redirect_data_map| contains confident redirect endpoint
302 // for |entry_point| and assigns it to the |redirect_endpoint|. 320 // for |entry_point| and assigns it to the |redirect_endpoint|.
303 static bool GetRedirectEndpoint(const std::string& entry_point, 321 static bool GetRedirectEndpoint(const std::string& entry_point,
304 const RedirectDataMap& redirect_data_map, 322 const RedirectDataMap& redirect_data_map,
305 std::string* redirect_endpoint); 323 std::string* redirect_endpoint);
306 324
307 static void SetAllowPortInUrlsForTesting(bool state); 325 static void SetAllowPortInUrlsForTesting(bool state);
308 326
309 // KeyedService methods override. 327 // KeyedService methods override.
310 void Shutdown() override; 328 void Shutdown() override;
311 329
312 // Functions called on different network events pertaining to the loading of 330 // Functions called on different network events pertaining to the loading of
313 // main frame resource or sub resources. 331 // main frame resource or sub resources.
314 void OnMainFrameRequest(const URLRequestSummary& request); 332 void OnMainFrameRequest(const URLRequestSummary& request);
315 void OnMainFrameResponse(const URLRequestSummary& response); 333 void OnMainFrameResponse(const URLRequestSummary& response);
316 void OnMainFrameRedirect(const URLRequestSummary& response); 334 void OnMainFrameRedirect(const URLRequestSummary& response);
317 void OnSubresourceResponse(const URLRequestSummary& response); 335 void OnSubresourceResponse(const URLRequestSummary& response);
336 void OnSubresourceRedirect(const URLRequestSummary& response);
318 337
319 // Called when onload completes for a navigation. We treat this point as the 338 // Called when onload completes for a navigation. We treat this point as the
320 // "completion" of the navigation. The resources requested by the page up to 339 // "completion" of the navigation. The resources requested by the page up to
321 // this point are the only ones considered for prefetching. 340 // this point are the only ones considered for prefetching.
322 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); 341 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info);
323 342
324 // Returns true iff there is PrefetchData that can be used for a 343 // Returns true iff there is PrefetchData that can be used for a
325 // |main_frame_url| and fills |prediction| with resources that need to be 344 // |main_frame_url| and fills |prediction| with resources that need to be
326 // prefetched. |prediction| pointer may be equal nullptr to get return value 345 // prefetched. |prediction| pointer may be equal nullptr to get return value
327 // only. 346 // only.
328 bool GetPrefetchData(const GURL& main_frame_url, 347 bool GetPrefetchData(const GURL& main_frame_url,
329 Prediction* prediction) const; 348 Prediction* prediction) const;
330 349
331 // Returns true iff the |data_map| contains PrefetchData that can be used 350 // Returns true iff the |data_map| contains PrefetchData that can be used
332 // for a |main_frame_key| and fills |urls| with resources that need to be 351 // for a |main_frame_key| and fills |urls| with resources that need to be
333 // prefetched. |urls| pointer may be equal nullptr to get return value only. 352 // prefetched. |urls| pointer may be equal nullptr to get return value only.
334 bool PopulatePrefetcherRequest(const std::string& main_frame_key, 353 bool PopulatePrefetcherRequest(const std::string& main_frame_key,
335 const PrefetchDataMap& data_map, 354 const PrefetchDataMap& data_map,
336 std::vector<GURL>* urls) const; 355 std::vector<GURL>* urls) const;
337 356
338 // Callback for task to read predictor database. Takes ownership of 357 // Callback for task to read predictor database. Takes ownership of
339 // all arguments. 358 // all arguments.
340 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 359 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
341 std::unique_ptr<PrefetchDataMap> host_data_map, 360 std::unique_ptr<PrefetchDataMap> host_data_map,
342 std::unique_ptr<RedirectDataMap> url_redirect_data_map, 361 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
343 std::unique_ptr<RedirectDataMap> host_redirect_data_map, 362 std::unique_ptr<RedirectDataMap> host_redirect_data_map,
344 std::unique_ptr<ManifestDataMap> manifest_data_map); 363 std::unique_ptr<ManifestDataMap> manifest_data_map,
364 std::unique_ptr<OriginDataMap> origin_data_map);
345 365
346 // Called during initialization when history is read and the predictor 366 // Called during initialization when history is read and the predictor
347 // database has been read. 367 // database has been read.
348 void OnHistoryAndCacheLoaded(); 368 void OnHistoryAndCacheLoaded();
349 369
350 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. 370 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_.
351 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 371 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
352 372
353 // Deletes all URLs from the predictor database, the caches and removes all 373 // Deletes all URLs from the predictor database, the caches and removes all
354 // inflight navigations. 374 // inflight navigations.
355 void DeleteAllUrls(); 375 void DeleteAllUrls();
356 376
357 // Deletes data for the input |urls| and their corresponding hosts from the 377 // Deletes data for the input |urls| and their corresponding hosts from the
358 // predictor database and caches. 378 // predictor database and caches.
359 void DeleteUrls(const history::URLRows& urls); 379 void DeleteUrls(const history::URLRows& urls);
360 380
361 // Callback for GetUrlVisitCountTask. 381 // Callback for GetUrlVisitCountTask.
362 void OnVisitCountLookup(size_t url_visit_count, 382 void OnVisitCountLookup(size_t url_visit_count,
363 const PageRequestSummary& summary); 383 const PageRequestSummary& summary);
364 384
365 // Removes the oldest entry in the input |data_map|, also deleting it from the 385 // Removes the oldest entry in the input |data_map|, also deleting it from the
366 // predictor database. 386 // predictor database.
367 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, 387 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
368 PrefetchDataMap* data_map); 388 PrefetchDataMap* data_map);
369 389
370 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, 390 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type,
371 RedirectDataMap* data_map); 391 RedirectDataMap* data_map);
372 392
373 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map); 393 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map);
394 void RemoveOldestEntryInOriginDataMap(OriginDataMap* data_map);
374 395
375 // Merges resources in |new_resources| into the |data_map| and correspondingly 396 // Merges resources in |new_resources| into the |data_map| and correspondingly
376 // updates the predictor database. Also calls LearnRedirect if relevant. 397 // updates the predictor database. Also calls LearnRedirect if relevant.
377 void LearnNavigation(const std::string& key, 398 void LearnNavigation(const std::string& key,
378 PrefetchKeyType key_type, 399 PrefetchKeyType key_type,
379 const std::vector<URLRequestSummary>& new_resources, 400 const std::vector<URLRequestSummary>& new_resources,
380 size_t max_data_map_size, 401 size_t max_data_map_size,
381 PrefetchDataMap* data_map, 402 PrefetchDataMap* data_map,
382 const std::string& key_before_redirects, 403 const std::string& key_before_redirects,
383 RedirectDataMap* redirect_map); 404 RedirectDataMap* redirect_map);
384 405
385 // Updates information about final redirect destination for |key| in 406 // Updates information about final redirect destination for |key| in
386 // |redirect_map| and correspondingly updates the predictor database. 407 // |redirect_map| and correspondingly updates the predictor database.
387 void LearnRedirect(const std::string& key, 408 void LearnRedirect(const std::string& key,
388 PrefetchKeyType key_type, 409 PrefetchKeyType key_type,
389 const std::string& final_redirect, 410 const std::string& final_redirect,
390 size_t max_redirect_map_size, 411 size_t max_redirect_map_size,
391 RedirectDataMap* redirect_map); 412 RedirectDataMap* redirect_map);
392 413
414 void LearnOrigins(const std::string& host,
415 const std::map<GURL, OriginRequestSummary>& summaries,
416 size_t max_data_map_size,
417 OriginDataMap* data_map);
418
393 // Reports database readiness metric defined as percentage of navigated hosts 419 // Reports database readiness metric defined as percentage of navigated hosts
394 // found in DB for last X entries in history. 420 // found in DB for last X entries in history.
395 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const; 421 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const;
396 422
397 // history::HistoryServiceObserver: 423 // history::HistoryServiceObserver:
398 void OnURLsDeleted(history::HistoryService* history_service, 424 void OnURLsDeleted(history::HistoryService* history_service,
399 bool all_history, 425 bool all_history,
400 bool expired, 426 bool expired,
401 const history::URLRows& deleted_rows, 427 const history::URLRows& deleted_rows,
402 const std::set<GURL>& favicon_urls) override; 428 const std::set<GURL>& favicon_urls) override;
(...skipping 16 matching lines...) Expand all
419 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 445 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
420 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 446 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
421 base::CancelableTaskTracker history_lookup_consumer_; 447 base::CancelableTaskTracker history_lookup_consumer_;
422 448
423 // Copy of the data in the predictor tables. 449 // Copy of the data in the predictor tables.
424 std::unique_ptr<PrefetchDataMap> url_table_cache_; 450 std::unique_ptr<PrefetchDataMap> url_table_cache_;
425 std::unique_ptr<PrefetchDataMap> host_table_cache_; 451 std::unique_ptr<PrefetchDataMap> host_table_cache_;
426 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; 452 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_;
427 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; 453 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_;
428 std::unique_ptr<ManifestDataMap> manifest_table_cache_; 454 std::unique_ptr<ManifestDataMap> manifest_table_cache_;
455 std::unique_ptr<OriginDataMap> origin_table_cache_;
429 456
430 std::map<GURL, base::TimeTicks> inflight_prefetches_; 457 std::map<GURL, base::TimeTicks> inflight_prefetches_;
431 NavigationMap inflight_navigations_; 458 NavigationMap inflight_navigations_;
432 459
433 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> 460 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>>
434 prefetcher_stats_; 461 prefetcher_stats_;
435 462
436 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 463 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
437 history_service_observer_; 464 history_service_observer_;
438 465
(...skipping 26 matching lines...) Expand all
465 492
466 private: 493 private:
467 ResourcePrefetchPredictor* predictor_; 494 ResourcePrefetchPredictor* predictor_;
468 495
469 DISALLOW_COPY_AND_ASSIGN(TestObserver); 496 DISALLOW_COPY_AND_ASSIGN(TestObserver);
470 }; 497 };
471 498
472 } // namespace predictors 499 } // namespace predictors
473 500
474 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 501 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698