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

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

Issue 2937623007: predictors: Move more methods from ResourcePrefetchPredictor into LoadingDataCollector. (Closed)
Patch Set: Fix browser test Created 3 years, 5 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 16 matching lines...) Expand all
27 #include "components/history/core/browser/history_db_task.h" 27 #include "components/history/core/browser/history_db_task.h"
28 #include "components/history/core/browser/history_service_observer.h" 28 #include "components/history/core/browser/history_service_observer.h"
29 #include "components/history/core/browser/history_types.h" 29 #include "components/history/core/browser/history_types.h"
30 #include "components/keyed_service/core/keyed_service.h" 30 #include "components/keyed_service/core/keyed_service.h"
31 #include "content/public/common/resource_type.h" 31 #include "content/public/common/resource_type.h"
32 #include "url/gurl.h" 32 #include "url/gurl.h"
33 33
34 class PredictorsHandler; 34 class PredictorsHandler;
35 class Profile; 35 class Profile;
36 36
37 namespace net { 37 namespace predictors {
38 class URLRequest;
39 }
40 38
41 namespace predictors { 39 struct OriginRequestSummary;
40 struct URLRequestSummary;
41 struct PageRequestSummary;
42 42
43 namespace internal { 43 namespace internal {
44 constexpr char kResourcePrefetchPredictorPrefetchingDurationHistogram[] = 44 constexpr char kResourcePrefetchPredictorPrefetchingDurationHistogram[] =
45 "ResourcePrefetchPredictor.PrefetchingDuration"; 45 "ResourcePrefetchPredictor.PrefetchingDuration";
46 46
47 const uint32_t kVersionedRemovedExperiment = 0x03ff25e3; 47 const uint32_t kVersionedRemovedExperiment = 0x03ff25e3;
48 const uint32_t kUnusedRemovedExperiment = 0xf7f77166; 48 const uint32_t kUnusedRemovedExperiment = 0xf7f77166;
49 const uint32_t kNoStoreRemovedExperiment = 0xd90a199a; 49 const uint32_t kNoStoreRemovedExperiment = 0xd90a199a;
50 50
51 struct LastVisitTimeCompare { 51 struct LastVisitTimeCompare {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data 88 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
89 // to a sql database. Runs entirely on the DB thread. Owned by the 89 // to a sql database. Runs entirely on the DB thread. Owned by the
90 // PredictorDatabase. 90 // PredictorDatabase.
91 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in 91 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
92 // the UI thread through the LoadingPredictorObserver and persists 92 // the UI thread through the LoadingPredictorObserver and persists
93 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It 93 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It
94 // initiates resource prefetching using the ResourcePrefetcherManager. Owned 94 // initiates resource prefetching using the ResourcePrefetcherManager. Owned
95 // by profile. 95 // by profile.
96 class ResourcePrefetchPredictor : public history::HistoryServiceObserver { 96 class ResourcePrefetchPredictor : public history::HistoryServiceObserver {
97 public: 97 public:
98 // Data collected for origin-based prediction, for a single origin during a
99 // page load (see PageRequestSummary).
100 struct OriginRequestSummary {
101 OriginRequestSummary();
102 OriginRequestSummary(const OriginRequestSummary& other);
103 ~OriginRequestSummary();
104
105 GURL origin;
106 bool always_access_network;
107 bool accessed_network;
108 int first_occurrence;
109 };
110
111 // Stores the data that we need to get from the URLRequest.
112 struct URLRequestSummary {
113 URLRequestSummary();
114 URLRequestSummary(const URLRequestSummary& other);
115 ~URLRequestSummary();
116
117 NavigationID navigation_id;
118 GURL resource_url;
119 GURL request_url; // URL after all redirects.
120 content::ResourceType resource_type;
121 net::RequestPriority priority;
122 base::TimeTicks response_time;
123 bool before_first_contentful_paint;
124
125 // Only for responses.
126 std::string mime_type;
127 bool was_cached;
128 GURL redirect_url; // Empty unless request was redirected to a valid url.
129
130 bool has_validators;
131 bool always_revalidate;
132 bool is_no_store;
133 bool network_accessed;
134
135 // Initializes a |URLRequestSummary| from a |URLRequest| response.
136 // Returns true for success. Note: NavigationID is NOT initialized
137 // by this function.
138 static bool SummarizeResponse(const net::URLRequest& request,
139 URLRequestSummary* summary);
140 };
141
142 // Stores the data learned from a single navigation.
143 struct PageRequestSummary {
144 explicit PageRequestSummary(const GURL& main_frame_url);
145 PageRequestSummary(const PageRequestSummary& other);
146 ~PageRequestSummary();
147
148 GURL main_frame_url;
149 GURL initial_url;
150 base::TimeTicks first_contentful_paint;
151
152 // Stores all subresource requests within a single navigation, from initial
153 // main frame request to navigation completion.
154 std::vector<URLRequestSummary> subresource_requests;
155 // Map of origin -> OriginRequestSummary. Only one instance of each origin
156 // is kept per navigation, but the summary is updated several times.
157 std::map<GURL, OriginRequestSummary> origins;
158 };
159
160 // Stores a result of prediction. Essentially, |subresource_urls| is main 98 // Stores a result of prediction. Essentially, |subresource_urls| is main
161 // result and other fields are used for diagnosis and histograms reporting. 99 // result and other fields are used for diagnosis and histograms reporting.
162 struct Prediction { 100 struct Prediction {
163 Prediction(); 101 Prediction();
164 Prediction(const Prediction& other); 102 Prediction(const Prediction& other);
165 ~Prediction(); 103 ~Prediction();
166 104
167 bool is_host; 105 bool is_host;
168 bool is_redirected; 106 bool is_redirected;
169 std::string main_frame_key; 107 std::string main_frame_key;
(...skipping 23 matching lines...) Expand all
193 131
194 ResourcePrefetchPredictor(const LoadingPredictorConfig& config, 132 ResourcePrefetchPredictor(const LoadingPredictorConfig& config,
195 Profile* profile); 133 Profile* profile);
196 ~ResourcePrefetchPredictor() override; 134 ~ResourcePrefetchPredictor() override;
197 135
198 // Starts initialization by posting a task to the DB thread to read the 136 // Starts initialization by posting a task to the DB thread to read the
199 // predictor database. Virtual for testing. 137 // predictor database. Virtual for testing.
200 virtual void StartInitialization(); 138 virtual void StartInitialization();
201 virtual void Shutdown(); 139 virtual void Shutdown();
202 140
203 // Determines the resource type from the declared one, falling back to MIME
204 // type detection when it is not explicit.
205 static content::ResourceType GetResourceType(
206 content::ResourceType resource_type,
207 const std::string& mime_type);
208
209 // Determines the ResourceType from the mime type, defaulting to the
210 // |fallback| if the ResourceType could not be determined.
211 static content::ResourceType GetResourceTypeFromMimeType(
212 const std::string& mime_type,
213 content::ResourceType fallback);
214
215 // Returns true if prefetching data exists for the |main_frame_url|. 141 // Returns true if prefetching data exists for the |main_frame_url|.
216 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const; 142 virtual bool IsUrlPrefetchable(const GURL& main_frame_url) const;
217 143
218 // Returns true iff |resource| has sufficient confidence level and required 144 // Returns true iff |resource| has sufficient confidence level and required
219 // number of hits. 145 // number of hits.
220 bool IsResourcePrefetchable(const ResourceData& resource) const; 146 bool IsResourcePrefetchable(const ResourceData& resource) const;
221 147
222 // Sets the |observer| to be notified when the resource prefetch predictor 148 // Sets the |observer| to be notified when the resource prefetch predictor
223 // data changes. Previously registered observer will be discarded. Call 149 // data changes. Previously registered observer will be discarded. Call
224 // this with nullptr parameter to de-register observer. 150 // this with nullptr parameter to de-register observer.
225 void SetObserverForTesting(TestObserver* observer); 151 void SetObserverForTesting(TestObserver* observer);
226 152
227 // Sets the |stats_collector| which is used to report histograms. 153 // Sets the |stats_collector| which is used to report histograms.
228 void SetStatsCollector(LoadingStatsCollector* stats_collector); 154 void SetStatsCollector(LoadingStatsCollector* stats_collector);
229 155
230 // Returns true iff there is PrefetchData that can be used for a 156 // Returns true iff there is PrefetchData that can be used for a
231 // |main_frame_url| and fills |prediction| with resources that need to be 157 // |main_frame_url| and fills |prediction| with resources that need to be
232 // prefetched. |prediction| pointer may be nullptr to get return value only. 158 // prefetched. |prediction| pointer may be nullptr to get return value only.
233 virtual bool GetPrefetchData(const GURL& main_frame_url, 159 virtual bool GetPrefetchData(const GURL& main_frame_url,
234 Prediction* prediction) const; 160 Prediction* prediction) const;
235 161
236 // Returns true iff there is OriginData that can be used for a |url| and fills 162 // Returns true iff there is OriginData that can be used for a |url| and fills
237 // |prediction| with origins and hosts that need to be preconnected and 163 // |prediction| with origins and hosts that need to be preconnected and
238 // preresolved respectively. 164 // preresolved respectively.
239 virtual bool PredictPreconnectOrigins(const GURL& url, 165 virtual bool PredictPreconnectOrigins(const GURL& url,
240 PreconnectPrediction* prediction) const; 166 PreconnectPrediction* prediction) const;
241 167
168 // Called by the collector after a page has finished loading resources and
169 // assembled a PageRequestSummary.
170 virtual void RecordPageRequestSummary(
171 std::unique_ptr<PageRequestSummary> summary);
172
242 private: 173 private:
243 // 'LoadingPredictorObserver' calls the below functions to inform the 174 friend class LoadingPredictor;
244 // predictor of main frame and resource requests. Should only be called if the
245 // corresponding Should* functions return true.
246 void RecordURLRequest(const URLRequestSummary& request);
247 void RecordURLResponse(const URLRequestSummary& response);
248 void RecordURLRedirect(const URLRequestSummary& response);
249
250 // Called when the main frame of a page completes loading.
251 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
252
253 // Called after the main frame's first contentful paint.
254 void RecordFirstContentfulPaint(
255 const NavigationID& navigation_id,
256 const base::TimeTicks& first_contentful_paint);
257
258 friend class ::PredictorsHandler; 175 friend class ::PredictorsHandler;
259 friend class LoadingDataCollector; 176 friend class LoadingDataCollector;
260 friend class ResourcePrefetchPredictorTest; 177 friend class ResourcePrefetchPredictorTest;
261 friend class ResourcePrefetchPredictorBrowserTest; 178 friend class ResourcePrefetchPredictorBrowserTest;
262 179
263 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); 180 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
264 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 181 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
265 LazilyInitializeEmpty); 182 LazilyInitializeEmpty);
266 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 183 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
267 LazilyInitializeWithData); 184 LazilyInitializeWithData);
(...skipping 21 matching lines...) Expand all
289 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 206 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
290 TestPrefetchingDurationHistogram); 207 TestPrefetchingDurationHistogram);
291 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, 208 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
292 TestRecordFirstContentfulPaint); 209 TestRecordFirstContentfulPaint);
293 210
294 enum InitializationState { 211 enum InitializationState {
295 NOT_INITIALIZED = 0, 212 NOT_INITIALIZED = 0,
296 INITIALIZING = 1, 213 INITIALIZING = 1,
297 INITIALIZED = 2 214 INITIALIZED = 2
298 }; 215 };
299 // Returns true if the request (should have a response in it) is "no-store".
300 static bool IsNoStore(const net::URLRequest& request);
301
302 // Functions called on different network events pertaining to the loading of
303 // main frame resource or sub resources.
304 void OnMainFrameRequest(const URLRequestSummary& request);
305 void OnMainFrameRedirect(const URLRequestSummary& response);
306 void OnSubresourceResponse(const URLRequestSummary& response);
307 void OnSubresourceRedirect(const URLRequestSummary& response);
308
309 // Called when onload completes for a navigation. We treat this point as the
310 // "completion" of the navigation. The resources requested by the page up to
311 // this point are the only ones considered for prefetching.
312 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info);
313 216
314 // Returns true iff one of the following conditions is true 217 // Returns true iff one of the following conditions is true
315 // * |redirect_data| contains confident redirect endpoint for |entry_point| 218 // * |redirect_data| contains confident redirect endpoint for |entry_point|
316 // and assigns it to the |redirect_endpoint| 219 // and assigns it to the |redirect_endpoint|
317 // 220 //
318 // * |redirect_data| doens't contain an entry for |entry_point| and assigns 221 // * |redirect_data| doens't contain an entry for |entry_point| and assigns
319 // |entry_point| to the |redirect_endpoint|. 222 // |entry_point| to the |redirect_endpoint|.
320 bool GetRedirectEndpoint(const std::string& entry_point, 223 bool GetRedirectEndpoint(const std::string& entry_point,
321 const RedirectDataMap& redirect_data, 224 const RedirectDataMap& redirect_data,
322 std::string* redirect_endpoint) const; 225 std::string* redirect_endpoint) const;
(...skipping 10 matching lines...) Expand all
333 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_resource_data, 236 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_resource_data,
334 std::unique_ptr<PrefetchDataMap> host_resource_data, 237 std::unique_ptr<PrefetchDataMap> host_resource_data,
335 std::unique_ptr<RedirectDataMap> url_redirect_data, 238 std::unique_ptr<RedirectDataMap> url_redirect_data,
336 std::unique_ptr<RedirectDataMap> host_redirect_data, 239 std::unique_ptr<RedirectDataMap> host_redirect_data,
337 std::unique_ptr<OriginDataMap> origin_data); 240 std::unique_ptr<OriginDataMap> origin_data);
338 241
339 // Called during initialization when history is read and the predictor 242 // Called during initialization when history is read and the predictor
340 // database has been read. 243 // database has been read.
341 void OnHistoryAndCacheLoaded(); 244 void OnHistoryAndCacheLoaded();
342 245
343 // Cleanup inflight_navigations_ and call a cleanup for stats_collector_. 246 // Deletes all URLs from the predictor database and caches.
344 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
345
346 // Deletes all URLs from the predictor database, the caches and removes all
347 // inflight navigations.
348 void DeleteAllUrls(); 247 void DeleteAllUrls();
349 248
350 // Deletes data for the input |urls| and their corresponding hosts from the 249 // Deletes data for the input |urls| and their corresponding hosts from the
351 // predictor database and caches. 250 // predictor database and caches.
352 void DeleteUrls(const history::URLRows& urls); 251 void DeleteUrls(const history::URLRows& urls);
353 252
354 // Callback for GetUrlVisitCountTask. 253 // Callback for GetUrlVisitCountTask.
355 void OnVisitCountLookup(size_t url_visit_count, 254 void OnVisitCountLookup(size_t url_visit_count,
356 const PageRequestSummary& summary); 255 const PageRequestSummary& summary);
357 256
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 InitializationState initialization_state_; 298 InitializationState initialization_state_;
400 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 299 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
401 base::CancelableTaskTracker history_lookup_consumer_; 300 base::CancelableTaskTracker history_lookup_consumer_;
402 301
403 std::unique_ptr<PrefetchDataMap> url_resource_data_; 302 std::unique_ptr<PrefetchDataMap> url_resource_data_;
404 std::unique_ptr<PrefetchDataMap> host_resource_data_; 303 std::unique_ptr<PrefetchDataMap> host_resource_data_;
405 std::unique_ptr<RedirectDataMap> url_redirect_data_; 304 std::unique_ptr<RedirectDataMap> url_redirect_data_;
406 std::unique_ptr<RedirectDataMap> host_redirect_data_; 305 std::unique_ptr<RedirectDataMap> host_redirect_data_;
407 std::unique_ptr<OriginDataMap> origin_data_; 306 std::unique_ptr<OriginDataMap> origin_data_;
408 307
409 NavigationMap inflight_navigations_;
410
411 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 308 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
412 history_service_observer_; 309 history_service_observer_;
413 310
414 base::WeakPtrFactory<ResourcePrefetchPredictor> weak_factory_; 311 base::WeakPtrFactory<ResourcePrefetchPredictor> weak_factory_;
415 312
416 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); 313 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
417 }; 314 };
418 315
419 // An interface used to notify that data in the ResourcePrefetchPredictor 316 // An interface used to notify that data in the ResourcePrefetchPredictor
420 // has changed. All methods are invoked on the UI thread. 317 // has changed. All methods are invoked on the UI thread.
421 class TestObserver { 318 class TestObserver {
422 public: 319 public:
423 // De-registers itself from |predictor_| on destruction. 320 // De-registers itself from |predictor_| on destruction.
424 virtual ~TestObserver(); 321 virtual ~TestObserver();
425 322
426 virtual void OnPredictorInitialized() {} 323 virtual void OnPredictorInitialized() {}
427 324
428 virtual void OnNavigationLearned( 325 virtual void OnNavigationLearned(size_t url_visit_count,
429 size_t url_visit_count, 326 const PageRequestSummary& summary) {}
430 const ResourcePrefetchPredictor::PageRequestSummary& summary) {}
431 327
432 protected: 328 protected:
433 // |predictor| must be non-NULL and has to outlive the TestObserver. 329 // |predictor| must be non-NULL and has to outlive the TestObserver.
434 // Also the predictor must not have a TestObserver set. 330 // Also the predictor must not have a TestObserver set.
435 explicit TestObserver(ResourcePrefetchPredictor* predictor); 331 explicit TestObserver(ResourcePrefetchPredictor* predictor);
436 332
437 private: 333 private:
438 ResourcePrefetchPredictor* predictor_; 334 ResourcePrefetchPredictor* predictor_;
439 335
440 DISALLOW_COPY_AND_ASSIGN(TestObserver); 336 DISALLOW_COPY_AND_ASSIGN(TestObserver);
441 }; 337 };
442 338
443 } // namespace predictors 339 } // namespace predictors
444 340
445 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 341 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW
« no previous file with comments | « chrome/browser/predictors/loading_test_util.cc ('k') | chrome/browser/predictors/resource_prefetch_predictor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698