OLD | NEW |
(Empty) | |
| 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 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
| 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
| 7 |
| 8 #include <cstddef> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" |
| 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" |
| 17 |
| 18 namespace i18n { |
| 19 namespace addressinput { |
| 20 |
| 21 class Downloader; |
| 22 class PreloadSupplier; |
| 23 class Storage; |
| 24 struct AddressData; |
| 25 |
| 26 } // namespace addressinput |
| 27 } // namespace i18n |
| 28 |
| 29 namespace autofill { |
| 30 |
| 31 // Interface to the libaddressinput AddressValidator for Chromium Autofill. |
| 32 class PreloadAddressValidator { |
| 33 public: |
| 34 typedef ::i18n::addressinput::Callback<std::string, int> Callback; |
| 35 |
| 36 // The status of address validation. |
| 37 enum Status { |
| 38 // Address validation completed successfully. Check |problems| to see if any |
| 39 // problems were found. |
| 40 SUCCESS, |
| 41 |
| 42 // The validation rules are not available, because LoadRules() was not |
| 43 // called or failed. Reload the rules. |
| 44 RULES_UNAVAILABLE, |
| 45 |
| 46 // The validation rules are being loaded. Try again later. |
| 47 RULES_NOT_READY |
| 48 }; |
| 49 |
| 50 // Constructors take ownership of |downloader| and |storage|. |
| 51 PreloadAddressValidator( |
| 52 scoped_ptr< ::i18n::addressinput::Downloader> downloader, |
| 53 scoped_ptr< ::i18n::addressinput::Storage> storage); |
| 54 |
| 55 PreloadAddressValidator( |
| 56 const std::string& validation_data_url, |
| 57 scoped_ptr< ::i18n::addressinput::Downloader> downloader, |
| 58 scoped_ptr< ::i18n::addressinput::Storage> storage); |
| 59 |
| 60 virtual ~PreloadAddressValidator(); |
| 61 |
| 62 // Loads the generic validation rules for |region_code| and specific rules |
| 63 // for the regions's administrative areas, localities, and dependent |
| 64 // localities. A typical data size is 10KB. The largest is 250KB. If a region |
| 65 // has language-specific validation rules, then these are also loaded. |
| 66 // |
| 67 // Example rule: |
| 68 // https://i18napis.appspot.com/ssl-aggregate-address/data/US |
| 69 // |
| 70 // If the rules are already in progress of being loaded, it does nothing. |
| 71 // Calls |loaded| when the loading has finished. |
| 72 virtual void LoadRules(const std::string& region_code, |
| 73 const Callback& loaded); |
| 74 |
| 75 // Validates the |address| and populates |problems| with the validation |
| 76 // problems, filtered according to the |filter| parameter. |
| 77 // |
| 78 // If the |filter| is empty, then all discovered validation problems are |
| 79 // returned. If the |filter| contains problem elements, then only the problems |
| 80 // in the |filter| may be returned. |
| 81 virtual Status Validate( |
| 82 const ::i18n::addressinput::AddressData& address, |
| 83 const ::i18n::addressinput::FieldProblemMap* filter, |
| 84 ::i18n::addressinput::FieldProblemMap* problems) const; |
| 85 |
| 86 // Fills in |suggestions| for the partially typed in |user_input|, assuming |
| 87 // the user is typing in the |focused_field|. If the number of |suggestions| |
| 88 // is over the |suggestion_limit|, then returns no |suggestions| at all. |
| 89 // |
| 90 // If the |solutions| parameter is NULL, the checks whether the validation |
| 91 // rules are available, but does not fill in suggestions. |
| 92 // |
| 93 // Sample user input 1: |
| 94 // country code = "US" |
| 95 // postal code = "90066" |
| 96 // focused field = POSTAL_CODE |
| 97 // suggestions limit = 1 |
| 98 // Suggestion: |
| 99 // [{administrative_area: "CA"}] |
| 100 // |
| 101 // Sample user input 2: |
| 102 // country code = "CN" |
| 103 // dependent locality = "Zongyang" |
| 104 // focused field = DEPENDENT_LOCALITY |
| 105 // suggestions limit = 10 |
| 106 // Suggestion: |
| 107 // [{dependent_locality: "Zongyang Xian", |
| 108 // locality: "Anqing Shi", |
| 109 // administrative_area: "Anhui Sheng"}] |
| 110 virtual Status GetSuggestions( |
| 111 const ::i18n::addressinput::AddressData& user_input, |
| 112 ::i18n::addressinput::AddressField focused_field, |
| 113 size_t suggestion_limit, |
| 114 std::vector< ::i18n::addressinput::AddressData>* suggestions) const; |
| 115 |
| 116 // Canonicalizes the administrative area in |address_data|. For example, |
| 117 // "texas" changes to "TX". Returns true on success, otherwise leaves |
| 118 // |address_data| alone and returns false. |
| 119 virtual bool CanonicalizeAdministrativeArea( |
| 120 ::i18n::addressinput::AddressData* address_data) const; |
| 121 |
| 122 private: |
| 123 void Validated(bool success, |
| 124 const ::i18n::addressinput::AddressData&, |
| 125 const ::i18n::addressinput::FieldProblemMap&); |
| 126 |
| 127 const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; |
| 128 const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; |
| 129 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 130 validated_; |
| 131 |
| 132 friend class MockAddressValidator; |
| 133 PreloadAddressValidator(); |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(PreloadAddressValidator); |
| 136 }; |
| 137 |
| 138 } // namespace autofill |
| 139 |
| 140 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_PRELOAD_ADDRESS_VALIDATOR_H_ |
OLD | NEW |