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

Unified Diff: components/payments/core/subkey_requester.cc

Issue 2879853003: [Payments] Code Refactoring for the Subkey Request (Closed)
Patch Set: Final nit 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/payments/core/subkey_requester.h ('k') | components/payments/core/subkey_requester_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/payments/core/subkey_requester.cc
diff --git a/components/payments/core/subkey_requester.cc b/components/payments/core/subkey_requester.cc
new file mode 100644
index 0000000000000000000000000000000000000000..513fc54ffc49fb980b37e1d24ad388fd2f504d2a
--- /dev/null
+++ b/components/payments/core/subkey_requester.cc
@@ -0,0 +1,129 @@
+// 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.
+
+#include "components/payments/core/subkey_requester.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/cancelable_callback.h"
+#include "base/memory/ptr_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/threading/sequenced_task_runner_handle.h"
+#include "base/time/time.h"
+#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
+#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
+#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
+#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
+namespace payments {
+
+namespace {
+
+using ::i18n::addressinput::Source;
+using ::i18n::addressinput::Storage;
+
+class SubKeyRequest : public SubKeyRequester::Request {
+ public:
+ // The |delegate| and |address_validator| need to outlive this Request.
+ SubKeyRequest(const std::string& region_code,
+ int timeout_seconds,
+ autofill::AddressValidator* address_validator,
+ SubKeyReceiverCallback on_subkeys_received)
+ : region_code_(region_code),
+ address_validator_(address_validator),
+ on_subkeys_received_(std::move(on_subkeys_received)),
+ has_responded_(false),
+ on_timeout_(base::Bind(&::payments::SubKeyRequest::OnRulesLoaded,
+ base::Unretained(this))) {
+ base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE, on_timeout_.callback(),
+ base::TimeDelta::FromSeconds(timeout_seconds));
+ }
+
+ ~SubKeyRequest() override {}
+
+ void OnRulesLoaded() override {
+ on_timeout_.Cancel();
+ // Check if the timeout happened before the rules were loaded.
+ if (has_responded_)
+ return;
+ has_responded_ = true;
+
+ std::move(on_subkeys_received_)
+ .Run(address_validator_->GetRegionSubKeys(region_code_));
+ }
+
+ private:
+ std::string region_code_;
+ // Not owned. Never null. Outlive this object.
+ autofill::AddressValidator* address_validator_;
+
+ SubKeyReceiverCallback on_subkeys_received_;
+
+ bool has_responded_;
+ base::CancelableCallback<void()> on_timeout_;
+
+ DISALLOW_COPY_AND_ASSIGN(SubKeyRequest);
+};
+
+} // namespace
+
+SubKeyRequester::SubKeyRequester(std::unique_ptr<Source> source,
+ std::unique_ptr<Storage> storage)
+ : address_validator_(std::move(source), std::move(storage), this) {}
+
+SubKeyRequester::~SubKeyRequester() {}
+
+void SubKeyRequester::StartRegionSubKeysRequest(const std::string& region_code,
+ int timeout_seconds,
+ SubKeyReceiverCallback cb) {
+ DCHECK(timeout_seconds >= 0);
+
+ std::unique_ptr<SubKeyRequest> request(base::MakeUnique<SubKeyRequest>(
+ region_code, timeout_seconds, &address_validator_, std::move(cb)));
+
+ if (AreRulesLoadedForRegion(region_code)) {
+ request->OnRulesLoaded();
+ } else {
+ // Setup the variables so that the subkeys request is sent, when the rules
+ // are loaded.
+ pending_subkey_region_code_ = region_code;
+ pending_subkey_request_ = std::move(request);
+
+ // Start loading the rules for that region. If the rules were already in the
+ // process of being loaded, this call will do nothing.
+ LoadRulesForRegion(region_code);
+ }
+}
+
+bool SubKeyRequester::AreRulesLoadedForRegion(const std::string& region_code) {
+ return address_validator_.AreRulesLoadedForRegion(region_code);
+}
+
+void SubKeyRequester::LoadRulesForRegion(const std::string& region_code) {
+ address_validator_.LoadRules(region_code);
+}
+
+void SubKeyRequester::OnAddressValidationRulesLoaded(
+ const std::string& region_code,
+ bool success) {
+ // The case for |success| == false is already handled. if |success| == false,
+ // AddressValidator::GetRegionSubKeys will return an empty list of subkeys.
+ // Therefore, here, we can ignore the value of |success|.
+
+ // Check if there is any subkey request for that region code.
+ if (!pending_subkey_region_code_.compare(region_code))
+ pending_subkey_request_->OnRulesLoaded();
+ pending_subkey_region_code_.clear();
+ pending_subkey_request_.reset();
+}
+
+void SubKeyRequester::CancelPendingGetSubKeys() {
+ pending_subkey_region_code_.clear();
+ pending_subkey_request_.reset();
+}
+
+} // namespace payments
« no previous file with comments | « components/payments/core/subkey_requester.h ('k') | components/payments/core/subkey_requester_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698