1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file. 4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ 6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ 7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <map> 9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <string> 10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <vector> 11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/macros.h" 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/scoped_ptr.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/memory/weak_ptr.h" 15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/time/time.h" 16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h" 17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h" 18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h" 19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h" 20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace i18n { 22116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace addressinput { 23116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AddressNormalizer; 246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class Source; 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass Storage; 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstruct AddressData; 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 30116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace autofill { 31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 32116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass InputSuggester; 33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// The object to be notified when loading of address validation rules is 35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// finished. 36116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass LoadRulesListener { 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual ~LoadRulesListener() {} 39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Called when the validation rules for the |region_code| have been loaded. 41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The validation rules include the generic rules for the |region_code| and 42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // specific rules for the country's administrative areas, localities, and 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // dependent localities. If a country has language-specific validation rules, 44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // then these are also loaded. 45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The |success| parameter is true when the rules were loaded successfully. 47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void OnAddressValidationRulesLoaded(const std::string& region_code, 48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch bool success) = 0; 49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Interface to the libaddressinput AddressValidator for Chromium Autofill. The 52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// class is named AddressValidator to simplify switching between libaddressinput 53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// and this version. 54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// 55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// It's not possible to name this file address_validator.h because some 56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// compilers do not handle multiple files with the same name (although in 57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// different directories) gracefully. This class is a shim between upstream 58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// libaddressinput API and the API that Chrome expects, hence the file name 59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// chrome_address_validator.h. 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AddressValidator { 61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public: 62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The status of address validation. 63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch enum Status { 64116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Address validation completed successfully. Check |problems| to see if any 65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // problems were found. 66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch SUCCESS, 67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The validation rules are not available, because LoadRules() was not 69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // called or failed. Reload the rules. 70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch RULES_UNAVAILABLE, 71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The validation rules are being loaded. Try again later. 73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch RULES_NOT_READY 74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch }; 75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Takes ownership of |source| and |storage|. 776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) AddressValidator(scoped_ptr< ::i18n::addressinput::Source> source, 78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_ptr< ::i18n::addressinput::Storage> storage, 79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LoadRulesListener* load_rules_listener); 80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual ~AddressValidator(); 82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Loads the generic validation rules for |region_code| and specific rules 84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // for the region's administrative areas, localities, and dependent 85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // localities. A typical data size is 10KB. The largest is 250KB. If a region 86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // has language-specific validation rules, then these are also loaded. 87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Example rule: 89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // https://i18napis.appspot.com/ssl-aggregate-address/data/US 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // If the rules are already in progress of being loaded, it does nothing. 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Invokes |load_rules_listener| when the loading has finished. 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual void LoadRules(const std::string& region_code); 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Validates the |address| and populates |problems| with the validation 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // problems, filtered according to the |filter| parameter. 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // If the |filter| is empty, then all discovered validation problems are 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // returned. If the |filter| contains problem elements, then only the problems 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // in the |filter| may be returned. 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual Status ValidateAddress( 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ::i18n::addressinput::AddressData& address, 103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ::i18n::addressinput::FieldProblemMap* filter, 104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ::i18n::addressinput::FieldProblemMap* problems) const; 105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Fills in |suggestions| for the partially typed in |user_input|, assuming 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the user is typing in the |focused_field|. If the number of |suggestions| 108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // is over the |suggestion_limit|, then returns no |suggestions| at all. 109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // If the |solutions| parameter is NULL, the checks whether the validation 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // rules are available, but does not fill in suggestions. 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Sample user input 1: 114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // country code = "US" 115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // postal code = "90066" 116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // focused field = POSTAL_CODE 117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // suggestions limit = 1 118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Suggestion: 119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // [{administrative_area: "CA"}] 120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // 121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Sample user input 2: 122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // country code = "CN" 123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // dependent locality = "Zongyang" 124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // focused field = DEPENDENT_LOCALITY 125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // suggestions limit = 10 126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Suggestion: 127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // [{dependent_locality: "Zongyang Xian", 128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // locality: "Anqing Shi", 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // administrative_area: "Anhui Sheng"}] 130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual Status GetSuggestions( 131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ::i18n::addressinput::AddressData& user_input, 132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ::i18n::addressinput::AddressField focused_field, 133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch size_t suggestion_limit, 134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::vector< ::i18n::addressinput::AddressData>* suggestions) const; 135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Canonicalizes the administrative area in |address_data|. For example, 137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // "texas" changes to "TX". Returns true on success, otherwise leaves 138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |address_data| alone and returns false. 139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual bool CanonicalizeAdministrativeArea( 140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch ::i18n::addressinput::AddressData* address) const; 141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch protected: 143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Constructor used only for MockAddressValidator. 144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch AddressValidator(); 145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Returns the period of time to wait between the first attempt's failure and 147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // the second attempt's initiation to load rules. Exposed for testing. 148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch virtual base::TimeDelta GetBaseRetryPeriod() const; 149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private: 151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Verifies that |validator_| succeeded. Invoked by |validated_| callback. 152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void Validated(bool success, 153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ::i18n::addressinput::AddressData&, 154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const ::i18n::addressinput::FieldProblemMap&); 155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Invokes the |load_rules_listener_|, if it's not NULL. Called by 157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // |rules_loaded_| callback. 158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void RulesLoaded(bool success, const std::string& region_code, int); 159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Retries loading rules without resetting the retry counter. 161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch void RetryLoadRules(const std::string& region_code); 162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Loads and stores aggregate rules at COUNTRY level. 164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr< ::i18n::addressinput::PreloadSupplier> supplier_; 165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Suggests addresses based on user input. 167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr<InputSuggester> input_suggester_; 168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Normalizes addresses into a canonical form. 170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr< ::i18n::addressinput::AddressNormalizer> normalizer_; 171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Validates addresses. 173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr<const ::i18n::addressinput::AddressValidator> validator_; 174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The callback that |validator_| invokes when it finished validating an 176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // address. 177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr<const ::i18n::addressinput::AddressValidator::Callback> 178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch validated_; 179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // The callback that |supplier_| invokes when it finished loading rules. 181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch const scoped_ptr<const ::i18n::addressinput::PreloadSupplier::Callback> 182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch rules_loaded_; 183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Not owned delegate to invoke when |suppler_| finished loading rules. Can be 185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // NULL. 186116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch LoadRulesListener* const load_rules_listener_; 187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // A mapping of region codes to the number of attempts to retry loading rules. 189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch std::map<std::string, int> attempts_number_; 190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // Member variables should appear before the WeakPtrFactory, to ensure that 192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // any WeakPtrs to AddressValidator are invalidated before its members 193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch // variable's destructors are executed, rendering them invalid. 194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::WeakPtrFactory<AddressValidator> weak_factory_; 195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch DISALLOW_COPY_AND_ASSIGN(AddressValidator); 197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}; 198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 199116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} // namespace autofill 200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_ADDRESS_VALIDATOR_H_ 202