Home | History | Annotate | Download | only in wallet
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "components/autofill/content/browser/wallet/form_field_error.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "base/values.h"
     11 
     12 namespace autofill {
     13 namespace wallet {
     14 
     15 namespace {
     16 
     17 FormFieldError::ErrorType ErrorTypeFromString(const std::string& error_type) {
     18   if (LowerCaseEqualsASCII(error_type, "unknown_error"))
     19     return FormFieldError::UNKNOWN_ERROR;
     20   if (LowerCaseEqualsASCII(error_type, "invalid_phone_number"))
     21     return FormFieldError::INVALID_PHONE_NUMBER;
     22   if (LowerCaseEqualsASCII(error_type, "invalid_postal_code"))
     23     return FormFieldError::INVALID_POSTAL_CODE;
     24   if (LowerCaseEqualsASCII(error_type, "invalid_address"))
     25     return FormFieldError::INVALID_ADDRESS;
     26   if (LowerCaseEqualsASCII(error_type, "invalid_card_details"))
     27     return FormFieldError::INVALID_CARD_DETAILS;
     28   if (LowerCaseEqualsASCII(error_type, "invalid_city"))
     29     return FormFieldError::INVALID_CITY;
     30   if (LowerCaseEqualsASCII(error_type, "invalid_instrument"))
     31     return FormFieldError::INVALID_INSTRUMENT;
     32   if (LowerCaseEqualsASCII(error_type, "invalid_state"))
     33     return FormFieldError::INVALID_STATE;
     34   if (LowerCaseEqualsASCII(error_type, "required_field_not_set"))
     35     return FormFieldError::REQUIRED_FIELD_NOT_SET;
     36   return FormFieldError::UNKNOWN_ERROR;
     37 }
     38 
     39 FormFieldError::Location LocationFromString(const std::string& location) {
     40   if (LowerCaseEqualsASCII(location, "unknown_location"))
     41     return FormFieldError::UNKNOWN_LOCATION;
     42   if (LowerCaseEqualsASCII(location, "payment_instrument"))
     43     return FormFieldError::PAYMENT_INSTRUMENT;
     44   if (LowerCaseEqualsASCII(location, "shipping_address"))
     45     return FormFieldError::SHIPPING_ADDRESS;
     46   if (LowerCaseEqualsASCII(location, "legal_address"))
     47     return FormFieldError::LEGAL_ADDRESS;
     48   return FormFieldError::UNKNOWN_LOCATION;
     49 }
     50 
     51 }  // namespace
     52 
     53 FormFieldError::FormFieldError(ErrorType error_type, Location location)
     54   : error_type_(error_type),
     55     location_(location) {}
     56 
     57 FormFieldError::~FormFieldError() {}
     58 
     59 ServerFieldType FormFieldError::GetAutofillType() const {
     60   switch (error_type_) {
     61     case INVALID_PHONE_NUMBER:
     62       if (location_ == LEGAL_ADDRESS || location_ == PAYMENT_INSTRUMENT)
     63         return PHONE_BILLING_WHOLE_NUMBER;
     64       if (location_ == SHIPPING_ADDRESS)
     65         return PHONE_HOME_WHOLE_NUMBER;
     66       break;
     67 
     68     case INVALID_POSTAL_CODE:
     69     case INVALID_CITY:
     70     case INVALID_STATE:
     71       if (location_ == LEGAL_ADDRESS || location_ == PAYMENT_INSTRUMENT)
     72         return ADDRESS_BILLING_ZIP;
     73       if (location_ == SHIPPING_ADDRESS)
     74         return ADDRESS_HOME_ZIP;
     75       break;
     76 
     77     case INVALID_ADDRESS:
     78       if (location_ == LEGAL_ADDRESS || location_ == PAYMENT_INSTRUMENT)
     79         return ADDRESS_BILLING_LINE1;
     80       if (location_ == SHIPPING_ADDRESS)
     81         return ADDRESS_HOME_LINE1;
     82       break;
     83 
     84     case INVALID_CARD_DETAILS:
     85       return CREDIT_CARD_VERIFICATION_CODE;
     86 
     87     case INVALID_INSTRUMENT:
     88       return CREDIT_CARD_NUMBER;
     89 
     90     case REQUIRED_FIELD_NOT_SET:
     91     case UNKNOWN_ERROR:
     92       return MAX_VALID_FIELD_TYPE;
     93   }
     94 
     95   return MAX_VALID_FIELD_TYPE;
     96 }
     97 
     98 // TODO(ahutter): L10n after UX provides strings.
     99 base::string16 FormFieldError::GetErrorMessage() const {
    100   switch (error_type_) {
    101     case INVALID_PHONE_NUMBER:
    102       return base::ASCIIToUTF16("Not a valid phone number");
    103 
    104     case INVALID_POSTAL_CODE:
    105       return base::ASCIIToUTF16("Not a valid zip code");
    106 
    107     case INVALID_CITY:
    108       return base::ASCIIToUTF16("Zip code is not valid for the entered city");
    109 
    110     case INVALID_STATE:
    111       return base::ASCIIToUTF16("Zip code is not valid for the entered state");
    112 
    113     case INVALID_ADDRESS:
    114       return base::ASCIIToUTF16("Not a valid street address");
    115 
    116     case INVALID_CARD_DETAILS:
    117       return base::ASCIIToUTF16("Not a valid CVN");
    118 
    119     case INVALID_INSTRUMENT:
    120       return base::ASCIIToUTF16("Not a valid CC#");
    121 
    122     case REQUIRED_FIELD_NOT_SET:
    123       return base::ASCIIToUTF16("Required field is missing");
    124 
    125     case UNKNOWN_ERROR:
    126       return base::ASCIIToUTF16("An unknown error occurred");
    127   }
    128 
    129   NOTREACHED();
    130   return base::string16();
    131 }
    132 
    133 // static
    134 FormFieldError FormFieldError::CreateFormFieldError(
    135     const base::DictionaryValue& dictionary) {
    136   FormFieldError form_field_error(UNKNOWN_ERROR, UNKNOWN_LOCATION);
    137 
    138   std::string error_type;
    139   if (dictionary.GetString("type", &error_type))
    140     form_field_error.error_type_ = ErrorTypeFromString(error_type);
    141 
    142   std::string location;
    143   if (dictionary.GetString("location", &location))
    144     form_field_error.location_ = LocationFromString(location);
    145 
    146   return form_field_error;
    147 }
    148 
    149 bool FormFieldError::operator==(const FormFieldError& other) const {
    150   return error_type_ == other.error_type_ && location_ == other.location_;
    151 }
    152 
    153 }  // namespace wallet
    154 }  // namespace autofill
    155