OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 5 #ifndef COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 6 #define COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <deque> |
11 #include <list> | |
12 #include <memory> | 11 #include <memory> |
13 #include <string> | 12 #include <string> |
14 #include <vector> | 13 #include <vector> |
15 | 14 |
16 #include "base/callback.h" | 15 #include "base/callback.h" |
17 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | |
18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "components/precache/core/fetcher_pool.h" | 21 #include "components/precache/core/fetcher_pool.h" |
22 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
23 #include "net/url_request/url_fetcher_delegate.h" | 23 #include "net/url_request/url_fetcher_delegate.h" |
24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
28 } | 28 } |
29 | 29 |
30 namespace net { | 30 namespace net { |
31 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
32 } | 32 } |
33 | 33 |
34 namespace precache { | 34 namespace precache { |
35 | 35 |
36 class PrecacheConfigurationSettings; | 36 class PrecacheConfigurationSettings; |
37 class PrecacheDatabase; | |
37 class PrecacheUnfinishedWork; | 38 class PrecacheUnfinishedWork; |
38 | 39 |
39 // Visible for testing. | 40 // Visible for testing. |
40 extern const int kNoTracking; | 41 extern const int kNoTracking; |
41 | 42 |
43 // Contains the information about manifest for a host. | |
44 struct ManifestHostInfo { | |
45 ManifestHostInfo(int64_t manifest_id, | |
46 const std::string& hostname, | |
47 const std::string& used_url_hash, | |
48 const std::string& unused_url_hash); | |
49 ManifestHostInfo(ManifestHostInfo&&); | |
50 ManifestHostInfo& operator=(ManifestHostInfo&&); | |
51 | |
52 ~ManifestHostInfo(); | |
53 | |
54 int64_t manifest_id; | |
55 std::string hostname; | |
56 GURL manifest_url; | |
57 std::string used_url_hash; | |
58 std::string unused_url_hash; | |
59 }; | |
60 | |
42 // Public interface to code that fetches resources that the user is likely to | 61 // Public interface to code that fetches resources that the user is likely to |
43 // want to fetch in the future, putting them in the network stack disk cache. | 62 // want to fetch in the future, putting them in the network stack disk cache. |
44 // Precaching is intended to be done when Chrome is not actively in use, likely | 63 // Precaching is intended to be done when Chrome is not actively in use, likely |
45 // hours ahead of the time when the resources are actually needed. | 64 // hours ahead of the time when the resources are actually needed. |
46 // | 65 // |
47 // This class takes as input a prioritized list of URL domains that the user | 66 // This class takes as input a prioritized list of URL domains that the user |
48 // commonly visits, referred to as starting hosts. This class interacts with a | 67 // commonly visits, referred to as starting hosts. This class interacts with a |
49 // server, sending it the list of starting hosts sequentially. For each starting | 68 // server, sending it the list of starting hosts sequentially. For each starting |
50 // host, the server returns a manifest of resource URLs that are good candidates | 69 // host, the server returns a manifest of resource URLs that are good candidates |
51 // for precaching. Every resource returned is fetched, and responses are cached | 70 // for precaching. Every resource returned is fetched, and responses are cached |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 }; | 113 }; |
95 | 114 |
96 // Visible for testing. | 115 // Visible for testing. |
97 class Fetcher; | 116 class Fetcher; |
98 | 117 |
99 static void RecordCompletionStatistics( | 118 static void RecordCompletionStatistics( |
100 const PrecacheUnfinishedWork& unfinished_work, | 119 const PrecacheUnfinishedWork& unfinished_work, |
101 size_t remaining_manifest_urls_to_fetch, | 120 size_t remaining_manifest_urls_to_fetch, |
102 size_t remaining_resource_urls_to_fetch); | 121 size_t remaining_resource_urls_to_fetch); |
103 | 122 |
104 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a | 123 static std::string GetResourceURLBase64HashForTesting( |
124 const std::vector<GURL>& urls); | |
125 | |
126 // Constructs a new PrecacheFetcher. The |unfinished_work| contains the | |
105 // prioritized list of hosts that the user commonly visits. These hosts are | 127 // prioritized list of hosts that the user commonly visits. These hosts are |
106 // used by a server side component to construct a list of resource URLs that | 128 // used by a server side component to construct a list of resource URLs that |
107 // the user is likely to fetch. Takes ownership of |unfinished_work|. | 129 // the user is likely to fetch. Takes ownership of |unfinished_work|. |
108 PrecacheFetcher(net::URLRequestContextGetter* request_context, | 130 // |precache_database| should be accessed on;y in |db_task_runner|. |
sclittle
2016/08/15 20:13:10
nit: fix comment, it says "on;y"
Raj
2016/08/16 17:54:37
Done.
| |
109 const GURL& config_url, | 131 PrecacheFetcher( |
110 const std::string& manifest_url_prefix, | 132 net::URLRequestContextGetter* request_context, |
111 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, | 133 const GURL& config_url, |
112 uint32_t experiment_id, | 134 const std::string& manifest_url_prefix, |
113 PrecacheDelegate* precache_delegate); | 135 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, |
136 uint32_t experiment_id, | |
137 const base::WeakPtr<PrecacheDatabase>& precache_database, | |
138 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner, | |
139 PrecacheDelegate* precache_delegate); | |
114 | 140 |
115 virtual ~PrecacheFetcher(); | 141 virtual ~PrecacheFetcher(); |
116 | 142 |
117 // Starts fetching resources to precache. URLs are fetched sequentially. Can | 143 // Starts fetching resources to precache. URLs are fetched sequentially. Can |
118 // be called from any thread. Start should only be called once on a | 144 // be called from any thread. Start should only be called once on a |
119 // PrecacheFetcher instance. | 145 // PrecacheFetcher instance. |
120 void Start(); | 146 void Start(); |
121 | 147 |
122 // Stops all precaching work. The PreacheFetcher should not be used after | 148 // Stops all precaching work. The PreacheFetcher should not be used after |
123 // calling this method. | 149 // calling this method. |
(...skipping 30 matching lines...) Expand all Loading... | |
154 // resource URLs to fetch according to the URLs in the manifest. If the fetch | 180 // resource URLs to fetch according to the URLs in the manifest. If the fetch |
155 // of a manifest fails, then it skips to the next manifest. | 181 // of a manifest fails, then it skips to the next manifest. |
156 void OnManifestFetchComplete(const Fetcher& source); | 182 void OnManifestFetchComplete(const Fetcher& source); |
157 | 183 |
158 // Called when a resource has been fetched. | 184 // Called when a resource has been fetched. |
159 void OnResourceFetchComplete(const Fetcher& source); | 185 void OnResourceFetchComplete(const Fetcher& source); |
160 | 186 |
161 // Adds up the response sizes. | 187 // Adds up the response sizes. |
162 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); | 188 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); |
163 | 189 |
190 // Retrieves the manifest info on the DB thread. Manifest info for each of the | |
191 // hosts in |hosts_to_fetch|, is added to |hosts_info|. | |
192 void RetrieveManifestInfo(std::deque<std::string> hosts_to_fetch, | |
193 std::deque<ManifestHostInfo>* hosts_info); | |
194 | |
195 // Callback invoked when the manifest info for all the top hosts is retrieved. | |
196 void OnManifestInfoRetrieved( | |
197 std::unique_ptr<std::deque<ManifestHostInfo>> manifests_info); | |
198 | |
164 // The request context used when fetching URLs. | 199 // The request context used when fetching URLs. |
165 const scoped_refptr<net::URLRequestContextGetter> request_context_; | 200 const scoped_refptr<net::URLRequestContextGetter> request_context_; |
166 | 201 |
167 // The custom URL to use when fetching the config. If not provided, the | 202 // The custom URL to use when fetching the config. If not provided, the |
168 // default flag-specified URL will be used. | 203 // default flag-specified URL will be used. |
169 const GURL config_url_; | 204 const GURL config_url_; |
170 | 205 |
171 // The custom URL prefix to use when fetching manifests. If not provided, the | 206 // The custom URL prefix to use when fetching manifests. If not provided, the |
172 // default flag-specified prefix will be used. | 207 // default flag-specified prefix will be used. |
173 const std::string manifest_url_prefix_; | 208 const std::string manifest_url_prefix_; |
174 | 209 |
210 // PrecacheDatabase should be accessed on the DB thread. | |
211 base::WeakPtr<PrecacheDatabase> precache_database_; | |
212 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; | |
213 | |
175 // Non-owning pointer. Should not be NULL. | 214 // Non-owning pointer. Should not be NULL. |
176 PrecacheDelegate* precache_delegate_; | 215 PrecacheDelegate* precache_delegate_; |
177 | 216 |
178 std::list<GURL> manifest_urls_to_fetch_; | 217 std::unique_ptr<std::deque<ManifestHostInfo>> top_hosts_to_fetch_; |
179 std::list<GURL> resource_urls_to_fetch_; | 218 std::deque<std::pair<GURL, std::string>> resources_to_fetch_; |
180 | 219 |
181 FetcherPool<Fetcher> pool_; | 220 FetcherPool<Fetcher> pool_; |
182 | 221 |
183 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; | 222 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; |
184 | 223 |
185 // The fieldtrial experiment ID. | 224 // The fieldtrial experiment ID. |
186 uint32_t experiment_id_; | 225 uint32_t experiment_id_; |
187 | 226 |
188 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); | 227 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); |
189 }; | 228 }; |
(...skipping 17 matching lines...) Expand all Loading... | |
207 // | 246 // |
208 // On completion it calls the given callback. This class cancels requests whose | 247 // On completion it calls the given callback. This class cancels requests whose |
209 // responses are or will be larger than max_bytes. In such cases, | 248 // responses are or will be larger than max_bytes. In such cases, |
210 // network_url_fetcher() will return nullptr. | 249 // network_url_fetcher() will return nullptr. |
211 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { | 250 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { |
212 public: | 251 public: |
213 // Construct a new Fetcher. This will create and start a new URLFetcher for | 252 // Construct a new Fetcher. This will create and start a new URLFetcher for |
214 // the specified URL using the specified request context. | 253 // the specified URL using the specified request context. |
215 Fetcher(net::URLRequestContextGetter* request_context, | 254 Fetcher(net::URLRequestContextGetter* request_context, |
216 const GURL& url, | 255 const GURL& url, |
256 const std::string& referrer, | |
217 const base::Callback<void(const Fetcher&)>& callback, | 257 const base::Callback<void(const Fetcher&)>& callback, |
218 bool is_resource_request, | 258 bool is_resource_request, |
219 size_t max_bytes); | 259 size_t max_bytes); |
220 ~Fetcher() override; | 260 ~Fetcher() override; |
221 void OnURLFetchDownloadProgress(const net::URLFetcher* source, | 261 void OnURLFetchDownloadProgress(const net::URLFetcher* source, |
222 int64_t current, | 262 int64_t current, |
223 int64_t total) override; | 263 int64_t total) override; |
224 void OnURLFetchComplete(const net::URLFetcher* source) override; | 264 void OnURLFetchComplete(const net::URLFetcher* source) override; |
225 int64_t response_bytes() const { return response_bytes_; } | 265 int64_t response_bytes() const { return response_bytes_; } |
226 int64_t network_response_bytes() const { return network_response_bytes_; } | 266 int64_t network_response_bytes() const { return network_response_bytes_; } |
227 const net::URLFetcher* network_url_fetcher() const { | 267 const net::URLFetcher* network_url_fetcher() const { |
228 return network_url_fetcher_.get(); | 268 return network_url_fetcher_.get(); |
229 } | 269 } |
230 const GURL& url() const { return url_; } | 270 const GURL& url() const { return url_; } |
271 const std::string& referrer() const { return referrer_; } | |
231 bool is_resource_request() const { return is_resource_request_; } | 272 bool is_resource_request() const { return is_resource_request_; } |
273 bool was_cached() const { return was_cached_; } | |
232 | 274 |
233 private: | 275 private: |
234 enum class FetchStage { CACHE, NETWORK }; | 276 enum class FetchStage { CACHE, NETWORK }; |
235 | 277 |
236 void LoadFromCache(); | 278 void LoadFromCache(); |
237 void LoadFromNetwork(); | 279 void LoadFromNetwork(); |
238 | 280 |
239 net::URLRequestContextGetter* const request_context_; | 281 net::URLRequestContextGetter* const request_context_; |
240 const GURL url_; | 282 const GURL url_; |
283 const std::string referrer_; | |
241 const base::Callback<void(const Fetcher&)> callback_; | 284 const base::Callback<void(const Fetcher&)> callback_; |
242 const bool is_resource_request_; | 285 const bool is_resource_request_; |
243 const size_t max_bytes_; | 286 const size_t max_bytes_; |
244 | 287 |
245 FetchStage fetch_stage_; | 288 FetchStage fetch_stage_; |
246 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. | 289 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. |
247 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; | 290 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; |
248 std::unique_ptr<net::URLFetcher> network_url_fetcher_; | 291 std::unique_ptr<net::URLFetcher> network_url_fetcher_; |
249 int64_t response_bytes_; | 292 int64_t response_bytes_; |
250 int64_t network_response_bytes_; | 293 int64_t network_response_bytes_; |
294 bool was_cached_; | |
251 | 295 |
252 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 296 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
253 }; | 297 }; |
254 | 298 |
255 } // namespace precache | 299 } // namespace precache |
256 | 300 |
257 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ | 301 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ |
OLD | NEW |