Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / autofill_dialog_i18n_input.cc
1 // Copyright 2014 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 "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
6
7 #include "base/strings/string_split.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "components/autofill/core/browser/address_i18n.h"
11 #include "components/autofill/core/browser/autofill_profile.h"
12 #include "components/autofill/core/browser/credit_card.h"
13 #include "components/autofill/core/browser/field_types.h"
14 #include "grit/components_strings.h"
15 #include "third_party/libaddressinput/chromium/addressinput_util.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
21 #include "ui/base/l10n/l10n_util.h"
22
23 namespace autofill {
24 namespace i18ninput {
25
26 namespace {
27
28 using base::UTF16ToUTF8;
29 using ::i18n::addressinput::AddressField;
30 using ::i18n::addressinput::AddressUiComponent;
31 using ::i18n::addressinput::Localization;
32
33 std::vector<AddressUiComponent> BuildComponents(const std::string& country_code,
34                                                 std::string* language_code) {
35   Localization localization;
36   localization.SetGetter(l10n_util::GetStringUTF8);
37   std::string not_used;
38   return ::i18n::addressinput::BuildComponents(
39       country_code, localization, g_browser_process->GetApplicationLocale(),
40       language_code == NULL ? &not_used : language_code);
41 }
42
43 DetailInput::Length LengthFromHint(AddressUiComponent::LengthHint hint) {
44   if (hint == AddressUiComponent::HINT_SHORT)
45     return DetailInput::SHORT;
46   DCHECK_EQ(hint, AddressUiComponent::HINT_LONG);
47   return DetailInput::LONG;
48 }
49
50 }  // namespace
51
52 void BuildAddressInputs(common::AddressType address_type,
53                         const std::string& country_code,
54                         DetailInputs* inputs,
55                         std::string* language_code) {
56   const std::vector<AddressUiComponent>& components(
57       BuildComponents(country_code, language_code));
58
59   const bool billing = address_type == common::ADDRESS_TYPE_BILLING;
60
61   for (size_t i = 0; i < components.size(); ++i) {
62     const AddressUiComponent& component = components[i];
63     // Interactive autofill dialog does not display organization.
64     if (component.field == ::i18n::addressinput::ORGANIZATION)
65       continue;
66     ServerFieldType server_type = i18n::TypeForField(component.field, billing);
67     DetailInput::Length length = LengthFromHint(component.length_hint);
68     base::string16 placeholder = base::UTF8ToUTF16(component.name);
69     DetailInput input = { length, server_type, placeholder };
70     inputs->push_back(input);
71   }
72
73   ServerFieldType server_type =
74       billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
75   base::string16 placeholder_text =
76       l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY);
77   DetailInput input = { DetailInput::LONG, server_type, placeholder_text };
78   inputs->push_back(input);
79 }
80
81 bool CardHasCompleteAndVerifiedData(const CreditCard& card) {
82   if (!card.IsVerified())
83     return false;
84
85   const ServerFieldType required_fields[] = {
86       CREDIT_CARD_NUMBER,
87       CREDIT_CARD_EXP_MONTH,
88       CREDIT_CARD_EXP_4_DIGIT_YEAR,
89   };
90
91   for (size_t i = 0; i < arraysize(required_fields); ++i) {
92     if (card.GetRawInfo(required_fields[i]).empty())
93       return false;
94   }
95
96   return true;
97 }
98
99 bool AddressHasCompleteAndVerifiedData(const AutofillProfile& profile,
100                                        const std::string& app_locale) {
101   if (!profile.IsVerified())
102     return false;
103
104   if (!addressinput::HasAllRequiredFields(
105           *i18n::CreateAddressDataFromAutofillProfile(profile, app_locale))) {
106     return false;
107   }
108
109   const ServerFieldType more_required_fields[] = {
110       NAME_FULL,
111       PHONE_HOME_WHOLE_NUMBER
112   };
113
114   for (size_t i = 0; i < arraysize(more_required_fields); ++i) {
115     if (profile.GetInfo(AutofillType(more_required_fields[i]),
116                         app_locale).empty()) {
117       return false;
118     }
119   }
120
121   return true;
122 }
123
124 }  // namespace i18ninput
125 }  // namespace autofill