#include "components/autofill/core/browser/address_normalizer_impl.h"
#include <stddef.h>
#include <utility>
#include "base/cancelable_callback.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_data_util.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "components/autofill/core/browser/geo/phone_number_i18n.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"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "components/autofill/android/main_autofill_jni_headers/AddressNormalizer_jni.h"
#endif
namespace autofill {
namespace {
Source;
Storage;
DeleteOnTaskRunnerStorageUniquePtr;
bool NormalizeProfileWithValidator(AutofillProfile* profile,
const std::string& app_locale,
AddressValidator* address_validator) { … }
void FormatPhoneNumberToE164(AutofillProfile* profile,
const std::string& region_code,
const std::string& app_locale) { … }
std::unique_ptr<AddressValidator> CreateAddressValidator(
std::unique_ptr<Source> source,
DeleteOnTaskRunnerStorageUniquePtr storage,
LoadRulesListener* load_rules_listener) { … }
#if BUILDFLAG(IS_ANDROID)
void OnAddressNormalized(base::android::ScopedJavaGlobalRef<jobject> jdelegate,
const std::string& app_locale,
bool success,
const AutofillProfile& profile) {
JNIEnv* env = base::android::AttachCurrentThread();
if (success) {
Java_NormalizedAddressRequestDelegate_onAddressNormalized(
env, jdelegate, profile.CreateJavaObject(app_locale));
} else {
Java_NormalizedAddressRequestDelegate_onCouldNotNormalize(
env, jdelegate, profile.CreateJavaObject(app_locale));
}
}
#endif
}
class AddressNormalizerImpl::NormalizationRequest { … };
AddressNormalizerImpl::AddressNormalizerImpl(std::unique_ptr<Source> source,
std::unique_ptr<Storage> storage,
const std::string& app_locale)
: … { … }
AddressNormalizerImpl::~AddressNormalizerImpl() { … }
void AddressNormalizerImpl::LoadRulesForRegion(const std::string& region_code) { … }
void AddressNormalizerImpl::NormalizeAddressAsync(
const AutofillProfile& profile,
int timeout_seconds,
AddressNormalizer::NormalizationCallback callback) { … }
bool AddressNormalizerImpl::NormalizeAddressSync(AutofillProfile* profile) { … }
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject>
AddressNormalizerImpl::GetJavaObject() {
if (!java_ref_) {
java_ref_.Reset(
Java_AddressNormalizer_Constructor(base::android::AttachCurrentThread(),
reinterpret_cast<intptr_t>(this)));
}
return base::android::ScopedJavaLocalRef<jobject>(java_ref_);
}
void AddressNormalizerImpl::LoadRulesForAddressNormalization(
JNIEnv* env,
const base::android::JavaParamRef<jstring>& jregion_code) {
LoadRulesForRegion(base::android::ConvertJavaStringToUTF8(env, jregion_code));
}
void AddressNormalizerImpl::StartAddressNormalization(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jprofile,
jint jtimeout_seconds,
const base::android::JavaParamRef<jobject>& jdelegate) {
AutofillProfile profile = AutofillProfile::CreateFromJavaObject(
jprofile, nullptr, app_locale_);
NormalizeAddressAsync(
profile, jtimeout_seconds,
base::BindOnce(&OnAddressNormalized,
base::android::ScopedJavaGlobalRef<jobject>(jdelegate),
app_locale_));
}
#endif
bool AddressNormalizerImpl::AreRulesLoadedForRegion(
const std::string& region_code) { … }
void AddressNormalizerImpl::OnAddressValidationRulesLoaded(
const std::string& region_code,
bool success) { … }
void AddressNormalizerImpl::OnAddressValidatorCreated(
std::unique_ptr<AddressValidator> validator) { … }
void AddressNormalizerImpl::AddNormalizationRequestForRegion(
std::unique_ptr<NormalizationRequest> request,
const std::string& region_code) { … }
}