File: feature_promo_specification.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (677 lines) | stat: -rw-r--r-- 25,112 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// 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 "components/user_education/common/feature_promo/feature_promo_specification.h"

#include <string>
#include <variant>

#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/functional/callback_forward.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_education/common/feature_promo/feature_promo_precondition.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/l10n/l10n_util.h"

namespace user_education {

namespace {

// This function provides the list of allowed legal promos.
// It is not to be modified except by the Frizzle team.
bool IsAllowedLegalNotice(const base::Feature& promo_feature) {
  // Add the text names of allowlisted critical promos here:
  // static constexpr auto kAllowedPromoNames =
  //     base::MakeFixedFlatSet<std::string_view>({ });
  // return kAllowedPromoNames.contains(promo_feature.name);
  return false;
}

bool IsAllowedActionableAlert(const base::Feature& promo_feature) {
  // Add the text names of allowlisted actionable alerts here:
  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>({
          "IPH_DownloadEsbPromo",
          "IPH_HighEfficiencyMode",
          "IPH_SupervisedUserProfileSignin",
          "IPH_TabSearchToolbarButton",
      });
  return kAllowedPromoNames.contains(promo_feature.name);
}

bool IsAllowedKeyedNotice(const base::Feature& promo_feature) {
  // Add the text names of allowlisted keyed notices here:
  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>({
          "IPH_DesktopPWAsLinkCapturingLaunch",
          "IPH_DesktopPWAsLinkCapturingLaunchAppInTab",
          "IPH_ExplicitBrowserSigninPreferenceRemembered",
          "IPH_SignoutWebIntercept",
          "IPH_PwaQuietNotification",
      });
  return kAllowedPromoNames.contains(promo_feature.name);
}

bool IsAllowedRotatingPromo(const base::Feature& promo_feature) {
  // Add the text names of allowlisted rotating promos here:
  // static constexpr auto kAllowedPromoNames =
  //     base::MakeFixedFlatSet<std::string_view>({ });
  // return kAllowedPromoNames.contains(promo_feature.name);
  return false;
}

bool IsAllowedCustomUiPromo(const base::Feature& promo_feature) {
  // Test-only features are allowed.
  if (std::string(promo_feature.name).starts_with("TEST_")) {
    return true;
  }

  // IMPORTANT NOTE: Because Custom UI promos can potentially violate
  // best-practice rules, be sure to include a link to a screenshot or mock of
  // your UI with a description of how it works in your CL in addition to
  // requesting an exception here.
  //
  // Or better yet, reach out to Frizzle (User Education) Team before adding
  // code for your new Custom UI promo.
  //
  // Add the text names of allowlisted rotating promos here:
  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>(
          {"IPH_ExtensionsZeroStatePromo"});
  return kAllowedPromoNames.contains(promo_feature.name);
}

bool IsAllowedLegacyPromo(const base::Feature& promo_feature) {
  // NOTE: LEGACY PROMOS ARE DEPRECATED.
  // NO NEW ITEMS SHOULD BE ADDED TO THIS LIST, EVER.
  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>({
          "IPH_AutofillExternalAccountProfileSuggestion",
          "IPH_AutofillVirtualCardSuggestion",
          "IPH_DesktopPwaInstall",
          "IPH_DesktopSharedHighlighting",
          "IPH_GMCCastStartStop",
          "IPH_PriceTrackingInSidePanel",
          "IPH_ReadingListDiscovery",
          "IPH_ReadingListInSidePanel",
          "IPH_TabSearch",
      });
  return kAllowedPromoNames.contains(promo_feature.name);
}

bool IsAllowedToastWithoutScreenreaderText(const base::Feature& promo_feature) {
  // Features used for tests have this prefix and are excluded.
  if (std::string_view(promo_feature.name).starts_with("TEST_")) {
    return true;
  }

  // Some toasts are purely informational and their normal text also works for
  // low-vision users. This is a very small percentage of toasts, and so only
  // specific such promos are allowlisted.
  //
  // TODO(dfried): Merge legacy promos into this category, eliminating the entry
  // point and promo type entirely.

  // TODO(crbug.com/421471598): Remove this exemption once we have a separate
  // string for screenreader.
  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>({"IPH_TabSearchToolbarButton"});
  return kAllowedPromoNames.contains(promo_feature.name);
}

bool IsAllowedPreconditionExemption(const base::Feature& promo_feature) {
  // Features used for tests have this prefix and are excluded.
  if (std::string_view(promo_feature.name).starts_with("TEST_")) {
    return true;
  }

  static constexpr auto kAllowedPromoNames =
      base::MakeFixedFlatSet<std::string_view>({"IPH_AutofillAiOptIn"});
  return kAllowedPromoNames.contains(promo_feature.name);
}

// Common check logic for gating reshow-ability of promos. Generates an error if
// `subtype` is not allowed to reshow.
void CheckReshowAllowedFor(FeaturePromoSpecification::PromoSubtype subtype) {
  CHECK(subtype == FeaturePromoSpecification::PromoSubtype::kKeyedNotice ||
        subtype == FeaturePromoSpecification::PromoSubtype::kLegalNotice)
      << "Reshow only allowed for certain promo subtypes; subtype was "
      << subtype;
}

// Gets the minimum allowed reshow delay for specific promo types and
// `subtypes`.
//
// Note that currently, the subtype parameter does not affect the delay; it is
// here for sanity checking and so we can tune policy later if necessary.
base::TimeDelta GetMinReshowDelay(
    FeaturePromoSpecification::PromoType type,
    FeaturePromoSpecification::PromoSubtype subtype) {
  CheckReshowAllowedFor(subtype);
  if (type == FeaturePromoSpecification::PromoType::kToast) {
    return base::Days(14);
  } else {
    return base::Days(90);
  }
}

}  // namespace

FeaturePromoSpecification::AdditionalConditions::AdditionalConditions() =
    default;
FeaturePromoSpecification::AdditionalConditions::AdditionalConditions(
    AdditionalConditions&&) noexcept = default;
FeaturePromoSpecification::AdditionalConditions&
FeaturePromoSpecification::AdditionalConditions::operator=(
    AdditionalConditions&&) noexcept = default;
FeaturePromoSpecification::AdditionalConditions::~AdditionalConditions() =
    default;

FeaturePromoSpecification::AdditionalConditions&
FeaturePromoSpecification::AdditionalConditions::AddAdditionalCondition(
    const AdditionalCondition& additional_condition) {
  additional_conditions_.emplace_back(additional_condition);
  return *this;
}

void FeaturePromoSpecification::AdditionalConditions::AddAdditionalCondition(
    const char* event_name,
    Constraint constraint,
    uint32_t count,
    std::optional<uint32_t> in_days) {
  AddAdditionalCondition({event_name, constraint, count, in_days});
}

FeaturePromoSpecification::AcceleratorInfo::AcceleratorInfo() = default;
FeaturePromoSpecification::AcceleratorInfo::AcceleratorInfo(
    const AcceleratorInfo& other) = default;
FeaturePromoSpecification::AcceleratorInfo::AcceleratorInfo(ValueType value)
    : value_(value) {}
FeaturePromoSpecification::AcceleratorInfo&
FeaturePromoSpecification::AcceleratorInfo::operator=(
    const AcceleratorInfo& other) = default;
FeaturePromoSpecification::AcceleratorInfo::~AcceleratorInfo() = default;

FeaturePromoSpecification::AcceleratorInfo&
FeaturePromoSpecification::AcceleratorInfo::operator=(ValueType value) {
  value_ = value;
  return *this;
}

FeaturePromoSpecification::AcceleratorInfo::operator bool() const {
  return std::holds_alternative<ui::Accelerator>(value_) ||
         std::get<int>(value_);
}

ui::Accelerator FeaturePromoSpecification::AcceleratorInfo::GetAccelerator(
    const ui::AcceleratorProvider* provider) const {
  if (std::holds_alternative<ui::Accelerator>(value_)) {
    return std::get<ui::Accelerator>(value_);
  }

  const int command_id = std::get<int>(value_);
  DCHECK_GT(command_id, 0);

  ui::Accelerator result;
  DCHECK(provider->GetAcceleratorForCommandId(command_id, &result));
  return result;
}

FeaturePromoSpecification::RotatingPromos::RotatingPromos() = default;
FeaturePromoSpecification::RotatingPromos::RotatingPromos(
    RotatingPromos&&) noexcept = default;
FeaturePromoSpecification::RotatingPromos&
FeaturePromoSpecification::RotatingPromos::operator=(
    RotatingPromos&&) noexcept = default;
FeaturePromoSpecification::RotatingPromos::~RotatingPromos() = default;

FeaturePromoSpecification::BuildHelpBubbleParams::BuildHelpBubbleParams() =
    default;
FeaturePromoSpecification::BuildHelpBubbleParams::BuildHelpBubbleParams(
    const BuildHelpBubbleParams&) = default;
FeaturePromoSpecification::BuildHelpBubbleParams::BuildHelpBubbleParams(
    BuildHelpBubbleParams&&) noexcept = default;
FeaturePromoSpecification::BuildHelpBubbleParams&
FeaturePromoSpecification::BuildHelpBubbleParams::operator=(
    const BuildHelpBubbleParams&) = default;
FeaturePromoSpecification::BuildHelpBubbleParams&
FeaturePromoSpecification::BuildHelpBubbleParams::operator=(
    BuildHelpBubbleParams&&) noexcept = default;
FeaturePromoSpecification::BuildHelpBubbleParams::~BuildHelpBubbleParams() =
    default;

// static
constexpr HelpBubbleArrow FeaturePromoSpecification::kDefaultBubbleArrow;

FeaturePromoSpecification::FeaturePromoSpecification() = default;

FeaturePromoSpecification::FeaturePromoSpecification(
    FeaturePromoSpecification&& other) noexcept = default;

FeaturePromoSpecification::FeaturePromoSpecification(
    const base::Feature* feature,
    PromoType promo_type,
    ui::ElementIdentifier anchor_element_id,
    int bubble_body_string_id)
    : AnchorElementProviderCommon(anchor_element_id),
      feature_(feature),
      promo_type_(promo_type),
      bubble_body_string_id_(bubble_body_string_id),
      custom_action_dismiss_string_id_(IDS_PROMO_DISMISS_BUTTON) {
  DCHECK_NE(promo_type, PromoType::kUnspecified);
  DCHECK(promo_type == PromoType::kCustomUi || bubble_body_string_id_ != 0);
}

FeaturePromoSpecification& FeaturePromoSpecification::operator=(
    FeaturePromoSpecification&& other) noexcept = default;

FeaturePromoSpecification::~FeaturePromoSpecification() = default;

std::u16string FeaturePromoSpecification::FormatString(
    int string_id,
    const FormatParameters& format_params) {
  if (!string_id) {
    CHECK(std::holds_alternative<NoSubstitution>(format_params));
    return std::u16string();
  }
  if (std::holds_alternative<NoSubstitution>(format_params)) {
    return l10n_util::GetStringUTF16(string_id);
  }
  if (const auto* substitutions =
          std::get_if<StringSubstitutions>(&format_params)) {
    return l10n_util::GetStringFUTF16(string_id, *substitutions, nullptr);
  }
  if (const std::u16string* str = std::get_if<std::u16string>(&format_params)) {
    return l10n_util::GetStringFUTF16(string_id, *str);
  }
  int number = std::get<int>(format_params);
  return l10n_util::GetPluralStringFUTF16(string_id, number);
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForToastPromo(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id,
    int accessible_text_string_id,
    AcceleratorInfo accessible_accelerator) {
  // In the vast majority of cases, separate screenreader text should be
  // included for toasts; this is strictly enforced.
  if (body_text_string_id == accessible_text_string_id ||
      accessible_text_string_id <= 0) {
    CHECK(IsAllowedToastWithoutScreenreaderText(feature))
        << "Because toasts are hard to notice and time out quickly, screen "
           "reader text associated with toasts should differ from the bubble "
           "text and either provide the accelerator to access the highlighted "
           "entry point for your feature, or at the very least provide a "
           "separate description of the screen element appropriate for "
           "keyboard "
           "and low-vision users.";
    if (accessible_text_string_id <= 0) {
      accessible_text_string_id = body_text_string_id;
    }
  }

  FeaturePromoSpecification spec(&feature, PromoType::kToast, anchor_element_id,
                                 body_text_string_id);
  spec.screen_reader_string_id_ = accessible_text_string_id;
  spec.screen_reader_accelerator_ = std::move(accessible_accelerator);
  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForSnoozePromo(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id) {
  return FeaturePromoSpecification(&feature, PromoType::kSnooze,
                                   anchor_element_id, body_text_string_id);
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForSnoozePromo(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id,
    int accessible_text_string_id,
    AcceleratorInfo accessible_accelerator) {
  // See `FeaturePromoSpecification::CreateForToastPromo()`.
  CHECK_NE(body_text_string_id, accessible_text_string_id);
  FeaturePromoSpecification spec(&feature, PromoType::kSnooze,
                                 anchor_element_id, body_text_string_id);
  spec.screen_reader_string_id_ = accessible_text_string_id;
  spec.screen_reader_accelerator_ = std::move(accessible_accelerator);
  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForTutorialPromo(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id,
    TutorialIdentifier tutorial_id) {
  FeaturePromoSpecification spec(&feature, PromoType::kTutorial,
                                 anchor_element_id, body_text_string_id);
  DCHECK(!tutorial_id.empty());
  spec.tutorial_id_ = tutorial_id;
  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForCustomAction(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id,
    int custom_action_string_id,
    CustomActionCallback custom_action_callback) {
  FeaturePromoSpecification spec(&feature, PromoType::kCustomAction,
                                 anchor_element_id, body_text_string_id);
  spec.custom_action_caption_ =
      l10n_util::GetStringUTF16(custom_action_string_id);
  spec.custom_action_callback_ = custom_action_callback;
  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForRotatingPromo(
    const base::Feature& feature,
    RotatingPromos rotating_promos) {
  CHECK(IsAllowedRotatingPromo(feature));
  CHECK_GT(rotating_promos.size(), 0U);
  FeaturePromoSpecification spec;
  spec.feature_ = &feature;
  spec.promo_type_ = PromoType::kRotating;

  // Check the rotating promos to ensure they're all normal promos.
  bool found_rotating_promo = false;
  for (const auto& promo : rotating_promos) {
    if (promo) {
      CHECK_EQ(PromoSubtype::kNormal, promo->promo_subtype())
          << "Rotating promo cannot contain promo of type "
          << promo->promo_type() << " and subtype " << promo->promo_subtype();
      CHECK_NE(PromoType::kLegacy, promo->promo_type())
          << "Rotating promo cannot contain promo of type Legacy";
      CHECK_NE(PromoType::kUnspecified, promo->promo_type())
          << "Rotating promo cannot contain promo of type Unspecified";
      found_rotating_promo = true;
    }
  }
  CHECK(found_rotating_promo);
  spec.rotating_promos_ = std::move(rotating_promos);

  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForCustomUi(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    WrappedCustomHelpBubbleFactoryCallback bubble_factory_callback,
    CustomActionCallback custom_action_callback) {
  CHECK(IsAllowedCustomUiPromo(feature));
  FeaturePromoSpecification spec(&feature, PromoType::kCustomUi,
                                 anchor_element_id, 0);
  spec.custom_ui_factory_callback_ = std::move(bubble_factory_callback);
  spec.custom_action_callback_ = std::move(custom_action_callback);
  return spec;
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForLegacyPromo(
    const base::Feature* feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id) {
  CHECK(!feature || IsAllowedLegacyPromo(*feature))
      << "Cannot create promo: " << feature->name
      << "\nNo new legacy promos may be created; use CreateForToastPromo() "
         "instead.";
  return FeaturePromoSpecification(feature, PromoType::kLegacy,
                                   anchor_element_id, body_text_string_id);
}

// static
FeaturePromoSpecification FeaturePromoSpecification::CreateForTesting(
    const base::Feature& feature,
    ui::ElementIdentifier anchor_element_id,
    int body_text_string_id,
    PromoType type,
    PromoSubtype subtype,
    CustomActionCallback custom_action_callback) {
  FeaturePromoSpecification result(&feature, type, anchor_element_id,
                                   body_text_string_id);
  result.set_promo_subtype_for_testing(subtype);  // IN-TEST
  CHECK_EQ(!custom_action_callback.is_null(), type == PromoType::kCustomAction);
  result.custom_action_callback_ = std::move(custom_action_callback);
  return result;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetBubbleTitleText(
    int title_text_string_id) {
  DCHECK_NE(promo_type_, PromoType::kUnspecified);
  bubble_title_string_id_ = title_text_string_id;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetBubbleIcon(
    const gfx::VectorIcon* bubble_icon) {
  DCHECK_NE(promo_type_, PromoType::kUnspecified);
  bubble_icon_ = bubble_icon;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetBubbleArrow(
    HelpBubbleArrow bubble_arrow) {
  bubble_arrow_ = bubble_arrow;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::OverrideFocusOnShow(
    bool focus_on_show) {
  focus_on_show_override_ = focus_on_show;
  for (auto& rotating_promo : rotating_promos_) {
    if (rotating_promo.has_value()) {
      rotating_promo->focus_on_show_override_ =
          rotating_promo->focus_on_show_override_.value_or(focus_on_show);
    }
  }
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetPromoSubtype(
    PromoSubtype promo_subtype) {
  CHECK_NE(promo_type_, PromoType::kUnspecified);
  CHECK_NE(promo_type_, PromoType::kRotating)
      << "Rotating is not compatible with other promo subtypes.";
  CHECK_NE(promo_type_, PromoType::kSnooze)
      << "Basic snooze is not compatible with other promo subtypes.";
  CHECK_EQ(promo_subtype_, PromoSubtype::kNormal)
      << "Promo subtype cannot be set multiple times.";
  switch (promo_subtype) {
    case PromoSubtype::kLegalNotice:
      CHECK(feature_);
      CHECK(IsAllowedLegalNotice(*feature_));
      break;
    case PromoSubtype::kActionableAlert:
      CHECK_EQ(promo_type_, PromoType::kCustomAction);
      CHECK(feature_);
      CHECK(IsAllowedActionableAlert(*feature_));
      break;
    case PromoSubtype::kKeyedNotice:
      CHECK(feature_);
      CHECK(IsAllowedKeyedNotice(*feature_));
      break;
    default:
      break;
  }
  promo_subtype_ = promo_subtype;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetReshowPolicy(
    base::TimeDelta reshow_delay,
    std::optional<int> max_show_count) {
  CheckReshowAllowedFor(promo_subtype_);
  CHECK_GE(reshow_delay, GetMinReshowDelay(promo_type_, promo_subtype_));
  CHECK(!max_show_count || max_show_count > 1);
  reshow_delay_ = reshow_delay;
  max_show_count_ = max_show_count;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetAnchorElementFilter(
    AnchorElementFilter anchor_element_filter) {
  set_anchor_element_filter(std::move(anchor_element_filter));
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetInAnyContext(
    bool in_any_context) {
  set_in_any_context(in_any_context);
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetAdditionalConditions(
    AdditionalConditions additional_conditions) {
  additional_conditions_ = std::move(additional_conditions);
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::AddPreconditionExemption(
    FeaturePromoPrecondition::Identifier exempt_precondition) {
  CHECK(IsAllowedPreconditionExemption(*feature_));
  exempt_preconditions_.insert(exempt_precondition);
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetMetadata(
    Metadata metadata) {
  metadata_ = std::move(metadata);
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetCustomActionIsDefault(
    bool custom_action_is_default) {
  DCHECK(!custom_action_callback_.is_null());
  custom_action_is_default_ = custom_action_is_default;
  return *this;
}

FeaturePromoSpecification&
FeaturePromoSpecification::SetCustomActionDismissText(
    int custom_action_dismiss_string_id) {
  DCHECK(promo_type_ == PromoType::kCustomAction);
  custom_action_dismiss_string_id_ = custom_action_dismiss_string_id;
  return *this;
}

FeaturePromoSpecification& FeaturePromoSpecification::SetHighlightedMenuItem(
    const ui::ElementIdentifier highlighted_menu_identifier) {
  highlighted_menu_identifier_ = highlighted_menu_identifier;
  return *this;
}

ui::TrackedElement* FeaturePromoSpecification::GetAnchorElement(
    ui::ElementContext context,
    std::optional<int> index) const {
  if (index) {
    CHECK_EQ(PromoType::kRotating, promo_type_);
    return rotating_promos_.at(*index)->GetAnchorElement(context, std::nullopt);
  } else {
    // Should not be called directly on a rotating promo.
    CHECK_NE(PromoType::kRotating, promo_type_);
    return AnchorElementProviderCommon::GetAnchorElement(context, index);
  }
}

int FeaturePromoSpecification::GetNextValidIndex(int starting_index) const {
  CHECK_EQ(PromoType::kRotating, promo_type_);
  int index = starting_index;
  while (!rotating_promos_.at(index).has_value()) {
    index = (index + 1) % rotating_promos_.size();
    CHECK_NE(index, starting_index)
        << "Wrapped around while looking for a valid rotating promo; this "
           "should have been caught during promo registration.";
  }
  return index;
}

// static
FeaturePromoSpecification
FeaturePromoSpecification::CreateRotatingPromoForTesting(
    const base::Feature& feature,
    RotatingPromos rotating_promos) {
  FeaturePromoSpecification spec;
  spec.feature_ = &feature;
  spec.promo_type_ = PromoType::kRotating;
  spec.rotating_promos_ = std::move(rotating_promos);
  return spec;
}

FeaturePromoSpecification::CustomHelpBubbleResult
FeaturePromoSpecification::BuildCustomHelpBubble(
    ui::ElementContext from_context,
    HelpBubbleArrow arrow,
    BuildHelpBubbleParams params) const {
  CHECK_EQ(PromoType::kCustomUi, promo_type_);
  return custom_ui_factory_callback_.Run(from_context, arrow,
                                         std::move(params));
}

std::ostream& operator<<(std::ostream& oss,
                         FeaturePromoSpecification::PromoType promo_type) {
  switch (promo_type) {
    case FeaturePromoSpecification::PromoType::kLegacy:
      oss << "kLegacy";
      break;
    case FeaturePromoSpecification::PromoType::kToast:
      oss << "kToast";
      break;
    case FeaturePromoSpecification::PromoType::kSnooze:
      oss << "kSnooze";
      break;
    case FeaturePromoSpecification::PromoType::kTutorial:
      oss << "kTutorial";
      break;
    case FeaturePromoSpecification::PromoType::kCustomAction:
      oss << "kCustomAction";
      break;
    case FeaturePromoSpecification::PromoType::kUnspecified:
      oss << "kUnspecified";
      break;
    case FeaturePromoSpecification::PromoType::kRotating:
      oss << "kRotating";
      break;
    case FeaturePromoSpecification::PromoType::kCustomUi:
      oss << "kCustomUi";
      break;
  }
  return oss;
}

std::ostream& operator<<(
    std::ostream& oss,
    FeaturePromoSpecification::PromoSubtype promo_subtype) {
  switch (promo_subtype) {
    case FeaturePromoSpecification::PromoSubtype::kNormal:
      oss << "kNormal";
      break;
    case FeaturePromoSpecification::PromoSubtype::kKeyedNotice:
      oss << "kKeyedNotice";
      break;
    case FeaturePromoSpecification::PromoSubtype::kLegalNotice:
      oss << "kLegalNotice";
      break;
    case FeaturePromoSpecification::PromoSubtype::kActionableAlert:
      oss << "kActionableAlert";
      break;
  }
  return oss;
}

}  // namespace user_education