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/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "chrome/browser/predictors/resource_prefetch_common.h" | 19 #include "chrome/browser/predictors/resource_prefetch_common.h" |
19 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 20 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
20 #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" | |
21 #include "components/history/core/browser/history_types.h" | 23 #include "components/history/core/browser/history_types.h" |
22 #include "components/keyed_service/core/keyed_service.h" | 24 #include "components/keyed_service/core/keyed_service.h" |
23 #include "content/public/browser/notification_observer.h" | 25 #include "content/public/browser/notification_observer.h" |
24 #include "content/public/browser/notification_registrar.h" | 26 #include "content/public/browser/notification_registrar.h" |
25 #include "content/public/common/resource_type.h" | 27 #include "content/public/common/resource_type.h" |
26 #include "url/gurl.h" | 28 #include "url/gurl.h" |
27 | 29 |
28 class PredictorsHandler; | 30 class PredictorsHandler; |
29 class Profile; | 31 class Profile; |
30 | 32 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. | 70 // ResourcePrefetcherManager, and issues net::URLRequest to fetch resources. |
69 // | 71 // |
70 // TODO(shishir): Do speculative prefetching for https resources and/or https | 72 // TODO(shishir): Do speculative prefetching for https resources and/or https |
71 // main frame urls. | 73 // main frame urls. |
72 // TODO(zhenw): Currently only main frame requests/redirects/responses are | 74 // TODO(zhenw): Currently only main frame requests/redirects/responses are |
73 // recorded. Consider recording sub-frame responses independently or together | 75 // recorded. Consider recording sub-frame responses independently or together |
74 // with main frame. | 76 // with main frame. |
75 class ResourcePrefetchPredictor | 77 class ResourcePrefetchPredictor |
76 : public KeyedService, | 78 : public KeyedService, |
77 public content::NotificationObserver, | 79 public content::NotificationObserver, |
80 public history::HistoryServiceObserver, | |
78 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { | 81 public base::SupportsWeakPtr<ResourcePrefetchPredictor> { |
79 public: | 82 public: |
80 // Stores the data that we need to get from the URLRequest. | 83 // Stores the data that we need to get from the URLRequest. |
81 struct URLRequestSummary { | 84 struct URLRequestSummary { |
82 URLRequestSummary(); | 85 URLRequestSummary(); |
83 URLRequestSummary(const URLRequestSummary& other); | 86 URLRequestSummary(const URLRequestSummary& other); |
84 ~URLRequestSummary(); | 87 ~URLRequestSummary(); |
85 | 88 |
86 NavigationID navigation_id; | 89 NavigationID navigation_id; |
87 GURL resource_url; | 90 GURL resource_url; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 // Called when the main frame of a page completes loading. | 122 // Called when the main frame of a page completes loading. |
120 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); | 123 void RecordMainFrameLoadComplete(const NavigationID& navigation_id); |
121 | 124 |
122 // Called by ResourcePrefetcherManager to notify that prefetching has finished | 125 // Called by ResourcePrefetcherManager to notify that prefetching has finished |
123 // for a navigation. Should take ownership of |requests|. | 126 // for a navigation. Should take ownership of |requests|. |
124 virtual void FinishedPrefetchForNavigation( | 127 virtual void FinishedPrefetchForNavigation( |
125 const NavigationID& navigation_id, | 128 const NavigationID& navigation_id, |
126 PrefetchKeyType key_type, | 129 PrefetchKeyType key_type, |
127 ResourcePrefetcher::RequestVector* requests); | 130 ResourcePrefetcher::RequestVector* requests); |
128 | 131 |
132 // history::HistoryServiceObserver: | |
133 void OnHistoryServiceLoaded(HistoryService* history_service) override; | |
134 | |
129 private: | 135 private: |
130 friend class ::PredictorsHandler; | 136 friend class ::PredictorsHandler; |
131 friend class ResourcePrefetchPredictorTest; | 137 friend class ResourcePrefetchPredictorTest; |
132 | 138 |
133 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); | 139 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, DeleteUrls); |
134 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 140 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
135 LazilyInitializeEmpty); | 141 LazilyInitializeEmpty); |
136 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 142 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
137 LazilyInitializeWithData); | 143 LazilyInitializeWithData); |
138 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, | 144 FRIEND_TEST_ALL_PREFIXES(ResourcePrefetchPredictorTest, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 const ResourcePrefetcher::RequestVector& predicted, | 287 const ResourcePrefetcher::RequestVector& predicted, |
282 const std::map<GURL, bool>& actual, | 288 const std::map<GURL, bool>& actual, |
283 size_t total_resources_fetched_from_network, | 289 size_t total_resources_fetched_from_network, |
284 size_t max_assumed_prefetched) const; | 290 size_t max_assumed_prefetched) const; |
285 | 291 |
286 // Used for testing to inject mock tables. | 292 // Used for testing to inject mock tables. |
287 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { | 293 void set_mock_tables(scoped_refptr<ResourcePrefetchPredictorTables> tables) { |
288 tables_ = tables; | 294 tables_ = tables; |
289 } | 295 } |
290 | 296 |
297 // If HistoryService is loaded then calls the Initialization function | |
298 // else Observe for the HistoryService to be loaded. | |
sdefresne
2014/11/03 19:14:13
nit: s/Observe/wait for OnHistoryLoaded event/ may
nshaik
2014/11/04 00:13:05
Done.
| |
299 bool CheckForHistoryService(); | |
300 | |
291 Profile* const profile_; | 301 Profile* const profile_; |
292 ResourcePrefetchPredictorConfig const config_; | 302 ResourcePrefetchPredictorConfig const config_; |
293 InitializationState initialization_state_; | 303 InitializationState initialization_state_; |
294 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 304 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
295 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; | 305 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; |
296 content::NotificationRegistrar notification_registrar_; | 306 content::NotificationRegistrar notification_registrar_; |
297 base::CancelableTaskTracker history_lookup_consumer_; | 307 base::CancelableTaskTracker history_lookup_consumer_; |
298 | 308 |
299 // Map of all the navigations in flight to their resource requests. | 309 // Map of all the navigations in flight to their resource requests. |
300 NavigationMap inflight_navigations_; | 310 NavigationMap inflight_navigations_; |
301 | 311 |
302 // Copy of the data in the predictor tables. | 312 // Copy of the data in the predictor tables. |
303 scoped_ptr<PrefetchDataMap> url_table_cache_; | 313 scoped_ptr<PrefetchDataMap> url_table_cache_; |
304 scoped_ptr<PrefetchDataMap> host_table_cache_; | 314 scoped_ptr<PrefetchDataMap> host_table_cache_; |
305 | 315 |
306 ResultsMap results_map_; | 316 ResultsMap results_map_; |
307 STLValueDeleter<ResultsMap> results_map_deleter_; | 317 STLValueDeleter<ResultsMap> results_map_deleter_; |
308 | 318 |
319 ScopedObserver<HistoryService, history::HistoryServiceObserver> | |
320 history_service_observer_; | |
321 | |
309 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); | 322 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictor); |
310 }; | 323 }; |
311 | 324 |
312 } // namespace predictors | 325 } // namespace predictors |
313 | 326 |
314 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ | 327 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ |
OLD | NEW |