Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <ostream> | 8 #include <ostream> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 const char kAutofillQueryServerNameStartInHeader[] = "GFE/"; | 31 const char kAutofillQueryServerNameStartInHeader[] = "GFE/"; |
| 32 | 32 |
| 33 const size_t kMaxFormCacheSize = 16; | 33 const size_t kMaxFormCacheSize = 16; |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 struct AutofillDownloadManager::FormRequestData { | 36 struct AutofillDownloadManager::FormRequestData { |
| 37 std::vector<std::string> form_signatures; | 37 std::vector<std::string> form_signatures; |
| 38 AutofillRequestType request_type; | 38 AutofillRequestType request_type; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 AutofillDownloadManager::AutofillDownloadManager(Profile* profile) | 41 AutofillDownloadManager::AutofillDownloadManager(Profile* profile, |
| 42 Observer* observer) | |
| 42 : profile_(profile), | 43 : profile_(profile), |
| 43 observer_(NULL), | 44 observer_(observer), |
| 44 max_form_cache_size_(kMaxFormCacheSize), | 45 max_form_cache_size_(kMaxFormCacheSize), |
| 45 next_query_request_(base::Time::Now()), | 46 next_query_request_(base::Time::Now()), |
| 46 next_upload_request_(base::Time::Now()), | 47 next_upload_request_(base::Time::Now()), |
| 47 positive_upload_rate_(0), | 48 positive_upload_rate_(0), |
| 48 negative_upload_rate_(0), | 49 negative_upload_rate_(0), |
| 49 fetcher_id_for_unittest_(0) { | 50 fetcher_id_for_unittest_(0) { |
| 50 // |profile_| could be NULL in some unit-tests. | 51 DCHECK(observer_); |
|
dhollowa
2011/10/26 15:58:33
Should have a DCHECK(profile_) here if you're elim
Ilya Sherman
2011/10/26 23:06:32
|profile_| is used on line 52, so the DCHECK would
dhollowa
2011/10/27 23:37:34
Ok, that's fine. But if PrefService has no virtua
| |
| 51 if (profile_) { | 52 PrefService* preferences = profile_->GetPrefs(); |
| 52 PrefService* preferences = profile_->GetPrefs(); | 53 positive_upload_rate_ = |
| 53 positive_upload_rate_ = | 54 preferences->GetDouble(prefs::kAutofillPositiveUploadRate); |
| 54 preferences->GetDouble(prefs::kAutofillPositiveUploadRate); | 55 negative_upload_rate_ = |
| 55 negative_upload_rate_ = | 56 preferences->GetDouble(prefs::kAutofillNegativeUploadRate); |
| 56 preferences->GetDouble(prefs::kAutofillNegativeUploadRate); | |
| 57 } | |
| 58 } | 57 } |
| 59 | 58 |
| 60 AutofillDownloadManager::~AutofillDownloadManager() { | 59 AutofillDownloadManager::~AutofillDownloadManager() { |
| 61 STLDeleteContainerPairFirstPointers(url_fetchers_.begin(), | 60 STLDeleteContainerPairFirstPointers(url_fetchers_.begin(), |
| 62 url_fetchers_.end()); | 61 url_fetchers_.end()); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void AutofillDownloadManager::SetObserver( | |
| 66 AutofillDownloadManager::Observer* observer) { | |
| 67 if (observer) { | |
| 68 DCHECK(!observer_); | |
| 69 observer_ = observer; | |
| 70 } else { | |
| 71 observer_ = NULL; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 bool AutofillDownloadManager::StartQueryRequest( | 64 bool AutofillDownloadManager::StartQueryRequest( |
| 76 const std::vector<FormStructure*>& forms, | 65 const std::vector<FormStructure*>& forms, |
| 77 const AutofillMetrics& metric_logger) { | 66 const AutofillMetrics& metric_logger) { |
| 78 if (next_query_request_ > base::Time::Now()) { | 67 if (next_query_request_ > base::Time::Now()) { |
| 79 // We are in back-off mode: do not do the request. | 68 // We are in back-off mode: do not do the request. |
| 80 return false; | 69 return false; |
| 81 } | 70 } |
| 82 std::string form_xml; | 71 std::string form_xml; |
| 83 FormRequestData request_data; | 72 FormRequestData request_data; |
| 84 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures, | 73 if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures, |
| 85 &form_xml)) { | 74 &form_xml)) { |
| 86 return false; | 75 return false; |
| 87 } | 76 } |
| 88 | 77 |
| 89 request_data.request_type = AutofillDownloadManager::REQUEST_QUERY; | 78 request_data.request_type = AutofillDownloadManager::REQUEST_QUERY; |
| 90 metric_logger.LogServerQueryMetric(AutofillMetrics::QUERY_SENT); | 79 metric_logger.LogServerQueryMetric(AutofillMetrics::QUERY_SENT); |
| 91 | 80 |
| 92 std::string query_data; | 81 std::string query_data; |
| 93 if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) { | 82 if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) { |
| 94 VLOG(1) << "AutofillDownloadManager: query request has been retrieved from" | 83 DVLOG(1) << "AutofillDownloadManager: query request has been retrieved from" |
| 95 << "the cache"; | 84 << "the cache"; |
| 96 if (observer_) | 85 observer_->OnLoadedServerPredictions(query_data); |
| 97 observer_->OnLoadedServerPredictions(query_data); | |
| 98 return true; | 86 return true; |
| 99 } | 87 } |
| 100 | 88 |
| 101 return StartRequest(form_xml, request_data); | 89 return StartRequest(form_xml, request_data); |
| 102 } | 90 } |
| 103 | 91 |
| 104 bool AutofillDownloadManager::StartUploadRequest( | 92 bool AutofillDownloadManager::StartUploadRequest( |
| 105 const FormStructure& form, | 93 const FormStructure& form, |
| 106 bool form_was_autofilled, | 94 bool form_was_autofilled, |
| 107 const FieldTypeSet& available_field_types) { | 95 const FieldTypeSet& available_field_types) { |
| 108 if (next_upload_request_ > base::Time::Now()) { | 96 if (next_upload_request_ > base::Time::Now()) { |
| 109 // We are in back-off mode: do not do the request. | 97 // We are in back-off mode: do not do the request. |
| 110 VLOG(1) << "AutofillDownloadManager: Upload request is throttled."; | 98 DVLOG(1) << "AutofillDownloadManager: Upload request is throttled."; |
| 111 return false; | 99 return false; |
| 112 } | 100 } |
| 113 | 101 |
| 114 // Flip a coin to see if we should upload this form. | 102 // Flip a coin to see if we should upload this form. |
| 115 double upload_rate = form_was_autofilled ? GetPositiveUploadRate() : | 103 double upload_rate = form_was_autofilled ? GetPositiveUploadRate() : |
| 116 GetNegativeUploadRate(); | 104 GetNegativeUploadRate(); |
| 117 if (form.upload_required() == UPLOAD_NOT_REQUIRED || | 105 if (form.upload_required() == UPLOAD_NOT_REQUIRED || |
| 118 (form.upload_required() == USE_UPLOAD_RATES && | 106 (form.upload_required() == USE_UPLOAD_RATES && |
| 119 base::RandDouble() > upload_rate)) { | 107 base::RandDouble() > upload_rate)) { |
| 120 VLOG(1) << "AutofillDownloadManager: Upload request is ignored."; | 108 DVLOG(1) << "AutofillDownloadManager: Upload request is ignored."; |
| 121 // If we ever need notification that upload was skipped, add it here. | 109 // If we ever need notification that upload was skipped, add it here. |
| 122 return false; | 110 return false; |
| 123 } | 111 } |
| 124 | 112 |
| 125 std::string form_xml; | 113 std::string form_xml; |
| 126 if (!form.EncodeUploadRequest(available_field_types, form_was_autofilled, | 114 if (!form.EncodeUploadRequest(available_field_types, form_was_autofilled, |
| 127 &form_xml)) | 115 &form_xml)) |
| 128 return false; | 116 return false; |
| 129 | 117 |
| 130 FormRequestData request_data; | 118 FormRequestData request_data; |
| 131 request_data.form_signatures.push_back(form.FormSignature()); | 119 request_data.form_signatures.push_back(form.FormSignature()); |
| 132 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD; | 120 request_data.request_type = AutofillDownloadManager::REQUEST_UPLOAD; |
| 133 | 121 |
| 134 return StartRequest(form_xml, request_data); | 122 return StartRequest(form_xml, request_data); |
| 135 } | 123 } |
| 136 | 124 |
| 137 bool AutofillDownloadManager::CancelRequest( | |
| 138 const std::string& form_signature, | |
| 139 AutofillDownloadManager::AutofillRequestType request_type) { | |
| 140 for (std::map<content::URLFetcher*, FormRequestData>::iterator it = | |
| 141 url_fetchers_.begin(); | |
| 142 it != url_fetchers_.end(); | |
| 143 ++it) { | |
| 144 if (std::find(it->second.form_signatures.begin(), | |
| 145 it->second.form_signatures.end(), form_signature) != | |
| 146 it->second.form_signatures.end() && | |
| 147 it->second.request_type == request_type) { | |
| 148 delete it->first; | |
| 149 url_fetchers_.erase(it); | |
| 150 return true; | |
| 151 } | |
| 152 } | |
| 153 return false; | |
| 154 } | |
| 155 | |
| 156 double AutofillDownloadManager::GetPositiveUploadRate() const { | 125 double AutofillDownloadManager::GetPositiveUploadRate() const { |
| 157 return positive_upload_rate_; | 126 return positive_upload_rate_; |
| 158 } | 127 } |
| 159 | 128 |
| 160 double AutofillDownloadManager::GetNegativeUploadRate() const { | 129 double AutofillDownloadManager::GetNegativeUploadRate() const { |
| 161 return negative_upload_rate_; | 130 return negative_upload_rate_; |
| 162 } | 131 } |
| 163 | 132 |
| 164 void AutofillDownloadManager::SetPositiveUploadRate(double rate) { | 133 void AutofillDownloadManager::SetPositiveUploadRate(double rate) { |
| 165 if (rate == positive_upload_rate_) | 134 if (rate == positive_upload_rate_) |
| 166 return; | 135 return; |
| 167 positive_upload_rate_ = rate; | 136 positive_upload_rate_ = rate; |
| 168 DCHECK_GE(rate, 0.0); | 137 DCHECK_GE(rate, 0.0); |
| 169 DCHECK_LE(rate, 1.0); | 138 DCHECK_LE(rate, 1.0); |
| 170 DCHECK(profile_); | |
| 171 PrefService* preferences = profile_->GetPrefs(); | 139 PrefService* preferences = profile_->GetPrefs(); |
| 172 preferences->SetDouble(prefs::kAutofillPositiveUploadRate, rate); | 140 preferences->SetDouble(prefs::kAutofillPositiveUploadRate, rate); |
| 173 } | 141 } |
| 174 | 142 |
| 175 void AutofillDownloadManager::SetNegativeUploadRate(double rate) { | 143 void AutofillDownloadManager::SetNegativeUploadRate(double rate) { |
| 176 if (rate == negative_upload_rate_) | 144 if (rate == negative_upload_rate_) |
| 177 return; | 145 return; |
| 178 negative_upload_rate_ = rate; | 146 negative_upload_rate_ = rate; |
| 179 DCHECK_GE(rate, 0.0); | 147 DCHECK_GE(rate, 0.0); |
| 180 DCHECK_LE(rate, 1.0); | 148 DCHECK_LE(rate, 1.0); |
| 181 DCHECK(profile_); | |
| 182 PrefService* preferences = profile_->GetPrefs(); | 149 PrefService* preferences = profile_->GetPrefs(); |
| 183 preferences->SetDouble(prefs::kAutofillNegativeUploadRate, rate); | 150 preferences->SetDouble(prefs::kAutofillNegativeUploadRate, rate); |
| 184 } | 151 } |
| 185 | 152 |
| 186 bool AutofillDownloadManager::StartRequest( | 153 bool AutofillDownloadManager::StartRequest( |
| 187 const std::string& form_xml, | 154 const std::string& form_xml, |
| 188 const FormRequestData& request_data) { | 155 const FormRequestData& request_data) { |
| 189 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); | 156 net::URLRequestContextGetter* request_context = profile_->GetRequestContext(); |
| 190 DCHECK(request_context); | 157 DCHECK(request_context); |
| 191 std::string request_url; | 158 std::string request_url; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 | 268 |
| 302 if (back_off) { | 269 if (back_off) { |
| 303 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); | 270 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); |
| 304 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 271 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
| 305 next_query_request_ = back_off_time; | 272 next_query_request_ = back_off_time; |
| 306 } else { | 273 } else { |
| 307 next_upload_request_ = back_off_time; | 274 next_upload_request_ = back_off_time; |
| 308 } | 275 } |
| 309 } | 276 } |
| 310 | 277 |
| 311 LOG(WARNING) << "AutofillDownloadManager: " << type_of_request | 278 DVLOG(1) << "AutofillDownloadManager: " << type_of_request |
| 312 << " request has failed with response " | 279 << " request has failed with response " |
| 313 << source->GetResponseCode(); | 280 << source->GetResponseCode(); |
| 314 if (observer_) { | 281 observer_->OnServerRequestError(it->second.form_signatures[0], |
| 315 observer_->OnServerRequestError(it->second.form_signatures[0], | 282 it->second.request_type, |
| 316 it->second.request_type, | 283 source->GetResponseCode()); |
| 317 source->GetResponseCode()); | |
| 318 } | |
| 319 } else { | 284 } else { |
| 320 VLOG(1) << "AutofillDownloadManager: " << type_of_request | 285 DVLOG(1) << "AutofillDownloadManager: " << type_of_request |
| 321 << " request has succeeded"; | 286 << " request has succeeded"; |
| 322 std::string response_body; | 287 std::string response_body; |
| 323 source->GetResponseAsString(&response_body); | 288 source->GetResponseAsString(&response_body); |
| 324 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 289 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
| 325 CacheQueryRequest(it->second.form_signatures, response_body); | 290 CacheQueryRequest(it->second.form_signatures, response_body); |
| 326 if (observer_) | 291 observer_->OnLoadedServerPredictions(response_body); |
| 327 observer_->OnLoadedServerPredictions(response_body); | |
| 328 } else { | 292 } else { |
| 329 double new_positive_upload_rate = 0; | 293 double new_positive_upload_rate = 0; |
| 330 double new_negative_upload_rate = 0; | 294 double new_negative_upload_rate = 0; |
| 331 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, | 295 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, |
| 332 &new_negative_upload_rate); | 296 &new_negative_upload_rate); |
| 333 buzz::XmlParser parser(&parse_handler); | 297 buzz::XmlParser parser(&parse_handler); |
| 334 parser.Parse(response_body.data(), response_body.length(), true); | 298 parser.Parse(response_body.data(), response_body.length(), true); |
| 335 if (parse_handler.succeeded()) { | 299 if (parse_handler.succeeded()) { |
| 336 SetPositiveUploadRate(new_positive_upload_rate); | 300 SetPositiveUploadRate(new_positive_upload_rate); |
| 337 SetNegativeUploadRate(new_negative_upload_rate); | 301 SetNegativeUploadRate(new_negative_upload_rate); |
| 338 } | 302 } |
| 339 | 303 |
| 340 if (observer_) | 304 observer_->OnUploadedPossibleFieldTypes(); |
| 341 observer_->OnUploadedPossibleFieldTypes(); | |
| 342 } | 305 } |
| 343 } | 306 } |
| 344 delete it->first; | 307 delete it->first; |
| 345 url_fetchers_.erase(it); | 308 url_fetchers_.erase(it); |
| 346 } | 309 } |
| OLD | NEW |