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_INPUT_SUGGESTER_H_ |
| 6 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_INPUT_SUGGESTER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "third_party/icu/source/i18n/unicode/coll.h" |
| 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" |
| 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_in
put_helper.h" |
| 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" |
| 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/region_dat
a_builder.h" |
| 17 |
| 18 namespace i18n { |
| 19 namespace addressinput { |
| 20 class PreloadSupplier; |
| 21 class RegionData; |
| 22 struct AddressData; |
| 23 } |
| 24 } |
| 25 |
| 26 namespace autofill { |
| 27 |
| 28 // Suggests address completions for a partially entered address from the user. |
| 29 class InputSuggester { |
| 30 public: |
| 31 // Does not take ownership of |supplier|, which should not be NULL. |
| 32 explicit InputSuggester(::i18n::addressinput::PreloadSupplier* supplier); |
| 33 ~InputSuggester(); |
| 34 |
| 35 // Fills in |suggestions| for the partially typed in |user_input|, assuming |
| 36 // the user is typing in the |focused_field|. If the number of |suggestions| |
| 37 // is over the |suggestion_limit|, then returns no |suggestions| at all. |
| 38 // |
| 39 // Sample user input 1: |
| 40 // country code = "US" |
| 41 // postal code = "90066" |
| 42 // focused field = POSTAL_CODE |
| 43 // suggestions limit = 1 |
| 44 // Suggestion: |
| 45 // [{administrative_area: "CA"}] |
| 46 // |
| 47 // Sample user input 2: |
| 48 // country code = "CN" |
| 49 // dependent locality = "Zongyang" |
| 50 // focused field = DEPENDENT_LOCALITY |
| 51 // suggestions limit = 10 |
| 52 // Suggestion: |
| 53 // [{dependent_locality: "Zongyang Xian", |
| 54 // locality: "Anqing Shi", |
| 55 // administrative_area: "Anhui Sheng"}] |
| 56 // |
| 57 // Builds the index for generating suggestions lazily. |
| 58 // |
| 59 // The |suggestions| parameter should not be NULL. The |focused_field| |
| 60 // parameter should be either POSTAL_CODE or between ADMIN_AREA and |
| 61 // DEPENDENT_LOCALITY inclusively. |
| 62 void GetSuggestions( |
| 63 const ::i18n::addressinput::AddressData& user_input, |
| 64 ::i18n::addressinput::AddressField focused_field, |
| 65 size_t suggestion_limit, |
| 66 std::vector< ::i18n::addressinput::AddressData>* suggestions); |
| 67 |
| 68 private: |
| 69 class SubRegionData; |
| 70 |
| 71 // Canonicalizes strings for case and diacritic insensitive comparison. |
| 72 class StringCanonicalizer { |
| 73 public: |
| 74 // Initializes the canonicalizer. This is slow, so avoid calling it more |
| 75 // often than necessary. |
| 76 StringCanonicalizer(); |
| 77 ~StringCanonicalizer(); |
| 78 |
| 79 // Returns a canonical version of the string that can be used for comparing |
| 80 // strings regardless of diacritics and capitalization. |
| 81 // Canonicalize("Texas") == Canonicalize("T\u00E9xas"); |
| 82 // Canonicalize("Texas") == Canonicalize("teXas"); |
| 83 // Canonicalize("Texas") != Canonicalize("California"); |
| 84 // |
| 85 // The output is not human-readable. |
| 86 // Canonicalize("Texas") != "Texas"; |
| 87 // |
| 88 // The caller does not own the result, which will not be NULL. The |
| 89 // |original| parameter should not be empty. The |result_size| parameter |
| 90 // should not be NULL. |
| 91 const uint8_t* Canonicalize(const std::string& original, |
| 92 int32_t* result_size) const; |
| 93 |
| 94 private: |
| 95 static const int32_t kInitialBufferSize = 32; |
| 96 mutable int32_t buffer_size_; |
| 97 mutable scoped_ptr<uint8_t[]> buffer_; |
| 98 scoped_ptr<icu::Collator> collator_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(StringCanonicalizer); |
| 101 }; |
| 102 |
| 103 // The method to be invoked by |validated_| callback. |
| 104 void Validated(bool success, |
| 105 const ::i18n::addressinput::AddressData&, |
| 106 const ::i18n::addressinput::FieldProblemMap&); |
| 107 |
| 108 // Data source for region data. |
| 109 ::i18n::addressinput::RegionDataBuilder region_data_builder_; |
| 110 |
| 111 // Suggests sub-regions based on postal code. |
| 112 const ::i18n::addressinput::AddressInputHelper input_helper_; |
| 113 |
| 114 // Verifies that suggested sub-regions match the postal code. |
| 115 ::i18n::addressinput::AddressValidator validator_; |
| 116 |
| 117 // The callback for |validator_| to invoke when validation finishes. |
| 118 const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> |
| 119 validated_; |
| 120 |
| 121 // A mapping from a COUNTRY level region to a collection of all of its |
| 122 // sub-regions along with metadata used to construct suggestions. |
| 123 std::map<const ::i18n::addressinput::RegionData*, const SubRegionData*> |
| 124 sub_regions_; |
| 125 |
| 126 // Canonicalizes strings for case and diacritic insensitive search of |
| 127 // sub-region names. |
| 128 StringCanonicalizer canonicalizer_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(InputSuggester); |
| 131 }; |
| 132 |
| 133 } // namespace autofill |
| 134 |
| 135 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_INPUT_SUGGESTER_H_ |
OLD | NEW |