Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Side by Side Diff: components/precache/core/precache_fetcher.h

Issue 2229983002: Send the list of used and unused resources for precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added-test Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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,
sclittle 2016/08/16 21:05:05 nit: since these hash strings can get pretty long,
Raj 2016/08/18 00:23:23 hmm. I don't see any benefit. The strings will get
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
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 // Constructs a new PrecacheFetcher. The |unfinished_work| contains the
105 // prioritized list of hosts that the user commonly visits. These hosts are 124 // 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 125 // 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|. 126 // the user is likely to fetch. Takes ownership of |unfinished_work|.
108 PrecacheFetcher(net::URLRequestContextGetter* request_context, 127 // |precache_database| should be accessed on;y in |db_task_runner|.
109 const GURL& config_url, 128 PrecacheFetcher(
110 const std::string& manifest_url_prefix, 129 net::URLRequestContextGetter* request_context,
111 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, 130 const GURL& config_url,
112 uint32_t experiment_id, 131 const std::string& manifest_url_prefix,
113 PrecacheDelegate* precache_delegate); 132 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work,
133 uint32_t experiment_id,
134 const base::WeakPtr<PrecacheDatabase>& precache_database,
135 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner,
136 PrecacheDelegate* precache_delegate);
114 137
115 virtual ~PrecacheFetcher(); 138 virtual ~PrecacheFetcher();
116 139
117 // Starts fetching resources to precache. URLs are fetched sequentially. Can 140 // Starts fetching resources to precache. URLs are fetched sequentially. Can
118 // be called from any thread. Start should only be called once on a 141 // be called from any thread. Start should only be called once on a
119 // PrecacheFetcher instance. 142 // PrecacheFetcher instance.
120 void Start(); 143 void Start();
121 144
122 // Stops all precaching work. The PreacheFetcher should not be used after 145 // Stops all precaching work. The PreacheFetcher should not be used after
123 // calling this method. 146 // calling this method.
(...skipping 30 matching lines...) Expand all
154 // resource URLs to fetch according to the URLs in the manifest. If the fetch 177 // 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. 178 // of a manifest fails, then it skips to the next manifest.
156 void OnManifestFetchComplete(const Fetcher& source); 179 void OnManifestFetchComplete(const Fetcher& source);
157 180
158 // Called when a resource has been fetched. 181 // Called when a resource has been fetched.
159 void OnResourceFetchComplete(const Fetcher& source); 182 void OnResourceFetchComplete(const Fetcher& source);
160 183
161 // Adds up the response sizes. 184 // Adds up the response sizes.
162 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); 185 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes);
163 186
187 // Retrieves the manifest info on the DB thread. Manifest info for each of the
188 // hosts in |hosts_to_fetch|, is added to |hosts_info|.
189 void RetrieveManifestInfo(std::deque<std::string> hosts_to_fetch,
190 std::deque<ManifestHostInfo>* hosts_info);
191
192 // Callback invoked when the manifest info for all the top hosts is retrieved.
193 void OnManifestInfoRetrieved(
194 std::unique_ptr<std::deque<ManifestHostInfo>> manifests_info);
195
164 // The request context used when fetching URLs. 196 // The request context used when fetching URLs.
165 const scoped_refptr<net::URLRequestContextGetter> request_context_; 197 const scoped_refptr<net::URLRequestContextGetter> request_context_;
166 198
167 // The custom URL to use when fetching the config. If not provided, the 199 // The custom URL to use when fetching the config. If not provided, the
168 // default flag-specified URL will be used. 200 // default flag-specified URL will be used.
169 const GURL config_url_; 201 const GURL config_url_;
170 202
171 // The custom URL prefix to use when fetching manifests. If not provided, the 203 // The custom URL prefix to use when fetching manifests. If not provided, the
172 // default flag-specified prefix will be used. 204 // default flag-specified prefix will be used.
173 const std::string manifest_url_prefix_; 205 const std::string manifest_url_prefix_;
174 206
207 // PrecacheDatabase should be accessed on the DB thread.
208 base::WeakPtr<PrecacheDatabase> precache_database_;
209 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_;
210
175 // Non-owning pointer. Should not be NULL. 211 // Non-owning pointer. Should not be NULL.
176 PrecacheDelegate* precache_delegate_; 212 PrecacheDelegate* precache_delegate_;
177 213
178 std::list<GURL> manifest_urls_to_fetch_; 214 std::unique_ptr<std::deque<ManifestHostInfo>> top_hosts_to_fetch_;
sclittle 2016/08/16 21:05:05 nit: could you remove the std::unique_ptr here and
Raj 2016/08/18 00:23:23 Done.
179 std::list<GURL> resource_urls_to_fetch_; 215 std::deque<std::pair<GURL, std::string>> resources_to_fetch_;
180 216
181 FetcherPool<Fetcher> pool_; 217 FetcherPool<Fetcher> pool_;
182 218
183 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; 219 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
184 220
185 // The fieldtrial experiment ID. 221 // The fieldtrial experiment ID.
186 uint32_t experiment_id_; 222 uint32_t experiment_id_;
187 223
188 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); 224 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher);
189 }; 225 };
(...skipping 17 matching lines...) Expand all
207 // 243 //
208 // On completion it calls the given callback. This class cancels requests whose 244 // 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, 245 // responses are or will be larger than max_bytes. In such cases,
210 // network_url_fetcher() will return nullptr. 246 // network_url_fetcher() will return nullptr.
211 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { 247 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate {
212 public: 248 public:
213 // Construct a new Fetcher. This will create and start a new URLFetcher for 249 // Construct a new Fetcher. This will create and start a new URLFetcher for
214 // the specified URL using the specified request context. 250 // the specified URL using the specified request context.
215 Fetcher(net::URLRequestContextGetter* request_context, 251 Fetcher(net::URLRequestContextGetter* request_context,
216 const GURL& url, 252 const GURL& url,
253 const std::string& referrer,
217 const base::Callback<void(const Fetcher&)>& callback, 254 const base::Callback<void(const Fetcher&)>& callback,
218 bool is_resource_request, 255 bool is_resource_request,
219 size_t max_bytes); 256 size_t max_bytes);
220 ~Fetcher() override; 257 ~Fetcher() override;
221 void OnURLFetchDownloadProgress(const net::URLFetcher* source, 258 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
222 int64_t current, 259 int64_t current,
223 int64_t total) override; 260 int64_t total) override;
224 void OnURLFetchComplete(const net::URLFetcher* source) override; 261 void OnURLFetchComplete(const net::URLFetcher* source) override;
225 int64_t response_bytes() const { return response_bytes_; } 262 int64_t response_bytes() const { return response_bytes_; }
226 int64_t network_response_bytes() const { return network_response_bytes_; } 263 int64_t network_response_bytes() const { return network_response_bytes_; }
227 const net::URLFetcher* network_url_fetcher() const { 264 const net::URLFetcher* network_url_fetcher() const {
228 return network_url_fetcher_.get(); 265 return network_url_fetcher_.get();
229 } 266 }
230 const GURL& url() const { return url_; } 267 const GURL& url() const { return url_; }
268 const std::string& referrer() const { return referrer_; }
231 bool is_resource_request() const { return is_resource_request_; } 269 bool is_resource_request() const { return is_resource_request_; }
270 bool was_cached() const { return was_cached_; }
232 271
233 private: 272 private:
234 enum class FetchStage { CACHE, NETWORK }; 273 enum class FetchStage { CACHE, NETWORK };
235 274
236 void LoadFromCache(); 275 void LoadFromCache();
237 void LoadFromNetwork(); 276 void LoadFromNetwork();
238 277
239 net::URLRequestContextGetter* const request_context_; 278 net::URLRequestContextGetter* const request_context_;
240 const GURL url_; 279 const GURL url_;
280 const std::string referrer_;
241 const base::Callback<void(const Fetcher&)> callback_; 281 const base::Callback<void(const Fetcher&)> callback_;
242 const bool is_resource_request_; 282 const bool is_resource_request_;
243 const size_t max_bytes_; 283 const size_t max_bytes_;
244 284
245 FetchStage fetch_stage_; 285 FetchStage fetch_stage_;
246 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. 286 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing.
247 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; 287 std::unique_ptr<net::URLFetcher> cache_url_fetcher_;
248 std::unique_ptr<net::URLFetcher> network_url_fetcher_; 288 std::unique_ptr<net::URLFetcher> network_url_fetcher_;
249 int64_t response_bytes_; 289 int64_t response_bytes_;
250 int64_t network_response_bytes_; 290 int64_t network_response_bytes_;
291 bool was_cached_;
251 292
252 DISALLOW_COPY_AND_ASSIGN(Fetcher); 293 DISALLOW_COPY_AND_ASSIGN(Fetcher);
253 }; 294 };
254 295
255 } // namespace precache 296 } // namespace precache
256 297
257 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 298 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698