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

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

Issue 2440723002: predictors: Make ResourcePrefetchPredictor observable. (Closed)
Patch Set: Rename TestObserver to TestDelegate Created 4 years, 1 month 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
« no previous file with comments | « no previous file | chrome/browser/predictors/resource_prefetch_predictor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 93
94 bool has_validators; 94 bool has_validators;
95 bool always_revalidate; 95 bool always_revalidate;
96 96
97 // Initializes a |URLRequestSummary| from a |URLRequest| response. 97 // Initializes a |URLRequestSummary| from a |URLRequest| response.
98 // Returns true for success. 98 // Returns true for success.
99 static bool SummarizeResponse(const net::URLRequest& request, 99 static bool SummarizeResponse(const net::URLRequest& request,
100 URLRequestSummary* summary); 100 URLRequestSummary* summary);
101 }; 101 };
102 102
103 // Stores the data learned from a single navigation.
104 struct PageRequestSummary {
105 explicit PageRequestSummary(const GURL& main_frame_url);
106 PageRequestSummary(const PageRequestSummary& other);
107 ~PageRequestSummary();
108
109 GURL main_frame_url;
110 GURL initial_url;
111
112 // Stores all subresource requests within a single navigation, from initial
113 // main frame request to navigation completion.
114 std::vector<URLRequestSummary> subresource_requests;
115 };
116
117 // An interface used to notify that data in the resource prefetch predictor
118 // has changed. All methods are invoked on the UI thread.
119 class TestDelegate {
pasko 2016/10/26 12:05:40 can this be moved outside of ResourcePrefetchPredi
pasko 2016/10/26 12:05:41 I realized that this class can be forward-declared
alexilin 2016/10/26 13:09:59 I'll try to advocate existing implementation. For
pasko 2016/10/26 13:48:35 I would argue that this long name is used only in
alexilin 2016/10/26 15:22:01 Done.
120 public:
121 virtual ~TestDelegate();
pasko 2016/10/26 12:05:41 A comment would be worth adding IMO: // De-registe
alexilin 2016/10/26 13:09:59 Done.
122
123 virtual void OnNavigationLearned(size_t url_visit_count,
124 const PageRequestSummary& summary) {}
125
126 protected:
127 // |predictor| must be non-NULL and has to outlive the TestDelegate.
128 // Also the predictor must not have a TestDelegate set.
129 explicit TestDelegate(ResourcePrefetchPredictor* predictor);
130
131 private:
132 ResourcePrefetchPredictor* predictor_;
133
134 DISALLOW_COPY_AND_ASSIGN(TestDelegate);
135 };
136
103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, 137 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config,
104 Profile* profile); 138 Profile* profile);
105 ~ResourcePrefetchPredictor() override; 139 ~ResourcePrefetchPredictor() override;
106 140
107 // Thread safe. 141 // Thread safe.
108 static bool ShouldRecordRequest(net::URLRequest* request, 142 static bool ShouldRecordRequest(net::URLRequest* request,
109 content::ResourceType resource_type); 143 content::ResourceType resource_type);
110 static bool ShouldRecordResponse(net::URLRequest* response); 144 static bool ShouldRecordResponse(net::URLRequest* response);
111 static bool ShouldRecordRedirect(net::URLRequest* response); 145 static bool ShouldRecordRedirect(net::URLRequest* response);
112 146
(...skipping 12 matching lines...) Expand all
125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the 159 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the
126 // predictor of main frame and resource requests. Should only be called if the 160 // predictor of main frame and resource requests. Should only be called if the
127 // corresponding Should* functions return true. 161 // corresponding Should* functions return true.
128 void RecordURLRequest(const URLRequestSummary& request); 162 void RecordURLRequest(const URLRequestSummary& request);
129 void RecordURLResponse(const URLRequestSummary& response); 163 void RecordURLResponse(const URLRequestSummary& response);
130 void RecordURLRedirect(const URLRequestSummary& response); 164 void RecordURLRedirect(const URLRequestSummary& response);
131 165
132 // Called when the main frame of a page completes loading. 166 // Called when the main frame of a page completes loading.
133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 167 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
134 168
169 // Starts prefetching if it is enabled and prefetching data exists for the
170 // |main_frame_url| either at the URL or at the host level.
171 void StartPrefetching(const GURL& main_frame_url);
172
173 // Stops prefetching that may be in progress corresponding to
174 // |main_frame_url|.
175 void StopPrefetching(const GURL& main_frame_url);
176
177 // Sets the |delegate| to be notified when the resource prefetch predictor
178 // data changes. Previously registered delegate will be discarded. Call
179 // this with nullptr parameter to de-register delegate.
180 void SetDelegateForTesting(TestDelegate* delegate);
181
135 private: 182 private:
136 friend class ::PredictorsHandler; 183 friend class ::PredictorsHandler;
137 friend class ResourcePrefetchPredictorTest; 184 friend class ResourcePrefetchPredictorTest;
138 185
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 186 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 187 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
141 LazilyInitializeEmpty); 188 LazilyInitializeEmpty);
142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 189 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
143 LazilyInitializeWithData); 190 LazilyInitializeWithData);
144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 191 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
(...skipping 14 matching lines...) Expand all
159 PopulatePrefetcherRequest); 206 PopulatePrefetcherRequest);
160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); 207 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint);
161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); 208 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData);
162 209
163 enum InitializationState { 210 enum InitializationState {
164 NOT_INITIALIZED = 0, 211 NOT_INITIALIZED = 0,
165 INITIALIZING = 1, 212 INITIALIZING = 1,
166 INITIALIZED = 2 213 INITIALIZED = 2
167 }; 214 };
168 215
169 // Stores information about inflight navigations.
170 struct PageRequestSummary {
171 explicit PageRequestSummary(const GURL& initial_url);
172 ~PageRequestSummary();
173
174 GURL initial_url;
175
176 // Stores all subresources requests within a single navigation, from initial
177 // main frame request to navigation completion.
178 std::vector<URLRequestSummary> subresource_requests;
179 };
180
181 // Used to fetch the visit count for a URL from the History database.
182 class GetUrlVisitCountTask : public history::HistoryDBTask {
183 public:
184 typedef ResourcePrefetchPredictor::URLRequestSummary URLRequestSummary;
185 typedef ResourcePrefetchPredictor::PageRequestSummary PageRequestSummary;
186 typedef base::Callback<void(size_t, // Visit count.
187 const NavigationID&,
188 const PageRequestSummary&)>
189 VisitInfoCallback;
190
191 GetUrlVisitCountTask(const NavigationID& navigation_id,
192 std::unique_ptr<PageRequestSummary> summary,
193 VisitInfoCallback callback);
194
195 bool RunOnDBThread(history::HistoryBackend* backend,
196 history::HistoryDatabase* db) override;
197
198 void DoneRunOnMainThread() override;
199
200 private:
201 ~GetUrlVisitCountTask() override;
202
203 int visit_count_;
204 NavigationID navigation_id_;
205 std::unique_ptr<PageRequestSummary> summary_;
206 VisitInfoCallback callback_;
207
208 DISALLOW_COPY_AND_ASSIGN(GetUrlVisitCountTask);
209 };
210
211 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 216 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; 217 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
213 218
214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> 219 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>>
215 NavigationMap; 220 NavigationMap;
216 221
217 // Returns true if the main page request is supported for prediction. 222 // Returns true if the main page request is supported for prediction.
218 static bool IsHandledMainPage(net::URLRequest* request); 223 static bool IsHandledMainPage(net::URLRequest* request);
219 224
220 // Returns true if the subresource request is supported for prediction. 225 // Returns true if the subresource request is supported for prediction.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // prefetched. 259 // prefetched.
255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); 260 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls);
256 261
257 // Returns true iff the |data_map| contains PrefetchData that can be used 262 // Returns true iff the |data_map| contains PrefetchData that can be used
258 // for a |main_frame_key| and fills |urls| with resources that need to be 263 // for a |main_frame_key| and fills |urls| with resources that need to be
259 // prefetched. 264 // prefetched.
260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, 265 bool PopulatePrefetcherRequest(const std::string& main_frame_key,
261 const PrefetchDataMap& data_map, 266 const PrefetchDataMap& data_map,
262 std::vector<GURL>* urls); 267 std::vector<GURL>* urls);
263 268
264 public:
265 // Starts prefetching if it is enabled and prefetching data exists for the
266 // NavigationID either at the URL or at the host level.
267 void StartPrefetching(const GURL& main_frame_url);
268
269 // Stops prefetching that may be in progress corresponding to |navigation_id|.
270 void StopPrefetching(const GURL& main_frame_url);
271
272 private:
273 // Starts initialization by posting a task to the DB thread to read the 269 // Starts initialization by posting a task to the DB thread to read the
274 // predictor database. 270 // predictor database.
275 void StartInitialization(); 271 void StartInitialization();
276 272
277 // Callback for task to read predictor database. Takes ownership of 273 // Callback for task to read predictor database. Takes ownership of
278 // all arguments. 274 // all arguments.
279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 275 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
280 std::unique_ptr<PrefetchDataMap> host_data_map, 276 std::unique_ptr<PrefetchDataMap> host_data_map,
281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, 277 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); 278 std::unique_ptr<RedirectDataMap> host_redirect_data_map);
283 279
284 // Called during initialization when history is read and the predictor 280 // Called during initialization when history is read and the predictor
285 // database has been read. 281 // database has been read.
286 void OnHistoryAndCacheLoaded(); 282 void OnHistoryAndCacheLoaded();
287 283
288 // Removes data for navigations where the onload never fired. Will cleanup 284 // Removes data for navigations where the onload never fired. Will cleanup
289 // inflight_navigations_. 285 // inflight_navigations_.
290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 286 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
291 287
292 // Deletes all URLs from the predictor database, the caches and removes all 288 // Deletes all URLs from the predictor database, the caches and removes all
293 // inflight navigations. 289 // inflight navigations.
294 void DeleteAllUrls(); 290 void DeleteAllUrls();
295 291
296 // Deletes data for the input |urls| and their corresponding hosts from the 292 // Deletes data for the input |urls| and their corresponding hosts from the
297 // predictor database and caches. 293 // predictor database and caches.
298 void DeleteUrls(const history::URLRows& urls); 294 void DeleteUrls(const history::URLRows& urls);
299 295
300 // Callback for GetUrlVisitCountTask. 296 // Callback for GetUrlVisitCountTask.
301 void OnVisitCountLookup(size_t visit_count, 297 void OnVisitCountLookup(size_t url_visit_count,
302 const NavigationID& navigation_id,
303 const PageRequestSummary& summary); 298 const PageRequestSummary& summary);
304 299
305 // Removes the oldest entry in the input |data_map|, also deleting it from the 300 // Removes the oldest entry in the input |data_map|, also deleting it from the
306 // predictor database. 301 // predictor database.
307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, 302 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
308 PrefetchDataMap* data_map); 303 PrefetchDataMap* data_map);
309 304
310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, 305 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type,
311 RedirectDataMap* data_map); 306 RedirectDataMap* data_map);
312 307
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 // Used to connect to HistoryService or register for service loaded 345 // Used to connect to HistoryService or register for service loaded
351 // notificatioan. 346 // notificatioan.
352 void ConnectToHistoryService(); 347 void ConnectToHistoryService();
353 348
354 // Used for testing to inject mock tables. 349 // Used for testing to inject mock tables.
355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { 350 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) {
356 tables_ = tables; 351 tables_ = tables;
357 } 352 }
358 353
359 Profile* const profile_; 354 Profile* const profile_;
355 TestDelegate* delegate_;
360 ResourcePrefetchPredictorConfig const config_; 356 ResourcePrefetchPredictorConfig const config_;
361 InitializationState initialization_state_; 357 InitializationState initialization_state_;
362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 358 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 359 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
364 base::CancelableTaskTracker history_lookup_consumer_; 360 base::CancelableTaskTracker history_lookup_consumer_;
365 361
366 // Copy of the data in the predictor tables. 362 // Copy of the data in the predictor tables.
367 std::unique_ptr<PrefetchDataMap> url_table_cache_; 363 std::unique_ptr<PrefetchDataMap> url_table_cache_;
368 std::unique_ptr<PrefetchDataMap> host_table_cache_; 364 std::unique_ptr<PrefetchDataMap> host_table_cache_;
369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; 365 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_;
370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; 366 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_;
371 367
372 NavigationMap inflight_navigations_; 368 NavigationMap inflight_navigations_;
373 369
374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 370 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
375 history_service_observer_; 371 history_service_observer_;
376 372
377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); 373 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
378 }; 374 };
379 375
380 } // namespace predictors 376 } // namespace predictors
381 377
382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 378 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/predictors/resource_prefetch_predictor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698