File: plus_address_suggestion_generator.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 (363 lines) | stat: -rw-r--r-- 14,614 bytes parent folder | download | duplicates (5)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// 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 "components/plus_addresses/plus_address_suggestion_generator.h"

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

#include "base/check_deref.h"
#include "base/feature_list.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/data_model/transliterator.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/suggestions/suggestion.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/plus_addresses/features.h"
#include "components/plus_addresses/grit/plus_addresses_strings.h"
#include "components/plus_addresses/plus_address_allocator.h"
#include "components/plus_addresses/plus_address_service.h"
#include "components/plus_addresses/plus_address_types.h"
#include "components/plus_addresses/settings/plus_address_setting_service.h"
#include "net/http/http_status_code.h"
#include "ui/base/l10n/l10n_util.h"

namespace plus_addresses {

namespace {

using autofill::FormFieldData;
using autofill::PasswordFormClassification;
using autofill::Suggestion;
using autofill::SuggestionType;

bool IsPasswordFieldVisible(
    const autofill::FormData& focused_form,
    const PasswordFormClassification& form_classification) {
  if (!form_classification.password_field) {
    return false;
  }
  // This visibility check is far from perfect - for example, fields may still
  // be transparent, have tiny sizes, etc., and this will not pick up on it.
  const FormFieldData* const pw_field =
      focused_form.FindFieldByGlobalId(*form_classification.password_field);
  return pw_field && pw_field->is_visible();
}

// Returns `true` when we wish to offer plus address creation on a form with
// password manager classification `form_classification` and a focused field
// with id `focused_field_id`.
// If password manager did not recognize a username field or the username field
// is different from the focused field, this is always `true`. Otherwise,
// whether we offer plus address creation depends on the form type.
bool ShouldOfferPlusAddressCreationOnForm(
    const autofill::FormData& focused_form,
    const base::flat_map<autofill::FieldGlobalId, autofill::FieldTypeGroup>&
        form_field_type_groups,
    const PasswordFormClassification& form_classification,
    autofill::FieldGlobalId focused_field_id) {
  if ((!form_classification.username_field ||
       *form_classification.username_field != focused_field_id) &&
      base::FeatureList::IsEnabled(
          features::kPlusAddressOfferCreationOnAllNonUsernameFields)) {
    return true;
  }

  auto form_has_unexpected_field_types_for_login_form = [&]() {
    // This check can be eliminated once
    // `kPlusAddressOfferCreationOnAllNonUsernameFields` is launched.
    if (!form_classification.username_field) {
      // This should not normally happen - but if it does, we might as well
      // offer generation.
      return true;
    }
    const autofill::FormGlobalId focused_renderer_form_id =
        CHECK_DEREF(focused_form.FindFieldByGlobalId(focused_field_id))
            .renderer_form_id();
    if (!focused_renderer_form_id.renderer_id) {
      // If the focused field is part of an unowned form, then we do not
      // override the login form prediction - it seems likely that the unowned
      // form consists of unrelated fields.
      return false;
    }
    // If there are name or address fields in the form, there is a high chance
    // that PWM got the classification incorrect.
    static constexpr autofill::FieldTypeGroupSet
        kUnexpectedFieldTypeGroupsInLoginForm = {
            autofill::FieldTypeGroup::kName,
            autofill::FieldTypeGroup::kAddress};
    return std::ranges::any_of(
        focused_form.fields(), [&](const FormFieldData& field) {
          if (field.renderer_form_id() != focused_renderer_form_id) {
            // Usually, PWM-forms are not spread across multiple frames -
            // therefore disregard all form elements that come from a different
            // renderer form.
            return false;
          }
          auto it = form_field_type_groups.find(field.global_id());
          return it != form_field_type_groups.end() &&
                 kUnexpectedFieldTypeGroupsInLoginForm.contains(it->second);
        });
  };

  switch (form_classification.type) {
    case PasswordFormClassification::Type::kNoPasswordForm:
    case PasswordFormClassification::Type::kSignupForm:
      return true;
    case PasswordFormClassification::Type::kLoginForm:
      if (!IsPasswordFieldVisible(focused_form, form_classification)) {
        return true;
      }
      return form_has_unexpected_field_types_for_login_form();
    case PasswordFormClassification::Type::kChangePasswordForm:
    case PasswordFormClassification::Type::kResetPasswordForm:
      return false;
    case PasswordFormClassification::Type::kSingleUsernameForm:
      return true;
  }
  NOTREACHED();
}

// Returns a suggestion to fill an existing plus address.
Suggestion CreateFillPlusAddressSuggestion(std::u16string plus_address) {
  Suggestion suggestion = Suggestion(std::move(plus_address),
                                     SuggestionType::kFillExistingPlusAddress);
  if constexpr (!BUILDFLAG(IS_ANDROID)) {
    suggestion.labels = {{Suggestion::Text(l10n_util::GetStringUTF16(
        IDS_PLUS_ADDRESS_FILL_SUGGESTION_SECONDARY_TEXT))}};
  }
  suggestion.icon = Suggestion::Icon::kPlusAddress;
  return suggestion;
}

// Returns the labels for a create new plus address suggestion.
// `forwarding_address` is the email that traffic is forwarded to.
std::vector<std::vector<Suggestion::Text>> CreateLabelsForCreateSuggestion(
    bool has_accepted_notice) {
  // On Android, there are no labels since the Keyboard Accessory only allows
  // for single line chips.
  if constexpr (BUILDFLAG(IS_ANDROID)) {
    return {};
  }
  if (!has_accepted_notice) {
    return {};
  }

  // On iOS the `forwarding_address` is not shown due to size constraints.
  if constexpr (BUILDFLAG(IS_IOS)) {
    return {{Suggestion::Text(l10n_util::GetStringUTF16(
        IDS_PLUS_ADDRESS_CREATE_SUGGESTION_SECONDARY_TEXT))}};
  }

  return {{Suggestion::Text(l10n_util::GetStringUTF16(
      IDS_PLUS_ADDRESS_CREATE_SUGGESTION_SECONDARY_TEXT))}};
}

}  // namespace

PlusAddressSuggestionGenerator::PlusAddressSuggestionGenerator(
    const PlusAddressSettingService* setting_service,
    PlusAddressAllocator* allocator,
    url::Origin origin)
    : setting_service_(CHECK_DEREF(setting_service)),
      allocator_(CHECK_DEREF(allocator)),
      origin_(std::move(origin)) {}

PlusAddressSuggestionGenerator::~PlusAddressSuggestionGenerator() = default;

std::vector<autofill::Suggestion>
PlusAddressSuggestionGenerator::GetSuggestions(
    const std::vector<std::string>& affiliated_plus_addresses,
    bool is_creation_enabled,
    const autofill::FormData& focused_form,
    const autofill::FormFieldData& focused_field,
    const base::flat_map<autofill::FieldGlobalId, autofill::FieldTypeGroup>&
        form_field_type_groups,
    const autofill::PasswordFormClassification& focused_form_classification,
    autofill::AutofillSuggestionTriggerSource trigger_source) {
  using enum autofill::AutofillSuggestionTriggerSource;
  const std::u16string normalized_field_value =
      autofill::RemoveDiacriticsAndConvertToLowerCase(focused_field.value());

  // Generally, plus address suggestions are only available on empty fields. In
  // cases where the field was previously autofilled, plus address suggestions
  // should be among the single field suggestions offered and no prefix matching
  // should be applied.
  const bool is_field_empty_or_autofilled =
      normalized_field_value.empty() || focused_field.is_autofilled();

  if (affiliated_plus_addresses.empty()) {
    // Do not offer creation if disabled.
    if (!is_creation_enabled) {
      return {};
    }

    // Do not offer creation on non-empty fields and certain form types (e.g.
    // login forms).
    if (trigger_source != kManualFallbackPlusAddresses &&
        (!is_field_empty_or_autofilled ||
         !ShouldOfferPlusAddressCreationOnForm(
             focused_form, form_field_type_groups, focused_form_classification,
             focused_field.global_id()))) {
      return {};
    }

    return {CreateNewPlusAddressSuggestion()};
  }

  std::vector<Suggestion> suggestions;
  suggestions.reserve(affiliated_plus_addresses.size());
  for (const std::string& affiliated_plus_address : affiliated_plus_addresses) {
    std::u16string plus_address = base::UTF8ToUTF16(affiliated_plus_address);
    // Only suggest filling a plus address whose prefix matches the field's
    // value.
    if (trigger_source == kManualFallbackPlusAddresses ||
        is_field_empty_or_autofilled ||
        // Apply prefix matching if the field is not empty and not autofilled.
        plus_address.starts_with(normalized_field_value)) {
      suggestions.push_back(
          CreateFillPlusAddressSuggestion(std::move(plus_address)));
    }
  }
  return suggestions;
}

void PlusAddressSuggestionGenerator::RefreshPlusAddressForSuggestion(
    Suggestion& suggestion) {
  CHECK(IsInlineGenerationEnabled());
  suggestion =
      CreateNewPlusAddressInlineSuggestion(/*refreshed_suggestion=*/true);
}

// static
Suggestion PlusAddressSuggestionGenerator::GetManagePlusAddressSuggestion() {
  Suggestion suggestion(
      l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_MANAGE_PLUS_ADDRESSES_TEXT),
      SuggestionType::kManagePlusAddress);
  suggestion.icon = Suggestion::Icon::kGoogleMonochrome;
  return suggestion;
}

