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

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

Issue 2440723002: predictors: Make ResourcePrefetchPredictor observable. (Closed)
Patch Set: . 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
« 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>
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"
pasko 2016/10/21 14:41:36 not needed any more?
alexilin 2016/10/21 16:04:19 Done. I still don't know how to keep list of inclu
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 {
106 explicit PageRequestSummary(const GURL& main_frame_url);
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
103 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config, 117 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config,
104 Profile* profile); 118 Profile* profile);
105 ~ResourcePrefetchPredictor() override; 119 ~ResourcePrefetchPredictor() override;
106 120
107 // Thread safe. 121 // Thread safe.
108 static bool ShouldRecordRequest(net::URLRequest* request, 122 static bool ShouldRecordRequest(net::URLRequest* request,
109 content::ResourceType resource_type); 123 content::ResourceType resource_type);
110 static bool ShouldRecordResponse(net::URLRequest* response); 124 static bool ShouldRecordResponse(net::URLRequest* response);
111 static bool ShouldRecordRedirect(net::URLRequest* response); 125 static bool ShouldRecordRedirect(net::URLRequest* response);
112 126
(...skipping 12 matching lines...) Expand all
125 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the 139 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the
126 // predictor of main frame and resource requests. Should only be called if the 140 // predictor of main frame and resource requests. Should only be called if the
127 // corresponding Should* functions return true. 141 // corresponding Should* functions return true.
128 void RecordURLRequest(const URLRequestSummary& request); 142 void RecordURLRequest(const URLRequestSummary& request);
129 void RecordURLResponse(const URLRequestSummary& response); 143 void RecordURLResponse(const URLRequestSummary& response);
130 void RecordURLRedirect(const URLRequestSummary& response); 144 void RecordURLRedirect(const URLRequestSummary& response);
131 145
132 // Called when the main frame of a page completes loading. 146 // Called when the main frame of a page completes loading.
133 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); 147 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
134 148
149 // Starts prefetching if it is enabled and prefetching data exists for the
150 // NavigationID either at the URL or at the host level.
151 void StartPrefetching(const GURL& main_frame_url);
152
153 // Stops prefetching that may be in progress corresponding to |navigation_id|.
154 void StopPrefetching(const GURL& main_frame_url);
155
135 private: 156 private:
136 friend class ::PredictorsHandler; 157 friend class ::PredictorsHandler;
137 friend class ResourcePrefetchPredictorTest; 158 friend class ResourcePrefetchPredictorTest;
138 159
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
141 LazilyInitializeEmpty); 162 LazilyInitializeEmpty);
142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 163 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
143 LazilyInitializeWithData); 164 LazilyInitializeWithData);
144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 165 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
145 NavigationNotRecorded); 166 NavigationNotRecorded);
146 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB); 167 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB);
147 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB); 168 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB);
148 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 169 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
149 NavigationUrlNotInDBAndDBFull); 170 NavigationUrlNotInDBAndDBFull);
150 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB); 171 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlNotInDB);
151 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB); 172 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, RedirectUrlInDB);
152 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest); 173 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest);
153 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect); 174 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect);
154 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 175 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
155 OnSubresourceResponse); 176 OnSubresourceResponse);
156 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT); 177 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT);
157 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes); 178 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, HandledResourceTypes);
158 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 179 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
159 PopulatePrefetcherRequest); 180 PopulatePrefetcherRequest);
160 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint); 181 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetRedirectEndpoint);
161 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData); 182 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetPrefetchData);
162 183
184 // An interface used to notify clients (observers) of this object that data in
185 // the resource prefetch predictor has changed. Register the observer via
186 // ResourcePrefetchPredictor::SetObserverForTesting.
187 class Observer {
pasko 2016/10/21 14:41:36 s/Observer/TestObserver/
alexilin 2016/10/21 16:04:19 Done.
188 public:
189 virtual void OnNavigationLearned(size_t url_visit_count,
190 const PageRequestSummary& summary) {}
191
192 protected:
193 Observer() = default;
pasko 2016/10/21 14:41:36 if this is to disallow instantiating this class, t
alexilin 2016/10/21 16:04:19 No, this is for enabling default constructor expli
pasko 2016/10/21 16:47:34 Ah, oh, I did not know DISALLOW_COPY_AND_ASSIGN pr
194 virtual ~Observer() {}
pasko 2016/10/21 14:41:36 if the destructor is protected, then it becomes pr
alexilin 2016/10/21 16:04:19 Done.
195
196 private:
197 DISALLOW_COPY_AND_ASSIGN(Observer);
198 };
199
163 enum InitializationState { 200 enum InitializationState {
164 NOT_INITIALIZED = 0, 201 NOT_INITIALIZED = 0,
165 INITIALIZING = 1, 202 INITIALIZING = 1,
166 INITIALIZED = 2 203 INITIALIZED = 2
167 }; 204 };
168 205
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; 206 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
212 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; 207 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
213 208
214 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> 209 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>>
215 NavigationMap; 210 NavigationMap;
216 211
217 // Returns true if the main page request is supported for prediction. 212 // Returns true if the main page request is supported for prediction.
218 static bool IsHandledMainPage(net::URLRequest* request); 213 static bool IsHandledMainPage(net::URLRequest* request);
219 214
220 // Returns true if the subresource request is supported for prediction. 215 // Returns true if the subresource request is supported for prediction.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // prefetched. 249 // prefetched.
255 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls); 250 bool GetPrefetchData(const GURL& main_frame_url, std::vector<GURL>* urls);
256 251
257 // Returns true iff the |data_map| contains PrefetchData that can be used 252 // 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 253 // for a |main_frame_key| and fills |urls| with resources that need to be
259 // prefetched. 254 // prefetched.
260 bool PopulatePrefetcherRequest(const std::string& main_frame_key, 255 bool PopulatePrefetcherRequest(const std::string& main_frame_key,
261 const PrefetchDataMap& data_map, 256 const PrefetchDataMap& data_map,
262 std::vector<GURL>* urls); 257 std::vector<GURL>* urls);
263 258
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 259 // Starts initialization by posting a task to the DB thread to read the
274 // predictor database. 260 // predictor database.
275 void StartInitialization(); 261 void StartInitialization();
276 262
277 // Callback for task to read predictor database. Takes ownership of 263 // Callback for task to read predictor database. Takes ownership of
278 // all arguments. 264 // all arguments.
279 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 265 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
280 std::unique_ptr<PrefetchDataMap> host_data_map, 266 std::unique_ptr<PrefetchDataMap> host_data_map,
281 std::unique_ptr<RedirectDataMap> url_redirect_data_map, 267 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
282 std::unique_ptr<RedirectDataMap> host_redirect_data_map); 268 std::unique_ptr<RedirectDataMap> host_redirect_data_map);
283 269
284 // Called during initialization when history is read and the predictor 270 // Called during initialization when history is read and the predictor
285 // database has been read. 271 // database has been read.
286 void OnHistoryAndCacheLoaded(); 272 void OnHistoryAndCacheLoaded();
287 273
288 // Removes data for navigations where the onload never fired. Will cleanup 274 // Removes data for navigations where the onload never fired. Will cleanup
289 // inflight_navigations_. 275 // inflight_navigations_.
290 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 276 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
291 277
292 // Deletes all URLs from the predictor database, the caches and removes all 278 // Deletes all URLs from the predictor database, the caches and removes all
293 // inflight navigations. 279 // inflight navigations.
294 void DeleteAllUrls(); 280 void DeleteAllUrls();
295 281
296 // Deletes data for the input |urls| and their corresponding hosts from the 282 // Deletes data for the input |urls| and their corresponding hosts from the
297 // predictor database and caches. 283 // predictor database and caches.
298 void DeleteUrls(const history::URLRows& urls); 284 void DeleteUrls(const history::URLRows& urls);
299 285
300 // Callback for GetUrlVisitCountTask. 286 // Callback for GetUrlVisitCountTask.
301 void OnVisitCountLookup(size_t visit_count, 287 void OnVisitCountLookup(size_t url_visit_count,
302 const NavigationID& navigation_id,
303 const PageRequestSummary& summary); 288 const PageRequestSummary& summary);
304 289
305 // Removes the oldest entry in the input |data_map|, also deleting it from the 290 // Removes the oldest entry in the input |data_map|, also deleting it from the
306 // predictor database. 291 // predictor database.
307 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, 292 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
308 PrefetchDataMap* data_map); 293 PrefetchDataMap* data_map);
309 294
310 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, 295 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type,
311 RedirectDataMap* data_map); 296 RedirectDataMap* data_map);
312 297
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 334
350 // Used to connect to HistoryService or register for service loaded 335 // Used to connect to HistoryService or register for service loaded
351 // notificatioan. 336 // notificatioan.
352 void ConnectToHistoryService(); 337 void ConnectToHistoryService();
353 338
354 // Used for testing to inject mock tables. 339 // Used for testing to inject mock tables.
355 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { 340 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) {
356 tables_ = tables; 341 tables_ = tables;
357 } 342 }
358 343
344 // Sets an |observer| to be notified when the resource prefetch predictor data
pasko 2016/10/21 14:41:36 s/an/the/ or: s/an//
alexilin 2016/10/21 16:04:19 Done.
345 // changes. Use only for testing.
pasko 2016/10/21 14:41:36 Better omit the "Use only for testing" comment bec
alexilin 2016/10/21 16:04:19 Done.
346 void SetObserverForTesting(Observer* observer);
347
348 // Removes |observer| from the observer list. Use only for testing.
349 void RemoveObserverForTesting(Observer* observer);
pasko 2016/10/21 14:41:36 providing the same observer for removal looks a li
alexilin 2016/10/21 16:04:19 Your statement is contradictory a little bit. You
pasko 2016/10/21 16:47:34 This scoped observer would be useful if we are sur
350
359 Profile* const profile_; 351 Profile* const profile_;
352 Observer* observer_;
360 ResourcePrefetchPredictorConfig const config_; 353 ResourcePrefetchPredictorConfig const config_;
361 InitializationState initialization_state_; 354 InitializationState initialization_state_;
362 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 355 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
363 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 356 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
364 base::CancelableTaskTracker history_lookup_consumer_; 357 base::CancelableTaskTracker history_lookup_consumer_;
365 358
366 // Copy of the data in the predictor tables. 359 // Copy of the data in the predictor tables.
367 std::unique_ptr<PrefetchDataMap> url_table_cache_; 360 std::unique_ptr<PrefetchDataMap> url_table_cache_;
368 std::unique_ptr<PrefetchDataMap> host_table_cache_; 361 std::unique_ptr<PrefetchDataMap> host_table_cache_;
369 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; 362 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_;
370 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; 363 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_;
371 364
372 NavigationMap inflight_navigations_; 365 NavigationMap inflight_navigations_;
373 366
374 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 367 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
375 history_service_observer_; 368 history_service_observer_;
376 369
377 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); 370 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
378 }; 371 };
379 372
380 } // namespace predictors 373 } // namespace predictors
381 374
382 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 375 #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