OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autofill/autofill_download.h" | 5 #include "chrome/browser/autofill/autofill_download.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 #endif | 28 #endif |
29 | 29 |
30 struct AutoFillDownloadManager::FormRequestData { | 30 struct AutoFillDownloadManager::FormRequestData { |
31 std::vector<std::string> form_signatures; | 31 std::vector<std::string> form_signatures; |
32 AutoFillRequestType request_type; | 32 AutoFillRequestType request_type; |
33 }; | 33 }; |
34 | 34 |
35 AutoFillDownloadManager::AutoFillDownloadManager(Profile* profile) | 35 AutoFillDownloadManager::AutoFillDownloadManager(Profile* profile) |
36 : profile_(profile), | 36 : profile_(profile), |
37 observer_(NULL), | 37 observer_(NULL), |
38 number_of_forms_to_cache_(16), | |
dhollowa
2011/01/25 23:33:02
Let's pull this |16| out into a named constant wit
GeorgeY
2011/01/25 23:51:30
Done.
| |
38 next_query_request_(base::Time::Now()), | 39 next_query_request_(base::Time::Now()), |
39 next_upload_request_(base::Time::Now()), | 40 next_upload_request_(base::Time::Now()), |
40 positive_upload_rate_(0), | 41 positive_upload_rate_(0), |
41 negative_upload_rate_(0), | 42 negative_upload_rate_(0), |
42 fetcher_id_for_unittest_(0), | 43 fetcher_id_for_unittest_(0), |
43 is_testing_(false) { | 44 is_testing_(false) { |
44 // |profile_| could be NULL in some unit-tests. | 45 // |profile_| could be NULL in some unit-tests. |
45 if (profile_) { | 46 if (profile_) { |
46 PrefService* preferences = profile_->GetPrefs(); | 47 PrefService* preferences = profile_->GetPrefs(); |
47 positive_upload_rate_ = | 48 positive_upload_rate_ = |
(...skipping 22 matching lines...) Expand all Loading... | |
70 const ScopedVector<FormStructure>& forms, | 71 const ScopedVector<FormStructure>& forms, |
71 const AutoFillMetrics& metric_logger) { | 72 const AutoFillMetrics& metric_logger) { |
72 if (next_query_request_ > base::Time::Now()) { | 73 if (next_query_request_ > base::Time::Now()) { |
73 // We are in back-off mode: do not do the request. | 74 // We are in back-off mode: do not do the request. |
74 return false; | 75 return false; |
75 } | 76 } |
76 std::string form_xml; | 77 std::string form_xml; |
77 FormRequestData request_data; | 78 FormRequestData request_data; |
78 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures, | 79 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures, |
79 &form_xml)) | 80 &form_xml)) |
80 return false; | 81 return false; |
dhollowa
2011/01/25 23:33:02
nit: style guide says this should be wrapped in br
GeorgeY
2011/01/25 23:51:30
Done.
| |
81 | 82 |
82 request_data.request_type = AutoFillDownloadManager::REQUEST_QUERY; | 83 request_data.request_type = AutoFillDownloadManager::REQUEST_QUERY; |
83 metric_logger.Log(AutoFillMetrics::QUERY_SENT); | 84 metric_logger.Log(AutoFillMetrics::QUERY_SENT); |
84 | 85 |
86 std::string query_data; | |
87 if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) { | |
88 VLOG(1) << "AutoFillDownloadManager: query request has been retrieved from" | |
89 << "the cache"; | |
90 if (observer_) | |
91 observer_->OnLoadedAutoFillHeuristics(query_data); | |
92 return true; | |
93 } | |
94 | |
85 return StartRequest(form_xml, request_data); | 95 return StartRequest(form_xml, request_data); |
86 } | 96 } |
87 | 97 |
88 bool AutoFillDownloadManager::StartUploadRequest( | 98 bool AutoFillDownloadManager::StartUploadRequest( |
89 const FormStructure& form, bool form_was_matched) { | 99 const FormStructure& form, bool form_was_matched) { |
90 if (next_upload_request_ > base::Time::Now()) { | 100 if (next_upload_request_ > base::Time::Now()) { |
91 // We are in back-off mode: do not do the request. | 101 // We are in back-off mode: do not do the request. |
92 return false; | 102 return false; |
93 } | 103 } |
94 | 104 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 URLFetcher::POST, | 191 URLFetcher::POST, |
182 this); | 192 this); |
183 url_fetchers_[fetcher] = request_data; | 193 url_fetchers_[fetcher] = request_data; |
184 fetcher->set_automatically_retry_on_5xx(false); | 194 fetcher->set_automatically_retry_on_5xx(false); |
185 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 195 fetcher->set_request_context(Profile::GetDefaultRequestContext()); |
186 fetcher->set_upload_data("text/plain", form_xml); | 196 fetcher->set_upload_data("text/plain", form_xml); |
187 fetcher->Start(); | 197 fetcher->Start(); |
188 return true; | 198 return true; |
189 } | 199 } |
190 | 200 |
201 void AutoFillDownloadManager::CacheQueryRequest( | |
202 std::vector<std::string> const& forms_in_query, | |
203 std::string const& query_data) { | |
204 std::string signature = GetCombinedSignature(forms_in_query); | |
205 for (std::list<std::pair<std::string, std::string> >::iterator it = | |
206 cached_forms_.begin(); it != cached_forms_.end(); ++it) { | |
207 if (it->first == signature) { | |
208 // We hit the cache, move to the first position and return. | |
209 std::pair<std::string, std::string> data = *it; | |
210 cached_forms_.erase(it); | |
211 cached_forms_.push_front(data); | |
212 return; | |
213 } | |
214 } | |
215 std::pair<std::string, std::string> data; | |
216 data.first = signature; | |
217 data.second = query_data; | |
218 cached_forms_.push_front(data); | |
219 while (cached_forms_.size() > number_of_forms_to_cache_) | |
220 cached_forms_.pop_back(); | |
221 } | |
222 | |
223 bool AutoFillDownloadManager::CheckCacheForQueryRequest( | |
224 std::vector<std::string> const& forms_in_query, | |
225 std::string* query_data) const { | |
226 std::string signature = GetCombinedSignature(forms_in_query); | |
227 for (std::list<std::pair<std::string, std::string> >::const_iterator it = | |
228 cached_forms_.begin(); it != cached_forms_.end(); ++it) { | |
229 if (it->first == signature) { | |
230 // We hit the cache, fill the data and return. | |
231 *query_data = it->second; | |
232 return true; | |
233 } | |
234 } | |
235 return false; | |
236 } | |
237 | |
238 std::string AutoFillDownloadManager::GetCombinedSignature( | |
239 std::vector<std::string> const& forms_in_query) const { | |
240 size_t total_size = forms_in_query.size(); | |
dhollowa
2011/01/25 23:33:02
My read of this is that if I passed in { "a", "b",
GeorgeY
2011/01/25 23:51:30
nope "a,b,c\0" so everything is right. I do not th
dhollowa
2011/01/26 00:15:02
Ok, I misread it. Seems ok.
On 2011/01/25 23:51:
| |
241 for (size_t i = 0; i < forms_in_query.size(); ++i) | |
242 total_size += forms_in_query[i].length(); | |
243 std::string signature; | |
244 | |
245 signature.reserve(total_size); | |
246 | |
247 for (size_t i = 0; i < forms_in_query.size(); ++i) { | |
248 if (i) | |
249 signature.append(","); | |
250 signature.append(forms_in_query[i]); | |
251 } | |
252 return signature; | |
253 } | |
254 | |
191 void AutoFillDownloadManager::OnURLFetchComplete( | 255 void AutoFillDownloadManager::OnURLFetchComplete( |
192 const URLFetcher* source, | 256 const URLFetcher* source, |
193 const GURL& url, | 257 const GURL& url, |
194 const net::URLRequestStatus& status, | 258 const net::URLRequestStatus& status, |
195 int response_code, | 259 int response_code, |
196 const ResponseCookies& cookies, | 260 const ResponseCookies& cookies, |
197 const std::string& data) { | 261 const std::string& data) { |
198 std::map<URLFetcher *, FormRequestData>::iterator it = | 262 std::map<URLFetcher *, FormRequestData>::iterator it = |
199 url_fetchers_.find(const_cast<URLFetcher*>(source)); | 263 url_fetchers_.find(const_cast<URLFetcher*>(source)); |
200 if (it == url_fetchers_.end()) { | 264 if (it == url_fetchers_.end()) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 << " request has failed with response " << response_code; | 307 << " request has failed with response " << response_code; |
244 if (observer_) { | 308 if (observer_) { |
245 observer_->OnHeuristicsRequestError(it->second.form_signatures[0], | 309 observer_->OnHeuristicsRequestError(it->second.form_signatures[0], |
246 it->second.request_type, | 310 it->second.request_type, |
247 response_code); | 311 response_code); |
248 } | 312 } |
249 } else { | 313 } else { |
250 VLOG(1) << "AutoFillDownloadManager: " << type_of_request | 314 VLOG(1) << "AutoFillDownloadManager: " << type_of_request |
251 << " request has succeeded"; | 315 << " request has succeeded"; |
252 if (it->second.request_type == AutoFillDownloadManager::REQUEST_QUERY) { | 316 if (it->second.request_type == AutoFillDownloadManager::REQUEST_QUERY) { |
317 CacheQueryRequest(it->second.form_signatures, data); | |
253 if (observer_) | 318 if (observer_) |
254 observer_->OnLoadedAutoFillHeuristics(data); | 319 observer_->OnLoadedAutoFillHeuristics(data); |
255 } else { | 320 } else { |
256 double new_positive_upload_rate = 0; | 321 double new_positive_upload_rate = 0; |
257 double new_negative_upload_rate = 0; | 322 double new_negative_upload_rate = 0; |
258 AutoFillUploadXmlParser parse_handler(&new_positive_upload_rate, | 323 AutoFillUploadXmlParser parse_handler(&new_positive_upload_rate, |
259 &new_negative_upload_rate); | 324 &new_negative_upload_rate); |
260 buzz::XmlParser parser(&parse_handler); | 325 buzz::XmlParser parser(&parse_handler); |
261 parser.Parse(data.data(), data.length(), true); | 326 parser.Parse(data.data(), data.length(), true); |
262 if (parse_handler.succeeded()) { | 327 if (parse_handler.succeeded()) { |
263 SetPositiveUploadRate(new_positive_upload_rate); | 328 SetPositiveUploadRate(new_positive_upload_rate); |
264 SetNegativeUploadRate(new_negative_upload_rate); | 329 SetNegativeUploadRate(new_negative_upload_rate); |
265 } | 330 } |
266 | 331 |
267 if (observer_) | 332 if (observer_) |
268 observer_->OnUploadedAutoFillHeuristics(it->second.form_signatures[0]); | 333 observer_->OnUploadedAutoFillHeuristics(it->second.form_signatures[0]); |
269 } | 334 } |
270 } | 335 } |
271 delete it->first; | 336 delete it->first; |
272 url_fetchers_.erase(it); | 337 url_fetchers_.erase(it); |
273 } | 338 } |
OLD | NEW |