File: new_badge_controller.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (78 lines) | stat: -rw-r--r-- 3,361 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef COMPONENTS_USER_EDUCATION_COMMON_NEW_BADGE_NEW_BADGE_CONTROLLER_H_
#define COMPONENTS_USER_EDUCATION_COMMON_NEW_BADGE_NEW_BADGE_CONTROLLER_H_

#include <memory>

#include "base/auto_reset.h"
#include "base/feature_list.h"
#include "base/memory/raw_ref.h"
#include "components/user_education/common/feature_promo/feature_promo_registry.h"
#include "components/user_education/common/new_badge/new_badge_policy.h"
#include "components/user_education/common/user_education_data.h"
#include "components/user_education/common/user_education_storage_service.h"
#include "ui/menus/simple_menu_model.h"

namespace user_education {

using DisplayNewBadge = ui::IsNewFeatureAtValue;

// Controls display of "New" Badge based on approved parameters.
class NewBadgeController {
 public:
  NewBadgeController(NewBadgeRegistry& registry,
                     UserEducationStorageService& storage_service,
                     std::unique_ptr<NewBadgePolicy> policy);
  NewBadgeController(const NewBadgeController&) = delete;
  void operator=(const NewBadgeController&) = delete;
  virtual ~NewBadgeController();

  // Called after registration of "New" Badges to ensure that all data is
  // consistent.
  void InitData();

  // Call when a UI element that could have a "New" Badge will be shown to the
  // user. Returns true if the badge should be shown. Note that successfully
  // calling this method a number of times will permanently disable the badge,
  // so do not call this method unless the badge will actually be displayed.
  DisplayNewBadge MaybeShowNewBadge(const base::Feature& feature);

  // Notifies that the `feature` associated with the badge has been shown. After
  // a certain (but low) number of uses, the badge will disappear. Fails if
  // there is no new badge registered for this feature.
  void NotifyFeatureUsed(const base::Feature& feature);

  // As NotifyFeatureUsed, but if there is no new badge registered for the given
  // feature or it is not enabled, does not generate an error.
  void NotifyFeatureUsedIfValid(const base::Feature& feature);

  // Disables "New" Badges for tests - specifically pixel tests, where the
  // presence of a badge could disrupt the expected image.
  //
  // Be sure to store the resulting lock and then release it at the end of the
  // test; badges are only disabled while the returned object is alive.
  using TestLock = std::unique_ptr<base::AutoReset<bool>>;
  [[nodiscard]] static TestLock DisableNewBadgesForTesting();

 private:
  void NotifyFeatureUsedImpl(const base::Feature& feature,
                             bool allow_not_registered);

  // Checks that the `feature` is enabled and has a registered "New" Badge.
  // If the feature is not registered and `allow_not_registered` is false, will
  // generate an error.
  bool CheckPrerequisites(const base::Feature& feature,
                          bool allow_not_registered) const;

  const raw_ref<NewBadgeRegistry> registry_;
  const raw_ref<UserEducationStorageService> storage_service_;
  const std::unique_ptr<NewBadgePolicy> policy_;
  static bool disable_new_badges_;
};

}  // namespace user_education

#endif  // COMPONENTS_USER_EDUCATION_COMMON_NEW_BADGE_NEW_BADGE_CONTROLLER_H_