| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/history/history_types.h" | 16 #include "chrome/browser/history/history_types.h" |
| 17 #include "chrome/browser/predictors/resource_prefetcher.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_common.h" | 18 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 19 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 19 #include "chrome/browser/profiles/profile_keyed_service.h" | 20 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 20 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
| 21 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
| 22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 23 #include "webkit/glue/resource_type.h" | 24 #include "webkit/glue/resource_type.h" |
| 24 | 25 |
| 25 class PredictorsHandler; | 26 class PredictorsHandler; |
| 26 class Profile; | 27 class Profile; |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 class WebContents; | 30 class WebContents; |
| 30 } | 31 } |
| 31 | 32 |
| 32 namespace net { | 33 namespace net { |
| 33 class URLRequest; | 34 class URLRequest; |
| 34 } | 35 } |
| 35 | 36 |
| 36 namespace predictors { | 37 namespace predictors { |
| 37 | 38 |
| 39 class ResourcePrefetcherManager; |
| 40 |
| 38 // Contains logic for learning what can be prefetched and for kicking off | 41 // Contains logic for learning what can be prefetched and for kicking off |
| 39 // speculative prefetching. | 42 // speculative prefetching. |
| 40 // - The class is a profile keyed service owned by the profile. | 43 // - The class is a profile keyed service owned by the profile. |
| 41 // - All the non-static methods of this class need to be called on the UI | 44 // - All the non-static methods of this class need to be called on the UI |
| 42 // thread. | 45 // thread. |
| 43 // | 46 // |
| 44 // The overall flow of the resource prefetching algorithm is as follows: | 47 // The overall flow of the resource prefetching algorithm is as follows: |
| 45 // | 48 // |
| 46 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and | 49 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and |
| 47 // redirects on the IO thread(via RDHostDelegate) and post tasks to the | 50 // redirects on the IO thread(via RDHostDelegate) and post tasks to the |
| 48 // ResourcePrefetchPredictor on the UI thread. This is owned by the | 51 // ResourcePrefetchPredictor on the UI thread. This is owned by the |
| 49 // ProfileIOData for the profile. | 52 // ProfileIOData for the profile. |
| 50 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data | 53 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data |
| 51 // to a sql database. Runs entirely on the DB thread. Owned by the | 54 // to a sql database. Runs entirely on the DB thread. Owned by the |
| 52 // PredictorDatabase. | 55 // PredictorDatabase. |
| 53 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in | 56 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in |
| 54 // the UI thread through the ResourcePrefetchPredictorObserver and perisists | 57 // the UI thread through the ResourcePrefetchPredictorObserver and perisists |
| 55 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. | 58 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It |
| 56 // Owned by profile. | 59 // initiates resource prefetching using the ResourcePrefetcherManager. Owned |
| 60 // by profile. |
| 61 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the |
| 62 // prefetching on the IO thread. The manager is owned by the |
| 63 // ResourcePrefetchPredictor and interfaces between the predictor on the UI |
| 64 // thread and the prefetchers on the IO thread. |
| 65 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
| 66 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
| 57 // | 67 // |
| 58 // TODO(shishir): Implement the prefetching of resources. | |
| 59 // TODO(shishir): Do speculative prefetching for https resources and/or https | 68 // TODO(shishir): Do speculative prefetching for https resources and/or https |
| 60 // main_frame urls. | 69 // main_frame urls. |
| 61 class ResourcePrefetchPredictor | 70 class ResourcePrefetchPredictor |
| 62 : public ProfileKeyedService, | 71 : public ProfileKeyedService, |
| 63 public content::NotificationObserver, | 72 public content::NotificationObserver, |
| 64 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { | 73 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { |
| 65 public: | 74 public: |
| 66 // The following config allows us to change the predictor constants and run | |
| 67 // field trials with different constants. | |
| 68 struct Config { | |
| 69 // Initializes the config with default values. | |
| 70 Config(); | |
| 71 | |
| 72 // If a navigation hasn't seen a load complete event in this much time, it | |
| 73 // is considered abandoned. | |
| 74 int max_navigation_lifetime_seconds; // Default 60 | |
| 75 // Size of LRU caches for the Url data. | |
| 76 size_t max_urls_to_track; // Default 500 | |
| 77 // The number of times, we should have seen a visit to this Url in history | |
| 78 // to start tracking it. This is to ensure we dont bother with oneoff | |
| 79 // entries. | |
| 80 int min_url_visit_count; // Default 3 | |
| 81 // The maximum number of resources to store per entry. | |
| 82 int max_resources_per_entry; // Default 50 | |
| 83 // The number of consecutive misses after we stop tracking a resource Url. | |
| 84 int max_consecutive_misses; // Default 3 | |
| 85 // The number of resources we should report accuracy stats on. | |
| 86 int num_resources_assumed_prefetched; // Default 25 | |
| 87 }; | |
| 88 | |
| 89 // Stores the data that we need to get from the URLRequest. | 75 // Stores the data that we need to get from the URLRequest. |
| 90 struct URLRequestSummary { | 76 struct URLRequestSummary { |
| 91 URLRequestSummary(); | 77 URLRequestSummary(); |
| 92 URLRequestSummary(const URLRequestSummary& other); | 78 URLRequestSummary(const URLRequestSummary& other); |
| 93 ~URLRequestSummary(); | 79 ~URLRequestSummary(); |
| 94 | 80 |
| 95 NavigationID navigation_id; | 81 NavigationID navigation_id; |
| 96 GURL resource_url; | 82 GURL resource_url; |
| 97 ResourceType::Type resource_type; | 83 ResourceType::Type resource_type; |
| 98 | 84 |
| 99 // Only for responses. | 85 // Only for responses. |
| 100 std::string mime_type; | 86 std::string mime_type; |
| 101 bool was_cached; | 87 bool was_cached; |
| 102 }; | 88 }; |
| 103 | 89 |
| 104 ResourcePrefetchPredictor(const Config& config, Profile* profile); | 90 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 91 Profile* profile); |
| 105 virtual ~ResourcePrefetchPredictor(); | 92 virtual ~ResourcePrefetchPredictor(); |
| 106 | 93 |
| 107 // Thread safe. | 94 // Thread safe. |
| 108 static bool IsEnabled(Profile* profile); | |
| 109 static bool ShouldRecordRequest(net::URLRequest* request, | 95 static bool ShouldRecordRequest(net::URLRequest* request, |
| 110 ResourceType::Type resource_type); | 96 ResourceType::Type resource_type); |
| 111 static bool ShouldRecordResponse(net::URLRequest* response); | 97 static bool ShouldRecordResponse(net::URLRequest* response); |
| 112 static bool ShouldRecordRedirect(net::URLRequest* response); | 98 static bool ShouldRecordRedirect(net::URLRequest* response); |
| 113 | 99 |
| 114 static ResourceType::Type GetResourceTypeFromMimeType( | 100 static ResourceType::Type GetResourceTypeFromMimeType( |
| 115 const std::string& mime_type, | 101 const std::string& mime_type, |
| 116 ResourceType::Type fallback); | 102 ResourceType::Type fallback); |
| 117 | 103 |
| 118 void RecordURLRequest(const URLRequestSummary& request); | 104 void RecordURLRequest(const URLRequestSummary& request); |
| 119 void RecordUrlResponse(const URLRequestSummary& response); | 105 void RecordUrlResponse(const URLRequestSummary& response); |
| 120 void RecordUrlRedirect(const URLRequestSummary& response); | 106 void RecordUrlRedirect(const URLRequestSummary& response); |
| 121 | 107 |
| 108 // Called by ResourcePrefetcherManager to notify that prefetching has finished |
| 109 // for a navigation. |
| 110 virtual void FinishedPrefetchForNavigation( |
| 111 const NavigationID& navigation_id, |
| 112 scoped_ptr<ResourcePrefetcher::RequestVector> requests); |
| 113 |
| 122 private: | 114 private: |
| 123 friend class ::PredictorsHandler; | 115 friend class ::PredictorsHandler; |
| 124 friend class ResourcePrefetchPredictorTest; | 116 friend class ResourcePrefetchPredictorTest; |
| 125 | 117 |
| 126 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 118 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
| 127 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 119 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 128 LazilyInitializeEmpty); | 120 LazilyInitializeEmpty); |
| 129 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 121 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| 130 LazilyInitializeWithData); | 122 LazilyInitializeWithData); |
| 131 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 123 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 152 struct UrlTableCacheValue { | 144 struct UrlTableCacheValue { |
| 153 UrlTableCacheValue(); | 145 UrlTableCacheValue(); |
| 154 ~UrlTableCacheValue(); | 146 ~UrlTableCacheValue(); |
| 155 | 147 |
| 156 UrlTableRowVector rows; | 148 UrlTableRowVector rows; |
| 157 base::Time last_visit; | 149 base::Time last_visit; |
| 158 }; | 150 }; |
| 159 | 151 |
| 160 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; | 152 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; |
| 161 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; | 153 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; |
| 154 typedef std::map<NavigationID, ResourcePrefetcher::RequestVector*> ResultsMap; |
| 162 | 155 |
| 163 // content::NotificationObserver methods OVERRIDE. | 156 // content::NotificationObserver methods OVERRIDE. |
| 164 virtual void Observe(int type, | 157 virtual void Observe(int type, |
| 165 const content::NotificationSource& source, | 158 const content::NotificationSource& source, |
| 166 const content::NotificationDetails& details) OVERRIDE; | 159 const content::NotificationDetails& details) OVERRIDE; |
| 167 | 160 |
| 168 static bool IsHandledMainPage(net::URLRequest* request); | 161 static bool IsHandledMainPage(net::URLRequest* request); |
| 169 static bool IsHandledSubresource(net::URLRequest* response); | 162 static bool IsHandledSubresource(net::URLRequest* response); |
| 170 static bool IsCacheable(const net::URLRequest* response); | 163 static bool IsCacheable(const net::URLRequest* response); |
| 171 | 164 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 187 // Database and cache cleanup code. | 180 // Database and cache cleanup code. |
| 188 void RemoveAnEntryFromUrlDB(); | 181 void RemoveAnEntryFromUrlDB(); |
| 189 void DeleteAllUrls(); | 182 void DeleteAllUrls(); |
| 190 void DeleteUrls(const history::URLRows& urls); | 183 void DeleteUrls(const history::URLRows& urls); |
| 191 | 184 |
| 192 bool ShouldTrackUrl(const GURL& url); | 185 bool ShouldTrackUrl(const GURL& url); |
| 193 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 186 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
| 194 void OnNavigationComplete(const NavigationID& navigation_id); | 187 void OnNavigationComplete(const NavigationID& navigation_id); |
| 195 void LearnUrlNavigation(const GURL& main_frame_url, | 188 void LearnUrlNavigation(const GURL& main_frame_url, |
| 196 const std::vector<URLRequestSummary>& new_value); | 189 const std::vector<URLRequestSummary>& new_value); |
| 197 void MaybeReportAccuracyStats(const NavigationID& navigation_id) const; | 190 |
| 191 void MaybeReportAccuracyStats(const NavigationID& navigation_id); |
| 192 void MaybeReportSimulatedAccuracyStats( |
| 193 const NavigationID& navigation_id) const; |
| 198 | 194 |
| 199 void SetTablesForTesting( | 195 void SetTablesForTesting( |
| 200 scoped_refptr<ResourcePrefetchPredictorTables> tables); | 196 scoped_refptr<ResourcePrefetchPredictorTables> tables); |
| 201 | 197 |
| 202 Profile* const profile_; | 198 Profile* const profile_; |
| 203 Config config_; | 199 ResourcePrefetchPredictorConfig const config_; |
| 204 InitializationState initialization_state_; | 200 InitializationState initialization_state_; |
| 205 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 201 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 202 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
| 206 content::NotificationRegistrar notification_registrar_; | 203 content::NotificationRegistrar notification_registrar_; |
| 207 | 204 |
| 208 NavigationMap inflight_navigations_; | 205 NavigationMap inflight_navigations_; |
| 209 UrlTableCacheMap url_table_cache_; | 206 UrlTableCacheMap url_table_cache_; |
| 207 ResultsMap results_map_; |
| 208 STLValueDeleter<ResultsMap> results_map_deleter_; |
| 210 | 209 |
| 211 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 210 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
| 212 }; | 211 }; |
| 213 | 212 |
| 214 } // namespace predictors | 213 } // namespace predictors |
| 215 | 214 |
| 216 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 215 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
| OLD | NEW |