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.
5 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_
6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.h"
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h"
22 namespace addressinput
{
23 class AddressNormalizer
;
34 // The object to be notified when loading of address validation rules is
36 class LoadRulesListener
{
38 virtual ~LoadRulesListener() {}
40 // Called when the validation rules for the |region_code| have been loaded.
41 // The validation rules include the generic rules for the |region_code| and
42 // specific rules for the country's administrative areas, localities, and
43 // dependent localities. If a country has language-specific validation rules,
44 // then these are also loaded.
46 // The |success| parameter is true when the rules were loaded successfully.
47 virtual void OnAddressValidationRulesLoaded(const std::string
& region_code
,
51 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The
52 // class is named AddressValidator to simplify switching between libaddressinput
55 // It's not possible to name this file address_validator.h because some
56 // compilers do not handle multiple files with the same name (although in
57 // different directories) gracefully. This class is a shim between upstream
58 // libaddressinput API and the API that Chrome expects, hence the file name
59 // chrome_address_validator.h.
60 class AddressValidator
{
62 // The status of address validation.
64 // Address validation completed successfully. Check |problems| to see if any
65 // problems were found.
68 // The validation rules are not available, because LoadRules() was not
69 // called or failed. Reload the rules.
72 // The validation rules are being loaded. Try again later.
76 // Takes ownership of |source| and |storage|.
77 AddressValidator(scoped_ptr
< ::i18n::addressinput::Source
> source
,
78 scoped_ptr
< ::i18n::addressinput::Storage
> storage
,
79 LoadRulesListener
* load_rules_listener
);
81 virtual ~AddressValidator();
83 // Loads the generic validation rules for |region_code| and specific rules
84 // for the region's administrative areas, localities, and dependent
85 // localities. A typical data size is 10KB. The largest is 250KB. If a region
86 // has language-specific validation rules, then these are also loaded.
89 // https://i18napis.appspot.com/ssl-aggregate-address/data/US
91 // If the rules are already in progress of being loaded, it does nothing.
92 // Invokes |load_rules_listener| when the loading has finished.
93 virtual void LoadRules(const std::string
& region_code
);
95 // Validates the |address| and populates |problems| with the validation
96 // problems, filtered according to the |filter| parameter.
98 // If the |filter| is empty, then all discovered validation problems are
99 // returned. If the |filter| contains problem elements, then only the problems
100 // in the |filter| may be returned.
101 virtual Status
ValidateAddress(
102 const ::i18n::addressinput::AddressData
& address
,
103 const ::i18n::addressinput::FieldProblemMap
* filter
,
104 ::i18n::addressinput::FieldProblemMap
* problems
) const;
106 // Fills in |suggestions| for the partially typed in |user_input|, assuming
107 // the user is typing in the |focused_field|. If the number of |suggestions|
108 // is over the |suggestion_limit|, then returns no |suggestions| at all.
110 // If the |solutions| parameter is NULL, the checks whether the validation
111 // rules are available, but does not fill in suggestions.
113 // Sample user input 1:
114 // country code = "US"
115 // postal code = "90066"
116 // focused field = POSTAL_CODE
117 // suggestions limit = 1
119 // [{administrative_area: "CA"}]
121 // Sample user input 2:
122 // country code = "CN"
123 // dependent locality = "Zongyang"
124 // focused field = DEPENDENT_LOCALITY
125 // suggestions limit = 10
127 // [{dependent_locality: "Zongyang Xian",
128 // locality: "Anqing Shi",
129 // administrative_area: "Anhui Sheng"}]
130 virtual Status
GetSuggestions(
131 const ::i18n::addressinput::AddressData
& user_input
,
132 ::i18n::addressinput::AddressField focused_field
,
133 size_t suggestion_limit
,
134 std::vector
< ::i18n::addressinput::AddressData
>* suggestions
) const;
136 // Canonicalizes the administrative area in |address_data|. For example,
137 // "texas" changes to "TX". Returns true on success, otherwise leaves
138 // |address_data| alone and returns false.
139 virtual bool CanonicalizeAdministrativeArea(
140 ::i18n::addressinput::AddressData
* address
) const;
143 // Constructor used only for MockAddressValidator.
146 // Returns the period of time to wait between the first attempt's failure and
147 // the second attempt's initiation to load rules. Exposed for testing.
148 virtual base::TimeDelta
GetBaseRetryPeriod() const;
151 // Verifies that |validator_| succeeded. Invoked by |validated_| callback.
152 void Validated(bool success
,
153 const ::i18n::addressinput::AddressData
&,
154 const ::i18n::addressinput::FieldProblemMap
&);
156 // Invokes the |load_rules_listener_|, if it's not NULL. Called by
157 // |rules_loaded_| callback.
158 void RulesLoaded(bool success
, const std::string
& region_code
, int);
160 // Retries loading rules without resetting the retry counter.
161 void RetryLoadRules(const std::string
& region_code
);
163 // Loads and stores aggregate rules at COUNTRY level.
164 const scoped_ptr
< ::i18n::addressinput::PreloadSupplier
> supplier_
;
166 // Suggests addresses based on user input.
167 const scoped_ptr
<InputSuggester
> input_suggester_
;
169 // Normalizes addresses into a canonical form.
170 const scoped_ptr
< ::i18n::addressinput::AddressNormalizer
> normalizer_
;
172 // Validates addresses.
173 const scoped_ptr
<const ::i18n::addressinput::AddressValidator
> validator_
;
175 // The callback that |validator_| invokes when it finished validating an
177 const scoped_ptr
<const ::i18n::addressinput::AddressValidator::Callback
>
180 // The callback that |supplier_| invokes when it finished loading rules.
181 const scoped_ptr
<const ::i18n::addressinput::PreloadSupplier::Callback
>
184 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be
186 LoadRulesListener
* const load_rules_listener_
;
188 // A mapping of region codes to the number of attempts to retry loading rules.
189 std::map
<std::string
, int> attempts_number_
;
191 // Member variables should appear before the WeakPtrFactory, to ensure that
192 // any WeakPtrs to AddressValidator are invalidated before its members
193 // variable's destructors are executed, rendering them invalid.
194 base::WeakPtrFactory
<AddressValidator
> weak_factory_
;
196 DISALLOW_COPY_AND_ASSIGN(AddressValidator
);
199 } // namespace autofill
201 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_