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