Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: components/autofill/browser/form_group.cc

Issue 14148004: [Autofill] Move IsValidState() into validation.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "components/autofill/browser/form_group.h" 5 #include "components/autofill/browser/form_group.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "components/autofill/browser/autofill_country.h" 14 #include "components/autofill/browser/autofill_country.h"
15 #include "components/autofill/browser/state_names.h"
16 #include "components/autofill/browser/validation.h"
15 #include "components/autofill/common/form_field_data.h" 17 #include "components/autofill/common/form_field_data.h"
16 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
17 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
18 20
19 namespace autofill { 21 namespace autofill {
20 namespace { 22 namespace {
21 23
22 // TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039.
23
24 class State {
25 public:
26 const char* name;
27 const char* abbreviation;
28
29 static const State all_states[];
30
31 static base::string16 Abbreviation(const base::string16& name);
32 static base::string16 FullName(const base::string16& abbreviation);
33 };
34
35 const State State::all_states[] = {
36 { "alabama", "al" },
37 { "alaska", "ak" },
38 { "arizona", "az" },
39 { "arkansas", "ar" },
40 { "california", "ca" },
41 { "colorado", "co" },
42 { "connecticut", "ct" },
43 { "delaware", "de" },
44 { "district of columbia", "dc" },
45 { "florida", "fl" },
46 { "georgia", "ga" },
47 { "hawaii", "hi" },
48 { "idaho", "id" },
49 { "illinois", "il" },
50 { "indiana", "in" },
51 { "iowa", "ia" },
52 { "kansas", "ks" },
53 { "kentucky", "ky" },
54 { "louisiana", "la" },
55 { "maine", "me" },
56 { "maryland", "md" },
57 { "massachusetts", "ma" },
58 { "michigan", "mi" },
59 { "minnesota", "mv" },
60 { "mississippi", "ms" },
61 { "missouri", "mo" },
62 { "montana", "mt" },
63 { "nebraska", "ne" },
64 { "nevada", "nv" },
65 { "new hampshire", "nh" },
66 { "new jersey", "nj" },
67 { "new mexico", "nm" },
68 { "new york", "ny" },
69 { "north carolina", "nc" },
70 { "north dakota", "nd" },
71 { "ohio", "oh" },
72 { "oklahoma", "ok" },
73 { "oregon", "or" },
74 { "pennsylvania", "pa" },
75 { "puerto rico", "pr" },
76 { "rhode island", "ri" },
77 { "south carolina", "sc" },
78 { "south dakota", "sd" },
79 { "tennessee", "tn" },
80 { "texas", "tx" },
81 { "utah", "ut" },
82 { "vermont", "vt" },
83 { "virginia", "va" },
84 { "washington", "wa" },
85 { "west virginia", "wv" },
86 { "wisconsin", "wi" },
87 { "wyoming", "wy" },
88 { NULL, NULL }
89 };
90
91 base::string16 State::Abbreviation(const base::string16& name) {
92 for (const State* state = all_states; state->name; ++state) {
93 if (LowerCaseEqualsASCII(name, state->name))
94 return ASCIIToUTF16(state->abbreviation);
95 }
96 return base::string16();
97 }
98
99 base::string16 State::FullName(const base::string16& abbreviation) {
100 for (const State* state = all_states; state->name; ++state) {
101 if (LowerCaseEqualsASCII(abbreviation, state->abbreviation))
102 return ASCIIToUTF16(state->name);
103 }
104 return base::string16();
105 }
106
107 const char* const kMonthsAbbreviated[] = { 24 const char* const kMonthsAbbreviated[] = {
108 NULL, // Padding so index 1 = month 1 = January. 25 NULL, // Padding so index 1 = month 1 = January.
109 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 26 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
110 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 27 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
111 }; 28 };
112 29
113 const char* const kMonthsFull[] = { 30 const char* const kMonthsFull[] = {
114 NULL, // Padding so index 1 = month 1 = January. 31 NULL, // Padding so index 1 = month 1 = January.
115 "January", "February", "March", "April", "May", "June", 32 "January", "February", "March", "April", "May", "June",
116 "July", "August", "September", "October", "November", "December", 33 "July", "August", "September", "October", "November", "December",
(...skipping 17 matching lines...) Expand all
134 field->value = field->option_values[i]; 51 field->value = field->option_values[i];
135 return true; 52 return true;
136 } 53 }
137 } 54 }
138 55
139 return false; 56 return false;
140 } 57 }
141 58
142 bool FillStateSelectControl(const base::string16& value, 59 bool FillStateSelectControl(const base::string16& value,
143 FormFieldData* field) { 60 FormFieldData* field) {
144 base::string16 abbrev, full; 61 base::string16 abbreviation;
Evan Stade 2013/04/15 18:55:34 is there a rule against this type of declaration?
Ilya Sherman 2013/04/16 01:12:25 Not really, I just disprefer it. Anyway, it's no
62 base::string16 full;
145 if (value.size() < 4U) { 63 if (value.size() < 4U) {
Evan Stade 2013/04/15 18:55:34 instead of this magic cutoff, why not just try Get
Ilya Sherman 2013/04/16 01:12:25 Done.
146 abbrev = value; 64 abbreviation = value;
147 full = State::FullName(value); 65 full = state_names::GetNameForAbbreviation(value);
148 } else { 66 } else {
149 abbrev = State::Abbreviation(value); 67 abbreviation = state_names::GetAbbreviationForName(value);
150 full = value; 68 full = value;
151 } 69 }
152 70
153 // Try the abbreviation name first. 71 // Try the abbreviation name first.
154 if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) 72 if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field))
155 return true; 73 return true;
156 74
157 if (full.empty()) 75 return !full.empty() && SetSelectControlValue(full, field);
158 return false;
159
160 return SetSelectControlValue(full, field);
161 } 76 }
162 77
163 bool FillExpirationMonthSelectControl(const base::string16& value, 78 bool FillExpirationMonthSelectControl(const base::string16& value,
164 FormFieldData* field) { 79 FormFieldData* field) {
165 int index = 0; 80 int index = 0;
166 if (!base::StringToInt(value, &index) || 81 if (!base::StringToInt(value, &index) ||
167 index <= 0 || 82 index <= 0 ||
168 static_cast<size_t>(index) >= arraysize(kMonthsFull)) 83 static_cast<size_t>(index) >= arraysize(kMonthsFull))
169 return false; 84 return false;
170 85
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } else if (type == CREDIT_CARD_TYPE) { 227 } else if (type == CREDIT_CARD_TYPE) {
313 FillCreditCardTypeSelectControl(field_text, field); 228 FillCreditCardTypeSelectControl(field_text, field);
314 } 229 }
315 } 230 }
316 231
317 bool FormGroup::FillCountrySelectControl(const std::string& app_locale, 232 bool FormGroup::FillCountrySelectControl(const std::string& app_locale,
318 FormFieldData* field_data) const { 233 FormFieldData* field_data) const {
319 return false; 234 return false;
320 } 235 }
321 236
322 // static
323 bool FormGroup::IsValidState(const base::string16& value) {
324 return !State::Abbreviation(value).empty() || !State::FullName(value).empty();
325 }
326
327 } // namespace autofill 237 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/browser/form_group.h ('k') | components/autofill/browser/personal_data_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698