Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / data_model_wrapper.cc
index a61a32b..781c614 100644 (file)
@@ -4,11 +4,13 @@
 
 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
 
+#include "base/bind.h"
 #include "base/callback.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/ui/autofill/autofill_dialog_common.h"
+#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
 #include "components/autofill/content/browser/wallet/full_wallet.h"
 #include "components/autofill/content/browser/wallet/wallet_address.h"
 #include "components/autofill/core/browser/autofill_type.h"
 #include "components/autofill/core/browser/credit_card.h"
 #include "components/autofill/core/browser/form_structure.h"
-#include "ui/base/resource/resource_bundle.h"
-#include "ui/gfx/image/image.h"
-
-#if !defined(OS_ANDROID)
 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
 #include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_ui.h"
-#endif
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/image/image.h"
 
 namespace autofill {
 
@@ -52,33 +51,19 @@ gfx::Image DataModelWrapper::GetIcon() {
   return gfx::Image();
 }
 
-#if !defined(OS_ANDROID)
 bool DataModelWrapper::GetDisplayText(
     base::string16* vertically_compact,
     base::string16* horizontally_compact) {
+  base::string16 phone =
+      GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER));
+  if (phone.empty())
+    return false;
+
   // Format the address.
   ::i18n::addressinput::AddressData address_data;
-  address_data.recipient = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(NAME_FULL)));
-  address_data.country_code = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_COUNTRY)));
-  address_data.administrative_area = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_STATE)));
-  address_data.locality = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_CITY)));
-  address_data.dependent_locality = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_DEPENDENT_LOCALITY)));
-  address_data.sorting_code = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_SORTING_CODE)));
-  address_data.postal_code = UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_ZIP)));
-
-  address_data.address_lines.push_back(UTF16ToUTF8(
-      GetInfoForDisplay(AutofillType(ADDRESS_HOME_LINE1))));
-  base::string16 address2 = GetInfoForDisplay(AutofillType(ADDRESS_HOME_LINE2));
-  if (!address2.empty())
-    address_data.address_lines.push_back(UTF16ToUTF8(address2));
-
+  i18ninput::CreateAddressData(
+      base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
+      &address_data);
   std::vector<std::string> lines;
   address_data.FormatForDisplay(&lines);
 
@@ -88,8 +73,7 @@ bool DataModelWrapper::GetDisplayText(
   if (!email.empty())
     non_address_info += ASCIIToUTF16("\n") + email;
 
-  non_address_info += ASCIIToUTF16("\n") +
-      GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER));
+  non_address_info += ASCIIToUTF16("\n") + phone;
 
   // The separator is locale-specific.
   std::string compact_separator =
@@ -103,39 +87,20 @@ bool DataModelWrapper::GetDisplayText(
 
   return true;
 }
-#endif
 
 bool DataModelWrapper::FillFormStructure(
-    const DetailInputs& inputs,
-    const InputFieldComparator& compare,
+    const std::vector<ServerFieldType>& types,
+    const FormStructure::InputFieldComparator& compare,
     FormStructure* form_structure) const {
-  bool filled_something = false;
-  for (size_t i = 0; i < form_structure->field_count(); ++i) {
-    AutofillField* field = form_structure->field(i);
-    for (size_t j = 0; j < inputs.size(); ++j) {
-      if (compare.Run(inputs[j].type, *field)) {
-        AutofillField::FillFormField(*field, GetInfo(field->Type()),
-                                     g_browser_process->GetApplicationLocale(),
-                                     field);
-        filled_something = true;
-        break;
-      }
-    }
-  }
-  return filled_something;
+  return form_structure->FillFields(
+      types,
+      compare,
+      base::Bind(&DataModelWrapper::GetInfo, base::Unretained(this)),
+      g_browser_process->GetApplicationLocale());
 }
 
 DataModelWrapper::DataModelWrapper() {}
 
-// EmptyDataModelWrapper
-
-EmptyDataModelWrapper::EmptyDataModelWrapper() {}
-EmptyDataModelWrapper::~EmptyDataModelWrapper() {}
-
-base::string16 EmptyDataModelWrapper::GetInfo(const AutofillType& type) const {
-  return base::string16();
-}
-
 // AutofillProfileWrapper
 
 AutofillProfileWrapper::AutofillProfileWrapper(const AutofillProfile* profile)
@@ -237,7 +202,6 @@ gfx::Image AutofillCreditCardWrapper::GetIcon() {
   return rb.GetImageNamed(CreditCard::IconResourceId(card_->type()));
 }
 
