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