diff options
author | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-17 00:23:27 +0000 |
---|---|---|
committer | isherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-17 00:23:27 +0000 |
commit | 5efd462dd2e75dbaecd489938859ff2e91550fdd (patch) | |
tree | da33623af72dcc1464b1b9c2ad6c74da785f646a /components | |
parent | adcb45692778cbc5101e82a9f5ccff65cdc37d83 (diff) | |
download | chromium_src-5efd462dd2e75dbaecd489938859ff2e91550fdd.zip chromium_src-5efd462dd2e75dbaecd489938859ff2e91550fdd.tar.gz chromium_src-5efd462dd2e75dbaecd489938859ff2e91550fdd.tar.bz2 |
[Autofill] Move IsValidState() into validation.h
BUG=none
Review URL: https://codereview.chromium.org/14148004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194497 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r-- | components/autofill.gypi | 2 | ||||
-rw-r--r-- | components/autofill/browser/form_group.cc | 111 | ||||
-rw-r--r-- | components/autofill/browser/form_group.h | 5 | ||||
-rw-r--r-- | components/autofill/browser/personal_data_manager.cc | 3 | ||||
-rw-r--r-- | components/autofill/browser/state_names.cc | 99 | ||||
-rw-r--r-- | components/autofill/browser/state_names.h | 24 | ||||
-rw-r--r-- | components/autofill/browser/validation.cc | 10 | ||||
-rw-r--r-- | components/autofill/browser/validation.h | 4 |
8 files changed, 147 insertions, 111 deletions
diff --git a/components/autofill.gypi b/components/autofill.gypi index 00f31c0..1fa9d96 100644 --- a/components/autofill.gypi +++ b/components/autofill.gypi @@ -206,6 +206,8 @@ 'autofill/browser/phone_number_i18n.h', 'autofill/browser/risk/fingerprint.cc', 'autofill/browser/risk/fingerprint.h', + 'autofill/browser/state_names.cc', + 'autofill/browser/state_names.h', 'autofill/browser/validation.cc', 'autofill/browser/validation.h', 'autofill/browser/wallet/cart.cc', diff --git a/components/autofill/browser/form_group.cc b/components/autofill/browser/form_group.cc index 82031f0..8e583e5 100644 --- a/components/autofill/browser/form_group.cc +++ b/components/autofill/browser/form_group.cc @@ -12,6 +12,8 @@ #include "base/strings/string_number_conversions.h" #include "base/utf_string_conversions.h" #include "components/autofill/browser/autofill_country.h" +#include "components/autofill/browser/state_names.h" +#include "components/autofill/browser/validation.h" #include "components/autofill/common/form_field_data.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" @@ -19,91 +21,6 @@ namespace autofill { namespace { -// TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. - -class State { - public: - const char* name; - const char* abbreviation; - - static const State all_states[]; - - static base::string16 Abbreviation(const base::string16& name); - static base::string16 FullName(const base::string16& abbreviation); -}; - -const State State::all_states[] = { - { "alabama", "al" }, - { "alaska", "ak" }, - { "arizona", "az" }, - { "arkansas", "ar" }, - { "california", "ca" }, - { "colorado", "co" }, - { "connecticut", "ct" }, - { "delaware", "de" }, - { "district of columbia", "dc" }, - { "florida", "fl" }, - { "georgia", "ga" }, - { "hawaii", "hi" }, - { "idaho", "id" }, - { "illinois", "il" }, - { "indiana", "in" }, - { "iowa", "ia" }, - { "kansas", "ks" }, - { "kentucky", "ky" }, - { "louisiana", "la" }, - { "maine", "me" }, - { "maryland", "md" }, - { "massachusetts", "ma" }, - { "michigan", "mi" }, - { "minnesota", "mv" }, - { "mississippi", "ms" }, - { "missouri", "mo" }, - { "montana", "mt" }, - { "nebraska", "ne" }, - { "nevada", "nv" }, - { "new hampshire", "nh" }, - { "new jersey", "nj" }, - { "new mexico", "nm" }, - { "new york", "ny" }, - { "north carolina", "nc" }, - { "north dakota", "nd" }, - { "ohio", "oh" }, - { "oklahoma", "ok" }, - { "oregon", "or" }, - { "pennsylvania", "pa" }, - { "puerto rico", "pr" }, - { "rhode island", "ri" }, - { "south carolina", "sc" }, - { "south dakota", "sd" }, - { "tennessee", "tn" }, - { "texas", "tx" }, - { "utah", "ut" }, - { "vermont", "vt" }, - { "virginia", "va" }, - { "washington", "wa" }, - { "west virginia", "wv" }, - { "wisconsin", "wi" }, - { "wyoming", "wy" }, - { NULL, NULL } -}; - -base::string16 State::Abbreviation(const base::string16& name) { - for (const State* state = all_states; state->name; ++state) { - if (LowerCaseEqualsASCII(name, state->name)) - return ASCIIToUTF16(state->abbreviation); - } - return base::string16(); -} - -base::string16 State::FullName(const base::string16& abbreviation) { - for (const State* state = all_states; state->name; ++state) { - if (LowerCaseEqualsASCII(abbreviation, state->abbreviation)) - return ASCIIToUTF16(state->name); - } - return base::string16(); -} - const char* const kMonthsAbbreviated[] = { NULL, // Padding so index 1 = month 1 = January. "Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -141,23 +58,18 @@ bool SetSelectControlValue(const base::string16& value, bool FillStateSelectControl(const base::string16& value, FormFieldData* field) { - base::string16 abbrev, full; - if (value.size() < 4U) { - abbrev = value; - full = State::FullName(value); - } else { - abbrev = State::Abbreviation(value); + base::string16 abbreviation = value; + base::string16 full = state_names::GetNameForAbbreviation(value); + if (full.empty()) { + abbreviation = state_names::GetAbbreviationForName(value); full = value; } - // Try the abbreviation name first. - if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) + // Try the abbreviation first. + if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field)) return true; - if (full.empty()) - return false; - - return SetSelectControlValue(full, field); + return !full.empty() && SetSelectControlValue(full, field); } bool FillExpirationMonthSelectControl(const base::string16& value, @@ -319,9 +231,4 @@ bool FormGroup::FillCountrySelectControl(const std::string& app_locale, return false; } -// static -bool FormGroup::IsValidState(const base::string16& value) { - return !State::Abbreviation(value).empty() || !State::FullName(value).empty(); -} - } // namespace autofill diff --git a/components/autofill/browser/form_group.h b/components/autofill/browser/form_group.h index 931b51f..9450758 100644 --- a/components/autofill/browser/form_group.h +++ b/components/autofill/browser/form_group.h @@ -75,11 +75,6 @@ class FormGroup { const std::string& app_locale, FormFieldData* field_data) const; - // Returns true if |value| is a valid US state name or abbreviation. It is - // case insensitive. Valid for US states only. - // TODO(estade): this is a crappy place for this function. - static bool IsValidState(const base::string16& value); - protected: // AutofillProfile needs to call into GetSupportedTypes() for objects of // non-AutofillProfile type, for which mere inheritance is insufficient. diff --git a/components/autofill/browser/personal_data_manager.cc b/components/autofill/browser/personal_data_manager.cc index c80d85e..481ca24 100644 --- a/components/autofill/browser/personal_data_manager.cc +++ b/components/autofill/browser/personal_data_manager.cc @@ -17,7 +17,6 @@ #include "components/autofill/browser/autofill_country.h" #include "components/autofill/browser/autofill_field.h" #include "components/autofill/browser/autofill_metrics.h" -#include "components/autofill/browser/form_group.h" #include "components/autofill/browser/form_structure.h" #include "components/autofill/browser/personal_data_manager_observer.h" #include "components/autofill/browser/phone_number.h" @@ -689,7 +688,7 @@ bool PersonalDataManager::IsValidLearnableProfile( // Reject profiles with invalid US state information. base::string16 state = profile.GetRawInfo(ADDRESS_HOME_STATE); if (profile.GetRawInfo(ADDRESS_HOME_COUNTRY) == ASCIIToUTF16("US") && - !state.empty() && !FormGroup::IsValidState(state)) { + !state.empty() && !IsValidState(state)) { return false; } diff --git a/components/autofill/browser/state_names.cc b/components/autofill/browser/state_names.cc new file mode 100644 index 0000000..3c28ef3 --- /dev/null +++ b/components/autofill/browser/state_names.cc @@ -0,0 +1,99 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/autofill/browser/state_names.h" + +#include "base/basictypes.h" +#include "base/string_util.h" +#include "base/utf_string_conversions.h" + +namespace autofill { +namespace state_names { + +namespace { + +// TODO(jhawkins): Add more states/provinces. See http://crbug.com/45039. + +struct StateData { + const char* const name; + const char abbreviation[3]; +}; + +StateData kStateData[] = { + { "alabama", "al" }, + { "alaska", "ak" }, + { "arizona", "az" }, + { "arkansas", "ar" }, + { "california", "ca" }, + { "colorado", "co" }, + { "connecticut", "ct" }, + { "delaware", "de" }, + { "district of columbia", "dc" }, + { "florida", "fl" }, + { "georgia", "ga" }, + { "hawaii", "hi" }, + { "idaho", "id" }, + { "illinois", "il" }, + { "indiana", "in" }, + { "iowa", "ia" }, + { "kansas", "ks" }, + { "kentucky", "ky" }, + { "louisiana", "la" }, + { "maine", "me" }, + { "maryland", "md" }, + { "massachusetts", "ma" }, + { "michigan", "mi" }, + { "minnesota", "mv" }, + { "mississippi", "ms" }, + { "missouri", "mo" }, + { "montana", "mt" }, + { "nebraska", "ne" }, + { "nevada", "nv" }, + { "new hampshire", "nh" }, + { "new jersey", "nj" }, + { "new mexico", "nm" }, + { "new york", "ny" }, + { "north carolina", "nc" }, + { "north dakota", "nd" }, + { "ohio", "oh" }, + { "oklahoma", "ok" }, + { "oregon", "or" }, + { "pennsylvania", "pa" }, + { "puerto rico", "pr" }, + { "rhode island", "ri" }, + { "south carolina", "sc" }, + { "south dakota", "sd" }, + { "tennessee", "tn" }, + { "texas", "tx" }, + { "utah", "ut" }, + { "vermont", "vt" }, + { "virginia", "va" }, + { "washington", "wa" }, + { "west virginia", "wv" }, + { "wisconsin", "wi" }, + { "wyoming", "wy" }, +}; + +} // namespace + +base::string16 GetAbbreviationForName(const base::string16& name) { + for (size_t i = 0; i < arraysize(kStateData); ++i) { + const StateData& state = kStateData[i]; + if (LowerCaseEqualsASCII(name, state.name)) + return ASCIIToUTF16(state.abbreviation); + } + return base::string16(); +} + +base::string16 GetNameForAbbreviation(const base::string16& abbreviation) { + for (size_t i = 0; i < arraysize(kStateData); ++i) { + const StateData& state = kStateData[i]; + if (LowerCaseEqualsASCII(abbreviation, state.abbreviation)) + return ASCIIToUTF16(state.name); + } + return base::string16(); +} + +} // namespace state_names +} // namespace autofill diff --git a/components/autofill/browser/state_names.h b/components/autofill/browser/state_names.h new file mode 100644 index 0000000..d609a31 --- /dev/null +++ b/components/autofill/browser/state_names.h @@ -0,0 +1,24 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/string16.h" + +#ifndef COMPONENTS_AUTOFILL_BROWSER_STATE_NAMES_H_ +#define COMPONENTS_AUTOFILL_BROWSER_STATE_NAMES_H_ + +namespace autofill { +namespace state_names { + +// Returns the abbrevation corresponding to the state named |name|, or the empty +// string if there is no such state. +base::string16 GetAbbreviationForName(const base::string16& name); + +// Returns the full state name corresponding to the |abbrevation|, or the empty +// string if there is no such state. +base::string16 GetNameForAbbreviation(const base::string16& abbreviation); + +} // namespace state_names +} // namespace autofill + +#endif // COMPONENTS_AUTOFILL_BROWSER_STATE_NAMES_H_ diff --git a/components/autofill/browser/validation.cc b/components/autofill/browser/validation.cc index c5779fb..c9a09f8 100644 --- a/components/autofill/browser/validation.cc +++ b/components/autofill/browser/validation.cc @@ -10,6 +10,7 @@ #include "base/utf_string_conversions.h" #include "components/autofill/browser/autofill_regexes.h" #include "components/autofill/browser/credit_card.h" +#include "components/autofill/browser/state_names.h" namespace autofill { @@ -110,9 +111,14 @@ bool IsValidEmailAddress(const base::string16& text) { return MatchesPattern(text, kEmailPattern); } -bool IsValidZip(const base::string16& value) { +bool IsValidState(const base::string16& text) { + return !state_names::GetAbbreviationForName(text).empty() || + !state_names::GetNameForAbbreviation(text).empty(); +} + +bool IsValidZip(const base::string16& text) { const base::string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$"); - return MatchesPattern(value, kZipPattern); + return MatchesPattern(text, kZipPattern); } } // namespace autofill diff --git a/components/autofill/browser/validation.h b/components/autofill/browser/validation.h index 618dcc1..eb7a6fc 100644 --- a/components/autofill/browser/validation.h +++ b/components/autofill/browser/validation.h @@ -34,6 +34,10 @@ bool IsValidCreditCardSecurityCode(const base::string16& code, // Returns true if |text| looks like a valid e-mail address. bool IsValidEmailAddress(const base::string16& text); +// Returns true if |text| is a valid US state name or abbreviation. It is +// case insensitive. Valid for US states only. +bool IsValidState(const base::string16& text); + // Returns true if |text| looks like a valid zip code. // Valid for US zip codes only. bool IsValidZip(const base::string16& text); |