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_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
19 #include "chrome/browser/predictors/resource_prefetcher_manager.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 { |
(...skipping 16 matching lines...) Expand all Loading... | |
45 // | 46 // |
46 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and | 47 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and |
47 // redirects on the IO thread(via RDHostDelegate) and post tasks to the | 48 // redirects on the IO thread(via RDHostDelegate) and post tasks to the |
48 // ResourcePrefetchPredictor on the UI thread. This is owned by the | 49 // ResourcePrefetchPredictor on the UI thread. This is owned by the |
49 // ProfileIOData for the profile. | 50 // ProfileIOData for the profile. |
50 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data | 51 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data |
51 // to a sql database. Runs entirely on the DB thread. Owned by the | 52 // to a sql database. Runs entirely on the DB thread. Owned by the |
52 // PredictorDatabase. | 53 // PredictorDatabase. |
53 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in | 54 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in |
54 // the UI thread through the ResourcePrefetchPredictorObserver and perisists | 55 // the UI thread through the ResourcePrefetchPredictorObserver and perisists |
55 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. | 56 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It |
56 // Owned by profile. | 57 // initiates resource prefetching using the ResourcePrefetcherManager. Owned |
58 // by profile. | |
59 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the | |
60 // prefetching on the IO thread. The manager is owned by the | |
61 // ResourcePrefetchPredictor and interfaces between the predictor on the UI | |
62 // thread and the prefetchers on the IO thread. | |
63 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | |
64 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | |
57 // | 65 // |
58 // TODO(shishir): Implement the prefetching of resources. | |
59 // TODO(shishir): Do speculative prefetching for https resources and/or https | 66 // TODO(shishir): Do speculative prefetching for https resources and/or https |
60 // main_frame urls. | 67 // main_frame urls. |
61 class ResourcePrefetchPredictor | 68 class ResourcePrefetchPredictor |
62 : public ProfileKeyedService, | 69 : public ProfileKeyedService, |
63 public content::NotificationObserver, | 70 public content::NotificationObserver, |
71 public ResourcePrefetcherManager::Delegate, | |
dominich
2012/07/23 16:04:41
do you really need the delegate pattern here? Cons
Shishir
2012/08/01 22:35:24
Done.
| |
64 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { | 72 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { |
65 public: | 73 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. | 74 // Stores the data that we need to get from the URLRequest. |
90 struct URLRequestSummary { | 75 struct URLRequestSummary { |
91 URLRequestSummary(); | 76 URLRequestSummary(); |
92 URLRequestSummary(const URLRequestSummary& other); | 77 URLRequestSummary(const URLRequestSummary& other); |
93 ~URLRequestSummary(); | 78 ~URLRequestSummary(); |
94 | 79 |
95 NavigationID navigation_id; | 80 NavigationID navigation_id; |
96 GURL resource_url; | 81 GURL resource_url; |
97 ResourceType::Type resource_type; | 82 ResourceType::Type resource_type; |
98 | 83 |
99 // Only for responses. | 84 // Only for responses. |
100 std::string mime_type; | 85 std::string mime_type; |
101 bool was_cached; | 86 bool was_cached; |
102 }; | 87 }; |
103 | 88 |
104 ResourcePrefetchPredictor(const Config& config, Profile* profile); | 89 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, |
90 Profile* profile); | |
105 virtual ~ResourcePrefetchPredictor(); | 91 virtual ~ResourcePrefetchPredictor(); |
106 | 92 |
107 // Thread safe. | 93 // Thread safe. |
108 static bool IsEnabled(Profile* profile); | |
109 static bool ShouldRecordRequest(net::URLRequest* request, | 94 static bool ShouldRecordRequest(net::URLRequest* request, |
110 ResourceType::Type resource_type); | 95 ResourceType::Type resource_type); |
111 static bool ShouldRecordResponse(net::URLRequest* response); | 96 static bool ShouldRecordResponse(net::URLRequest* response); |
112 static bool ShouldRecordRedirect(net::URLRequest* response); | 97 static bool ShouldRecordRedirect(net::URLRequest* response); |
113 | 98 |
114 static ResourceType::Type GetResourceTypeFromMimeType( | 99 static ResourceType::Type GetResourceTypeFromMimeType( |
115 const std::string& mime_type, | 100 const std::string& mime_type, |
116 ResourceType::Type fallback); | 101 ResourceType::Type fallback); |
117 | 102 |
118 void RecordURLRequest(const URLRequestSummary& request); | 103 void RecordURLRequest(const URLRequestSummary& request); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 struct UrlTableCacheValue { | 137 struct UrlTableCacheValue { |
153 UrlTableCacheValue(); | 138 UrlTableCacheValue(); |
154 ~UrlTableCacheValue(); | 139 ~UrlTableCacheValue(); |
155 | 140 |
156 UrlTableRowVector rows; | 141 UrlTableRowVector rows; |
157 base::Time last_visit; | 142 base::Time last_visit; |
158 }; | 143 }; |
159 | 144 |
160 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; | 145 typedef std::map<NavigationID, std::vector<URLRequestSummary> > NavigationMap; |
161 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; | 146 typedef std::map<GURL, UrlTableCacheValue> UrlTableCacheMap; |
147 typedef std::map<NavigationID, linked_ptr<ResourcePrefetcher::RequestVector> > | |
dominich
2012/07/23 16:04:41
red flag: linked_ptr is unlikely to be what you wa
Shishir
2012/08/01 22:35:24
Changed to plain pointers.
| |
148 ResultsMap; | |
162 | 149 |
163 // content::NotificationObserver methods OVERRIDE. | 150 // content::NotificationObserver methods OVERRIDE. |
164 virtual void Observe(int type, | 151 virtual void Observe(int type, |
165 const content::NotificationSource& source, | 152 const content::NotificationSource& source, |
166 const content::NotificationDetails& details) OVERRIDE; | 153 const content::NotificationDetails& details) OVERRIDE; |
167 | 154 |
155 // ResourcePrefetcherManager::Delegate methods OVERRIDE. | |
156 virtual void FinishedPrefetchForNavigation( | |
157 const NavigationID& navigation_id, | |
158 scoped_ptr<ResourcePrefetcher::RequestVector> requests) OVERRIDE; | |
159 | |
168 static bool IsHandledMainPage(net::URLRequest* request); | 160 static bool IsHandledMainPage(net::URLRequest* request); |
169 static bool IsHandledSubresource(net::URLRequest* response); | 161 static bool IsHandledSubresource(net::URLRequest* response); |
170 static bool IsCacheable(const net::URLRequest* response); | 162 static bool IsCacheable(const net::URLRequest* response); |
171 | 163 |
172 // Deal with different kinds of requests. | 164 // Deal with different kinds of requests. |
173 void OnMainFrameRequest(const URLRequestSummary& request); | 165 void OnMainFrameRequest(const URLRequestSummary& request); |
174 void OnMainFrameResponse(const URLRequestSummary& response); | 166 void OnMainFrameResponse(const URLRequestSummary& response); |
175 void OnMainFrameRedirect(const URLRequestSummary& response); | 167 void OnMainFrameRedirect(const URLRequestSummary& response); |
176 void OnSubresourceResponse(const URLRequestSummary& response); | 168 void OnSubresourceResponse(const URLRequestSummary& response); |
177 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, | 169 void OnSubresourceLoadedFromMemory(const NavigationID& navigation_id, |
178 const GURL& resource_url, | 170 const GURL& resource_url, |
179 const std::string& mime_type, | 171 const std::string& mime_type, |
180 ResourceType::Type resource_type); | 172 ResourceType::Type resource_type); |
181 | 173 |
182 // Initialization code. | 174 // Initialization code. |
183 void LazilyInitialize(); | 175 void LazilyInitialize(); |
184 void OnHistoryAndCacheLoaded(); | 176 void OnHistoryAndCacheLoaded(); |
185 void CreateCaches(std::vector<UrlTableRow>* url_rows); | 177 void CreateCaches(std::vector<UrlTableRow>* url_rows); |
186 | 178 |
187 // Database and cache cleanup code. | 179 // Database and cache cleanup code. |
188 void RemoveAnEntryFromUrlDB(); | 180 void RemoveAnEntryFromUrlDB(); |
189 void DeleteAllUrls(); | 181 void DeleteAllUrls(); |
190 void DeleteUrls(const history::URLRows& urls); | 182 void DeleteUrls(const history::URLRows& urls); |
191 | 183 |
192 bool ShouldTrackUrl(const GURL& url); | 184 bool ShouldTrackUrl(const GURL& url); |
193 void CleanupAbandonedNavigations(const NavigationID& navigation_id); | 185 void CleanupAbandonedNavigations(const NavigationID& navigation_id); |
194 void OnNavigationComplete(const NavigationID& navigation_id); | 186 void OnNavigationComplete(const NavigationID& navigation_id); |
195 void LearnUrlNavigation(const GURL& main_frame_url, | 187 void LearnUrlNavigation(const GURL& main_frame_url, |
196 const std::vector<URLRequestSummary>& new_value); | 188 const std::vector<URLRequestSummary>& new_value); |
197 void MaybeReportAccuracyStats(const NavigationID& navigation_id) const; | 189 |
190 void MaybeReportAccuracyStats(const NavigationID& navigation_id); | |
191 void MaybeReportSimulatedAccuracyStats( | |
192 const NavigationID& navigation_id) const; | |
198 | 193 |
199 void SetTablesForTesting( | 194 void SetTablesForTesting( |
200 scoped_refptr<ResourcePrefetchPredictorTables> tables); | 195 scoped_refptr<ResourcePrefetchPredictorTables> tables); |
201 | 196 |
202 Profile* const profile_; | 197 Profile* const profile_; |
203 Config config_; | 198 ResourcePrefetchPredictorConfig config_; |
dominich
2012/07/23 16:04:41
const?
Shishir
2012/08/01 22:35:24
Done.
| |
204 InitializationState initialization_state_; | 199 InitializationState initialization_state_; |
205 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 200 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
201 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | |
dominich
2012/07/23 16:04:41
Why is this a refptr? I'm surprised there can be m
Shishir
2012/08/01 22:35:24
Needs to be refcounted because it needs to be acce
| |
206 content::NotificationRegistrar notification_registrar_; | 202 content::NotificationRegistrar notification_registrar_; |
207 | 203 |
208 NavigationMap inflight_navigations_; | 204 NavigationMap inflight_navigations_; |
209 UrlTableCacheMap url_table_cache_; | 205 UrlTableCacheMap url_table_cache_; |
206 ResultsMap results_map_; | |
210 | 207 |
211 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 208 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
212 }; | 209 }; |
213 | 210 |
214 } // namespace predictors | 211 } // namespace predictors |
215 | 212 |
216 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 213 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
OLD | NEW |