Commit 1a0d61c7 authored by Mohamad Ahmadi's avatar Mohamad Ahmadi Committed by Commit Bot

[PR][AF] Creates an AddressNormalizerFactory class

This class is to be used in ChromeAutofillClientIOS and PaymentRequest for
lazy instantiation of the AddressNormalizerImpl

Bug: 788229
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ibe57b150e0aeea1777fab5e5a5aebecfd04fdfc3
Reviewed-on: https://chromium-review.googlesource.com/797735
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#521410}
parent 743dc24a
......@@ -5,6 +5,8 @@
source_set("autofill") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"address_normalizer_factory.cc",
"address_normalizer_factory.h",
"form_input_accessory_view.h",
"form_input_accessory_view.mm",
"form_input_accessory_view_controller.h",
......
// 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 "ios/chrome/browser/autofill/address_normalizer_factory.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/autofill/validation_rules_storage_factory.h"
#include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
namespace autofill {
namespace {
std::unique_ptr<::i18n::addressinput::Source> GetAddressInputSource(
net::URLRequestContextGetter* url_context_getter) {
return std::unique_ptr<::i18n::addressinput::Source>(
new autofill::ChromeMetadataSource(I18N_ADDRESS_VALIDATION_DATA_URL,
url_context_getter));
}
std::unique_ptr<::i18n::addressinput::Storage> GetAddressInputStorage() {
return autofill::ValidationRulesStorageFactory::CreateStorage();
}
} // namespace
// static
AddressNormalizer* AddressNormalizerFactory::GetInstance() {
static base::LazyInstance<AddressNormalizerFactory>::DestructorAtExit
instance = LAZY_INSTANCE_INITIALIZER;
return &(instance.Get().address_normalizer_);
}
AddressNormalizerFactory::AddressNormalizerFactory()
: address_normalizer_(
GetAddressInputSource(
GetApplicationContext()->GetSystemURLRequestContext()),
GetAddressInputStorage(),
GetApplicationContext()->GetApplicationLocale()) {}
AddressNormalizerFactory::~AddressNormalizerFactory() {}
} // namespace autofill
// 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 IOS_CHROME_BROWSER_AUTOFILL_ADDRESS_NORMALIZER_FACTORY_H_
#define IOS_CHROME_BROWSER_AUTOFILL_ADDRESS_NORMALIZER_FACTORY_H_
#include "base/lazy_instance.h"
#include "base/macros.h"
#include "components/autofill/core/browser/address_normalizer_impl.h"
namespace autofill {
// Singleton that owns a single AddressNormalizerImpl instance.
class AddressNormalizerFactory {
public:
static AddressNormalizer* GetInstance();
private:
friend struct base::LazyInstanceTraitsBase<AddressNormalizerFactory>;
AddressNormalizerFactory();
~AddressNormalizerFactory();
// The only instance that exists.
AddressNormalizerImpl address_normalizer_;
DISALLOW_COPY_AND_ASSIGN(AddressNormalizerFactory);
};
} // namespace autofill
#endif // IOS_CHROME_BROWSER_AUTOFILL_ADDRESS_NORMALIZER_FACTORY_H_
......@@ -381,11 +381,9 @@ class PaymentRequest : public PaymentOptionsProvider,
// created this PaymentRequest object.
__weak id<PaymentRequestUIDelegate> payment_request_ui_delegate_;
// The address normalizer to use for the duration of the Payment Request.
autofill::AddressNormalizerImpl address_normalizer_;
// Used to normalize the shipping address and the contact info.
autofill::AddressNormalizationManager address_normalization_manager_;
std::unique_ptr<autofill::AddressNormalizationManager>
address_normalization_manager_;
// The currency formatter instance for this PaymentRequest flow.
std::unique_ptr<CurrencyFormatter> currency_formatter_;
......
......@@ -28,6 +28,7 @@
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/autofill/address_normalizer_factory.h"
#include "ios/chrome/browser/autofill/validation_rules_storage_factory.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/payments/ios_payment_instrument.h"
......@@ -89,15 +90,6 @@ PaymentRequest::PaymentRequest(
web_state_(web_state),
personal_data_manager_(personal_data_manager),
payment_request_ui_delegate_(payment_request_ui_delegate),
// TODO(crbug.com/788229): Use a factory for the AddressNormalizer.
address_normalizer_(
GetAddressInputSource(
GetApplicationContext()->GetSystemURLRequestContext()),
GetAddressInputStorage(),
GetApplicationContext()->GetApplicationLocale()),
address_normalization_manager_(
&address_normalizer_,
GetApplicationContext()->GetApplicationLocale()),
selected_shipping_profile_(nullptr),
selected_contact_profile_(nullptr),
selected_payment_method_(nullptr),
......@@ -179,7 +171,7 @@ void PaymentRequest::DoFullCardRequest(
}
autofill::AddressNormalizer* PaymentRequest::GetAddressNormalizer() {
return &address_normalizer_;
return autofill::AddressNormalizerFactory::GetInstance();
}
autofill::RegionDataLoader* PaymentRequest::GetRegionDataLoader() {
......@@ -281,7 +273,13 @@ CurrencyFormatter* PaymentRequest::GetOrCreateCurrencyFormatter() {
autofill::AddressNormalizationManager*
PaymentRequest::GetAddressNormalizationManager() {
return &address_normalization_manager_;
if (!address_normalization_manager_) {
address_normalization_manager_ =
std::make_unique<autofill::AddressNormalizationManager>(
GetAddressNormalizer(),
GetApplicationContext()->GetApplicationLocale());
}
return address_normalization_manager_.get();
}
autofill::AutofillProfile* PaymentRequest::AddAutofillProfile(
......
......@@ -7,12 +7,14 @@
#include <utility>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/memory/ptr_util.h"
#include "components/autofill/core/browser/autofill_credit_card_filling_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/autofill_save_card_infobar_delegate_mobile.h"
#include "components/autofill/core/browser/autofill_save_card_infobar_mobile.h"
#include "components/autofill/core/browser/ui/card_unmask_prompt_view.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
......@@ -21,6 +23,7 @@
#include "components/prefs/pref_service.h"
#include "google_apis/gaia/identity_provider.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/autofill/address_normalizer_factory.h"
#include "ios/chrome/browser/autofill/personal_data_manager_factory.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/infobars/infobar_utils.h"
......@@ -65,7 +68,8 @@ ukm::UkmRecorder* ChromeAutofillClientIOS::GetUkmRecorder() {
}
AddressNormalizer* ChromeAutofillClientIOS::GetAddressNormalizer() {
// TODO(crbug.com/788229): Supply an AddressNormalizer instance.
if (base::FeatureList::IsEnabled(features::kAutofillAddressNormalizer))
return AddressNormalizerFactory::GetInstance();
return nullptr;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment