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