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 requested will be informed and the request cancelled. This value should | |
please use gerrit instead
2017/02/21 17:15:13
s/requested/requester/
sebsg
2017/02/21 18:30:59
Done.
| |
54 // be greated or equal to 0, for which it means that the normalization should | |
please use gerrit instead
2017/02/21 17:15:12
s/greated or equal to/at least/
sebsg
2017/02/21 18:30:59
Done.
| |
55 // happen synchronously, or not at all. Will start loading the rules for the | |
please use gerrit instead
2017/02/21 17:15:13
Please explain why 0 could be no normalization at
sebsg
2017/02/21 18:30:59
Done.
| |
56 // |region_code| if they had not started loading. | |
55 void StartAddressNormalization(const autofill::AutofillProfile& profile, | 57 void StartAddressNormalization(const autofill::AutofillProfile& profile, |
56 const std::string& region_code, | 58 const std::string& region_code, |
59 int timeout_seconds, | |
57 Delegate* requester); | 60 Delegate* requester); |
58 | 61 |
59 private: | 62 private: |
60 // Called when the validation rules for the |region_code| have finished | 63 // Called when the validation rules for the |region_code| have finished |
61 // loading. Implementation of the LoadRulesListener interface. | 64 // loading. Implementation of the LoadRulesListener interface. |
62 void OnAddressValidationRulesLoaded(const std::string& region_code, | 65 void OnAddressValidationRulesLoaded(const std::string& region_code, |
63 bool success) override; | 66 bool success) override; |
64 | 67 |
65 // Map associating a region code to pending normalizations. | 68 // Map associating a region code to pending normalizations. |
66 std::map<std::string, std::vector<std::unique_ptr<Request>>> | 69 std::map<std::string, std::vector<std::unique_ptr<Request>>> |
67 pending_normalization_; | 70 pending_normalization_; |
68 | 71 |
69 // The address validator used to normalize addresses. | 72 // The address validator used to normalize addresses. |
70 autofill::AddressValidator address_validator_; | 73 autofill::AddressValidator address_validator_; |
71 | 74 |
72 DISALLOW_COPY_AND_ASSIGN(AddressNormalizer); | 75 DISALLOW_COPY_AND_ASSIGN(AddressNormalizer); |
73 }; | 76 }; |
74 | 77 |
75 } // namespace payments | 78 } // namespace payments |
76 | 79 |
77 #endif // COMPONENTS_PAYMENTS_ADDRESS_NORMALIZER_H_ | 80 #endif // COMPONENTS_PAYMENTS_ADDRESS_NORMALIZER_H_ |
OLD | NEW |