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_); |
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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 | 267 |
301 if (back_off) { | 268 if (back_off) { |
302 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); | 269 base::Time back_off_time(base::Time::Now() + source->GetBackoffDelay()); |
303 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 270 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
304 next_query_request_ = back_off_time; | 271 next_query_request_ = back_off_time; |
305 } else { | 272 } else { |
306 next_upload_request_ = back_off_time; | 273 next_upload_request_ = back_off_time; |
307 } | 274 } |
308 } | 275 } |
309 | 276 |
310 LOG(WARNING) << "AutofillDownloadManager: " << type_of_request | 277 DVLOG(1) << "AutofillDownloadManager: " << type_of_request |
311 << " request has failed with response " | 278 << " request has failed with response " |
312 << source->GetResponseCode(); | 279 << source->GetResponseCode(); |
313 if (observer_) { | 280 observer_->OnServerRequestError(it->second.form_signatures[0], |
314 observer_->OnServerRequestError(it->second.form_signatures[0], | 281 it->second.request_type, |
315 it->second.request_type, | 282 source->GetResponseCode()); |
316 source->GetResponseCode()); | |
317 } | |
318 } else { | 283 } else { |
319 VLOG(1) << "AutofillDownloadManager: " << type_of_request | 284 DVLOG(1) << "AutofillDownloadManager: " << type_of_request |
320 << " request has succeeded"; | 285 << " request has succeeded"; |
321 std::string response_body; | 286 std::string response_body; |
322 source->GetResponseAsString(&response_body); | 287 source->GetResponseAsString(&response_body); |
323 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { | 288 if (it->second.request_type == AutofillDownloadManager::REQUEST_QUERY) { |
324 CacheQueryRequest(it->second.form_signatures, response_body); | 289 CacheQueryRequest(it->second.form_signatures, response_body); |
325 if (observer_) | 290 observer_->OnLoadedServerPredictions(response_body); |
326 observer_->OnLoadedServerPredictions(response_body); | |
327 } else { | 291 } else { |
328 double new_positive_upload_rate = 0; | 292 double new_positive_upload_rate = 0; |
329 double new_negative_upload_rate = 0; | 293 double new_negative_upload_rate = 0; |
330 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, | 294 AutofillUploadXmlParser parse_handler(&new_positive_upload_rate, |
331 &new_negative_upload_rate); | 295 &new_negative_upload_rate); |
332 buzz::XmlParser parser(&parse_handler); | 296 buzz::XmlParser parser(&parse_handler); |
333 parser.Parse(response_body.data(), response_body.length(), true); | 297 parser.Parse(response_body.data(), response_body.length(), true); |
334 if (parse_handler.succeeded()) { | 298 if (parse_handler.succeeded()) { |
335 SetPositiveUploadRate(new_positive_upload_rate); | 299 SetPositiveUploadRate(new_positive_upload_rate); |
336 SetNegativeUploadRate(new_negative_upload_rate); | 300 SetNegativeUploadRate(new_negative_upload_rate); |
337 } | 301 } |
338 | 302 |
339 if (observer_) | 303 observer_->OnUploadedPossibleFieldTypes(); |
340 observer_->OnUploadedPossibleFieldTypes(); | |
341 } | 304 } |
342 } | 305 } |
343 delete it->first; | 306 delete it->first; |
344 url_fetchers_.erase(it); | 307 url_fetchers_.erase(it); |
345 } | 308 } |
OLD | NEW |