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 <memory> | 9 #include <memory> |
| 10 #include <utility> |
9 | 11 |
| 12 #include "base/gtest_prod_util.h" |
10 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" |
11 #include "chrome/browser/predictors/resource_prefetch_common.h" | 15 #include "chrome/browser/predictors/resource_prefetch_common.h" |
12 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 16 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
13 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "url/gurl.h" |
14 | 19 |
15 class Profile; | 20 class Profile; |
16 | 21 |
17 namespace predictors { | 22 namespace predictors { |
18 | 23 |
19 class ResourcePrefetchPredictor; | 24 class ResourcePrefetchPredictor; |
20 | 25 |
21 // Entry point for the Loading predictor. | 26 // Entry point for the Loading predictor. |
22 // From a high-level request (GURL and motivation) and a database of historical | 27 // From a high-level request (GURL and motivation) and a database of historical |
23 // data, initiates predictive actions to speed up page loads. | 28 // data, initiates predictive actions to speed up page loads. |
24 // | 29 // |
| 30 // Also listens to main frame requests/redirects/responses to initiate and |
| 31 // cancel page load hints. |
| 32 // |
25 // See ResourcePrefetchPredictor for a description of the resource prefetch | 33 // See ResourcePrefetchPredictor for a description of the resource prefetch |
26 // predictor. | 34 // predictor. |
27 // | 35 // |
28 // All methods must be called from the UI thread. | 36 // All methods must be called from the UI thread. |
29 class LoadingPredictor : public KeyedService, | 37 class LoadingPredictor : public KeyedService, |
30 public base::SupportsWeakPtr<LoadingPredictor> { | 38 public base::SupportsWeakPtr<LoadingPredictor> { |
31 public: | 39 public: |
32 LoadingPredictor(const LoadingPredictorConfig& config, Profile* profile); | 40 LoadingPredictor(const LoadingPredictorConfig& config, Profile* profile); |
33 ~LoadingPredictor() override; | 41 ~LoadingPredictor() override; |
34 | 42 |
35 // Hints that a page load is expected for |url|, with the hint coming from a | 43 // Hints that a page load is expected for |url|, with the hint coming from a |
36 // given |origin|. May trigger actions, such as prefetch and/or preconnect. | 44 // given |origin|. May trigger actions, such as prefetch and/or preconnect. |
37 void PrepareForPageLoad(const GURL& url, HintOrigin origin); | 45 void PrepareForPageLoad(const GURL& url, HintOrigin origin); |
38 | 46 |
39 // Indicates that a page load hint is no longer active. | 47 // Indicates that a page load hint is no longer active. |
40 void CancelPageLoadHint(const GURL& url); | 48 void CancelPageLoadHint(const GURL& url); |
41 | 49 |
42 // Starts initialization, will complete asynchronously. | 50 // Starts initialization, will complete asynchronously. |
43 void StartInitialization(); | 51 void StartInitialization(); |
44 | 52 |
45 // Don't use, internal only. | 53 // Don't use, internal only. |
46 ResourcePrefetchPredictor* resource_prefetch_predictor() const; | 54 ResourcePrefetchPredictor* resource_prefetch_predictor() const; |
47 | 55 |
48 // KeyedService: | 56 // KeyedService: |
49 void Shutdown() override; | 57 void Shutdown() override; |
50 | 58 |
| 59 void OnMainFrameRequest( |
| 60 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 61 void OnMainFrameRedirect( |
| 62 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 63 void OnMainFrameResponse( |
| 64 const ResourcePrefetchPredictor::URLRequestSummary& summary); |
| 65 |
51 private: | 66 private: |
| 67 // Cancels an active hint, from its iterator inside |active_hints_|. If the |
| 68 // iterator is .end(), does nothing. Returns the iterator after deletion of |
| 69 // the entry. |
| 70 std::map<GURL, base::TimeTicks>::iterator CancelActiveHint( |
| 71 std::map<GURL, base::TimeTicks>::iterator hint_it); |
| 72 void CleanupAbandonedHintsAndNavigations(const NavigationID& navigation_id); |
| 73 |
| 74 // For testing. |
| 75 void set_mock_resource_prefetch_predictor( |
| 76 std::unique_ptr<ResourcePrefetchPredictor> predictor) { |
| 77 resource_prefetch_predictor_ = std::move(predictor); |
| 78 } |
| 79 |
| 80 LoadingPredictorConfig config_; |
| 81 Profile* profile_; |
52 std::unique_ptr<ResourcePrefetchPredictor> resource_prefetch_predictor_; | 82 std::unique_ptr<ResourcePrefetchPredictor> resource_prefetch_predictor_; |
| 83 std::map<GURL, base::TimeTicks> active_hints_; |
| 84 // Initial URL. |
| 85 std::map<NavigationID, GURL> active_navigations_; |
| 86 |
| 87 friend class LoadingPredictorTest; |
| 88 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 89 TestMainFrameResponseCancelsHint); |
| 90 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 91 TestMainFrameRequestCancelsStaleNavigations); |
| 92 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 93 TestMainFrameResponseClearsNavigations); |
| 94 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 95 TestMainFrameRequestDoesntCancelExternalHint); |
| 96 FRIEND_TEST_ALL_PREFIXES(LoadingPredictorTest, |
| 97 TestDontTrackNonPrefetchableUrls); |
53 | 98 |
54 DISALLOW_COPY_AND_ASSIGN(LoadingPredictor); | 99 DISALLOW_COPY_AND_ASSIGN(LoadingPredictor); |
55 }; | 100 }; |
56 | 101 |
57 } // namespace predictors | 102 } // namespace predictors |
58 | 103 |
59 #endif // CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ | 104 #endif // CHROME_BROWSER_PREDICTORS_LOADING_PREDICTOR_H_ |
OLD | NEW |