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

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: Moved members to proto and updated UMA 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:
104 // Fetches the next resource or manifest URL, if any remain. Fetching is done 127 // 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 128 // 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 129 // 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. 130 // the |resource_urls_to_fetch_| list, reducing the memory usage.
108 void StartNextFetch(); 131 void StartNextFetch();
109 132
110 void StartNextManifestFetch(); 133 void StartNextManifestFetch();
111 void StartNextResourceFetch(); 134 void StartNextResourceFetch();
112 135
113 // Called when the precache configuration settings have been fetched. 136 // Called when the precache configuration settings have been fetched.
114 // Determines the list of manifest URLs to fetch according to the list of 137 // Determines the list of manifest URLs to fetch according to the list of
115 // |starting_hosts_| and information from the precache configuration settings. 138 // |starting_hosts_| and information from the precache configuration settings.
116 // If the fetch of the configuration settings fails, then precaching ends. 139 // If the fetch of the configuration settings fails, then precaching ends.
117 void OnConfigFetchComplete(const Fetcher& source); 140 void OnConfigFetchComplete(const Fetcher& source);
118 141
142 // Constructs manifest URLs using a manifest URL prefix, and lists of hosts.
143 void DetermineManifests();
144
119 // Called when a precache manifest has been fetched. Builds the list of 145 // 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 146 // 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. 147 // of a manifest fails, then it skips to the next manifest.
122 void OnManifestFetchComplete(const Fetcher& source); 148 void OnManifestFetchComplete(const Fetcher& source);
123 149
124 // Called when a resource has been fetched. 150 // Called when a resource has been fetched.
125 void OnResourceFetchComplete(const Fetcher& source); 151 void OnResourceFetchComplete(const Fetcher& source);
126 152
127 // Adds up the response sizes. 153 // Adds up the response sizes.
128 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes); 154 void UpdateStats(int64_t response_bytes, int64_t network_response_bytes);
129 155
130 // The prioritized list of starting hosts that the server will pick resource 156 // The prioritized list of starting hosts that the server will pick resource
131 // URLs to be precached for. 157 // URLs to be precached for.
132 const std::vector<std::string> starting_hosts_; 158 const std::vector<std::string> starting_hosts_;
sclittle 2016/05/20 21:57:07 Isn't this in the UnfinishedWork now?
bengr 2016/05/21 00:16:32 Done.
133 159
134 // The request context used when fetching URLs. 160 // The request context used when fetching URLs.
135 const scoped_refptr<net::URLRequestContextGetter> request_context_; 161 const scoped_refptr<net::URLRequestContextGetter> request_context_;
136 162
137 // The custom URL to use when fetching the config. If not provided, the 163 // The custom URL to use when fetching the config. If not provided, the
138 // default flag-specified URL will be used. 164 // default flag-specified URL will be used.
139 const GURL config_url_; 165 const GURL config_url_;
140 166
141 // The custom URL prefix to use when fetching manifests. If not provided, the 167 // The custom URL prefix to use when fetching manifests. If not provided, the
142 // default flag-specified prefix will be used. 168 // default flag-specified prefix will be used.
143 const std::string manifest_url_prefix_; 169 const std::string manifest_url_prefix_;
144 170
145 // Non-owning pointer. Should not be NULL. 171 // Non-owning pointer. Should not be NULL.
146 PrecacheDelegate* precache_delegate_; 172 PrecacheDelegate* precache_delegate_;
147 173
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_; 174 std::list<GURL> manifest_urls_to_fetch_;
165 std::list<GURL> resource_urls_to_fetch_; 175 std::list<GURL> resource_urls_to_fetch_;
166 176
167 FetcherPool<Fetcher> pool_; 177 FetcherPool<Fetcher> pool_;
168 178
179 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work_;
180
169 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher); 181 DISALLOW_COPY_AND_ASSIGN(PrecacheFetcher);
170 }; 182 };
171 183
172 // Class that fetches a URL, and runs the specified callback when the fetch is 184 // 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 185 // complete. This class exists so that a different method can be run in
174 // response to different kinds of fetches, e.g. OnConfigFetchComplete when 186 // response to different kinds of fetches, e.g. OnConfigFetchComplete when
175 // configuration settings are fetched, OnManifestFetchComplete when a manifest 187 // configuration settings are fetched, OnManifestFetchComplete when a manifest
176 // is fetched, etc. 188 // is fetched, etc.
177 // 189 //
178 // This class tries to increase freshness while limiting network usage, by using 190 // 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_; 239 std::unique_ptr<net::URLFetcher> network_url_fetcher_;
228 int64_t response_bytes_; 240 int64_t response_bytes_;
229 int64_t network_response_bytes_; 241 int64_t network_response_bytes_;
230 242
231 DISALLOW_COPY_AND_ASSIGN(Fetcher); 243 DISALLOW_COPY_AND_ASSIGN(Fetcher);
232 }; 244 };
233 245
234 } // namespace precache 246 } // namespace precache
235 247
236 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_ 248 #endif // COMPONENTS_PRECACHE_CORE_PRECACHE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698