OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZATION_MANAGER_H_ |
| 6 #define COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZATION_MANAGER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/threading/thread_checker.h" |
| 14 #include "components/payments/core/address_normalizer.h" |
| 15 |
| 16 namespace autofill { |
| 17 class AutofillProfile; |
| 18 } // namespace autofill |
| 19 |
| 20 namespace payments { |
| 21 |
| 22 class AddressNormalizer; |
| 23 |
| 24 // Class to handle multiple concurrent address normalization requests. This |
| 25 // class is not thread-safe. |
| 26 class AddressNormalizationManager { |
| 27 public: |
| 28 // Initializes an AddressNormalizationManager. |default_country_code| will be |
| 29 // used if the country code in an AutofillProfile to normalize is not valid. |
| 30 // The AddressNormalizationManager takes ownership of |address_normalizer|. |
| 31 AddressNormalizationManager( |
| 32 std::unique_ptr<AddressNormalizer> address_normalizer, |
| 33 const std::string& default_country_code); |
| 34 |
| 35 ~AddressNormalizationManager(); |
| 36 |
| 37 // Stops accepting normalization requests. If all the address normalization |
| 38 // requests have already completed, |completion_callback| will be called |
| 39 // before this method returns. Otherwise, it will be called as soon as the |
| 40 // last pending request completes. |
| 41 void FinalizeWithCompletionCallback(base::OnceClosure completion_callback); |
| 42 |
| 43 // Normalizes the address in |profile|. This may or may not happen |
| 44 // asynchronously. On completion, the address in |profile| will be updated |
| 45 // with the normalized address. |
| 46 void StartNormalizingAddress(autofill::AutofillProfile* profile); |
| 47 |
| 48 private: |
| 49 // Implements the payments::AddressNormalizer::Delegate interface, and |
| 50 // notifies its parent AddressNormalizationManager when normalization has |
| 51 // completed. |
| 52 class NormalizerDelegate : public AddressNormalizer::Delegate { |
| 53 public: |
| 54 // |owner| is the parent AddressNormalizationManager, |address_normalizer| |
| 55 // is a pointer to an instance of AddressNormalizer which will handle |
| 56 // normalization of |profile|. |profile| will be updated when normalization |
| 57 // is complete. |
| 58 NormalizerDelegate(AddressNormalizationManager* owner, |
| 59 AddressNormalizer* address_normalizer, |
| 60 autofill::AutofillProfile* profile); |
| 61 |
| 62 // Returns whether this delegate has completed or not. |
| 63 bool has_completed() const { return has_completed_; } |
| 64 |
| 65 // payments::AddressNormalizer::Delegate: |
| 66 void OnAddressNormalized( |
| 67 const autofill::AutofillProfile& normalized_profile) override; |
| 68 void OnCouldNotNormalize(const autofill::AutofillProfile& profile) override; |
| 69 |
| 70 private: |
| 71 // Helper method that handles when normalization has completed. |
| 72 void OnCompletion(const autofill::AutofillProfile& profile); |
| 73 |
| 74 bool has_completed_ = false; |
| 75 AddressNormalizationManager* owner_ = nullptr; |
| 76 autofill::AutofillProfile* profile_ = nullptr; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(NormalizerDelegate); |
| 79 }; |
| 80 |
| 81 friend class NormalizerDelegate; |
| 82 |
| 83 // Runs the completion callback if all the delegates have completed. |
| 84 void MaybeRunCompletionCallback(); |
| 85 |
| 86 // Whether the AddressNormalizationManager is still accepting requests or not. |
| 87 bool accepting_requests_ = true; |
| 88 |
| 89 // The default country code to use if a profile does not have a valid country. |
| 90 const std::string default_country_code_; |
| 91 |
| 92 // The callback to execute when all addresses have been normalized. |
| 93 base::OnceClosure completion_callback_; |
| 94 |
| 95 // Storage for all the delegates that handle the normalization requests. |
| 96 std::vector<std::unique_ptr<NormalizerDelegate>> delegates_; |
| 97 |
| 98 // The AddressNormalizer to use. Owned by this class. |
| 99 std::unique_ptr<AddressNormalizer> address_normalizer_; |
| 100 |
| 101 THREAD_CHECKER(thread_checker_); |
| 102 DISALLOW_COPY_AND_ASSIGN(AddressNormalizationManager); |
| 103 }; |
| 104 |
| 105 } // namespace payments |
| 106 |
| 107 #endif // COMPONENTS_PAYMENTS_CORE_ADDRESS_NORMALIZATION_MANAGER_H_ |
OLD | NEW |