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_
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"
28 class PredictorsHandler
;
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
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
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
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
68 // TODO(zhenw): Currently only main frame requests/redirects/responses are
69 // recorded. Consider recording sub-frame responses independently or together
71 class ResourcePrefetchPredictor
72 : public KeyedService
,
73 public history::HistoryServiceObserver
,
74 public base::SupportsWeakPtr
<ResourcePrefetchPredictor
> {
76 // Stores the data that we need to get from the URLRequest.
77 struct URLRequestSummary
{
79 URLRequestSummary(const URLRequestSummary
& other
);
82 NavigationID navigation_id
;
84 content::ResourceType resource_type
;
86 // Only for responses.
87 std::string mime_type
;
89 GURL redirect_url
; // Empty unless request was redirected to a valid url.
92 ResourcePrefetchPredictor(const ResourcePrefetchPredictorConfig
& config
,
94 ~ResourcePrefetchPredictor() override
;
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
);
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
);
144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest
, GetCorrectPLT
);
146 enum InitializationState
{
152 // Stores prefetching results.
154 // Takes ownership of requests.
155 Result(PrefetchKeyType key_type
,
156 ResourcePrefetcher::RequestVector
* requests
);
159 PrefetchKeyType key_type
;
160 scoped_ptr
<ResourcePrefetcher::RequestVector
> requests
;
163 DISALLOW_COPY_AND_ASSIGN(Result
);
166 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow
;
167 typedef ResourcePrefetchPredictorTables::ResourceRows ResourceRows
;
168 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData
;
169 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap
;
170 typedef std::map
<NavigationID
, linked_ptr
<std::vector
<URLRequestSummary
> > >
172 typedef std::map
<NavigationID
, Result
*> ResultsMap
;
174 // Returns true if the main page request is supported for prediction.
175 static bool IsHandledMainPage(net::URLRequest
* request
);
177 // Returns true if the subresource request is supported for prediction.
178 static bool IsHandledSubresource(net::URLRequest
* request
);
180 // Returns true if the request (should have a response in it) is cacheable.
181 static bool IsCacheable(const net::URLRequest
* request
);
183 // KeyedService methods override.
184 void Shutdown() override
;
186 // Functions called on different network events pertaining to the loading of
187 // main frame resource or sub resources.
188 void OnMainFrameRequest(const URLRequestSummary
& request
);
189 void OnMainFrameResponse(const URLRequestSummary
& response
);
190 void OnMainFrameRedirect(const URLRequestSummary
& response
);
191 void OnSubresourceResponse(const URLRequestSummary
& response
);
193 // Called when onload completes for a navigation. We treat this point as the
194 // "completion" of the navigation. The resources requested by the page up to
195 // this point are the only ones considered for prefetching. Return the page
196 // load time for testing.
197 base::TimeDelta
OnNavigationComplete(
198 const NavigationID
& nav_id_without_timing_info
);
200 // Returns true if there is PrefetchData that can be used for the
201 // navigation and fills in the |prefetch_data| to resources that need to be
203 bool GetPrefetchData(const NavigationID
& navigation_id
,
204 ResourcePrefetcher::RequestVector
* prefetch_requests
,
205 PrefetchKeyType
* key_type
);
207 // Converts a PrefetchData into a ResourcePrefetcher::RequestVector.
208 void PopulatePrefetcherRequest(const PrefetchData
& data
,
209 ResourcePrefetcher::RequestVector
* requests
);
211 // Starts prefetching if it is enabled and prefetching data exists for the
212 // NavigationID either at the URL or at the host level.
213 void StartPrefetching(const NavigationID
& navigation_id
);
215 // Stops prefetching that may be in progress corresponding to |navigation_id|.
216 void StopPrefetching(const NavigationID
& navigation_id
);
218 // Starts initialization by posting a task to the DB thread to read the
219 // predictor database.
220 void StartInitialization();
222 // Callback for task to read predictor database. Takes ownership of
223 // |url_data_map| and |host_data_map|.
224 void CreateCaches(scoped_ptr
<PrefetchDataMap
> url_data_map
,
225 scoped_ptr
<PrefetchDataMap
> host_data_map
);
227 // Called during initialization when history is read and the predictor
228 // database has been read.
229 void OnHistoryAndCacheLoaded();
231 // Removes data for navigations where the onload never fired. Will cleanup
232 // inflight_navigations_ and results_map_.
233 void CleanupAbandonedNavigations(const NavigationID
& navigation_id
);
235 // Deletes all URLs from the predictor database, the caches and removes all
236 // inflight navigations.
237 void DeleteAllUrls();
239 // Deletes data for the input |urls| and their corresponding hosts from the
240 // predictor database and caches.
241 void DeleteUrls(const history::URLRows
& urls
);
243 // Callback for GetUrlVisitCountTask.
244 void OnVisitCountLookup(size_t visit_count
,
245 const NavigationID
& navigation_id
,
246 const std::vector
<URLRequestSummary
>& requests
);
248 // Removes the oldest entry in the input |data_map|, also deleting it from the
249 // predictor database.
250 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type
,
251 PrefetchDataMap
* data_map
);
253 // Merges resources in |new_resources| into the |data_map| and correspondingly
254 // updates the predictor database.
255 void LearnNavigation(const std::string
& key
,
256 PrefetchKeyType key_type
,
257 const std::vector
<URLRequestSummary
>& new_resources
,
258 size_t max_data_map_size
,
259 PrefetchDataMap
* data_map
);
261 // Reports overall page load time.
262 void ReportPageLoadTimeStats(base::TimeDelta plt
) const;
264 // Reports page load time for prefetched and not prefetched pages
265 void ReportPageLoadTimePrefetchStats(
268 base::Callback
<void(int)> report_network_type_callback
,
269 PrefetchKeyType key_type
) const;
271 // Reports accuracy by comparing prefetched resources with resources that are
272 // actually used by the page.
273 void ReportAccuracyStats(PrefetchKeyType key_type
,
274 const std::vector
<URLRequestSummary
>& actual
,
275 ResourcePrefetcher::RequestVector
* prefetched
) const;
277 // Reports predicted accuracy i.e. by comparing resources that are actually
278 // used by the page with those that may have been prefetched.
279 void ReportPredictedAccuracyStats(
280 PrefetchKeyType key_type
,
281 const std::vector
<URLRequestSummary
>& actual
,
282 const ResourcePrefetcher::RequestVector
& predicted
) const;
283 void ReportPredictedAccuracyStatsHelper(
284 PrefetchKeyType key_type
,
285 const ResourcePrefetcher::RequestVector
& predicted
,
286 const std::map
<GURL
, bool>& actual
,
287 size_t total_resources_fetched_from_network
,
288 size_t max_assumed_prefetched
) const;
290 // history::HistoryServiceObserver:
291 void OnURLsDeleted(history::HistoryService
* history_service
,
294 const history::URLRows
& deleted_rows
,
295 const std::set
<GURL
>& favicon_urls
) override
;
296 void OnHistoryServiceLoaded(
297 history::HistoryService
* history_service
) override
;
299 // Used to connect to HistoryService or register for service loaded
301 void ConnectToHistoryService();
303 // Used for testing to inject mock tables.
304 void set_mock_tables(scoped_refptr
<ResourcePrefetchPredictorTables
> tables
) {
308 Profile
* const profile_
;
309 ResourcePrefetchPredictorConfig
const config_
;
310 InitializationState initialization_state_
;
311 scoped_refptr
<ResourcePrefetchPredictorTables
> tables_
;
312 scoped_refptr
<ResourcePrefetcherManager
> prefetch_manager_
;
313 base::CancelableTaskTracker history_lookup_consumer_
;
315 // Map of all the navigations in flight to their resource requests.
316 NavigationMap inflight_navigations_
;
318 // Copy of the data in the predictor tables.
319 scoped_ptr
<PrefetchDataMap
> url_table_cache_
;
320 scoped_ptr
<PrefetchDataMap
> host_table_cache_
;
322 ResultsMap results_map_
;
323 STLValueDeleter
<ResultsMap
> results_map_deleter_
;
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_