Commit 5efd462d authored by isherman@chromium.org's avatar isherman@chromium.org

[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
parent adcb4569
...@@ -206,6 +206,8 @@ ...@@ -206,6 +206,8 @@
'autofill/browser/phone_number_i18n.h', 'autofill/browser/phone_number_i18n.h',
'autofill/browser/risk/fingerprint.cc', 'autofill/browser/risk/fingerprint.cc',
'autofill/browser/risk/fingerprint.h', 'autofill/browser/risk/fingerprint.h',
'autofill/browser/state_names.cc',
'autofill/browser/state_names.h',
'autofill/browser/validation.cc', 'autofill/browser/validation.cc',
'autofill/browser/validation.h', 'autofill/browser/validation.h',
'autofill/browser/wallet/cart.cc', 'autofill/browser/wallet/cart.cc',
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "components/autofill/browser/autofill_country.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 "components/autofill/common/form_field_data.h"
#include "grit/generated_resources.h" #include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -19,91 +21,6 @@ ...@@ -19,91 +21,6 @@
namespace autofill { namespace autofill {
namespace { 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[] = { const char* const kMonthsAbbreviated[] = {
NULL, // Padding so index 1 = month 1 = January. NULL, // Padding so index 1 = month 1 = January.
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
...@@ -141,23 +58,18 @@ bool SetSelectControlValue(const base::string16& value, ...@@ -141,23 +58,18 @@ bool SetSelectControlValue(const base::string16& value,
bool FillStateSelectControl(const base::string16& value, bool FillStateSelectControl(const base::string16& value,
FormFieldData* field) { FormFieldData* field) {
base::string16 abbrev, full; base::string16 abbreviation = value;
if (value.size() < 4U) { base::string16 full = state_names::GetNameForAbbreviation(value);
abbrev = value; if (full.empty()) {
full = State::FullName(value); abbreviation = state_names::GetAbbreviationForName(value);
} else {
abbrev = State::Abbreviation(value);
full = value; full = value;
} }
// Try the abbreviation name first. // Try the abbreviation first.
if (!abbrev.empty() && SetSelectControlValue(abbrev, field)) if (!abbreviation.empty() && SetSelectControlValue(abbreviation, field))
return true; return true;
if (full.empty()) return !full.empty() && SetSelectControlValue(full, field);
return false;
return SetSelectControlValue(full, field);
} }
bool FillExpirationMonthSelectControl(const base::string16& value, bool FillExpirationMonthSelectControl(const base::string16& value,
...@@ -319,9 +231,4 @@ bool FormGroup::FillCountrySelectControl(const std::string& app_locale, ...@@ -319,9 +231,4 @@ bool FormGroup::FillCountrySelectControl(const std::string& app_locale,
return false; return false;
} }
// static
bool FormGroup::IsValidState(const base::string16& value) {
return !State::Abbreviation(value).empty() || !State::FullName(value).empty();
}
} // namespace autofill } // namespace autofill
...@@ -75,11 +75,6 @@ class FormGroup { ...@@ -75,11 +75,6 @@ class FormGroup {
const std::string& app_locale, const std::string& app_locale,
FormFieldData* field_data) const; 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: protected:
// AutofillProfile needs to call into GetSupportedTypes() for objects of // AutofillProfile needs to call into GetSupportedTypes() for objects of
// non-AutofillProfile type, for which mere inheritance is insufficient. // non-AutofillProfile type, for which mere inheritance is insufficient.
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "components/autofill/browser/autofill_country.h" #include "components/autofill/browser/autofill_country.h"
#include "components/autofill/browser/autofill_field.h" #include "components/autofill/browser/autofill_field.h"
#include "components/autofill/browser/autofill_metrics.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/form_structure.h"
#include "components/autofill/browser/personal_data_manager_observer.h" #include "components/autofill/browser/personal_data_manager_observer.h"
#include "components/autofill/browser/phone_number.h" #include "components/autofill/browser/phone_number.h"
...@@ -689,7 +688,7 @@ bool PersonalDataManager::IsValidLearnableProfile( ...@@ -689,7 +688,7 @@ bool PersonalDataManager::IsValidLearnableProfile(
// Reject profiles with invalid US state information. // Reject profiles with invalid US state information.
base::string16 state = profile.GetRawInfo(ADDRESS_HOME_STATE); base::string16 state = profile.GetRawInfo(ADDRESS_HOME_STATE);
if (profile.GetRawInfo(ADDRESS_HOME_COUNTRY) == ASCIIToUTF16("US") && if (profile.GetRawInfo(ADDRESS_HOME_COUNTRY) == ASCIIToUTF16("US") &&
!state.empty() && !FormGroup::IsValidState(state)) { !state.empty() && !IsValidState(state)) {
return false; return false;
} }
......
// 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
// 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_
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/utf_string_conversions.h" #include "base/utf_string_conversions.h"
#include "components/autofill/browser/autofill_regexes.h" #include "components/autofill/browser/autofill_regexes.h"
#include "components/autofill/browser/credit_card.h" #include "components/autofill/browser/credit_card.h"
#include "components/autofill/browser/state_names.h"
namespace autofill { namespace autofill {
...@@ -110,9 +111,14 @@ bool IsValidEmailAddress(const base::string16& text) { ...@@ -110,9 +111,14 @@ bool IsValidEmailAddress(const base::string16& text) {
return MatchesPattern(text, kEmailPattern); 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})?$"); const base::string16 kZipPattern = ASCIIToUTF16("^\\d{5}(-\\d{4})?$");
return MatchesPattern(value, kZipPattern); return MatchesPattern(text, kZipPattern);
} }
} // namespace autofill } // namespace autofill
...@@ -34,6 +34,10 @@ bool IsValidCreditCardSecurityCode(const base::string16& code, ...@@ -34,6 +34,10 @@ bool IsValidCreditCardSecurityCode(const base::string16& code,
// Returns true if |text| looks like a valid e-mail address. // Returns true if |text| looks like a valid e-mail address.
bool IsValidEmailAddress(const base::string16& text); 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. // Returns true if |text| looks like a valid zip code.
// Valid for US zip codes only. // Valid for US zip codes only.
bool IsValidZip(const base::string16& text); bool IsValidZip(const base::string16& text);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment