OLD | NEW |
---|---|
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 #include "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/metrics/histogram_macros.h" | |
15 #include "components/precache/core/precache_switches.h" | 16 #include "components/precache/core/precache_switches.h" |
16 #include "components/precache/core/proto/precache.pb.h" | 17 #include "components/precache/core/proto/precache.pb.h" |
17 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
18 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
19 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 21 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
22 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
23 | 24 |
24 using net::URLFetcher; | 25 using net::URLFetcher; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 // response to different kinds of fetches, e.g. OnConfigFetchComplete when | 102 // response to different kinds of fetches, e.g. OnConfigFetchComplete when |
102 // configuration settings are fetched, OnManifestFetchComplete when a manifest | 103 // configuration settings are fetched, OnManifestFetchComplete when a manifest |
103 // is fetched, etc. | 104 // is fetched, etc. |
104 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { | 105 class PrecacheFetcher::Fetcher : public net::URLFetcherDelegate { |
105 public: | 106 public: |
106 // Construct a new Fetcher. This will create and start a new URLFetcher for | 107 // Construct a new Fetcher. This will create and start a new URLFetcher for |
107 // the specified URL using the specified request context. | 108 // the specified URL using the specified request context. |
108 Fetcher(net::URLRequestContextGetter* request_context, const GURL& url, | 109 Fetcher(net::URLRequestContextGetter* request_context, const GURL& url, |
109 const base::Callback<void(const URLFetcher&)>& callback); | 110 const base::Callback<void(const URLFetcher&)>& callback); |
110 ~Fetcher() override {} | 111 ~Fetcher() override {} |
112 void OnURLFetchDownloadProgress(const URLFetcher* source, | |
113 int64 current, | |
114 int64 total) override; | |
111 void OnURLFetchComplete(const URLFetcher* source) override; | 115 void OnURLFetchComplete(const URLFetcher* source) override; |
116 int response_bytes() { return response_bytes_; } | |
112 | 117 |
113 private: | 118 private: |
114 const base::Callback<void(const URLFetcher&)> callback_; | 119 const base::Callback<void(const URLFetcher&)> callback_; |
115 scoped_ptr<URLFetcher> url_fetcher_; | 120 scoped_ptr<URLFetcher> url_fetcher_; |
121 int response_bytes_; | |
116 | 122 |
117 DISALLOW_COPY_AND_ASSIGN(Fetcher); | 123 DISALLOW_COPY_AND_ASSIGN(Fetcher); |
118 }; | 124 }; |
119 | 125 |
120 PrecacheFetcher::Fetcher::Fetcher( | 126 PrecacheFetcher::Fetcher::Fetcher( |
121 net::URLRequestContextGetter* request_context, const GURL& url, | 127 net::URLRequestContextGetter* request_context, |
128 const GURL& url, | |
122 const base::Callback<void(const URLFetcher&)>& callback) | 129 const base::Callback<void(const URLFetcher&)>& callback) |
123 : callback_(callback) { | 130 : callback_(callback), response_bytes_(0) { |
124 url_fetcher_ = URLFetcher::Create(url, URLFetcher::GET, this); | 131 url_fetcher_ = URLFetcher::Create(url, URLFetcher::GET, this); |
125 url_fetcher_->SetRequestContext(request_context); | 132 url_fetcher_->SetRequestContext(request_context); |
126 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 133 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
127 net::LOAD_DO_NOT_SEND_COOKIES); | 134 net::LOAD_DO_NOT_SEND_COOKIES); |
128 url_fetcher_->Start(); | 135 url_fetcher_->Start(); |
129 } | 136 } |
130 | 137 |
138 void PrecacheFetcher::Fetcher::OnURLFetchDownloadProgress( | |
139 const URLFetcher* source, | |
140 int64 current, | |
141 int64 total) { | |
142 response_bytes_ = current; | |
143 } | |
144 | |
131 void PrecacheFetcher::Fetcher::OnURLFetchComplete(const URLFetcher* source) { | 145 void PrecacheFetcher::Fetcher::OnURLFetchComplete(const URLFetcher* source) { |
132 callback_.Run(*source); | 146 callback_.Run(*source); |
133 } | 147 } |
134 | 148 |
135 PrecacheFetcher::PrecacheFetcher( | 149 PrecacheFetcher::PrecacheFetcher( |
136 const std::vector<std::string>& starting_hosts, | 150 const std::vector<std::string>& starting_hosts, |
137 net::URLRequestContextGetter* request_context, | 151 net::URLRequestContextGetter* request_context, |
138 const std::string& manifest_url_prefix, | 152 const std::string& manifest_url_prefix, |
139 PrecacheFetcher::PrecacheDelegate* precache_delegate) | 153 PrecacheFetcher::PrecacheDelegate* precache_delegate) |
140 : starting_hosts_(starting_hosts), | 154 : starting_hosts_(starting_hosts), |
141 request_context_(request_context), | 155 request_context_(request_context), |
142 manifest_url_prefix_(manifest_url_prefix), | 156 manifest_url_prefix_(manifest_url_prefix), |
143 precache_delegate_(precache_delegate) { | 157 precache_delegate_(precache_delegate), |
158 total_response_bytes_(0), | |
159 num_manifest_urls_to_fetch_(0) { | |
144 DCHECK(request_context_.get()); // Request context must be non-NULL. | 160 DCHECK(request_context_.get()); // Request context must be non-NULL. |
145 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. | 161 DCHECK(precache_delegate_); // Precache delegate must be non-NULL. |
146 | 162 |
147 DCHECK_NE(GURL(), GetConfigURL()) | 163 DCHECK_NE(GURL(), GetConfigURL()) |
148 << "Could not determine the precache config settings URL."; | 164 << "Could not determine the precache config settings URL."; |
149 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) | 165 DCHECK_NE(std::string(), GetDefaultManifestURLPrefix()) |
150 << "Could not determine the default precache manifest URL prefix."; | 166 << "Could not determine the default precache manifest URL prefix."; |
151 } | 167 } |
152 | 168 |
153 PrecacheFetcher::~PrecacheFetcher() { | 169 PrecacheFetcher::~PrecacheFetcher() { |
170 // Number of manifests for which we have downloaded all resources. | |
171 int manifests_completed = | |
172 num_manifest_urls_to_fetch_ - manifest_urls_to_fetch_.size(); | |
173 | |
174 // If there are resource URLs left to fetch, the last manifest is not yet | |
175 // completed. | |
176 if (!resource_urls_to_fetch_.empty()) | |
177 --manifests_completed; | |
178 | |
179 int percent_completed = | |
180 manifests_completed == 0 ? 0 : (static_cast<double>(manifests_completed) / | |
181 num_manifest_urls_to_fetch_ * 100); | |
182 UMA_HISTOGRAM_COUNTS_100("Precache.Fetch.PercentCompleted", | |
Mark P
2015/06/26 17:03:37
Why not UMA_HISTOGRAM_PERCENTAGE?
twifkak
2015/06/26 17:48:57
Because I didn't know any better.
| |
183 percent_completed); | |
184 UMA_HISTOGRAM_COUNTS("Precache.Fetch.ResponseBytes", total_response_bytes_); | |
Mark P
2015/06/26 17:03:37
This maxs out at 1,000,000. Is that enough range
twifkak
2015/06/26 17:48:57
Eek, no. Fixed.
| |
154 } | 185 } |
155 | 186 |
156 void PrecacheFetcher::Start() { | 187 void PrecacheFetcher::Start() { |
157 DCHECK(!fetcher_); // Start shouldn't be called repeatedly. | 188 DCHECK(!fetcher_); // Start shouldn't be called repeatedly. |
158 | 189 |
159 GURL config_url = GetConfigURL(); | 190 GURL config_url = GetConfigURL(); |
160 DCHECK(config_url.is_valid()); | 191 DCHECK(config_url.is_valid()); |
161 | 192 |
162 // Fetch the precache configuration settings from the server. | 193 // Fetch the precache configuration settings from the server. |
163 fetcher_.reset(new Fetcher(request_context_.get(), | 194 fetcher_.reset(new Fetcher(request_context_.get(), |
164 config_url, | 195 config_url, |
165 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, | 196 base::Bind(&PrecacheFetcher::OnConfigFetchComplete, |
166 base::Unretained(this)))); | 197 base::Unretained(this)))); |
167 } | 198 } |
168 | 199 |
169 void PrecacheFetcher::StartNextFetch() { | 200 void PrecacheFetcher::StartNextFetch() { |
201 total_response_bytes_ += fetcher_->response_bytes(); | |
202 | |
170 if (!resource_urls_to_fetch_.empty()) { | 203 if (!resource_urls_to_fetch_.empty()) { |
171 // Fetch the next resource URL. | 204 // Fetch the next resource URL. |
172 fetcher_.reset( | 205 fetcher_.reset( |
173 new Fetcher(request_context_.get(), | 206 new Fetcher(request_context_.get(), |
174 resource_urls_to_fetch_.front(), | 207 resource_urls_to_fetch_.front(), |
175 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, | 208 base::Bind(&PrecacheFetcher::OnResourceFetchComplete, |
176 base::Unretained(this)))); | 209 base::Unretained(this)))); |
177 | 210 |
178 resource_urls_to_fetch_.pop_front(); | 211 resource_urls_to_fetch_.pop_front(); |
179 return; | 212 return; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 if (rank > config.top_sites_count()) | 255 if (rank > config.top_sites_count()) |
223 break; | 256 break; |
224 unique_manifest_urls.insert(ConstructManifestURL(prefix, host)); | 257 unique_manifest_urls.insert(ConstructManifestURL(prefix, host)); |
225 } | 258 } |
226 | 259 |
227 for (const std::string& url : config.forced_site()) | 260 for (const std::string& url : config.forced_site()) |
228 unique_manifest_urls.insert(ConstructManifestURL(prefix, url)); | 261 unique_manifest_urls.insert(ConstructManifestURL(prefix, url)); |
229 | 262 |
230 for (const std::string& manifest_url : unique_manifest_urls) | 263 for (const std::string& manifest_url : unique_manifest_urls) |
231 manifest_urls_to_fetch_.push_back(GURL(manifest_url)); | 264 manifest_urls_to_fetch_.push_back(GURL(manifest_url)); |
265 num_manifest_urls_to_fetch_ = manifest_urls_to_fetch_.size(); | |
232 | 266 |
233 StartNextFetch(); | 267 StartNextFetch(); |
234 } | 268 } |
235 | 269 |
236 void PrecacheFetcher::OnManifestFetchComplete(const URLFetcher& source) { | 270 void PrecacheFetcher::OnManifestFetchComplete(const URLFetcher& source) { |
237 PrecacheManifest manifest; | 271 PrecacheManifest manifest; |
238 | 272 |
239 if (ParseProtoFromFetchResponse(source, &manifest)) { | 273 if (ParseProtoFromFetchResponse(source, &manifest)) { |
240 for (int i = 0; i < manifest.resource_size(); ++i) { | 274 for (int i = 0; i < manifest.resource_size(); ++i) { |
241 if (manifest.resource(i).has_url()) { | 275 if (manifest.resource(i).has_url()) { |
242 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); | 276 resource_urls_to_fetch_.push_back(GURL(manifest.resource(i).url())); |
243 } | 277 } |
244 } | 278 } |
245 } | 279 } |
246 | 280 |
247 StartNextFetch(); | 281 StartNextFetch(); |
248 } | 282 } |
249 | 283 |
250 void PrecacheFetcher::OnResourceFetchComplete(const URLFetcher& source) { | 284 void PrecacheFetcher::OnResourceFetchComplete(const URLFetcher& source) { |
251 // The resource has already been put in the cache during the fetch process, so | 285 // The resource has already been put in the cache during the fetch process, so |
252 // nothing more needs to be done for the resource. | 286 // nothing more needs to be done for the resource. |
253 StartNextFetch(); | 287 StartNextFetch(); |
254 } | 288 } |
255 | 289 |
256 } // namespace precache | 290 } // namespace precache |
OLD | NEW |