| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/autocomplete_history_manager.h" | 5 #include "chrome/browser/autocomplete_history_manager.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/autofill/credit_card.h" | 11 #include "chrome/browser/autofill/credit_card.h" |
| 12 #include "chrome/browser/pref_service.h" | 12 #include "chrome/browser/pref_service.h" |
| 13 #include "chrome/browser/profile.h" | 13 #include "chrome/browser/profile.h" |
| 14 #include "chrome/browser/renderer_host/render_view_host.h" | 14 #include "chrome/browser/renderer_host/render_view_host.h" |
| 15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 17 #include "webkit/glue/form_data.h" | 17 #include "webkit/glue/form_data.h" |
| 18 | 18 |
| 19 using webkit_glue::FormData; | 19 using webkit_glue::FormData; |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Limit on the number of suggestions to appear in the pop-up menu under an | 23 // Limit on the number of suggestions to appear in the pop-up menu under an |
| 24 // text input element in a form. | 24 // text input element in a form. |
| 25 const int kMaxAutocompleteMenuItems = 6; | 25 const int kMaxAutocompleteMenuItems = 6; |
| 26 | 26 |
| 27 // The separator characters for credit card values. | 27 // The separator characters for SSNs. |
| 28 const string16 kCreditCardSeparators = ASCIIToUTF16(" -"); | 28 const string16 kSSNSeparators = ASCIIToUTF16(" -"); |
| 29 |
| 30 bool IsSSN(const string16& text) { |
| 31 string16 number_string; |
| 32 RemoveChars(text, kSSNSeparators.c_str(), &number_string); |
| 33 if (number_string.length() != 9) |
| 34 return false; |
| 35 |
| 36 // A SSN is of the form AAA-GG-SSSS (A = area number, G = group number, S = |
| 37 // serial number). The validation we do here is simply checking if the area, |
| 38 // group, and serial numbers are valid. It is possible to check if the group |
| 39 // number is valid for the given area, but that data changes all the time. |
| 40 // |
| 41 // See: http://www.socialsecurity.gov/history/ssn/geocard.html |
| 42 // http://www.socialsecurity.gov/employer/stateweb.htm |
| 43 // http://www.socialsecurity.gov/employer/ssnvhighgroup.htm |
| 44 |
| 45 string16 area_string = number_string.substr(0, 3); |
| 46 string16 group_string = number_string.substr(3, 2); |
| 47 string16 serial_string = number_string.substr(5, 4); |
| 48 |
| 49 int area = StringToInt(area_string); |
| 50 if (area < 1 || |
| 51 area == 666 || |
| 52 area > 733 && area < 750 || |
| 53 area > 772) |
| 54 return false; |
| 55 |
| 56 int group = StringToInt(group_string); |
| 57 if (group == 0) |
| 58 return false; |
| 59 |
| 60 int serial = StringToInt(serial_string); |
| 61 if (serial == 0) |
| 62 return false; |
| 63 |
| 64 return true; |
| 65 } |
| 29 | 66 |
| 30 } // namespace | 67 } // namespace |
| 31 | 68 |
| 32 AutocompleteHistoryManager::AutocompleteHistoryManager( | 69 AutocompleteHistoryManager::AutocompleteHistoryManager( |
| 33 TabContents* tab_contents) : tab_contents_(tab_contents), | 70 TabContents* tab_contents) : tab_contents_(tab_contents), |
| 34 pending_query_handle_(0), | 71 pending_query_handle_(0), |
| 35 query_id_(0) { | 72 query_id_(0) { |
| 36 DCHECK(tab_contents); | 73 DCHECK(tab_contents); |
| 37 | 74 |
| 38 profile_ = tab_contents_->profile(); | 75 profile_ = tab_contents_->profile(); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // - non-empty value | 152 // - non-empty value |
| 116 // - text field | 153 // - text field |
| 117 // - value is not a credit card number | 154 // - value is not a credit card number |
| 118 std::vector<webkit_glue::FormField> values; | 155 std::vector<webkit_glue::FormField> values; |
| 119 for (std::vector<webkit_glue::FormField>::const_iterator iter = | 156 for (std::vector<webkit_glue::FormField>::const_iterator iter = |
| 120 form.fields.begin(); | 157 form.fields.begin(); |
| 121 iter != form.fields.end(); ++iter) { | 158 iter != form.fields.end(); ++iter) { |
| 122 if (!iter->value().empty() && | 159 if (!iter->value().empty() && |
| 123 !iter->name().empty() && | 160 !iter->name().empty() && |
| 124 iter->form_control_type() == ASCIIToUTF16("text") && | 161 iter->form_control_type() == ASCIIToUTF16("text") && |
| 125 !CreditCard::IsCreditCardNumber(iter->value())) | 162 !CreditCard::IsCreditCardNumber(iter->value()) && |
| 163 !IsSSN(iter->value())) |
| 126 values.push_back(*iter); | 164 values.push_back(*iter); |
| 127 } | 165 } |
| 128 | 166 |
| 129 if (!values.empty()) | 167 if (!values.empty()) |
| 130 web_data_service_->AddFormFields(values); | 168 web_data_service_->AddFormFields(values); |
| 131 } | 169 } |
| 132 | 170 |
| 133 void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) { | 171 void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) { |
| 134 RenderViewHost* host = tab_contents_->render_view_host(); | 172 RenderViewHost* host = tab_contents_->render_view_host(); |
| 135 if (!host) | 173 if (!host) |
| 136 return; | 174 return; |
| 137 | 175 |
| 138 if (result) { | 176 if (result) { |
| 139 DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT); | 177 DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT); |
| 140 const WDResult<std::vector<string16> >* autofill_result = | 178 const WDResult<std::vector<string16> >* autofill_result = |
| 141 static_cast<const WDResult<std::vector<string16> >*>(result); | 179 static_cast<const WDResult<std::vector<string16> >*>(result); |
| 142 host->AutocompleteSuggestionsReturned( | 180 host->AutocompleteSuggestionsReturned( |
| 143 query_id_, autofill_result->GetValue(), -1); | 181 query_id_, autofill_result->GetValue(), -1); |
| 144 } else { | 182 } else { |
| 145 host->AutocompleteSuggestionsReturned( | 183 host->AutocompleteSuggestionsReturned( |
| 146 query_id_, std::vector<string16>(), -1); | 184 query_id_, std::vector<string16>(), -1); |
| 147 } | 185 } |
| 148 } | 186 } |
| OLD | NEW |