[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / chrome_address_validator.h
blobba4b413ab05bedb7bf66770cf2ff0361cf9ffaa1
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
21 namespace i18n {
22 namespace addressinput {
23 class AddressNormalizer;
24 class Downloader;
25 class Storage;
26 struct AddressData;
30 namespace autofill {
32 class InputSuggester;
34 // The object to be notified when loading of address validation rules is
35 // finished.
36 class LoadRulesListener {
37 public:
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,
48 bool success) = 0;
51 // Interface to the libaddressinput AddressValidator for Chromium Autofill. The
52 // class is named AddressValidator to simplify switching between libaddressinput
53 // and this version.
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 {
61 public:
62 // The status of address validation.
63 enum Status {
64 // Address validation completed successfully. Check |problems| to see if any
65 // problems were found.
66 SUCCESS,
68 // The validation rules are not available, because LoadRules() was not
69 // called or failed. Reload the rules.
70 RULES_UNAVAILABLE,
72 // The validation rules are being loaded. Try again later.
73 RULES_NOT_READY
76 // Takes ownership of |downloader| and |storage|.
77 AddressValidator(const std::string& validation_data_url,
78 scoped_ptr< ::i18n::addressinput::Downloader> downloader,
79 scoped_ptr< ::i18n::addressinput::Storage> storage,
80 LoadRulesListener* load_rules_listener);
82 virtual ~AddressValidator();
84 // Loads the generic validation rules for |region_code| and specific rules
85 // for the region's administrative areas, localities, and dependent
86 // localities. A typical data size is 10KB. The largest is 250KB. If a region
87 // has language-specific validation rules, then these are also loaded.
89 // Example rule:
90 // https://i18napis.appspot.com/ssl-aggregate-address/data/US
92 // If the rules are already in progress of being loaded, it does nothing.
93 // Invokes |load_rules_listener| when the loading has finished.
94 virtual void LoadRules(const std::string& region_code);
96 // Validates the |address| and populates |problems| with the validation
97 // problems, filtered according to the |filter| parameter.
99 // If the |filter| is empty, then all discovered validation problems are
100 // returned. If the |filter| contains problem elements, then only the problems
101 // in the |filter| may be returned.
102 virtual Status ValidateAddress(
103 const ::i18n::addressinput::AddressData& address,
104 const ::i18n::addressinput::FieldProblemMap* filter,
105 ::i18n::addressinput::FieldProblemMap* problems) const;
107 // Fills in |suggestions| for the partially typed in |user_input|, assuming
108 // the user is typing in the |focused_field|. If the number of |suggestions|
109 // is over the |suggestion_limit|, then returns no |suggestions| at all.
111 // If the |solutions| parameter is NULL, the checks whether the validation
112 // rules are available, but does not fill in suggestions.
114 // Sample user input 1:
115 // country code = "US"
116 // postal code = "90066"
117 // focused field = POSTAL_CODE
118 // suggestions limit = 1
119 // Suggestion:
120 // [{administrative_area: "CA"}]
122 // Sample user input 2:
123 // country code = "CN"
124 // dependent locality = "Zongyang"
125 // focused field = DEPENDENT_LOCALITY
126 // suggestions limit = 10
127 // Suggestion:
128 // [{dependent_locality: "Zongyang Xian",
129 // locality: "Anqing Shi",
130 // administrative_area: "Anhui Sheng"}]
131 virtual Status GetSuggestions(
132 const ::i18n::addressinput::AddressData& user_input,
133 ::i18n::addressinput::AddressField focused_field,
134 size_t suggestion_limit,
135 std::vector< ::i18n::addressinput::AddressData>* suggestions) const;
137 // Canonicalizes the administrative area in |address_data|. For example,
138 // "texas" changes to "TX". Returns true on success, otherwise leaves
139 // |address_data| alone and returns false.
140 virtual bool CanonicalizeAdministrativeArea(
141 ::i18n::addressinput::AddressData* address) const;
143 protected:
144 // Constructor used only for MockAddressValidator.
145 AddressValidator();
147 // Returns the period of time to wait between the first attempt's failure and
148 // the second attempt's initiation to load rules. Exposed for testing.
149 virtual base::TimeDelta GetBaseRetryPeriod() const;
151 private:
152 // Verifies that |validator_| succeeded. Invoked by |validated_| callback.
153 void Validated(bool success,
154 const ::i18n::addressinput::AddressData&,
155 const ::i18n::addressinput::FieldProblemMap&);
157 // Invokes the |load_rules_listener_|, if it's not NULL. Called by
158 // |rules_loaded_| callback.
159 void RulesLoaded(bool success, const std::string& region_code, int);
161 // Retries loading rules without resetting the retry counter.
162 void RetryLoadRules(const std::string& region_code);
164 // Loads and stores aggregate rules at COUNTRY level.
165 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_;
167 // Suggests addresses based on user input.
168 const scoped_ptr<InputSuggester> input_suggester_;
170 // Normalizes addresses into a canonical form.
171 const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_;
173 // Validates addresses.
174 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_;
176 // The callback that |validator_| invokes when it finished validating an
177 // address.
178 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback>
179 validated_;
181 // The callback that |supplier_| invokes when it finished loading rules.
182 const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback>
183 rules_loaded_;
185 // Not owned delegate to invoke when |suppler_| finished loading rules. Can be
186 // NULL.
187 LoadRulesListener* const load_rules_listener_;
189 // A mapping of region codes to the number of attempts to retry loading rules.
190 std::map<std::string, int> attempts_number_;
192 // Member variables should appear before the WeakPtrFactory, to ensure that
193 // any WeakPtrs to AddressValidator are invalidated before its members
194 // variable's destructors are executed, rendering them invalid.
195 base::WeakPtrFactory<AddressValidator> weak_factory_;
197 DISALLOW_COPY_AND_ASSIGN(AddressValidator);
200 } // namespace autofill
202 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_