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 <utility> | 14 #include <utility> |
14 #include <vector> | 15 #include <vector> |
15 | 16 |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
18 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 20 #include "base/time/time.h" |
19 #include "chrome/browser/predictors/resource_prefetch_common.h" | 21 #include "chrome/browser/predictors/resource_prefetch_common.h" |
20 #include "net/url_request/redirect_info.h" | 22 #include "net/url_request/redirect_info.h" |
21 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
23 | 25 |
24 namespace net { | 26 namespace net { |
25 class URLRequestContext; | 27 class URLRequestContext; |
26 } | 28 } |
27 | 29 |
28 namespace predictors { | 30 namespace predictors { |
29 | 31 |
30 namespace internal { | 32 namespace internal { |
31 constexpr char kResourcePrefetchPredictorCachePatternHistogram[] = | 33 constexpr char kResourcePrefetchPredictorCachePatternHistogram[] = |
32 "ResourcePrefetchPredictor.CachePattern"; | 34 "ResourcePrefetchPredictor.CachePattern"; |
33 constexpr char kResourcePrefetchPredictorPrefetchedCountHistogram[] = | 35 constexpr char kResourcePrefetchPredictorPrefetchedCountHistogram[] = |
34 "ResourcePrefetchPredictor.PrefetchedCount"; | 36 "ResourcePrefetchPredictor.PrefetchedCount"; |
35 constexpr char kResourcePrefetchPredictorPrefetchedSizeHistogram[] = | 37 constexpr char kResourcePrefetchPredictorPrefetchedSizeHistogram[] = |
36 "ResourcePrefetchPredictor.PrefetchedSizeKB"; | 38 "ResourcePrefetchPredictor.PrefetchedSizeKB"; |
37 } // namespace internal | 39 } // namespace internal |
38 | 40 |
39 // Responsible for prefetching resources for a single main frame URL based on | 41 // Responsible for prefetching resources for a single main frame URL based on |
40 // the input list of resources. | 42 // the input list of resources. |
41 // - Limits the max number of resources in flight for any host and also across | 43 // - Limits the max number of resources in flight for any host and also across |
42 // hosts. | 44 // hosts. |
43 // - When stopped, will wait for the pending requests to finish. | 45 // - When stopped, will wait for the pending requests to finish. |
44 // - Lives entirely on the IO thread. | 46 // - Lives entirely on the IO thread. |
45 class ResourcePrefetcher : public net::URLRequest::Delegate { | 47 class ResourcePrefetcher : public net::URLRequest::Delegate { |
46 public: | 48 public: |
| 49 struct PrefetchedRequestStats { |
| 50 PrefetchedRequestStats(const GURL& resource_url, |
| 51 bool was_cached, |
| 52 size_t total_received_bytes); |
| 53 ~PrefetchedRequestStats(); |
| 54 |
| 55 GURL resource_url; |
| 56 bool was_cached; |
| 57 size_t total_received_bytes; |
| 58 }; |
| 59 |
| 60 struct PrefetcherStats { |
| 61 explicit PrefetcherStats(const GURL& url); |
| 62 ~PrefetcherStats(); |
| 63 PrefetcherStats(const PrefetcherStats& other); |
| 64 |
| 65 GURL url; |
| 66 base::TimeTicks start_time; |
| 67 std::vector<PrefetchedRequestStats> requests_stats; |
| 68 }; |
| 69 |
47 // Used to communicate when the prefetching is done. All methods are invoked | 70 // Used to communicate when the prefetching is done. All methods are invoked |
48 // on the IO thread. | 71 // on the IO thread. |
49 class Delegate { | 72 class Delegate { |
50 public: | 73 public: |
51 virtual ~Delegate() { } | 74 virtual ~Delegate() { } |
52 | 75 |
53 // Called when the ResourcePrefetcher is finished, i.e. there is nothing | 76 // Called when the ResourcePrefetcher is finished, i.e. there is nothing |
54 // pending in flight. | 77 // pending in flight. |
55 virtual void ResourcePrefetcherFinished(ResourcePrefetcher* prefetcher) = 0; | 78 virtual void ResourcePrefetcherFinished( |
| 79 ResourcePrefetcher* prefetcher, |
| 80 std::unique_ptr<PrefetcherStats> stats) = 0; |
56 | 81 |
57 virtual net::URLRequestContext* GetURLRequestContext() = 0; | 82 virtual net::URLRequestContext* GetURLRequestContext() = 0; |
58 }; | 83 }; |
59 | 84 |
60 // |delegate| has to outlive the ResourcePrefetcher. | 85 // |delegate| has to outlive the ResourcePrefetcher. |
61 ResourcePrefetcher(Delegate* delegate, | 86 ResourcePrefetcher(Delegate* delegate, |
62 const ResourcePrefetchPredictorConfig& config, | 87 const ResourcePrefetchPredictorConfig& config, |
63 const GURL& main_frame_url, | 88 const GURL& main_frame_url, |
64 const std::vector<GURL>& urls); | 89 const std::vector<GURL>& urls); |
65 ~ResourcePrefetcher() override; | 90 ~ResourcePrefetcher() override; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 GURL main_frame_url_; | 147 GURL main_frame_url_; |
123 | 148 |
124 // For histogram reports. | 149 // For histogram reports. |
125 size_t prefetched_count_; | 150 size_t prefetched_count_; |
126 int64_t prefetched_bytes_; | 151 int64_t prefetched_bytes_; |
127 | 152 |
128 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> | 153 std::map<net::URLRequest*, std::unique_ptr<net::URLRequest>> |
129 inflight_requests_; | 154 inflight_requests_; |
130 std::list<GURL> request_queue_; | 155 std::list<GURL> request_queue_; |
131 std::map<std::string, size_t> host_inflight_counts_; | 156 std::map<std::string, size_t> host_inflight_counts_; |
| 157 std::unique_ptr<PrefetcherStats> stats_; |
132 | 158 |
133 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); | 159 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetcher); |
134 }; | 160 }; |
135 | 161 |
136 } // namespace predictors | 162 } // namespace predictors |
137 | 163 |
138 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ | 164 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCHER_H_ |
OLD | NEW |