Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: components/payments/core/subkey_requester.cc

Issue 2879853003: [Payments] Code Refactoring for the Subkey Request (Closed)
Patch Set: Remove Delegate Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "components/payments/core/subkey_requester.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/sequenced_task_runner_handle.h"
16 #include "base/time/time.h"
17 #include "third_party/libaddressinput/chromium/chrome_address_validator.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
21 namespace payments {
22
23 namespace {
24
25 using ::i18n::addressinput::Source;
26 using ::i18n::addressinput::Storage;
27
28 class SubKeyRequest : public SubKeyRequester::Request {
29 public:
30 // The |delegate| and |address_validator| need to outlive this Request.
31 SubKeyRequest(
32 const std::string& region_code,
33 int timeout_seconds,
34 autofill::AddressValidator* address_validator,
35 base::OnceCallback<void(std::vector<std::string>)> on_subkeys_received)
36 : region_code_(region_code),
37 address_validator_(address_validator),
38 on_subkeys_received_(std::move(on_subkeys_received)),
39 has_responded_(false),
40 on_timeout_(base::Bind(&::payments::SubKeyRequest::OnRulesLoaded,
41 base::Unretained(this))) {
42 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
43 FROM_HERE, on_timeout_.callback(),
44 base::TimeDelta::FromSeconds(timeout_seconds));
45 }
46
47 ~SubKeyRequest() override {}
48
49 void OnRulesLoaded() override {
50 on_timeout_.Cancel();
51 // Check if the timeout happened before the rules were loaded.
52 if (has_responded_)
53 return;
54 has_responded_ = true;
55
56 std::move(on_subkeys_received_)
57 .Run(address_validator_->GetRegionSubKeys(region_code_));
58 }
59
60 private:
61 std::string region_code_;
62 // Not owned. Never null. Outlive this object.
63 autofill::AddressValidator* address_validator_;
64
65 base::OnceCallback<void(std::vector<std::string>)> on_subkeys_received_;
66
67 bool has_responded_;
68 base::CancelableCallback<void()> on_timeout_;
69
70 DISALLOW_COPY_AND_ASSIGN(SubKeyRequest);
71 };
72
73 } // namespace
74
75 SubKeyRequester::SubKeyRequester(std::unique_ptr<Source> source,
76 std::unique_ptr<Storage> storage)
77 : address_validator_(std::move(source), std::move(storage), this) {}
78
79 SubKeyRequester::~SubKeyRequester() {}
80
81 void SubKeyRequester::StartRegionSubKeysRequest(
82 const std::string& region_code,
83 int timeout_seconds,
84 base::OnceCallback<void(std::vector<std::string>)> cb) {
85 DCHECK(timeout_seconds >= 0);
86
87 std::unique_ptr<SubKeyRequest> request(base::MakeUnique<SubKeyRequest>(
88 region_code, timeout_seconds, &address_validator_, std::move(cb)));
89
90 if (AreRulesLoadedForRegion(region_code)) {
91 request->OnRulesLoaded();
92 } else {
93 // Setup the variables so that the subkeys request is sent, when the rules
94 // are loaded.
95 pending_subkey_region_code_ = region_code;
96 pending_subkey_request_ = std::move(request);
97
98 // Start loading the rules for that region. If the rules were already in the
99 // process of being loaded, this call will do nothing.
100 LoadRulesForRegion(region_code);
101 }
102 }
103
104 bool SubKeyRequester::AreRulesLoadedForRegion(const std::string& region_code) {
105 return address_validator_.AreRulesLoadedForRegion(region_code);
106 }
107
108 void SubKeyRequester::LoadRulesForRegion(const std::string& region_code) {
109 address_validator_.LoadRules(region_code);
110 }
111
112 void SubKeyRequester::OnAddressValidationRulesLoaded(
113 const std::string& region_code,
114 bool success) {
115 // The case for |success| == false is already handled. if |success| == false,
116 // AddressValidator::GetRegionSubKeys will return an empty list of subkeys.
117 // Therefore, here, we can ignore the value of |success|.
118
119 // Check if there is any subkey request for that region code.
120 if (!pending_subkey_region_code_.compare(region_code))
121 pending_subkey_request_->OnRulesLoaded();
122 pending_subkey_region_code_.clear();
123 pending_subkey_request_.reset();
124 }
125
126 void SubKeyRequester::CancelPendingGetSubKeys() {
127 pending_subkey_region_code_.clear();
128 pending_subkey_request_.reset();
129 }
130
131 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698