// static
Suggestion PlusAddressSuggestionGenerator::GetPlusAddressErrorSuggestion(
    const PlusAddressRequestError& error) {
  Suggestion suggestion(
      l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT),
      SuggestionType::kPlusAddressError);
  suggestion.icon = Suggestion::Icon::kError;

  // Refreshing does not make sense for a quota error, since those will persist
  // for a significant amount of time.
  Suggestion::PlusAddressPayload payload;
  payload.offer_refresh = !error.IsQuotaError();
  suggestion.payload = std::move(payload);

  // The label depends on the error type.
  std::u16string label_text;
  if (error.IsQuotaError()) {
    label_text =
        l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_RESERVE_QUOTA_ERROR_TEXT);
  } else if (error.IsTimeoutError()) {
    label_text =
        l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_RESERVE_TIMEOUT_ERROR_TEXT);
  } else {
    label_text =
        l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_RESERVE_GENERIC_ERROR_TEXT);
  }
  suggestion.labels = {{Suggestion::Text(std::move(label_text))}};
  return suggestion;
}

// static
void PlusAddressSuggestionGenerator::SetSuggestedPlusAddressForSuggestion(
    const PlusAddress& plus_address,
    autofill::Suggestion& suggestion) {
  suggestion.payload =
      Suggestion::PlusAddressPayload(base::UTF8ToUTF16(*plus_address));
  SetLoadingStateForSuggestion(/*is_loading=*/false, suggestion);
}

