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

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

Issue 2879853003: [Payments] Code Refactoring for the Subkey Request (Closed)
Patch Set: Changing a comment. 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(const std::string& region_code,
32 int timeout_seconds,
33 SubKeyRequester::Delegate* delegate,
34 autofill::AddressValidator* address_validator)
35 : region_code_(region_code),
36 delegate_(delegate),
37 address_validator_(address_validator),
38 has_responded_(false),
39 on_timeout_(base::Bind(&::payments::SubKeyRequest::OnRulesLoaded,
40 base::Unretained(this))) {
41 base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
42 FROM_HERE, on_timeout_.callback(),
43 base::TimeDelta::FromSeconds(timeout_seconds));
44 }
45
46 ~SubKeyRequest() override {}
47
48 void OnRulesLoaded() override {
49 on_timeout_.Cancel();
50 // Check if the timeout happened before the rules were loaded.
51 if (has_responded_)
52 return;
David Trainor- moved to gerrit 2017/05/19 17:44:35 Will this leak the delegate? This is why we shoul
Parastoo 2017/05/23 18:10:38 Acknowledged.
53 has_responded_ = true;
54
55 delegate_->OnSubKeysReceived(
56 address_validator_->GetRegionSubKeys(region_code_));
57 }
58
59 private:
60 std::string region_code_;
61 // Not owned. Never null. Outlive this object.
62 SubKeyRequester::Delegate* delegate_;
63 autofill::AddressValidator* address_validator_;
64
65 bool has_responded_;
66 base::CancelableCallback<void()> on_timeout_;
67
68 DISALLOW_COPY_AND_ASSIGN(SubKeyRequest);
69 };
70
71 } // namespace
72
73 SubKeyRequester::SubKeyRequester(std::unique_ptr<Source> source,
74 std::unique_ptr<Storage> storage)
75 : address_validator_(std::move(source), std::move(storage), this) {}
76
77 SubKeyRequester::~SubKeyRequester() {}
78
79 void SubKeyRequester::StartRegionSubKeysRequest(const std::string& region_code,
80 int timeout_seconds,
81 Delegate* requester) {
82 DCHECK(timeout_seconds >= 0);
83 DCHECK(requester != nullptr);
84
85 std::unique_ptr<SubKeyRequest> request(base::MakeUnique<SubKeyRequest>(
David Trainor- moved to gerrit 2017/05/19 17:44:35 Could just store a base::Callback or base::Callbac
Parastoo 2017/05/23 18:09:52 Done.
86 region_code, timeout_seconds, requester, &address_validator_));
87
88 if (AreRulesLoadedForRegion(region_code)) {
89 request->OnRulesLoaded();
90 } else {
91 // Setup the variables so that the subkeys request is sent, when the rules
92 // are loaded.
93 pending_subkey_region_code_ = region_code;
94 pending_subkey_request_ = std::move(request);
95
96 // Start loading the rules for that region. If the rules were already in the
97 // process of being loaded, this call will do nothing.
98 LoadRulesForRegion(region_code);
99 }
100 }
101
102 bool SubKeyRequester::AreRulesLoadedForRegion(const std::string& region_code) {
103 return address_validator_.AreRulesLoadedForRegion(region_code);
104 }
105
106 void SubKeyRequester::LoadRulesForRegion(const std::string& region_code) {
107 address_validator_.LoadRules(region_code);
108 }
109
110 void SubKeyRequester::OnAddressValidationRulesLoaded(
111 const std::string& region_code,
112 bool success) {
113 // The case for |success| == false is already handled. if |success| == false,
114 // AddressValidator::GetRegionSubKeys will return an empty list of subkeys.
115 // Therefore, here, we can ignore the value of |success|.
116
117 // Check if there is any subkey request for that region code.
118 if (!pending_subkey_region_code_.compare(region_code))
119 pending_subkey_request_->OnRulesLoaded();
David Trainor- moved to gerrit 2017/05/19 17:44:35 Are we guaranteed to get a callback for this? Jus
Parastoo 2017/05/23 18:09:52 Yes.
120 pending_subkey_region_code_.clear();
121 pending_subkey_request_.reset();
122 }
123
124 void SubKeyRequester::CancelPendingGetSubKeys() {
125 pending_subkey_region_code_.clear();
126 pending_subkey_request_.reset();
127 }
128
129 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698