File: address_editor_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (160 lines) | stat: -rw-r--r-- 6,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/autofill/address_editor_controller.h"

#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/callback_list.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/autofill/core/browser/country_type.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/geo/address_i18n.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/autofill/core/browser/ui/addresses/autofill_address_util.h"
#include "components/variations/service/variations_service.h"
#include "third_party/libaddressinput/chromium/chrome_metadata_source.h"
#include "third_party/libaddressinput/messages.h"
#include "ui/base/l10n/l10n_util.h"

namespace autofill {

AddressEditorController::AddressEditorController(
    const AutofillProfile& profile_to_edit,
    PersonalDataManager* pdm,
    bool is_validatable)
    : profile_to_edit_(profile_to_edit),
      pdm_(*pdm),
      locale_(g_browser_process->GetFeatures()
                  ->application_locale_storage()
                  ->Get()),
      is_validatable_(is_validatable) {
  const variations::VariationsService* variations_service =
      g_browser_process->variations_service();
  countries_.SetCountries(
      GeoIpCountryCode(variations_service
                           ? variations_service->GetLatestCountry()
                           : std::string()),
      locale_);
  std::u16string profile_country_code =
      profile_to_edit_.GetRawInfo(ADDRESS_HOME_COUNTRY);
  UpdateEditorFields(base::UTF16ToASCII(profile_country_code));
}

AddressEditorController::~AddressEditorController() = default;

CountryComboboxModel& AddressEditorController::GetCountryComboboxModel() {
  return countries_;
}

void AddressEditorController::UpdateEditorFields(
    const std::string& country_code) {
  editor_fields_.clear();

  std::vector<std::vector<AutofillAddressUIComponent>> components;
  GetAddressComponents(country_code, locale_,
                       /*include_literals=*/false, &components,
                       &language_code_);
  profile_to_edit_.set_language_code(language_code_);

  // Insert the Country combobox at the top.
  editor_fields_.emplace_back(
      ADDRESS_HOME_COUNTRY,
      l10n_util::GetStringUTF16(IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL),
      EditorField::LengthHint::HINT_LONG, /*is_required=*/false,
      EditorField::ControlType::COMBOBOX);

  for (const std::vector<AutofillAddressUIComponent>& line : components) {
    for (const AutofillAddressUIComponent& component : line) {
      EditorField::LengthHint length_hint =
          component.length_hint == AutofillAddressUIComponent::HINT_LONG
              ? EditorField::LengthHint::HINT_LONG
              : EditorField::LengthHint::HINT_SHORT;
      EditorField::ControlType control_type =
          component.field == ADDRESS_HOME_COUNTRY
              ? EditorField::ControlType::COMBOBOX
              : EditorField::ControlType::TEXTFIELD;

      editor_fields_.emplace_back(
          component.field, base::UTF8ToUTF16(component.name), length_hint,
          component.is_required, control_type);
    }
  }
  // Always add phone number and email at the end.
  editor_fields_.emplace_back(
      PHONE_HOME_WHOLE_NUMBER,
      l10n_util::GetStringUTF16(IDS_SETTINGS_AUTOFILL_ADDRESSES_PHONE),
      EditorField::LengthHint::HINT_SHORT, /*is_required=*/false,
      EditorField::ControlType::TEXTFIELD_NUMBER);

  editor_fields_.emplace_back(
      EMAIL_ADDRESS,
      l10n_util::GetStringUTF16(IDS_SETTINGS_AUTOFILL_ADDRESSES_EMAIL),
      EditorField::LengthHint::HINT_LONG, /*is_required=*/false,
      EditorField::ControlType::TEXTFIELD);
}

void AddressEditorController::SetProfileInfo(FieldType type,
                                             const std::u16string& value) {
  // Since the countries combobox contains the country names, not the country
  // codes, and hence we should use `SetInfo()` to make sure they get converted
  // to country codes. Also use `SetInfo()` for `NAME_FULL` so that its
  // dependent nodes (NAME_FIRST, NAME_LAST, etc) are also updated. This does
  // not need to be done for addresses because it is handled internally inside
  // `SetRawInfo()`.
  if (type == ADDRESS_HOME_COUNTRY || type == NAME_FULL) {
    profile_to_edit_.SetInfoWithVerificationStatus(
        type, value, locale_, VerificationStatus::kUserVerified);
    return;
  }

  profile_to_edit_.SetRawInfoWithVerificationStatus(
      type, value, VerificationStatus::kUserVerified);
}

std::u16string AddressEditorController::GetProfileInfo(FieldType type) {
  // TDOD(mamir): Update the implementation to format strings properly.
  return profile_to_edit_.GetInfo(type, locale_);
}

const AutofillProfile& AddressEditorController::GetAddressProfile() {
  return profile_to_edit_;
}

void AddressEditorController::SetIsValid(bool is_valid) {
  bool should_notify = is_valid_ != is_valid;
  is_valid_ = is_valid;
  if (should_notify) {
    on_is_valid_change_callbacks_.Notify(is_valid);
  }
}

base::CallbackListSubscription
AddressEditorController::AddIsValidChangedCallback(
    OnIsValidChangeCallbackList::CallbackType callback) {
  return on_is_valid_change_callbacks_.Add(std::move(callback));
}

bool AddressEditorController::IsValid(const EditorField& field,
                                      std::u16string_view value) {
  if (is_validatable_ && field.is_required &&
      base::CollapseWhitespace(value, true).empty()) {
    return false;
  }

  return true;
}

}  // namespace autofill