-#if !defined(OS_ANDROID)
 bool AutofillCreditCardWrapper::GetDisplayText(
     base::string16* vertically_compact,
     base::string16* horizontally_compact) {
@@ -247,7 +211,6 @@ bool AutofillCreditCardWrapper::GetDisplayText(
   *vertically_compact = *horizontally_compact = card_->TypeAndLastFourDigits();
   return true;
 }
-#endif
 
 // WalletAddressWrapper
 
@@ -272,19 +235,15 @@ base::string16 WalletAddressWrapper::GetInfoForDisplay(const AutofillType& type)
   return DataModelWrapper::GetInfoForDisplay(type);
 }
 
-#if !defined(OS_ANDROID)
 bool WalletAddressWrapper::GetDisplayText(
     base::string16* vertically_compact,
     base::string16* horizontally_compact) {
-  if (!address_->is_complete_address() ||
-      GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) {
+  if (!address_->is_complete_address())
     return false;
-  }
 
   return DataModelWrapper::GetDisplayText(vertically_compact,
                                           horizontally_compact);
 }
-#endif
 
 // WalletInstrumentWrapper
 
@@ -318,18 +277,20 @@ gfx::Image WalletInstrumentWrapper::GetIcon() {
   return instrument_->CardIcon();
 }
 
-#if !defined(OS_ANDROID)
 bool WalletInstrumentWrapper::GetDisplayText(
     base::string16* vertically_compact,
     base::string16* horizontally_compact) {
   // TODO(dbeam): handle other instrument statuses? http://crbug.com/233048
   if (instrument_->status() == wallet::WalletItems::MaskedInstrument::EXPIRED ||
-      !instrument_->address().is_complete_address() ||
-      GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)).empty()) {
+      !instrument_->address().is_complete_address()) {
+    return false;
+  }
+
+  if (!DataModelWrapper::GetDisplayText(vertically_compact,
+                                        horizontally_compact)) {
     return false;
   }
 
-  DataModelWrapper::GetDisplayText(vertically_compact, horizontally_compact);
   // TODO(estade): descriptive_name() is user-provided. Should we use it or
   // just type + last 4 digits?
   base::string16 line1 = instrument_->descriptive_name() + ASCIIToUTF16("\n");
@@ -337,7 +298,6 @@ bool WalletInstrumentWrapper::GetDisplayText(
   *horizontally_compact = line1 + *horizontally_compact;
   return true;
 }
-#endif
 
 // FullWalletBillingWrapper
 
@@ -351,17 +311,12 @@ FullWalletBillingWrapper::~FullWalletBillingWrapper() {}
 
 base::string16 FullWalletBillingWrapper::GetInfo(const AutofillType& type)
     const {
-  if (type.GetStorableType() == CREDIT_CARD_EXP_MONTH)
-    return MonthComboboxModel::FormatMonth(full_wallet_->expiration_month());
-
-  if (type.group() == CREDIT_CARD)
-    return full_wallet_->GetInfo(type);
-
-  return full_wallet_->billing_address()->GetInfo(
-      type, g_browser_process->GetApplicationLocale());
+  return full_wallet_->GetInfo(
+      g_browser_process->GetApplicationLocale(),
+      AutofillType(AutofillType::GetEquivalentBillingFieldType(
+          type.GetStorableType())));
 }
 
-#if !defined(OS_ANDROID)
 bool FullWalletBillingWrapper::GetDisplayText(
     base::string16* vertically_compact,
     base::string16* horizontally_compact) {
@@ -372,7 +327,6 @@ bool FullWalletBillingWrapper::GetDisplayText(
   return DataModelWrapper::GetDisplayText(vertically_compact,
                                           horizontally_compact);
 }
-#endif
 
 // FullWalletShippingWrapper
 
@@ -390,14 +344,4 @@ base::string16 FullWalletShippingWrapper::GetInfo(
       type, g_browser_process->GetApplicationLocale());
 }
 
-FieldMapWrapper::FieldMapWrapper(const FieldValueMap& field_map)
-    : field_map_(field_map) {}
-
-FieldMapWrapper::~FieldMapWrapper() {}
-
-base::string16 FieldMapWrapper::GetInfo(const AutofillType& type) const {
-  FieldValueMap::const_iterator it = field_map_.find(type.server_type());
-  return it != field_map_.end() ? it->second : base::string16();
-}
-
 }  // namespace autofill