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

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

Issue 1961153003: Add pause/resume functionality to precache (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: test fix Created 4 years, 7 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 <algorithm>
11 #include <list> 11 #include <list>
12 #include <memory> 12 #include <memory>
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/callback.h" 16 #include "base/callback.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h" 18 #include "base/memory/weak_ptr.h"
19 #include "base/single_thread_task_runner.h"
19 #include "base/time/time.h" 20 #include "base/time/time.h"
20 #include "components/precache/core/fetcher_pool.h" 21 #include "components/precache/core/fetcher_pool.h"
21 #include "net/url_request/url_fetcher.h" 22 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_fetcher_delegate.h" 23 #include "net/url_request/url_fetcher_delegate.h"
23 #include "url/gurl.h" 24 #include "url/gurl.h"
24 25
26 namespace base {
27 class SingleThreadTaskRunner;
28 }
29
25 namespace net { 30 namespace net {
26 class URLRequestContextGetter; 31 class URLRequestContextGetter;
27 } 32 }
28 33
29 namespace precache { 34 namespace precache {
30 35
31 class PrecacheConfigurationSettings; 36 class PrecacheConfigurationSettings;
37 class PrecacheUnfinishedWork;
32 38
33 // Visible for testing. 39 // Visible for testing.
34 extern const int kNoTracking; 40 extern const int kNoTracking;
35 41
36 // Public interface to code that fetches resources that the user is likely to 42 // Public interface to code that fetches resources that the user is likely to
37 // want to fetch in the future, putting them in the network stack disk cache. 43 // want to fetch in the future, putting them in the network stack disk cache.
38 // Precaching is intended to be done when Chrome is not actively in use, likely 44 // Precaching is intended to be done when Chrome is not actively in use, likely
39 // hours ahead of the time when the resources are actually needed. 45 // hours ahead of the time when the resources are actually needed.
40 // 46 //
41 // This class takes as input a prioritized list of URL domains that the user 47 // This class takes as input a prioritized list of URL domains that the user
(...skipping 10 matching lines...) Expand all
52 // manifests are fetched from. These can be set by using command line switches 58 // manifests are fetched from. These can be set by using command line switches
53 // or by providing default values. 59 // or by providing default values.
54 // 60 //
55 // Sample interaction: 61 // Sample interaction:
56 // 62 //
57 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate { 63 // class MyPrecacheFetcherDelegate : public PrecacheFetcher::PrecacheDelegate {
58 // public: 64 // public:
59 // void PrecacheResourcesForTopURLs( 65 // void PrecacheResourcesForTopURLs(
60 // net::URLRequestContextGetter* request_context, 66 // net::URLRequestContextGetter* request_context,
61 // const std::list<GURL>& top_urls) { 67 // const std::list<GURL>& top_urls) {
62 // fetcher_.reset(new PrecacheFetcher(request_context, top_urls, this)); 68 // fetcher_.reset(new PrecacheFetcher(...));
63 // fetcher_->Start(); 69 // fetcher_->Start();
64 // } 70 // }
65 // 71 //
72 // void Cancel() {
73 // std::unique_ptr<PrecacheUnfinishedWork> unfinished_work =
74 // fetcher_->CancelPrecaching();
75 // fetcher_.reset();
76 // }
77 //
66 // virtual void OnDone() { 78 // virtual void OnDone() {
67 // // Do something when precaching is done. 79 // // Do something when precaching is done.
68 // } 80 // }
69 // 81 //
70 // private: 82 // private:
71 // std::unique_ptr<PrecacheFetcher> fetcher_; 83 // std::unique_ptr<PrecacheFetcher> fetcher_;
72 // }; 84 // };
73 class PrecacheFetcher { 85 class PrecacheFetcher : public base::SupportsWeakPtr<PrecacheFetcher> {
74 public: 86 public:
75 class PrecacheDelegate { 87 class PrecacheDelegate {
76 public: 88 public:
77 // Called when the fetching of resources has finished, whether the resources 89 // Called when the fetching of resources has finished, whether the resources
78 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is 90 // were fetched or not. If the PrecacheFetcher is destroyed before OnDone is
79 // called, then precaching will be canceled and OnDone will not be called. 91 // called, then precaching will be canceled and OnDone will not be called.
80 virtual void OnDone() = 0; 92 virtual void OnDone() = 0;
93
81 }; 94 };
82 95
83 // Visible for testing. 96 // Visible for testing.
84 class Fetcher; 97 class Fetcher;
85 98
99 static void RecordCompletionStatistics(
100 const PrecacheUnfinishedWork& unfinished_work,
101 size_t remaining_manifest_urls_to_fetch,
102 size_t remaining_resource_urls_to_fetch);
103
86 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a 104 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a
87 // prioritized list of hosts that the user commonly visits. These hosts are 105 // prioritized list of hosts that the user commonly visits. These hosts are
88 // used by a server side component to construct a list of resource URLs that 106 // used by a server side component to construct a list of resource URLs that
89 // the user is likely to fetch. 107 // the user is likely to fetch. Takes ownership of |unfinished_work|.
90 PrecacheFetcher(const std::vector<std::string>& starting_hosts, 108 PrecacheFetcher(
91 net::URLRequestContextGetter* request_context, 109 net::URLRequestContextGetter* request_context,
92 const GURL& config_url, 110 const GURL& config_url,
93 const std::string& manifest_url_prefix, 111 const std::string& manifest_url_prefix,
94 PrecacheDelegate* precache_delegate); 112 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work,
113 PrecacheDelegate* precache_delegate);
95 114
96 virtual ~PrecacheFetcher(); 115 virtual ~PrecacheFetcher();
97 116
98 // Starts fetching resources to precache. URLs are fetched sequentially. Can 117 // Starts fetching resources to precache. URLs are fetched sequentially. Can
99 // be called from any thread. Start should only be called once on a 118 // be called from any thread. Start should only be called once on a
100 // PrecacheFetcher instance. 119 // PrecacheFetcher instance.
101 void Start(); 120 void Start();
102 121
122 // Stops all precaching work. The PreacheFetcher should not be used after
123 // calling this method.
124 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching();
125
103 private: 126 private:
127 // Notifies the precache delete that precaching is done, and report
128 // completion statistics.
129 void NotifyDone(size_t remaining_manifest_urls_to_fetch,
130 size_t remaining_resource_urls_to_fetch);
131
104 // Fetches the next resource or manifest URL, if any remain. Fetching is done 132 // Fetches the next resource or manifest URL, if any remain. Fetching is done
105 // sequentially and depth-first: all resources are fetched for a manifest 133 // sequentially and depth-first: all resources are fetched for a manifest
106 // before the next manifest is fetched. This is done to limit the length of 134 // before the next manifest is fetched. This is done to limit the length of
107 // the |resource_urls_to_fetch_| list, reducing the memory usage. 135 // the |resource_urls_to_fetch_| list, reducing the memory usage.
108 void StartNextFetch(); 136 void StartNextFetch();
109 137
110 void StartNextManifestFetch(); 138 void StartNextManifestFetch();
111 void StartNextResourceFetch(); 139 void StartNextResourceFetch();
112 140
113 // Called when the precache configuration settings have been fetched. 141 // Called when the precache configuration settings have been fetched.
114 // Determines the list of manifest URLs to fetch according to the list of 142 // Determines the list of manifest URLs to fetch according to the list of
115 // |starting_hosts_| and information from the precache configuration settings. 143 // |starting_hosts_| and information from the precache configuration settings.
116 // If the fetch of the configuration settings fails, then precaching ends. 144 // If the fetch of the configuration settings fails, then precaching ends.
117 void OnConfigFetchComplete(const Fetcher& source); 145 void OnConfigFetchComplete(const Fetcher& source);
118 146
147 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts.
148 void DetermineManifests();
149
119 // Called when a precache manifest has been fetched. Builds the list of 150 // Called when a precache manifest has been fetched. Builds the list of
120 // resource URLs to fetch according to the URLs in the manifest. If the fetch 151 // resource URLs to fetch according to the URLs in the manifest. If the fetch
121 // of a manifest fails, then it skips to the next manifest. 152 // of a manifest fails, then it skips to the next manifest.
122 void OnManifestFetchComplete(const Fetcher& source); 153 void OnManifestFetchComplete(const Fetcher& source);
123 154
124 // Called when a resource has been fetched. 155 // Called when a resource has been fetched.
125 void OnResourceFetchComplete(const Fetcher& source); 156 void OnResourceFetchComplete(const Fetcher& source);
126 157
127 // Adds up the response sizes. 158 // Adds up the response sizes.
128 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); 159 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes);
129 160
130 // The prioritized list of starting hosts that the server will pick resource
131 // URLs to be precached for.
132 const std::vector<std::string> starting_hosts_;
133
134 // The request context used when fetching URLs. 161 // The request context used when fetching URLs.
135 const scoped_refptr<net::URLRequestContextGetter> request_context_; 162 const scoped_refptr<net::URLRequestContextGetter> request_context_;
136 163
137 // The custom URL to use when fetching the config. If not provided, the 164 // The custom URL to use when fetching the config. If not provided, the
138 // default flag-specified URL will be used. 165 // default flag-specified URL will be used.
139 const GURL config_url_; 166 const GURL config_url_;
140 167
141 // The custom URL prefix to use when fetching manifests. If not provided, the 168 // The custom URL prefix to use when fetching manifests. If not provided, the
142 // default flag-specified prefix will be used. 169 // default flag-specified prefix will be used.
143 const std::string manifest_url_prefix_; 170 const std::string manifest_url_prefix_;
144 171
145 // Non-owning pointer. Should not be NULL. 172 // Non-owning pointer. Should not be NULL.
146 PrecacheDelegate* precache_delegate_; 173 PrecacheDelegate* precache_delegate_;
147 174
148 std::unique_ptr<PrecacheConfigurationSettings> config_;
149
150 // Tally of the total number of bytes contained in URL fetches, including
151 // config, manifests, and resources. This the number of bytes as they would be
152 // compressed over the network.
153 size_t total_response_bytes_;
154
155 // Tally of the total number of bytes received over the network from URL
156 // fetches (the same ones as in total_response_bytes_). This includes response
157 // headers and intermediate responses such as 30xs.
158 size_t network_response_bytes_;
159
160 // Time when the prefetch was started.
161 base::TimeTicks start_time_;
162
163 int num_manifest_urls_to_fetch_;
164 std::list<GURL> manifest_urls_to_fetch_; 175 std::list<GURL> manifest_urls_to_fetch_;
165 std::list<GURL> resource_urls_to_fetch_; 176 std::list<GURL> resource_urls_to_fetch_;
166 177
167 FetcherPool<Fetcher> pool_; 178 FetcherPool<Fetcher> pool_;
168 179
180 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
181
169 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); 182 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher);
170 }; 183 };
171 184
172 // Class that fetches a URL, and runs the specified callback when the fetch is 185 // Class that fetches a URL, and runs the specified callback when the fetch is
173 // complete. This class exists so that a different method can be run in 186 // complete. This class exists so that a different method can be run in
174 // response to different kinds of fetches, e.g. OnConfigFetchComplete when 187 // response to different kinds of fetches, e.g. OnConfigFetchComplete when
175 // configuration settings are fetched, OnManifestFetchComplete when a manifest 188 // configuration settings are fetched, OnManifestFetchComplete when a manifest
176 // is fetched, etc. 189 // is fetched, etc.
177 // 190 //
178 // This class tries to increase freshness while limiting network usage, by using 191 // This class tries to increase freshness while limiting network usage, by using
(...skipping 22 matching lines...) Expand all
201 ~Fetcher() override; 214 ~Fetcher() override;
202 void OnURLFetchDownloadProgress(const net::URLFetcher* source, 215 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
203 int64_t current, 216 int64_t current,
204 int64_t total) override; 217 int64_t total) override;
205 void OnURLFetchComplete(const net::URLFetcher* source) override; 218 void OnURLFetchComplete(const net::URLFetcher* source) override;
206 int64_t response_bytes() const { return response_bytes_; } 219 int64_t response_bytes() const { return response_bytes_; }
207 int64_t network_response_bytes() const { return network_response_bytes_; } 220 int64_t network_response_bytes() const { return network_response_bytes_; }
208 const net::URLFetcher* network_url_fetcher() const { 221 const net::URLFetcher* network_url_fetcher() const {
209 return network_url_fetcher_.get(); 222 return network_url_fetcher_.get();
210 } 223 }
224 const GURL& url() const { return url_; }
225 bool is_resource_request() const { return is_resource_request_; }
211 226
212 private: 227 private:
213 enum class FetchStage { CACHE, NETWORK }; 228 enum class FetchStage { CACHE, NETWORK };
214 229
215 void LoadFromCache(); 230 void LoadFromCache();
216 void LoadFromNetwork(); 231 void LoadFromNetwork();
217 232
218 net::URLRequestContextGetter* const request_context_; 233 net::URLRequestContextGetter* const request_context_;
219 const GURL url_; 234 const GURL url_;
220 const base::Callback<void(const Fetcher&)> callback_; 235 const base::Callback<void(const Fetcher&)> callback_;
221 const bool is_resource_request_; 236 const bool is_resource_request_;
222 const size_t max_bytes_; 237 const size_t max_bytes_;
223 238
224 FetchStage fetch_stage_; 239 FetchStage fetch_stage_;
225 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing. 240 // The cache_url_fetcher_ is kept alive until Fetcher destruction for testing.
226 std::unique_ptr<net::URLFetcher> cache_url_fetcher_; 241 std::unique_ptr<net::URLFetcher> cache_url_fetcher_;
227 std::unique_ptr<net::URLFetcher> network_url_fetcher_; 242 std::unique_ptr<net::URLFetcher> network_url_fetcher_;
228 int64_t response_bytes_; 243 int64_t response_bytes_;
229 int64_t network_response_bytes_; 244 int64_t network_response_bytes_;
230 245
231 DISALLOW_COPY_AND_ASSIGN(Fetcher); 246 DISALLOW_COPY_AND_ASSIGN(Fetcher);
232 }; 247 };
233 248
234 } // namespace precache 249 } // namespace precache
235 250
236 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 251 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW
« no previous file with comments | « components/precache/core/precache_database.cc ('k') | components/precache/core/precache_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698