| 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_PREFETCHER_H_ | 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "net/url_request/redirect_info.h" | 21 #include "net/url_request/redirect_info.h" |
| 21 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 namespace net { | |
| 25 class URLRequestContext; | |
| 26 } | |
| 27 | |
| 28 namespace predictors { | 26 namespace predictors { |
| 29 | 27 |
| 30 namespace internal { | 28 namespace internal { |
| 31 constexpr char kResourcePrefetchPredictorCachePatternHistogram[] = | 29 constexpr char kResourcePrefetchPredictorCachePatternHistogram[] = |
| 32 "ResourcePrefetchPredictor.CachePattern"; | 30 "ResourcePrefetchPredictor.CachePattern"; |
| 33 constexpr char kResourcePrefetchPredictorPrefetchedCountHistogram[] = | 31 constexpr char kResourcePrefetchPredictorPrefetchedCountHistogram[] = |
| 34 "ResourcePrefetchPredictor.PrefetchedCount"; | 32 "ResourcePrefetchPredictor.PrefetchedCount"; |
| 35 constexpr char kResourcePrefetchPredictorPrefetchedSizeHistogram[] = | 33 constexpr char kResourcePrefetchPredictorPrefetchedSizeHistogram[] = |
| 36 "ResourcePrefetchPredictor.PrefetchedSizeKB"; | 34 "ResourcePrefetchPredictor.PrefetchedSizeKB"; |
| 37 } // namespace internal | 35 } // namespace internal |
| 38 | 36 |
| 39 // Responsible for prefetching resources for a single main frame URL based on | 37 // Responsible for prefetching resources for a single main frame URL based on |
| 40 // the input list of resources. | 38 // the input list of resources. |
| 41 // - Limits the max number of resources in flight for any host and also across | 39 // - Limits the max number of resources in flight for any host and also across |
| 42 // hosts. | 40 // hosts. |
| 43 // - When stopped, will wait for the pending requests to finish. | 41 // - When stopped, will wait for the pending requests to finish. |
| 44 // - Lives entirely on the IO thread. | 42 // - Created on the UI thread, member functions called and instances destroyed |
| 43 // on the IO thread. |
| 45 class ResourcePrefetcher : public net::URLRequest::Delegate { | 44 class ResourcePrefetcher : public net::URLRequest::Delegate { |
| 46 public: | 45 public: |
| 47 struct PrefetchedRequestStats { | 46 struct PrefetchedRequestStats { |
| 48 PrefetchedRequestStats(const GURL& resource_url, | 47 PrefetchedRequestStats(const GURL& resource_url, |
| 49 bool was_cached, | 48 bool was_cached, |
| 50 size_t total_received_bytes); | 49 size_t total_received_bytes); |
| 51 ~PrefetchedRequestStats(); | 50 ~PrefetchedRequestStats(); |
| 52 | 51 |
| 53 GURL resource_url; | 52 GURL resource_url; |
| 54 bool was_cached; | 53 bool was_cached; |
| 55 size_t total_received_bytes; | 54 size_t total_received_bytes; |
| 56 }; | 55 }; |
| 57 | 56 |
| 58 struct PrefetcherStats { | 57 struct PrefetcherStats { |
| 59 explicit PrefetcherStats(const GURL& url); | 58 explicit PrefetcherStats(const GURL& url); |
| 60 ~PrefetcherStats(); | 59 ~PrefetcherStats(); |
| 61 PrefetcherStats(const PrefetcherStats& other); | 60 PrefetcherStats(const PrefetcherStats& other); |
| 62 | 61 |
| 63 GURL url; | 62 GURL url; |
| 64 base::TimeTicks start_time; | 63 base::TimeTicks start_time; |
| 65 std::vector<PrefetchedRequestStats> requests_stats; | 64 std::vector<PrefetchedRequestStats> requests_stats; |
| 66 }; | 65 }; |
| 67 | 66 |
| 68 // Used to communicate when the prefetching is done. All methods are invoked | 67 // Used to communicate when the prefetching is done. Lives on the UI thread. |
| 69 // on the IO thread. | |
| 70 class Delegate { | 68 class Delegate { |
| 71 public: | 69 public: |
| 72 virtual ~Delegate() { } | 70 virtual ~Delegate() {} |
| 73 | 71 |
| 74 // Called when the ResourcePrefetcher is finished, i.e. there is nothing | 72 // Called when the ResourcePrefetcher is finished, i.e. there is nothing |
| 75 // pending in flight. | 73 // pending in flight. |
| 76 virtual void ResourcePrefetcherFinished( | 74 virtual void ResourcePrefetcherFinished( |
| 77 ResourcePrefetcher* prefetcher, | 75 ResourcePrefetcher* prefetcher, |
| 78 std::unique_ptr<PrefetcherStats> stats) = 0; | 76 std::unique_ptr<PrefetcherStats> stats) = 0; |
| 79 | |
| 80 virtual net::URLRequestContext* GetURLRequestContext() = 0; | |
| 81 }; | 77 }; |
| 82 | 78 |
| 83 // |delegate| has to outlive the ResourcePrefetcher. | 79 ResourcePrefetcher(base::WeakPtr<Delegate> delegate, |
| 84 ResourcePrefetcher(Delegate* delegate, | 80 scoped_refptr<net::URLRequestContextGetter> context_getter, |
| 85 size_t max_concurrent_requests, | 81 size_t max_concurrent_requests, |
| 86 size_t max_concurrent_requests_per_host, | 82 size_t max_concurrent_requests_per_host, |
| 87 const GURL& main_frame_url, | 83 const GURL& main_frame_url, |
| 88 const std::vector<GURL>& urls); | 84 const std::vector<GURL>& urls); |
| 89 ~ResourcePrefetcher() override; | 85 ~ResourcePrefetcher() override; |
| 90 | 86 |
| 91 void Start(); // Kicks off the prefetching. Can only be called once. | 87 void Start(); // Kicks off the prefetching. Can only be called once. |
| 92 void Stop(); // No additional prefetches will be queued after this. | 88 void Stop(); // No additional prefetches will be queued after this. |
| 93 | 89 |
| 94 const GURL& main_frame_url() const { return main_frame_url_; } | 90 const GURL& main_frame_url() const { return main_frame_url_; } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 void OnResponseStarted(net::URLRequest* request, int net_error) override; | 128 void OnResponseStarted(net::URLRequest* request, int net_error) override; |
| 133 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | 129 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| 134 | 130 |
| 135 enum PrefetcherState { | 131 enum PrefetcherState { |
| 136 INITIALIZED = 0, // Prefetching hasn't started. | 132 INITIALIZED = 0, // Prefetching hasn't started. |
| 137 RUNNING = 1, // Prefetching started, allowed to add more requests. | 133 RUNNING = 1, // Prefetching started, allowed to add more requests. |
| 138 STOPPED = 2, // Prefetching started, not allowed to add more requests. | 134 STOPPED = 2, // Prefetching started, not allowed to add more requests. |
| 139 FINISHED = 3 // No more inflight request, new requests not possible. | 135 FINISHED = 3 // No more inflight request, new requests not possible. |
| 140 }; | 136 }; |
| 141 | 137 |
| 142 base::ThreadChecker thread_checker_; | |
| 143 PrefetcherState state_; | 138 PrefetcherState state_; |
| 144 Delegate* const delegate_; | 139 base::WeakPtr<Delegate> delegate_; |
| 140 scoped_refptr<net::URLRequestContextGetter> context_getter_; |
| 145 size_t max_concurrent_requests_; | 141 size_t max_concurrent_requests_; |
| 146 size_t max_concurrent_requests_per_host_; | 142 size_t max_concurrent_requests_per_host_; |
| 147 GURL main_frame_url_; | 143 GURL main_frame_url_; |
| 148 | 144 |
| 149 // For histogram reports. | 145 // For histogram reports. |
| 150 size_t prefetched_count_; | 146 size_t prefetched_count_; |
| 151 int64_t prefetched_bytes_; | 147 int64_t prefetched_bytes_; |
| 152 | 148 |
| 153 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> | 149 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> |
| 154 inflight_requests_; | 150 inflight_requests_; |
| 155 std::list<GURL> request_queue_; | 151 std::list<GURL> request_queue_; |
| 156 std::map<std::string, size_t> host_inflight_counts_; | 152 std::map<std::string, size_t> host_inflight_counts_; |
| 157 std::unique_ptr<PrefetcherStats> stats_; | 153 std::unique_ptr<PrefetcherStats> stats_; |
| 158 | 154 |
| 159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | 155 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); |
| 160 }; | 156 }; |
| 161 | 157 |
| 162 } // namespace predictors | 158 } // namespace predictors |
| 163 | 159 |
| 164 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 160 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
| OLD | NEW |