scoped_new_badge_tracker_base.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/user_education/common/scoped_new_badge_tracker_base.h"
  5. #include "base/containers/contains.h"
  6. #include "base/feature_list.h"
  7. #include "components/feature_engagement/public/tracker.h"
  8. namespace user_education {
  9. ScopedNewBadgeTrackerBase::ScopedNewBadgeTrackerBase(
  10. feature_engagement::Tracker* tracker)
  11. : tracker_(tracker) {}
  12. // TODO(crbug.com/1258216): When we have the ability to do concurrent FE promos,
  13. // dismiss all of the badge promos here instead of in TryShowNewBadge().
  14. ScopedNewBadgeTrackerBase::~ScopedNewBadgeTrackerBase() = default;
  15. bool ScopedNewBadgeTrackerBase::TryShowNewBadge(
  16. const base::Feature& badge_feature,
  17. const base::Feature* promoted_feature) {
  18. // In the event of a submenu that the user could open multiple times while
  19. // navigating the same top-level menu, and we don't want to count those as
  20. // separate times the user sees the New Badge:
  21. if (base::Contains(active_badge_features_, &badge_feature))
  22. return true;
  23. // If there is no tracker available or the feature being promoted is disabled,
  24. // do not show the New Badge.
  25. if (!tracker_)
  26. return false;
  27. if (promoted_feature && !base::FeatureList::IsEnabled(*promoted_feature))
  28. return false;
  29. const bool result = tracker_->ShouldTriggerHelpUI(badge_feature);
  30. if (result) {
  31. active_badge_features_.insert(&badge_feature);
  32. // TODO(crbug.com/1258216): Immediately dismiss to work around an issue
  33. // where the FE backend disallows concurrent promos; move the call to
  34. // Dismiss() to the destructor when concurrency is added.
  35. //
  36. // Note that "Dismiss" in this case does not dismiss the UI. It's telling
  37. // the FE backend that the promo is done so that other promos can run. A
  38. // badge showing in a menu should not block e.g. other badges from
  39. // displaying (never mind help bubbles).
  40. tracker_->Dismissed(badge_feature);
  41. }
  42. return result;
  43. }
  44. void ScopedNewBadgeTrackerBase::ActionPerformed(const char* event_name) {
  45. if (tracker_)
  46. tracker_->NotifyEvent(event_name);
  47. }
  48. } // namespace user_education