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

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

Issue 2440723002: predictors: Make ResourcePrefetchPredictor observable. (Closed)
Patch Set: Fix a mess with access modifiers. Created 4 years, 2 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>
11 #include <memory> 11 #include <memory>
12 #include <set> 12 #include <set>
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/gtest_prod_util.h" 16 #include "base/gtest_prod_util.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/weak_ptr.h" 18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
19 #include "base/scoped_observer.h" 20 #include "base/scoped_observer.h"
20 #include "base/task/cancelable_task_tracker.h" 21 #include "base/task/cancelable_task_tracker.h"
21 #include "base/time/time.h" 22 #include "base/time/time.h"
22 #include "chrome/browser/predictors/resource_prefetch_common.h" 23 #include "chrome/browser/predictors/resource_prefetch_common.h"
23 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 24 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
24 #include "chrome/browser/predictors/resource_prefetcher.h" 25 #include "chrome/browser/predictors/resource_prefetcher.h"
25 #include "components/history/core/browser/history_db_task.h" 26 #include "components/history/core/browser/history_db_task.h"
26 #include "components/history/core/browser/history_service_observer.h" 27 #include "components/history/core/browser/history_service_observer.h"
27 #include "components/history/core/browser/history_types.h" 28 #include "components/history/core/browser/history_types.h"
28 #include "components/keyed_service/core/keyed_service.h" 29 #include "components/keyed_service/core/keyed_service.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 94
94 bool has_validators; 95 bool has_validators;
95 bool always_revalidate; 96 bool always_revalidate;
96 97
97 // Initializes a |URLRequestSummary| from a |URLRequest| response. 98 // Initializes a |URLRequestSummary| from a |URLRequest| response.
98 // Returns true for success. 99 // Returns true for success.
99 static bool SummarizeResponse(const net::URLRequest& request, 100 static bool SummarizeResponse(const net::URLRequest& request,
100 URLRequestSummary* summary); 101 URLRequestSummary* summary);
101 }; 102 };
102 103
104 // Stores information about inflight navigation.
105 struct PageRequestSummary {
pasko 2016/10/21 12:12:53 What is the value of having PageRequestSummary in
alexilin 2016/10/21 13:38:04 We could extract the URLRequestSummary class from
pasko 2016/10/21 14:41:35 Frankly I do not really understand the purpose of
alexilin 2016/10/21 16:04:18 Ok, it turned out that I can't take PageRequestSum
pasko 2016/10/21 16:47:34 Ah, right. OK.
106 explicit PageRequestSummary(const GURL& main_frame_url);
107 ~PageRequestSummary();
108
109 GURL main_frame_url;
110 GURL initial_url;
111
112 // Stores all subresources requests within a single navigation, from initial
pasko 2016/10/21 12:12:52 nit: s/subresources/subresource/ (I know, I know,
alexilin 2016/10/21 13:38:04 Done.
113 // main frame request to navigation completion.
114 std::vector<URLRequestSummary> subresource_requests;
115 };
116
117 // An interface used to notify clients (observers) of this object that data in
118 // the resource prefetch predictor has changed. Register the observer via
119 // ResourcePrefetchPredictor::AddObserver.
120 class Observer {
121 public:
122 virtual void OnNavigationLearned(size_t url_visit_count,
123 const PageRequestSummary& summary) {}
124
125 protected:
126 virtual ~Observer() {}
127 };
pasko 2016/10/21 12:12:52 private: DISALLOW_COPY_AND_ASSIGN(Observer);
alexilin 2016/10/21 13:38:04 Done.
128
103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, 129 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config,
104 Profile* profile); 130 Profile* profile);
105 ~ResourcePrefetchPredictor() override; 131 ~ResourcePrefetchPredictor() override;
106 132
107 // Thread safe. 133 // Thread safe.
108 static bool ShouldRecordRequest(net::URLRequest* request, 134 static bool ShouldRecordRequest(net::URLRequest* request,
109 content::ResourceType resource_type); 135 content::ResourceType resource_type);
110 static bool ShouldRecordResponse(net::URLRequest* response); 136 static bool ShouldRecordResponse(net::URLRequest* response);
111 static bool ShouldRecordRedirect(net::URLRequest* response); 137 static bool ShouldRecordRedirect(net::URLRequest* response);
112 138
(...skipping 12 matching lines...) Expand all
125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the 151 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the
126 // predictor of main frame and resource requests. Should only be called if the 152 // predictor of main frame and resource requests. Should only be called if the
127 // corresponding Should* functions return true. 153 // corresponding Should* functions return true.
128 void RecordURLRequest(const URLRequestSummary& request); 154 void RecordURLRequest(const URLRequestSummary& request);
129 void RecordURLResponse(const URLRequestSummary& response); 155 void RecordURLResponse(const URLRequestSummary& response);
130 void RecordURLRedirect(const URLRequestSummary& response); 156 void RecordURLRedirect(const URLRequestSummary& response);
131 157
132 // Called when the main frame of a page completes loading. 158 // Called when the main frame of a page completes loading.
133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 159 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
134 160
161 // Starts prefetching if it is enabled and prefetching data exists for the
162 // NavigationID either at the URL or at the host level.
163 void StartPrefetching(const GURL& main_frame_url);
pasko 2016/10/21 12:12:52 thanks for making only one public section, looks c
alexilin 2016/10/21 13:38:04 Acknowledged.
164
165 // Stops prefetching that may be in progress corresponding to |navigation_id|.
166 void StopPrefetching(const GURL& main_frame_url);
167
168 // Adds an |observer| to be notified when the resource prefetch predictor data
169 // changes.
170 void AddObserver(Observer* observer);
171
172 // Removes |observer| from the observer list.
173 void RemoveObserver(Observer* observer);
174
135 private: 175 private:
136 friend class ::PredictorsHandler; 176 friend class ::PredictorsHandler;
137 friend class ResourcePrefetchPredictorTest; 177 friend class ResourcePrefetchPredictorTest;
138 178
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 179 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 180 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
141 LazilyInitializeEmpty); 181 LazilyInitializeEmpty);
142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 182 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
143 LazilyInitializeWithData); 183 LazilyInitializeWithData);
144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 184 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
(...skipping 14 matching lines...) Expand all
159 PopulatePrefetcherRequest); 199 PopulatePrefetcherRequest);
160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); 200 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint);
161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); 201 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData);
162 202
163 enum InitializationState { 203 enum InitializationState {
164 NOT_INITIALIZED = 0, 204 NOT_INITIALIZED = 0,
165 INITIALIZING = 1, 205 INITIALIZING = 1,
166 INITIALIZED = 2 206 INITIALIZED = 2
167 }; 207 };
168 208
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; 209 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; 210 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
213 211
214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> 212 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>>
215 NavigationMap; 213 NavigationMap;
216 214
217 // Returns true if the main page request is supported for prediction. 215 // Returns true if the main page request is supported for prediction.
218 static bool IsHandledMainPage(net::URLRequest* request); 216 static bool IsHandledMainPage(net::URLRequest* request);
219 217
220 // Returns true if the subresource request is supported for prediction. 218 // Returns true if the subresource request is supported for prediction.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // prefetched. 252 // prefetched.
255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); 253 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls);
256 254
257 // Returns true iff the |data_map| contains PrefetchData that can be used 255 // 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 256 // for a |main_frame_key| and fills |urls| with resources that need to be
259 // prefetched. 257 // prefetched.
260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, 258 bool PopulatePrefetcherRequest(const std::string& main_frame_key,
261 const PrefetchDataMap& data_map, 259 const PrefetchDataMap& data_map,
262 std::vector<GURL>* urls); 260 std::vector<GURL>* urls);
263 261
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 262 // Starts initialization by posting a task to the DB thread to read the
274 // predictor database. 263 // predictor database.
275 void StartInitialization(); 264 void StartInitialization();
276 265
277 // Callback for task to read predictor database. Takes ownership of 266 // Callback for task to read predictor database. Takes ownership of
278 // all arguments. 267 // all arguments.
279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 268 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
280 std::unique_ptr<PrefetchDataMap> host_data_map, 269 std::unique_ptr<PrefetchDataMap> host_data_map,
281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, 270 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); 271 std::unique_ptr<RedirectDataMap> host_redirect_data_map);
283 272
284 // Called during initialization when history is read and the predictor 273 // Called during initialization when history is read and the predictor
285 // database has been read. 274 // database has been read.
286 void OnHistoryAndCacheLoaded(); 275 void OnHistoryAndCacheLoaded();
287 276
288 // Removes data for navigations where the onload never fired. Will cleanup 277 // Removes data for navigations where the onload never fired. Will cleanup
289 // inflight_navigations_. 278 // inflight_navigations_.
290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 279 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
291 280
292 // Deletes all URLs from the predictor database, the caches and removes all 281 // Deletes all URLs from the predictor database, the caches and removes all
293 // inflight navigations. 282 // inflight navigations.
294 void DeleteAllUrls(); 283 void DeleteAllUrls();
295 284
296 // Deletes data for the input |urls| and their corresponding hosts from the 285 // Deletes data for the input |urls| and their corresponding hosts from the
297 // predictor database and caches. 286 // predictor database and caches.
298 void DeleteUrls(const history::URLRows& urls); 287 void DeleteUrls(const history::URLRows& urls);
299 288
300 // Callback for GetUrlVisitCountTask. 289 // Callback for GetUrlVisitCountTask.
301 void OnVisitCountLookup(size_t visit_count, 290 void OnVisitCountLookup(size_t url_visit_count,
302 const NavigationID& navigation_id,
303 const PageRequestSummary& summary); 291 const PageRequestSummary& summary);
304 292
305 // Removes the oldest entry in the input |data_map|, also deleting it from the 293 // Removes the oldest entry in the input |data_map|, also deleting it from the
306 // predictor database. 294 // predictor database.
307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, 295 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
308 PrefetchDataMap* data_map); 296 PrefetchDataMap* data_map);
309 297
310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, 298 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type,
311 RedirectDataMap* data_map); 299 RedirectDataMap* data_map);
312 300
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { 343 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) {
356 tables_ = tables; 344 tables_ = tables;
357 } 345 }
358 346
359 Profile* const profile_; 347 Profile* const profile_;
360 ResourcePrefetchPredictorConfig const config_; 348 ResourcePrefetchPredictorConfig const config_;
361 InitializationState initialization_state_; 349 InitializationState initialization_state_;
362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 350 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 351 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
364 base::CancelableTaskTracker history_lookup_consumer_; 352 base::CancelableTaskTracker history_lookup_consumer_;
353 base::ObserverList<Observer> observers_;
pasko 2016/10/21 12:12:52 Do we need many observers or just one would be eno
alexilin 2016/10/21 13:38:04 I see only one application point for these observe
365 354
366 // Copy of the data in the predictor tables. 355 // Copy of the data in the predictor tables.
367 std::unique_ptr<PrefetchDataMap> url_table_cache_; 356 std::unique_ptr<PrefetchDataMap> url_table_cache_;
368 std::unique_ptr<PrefetchDataMap> host_table_cache_; 357 std::unique_ptr<PrefetchDataMap> host_table_cache_;
369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; 358 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_;
370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; 359 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_;
371 360
372 NavigationMap inflight_navigations_; 361 NavigationMap inflight_navigations_;
373 362
374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 363 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
375 history_service_observer_; 364 history_service_observer_;
376 365
377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); 366 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
378 }; 367 };
379 368
380 } // namespace predictors 369 } // namespace predictors
381 370
382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 371 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698