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

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

Issue 2403193002: Precache: Optionally rank resources-to-precache globally. (Closed)
Patch Set: Created 4 years, 2 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 <deque> 10 #include <deque>
11 #include <list>
11 #include <memory> 12 #include <memory>
12 #include <string> 13 #include <string>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/callback.h" 16 #include "base/callback.h"
16 #include "base/macros.h" 17 #include "base/macros.h"
17 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h" 19 #include "base/memory/weak_ptr.h"
19 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
(...skipping 12 matching lines...) Expand all
33 } 34 }
34 35
35 namespace precache { 36 namespace precache {
36 37
37 class PrecacheConfigurationSettings; 38 class PrecacheConfigurationSettings;
38 class PrecacheDatabase; 39 class PrecacheDatabase;
39 class PrecacheUnfinishedWork; 40 class PrecacheUnfinishedWork;
40 41
41 // Visible for testing. 42 // Visible for testing.
42 extern const int kNoTracking; 43 extern const int kNoTracking;
44 extern const int kMaxParallelFetches;
43 45
44 // Contains the information about manifest for a host. 46 // Information about the manifest for a host.
45 struct ManifestHostInfo { 47 struct ManifestHostInfo {
46 ManifestHostInfo(int64_t manifest_id, 48 ManifestHostInfo(int64_t manifest_id,
47 const std::string& hostname, 49 const std::string& hostname,
50 int64_t visits,
48 const std::string& used_url_hash, 51 const std::string& used_url_hash,
49 const std::string& unused_url_hash); 52 const std::string& unused_url_hash);
53 ~ManifestHostInfo();
50 ManifestHostInfo(ManifestHostInfo&&); 54 ManifestHostInfo(ManifestHostInfo&&);
51 ManifestHostInfo& operator=(ManifestHostInfo&&); 55 ManifestHostInfo& operator=(ManifestHostInfo&&);
52 56 // Copy constructor and assignment operator are implicitly deleted.
53 ~ManifestHostInfo();
54 57
55 int64_t manifest_id; 58 int64_t manifest_id;
56 std::string hostname; 59 std::string hostname;
57 GURL manifest_url; 60 GURL manifest_url;
61 int64_t visits;
58 std::string used_url_hash; 62 std::string used_url_hash;
59 std::string unused_url_hash; 63 std::string unused_url_hash;
60 }; 64 };
61 65
66 // Information about a resource to be downloaded.
67 struct ResourceInfo {
68 ResourceInfo(const GURL& url, const std::string& referrer, double weight);
69 ~ResourceInfo();
70 ResourceInfo(ResourceInfo&&);
71 ResourceInfo& operator=(ResourceInfo&&);
72 // Copy constructor and assignment operator are implicitly deleted.
73
74 GURL url; // The resource being requested.
bengr 2016/10/14 21:52:19 Nit: these comments aren't lined up.
twifkak 2016/10/14 22:41:45 Believe it or not, that's what clang-format produc
75 std::string referrer; // The host of the manifest requesting this resource.
76 double weight; // An estimate of the expected utility of this resource.
77 };
78
62 // Public interface to code that fetches resources that the user is likely to 79 // Public interface to code that fetches resources that the user is likely to
63 // want to fetch in the future, putting them in the network stack disk cache. 80 // want to fetch in the future, putting them in the network stack disk cache.
64 // Precaching is intended to be done when Chrome is not actively in use, likely 81 // Precaching is intended to be done when Chrome is not actively in use, likely
65 // hours ahead of the time when the resources are actually needed. 82 // hours ahead of the time when the resources are actually needed.
66 // 83 //
67 // This class takes as input a prioritized list of URL domains that the user 84 // This class takes as input a prioritized list of URL domains that the user
68 // commonly visits, referred to as starting hosts. This class interacts with a 85 // commonly visits, referred to as starting hosts. This class interacts with a
69 // server, sending it the list of starting hosts sequentially. For each starting 86 // server, sending it the list of starting hosts sequentially. For each starting
70 // host, the server returns a manifest of resource URLs that are good candidates 87 // host, the server returns a manifest of resource URLs that are good candidates
71 // for precaching. Every resource returned is fetched, and responses are cached 88 // for precaching. Every resource returned is fetched, and responses are cached
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // std::unique_ptr<PrecacheFetcher> fetcher_; 120 // std::unique_ptr<PrecacheFetcher> fetcher_;
104 // }; 121 // };
105 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> { 122 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> {
106 public: 123 public:
107 class PrecacheDelegate { 124 class PrecacheDelegate {
108 public: 125 public:
109 // Called when the fetching of resources has finished, whether the resources 126 // Called when the fetching of resources has finished, whether the resources
110 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is 127 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is
111 // called, then precaching will be canceled and OnDone will not be called. 128 // called, then precaching will be canceled and OnDone will not be called.
112 virtual void OnDone() = 0; 129 virtual void OnDone() = 0;
113
114 }; 130 };
115 131
116 // Visible for testing. 132 // Visible for testing.
117 class Fetcher; 133 class Fetcher;
118 134
119 static void RecordCompletionStatistics( 135 static void RecordCompletionStatistics(
120 const PrecacheUnfinishedWork& unfinished_work, 136 const PrecacheUnfinishedWork& unfinished_work,
121 size_t remaining_manifest_urls_to_fetch, 137 size_t remaining_manifest_urls_to_fetch,
122 size_t remaining_resource_urls_to_fetch); 138 size_t remaining_resource_urls_to_fetch);
123 139
124 static std::string GetResourceURLBase64HashForTesting( 140 static std::string GetResourceURLBase64HashForTesting(
125 const std::vector<GURL>& urls); 141 const std::vector<GURL>& urls);
126 142
127 // Constructs a new PrecacheFetcher. The |unfinished_work| contains the 143 // Constructs a new PrecacheFetcher. The |unfinished_work| contains the
128 // prioritized list of hosts that the user commonly visits. These hosts are 144 // prioritized list of hosts that the user commonly visits. These hosts are
129 // used by a server side component to construct a list of resource URLs that 145 // used by a server side component to construct a list of resource URLs that
130 // the user is likely to fetch. Takes ownership of |unfinished_work|. 146 // the user is likely to fetch. Takes ownership of |unfinished_work|.
131 // |precache_database| should be accessed only in |db_task_runner|. 147 // |precache_database| should be accessed only in |db_task_runner|.
132 PrecacheFetcher( 148 PrecacheFetcher(
133 net::URLRequestContextGetter* request_context, 149 net::URLRequestContextGetter* request_context,
134 const GURL& config_url, 150 const GURL& config_url,
135 const std::string& manifest_url_prefix, 151 const std::string& manifest_url_prefix,
152 bool global_ranking,
136 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work, 153 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work,
137 uint32_t experiment_id, 154 uint32_t experiment_id,
138 const base::WeakPtr<PrecacheDatabase>& precache_database, 155 const base::WeakPtr<PrecacheDatabase>& precache_database,
139 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner, 156 const scoped_refptr<base::SingleThreadTaskRunner>& db_task_runner,
140 PrecacheDelegate* precache_delegate); 157 PrecacheDelegate* precache_delegate);
141 158
142 virtual ~PrecacheFetcher(); 159 virtual ~PrecacheFetcher();
143 160
144 // Starts fetching resources to precache. URLs are fetched sequentially. Can 161 // Starts fetching resources to precache. URLs are fetched sequentially. Can
145 // be called from any thread. Start should only be called once on a 162 // be called from any thread. Start should only be called once on a
146 // PrecacheFetcher instance. 163 // PrecacheFetcher instance.
147 void Start(); 164 void Start();
148 165
149 // Stops all precaching work. The PreacheFetcher should not be used after 166 // Stops all precaching work. The PreacheFetcher should not be used after
150 // calling this method. 167 // calling this method.
151 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching(); 168 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching();
152 169
153 private: 170 private:
154 friend class PrecacheFetcherTest; 171 friend class PrecacheFetcherTest;
172 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest,
173 GloballyRankResourcesAfterPauseResume);
155 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached); 174 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, FetcherPoolMaxLimitReached);
156 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, 175 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest,
157 CancelPrecachingAfterAllManifestFetch); 176 CancelPrecachingAfterAllManifestFetch);
158 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, DailyQuota); 177 FRIEND_TEST_ALL_PREFIXES(PrecacheFetcherTest, DailyQuota);
159 178
160 // Notifies the precache delete that precaching is done, and report 179 // Notifies the precache delete that precaching is done, and report
161 // completion statistics. 180 // completion statistics.
162 void NotifyDone(size_t remaining_manifest_urls_to_fetch, 181 void NotifyDone(size_t remaining_manifest_urls_to_fetch,
163 size_t remaining_resource_urls_to_fetch); 182 size_t remaining_resource_urls_to_fetch);
164 183
165 // Fetches the next resource or manifest URL, if any remain. Fetching is done 184 // Fetches the next resource or manifest URL, if any remain. Fetching is done
166 // sequentially and depth-first: all resources are fetched for a manifest 185 // sequentially and depth-first: all resources are fetched for a manifest
167 // before the next manifest is fetched. This is done to limit the length of 186 // before the next manifest is fetched. This is done to limit the length of
168 // the |resource_urls_to_fetch_| list, reducing the memory usage. 187 // the |resource_urls_to_fetch_| list, reducing the memory usage.
169 void StartNextFetch(); 188 void StartNextFetch();
170 189
171 void StartNextManifestFetch(); 190 void StartNextManifestFetches();
172 void StartNextResourceFetch(); 191 void StartNextResourceFetch();
173 192
174 // Called when the precache configuration settings have been fetched. 193 // Called when the precache configuration settings have been fetched.
175 // Determines the list of manifest URLs to fetch according to the list of 194 // Determines the list of manifest URLs to fetch according to the list of
176 // |starting_hosts_| and information from the precache configuration settings. 195 // |starting_hosts_| and information from the precache configuration settings.
177 // If the fetch of the configuration settings fails, then precaching ends. 196 // If the fetch of the configuration settings fails, then precaching ends.
178 void OnConfigFetchComplete(const Fetcher& source); 197 void OnConfigFetchComplete(const Fetcher& source);
179 198
180 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts. 199 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts.
181 void DetermineManifests(); 200 void DetermineManifests();
182 201
183 // Called when a precache manifest has been fetched. Builds the list of 202 // Called when a precache manifest has been fetched. Builds the list of
184 // resource URLs to fetch according to the URLs in the manifest. If the fetch 203 // resource URLs to fetch according to the URLs in the manifest. If the fetch
185 // of a manifest fails, then it skips to the next manifest. 204 // of a manifest fails, then it skips to the next manifest.
186 void OnManifestFetchComplete(const Fetcher& source); 205 void OnManifestFetchComplete(int64_t host_visits, const Fetcher& source);
187 206
188 // Called when a resource has been fetched. 207 // Called when a resource has been fetched.
189 void OnResourceFetchComplete(const Fetcher& source); 208 void OnResourceFetchComplete(const Fetcher& source);
190 209
191 // Adds up the response sizes. 210 // Adds up the response sizes.
192 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); 211 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes);
193 212
194 // Callback invoked when the manifest info for all the top hosts is retrieved. 213 // Callback invoked when the manifest info for all the top hosts is retrieved.
195 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info); 214 void OnManifestInfoRetrieved(std::deque<ManifestHostInfo> manifests_info);
196 215
197 // Callback invoked when the quota is retrieved. 216 // Callback invoked when the quota is retrieved.
198 void OnQuotaInfoRetrieved(const PrecacheQuota& quota); 217 void OnQuotaInfoRetrieved(const PrecacheQuota& quota);
199 218
200 // The request context used when fetching URLs. 219 // The request context used when fetching URLs.
201 const scoped_refptr<net::URLRequestContextGetter> request_context_; 220 const scoped_refptr<net::URLRequestContextGetter> request_context_;
202 221
203 // The custom URL to use when fetching the config. If not provided, the 222 // The custom URL to use when fetching the config. If not provided, the
204 // default flag-specified URL will be used. 223 // default flag-specified URL will be used.
205 const GURL config_url_; 224 const GURL config_url_;
206 225
207 // The custom URL prefix to use when fetching manifests. If not provided, the 226 // The custom URL prefix to use when fetching manifests. If not provided, the
208 // default flag-specified prefix will be used. 227 // default flag-specified prefix will be used.
209 const std::string manifest_url_prefix_; 228 const std::string manifest_url_prefix_;
210 229
230 // Whether to order the resource fetches by the global ranking function.
231 bool global_ranking_;
232
211 // PrecacheDatabase should be accessed on the DB thread. 233 // PrecacheDatabase should be accessed on the DB thread.
212 base::WeakPtr<PrecacheDatabase> precache_database_; 234 base::WeakPtr<PrecacheDatabase> precache_database_;
213 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; 235 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_;
214 236
215 // Non-owning pointer. Should not be NULL. 237 // Non-owning pointer. Should not be NULL.
216 PrecacheDelegate* precache_delegate_; 238 PrecacheDelegate* precache_delegate_;
217 239
240 // Top hosts for which manifests still need to be fetched (i.e. no Fetcher has
241 // been created yet).
218 std::deque<ManifestHostInfo> top_hosts_to_fetch_; 242 std::deque<ManifestHostInfo> top_hosts_to_fetch_;
219 std::deque<std::pair<GURL, std::string>> resources_to_fetch_; 243
244 // Top hosts for which manifests are currently being fetched.
245 std::list<ManifestHostInfo> top_hosts_fetching_;
246
247 // Resources to be fetched, in desired fetch order. Populated only after
248 // manifest fetching is complete.
249 std::deque<ResourceInfo> resources_to_fetch_;
250
251 // Resources currently being fetched, in the order requested.
252 std::list<ResourceInfo> resources_fetching_;
253
254 // Resources to be fetched, not yet ranked. Valid until manifest fetching is
255 // done, after which resources are sorted and places in resources_to_fetch_.
256 std::deque<ResourceInfo> resources_to_rank_;
220 257
221 FetcherPool<Fetcher> pool_; 258 FetcherPool<Fetcher> pool_;
222 259
223 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_; 260 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
224 261
225 // Daily quota. 262 // Daily quota.
226 PrecacheQuota quota_; 263 PrecacheQuota quota_;
227 264
228 // The fieldtrial experiment ID. 265 // The fieldtrial experiment ID.
229 uint32_t experiment_id_; 266 uint32_t experiment_id_;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 int64_t response_bytes_; 334 int64_t response_bytes_;
298 int64_t network_response_bytes_; 335 int64_t network_response_bytes_;
299 bool was_cached_; 336 bool was_cached_;
300 337
301 DISALLOW_COPY_AND_ASSIGN(Fetcher); 338 DISALLOW_COPY_AND_ASSIGN(Fetcher);
302 }; 339 };
303 340
304 } // namespace precache 341 } // namespace precache
305 342
306 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 343 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698