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

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: Cleaned up PrecacheFetcher constructor 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_->GetUnfinishedWork();
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
86 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a 99 // Constructs a new PrecacheFetcher. The |starting_hosts| parameter is a
87 // prioritized list of hosts that the user commonly visits. These hosts are 100 // 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 101 // used by a server side component to construct a list of resource URLs that
89 // the user is likely to fetch. 102 // the user is likely to fetch. Takes ownership of |unfinished_work|.
90 PrecacheFetcher(const std::vector<std::string>& starting_hosts, 103 PrecacheFetcher(
91 net::URLRequestContextGetter* request_context, 104 net::URLRequestContextGetter* request_context,
92 const GURL& config_url, 105 const GURL& config_url,
93 const std::string& manifest_url_prefix, 106 const std::string& manifest_url_prefix,
94 PrecacheDelegate* precache_delegate); 107 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work,
108 PrecacheDelegate* precache_delegate);
95 109
96 virtual ~PrecacheFetcher(); 110 virtual ~PrecacheFetcher();
97 111
98 // Starts fetching resources to precache. URLs are fetched sequentially. Can 112 // Starts fetching resources to precache. URLs are fetched sequentially. Can
99 // be called from any thread. Start should only be called once on a 113 // be called from any thread. Start should only be called once on a
100 // PrecacheFetcher instance. 114 // PrecacheFetcher instance.
101 void Start(); 115 void Start();
102 116
117 std::unique_ptr<PrecacheUnfinishedWork> CancelPrecaching();
sclittle 2016/05/20 21:57:07 nit: add a comment saying that nothing should be d
bengr 2016/05/21 00:16:32 Done.
118
119 // Used for testing to put the fetcher into a state where it has work to do.
120 void AssignWorkForTest(const std::list<GURL>& manifests,
sclittle 2016/05/20 21:57:07 Is this method really necessary? Couldn't you just
bengr 2016/05/21 00:16:32 Done.
121 const std::list<GURL>& resources,
122 size_t total_response_bytes,
123 size_t network_response_bytes,
124 size_t num_manifest_urls_to_fetch,
125 const base::Time& start_time);
126
103 private: 127 private:
104 // Fetches the next resource or manifest URL, if any remain. Fetching is done 128 // 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 129 // 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 130 // 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. 131 // the |resource_urls_to_fetch_| list, reducing the memory usage.
108 void StartNextFetch(); 132 void StartNextFetch();
109 133
110 void StartNextManifestFetch(); 134 void StartNextManifestFetch();
111 void StartNextResourceFetch(); 135 void StartNextResourceFetch();
112 136
113 // Called when the precache configuration settings have been fetched. 137 // Called when the precache configuration settings have been fetched.
114 // Determines the list of manifest URLs to fetch according to the list of 138 // Determines the list of manifest URLs to fetch according to the list of
115 // |starting_hosts_| and information from the precache configuration settings. 139 // |starting_hosts_| and information from the precache configuration settings.
116 // If the fetch of the configuration settings fails, then precaching ends. 140 // If the fetch of the configuration settings fails, then precaching ends.
117 void OnConfigFetchComplete(const Fetcher& source); 141 void OnConfigFetchComplete(const Fetcher& source);
118 142
143 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts.
144 void DetermineManifests();
145
119 // Called when a precache manifest has been fetched. Builds the list of 146 // 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 147 // 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. 148 // of a manifest fails, then it skips to the next manifest.
122 void OnManifestFetchComplete(const Fetcher& source); 149 void OnManifestFetchComplete(const Fetcher& source);
123 150
124 // Called when a resource has been fetched. 151 // Called when a resource has been fetched.
125 void OnResourceFetchComplete(const Fetcher& source); 152 void OnResourceFetchComplete(const Fetcher& source);
126 153
127 // Adds up the response sizes. 154 // Adds up the response sizes.
128 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); 155 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes);
(...skipping 22 matching lines...) Expand all
151 // config, manifests, and resources. This the number of bytes as they would be 178 // config, manifests, and resources. This the number of bytes as they would be
152 // compressed over the network. 179 // compressed over the network.
153 size_t total_response_bytes_; 180 size_t total_response_bytes_;
154 181
155 // Tally of the total number of bytes received over the network from URL 182 // 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 183 // fetches (the same ones as in total_response_bytes_). This includes response
157 // headers and intermediate responses such as 30xs. 184 // headers and intermediate responses such as 30xs.
158 size_t network_response_bytes_; 185 size_t network_response_bytes_;
159 186
160 // Time when the prefetch was started. 187 // Time when the prefetch was started.
161 base::TimeTicks start_time_; 188 base::Time start_time_;
162 189
163 int num_manifest_urls_to_fetch_; 190 size_t num_manifest_urls_to_fetch_;
164 std::list<GURL> manifest_urls_to_fetch_; 191 std::list<GURL> manifest_urls_to_fetch_;
165 std::list<GURL> resource_urls_to_fetch_; 192 std::list<GURL> resource_urls_to_fetch_;
166 193
167 FetcherPool<Fetcher> pool_; 194 FetcherPool<Fetcher> pool_;
168 195
196 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
197
169 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); 198 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher);
170 }; 199 };
171 200
172 // Class that fetches a URL, and runs the specified callback when the fetch is 201 // 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 202 // complete. This class exists so that a different method can be run in
174 // response to different kinds of fetches, e.g. OnConfigFetchComplete when 203 // response to different kinds of fetches, e.g. OnConfigFetchComplete when
175 // configuration settings are fetched, OnManifestFetchComplete when a manifest 204 // configuration settings are fetched, OnManifestFetchComplete when a manifest
176 // is fetched, etc. 205 // is fetched, etc.
177 // 206 //
178 // This class tries to increase freshness while limiting network usage, by using 207 // This class tries to increase freshness while limiting network usage, by using
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 std::unique_ptr<net::URLFetcher> network_url_fetcher_; 256 std::unique_ptr<net::URLFetcher> network_url_fetcher_;
228 int64_t response_bytes_; 257 int64_t response_bytes_;
229 int64_t network_response_bytes_; 258 int64_t network_response_bytes_;
230 259
231 DISALLOW_COPY_AND_ASSIGN(Fetcher); 260 DISALLOW_COPY_AND_ASSIGN(Fetcher);
232 }; 261 };
233 262
234 } // namespace precache 263 } // namespace precache
235 264
236 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 265 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698