Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_LOADING_PREDICTOR_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ | 6 #define CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | |
| 10 #include <utility> | 11 #include <utility> |
| 11 | 12 |
| 12 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "chrome/browser/predictors/loading_data_collector.h" | 16 #include "chrome/browser/predictors/loading_data_collector.h" |
| 16 #include "chrome/browser/predictors/resource_prefetch_common.h" | 17 #include "chrome/browser/predictors/resource_prefetch_common.h" |
| 17 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 18 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 19 #include "chrome/browser/predictors/resource_prefetcher.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
| 19 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 20 | 23 |
| 21 class Profile; | 24 class Profile; |
| 22 | 25 |
| 23 namespace predictors { | 26 namespace predictors { |
| 24 | 27 |
| 25 class ResourcePrefetchPredictor; | 28 class ResourcePrefetchPredictor; |
| 26 class LoadingStatsCollector; | 29 class LoadingStatsCollector; |
| 30 class TestLoadingObserver; | |
| 27 | 31 |
| 28 // Entry point for the Loading predictor. | 32 // Entry point for the Loading predictor. |
| 29 // From a high-level request (GURL and motivation) and a database of historical | 33 // From a high-level request (GURL and motivation) and a database of historical |
| 30 // data, initiates predictive actions to speed up page loads. | 34 // data, initiates predictive actions to speed up page loads. |
| 31 // | 35 // |
| 32 // Also listens to main frame requests/redirects/responses to initiate and | 36 // Also listens to main frame requests/redirects/responses to initiate and |
| 33 // cancel page load hints. | 37 // cancel page load hints. |
| 34 // | 38 // |
| 35 // See ResourcePrefetchPredictor for a description of the resource prefetch | 39 // See ResourcePrefetchPredictor for a description of the resource prefetch |
| 36 // predictor. | 40 // predictor. |
| 37 // | 41 // |
| 38 // All methods must be called from the UI thread. | 42 // All methods must be called from the UI thread. |
| 39 class LoadingPredictor : public KeyedService { | 43 class LoadingPredictor : public KeyedService, |
| 44 public ResourcePrefetcher::Delegate { | |
| 40 public: | 45 public: |
| 41 LoadingPredictor(const LoadingPredictorConfig& config, Profile* profile); | 46 LoadingPredictor(const LoadingPredictorConfig& config, Profile* profile); |
| 42 ~LoadingPredictor() override; | 47 ~LoadingPredictor() override; |
| 43 | 48 |
| 44 // Hints that a page load is expected for |url|, with the hint coming from a | 49 // Hints that a page load is expected for |url|, with the hint coming from a |
| 45 // given |origin|. May trigger actions, such as prefetch and/or preconnect. | 50 // given |origin|. May trigger actions, such as prefetch and/or preconnect. |
| 46 void PrepareForPageLoad(const GURL& url, HintOrigin origin); | 51 void PrepareForPageLoad(const GURL& url, HintOrigin origin); |
| 47 | 52 |
| 48 // Indicates that a page load hint is no longer active. | 53 // Indicates that a page load hint is no longer active. |
| 49 void CancelPageLoadHint(const GURL& url); | 54 void CancelPageLoadHint(const GURL& url); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 62 const ResourcePrefetchPredictor::URLRequestSummary& summary); | 67 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 63 void OnMainFrameRedirect( | 68 void OnMainFrameRedirect( |
| 64 const ResourcePrefetchPredictor::URLRequestSummary& summary); | 69 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 65 void OnMainFrameResponse( | 70 void OnMainFrameResponse( |
| 66 const ResourcePrefetchPredictor::URLRequestSummary& summary); | 71 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 67 | 72 |
| 68 base::WeakPtr<LoadingPredictor> GetWeakPtr() { | 73 base::WeakPtr<LoadingPredictor> GetWeakPtr() { |
| 69 return weak_factory_.GetWeakPtr(); | 74 return weak_factory_.GetWeakPtr(); |
| 70 } | 75 } |
| 71 | 76 |
| 77 // ResourcePrefetcher::Delegate: | |
| 78 void ResourcePrefetcherFinished( | |
| 79 ResourcePrefetcher* prefetcher, | |
| 80 std::unique_ptr<ResourcePrefetcher::PrefetcherStats> stats) override; | |
| 81 | |
| 82 // Sets the |observer| to be notified when prefetches start and | |
| 83 // finish. A previously registered observer will be discarded. Call this with | |
| 84 // a nullptr parameter to de-register the observer. | |
| 85 void SetObserverForTesting(TestLoadingObserver* observer); | |
| 86 | |
| 72 private: | 87 private: |
| 73 // Cancels an active hint, from its iterator inside |active_hints_|. If the | 88 // Cancels an active hint, from its iterator inside |active_hints_|. If the |
| 74 // iterator is .end(), does nothing. Returns the iterator after deletion of | 89 // iterator is .end(), does nothing. Returns the iterator after deletion of |
| 75 // the entry. | 90 // the entry. |
| 76 std::map<GURL, base::TimeTicks>::iterator CancelActiveHint( | 91 std::map<GURL, base::TimeTicks>::iterator CancelActiveHint( |
| 77 std::map<GURL, base::TimeTicks>::iterator hint_it); | 92 std::map<GURL, base::TimeTicks>::iterator hint_it); |
| 78 void CleanupAbandonedHintsAndNavigations(const NavigationID& navigation_id); | 93 void CleanupAbandonedHintsAndNavigations(const NavigationID& navigation_id); |
| 79 | 94 |
| 95 // May start a prefetch for |url| with the data from |prediction|, and a | |
| 96 // given hint |origin|. A new prefetch may not start if there is already | |
| 97 // one in flight, for instance. | |
| 98 void MaybeAddPrefetch(const GURL& url, | |
| 99 const ResourcePrefetchPredictor::Prediction& prediction, | |
| 100 HintOrigin origin); | |
| 101 | |
| 102 // If a prefetch exists for |url|, stop it. | |
| 103 void MaybeRemovePrefetch(const GURL& url); | |
| 104 | |
| 80 // For testing. | 105 // For testing. |
| 81 void set_mock_resource_prefetch_predictor( | 106 void set_mock_resource_prefetch_predictor( |
| 82 std::unique_ptr<ResourcePrefetchPredictor> predictor) { | 107 std::unique_ptr<ResourcePrefetchPredictor> predictor) { |
| 83 resource_prefetch_predictor_ = std::move(predictor); | 108 resource_prefetch_predictor_ = std::move(predictor); |
| 84 } | 109 } |
| 85 | 110 |
| 86 LoadingPredictorConfig config_; | 111 LoadingPredictorConfig config_; |
| 87 Profile* profile_; | 112 Profile* profile_; |
| 88 std::unique_ptr<ResourcePrefetchPredictor> resource_prefetch_predictor_; | 113 std::unique_ptr<ResourcePrefetchPredictor> resource_prefetch_predictor_; |
| 89 std::unique_ptr<LoadingStatsCollector> stats_collector_; | 114 std::unique_ptr<LoadingStatsCollector> stats_collector_; |
| 90 std::unique_ptr<LoadingDataCollector> loading_data_collector_; | 115 std::unique_ptr<LoadingDataCollector> loading_data_collector_; |
| 91 std::map<GURL, base::TimeTicks> active_hints_; | 116 std::map<GURL, base::TimeTicks> active_hints_; |
| 92 // Initial URL. | 117 // Initial URL. |
| 93 std::map<NavigationID, GURL> active_navigations_; | 118 std::map<NavigationID, GURL> active_navigations_; |
| 119 // The value is {prefetcher, already deleted}. | |
| 120 std::map<std::string, std::pair<std::unique_ptr<ResourcePrefetcher>, bool>> | |
| 121 prefetches_; | |
|
alexilin
2017/07/03 14:55:02
I think |active_navigations_| and |prefetches_| co
Benoit L
2017/07/03 15:43:15
It does not seem entirely clear to me that merging
| |
| 122 TestLoadingObserver* observer_; | |
| 94 | 123 |
| 95 friend class LoadingPredictorTest; | 124 friend class LoadingPredictorTest; |
| 96 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, | 125 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 97 TestMainFrameResponseCancelsHint); | 126 TestMainFrameResponseCancelsHint); |
| 98 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, | 127 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 99 TestMainFrameRequestCancelsStaleNavigations); | 128 TestMainFrameRequestCancelsStaleNavigations); |
| 100 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, | 129 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 101 TestMainFrameResponseClearsNavigations); | 130 TestMainFrameResponseClearsNavigations); |
| 102 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, | 131 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 103 TestMainFrameRequestDoesntCancelExternalHint); | 132 TestMainFrameRequestDoesntCancelExternalHint); |
| 104 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, | 133 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 105 TestDontTrackNonPrefetchableUrls); | 134 TestDontTrackNonPrefetchableUrls); |
| 106 | 135 |
| 107 base::WeakPtrFactory<LoadingPredictor> weak_factory_; | 136 base::WeakPtrFactory<LoadingPredictor> weak_factory_; |
| 108 | 137 |
| 109 DISALLOW_COPY_AND_ASSIGN(LoadingPredictor); | 138 DISALLOW_COPY_AND_ASSIGN(LoadingPredictor); |
| 110 }; | 139 }; |
| 111 | 140 |
| 141 // An interface used to notify that data in the LoadingPredictor has | |
| 142 // changed. All methods are called on the UI thread. | |
| 143 class TestLoadingObserver { | |
| 144 public: | |
| 145 // De-registers itself from |predictor_| on destruction. | |
| 146 virtual ~TestLoadingObserver(); | |
| 147 | |
| 148 virtual void OnPrefetchingStarted(const GURL& main_frame_url) {} | |
| 149 | |
| 150 virtual void OnPrefetchingStopped(const GURL& main_frame_url) {} | |
| 151 | |
| 152 virtual void OnPrefetchingFinished(const GURL& main_frame_url) {} | |
| 153 | |
| 154 protected: | |
| 155 // |predictor| must be non-NULL and has to outlive the LoadingTestObserver. | |
| 156 // Also the predictor must not have a LoadingTestObserver set. | |
| 157 explicit TestLoadingObserver(LoadingPredictor* predictor); | |
| 158 | |
| 159 private: | |
| 160 LoadingPredictor* predictor_; | |
| 161 | |
| 162 DISALLOW_COPY_AND_ASSIGN(TestLoadingObserver); | |
| 163 }; | |
| 164 | |
| 112 } // namespace predictors | 165 } // namespace predictors |
| 113 | 166 |
| 114 #endif // CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ | 167 #endif // CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ |
| OLD | NEW |