OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ | 5 #ifndef COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ |
6 #define COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ | 6 #define COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ |
7 | 7 |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | 8 #include <string> |
11 #include <vector> | |
12 | 9 |
13 #include "base/macros.h" | |
14 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" | 10 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
15 | 11 |
16 namespace autofill { | 12 namespace autofill { |
17 class AutofillProfile; | 13 class AutofillProfile; |
18 } | 14 } |
19 | 15 |
20 namespace i18n { | |
21 namespace libadderssinput { | |
22 class Source; | |
23 class Storage; | |
24 } | |
25 } | |
26 | |
27 namespace payments { | 16 namespace payments { |
28 | 17 |
29 // A class used to normalize addresses. | 18 // A class used to normalize addresses. |
30 class AddressNormalizer : public autofill::LoadRulesListener { | 19 class AddressNormalizer : public autofill::LoadRulesListener { |
31 public: | 20 public: |
32 // The interface for the normalization delegates. | 21 // The interface for the normalization delegates. |
33 class Delegate { | 22 class Delegate { |
34 public: | 23 public: |
35 virtual void OnAddressNormalized( | 24 virtual void OnAddressNormalized( |
36 const autofill::AutofillProfile& normalized_profile) = 0; | 25 const autofill::AutofillProfile& normalized_profile) = 0; |
37 | 26 |
38 virtual void OnCouldNotNormalize( | 27 virtual void OnCouldNotNormalize( |
39 const autofill::AutofillProfile& profile) = 0; | 28 const autofill::AutofillProfile& profile) = 0; |
40 | 29 |
41 protected: | 30 protected: |
42 virtual ~Delegate() {} | 31 virtual ~Delegate() {} |
43 }; | 32 }; |
44 | 33 |
45 // The interface for the normalization request. | 34 // The interface for the normalization request. |
46 class Request { | 35 class Request { |
47 public: | 36 public: |
48 virtual void OnRulesLoaded(bool success) = 0; | 37 virtual void OnRulesLoaded(bool success) = 0; |
49 virtual ~Request() {} | 38 virtual ~Request() {} |
50 }; | 39 }; |
51 | 40 |
52 AddressNormalizer(std::unique_ptr<::i18n::addressinput::Source> source, | |
53 std::unique_ptr<::i18n::addressinput::Storage> storage); | |
54 ~AddressNormalizer() override; | |
55 | |
56 // Start loading the validation rules for the specified |region_code|. | 41 // Start loading the validation rules for the specified |region_code|. |
57 virtual void LoadRulesForRegion(const std::string& region_code); | 42 virtual void LoadRulesForRegion(const std::string& region_code) = 0; |
58 | 43 |
59 // Returns whether the rules for the specified |region_code| have finished | 44 // Returns whether the rules for the specified |region_code| have finished |
60 // loading. | 45 // loading. |
61 bool AreRulesLoadedForRegion(const std::string& region_code); | 46 virtual bool AreRulesLoadedForRegion(const std::string& region_code) = 0; |
62 | 47 |
63 // Starts the normalization of the |profile| based on the |region_code|. The | 48 // Starts the normalization of the |profile| based on the |region_code|. The |
64 // normalized profile will be returned to the |requester| possibly | 49 // normalized profile will be returned to the |requester| possibly |
65 // asynchronously. If the normalization is not completed in |timeout_seconds| | 50 // asynchronously. If the normalization is not completed in |timeout_seconds| |
66 // the requester will be informed and the request cancelled. This value should | 51 // the requester will be informed and the request cancelled. This value should |
67 // be greater or equal to 0, for which it means that the normalization should | 52 // be greater or equal to 0, for which it means that the normalization should |
68 // happen synchronously, or not at all if the rules are not already loaded. | 53 // happen synchronously, or not at all if the rules are not already loaded. |
69 // Will start loading the rules for the |region_code| if they had not started | 54 // Will start loading the rules for the |region_code| if they had not started |
70 // loading. | 55 // loading. |
71 void StartAddressNormalization(const autofill::AutofillProfile& profile, | 56 virtual void StartAddressNormalization( |
72 const std::string& region_code, | 57 const autofill::AutofillProfile& profile, |
73 int timeout_seconds, | 58 const std::string& region_code, |
74 Delegate* requester); | 59 int timeout_seconds, |
75 | 60 Delegate* requester) = 0; |
76 private: | |
77 // Called when the validation rules for the |region_code| have finished | |
78 // loading. Implementation of the LoadRulesListener interface. | |
79 void OnAddressValidationRulesLoaded(const std::string& region_code, | |
80 bool success) override; | |
81 | |
82 // Map associating a region code to pending normalizations. | |
83 std::map<std::string, std::vector<std::unique_ptr<Request>>> | |
84 pending_normalization_; | |
85 | |
86 // The address validator used to normalize addresses. | |
87 autofill::AddressValidator address_validator_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(AddressNormalizer); | |
90 }; | 61 }; |
91 | 62 |
92 } // namespace payments | 63 } // namespace payments |
93 | 64 |
94 #endif // COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ | 65 #endif // COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZER_H_ |
OLD | NEW |