File: save_address_bubble_controller.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (227 lines) | stat: -rw-r--r-- 8,555 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2024 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/save_address_bubble_controller.h"

#include <array>
#include <optional>
#include <string>
#include <vector>

#include "base/check.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/ui/ui_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/autofill/content/browser/content_autofill_client.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/ui/addresses/autofill_address_util.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync/base/features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"

namespace autofill {

SaveAddressBubbleController::SaveAddressBubbleController(
    base::WeakPtr<AddressBubbleControllerDelegate> delegate,
    content::WebContents* web_contents,
    const AutofillProfile& address_profile,
    bool is_migration_to_account)
    : content::WebContentsObserver(web_contents),
      delegate_(delegate),
      address_profile_(address_profile),
      is_migration_to_account_(is_migration_to_account) {}

SaveAddressBubbleController::~SaveAddressBubbleController() = default;

std::u16string SaveAddressBubbleController::GetWindowTitle() const {
  return l10n_util::GetStringUTF16(
      is_migration_to_account_
          ? IDS_AUTOFILL_ACCOUNT_MIGRATE_ADDRESS_PROMPT_TITLE
          : IDS_AUTOFILL_SAVE_ADDRESS_PROMPT_TITLE);
}

std::optional<SaveAddressBubbleController::HeaderImages>
SaveAddressBubbleController::GetHeaderImages() const {
  if (is_migration_to_account_ && web_contents()) {
    std::optional<AccountInfo> account =
        GetPrimaryAccountInfoFromBrowserContext(
            web_contents()->GetBrowserContext());
    if (account) {
      // The position and size must match the implied one in the image,
      // so these numbers are exclusively for ..._AVATAR50_X135_Y54.
      static constexpr gfx::Point kAvatarPosition{135, 54};
      static constexpr size_t kAvatarSize{50};
      return HeaderImages{
          .light = profiles::EmbedAvatarOntoImage(
              IDR_MIGRATE_ADDRESS_AVATAR50_X135_Y54, account->account_image,
              kAvatarPosition, kAvatarSize),
          .dark = profiles::EmbedAvatarOntoImage(
              IDR_MIGRATE_ADDRESS_AVATAR50_X135_Y54_DARK,
              account->account_image, kAvatarPosition, kAvatarSize)};
    }
  }

  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
  return HeaderImages{.lottie = bundle.GetThemedLottieImageNamed(
                          IDR_AUTOFILL_SAVE_ADDRESS_LOTTIE)};
}

std::u16string SaveAddressBubbleController::GetBodyText() const {
  if (is_migration_to_account_ && web_contents()) {
    PersonalDataManager& pdm =
        ContentAutofillClient::FromWebContents(web_contents())
            ->GetPersonalDataManager();

    std::optional<AccountInfo> account =
        GetPrimaryAccountInfoFromBrowserContext(
            web_contents()->GetBrowserContext());

    const bool is_syncing =
        base::FeatureList::IsEnabled(syncer::kReplaceSyncPromosWithSignInPromos)
            ? pdm.address_data_manager().IsAutofillUserSelectableTypeEnabled()
            : pdm.address_data_manager().IsSyncFeatureEnabledForAutofill();
    int string_id = is_syncing
                        ? IDS_AUTOFILL_SYNCABLE_PROFILE_MIGRATION_PROMPT_NOTICE
                        : IDS_AUTOFILL_LOCAL_PROFILE_MIGRATION_PROMPT_NOTICE;

    return l10n_util::GetStringFUTF16(string_id,
                                      base::UTF8ToUTF16(account->email));
  }

  return {};
}

std::u16string SaveAddressBubbleController::GetAddressSummary() const {
  // Use a shorter version of the address summary for migration, it has
  // a fixed set of fields and doesn't depend on libaddressinput.
  if (is_migration_to_account_) {
    static constexpr std::array fields = {
        NAME_FULL, ADDRESS_HOME_LINE1, EMAIL_ADDRESS, PHONE_HOME_WHOLE_NUMBER};
    std::vector<std::u16string> values;
    for (FieldType field : fields) {
      std::u16string value =
          address_profile_.GetInfo(field, g_browser_process->GetFeatures()
                                              ->application_locale_storage()
                                              ->Get());
      if (!value.empty()) {
        values.push_back(value);
      }
    }
    if (values.empty()) {
      return {};
    }
    return base::JoinString(values, u"\n");
  }

  return GetEnvelopeStyleAddress(
      address_profile_,
      g_browser_process->GetFeatures()->application_locale_storage()->Get(),
      /*include_recipient=*/true, /*include_country=*/true);
}

std::u16string SaveAddressBubbleController::GetProfileEmail() const {
  // Email is not shown as a separate field in the migration flow,
  // it is included in the address summary, see GetAddressSummary().
  if (is_migration_to_account_) {
    return {};
  }

  return address_profile_.GetInfo(
      EMAIL_ADDRESS,
      g_browser_process->GetFeatures()->application_locale_storage()->Get());
}

std::u16string SaveAddressBubbleController::GetProfilePhone() const {
  // Phone is not shown as a separate field in the migration flow,
  // it is included in the address summary, see GetAddressSummary().
  if (is_migration_to_account_) {
    return {};
  }

  return address_profile_.GetInfo(
      PHONE_HOME_WHOLE_NUMBER,
      g_browser_process->GetFeatures()->application_locale_storage()->Get());
}

std::u16string SaveAddressBubbleController::GetOkButtonLabel() const {
  return l10n_util::GetStringUTF16(
      is_migration_to_account_
          ? IDS_AUTOFILL_MIGRATE_ADDRESS_DIALOG_OK_BUTTON_LABEL_SAVE
          : IDS_AUTOFILL_EDIT_ADDRESS_DIALOG_OK_BUTTON_LABEL_SAVE);
}

AutofillClient::AddressPromptUserDecision
SaveAddressBubbleController::GetCancelCallbackValue() const {
  // The migration prompt should not be shown again if the user explicitly
  // rejects it (for a particular address, due to legal and privacy
  // requirements). In other cases it is acceptable to show it a few times more.
  return is_migration_to_account_
             ? AutofillClient::AddressPromptUserDecision::kNever
             : AutofillClient::AddressPromptUserDecision::kDeclined;
}

std::u16string SaveAddressBubbleController::GetFooterMessage() const {
  if (address_profile_.IsAccountProfile() && web_contents()) {
    std::optional<AccountInfo> account =
        GetPrimaryAccountInfoFromBrowserContext(
            web_contents()->GetBrowserContext());

    return l10n_util::GetStringFUTF16(
        IDS_AUTOFILL_SAVE_IN_ACCOUNT_PROMPT_ADDRESS_SOURCE_NOTICE,
        base::UTF8ToUTF16(account->email));
  }

  return {};
}

std::u16string SaveAddressBubbleController::GetEditorFooterMessage() const {
  if (is_migration_to_account_ && web_contents()) {
    std::optional<AccountInfo> account =
        GetPrimaryAccountInfoFromBrowserContext(
            web_contents()->GetBrowserContext());
    return l10n_util::GetStringFUTF16(
        IDS_AUTOFILL_SAVE_IN_ACCOUNT_PROMPT_ADDRESS_SOURCE_NOTICE,
        base::UTF8ToUTF16(account->email));
  }

  return GetFooterMessage();
}

void SaveAddressBubbleController::OnUserDecision(
    AutofillClient::AddressPromptUserDecision decision,
    base::optional_ref<const AutofillProfile> profile) {
  if (delegate_) {
    delegate_->OnUserDecision(decision, profile);
  }
}

void SaveAddressBubbleController::OnEditButtonClicked() {
  if (delegate_) {
    delegate_->ShowEditor(address_profile_, /*title_override=*/u"",
                          GetEditorFooterMessage(),
                          /*is_editing_existing_address=*/false);
  }
}

void SaveAddressBubbleController::OnBubbleClosed() {
  if (delegate_) {
    delegate_->OnBubbleClosed();
  }
}

}  // namespace autofill