Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_manager.h" | 5 #include "chrome/browser/autofill/autofill_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/guid.h" | 16 #include "base/guid.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/prefs/public/pref_service_base.h" | 18 #include "base/prefs/public/pref_service_base.h" |
| 19 #include "base/string16.h" | 19 #include "base/string16.h" |
| 20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "base/supports_user_data.h" | 21 #include "base/supports_user_data.h" |
| 22 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 23 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
| 24 #include "chrome/browser/api/infobars/infobar_service.h" | 24 #include "chrome/browser/api/infobars/infobar_service.h" |
| 25 #include "chrome/browser/api/sync/profile_sync_service_base.h" | 25 #include "chrome/browser/api/sync/profile_sync_service_base.h" |
| 26 #include "chrome/browser/autofill/autocheckout/whitelist_manager.h" | |
| 26 #include "chrome/browser/autofill/autocomplete_history_manager.h" | 27 #include "chrome/browser/autofill/autocomplete_history_manager.h" |
| 27 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 28 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 28 #include "chrome/browser/autofill/autofill_country.h" | 29 #include "chrome/browser/autofill/autofill_country.h" |
| 29 #include "chrome/browser/autofill/autofill_external_delegate.h" | 30 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 30 #include "chrome/browser/autofill/autofill_field.h" | 31 #include "chrome/browser/autofill/autofill_field.h" |
| 31 #include "chrome/browser/autofill/autofill_manager_delegate.h" | 32 #include "chrome/browser/autofill/autofill_manager_delegate.h" |
| 32 #include "chrome/browser/autofill/autofill_metrics.h" | 33 #include "chrome/browser/autofill/autofill_metrics.h" |
| 33 #include "chrome/browser/autofill/autofill_profile.h" | 34 #include "chrome/browser/autofill/autofill_profile.h" |
| 34 #include "chrome/browser/autofill/autofill_type.h" | 35 #include "chrome/browser/autofill/autofill_type.h" |
| 35 #include "chrome/browser/autofill/credit_card.h" | 36 #include "chrome/browser/autofill/credit_card.h" |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 return false; | 385 return false; |
| 385 | 386 |
| 386 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) | 387 if (web_contents()->GetBrowserContext()->IsOffTheRecord()) |
| 387 return false; | 388 return false; |
| 388 | 389 |
| 389 // Don't save data that was submitted through JavaScript. | 390 // Don't save data that was submitted through JavaScript. |
| 390 if (!form.user_submitted) | 391 if (!form.user_submitted) |
| 391 return false; | 392 return false; |
| 392 | 393 |
| 393 // Grab a copy of the form data. | 394 // Grab a copy of the form data. |
| 394 scoped_ptr<FormStructure> submitted_form(new FormStructure(form)); | 395 scoped_ptr<FormStructure> submitted_form( |
| 396 new FormStructure(form, IsAutocheckoutEnabled())); | |
| 395 | 397 |
| 396 // Disregard forms that we wouldn't ever autofill in the first place. | 398 // Disregard forms that we wouldn't ever autofill in the first place. |
| 397 if (!submitted_form->ShouldBeParsed(true)) | 399 if (!submitted_form->ShouldBeParsed(true)) |
| 398 return false; | 400 return false; |
| 399 | 401 |
| 400 // Ignore forms not present in our cache. These are typically forms with | 402 // Ignore forms not present in our cache. These are typically forms with |
| 401 // wonky JavaScript that also makes them not auto-fillable. | 403 // wonky JavaScript that also makes them not auto-fillable. |
| 402 FormStructure* cached_submitted_form; | 404 FormStructure* cached_submitted_form; |
| 403 if (!FindCachedForm(form, &cached_submitted_form)) | 405 if (!FindCachedForm(form, &cached_submitted_form)) |
| 404 return false; | 406 return false; |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 832 | 834 |
| 833 // If the corresponding flag is set, annotate forms with the predicted types. | 835 // If the corresponding flag is set, annotate forms with the predicted types. |
| 834 SendAutofillTypePredictions(form_structures_.get()); | 836 SendAutofillTypePredictions(form_structures_.get()); |
| 835 } | 837 } |
| 836 | 838 |
| 837 void AutofillManager::OnDidEndTextFieldEditing() { | 839 void AutofillManager::OnDidEndTextFieldEditing() { |
| 838 if (external_delegate_) | 840 if (external_delegate_) |
| 839 external_delegate_->DidEndTextFieldEditing(); | 841 external_delegate_->DidEndTextFieldEditing(); |
| 840 } | 842 } |
| 841 | 843 |
| 844 bool AutofillManager::IsAutocheckoutEnabled() const{ | |
|
ahutter
2013/01/19 02:07:16
missing space
benquan
2013/01/23 23:50:53
Done.
| |
| 845 if (!web_contents()) return false; | |
|
ahutter
2013/01/19 02:07:16
newline
benquan
2013/01/23 23:50:53
Done.
| |
| 846 autocheckout::WhitelistManager* wm = | |
| 847 autocheckout::WhitelistManager::GetForBrowserContext( | |
| 848 web_contents()->GetBrowserContext()); | |
| 849 return wm->IsAutocheckoutEnabled(web_contents()->GetURL()); | |
|
ahutter
2013/01/19 02:07:16
does web_contents() always refer to url in the nav
benquan
2013/01/23 23:50:53
it refers to the tab the AutofillManager associate
| |
| 850 } | |
| 851 | |
| 842 bool AutofillManager::IsAutofillEnabled() const { | 852 bool AutofillManager::IsAutofillEnabled() const { |
| 843 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); | 853 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled) || |
| 854 IsAutocheckoutEnabled(); | |
| 844 } | 855 } |
| 845 | 856 |
| 846 void AutofillManager::SendAutofillTypePredictions( | 857 void AutofillManager::SendAutofillTypePredictions( |
| 847 const std::vector<FormStructure*>& forms) const { | 858 const std::vector<FormStructure*>& forms) const { |
| 848 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 859 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 849 switches::kShowAutofillTypePredictions)) | 860 switches::kShowAutofillTypePredictions)) |
| 850 return; | 861 return; |
| 851 | 862 |
| 852 RenderViewHost* host = web_contents()->GetRenderViewHost(); | 863 RenderViewHost* host = web_contents()->GetRenderViewHost(); |
| 853 if (!host) | 864 if (!host) |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1031 } | 1042 } |
| 1032 | 1043 |
| 1033 bool AutofillManager::GetCachedFormAndField(const FormData& form, | 1044 bool AutofillManager::GetCachedFormAndField(const FormData& form, |
| 1034 const FormFieldData& field, | 1045 const FormFieldData& field, |
| 1035 FormStructure** form_structure, | 1046 FormStructure** form_structure, |
| 1036 AutofillField** autofill_field) { | 1047 AutofillField** autofill_field) { |
| 1037 // Find the FormStructure that corresponds to |form|. | 1048 // Find the FormStructure that corresponds to |form|. |
| 1038 // If we do not have this form in our cache but it is parseable, we'll add it | 1049 // If we do not have this form in our cache but it is parseable, we'll add it |
| 1039 // in the call to |UpdateCachedForm()|. | 1050 // in the call to |UpdateCachedForm()|. |
| 1040 if (!FindCachedForm(form, form_structure) && | 1051 if (!FindCachedForm(form, form_structure) && |
| 1041 !FormStructure(form).ShouldBeParsed(false)) { | 1052 !FormStructure(form, IsAutocheckoutEnabled()).ShouldBeParsed(false)) { |
| 1042 return false; | 1053 return false; |
| 1043 } | 1054 } |
| 1044 | 1055 |
| 1045 // Update the cached form to reflect any dynamic changes to the form data, if | 1056 // Update the cached form to reflect any dynamic changes to the form data, if |
| 1046 // necessary. | 1057 // necessary. |
| 1047 if (!UpdateCachedForm(form, *form_structure, form_structure)) | 1058 if (!UpdateCachedForm(form, *form_structure, form_structure)) |
| 1048 return false; | 1059 return false; |
| 1049 | 1060 |
| 1050 // No data to return if there are no auto-fillable fields. | 1061 // No data to return if there are no auto-fillable fields. |
| 1051 if (!(*form_structure)->autofill_count()) | 1062 if (!(*form_structure)->autofill_count()) |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1078 needs_update = *cached_form->field(i) != live_form.fields[i]; | 1089 needs_update = *cached_form->field(i) != live_form.fields[i]; |
| 1079 } | 1090 } |
| 1080 | 1091 |
| 1081 if (!needs_update) | 1092 if (!needs_update) |
| 1082 return true; | 1093 return true; |
| 1083 | 1094 |
| 1084 if (form_structures_.size() >= kMaxFormCacheSize) | 1095 if (form_structures_.size() >= kMaxFormCacheSize) |
| 1085 return false; | 1096 return false; |
| 1086 | 1097 |
| 1087 // Add the new or updated form to our cache. | 1098 // Add the new or updated form to our cache. |
| 1088 form_structures_.push_back(new FormStructure(live_form)); | 1099 form_structures_.push_back( |
| 1100 new FormStructure(live_form, IsAutocheckoutEnabled())); | |
| 1089 *updated_form = *form_structures_.rbegin(); | 1101 *updated_form = *form_structures_.rbegin(); |
| 1090 (*updated_form)->DetermineHeuristicTypes(*metric_logger_); | 1102 (*updated_form)->DetermineHeuristicTypes(*metric_logger_); |
| 1091 | 1103 |
| 1092 // If we have cached data, propagate it to the updated form. | 1104 // If we have cached data, propagate it to the updated form. |
| 1093 if (cached_form) { | 1105 if (cached_form) { |
| 1094 std::map<string16, const AutofillField*> cached_fields; | 1106 std::map<string16, const AutofillField*> cached_fields; |
| 1095 for (size_t i = 0; i < cached_form->field_count(); ++i) { | 1107 for (size_t i = 0; i < cached_form->field_count(); ++i) { |
| 1096 const AutofillField* field = cached_form->field(i); | 1108 const AutofillField* field = cached_form->field(i); |
| 1097 cached_fields[field->unique_name()] = field; | 1109 cached_fields[field->unique_name()] = field; |
| 1098 } | 1110 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1177 labels->push_back(label); | 1189 labels->push_back(label); |
| 1178 icons->push_back(UTF8ToUTF16(credit_card->type())); | 1190 icons->push_back(UTF8ToUTF16(credit_card->type())); |
| 1179 unique_ids->push_back(PackGUIDs(GUIDPair(credit_card->guid(), 0), | 1191 unique_ids->push_back(PackGUIDs(GUIDPair(credit_card->guid(), 0), |
| 1180 GUIDPair(std::string(), 0))); | 1192 GUIDPair(std::string(), 0))); |
| 1181 } | 1193 } |
| 1182 } | 1194 } |
| 1183 } | 1195 } |
| 1184 | 1196 |
| 1185 void AutofillManager::ParseForms(const std::vector<FormData>& forms) { | 1197 void AutofillManager::ParseForms(const std::vector<FormData>& forms) { |
| 1186 std::vector<FormStructure*> non_queryable_forms; | 1198 std::vector<FormStructure*> non_queryable_forms; |
| 1199 bool autocheckout_enabled = IsAutocheckoutEnabled(); | |
| 1187 for (std::vector<FormData>::const_iterator iter = forms.begin(); | 1200 for (std::vector<FormData>::const_iterator iter = forms.begin(); |
| 1188 iter != forms.end(); ++iter) { | 1201 iter != forms.end(); ++iter) { |
| 1189 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); | 1202 scoped_ptr<FormStructure> form_structure( |
| 1203 new FormStructure(*iter, autocheckout_enabled)); | |
| 1190 if (!form_structure->ShouldBeParsed(false)) | 1204 if (!form_structure->ShouldBeParsed(false)) |
| 1191 continue; | 1205 continue; |
| 1192 | 1206 |
| 1193 form_structure->DetermineHeuristicTypes(*metric_logger_); | 1207 form_structure->DetermineHeuristicTypes(*metric_logger_); |
| 1194 | 1208 |
| 1195 // Set aside forms with method GET or author-specified types, so that they | 1209 // Set aside forms with method GET or author-specified types, so that they |
| 1196 // are not included in the query to the server. | 1210 // are not included in the query to the server. |
| 1197 if (form_structure->ShouldBeCrowdsourced()) | 1211 if (form_structure->ShouldBeCrowdsourced()) |
| 1198 form_structures_.push_back(form_structure.release()); | 1212 form_structures_.push_back(form_structure.release()); |
| 1199 else | 1213 else |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1277 *profile_guid = IDToGUID(profile_id); | 1291 *profile_guid = IDToGUID(profile_id); |
| 1278 } | 1292 } |
| 1279 | 1293 |
| 1280 void AutofillManager::UpdateInitialInteractionTimestamp( | 1294 void AutofillManager::UpdateInitialInteractionTimestamp( |
| 1281 const TimeTicks& interaction_timestamp) { | 1295 const TimeTicks& interaction_timestamp) { |
| 1282 if (initial_interaction_timestamp_.is_null() || | 1296 if (initial_interaction_timestamp_.is_null() || |
| 1283 interaction_timestamp < initial_interaction_timestamp_) { | 1297 interaction_timestamp < initial_interaction_timestamp_) { |
| 1284 initial_interaction_timestamp_ = interaction_timestamp; | 1298 initial_interaction_timestamp_ = interaction_timestamp; |
| 1285 } | 1299 } |
| 1286 } | 1300 } |
| OLD | NEW |