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