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