OLD | NEW |
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 <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/scoped_observer.h" | 16 #include "base/scoped_observer.h" |
17 #include "base/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "chrome/browser/predictors/resource_prefetch_common.h" | 19 #include "chrome/browser/predictors/resource_prefetch_common.h" |
20 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 20 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
21 #include "chrome/browser/predictors/resource_prefetcher.h" | 21 #include "chrome/browser/predictors/resource_prefetcher.h" |
22 #include "components/history/core/browser/history_service_observer.h" | 22 #include "components/history/core/browser/history_service_observer.h" |
23 #include "components/history/core/browser/history_types.h" | 23 #include "components/history/core/browser/history_types.h" |
24 #include "components/keyed_service/core/keyed_service.h" | 24 #include "components/keyed_service/core/keyed_service.h" |
25 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
26 #include "content/public/browser/notification_registrar.h" | |
27 #include "content/public/common/resource_type.h" | 26 #include "content/public/common/resource_type.h" |
28 #include "url/gurl.h" | 27 #include "url/gurl.h" |
29 | 28 |
30 class PredictorsHandler; | 29 class PredictorsHandler; |
31 class Profile; | 30 class Profile; |
32 | 31 |
33 namespace content { | 32 namespace content { |
34 class WebContents; | 33 class WebContents; |
35 } | 34 } |
36 | 35 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the | 68 // * ResourcePrefetcher - Lives entirely on the IO thread, owned by the |
70 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 69 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
71 // | 70 // |
72 // TODO(shishir): Do speculative prefetching for https resources and/or https | 71 // TODO(shishir): Do speculative prefetching for https resources and/or https |
73 // main frame urls. | 72 // main frame urls. |
74 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 73 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
75 // recorded. Consider recording sub-frame responses independently or together | 74 // recorded. Consider recording sub-frame responses independently or together |
76 // with main frame. | 75 // with main frame. |
77 class ResourcePrefetchPredictor | 76 class ResourcePrefetchPredictor |
78 : public KeyedService, | 77 : public KeyedService, |
79 public content::NotificationObserver, | |
80 public history::HistoryServiceObserver, | 78 public history::HistoryServiceObserver, |
81 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { | 79 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { |
82 public: | 80 public: |
83 // Stores the data that we need to get from the URLRequest. | 81 // Stores the data that we need to get from the URLRequest. |
84 struct URLRequestSummary { | 82 struct URLRequestSummary { |
85 URLRequestSummary(); | 83 URLRequestSummary(); |
86 URLRequestSummary(const URLRequestSummary& other); | 84 URLRequestSummary(const URLRequestSummary& other); |
87 ~URLRequestSummary(); | 85 ~URLRequestSummary(); |
88 | 86 |
89 NavigationID navigation_id; | 87 NavigationID navigation_id; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 177 |
180 // Returns true if the main page request is supported for prediction. | 178 // Returns true if the main page request is supported for prediction. |
181 static bool IsHandledMainPage(net::URLRequest* request); | 179 static bool IsHandledMainPage(net::URLRequest* request); |
182 | 180 |
183 // Returns true if the subresource request is supported for prediction. | 181 // Returns true if the subresource request is supported for prediction. |
184 static bool IsHandledSubresource(net::URLRequest* request); | 182 static bool IsHandledSubresource(net::URLRequest* request); |
185 | 183 |
186 // Returns true if the request (should have a response in it) is cacheable. | 184 // Returns true if the request (should have a response in it) is cacheable. |
187 static bool IsCacheable(const net::URLRequest* request); | 185 static bool IsCacheable(const net::URLRequest* request); |
188 | 186 |
189 // content::NotificationObserver methods override. | |
190 void Observe(int type, | |
191 const content::NotificationSource& source, | |
192 const content::NotificationDetails& details) override; | |
193 | |
194 // KeyedService methods override. | 187 // KeyedService methods override. |
195 void Shutdown() override; | 188 void Shutdown() override; |
196 | 189 |
197 // Functions called on different network events pertaining to the loading of | 190 // Functions called on different network events pertaining to the loading of |
198 // main frame resource or sub resources. | 191 // main frame resource or sub resources. |
199 void OnMainFrameRequest(const URLRequestSummary& request); | 192 void OnMainFrameRequest(const URLRequestSummary& request); |
200 void OnMainFrameResponse(const URLRequestSummary& response); | 193 void OnMainFrameResponse(const URLRequestSummary& response); |
201 void OnMainFrameRedirect(const URLRequestSummary& response); | 194 void OnMainFrameRedirect(const URLRequestSummary& response); |
202 void OnSubresourceResponse(const URLRequestSummary& response); | 195 void OnSubresourceResponse(const URLRequestSummary& response); |
203 | 196 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 const std::vector<URLRequestSummary>& actual, | 283 const std::vector<URLRequestSummary>& actual, |
291 const ResourcePrefetcher::RequestVector& predicted) const; | 284 const ResourcePrefetcher::RequestVector& predicted) const; |
292 void ReportPredictedAccuracyStatsHelper( | 285 void ReportPredictedAccuracyStatsHelper( |
293 PrefetchKeyType key_type, | 286 PrefetchKeyType key_type, |
294 const ResourcePrefetcher::RequestVector& predicted, | 287 const ResourcePrefetcher::RequestVector& predicted, |
295 const std::map<GURL, bool>& actual, | 288 const std::map<GURL, bool>& actual, |
296 size_t total_resources_fetched_from_network, | 289 size_t total_resources_fetched_from_network, |
297 size_t max_assumed_prefetched) const; | 290 size_t max_assumed_prefetched) const; |
298 | 291 |
299 // history::HistoryServiceObserver: | 292 // history::HistoryServiceObserver: |
| 293 void OnURLsDeleted( |
| 294 HistoryService* history_service, |
| 295 const history::URLsDeletedDetails& deleted_details) override; |
300 void OnHistoryServiceLoaded(HistoryService* history_service) override; | 296 void OnHistoryServiceLoaded(HistoryService* history_service) override; |
301 | 297 |
302 // Used to connect to HistoryService or register for service loaded | 298 // Used to connect to HistoryService or register for service loaded |
303 // notificatioan. | 299 // notificatioan. |
304 void ConnectToHistoryService(); | 300 void ConnectToHistoryService(); |
305 | 301 |
306 // Used for testing to inject mock tables. | 302 // Used for testing to inject mock tables. |
307 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 303 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
308 tables_ = tables; | 304 tables_ = tables; |
309 } | 305 } |
310 | 306 |
311 Profile* const profile_; | 307 Profile* const profile_; |
312 ResourcePrefetchPredictorConfig const config_; | 308 ResourcePrefetchPredictorConfig const config_; |
313 InitializationState initialization_state_; | 309 InitializationState initialization_state_; |
314 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 310 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
315 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 311 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
316 content::NotificationRegistrar notification_registrar_; | |
317 base::CancelableTaskTracker history_lookup_consumer_; | 312 base::CancelableTaskTracker history_lookup_consumer_; |
318 | 313 |
319 // Map of all the navigations in flight to their resource requests. | 314 // Map of all the navigations in flight to their resource requests. |
320 NavigationMap inflight_navigations_; | 315 NavigationMap inflight_navigations_; |
321 | 316 |
322 // Copy of the data in the predictor tables. | 317 // Copy of the data in the predictor tables. |
323 scoped_ptr<PrefetchDataMap> url_table_cache_; | 318 scoped_ptr<PrefetchDataMap> url_table_cache_; |
324 scoped_ptr<PrefetchDataMap> host_table_cache_; | 319 scoped_ptr<PrefetchDataMap> host_table_cache_; |
325 | 320 |
326 ResultsMap results_map_; | 321 ResultsMap results_map_; |
327 STLValueDeleter<ResultsMap> results_map_deleter_; | 322 STLValueDeleter<ResultsMap> results_map_deleter_; |
328 | 323 |
329 ScopedObserver<HistoryService, history::HistoryServiceObserver> | 324 ScopedObserver<HistoryService, history::HistoryServiceObserver> |
330 history_service_observer_; | 325 history_service_observer_; |
331 | 326 |
332 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 327 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
333 }; | 328 }; |
334 | 329 |
335 } // namespace predictors | 330 } // namespace predictors |
336 | 331 |
337 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 332 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
OLD | NEW |