Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / autofill_field.cc
index c5b696e..8822bd8 100644 (file)
@@ -280,21 +280,23 @@ bool FillCreditCardTypeSelectControl(const base::string16& value,
 void FillPhoneNumberField(const AutofillField& field,
                           const base::string16& number,
                           FormFieldData* field_data) {
-  // Check to see if the size field matches the "prefix" or "suffix" sizes and
-  // fill accordingly.
+  field_data->value =
+      AutofillField::GetPhoneNumberValue(field, number, *field_data);
+}
+
+// Set |field_data|'s value to |number|, or possibly an appropriate substring
+// of |number| for cases where credit card number splits across multiple HTML
+// form input fields.
+// The |field| specifies the |credit_card_number_offset_| to the substring
+// within credit card number.
+void FillCreditCardNumberField(const AutofillField& field,
+                               const base::string16& number,
+                               FormFieldData* field_data) {
   base::string16 value = number;
-  if (number.length() ==
-          PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) {
-    if (field.phone_part() == AutofillField::PHONE_PREFIX ||
-        field_data->max_length == PhoneNumber::kPrefixLength) {
-      value = number.substr(PhoneNumber::kPrefixOffset,
-                            PhoneNumber::kPrefixLength);
-    } else if (field.phone_part() == AutofillField::PHONE_SUFFIX ||
-               field_data->max_length == PhoneNumber::kSuffixLength) {
-      value = number.substr(PhoneNumber::kSuffixOffset,
-                            PhoneNumber::kSuffixLength);
-    }
-  }
+
+  // |field|'s max_length truncates credit card number to fit within.
+  if (field.credit_card_number_offset() < value.length())
+    value = value.substr(field.credit_card_number_offset());
 
   field_data->value = value;
 }
@@ -397,7 +399,8 @@ AutofillField::AutofillField()
       heuristic_type_(UNKNOWN_TYPE),
       html_type_(HTML_TYPE_UNKNOWN),
       html_mode_(HTML_MODE_NONE),
-      phone_part_(IGNORED) {
+      phone_part_(IGNORED),
+      credit_card_number_offset_(0) {
 }
 
 AutofillField::AutofillField(const FormFieldData& field,
@@ -408,7 +411,8 @@ AutofillField::AutofillField(const FormFieldData& field,
       heuristic_type_(UNKNOWN_TYPE),
       html_type_(HTML_TYPE_UNKNOWN),
       html_mode_(HTML_MODE_NONE),
-      phone_part_(IGNORED) {
+      phone_part_(IGNORED),
+      credit_card_number_offset_(0) {
 }
 
 AutofillField::~AutofillField() {}
@@ -487,10 +491,39 @@ bool AutofillField::FillFormField(const AutofillField& field,
   } else if (type.GetStorableType() == ADDRESS_HOME_STREET_ADDRESS) {
     FillStreetAddress(value, address_language_code, field_data);
     return true;
+  } else if (type.GetStorableType() == CREDIT_CARD_NUMBER) {
+    FillCreditCardNumberField(field, value, field_data);
+    return true;
   }
 
   field_data->value = value;
   return true;
 }
 
+base::string16 AutofillField::GetPhoneNumberValue(
+    const AutofillField& field,
+    const base::string16& number,
+    const FormFieldData& field_data) {
+  // Check to see if the size field matches the "prefix" or "suffix" size.
+  // If so, return the appropriate substring.
+  if (number.length() !=
+          PhoneNumber::kPrefixLength + PhoneNumber::kSuffixLength) {
+    return number;
+  }
+
+  if (field.phone_part() == AutofillField::PHONE_PREFIX ||
+      field_data.max_length == PhoneNumber::kPrefixLength) {
+    return
+        number.substr(PhoneNumber::kPrefixOffset, PhoneNumber::kPrefixLength);
+  }
+
+  if (field.phone_part() == AutofillField::PHONE_SUFFIX ||
+      field_data.max_length == PhoneNumber::kSuffixLength) {
+    return
+        number.substr(PhoneNumber::kSuffixOffset, PhoneNumber::kSuffixLength);
+  }
+
+  return number;
+}
+
 }  // namespace autofill