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 }; | |
86 | |
87 // Stores the data that we need to get from the URLRequest. | 75 // Stores the data that we need to get from the URLRequest. |
88 struct URLRequestSummary { | 76 struct URLRequestSummary { |
89 URLRequestSummary(); | 77 URLRequestSummary(); |
90 URLRequestSummary(const URLRequestSummary& other); | 78 URLRequestSummary(const URLRequestSummary& other); |
91 ~URLRequestSummary(); | 79 ~URLRequestSummary(); |
92 | 80 |
93 NavigationID navigation_id; | 81 NavigationID navigation_id; |
94 GURL resource_url; | 82 GURL resource_url; |
95 ResourceType::Type resource_type; | 83 ResourceType::Type resource_type; |
96 | 84 |
97 // Only for responses. | 85 // Only for responses. |
98 std::string mime_type; | 86 std::string mime_type; |
99 bool was_cached; | 87 bool was_cached; |
100 GURL redirect_url; // Empty unless request was redirected to a valid url. | 88 GURL redirect_url; // Empty unless request was redirected to a valid url. |
101 }; | 89 }; |
102 | 90 |
103 ResourcePrefetchPredictor(const Config& config, Profile* profile); | 91 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
| 92 Profile* profile); |
104 virtual ~ResourcePrefetchPredictor(); | 93 virtual ~ResourcePrefetchPredictor(); |
105 | 94 |
106 // Thread safe. | 95 // Thread safe. |
107 static bool IsEnabled(Profile* profile); | |
108 static bool ShouldRecordRequest(net::URLRequest* request, | 96 static bool ShouldRecordRequest(net::URLRequest* request, |
109 ResourceType::Type resource_type); | 97 ResourceType::Type resource_type); |
110 static bool ShouldRecordResponse(net::URLRequest* response); | 98 static bool ShouldRecordResponse(net::URLRequest* response); |
111 static bool ShouldRecordRedirect(net::URLRequest* response); | 99 static bool ShouldRecordRedirect(net::URLRequest* response); |
112 | 100 |
113 static ResourceType::Type GetResourceTypeFromMimeType( | 101 static ResourceType::Type GetResourceTypeFromMimeType( |
114 const std::string& mime_type, | 102 const std::string& mime_type, |
115 ResourceType::Type fallback); | 103 ResourceType::Type fallback); |
116 | 104 |
117 void RecordURLRequest(const URLRequestSummary& request); | 105 void RecordURLRequest(const URLRequestSummary& request); |
118 void RecordUrlResponse(const URLRequestSummary& response); | 106 void RecordUrlResponse(const URLRequestSummary& response); |
119 void RecordUrlRedirect(const URLRequestSummary& response); | 107 void RecordUrlRedirect(const URLRequestSummary& response); |
120 | 108 |
| 109 // Called by ResourcePrefetcherManager to notify that prefetching has finished |
| 110 // for a navigation. Should take ownership of |requests|. |
| 111 virtual void FinishedPrefetchForNavigation( |
| 112 const NavigationID& navigation_id, |
| 113 ResourcePrefetcher::RequestVector* requests); |
| 114 |
121 private: | 115 private: |
122 friend class ::PredictorsHandler; | 116 friend class ::PredictorsHandler; |
123 friend class ResourcePrefetchPredictorTest; | 117 friend class ResourcePrefetchPredictorTest; |
124 | 118 |
125 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 119 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
126 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 120 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
127 LazilyInitializeEmpty); | 121 LazilyInitializeEmpty); |
128 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 122 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
129 LazilyInitializeWithData); | 123 LazilyInitializeWithData); |
130 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 124 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
(...skipping 20 matching lines...) Expand all Loading... |
151 struct UrlTableCacheValue { | 145 struct UrlTableCacheValue { |
152 UrlTableCacheValue(); | 146 UrlTableCacheValue(); |
153 ~UrlTableCacheValue(); | 147 ~UrlTableCacheValue(); |
154 | 148 |
155 UrlTableRowVector rows; | 149 UrlTableRowVector rows; |
156 base::Time last_visit; | 150 base::Time last_visit; |
157 }; | 151 }; |
158 | 152 |
159 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; | 153 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; |
160 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; | 154 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; |
| 155 typedef std::map<NavigationID, ResourcePrefetcher::RequestVector*> ResultsMap; |
161 | 156 |
162 // content::NotificationObserver methods OVERRIDE. | 157 // content::NotificationObserver methods OVERRIDE. |
163 virtual void Observe(int type, | 158 virtual void Observe(int type, |
164 const content::NotificationSource& source, | 159 const content::NotificationSource& source, |
165 const content::NotificationDetails& details) OVERRIDE; | 160 const content::NotificationDetails& details) OVERRIDE; |
166 | 161 |
| 162 // ProfileKeyedService methods OVERRIDE. |
| 163 virtual void Shutdown() OVERRIDE; |
| 164 |
167 static bool IsHandledMainPage(net::URLRequest* request); | 165 static bool IsHandledMainPage(net::URLRequest* request); |
168 static bool IsHandledSubresource(net::URLRequest* response); | 166 static bool IsHandledSubresource(net::URLRequest* response); |
169 static bool IsCacheable(const net::URLRequest* response); | 167 static bool IsCacheable(const net::URLRequest* response); |
170 | 168 |
171 // Deal with different kinds of requests. | 169 // Deal with different kinds of requests. |
172 void OnMainFrameRequest(const URLRequestSummary& request); | 170 void OnMainFrameRequest(const URLRequestSummary& request); |
173 void OnMainFrameResponse(const URLRequestSummary& response); | 171 void OnMainFrameResponse(const URLRequestSummary& response); |
174 void OnMainFrameRedirect(const URLRequestSummary& response); | 172 void OnMainFrameRedirect(const URLRequestSummary& response); |
175 void OnSubresourceResponse(const URLRequestSummary& response); | 173 void OnSubresourceResponse(const URLRequestSummary& response); |
176 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, | 174 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, |
177 const GURL& resource_url, | 175 const GURL& resource_url, |
178 const std::string& mime_type, | 176 const std::string& mime_type, |
179 ResourceType::Type resource_type); | 177 ResourceType::Type resource_type); |
180 | 178 |
181 // Initialization code. | 179 // Initialization code. |
182 void LazilyInitialize(); | 180 void LazilyInitialize(); |
183 void OnHistoryAndCacheLoaded(); | 181 void OnHistoryAndCacheLoaded(); |
184 void CreateCaches(std::vector<UrlTableRow>* url_rows); | 182 void CreateCaches(std::vector<UrlTableRow>* url_rows); |
185 | 183 |
186 // Database and cache cleanup code. | 184 // Database and cache cleanup code. |
187 void RemoveAnEntryFromUrlDB(); | 185 void RemoveAnEntryFromUrlDB(); |
188 void DeleteAllUrls(); | 186 void DeleteAllUrls(); |
189 void DeleteUrls(const history::URLRows& urls); | 187 void DeleteUrls(const history::URLRows& urls); |
190 | 188 |
191 bool ShouldTrackUrl(const GURL& url); | 189 bool ShouldTrackUrl(const GURL& url); |
192 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 190 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
193 void OnNavigationComplete(const NavigationID& navigation_id); | 191 void OnNavigationComplete(const NavigationID& navigation_id); |
194 void LearnUrlNavigation(const GURL& main_frame_url, | 192 void LearnUrlNavigation(const GURL& main_frame_url, |
195 const std::vector<URLRequestSummary>& new_value); | 193 const std::vector<URLRequestSummary>& new_value); |
196 void MaybeReportAccuracyStats(const NavigationID& navigation_id) const; | 194 void MaybeReportAccuracyStats(const NavigationID& navigation_id); |
| 195 void MaybeReportSimulatedAccuracyStats( |
| 196 const NavigationID& navigation_id) const; |
197 void ReportAccuracyHistograms(const UrlTableRowVector& predicted, | 197 void ReportAccuracyHistograms(const UrlTableRowVector& predicted, |
198 const std::map<GURL, bool>& actual_resources, | 198 const std::map<GURL, bool>& actual_resources, |
199 int total_resources_fetched_from_network, | 199 int total_resources_fetched_from_network, |
200 int max_assumed_prefetched) const; | 200 int max_assumed_prefetched) const; |
201 | 201 |
202 void SetTablesForTesting( | 202 void SetTablesForTesting( |
203 scoped_refptr<ResourcePrefetchPredictorTables> tables); | 203 scoped_refptr<ResourcePrefetchPredictorTables> tables); |
204 | 204 |
205 Profile* const profile_; | 205 Profile* const profile_; |
206 Config config_; | 206 ResourcePrefetchPredictorConfig const config_; |
207 InitializationState initialization_state_; | 207 InitializationState initialization_state_; |
208 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 208 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 209 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
209 content::NotificationRegistrar notification_registrar_; | 210 content::NotificationRegistrar notification_registrar_; |
210 | 211 |
211 NavigationMap inflight_navigations_; | 212 NavigationMap inflight_navigations_; |
212 UrlTableCacheMap url_table_cache_; | 213 UrlTableCacheMap url_table_cache_; |
| 214 ResultsMap results_map_; |
| 215 STLValueDeleter<ResultsMap> results_map_deleter_; |
213 | 216 |
214 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 217 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
215 }; | 218 }; |
216 | 219 |
217 } // namespace predictors | 220 } // namespace predictors |
218 | 221 |
219 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 222 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
OLD | NEW |