blob: 977b5e0009b10286e4a89c5ec26daa7f545b1565 [file] [log] [blame]
// 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 <stddef.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
namespace autofill {
class AutofillProfile;
using AutofillProfileValidatorCallback =
// AutofillProfileValidator Loads Rules from the server and validates an
// autofill profile. For a given autofill profile, it will set the ValidityState
// of several fields in the profile such as country, administrative area, etc.
// See implementation for more details.
class AutofillProfileValidator : public autofill::LoadRulesListener {
// Takes ownership of |source| and |storage|.
std::unique_ptr<::i18n::addressinput::Source> source,
std::unique_ptr<::i18n::addressinput::Storage> storage);
~AutofillProfileValidator() override;
// If the rule corresponding to the |profile| is loaded, this validates the
// profile, synchronously. If it is not loaded yet, it sets up a
// task to validate the profile when the rule is loaded (asynchronous). If the
// loading has not yet started, it will also start loading the rules.
void StartProfileValidation(AutofillProfile* profile,
AutofillProfileValidatorCallback cb);
// ValidationRequest loads Rules from the server and validates various fields
// in an autofill profile.
class ValidationRequest {
ValidationRequest(base::WeakPtr<AutofillProfile> profile,
AddressValidator* validator,
AutofillProfileValidatorCallback on_validated);
// Validates various fields of the |profile_|, and calls |on_validated_|.
void OnRulesLoaded();
base::WeakPtr<AutofillProfile> profile_;
// Not owned. Outlives this object.
AddressValidator* validator_;
AutofillProfileValidatorCallback on_validated_;
bool has_responded_ = false;
base::CancelableCallback<void()> on_timeout_;
friend class AutofillProfileValidatorTest;
// Returns whether the rules for the specified |region_code| is loaded.
bool AreRulesLoadedForRegion(const std::string& region_code);
// Starts loading the rules for the specified |region_code|.
void LoadRulesForRegion(const std::string& region_code);
// Implementation of the LoadRulesListener interface. Called when the address
// rules for the |region_code| have finished loading.
void OnAddressValidationRulesLoaded(const std::string& region_code,
bool success) override;
// A map of the region code and the pending requests for that region code.
std::map<std::string, std::vector<std::unique_ptr<ValidationRequest>>>
// The address validator used to load rules.
AddressValidator address_validator_;
} // namespace autofill