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

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

Issue 2928033003: predictors: move ResourcePrefetcher handling to LoadingPredictor. (Closed)
Patch Set: Address comment. Created 3 years, 5 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>
11 #include <memory> 11 #include <memory>
12 #include <set> 12 #include <set>
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
19 #include "base/scoped_observer.h" 19 #include "base/scoped_observer.h"
20 #include "base/task/cancelable_task_tracker.h" 20 #include "base/task/cancelable_task_tracker.h"
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "chrome/browser/predictors/glowplug_key_value_data.h" 22 #include "chrome/browser/predictors/glowplug_key_value_data.h"
23 #include "chrome/browser/predictors/loading_predictor_config.h" 23 #include "chrome/browser/predictors/loading_predictor_config.h"
24 #include "chrome/browser/predictors/resource_prefetch_common.h" 24 #include "chrome/browser/predictors/resource_prefetch_common.h"
25 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 25 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
26 #include "chrome/browser/predictors/resource_prefetcher.h" 26 #include "chrome/browser/predictors/resource_prefetcher.h"
27 #include "chrome/browser/predictors/resource_prefetcher_manager.h"
28 #include "components/history/core/browser/history_db_task.h" 27 #include "components/history/core/browser/history_db_task.h"
29 #include "components/history/core/browser/history_service_observer.h" 28 #include "components/history/core/browser/history_service_observer.h"
30 #include "components/history/core/browser/history_types.h" 29 #include "components/history/core/browser/history_types.h"
31 #include "components/keyed_service/core/keyed_service.h" 30 #include "components/keyed_service/core/keyed_service.h"
32 #include "components/precache/content/precache_manager.h" 31 #include "components/precache/content/precache_manager.h"
33 #include "content/public/common/resource_type.h" 32 #include "content/public/common/resource_type.h"
34 #include "url/gurl.h" 33 #include "url/gurl.h"
35 34
36 class PredictorsHandler; 35 class PredictorsHandler;
37 class Profile; 36 class Profile;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 // LoadingDataCollector on the UI thread. This is owned by the ProfileIOData 92 // LoadingDataCollector on the UI thread. This is owned by the ProfileIOData
94 // for the profile. 93 // for the profile.
95 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data 94 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
96 // to a sql database. Runs entirely on the DB thread. Owned by the 95 // to a sql database. Runs entirely on the DB thread. Owned by the
97 // PredictorDatabase. 96 // PredictorDatabase.
98 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in 97 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
99 // the UI thread through the LoadingPredictorObserver and persists 98 // the UI thread through the LoadingPredictorObserver and persists
100 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It 99 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It
101 // initiates resource prefetching using the ResourcePrefetcherManager. Owned 100 // initiates resource prefetching using the ResourcePrefetcherManager. Owned
102 // by profile. 101 // by profile.
103 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the
104 // prefetching on the IO thread. The manager is owned by the
105 // ResourcePrefetchPredictor and interfaces between the predictor on the UI
106 // thread and the prefetchers on the IO thread.
107 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the
108 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources.
109 //
110 // TODO(zhenw): Currently only main frame requests/redirects/responses are
111 // recorded. Consider recording sub-frame responses independently or together
112 // with main frame.
113 class ResourcePrefetchPredictor 102 class ResourcePrefetchPredictor
114 : public history::HistoryServiceObserver, 103 : public history::HistoryServiceObserver,
115 public precache::PrecacheManager::Delegate { 104 public precache::PrecacheManager::Delegate {
116 public: 105 public:
117 // Data collected for origin-based prediction, for a single origin during a 106 // Data collected for origin-based prediction, for a single origin during a
118 // page load (see PageRequestSummary). 107 // page load (see PageRequestSummary).
119 struct OriginRequestSummary { 108 struct OriginRequestSummary {
120 OriginRequestSummary(); 109 OriginRequestSummary();
121 OriginRequestSummary(const OriginRequestSummary& other); 110 OriginRequestSummary(const OriginRequestSummary& other);
122 ~OriginRequestSummary(); 111 ~OriginRequestSummary();
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 static content::ResourceType GetResourceType( 215 static content::ResourceType GetResourceType(
227 content::ResourceType resource_type, 216 content::ResourceType resource_type,
228 const std::string& mime_type); 217 const std::string& mime_type);
229 218
230 // Determines the ResourceType from the mime type, defaulting to the 219 // Determines the ResourceType from the mime type, defaulting to the
231 // |fallback| if the ResourceType could not be determined. 220 // |fallback| if the ResourceType could not be determined.
232 static content::ResourceType GetResourceTypeFromMimeType( 221 static content::ResourceType GetResourceTypeFromMimeType(
233 const std::string& mime_type, 222 const std::string& mime_type,
234 content::ResourceType fallback); 223 content::ResourceType fallback);
235 224
236 // Called when ResourcePrefetcher is finished, i.e. there is nothing pending
237 // in flight.
238 void OnPrefetchingFinished(
239 const GURL& main_frame_url,
240 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats);
241
242 // Returns true if prefetching data exists for the |main_frame_url|. 225 // Returns true if prefetching data exists for the |main_frame_url|.
243 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; 226 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const;
244 227
245 // Returns true iff |resource| has sufficient confidence level and required 228 // Returns true iff |resource| has sufficient confidence level and required
246 // number of hits. 229 // number of hits.
247 bool IsResourcePrefetchable(const ResourceData& resource) const; 230 bool IsResourcePrefetchable(const ResourceData& resource) const;
248 231
249 // precache::PrecacheManager::Delegate: 232 // precache::PrecacheManager::Delegate:
250 void OnManifestFetched(const std::string& host, 233 void OnManifestFetched(const std::string& host,
251 const precache::PrecacheManifest& manifest) override; 234 const precache::PrecacheManifest& manifest) override;
(...skipping 27 matching lines...) Expand all
279 void RecordURLRedirect(const URLRequestSummary& response); 262 void RecordURLRedirect(const URLRequestSummary& response);
280 263
281 // Called when the main frame of a page completes loading. 264 // Called when the main frame of a page completes loading.
282 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 265 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
283 266
284 // Called after the main frame's first contentful paint. 267 // Called after the main frame's first contentful paint.
285 void RecordFirstContentfulPaint( 268 void RecordFirstContentfulPaint(
286 const NavigationID& navigation_id, 269 const NavigationID& navigation_id,
287 const base::TimeTicks& first_contentful_paint); 270 const base::TimeTicks& first_contentful_paint);
288 271
289 // Starts prefetching for |main_frame_url| from a |prediction|.
290 void StartPrefetching(const GURL& main_frame_url,
291 const Prediction& prediction);
292
293 // Stops prefetching that may be in progress corresponding to
294 // |main_frame_url|.
295 void StopPrefetching(const GURL& main_frame_url);
296
297 friend class LoadingPredictor;
298 friend class ::PredictorsHandler; 272 friend class ::PredictorsHandler;
299 friend class LoadingDataCollector; 273 friend class LoadingDataCollector;
300 friend class ResourcePrefetchPredictorTest; 274 friend class ResourcePrefetchPredictorTest;
301 friend class ResourcePrefetchPredictorBrowserTest; 275 friend class ResourcePrefetchPredictorBrowserTest;
302 276
303 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 277 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
304 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 278 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
305 LazilyInitializeEmpty); 279 LazilyInitializeEmpty);
306 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 280 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
307 LazilyInitializeWithData); 281 LazilyInitializeWithData);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 419
446 // Used to connect to HistoryService or register for service loaded 420 // Used to connect to HistoryService or register for service loaded
447 // notificatioan. 421 // notificatioan.
448 void ConnectToHistoryService(); 422 void ConnectToHistoryService();
449 423
450 // Used for testing to inject mock tables. 424 // Used for testing to inject mock tables.
451 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { 425 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) {
452 tables_ = tables; 426 tables_ = tables;
453 } 427 }
454 428
455 // For testing.
456 void set_mock_resource_prefetcher_manager(
457 scoped_refptr<ResourcePrefetcherManager> prefetch_manager) {
458 prefetch_manager_ = prefetch_manager;
459 }
460
461 Profile* const profile_; 429 Profile* const profile_;
462 TestObserver* observer_; 430 TestObserver* observer_;
463 LoadingStatsCollector* stats_collector_; 431 LoadingStatsCollector* stats_collector_;
464 const LoadingPredictorConfig config_; 432 const LoadingPredictorConfig config_;
465 InitializationState initialization_state_; 433 InitializationState initialization_state_;
466 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 434 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
467 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
468 base::CancelableTaskTracker history_lookup_consumer_; 435 base::CancelableTaskTracker history_lookup_consumer_;
469 436
470 std::unique_ptr<PrefetchDataMap> url_resource_data_; 437 std::unique_ptr<PrefetchDataMap> url_resource_data_;
471 std::unique_ptr<PrefetchDataMap> host_resource_data_; 438 std::unique_ptr<PrefetchDataMap> host_resource_data_;
472 std::unique_ptr<RedirectDataMap> url_redirect_data_; 439 std::unique_ptr<RedirectDataMap> url_redirect_data_;
473 std::unique_ptr<RedirectDataMap> host_redirect_data_; 440 std::unique_ptr<RedirectDataMap> host_redirect_data_;
474 std::unique_ptr<ManifestDataMap> manifest_data_; 441 std::unique_ptr<ManifestDataMap> manifest_data_;
475 std::unique_ptr<OriginDataMap> origin_data_; 442 std::unique_ptr<OriginDataMap> origin_data_;
476 443
477 NavigationMap inflight_navigations_; 444 NavigationMap inflight_navigations_;
(...skipping 12 matching lines...) Expand all
490 public: 457 public:
491 // De-registers itself from |predictor_| on destruction. 458 // De-registers itself from |predictor_| on destruction.
492 virtual ~TestObserver(); 459 virtual ~TestObserver();
493 460
494 virtual void OnPredictorInitialized() {} 461 virtual void OnPredictorInitialized() {}
495 462
496 virtual void OnNavigationLearned( 463 virtual void OnNavigationLearned(
497 size_t url_visit_count, 464 size_t url_visit_count,
498 const ResourcePrefetchPredictor::PageRequestSummary& summary) {} 465 const ResourcePrefetchPredictor::PageRequestSummary& summary) {}
499 466
500 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {}
501
502 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {}
503
504 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {}
505
506 protected: 467 protected:
507 // |predictor| must be non-NULL and has to outlive the TestObserver. 468 // |predictor| must be non-NULL and has to outlive the TestObserver.
508 // Also the predictor must not have a TestObserver set. 469 // Also the predictor must not have a TestObserver set.
509 explicit TestObserver(ResourcePrefetchPredictor* predictor); 470 explicit TestObserver(ResourcePrefetchPredictor* predictor);
510 471
511 private: 472 private:
512 ResourcePrefetchPredictor* predictor_; 473 ResourcePrefetchPredictor* predictor_;
513 474
514 DISALLOW_COPY_AND_ASSIGN(TestObserver); 475 DISALLOW_COPY_AND_ASSIGN(TestObserver);
515 }; 476 };
516 477
517 } // namespace predictors 478 } // namespace predictors
518 479
519 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 480 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW
« no previous file with comments | « chrome/browser/predictors/loading_predictor.cc ('k') | chrome/browser/predictors/resource_prefetch_predictor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698