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

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: Addressed nits Created 4 years, 3 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,
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 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 only in |db_task_runner|.
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
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 // Callback invoked when the manifest info for all the top hosts is retrieved.
191 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info);
192
164 // The request context used when fetching URLs. 193 // The request context used when fetching URLs.
165 const scoped_refptr<net::URLRequestContextGetter> request_context_; 194 const scoped_refptr<net::URLRequestContextGetter> request_context_;
166 195
167 // The custom URL to use when fetching the config. If not provided, the 196 // The custom URL to use when fetching the config. If not provided, the
168 // default flag-specified URL will be used. 197 // default flag-specified URL will be used.
169 const GURL config_url_; 198 const GURL config_url_;
170 199
171 // The custom URL prefix to use when fetching manifests. If not provided, the 200 // The custom URL prefix to use when fetching manifests. If not provided, the
172 // default flag-specified prefix will be used. 201 // default flag-specified prefix will be used.
173 const std::string manifest_url_prefix_; 202 const std::string manifest_url_prefix_;
174 203
204 // PrecacheDatabase should be accessed on the DB thread.
205 base::WeakPtr<PrecacheDatabase> precache_database_;
206 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_;
207
175 // Non-owning pointer. Should not be NULL. 208 // Non-owning pointer. Should not be NULL.
176 PrecacheDelegate* precache_delegate_; 209 PrecacheDelegate* precache_delegate_;
177 210
178 std::list<GURL> manifest_urls_to_fetch_; 211 std::deque<ManifestHostInfo> top_hosts_to_fetch_;
179 std::list<GURL> resource_urls_to_fetch_; 212 std::deque<std::pair<GURL, std::string>> resources_to_fetch_;
180 213
181 FetcherPool<Fetcher> pool_; 214 FetcherPool<Fetcher> pool_;
182 215
183 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; 216 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
184 217
185 // The fieldtrial experiment ID. 218 // The fieldtrial experiment ID.
186 uint32_t experiment_id_; 219 uint32_t experiment_id_;
187 220
188 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); 221 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher);
189 }; 222 };
(...skipping 17 matching lines...) Expand all
207 // 240 //
208 // On completion it calls the given callback. This class cancels requests whose 241 // 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, 242 // responses are or will be larger than max_bytes. In such cases,
210 // network_url_fetcher() will return nullptr. 243 // network_url_fetcher() will return nullptr.
211 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { 244 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate {
212 public: 245 public:
213 // Construct a new Fetcher. This will create and start a new URLFetcher for 246 // Construct a new Fetcher. This will create and start a new URLFetcher for
214 // the specified URL using the specified request context. 247 // the specified URL using the specified request context.
215 Fetcher(net::URLRequestContextGetter* request_context, 248 Fetcher(net::URLRequestContextGetter* request_context,
216 const GURL& url, 249 const GURL& url,
250 const std::string& referrer,
217 const base::Callback<void(const Fetcher&)>& callback, 251 const base::Callback<void(const Fetcher&)>& callback,
218 bool is_resource_request, 252 bool is_resource_request,
219 size_t max_bytes); 253 size_t max_bytes);
220 ~Fetcher() override; 254 ~Fetcher() override;
221 void OnURLFetchDownloadProgress(const net::URLFetcher* source, 255 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
222 int64_t current, 256 int64_t current,
223 int64_t total) override; 257 int64_t total) override;
224 void OnURLFetchComplete(const net::URLFetcher* source) override; 258 void OnURLFetchComplete(const net::URLFetcher* source) override;
225 int64_t response_bytes() const { return response_bytes_; } 259 int64_t response_bytes() const { return response_bytes_; }
226 int64_t network_response_bytes() const { return network_response_bytes_; } 260 int64_t network_response_bytes() const { return network_response_bytes_; }
227 const net::URLFetcher* network_url_fetcher() const { 261 const net::URLFetcher* network_url_fetcher() const {
228 return network_url_fetcher_.get(); 262 return network_url_fetcher_.get();
229 } 263 }
230 const GURL& url() const { return url_; } 264 const GURL& url() const { return url_; }
265 const std::string& referrer() const { return referrer_; }
231 bool is_resource_request() const { return is_resource_request_; } 266 bool is_resource_request() const { return is_resource_request_; }
267 bool was_cached() const { return was_cached_; }
232 268
233 private: 269 private:
234 enum class FetchStage { CACHE, NETWORK }; 270 enum class FetchStage { CACHE, NETWORK };
235 271
236 void LoadFromCache(); 272 void LoadFromCache();
237 void LoadFromNetwork(); 273 void LoadFromNetwork();
238 274
239 net::URLRequestContextGetter* const request_context_; 275 net::URLRequestContextGetter* const request_context_;
240 const GURL url_; 276 const GURL url_;
277 const std::string referrer_;
241 const base::Callback<void(const Fetcher&)> callback_; 278 const base::Callback<void(const Fetcher&)> callback_;
242 const bool is_resource_request_; 279 const bool is_resource_request_;
243 const size_t max_bytes_; 280 const size_t max_bytes_;
244 281
245 FetchStage fetch_stage_; 282 FetchStage fetch_stage_;
246 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. 283 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing.
247 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; 284 std::unique_ptr<net::URLFetcher> cache_url_fetcher_;
248 std::unique_ptr<net::URLFetcher> network_url_fetcher_; 285 std::unique_ptr<net::URLFetcher> network_url_fetcher_;
249 int64_t response_bytes_; 286 int64_t response_bytes_;
250 int64_t network_response_bytes_; 287 int64_t network_response_bytes_;
288 bool was_cached_;
251 289
252 DISALLOW_COPY_AND_ASSIGN(Fetcher); 290 DISALLOW_COPY_AND_ASSIGN(Fetcher);
253 }; 291 };
254 292
255 } // namespace precache 293 } // namespace precache
256 294
257 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 295 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW
« no previous file with comments | « components/precache/core/precache_database_unittest.cc ('k') | components/precache/core/precache_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698