Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / predictors / resource_prefetch_predictor.h
blob7f6aca45f639b75c830311d946ba66552036ddd2
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/containers/scoped_ptr_map.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/scoped_observer.h"
18 #include "base/task/cancelable_task_tracker.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/predictors/resource_prefetch_common.h"
21 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
22 #include "chrome/browser/predictors/resource_prefetcher.h"
23 #include "components/history/core/browser/history_service_observer.h"
24 #include "components/history/core/browser/history_types.h"
25 #include "components/keyed_service/core/keyed_service.h"
26 #include "content/public/common/resource_type.h"
27 #include "url/gurl.h"
29 class PredictorsHandler;
30 class Profile;
32 namespace net {
33 class URLRequest;
36 namespace predictors {
38 class ResourcePrefetcherManager;
40 // Contains logic for learning what can be prefetched and for kicking off
41 // speculative prefetching.
42 // - The class is a profile keyed service owned by the profile.
43 // - All the non-static methods of this class need to be called on the UI
44 // thread.
46 // The overall flow of the resource prefetching algorithm is as follows:
48 // * ResourcePrefetchPredictorObserver - Listens for URL requests, responses and
49 // redirects on the IO thread (via ResourceDispatcherHostDelegate) and posts
50 // tasks to the ResourcePrefetchPredictor on the UI thread. This is owned by
51 // the ProfileIOData for the profile.
52 // * ResourcePrefetchPredictorTables - Persists ResourcePrefetchPredictor data
53 // to a sql database. Runs entirely on the DB thread. Owned by the
54 // PredictorDatabase.
55 // * ResourcePrefetchPredictor - Learns about resource requirements per URL in
56 // the UI thread through the ResourcePrefetchPredictorObserver and persists
57 // it to disk in the DB thread through the ResourcePrefetchPredictorTables. It
58 // initiates resource prefetching using the ResourcePrefetcherManager. Owned
59 // by profile.
60 // * ResourcePrefetcherManager - Manages the ResourcePrefetchers that do the
61 // prefetching on the IO thread. The manager is owned by the
62 // ResourcePrefetchPredictor and interfaces between the predictor on the UI
63 // thread and the prefetchers on the IO thread.
64 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the
65 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources.
67 // TODO(shishir): Do speculative prefetching for https resources and/or https
68 // main frame urls.
69 // TODO(zhenw): Currently only main frame requests/redirects/responses are
70 // recorded. Consider recording sub-frame responses independently or together
71 // with main frame.
72 class ResourcePrefetchPredictor
73 : public KeyedService,
74 public history::HistoryServiceObserver,
75 public base::SupportsWeakPtr<ResourcePrefetchPredictor> {
76 public:
77 // Stores the data that we need to get from the URLRequest.
78 struct URLRequestSummary {
79 URLRequestSummary();
80 URLRequestSummary(const URLRequestSummary& other);
81 ~URLRequestSummary();
83 NavigationID navigation_id;
84 GURL resource_url;
85 content::ResourceType resource_type;
87 // Only for responses.
88 std::string mime_type;
89 bool was_cached;
90 GURL redirect_url; // Empty unless request was redirected to a valid url.
93 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig& config,
94 Profile* profile);
95 ~ResourcePrefetchPredictor() override;
97 // Thread safe.
98 static bool ShouldRecordRequest(net::URLRequest* request,
99 content::ResourceType resource_type);
100 static bool ShouldRecordResponse(net::URLRequest* response);
101 static bool ShouldRecordRedirect(net::URLRequest* response);
103 // Determines the ResourceType from the mime type, defaulting to the
104 // |fallback| if the ResourceType could not be determined.
105 static content::ResourceType GetResourceTypeFromMimeType(
106 const std::string& mime_type,
107 content::ResourceType fallback);
109 // 'ResourcePrefetchPredictorObserver' calls the below functions to inform the
110 // predictor of main frame and resource requests. Should only be called if the
111 // corresponding Should* functions return true.
112 void RecordURLRequest(const URLRequestSummary& request);
113 void RecordURLResponse(const URLRequestSummary& response);
114 void RecordURLRedirect(const URLRequestSummary& response);
116 // Called when the main frame of a page completes loading.
117 void RecordMainFrameLoadComplete(const NavigationID& navigation_id);
119 // Called by ResourcePrefetcherManager to notify that prefetching has finished
120 // for a navigation. Should take ownership of |requests|.
121 virtual void FinishedPrefetchForNavigation(
122 const NavigationID& navigation_id,
123 PrefetchKeyType key_type,
124 ResourcePrefetcher::RequestVector* requests);
126 private:
127 friend class ::PredictorsHandler;
128 friend class ResourcePrefetchPredictorTest;
130 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls);
131 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
132 LazilyInitializeEmpty);
133 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
134 LazilyInitializeWithData);
135 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
136 NavigationNotRecorded);
137 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlInDB);
138 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, NavigationUrlNotInDB);
139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
140 NavigationUrlNotInDBAndDBFull);
141 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRequest);
142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, OnMainFrameRedirect);
143 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest,
144 OnSubresourceResponse);
145 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, GetCorrectPLT);
147 enum InitializationState {
148 NOT_INITIALIZED = 0,
149 INITIALIZING = 1,
150 INITIALIZED = 2
153 // Stores prefetching results.
154 struct Result {
155 // Takes ownership of requests.
156 Result(PrefetchKeyType key_type,
157 ResourcePrefetcher::RequestVector* requests);
158 ~Result();
160 PrefetchKeyType key_type;
161 scoped_ptr<ResourcePrefetcher::RequestVector> requests;
163 private:
164 DISALLOW_COPY_AND_ASSIGN(Result);
167 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
168 typedef ResourcePrefetchPredictorTables::ResourceRows ResourceRows;
169 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
170 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
171 typedef std::map<NavigationID, linked_ptr<std::vector<URLRequestSummary> > >
172 NavigationMap;
173 typedef base::ScopedPtrMap<NavigationID, scoped_ptr<Result>> ResultsMap;
175 // Returns true if the main page request is supported for prediction.
176 static bool IsHandledMainPage(net::URLRequest* request);
178 // Returns true if the subresource request is supported for prediction.
179 static bool IsHandledSubresource(net::URLRequest* request);
181 // Returns true if the request (should have a response in it) is cacheable.
182 static bool IsCacheable(const net::URLRequest* request);
184 // KeyedService methods override.
185 void Shutdown() override;
187 // Functions called on different network events pertaining to the loading of
188 // main frame resource or sub resources.
189 void OnMainFrameRequest(const URLRequestSummary& request);
190 void OnMainFrameResponse(const URLRequestSummary& response);
191 void OnMainFrameRedirect(const URLRequestSummary& response);
192 void OnSubresourceResponse(const URLRequestSummary& response);
194 // Called when onload completes for a navigation. We treat this point as the
195 // "completion" of the navigation. The resources requested by the page up to
196 // this point are the only ones considered for prefetching. Return the page
197 // load time for testing.
198 base::TimeDelta OnNavigationComplete(
199 const NavigationID& nav_id_without_timing_info);
201 // Returns true if there is PrefetchData that can be used for the
202 // navigation and fills in the |prefetch_data| to resources that need to be
203 // prefetched.
204 bool GetPrefetchData(const NavigationID& navigation_id,
205 ResourcePrefetcher::RequestVector* prefetch_requests,
206 PrefetchKeyType* key_type);
208 // Converts a PrefetchData into a ResourcePrefetcher::RequestVector.
209 void PopulatePrefetcherRequest(const PrefetchData& data,
210 ResourcePrefetcher::RequestVector* requests);
212 // Starts prefetching if it is enabled and prefetching data exists for the
213 // NavigationID either at the URL or at the host level.
214 void StartPrefetching(const NavigationID& navigation_id);
216 // Stops prefetching that may be in progress corresponding to |navigation_id|.
217 void StopPrefetching(const NavigationID& navigation_id);
219 // Starts initialization by posting a task to the DB thread to read the
220 // predictor database.
221 void StartInitialization();
223 // Callback for task to read predictor database. Takes ownership of
224 // |url_data_map| and |host_data_map|.
225 void CreateCaches(scoped_ptr<PrefetchDataMap> url_data_map,
226 scoped_ptr<PrefetchDataMap> host_data_map);
228 // Called during initialization when history is read and the predictor
229 // database has been read.
230 void OnHistoryAndCacheLoaded();
232 // Removes data for navigations where the onload never fired. Will cleanup
233 // inflight_navigations_ and results_map_.
234 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
236 // Deletes all URLs from the predictor database, the caches and removes all
237 // inflight navigations.
238 void DeleteAllUrls();
240 // Deletes data for the input |urls| and their corresponding hosts from the
241 // predictor database and caches.
242 void DeleteUrls(const history::URLRows& urls);
244 // Callback for GetUrlVisitCountTask.
245 void OnVisitCountLookup(size_t visit_count,
246 const NavigationID& navigation_id,
247 const std::vector<URLRequestSummary>& requests);
249 // Removes the oldest entry in the input |data_map|, also deleting it from the
250 // predictor database.
251 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
252 PrefetchDataMap* data_map);
254 // Merges resources in |new_resources| into the |data_map| and correspondingly
255 // updates the predictor database.
256 void LearnNavigation(const std::string& key,
257 PrefetchKeyType key_type,
258 const std::vector<URLRequestSummary>& new_resources,
259 size_t max_data_map_size,
260 PrefetchDataMap* data_map);
262 // Reports overall page load time.
263 void ReportPageLoadTimeStats(base::TimeDelta plt) const;
265 // Reports page load time for prefetched and not prefetched pages
266 void ReportPageLoadTimePrefetchStats(
267 base::TimeDelta plt,
268 bool prefetched,
269 base::Callback<void(int)> report_network_type_callback,
270 PrefetchKeyType key_type) const;
272 // Reports accuracy by comparing prefetched resources with resources that are
273 // actually used by the page.
274 void ReportAccuracyStats(PrefetchKeyType key_type,
275 const std::vector<URLRequestSummary>& actual,
276 ResourcePrefetcher::RequestVector* prefetched) const;
278 // Reports predicted accuracy i.e. by comparing resources that are actually
279 // used by the page with those that may have been prefetched.
280 void ReportPredictedAccuracyStats(
281 PrefetchKeyType key_type,
282 const std::vector<URLRequestSummary>& actual,
283 const ResourcePrefetcher::RequestVector& predicted) const;
284 void ReportPredictedAccuracyStatsHelper(
285 PrefetchKeyType key_type,
286 const ResourcePrefetcher::RequestVector& predicted,
287 const std::map<GURL, bool>& actual,
288 size_t total_resources_fetched_from_network,
289 size_t max_assumed_prefetched) const;
291 // history::HistoryServiceObserver:
292 void OnURLsDeleted(history::HistoryService* history_service,
293 bool all_history,
294 bool expired,
295 const history::URLRows& deleted_rows,
296 const std::set<GURL>& favicon_urls) override;
297 void OnHistoryServiceLoaded(
298 history::HistoryService* history_service) override;
300 // Used to connect to HistoryService or register for service loaded
301 // notificatioan.
302 void ConnectToHistoryService();
304 // Used for testing to inject mock tables.
305 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) {
306 tables_ = tables;
309 Profile* const profile_;
310 ResourcePrefetchPredictorConfig const config_;
311 InitializationState initialization_state_;
312 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
313 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
314 base::CancelableTaskTracker history_lookup_consumer_;
316 // Map of all the navigations in flight to their resource requests.
317 NavigationMap inflight_navigations_;
319 // Copy of the data in the predictor tables.
320 scoped_ptr<PrefetchDataMap> url_table_cache_;
321 scoped_ptr<PrefetchDataMap> host_table_cache_;
323 ResultsMap results_map_;
325 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
326 history_service_observer_;
328 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor);
331 } // namespace predictors
333 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_