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

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

Issue 2365613002: predictors: Refactor the prefetcher in resource_prefetch_predictor. (Closed)
Patch Set: . Created 4 years, 3 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>
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the 124 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the
125 // predictor of main frame and resource requests. Should only be called if the 125 // predictor of main frame and resource requests. Should only be called if the
126 // corresponding Should* functions return true. 126 // corresponding Should* functions return true.
127 void RecordURLRequest(const URLRequestSummary& request); 127 void RecordURLRequest(const URLRequestSummary& request);
128 void RecordURLResponse(const URLRequestSummary& response); 128 void RecordURLResponse(const URLRequestSummary& response);
129 void RecordURLRedirect(const URLRequestSummary& response); 129 void RecordURLRedirect(const URLRequestSummary& response);
130 130
131 // Called when the main frame of a page completes loading. 131 // Called when the main frame of a page completes loading.
132 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 132 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
133 133
134 // Called by ResourcePrefetcherManager to notify that prefetching has finished
135 // for a navigation. Should take ownership of |requests|.
136 virtual void FinishedPrefetchForNavigation(
137 const NavigationID& navigation_id,
138 PrefetchKeyType key_type,
139 ResourcePrefetcher::RequestVector* requests);
140
141 private: 134 private:
142 friend class ::PredictorsHandler; 135 friend class ::PredictorsHandler;
143 friend class ResourcePrefetchPredictorTest; 136 friend class ResourcePrefetchPredictorTest;
144 137
145 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 138 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
147 LazilyInitializeEmpty); 140 LazilyInitializeEmpty);
148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
149 LazilyInitializeWithData); 142 LazilyInitializeWithData);
150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 143 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
151 NavigationNotRecorded); 144 NavigationNotRecorded);
152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); 145 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB);
153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); 146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB);
154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
155 NavigationUrlNotInDBAndDBFull); 148 NavigationUrlNotInDBAndDBFull);
156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); 149 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest);
157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); 150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect);
158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
159 OnSubresourceResponse); 152 OnSubresourceResponse);
160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); 153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT);
161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); 154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes);
162 155
163 enum InitializationState { 156 enum InitializationState {
164 NOT_INITIALIZED = 0, 157 NOT_INITIALIZED = 0,
165 INITIALIZING = 1, 158 INITIALIZING = 1,
166 INITIALIZED = 2 159 INITIALIZED = 2
167 }; 160 };
168 161
169 // Stores prefetching results.
170 struct Result {
171 // Takes ownership of requests.
172 Result(PrefetchKeyType key_type,
173 ResourcePrefetcher::RequestVector* requests);
174 ~Result();
175
176 PrefetchKeyType key_type;
177 std::unique_ptr<ResourcePrefetcher::RequestVector> requests;
178
179 private:
180 DISALLOW_COPY_AND_ASSIGN(Result);
181 };
182
183 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; 162 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
184 typedef ResourcePrefetchPredictorTables::ResourceRows ResourceRows; 163 typedef ResourcePrefetchPredictorTables::ResourceRows ResourceRows;
185 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; 164 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
186 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 165 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
187 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > > 166 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > >
188 NavigationMap; 167 NavigationMap;
189 typedef std::map<NavigationID, std::unique_ptr<Result>> ResultsMap;
190 168
191 // Returns true if the main page request is supported for prediction. 169 // Returns true if the main page request is supported for prediction.
192 static bool IsHandledMainPage(net::URLRequest* request); 170 static bool IsHandledMainPage(net::URLRequest* request);
193 171
194 // Returns true if the subresource request is supported for prediction. 172 // Returns true if the subresource request is supported for prediction.
195 static bool IsHandledSubresource(net::URLRequest* request, 173 static bool IsHandledSubresource(net::URLRequest* request,
196 content::ResourceType resource_type); 174 content::ResourceType resource_type);
197 175
198 // Returns true if the subresource has a supported type. 176 // Returns true if the subresource has a supported type.
199 static bool IsHandledResourceType(content::ResourceType resource_type, 177 static bool IsHandledResourceType(content::ResourceType resource_type,
(...skipping 14 matching lines...) Expand all
214 192
215 // Called when onload completes for a navigation. We treat this point as the 193 // Called when onload completes for a navigation. We treat this point as the
216 // "completion" of the navigation. The resources requested by the page up to 194 // "completion" of the navigation. The resources requested by the page up to
217 // this point are the only ones considered for prefetching. 195 // this point are the only ones considered for prefetching.
218 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); 196 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info);
219 197
220 // Returns true if there is PrefetchData that can be used for the 198 // Returns true if there is PrefetchData that can be used for the
221 // navigation and fills in the |prefetch_data| to resources that need to be 199 // navigation and fills in the |prefetch_data| to resources that need to be
222 // prefetched. 200 // prefetched.
223 bool GetPrefetchData(const NavigationID& navigation_id, 201 bool GetPrefetchData(const NavigationID& navigation_id,
224 ResourcePrefetcher::RequestVector* prefetch_requests, 202 std::vector<GURL>* urls,
225 PrefetchKeyType* key_type); 203 PrefetchKeyType* key_type);
226 204
227 // Converts a PrefetchData into a ResourcePrefetcher::RequestVector. 205 // Converts a PrefetchData into a list of URLs.
228 void PopulatePrefetcherRequest(const PrefetchData& data, 206 void PopulatePrefetcherRequest(const PrefetchData& data,
229 ResourcePrefetcher::RequestVector* requests); 207 std::vector<GURL>* urls);
230 208
231 // Starts prefetching if it is enabled and prefetching data exists for the 209 // Starts prefetching if it is enabled and prefetching data exists for the
232 // NavigationID either at the URL or at the host level. 210 // NavigationID either at the URL or at the host level.
233 void StartPrefetching(const NavigationID& navigation_id); 211 void StartPrefetching(const NavigationID& navigation_id);
234 212
235 // Stops prefetching that may be in progress corresponding to |navigation_id|. 213 // Stops prefetching that may be in progress corresponding to |navigation_id|.
236 void StopPrefetching(const NavigationID& navigation_id); 214 void StopPrefetching(const NavigationID& navigation_id);
237 215
238 // Starts initialization by posting a task to the DB thread to read the 216 // Starts initialization by posting a task to the DB thread to read the
239 // predictor database. 217 // predictor database.
240 void StartInitialization(); 218 void StartInitialization();
241 219
242 // Callback for task to read predictor database. Takes ownership of 220 // Callback for task to read predictor database. Takes ownership of
243 // |url_data_map| and |host_data_map|. 221 // |url_data_map| and |host_data_map|.
244 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 222 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
245 std::unique_ptr<PrefetchDataMap> host_data_map); 223 std::unique_ptr<PrefetchDataMap> host_data_map);
246 224
247 // Called during initialization when history is read and the predictor 225 // Called during initialization when history is read and the predictor
248 // database has been read. 226 // database has been read.
249 void OnHistoryAndCacheLoaded(); 227 void OnHistoryAndCacheLoaded();
250 228
251 // Removes data for navigations where the onload never fired. Will cleanup 229 // Removes data for navigations where the onload never fired. Will cleanup
252 // inflight_navigations_ and results_map_. 230 // inflight_navigations_.
253 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 231 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
254 232
255 // Deletes all URLs from the predictor database, the caches and removes all 233 // Deletes all URLs from the predictor database, the caches and removes all
256 // inflight navigations. 234 // inflight navigations.
257 void DeleteAllUrls(); 235 void DeleteAllUrls();
258 236
259 // Deletes data for the input |urls| and their corresponding hosts from the 237 // Deletes data for the input |urls| and their corresponding hosts from the
260 // predictor database and caches. 238 // predictor database and caches.
261 void DeleteUrls(const history::URLRows& urls); 239 void DeleteUrls(const history::URLRows& urls);
262 240
(...skipping 18 matching lines...) Expand all
281 // Reports overall page load time. 259 // Reports overall page load time.
282 void ReportPageLoadTimeStats(base::TimeDelta plt) const; 260 void ReportPageLoadTimeStats(base::TimeDelta plt) const;
283 261
284 // Reports page load time for prefetched and not prefetched pages 262 // Reports page load time for prefetched and not prefetched pages
285 void ReportPageLoadTimePrefetchStats( 263 void ReportPageLoadTimePrefetchStats(
286 base::TimeDelta plt, 264 base::TimeDelta plt,
287 bool prefetched, 265 bool prefetched,
288 base::Callback<void(int)> report_network_type_callback, 266 base::Callback<void(int)> report_network_type_callback,
289 PrefetchKeyType key_type) const; 267 PrefetchKeyType key_type) const;
290 268
291 // Reports accuracy by comparing prefetched resources with resources that are
292 // actually used by the page.
293 void ReportAccuracyStats(PrefetchKeyType key_type,
294 const std::vector<URLRequestSummary>& actual,
295 ResourcePrefetcher::RequestVector* prefetched) const;
296
297 // Reports predicted accuracy i.e. by comparing resources that are actually
298 // used by the page with those that may have been prefetched.
299 void ReportPredictedAccuracyStats(
300 PrefetchKeyType key_type,
301 const std::vector<URLRequestSummary>& actual,
302 const ResourcePrefetcher::RequestVector& predicted) const;
303 void ReportPredictedAccuracyStatsHelper(
304 PrefetchKeyType key_type,
305 const ResourcePrefetcher::RequestVector& predicted,
306 const std::map<GURL, bool>& actual,
307 size_t total_resources_fetched_from_network,
308 size_t max_assumed_prefetched) const;
309
310 // history::HistoryServiceObserver: 269 // history::HistoryServiceObserver:
311 void OnURLsDeleted(history::HistoryService* history_service, 270 void OnURLsDeleted(history::HistoryService* history_service,
312 bool all_history, 271 bool all_history,
313 bool expired, 272 bool expired,
314 const history::URLRows& deleted_rows, 273 const history::URLRows& deleted_rows,
315 const std::set<GURL>& favicon_urls) override; 274 const std::set<GURL>& favicon_urls) override;
316 void OnHistoryServiceLoaded( 275 void OnHistoryServiceLoaded(
317 history::HistoryService* history_service) override; 276 history::HistoryService* history_service) override;
318 277
319 // Used to connect to HistoryService or register for service loaded 278 // Used to connect to HistoryService or register for service loaded
(...skipping 12 matching lines...) Expand all
332 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 291 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
333 base::CancelableTaskTracker history_lookup_consumer_; 292 base::CancelableTaskTracker history_lookup_consumer_;
334 293
335 // Map of all the navigations in flight to their resource requests. 294 // Map of all the navigations in flight to their resource requests.
336 NavigationMap inflight_navigations_; 295 NavigationMap inflight_navigations_;
337 296
338 // Copy of the data in the predictor tables. 297 // Copy of the data in the predictor tables.
339 std::unique_ptr<PrefetchDataMap> url_table_cache_; 298 std::unique_ptr<PrefetchDataMap> url_table_cache_;
340 std::unique_ptr<PrefetchDataMap> host_table_cache_; 299 std::unique_ptr<PrefetchDataMap> host_table_cache_;
341 300
342 ResultsMap results_map_;
343
344 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 301 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
345 history_service_observer_; 302 history_service_observer_;
346 303
347 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); 304 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
348 }; 305 };
349 306
350 } // namespace predictors 307 } // namespace predictors
351 308
352 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 309 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698