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

ash / wm / overview / birch / birch_privacy_nudge_controller.cc [blame]

// 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 "ash/wm/overview/birch/birch_privacy_nudge_controller.h"

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/system/anchored_nudge_data.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "base/command_line.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

namespace {

// Maximum number of times to show the nudge.
constexpr int kMaxShownCount = 3;

// Minimum time between shows.
constexpr base::TimeDelta kTimeBetweenShown = base::Hours(24);

constexpr const char* const kNudgeId = "BirchPrivacyId";

PrefService* GetPrefService() {
  return Shell::Get()->session_controller()->GetActivePrefService();
}

}  // namespace

BirchPrivacyNudgeController::BirchPrivacyNudgeController() = default;

BirchPrivacyNudgeController::~BirchPrivacyNudgeController() = default;

// static
void BirchPrivacyNudgeController::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(prefs::kBirchContextMenuShown, false);
  registry->RegisterIntegerPref(prefs::kBirchPrivacyNudgeShownCount, 0);
  registry->RegisterTimePref(prefs::kBirchPrivacyNudgeLastShownTime,
                             base::Time());
}

// static
void BirchPrivacyNudgeController::DidShowContextMenu() {
  GetPrefService()->SetBoolean(prefs::kBirchContextMenuShown, true);
}

void BirchPrivacyNudgeController::MaybeShowNudge(views::View* anchor_view) {
  auto* prefs = GetPrefService();

  // Don't show nudge if the user has already opened the context menu.
  if (prefs->GetBoolean(prefs::kBirchContextMenuShown)) {
    return;
  }

  // Nudge has been shown three times. No need to educate anymore.
  const int shown_count =
      prefs->GetInteger(prefs::kBirchPrivacyNudgeShownCount);
  if (shown_count >= kMaxShownCount) {
    return;
  }

  // Don't show nudge if it was shown within the last 24 hours.
  const base::Time last_shown =
      prefs->GetTime(prefs::kBirchPrivacyNudgeLastShownTime);
  if (base::Time::Now() - last_shown < kTimeBetweenShown) {
    return;
  }

  AnchoredNudgeData nudge_data(
      kNudgeId, NudgeCatalogName::kBirchPrivacy,
      l10n_util::GetStringUTF16(IDS_ASH_BIRCH_PRIVACY_NUDGE), anchor_view);
  nudge_data.arrow = views::BubbleBorder::BOTTOM_LEFT;

  Shell::Get()->anchored_nudge_manager()->Show(nudge_data);

  // Update nudge prefs.
  prefs->SetInteger(prefs::kBirchPrivacyNudgeShownCount, shown_count + 1);
  prefs->SetTime(prefs::kBirchPrivacyNudgeLastShownTime, base::Time::Now());
}

}  // namespace ash