// static
void PlusAddressSuggestionGenerator::SetLoadingStateForSuggestion(
    bool is_loading,
    autofill::Suggestion& suggestion) {
  suggestion.is_loading = Suggestion::IsLoading(is_loading);
  suggestion.acceptability = is_loading
                                 ? Suggestion::Acceptability::kUnacceptable
                                 : Suggestion::Acceptability::kAcceptable;
  auto existing_payload =
      suggestion.GetPayload<Suggestion::PlusAddressPayload>();
  existing_payload.offer_refresh = !is_loading;
  suggestion.payload = std::move(existing_payload);
}

autofill::Suggestion
PlusAddressSuggestionGenerator::CreateNewPlusAddressSuggestion() {
  if (IsInlineGenerationEnabled()) {
    return CreateNewPlusAddressInlineSuggestion(/*refreshed_suggestion=*/false);
  }

  Suggestion suggestion(
      l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT),
      SuggestionType::kCreateNewPlusAddress);

  suggestion.labels =
      CreateLabelsForCreateSuggestion(setting_service_->GetHasAcceptedNotice());
  suggestion.icon = Suggestion::Icon::kPlusAddress;
  suggestion.feature_for_new_badge = &features::kPlusAddressesEnabled;
  suggestion.iph_metadata = Suggestion::IPHMetadata(
      &feature_engagement::kIPHPlusAddressCreateSuggestionFeature);
  return suggestion;
}

bool PlusAddressSuggestionGenerator::IsInlineGenerationEnabled() const {
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  if (!setting_service_->GetHasAcceptedNotice()) {
    return false;
  }
  return true;
#else
  return false;
#endif
}

autofill::Suggestion
PlusAddressSuggestionGenerator::CreateNewPlusAddressInlineSuggestion(
    bool refreshed_suggestion) {
  Suggestion suggestion(
      l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_SUGGESTION_MAIN_TEXT),
      SuggestionType::kCreateNewPlusAddressInline);

  PlusAddressAllocator::AllocationMode mode =
      refreshed_suggestion
          ? PlusAddressAllocator::AllocationMode::kNewPlusAddress
          : PlusAddressAllocator::AllocationMode::kAny;
  if (std::optional<PlusProfile> profile =
          allocator_->AllocatePlusAddressSynchronously(origin_, mode)) {
    SetSuggestedPlusAddressForSuggestion(profile->plus_address, suggestion);
    // Set IPH and new badge information only if allocation is synchronous.
    // Otherwise, they will be showing only during the loading stage and then be
    // hidden automatically.
    suggestion.feature_for_new_badge = &features::kPlusAddressesEnabled;
    suggestion.iph_metadata = Suggestion::IPHMetadata(
        &feature_engagement::kIPHPlusAddressCreateSuggestionFeature);
    suggestion.voice_over = l10n_util::GetStringFUTF16(
        IDS_PLUS_ADDRESS_CREATE_INLINE_SUGGESTION_A11Y_VOICE_OVER,
        base::UTF8ToUTF16(*profile->plus_address));
  } else {
    suggestion.payload = Suggestion::PlusAddressPayload();
    SetLoadingStateForSuggestion(/*is_loading=*/true, suggestion);
  }
  suggestion.icon = Suggestion::Icon::kPlusAddress;
  suggestion.labels =
      CreateLabelsForCreateSuggestion(setting_service_->GetHasAcceptedNotice());
  return suggestion;
}

}  // namespace plus_addresses