input_suggester.h 4.6 KB

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