summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/passwords/password_bubble_experiment.cc
blob: 8afabfcaee87a94f2ed4c0429427e1781844b48c (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/passwords/password_bubble_experiment.h"

#include "base/metrics/field_trial.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/variations/variations_associated_data.h"

namespace password_bubble_experiment {
namespace {

// Return the number of consecutive times the user clicked "Not now" in the
// "Save password" bubble.
int GetCurrentNopesCount(PrefService* prefs) {
  return prefs->GetInteger(prefs::kPasswordBubbleNopesCount);
}

} // namespace

const char kExperimentName[] = "PasswordBubbleAlgorithm";
const char kParamNopeThreshold[] = "consecutive_nope_threshold";

void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterIntegerPref(
      prefs::kPasswordBubbleNopesCount,
      0,
      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}

bool ShouldShowNeverForThisSiteDefault(PrefService* prefs) {
  if (!base::FieldTrialList::TrialExists(kExperimentName))
    return false;
  std::string param =
      variations::GetVariationParamValue(kExperimentName, kParamNopeThreshold);

  int threshold = 0;
  return base::StringToInt(param, &threshold) &&
      GetCurrentNopesCount(prefs) >= threshold;
}

void RecordBubbleClosed(
    PrefService* prefs,
    password_manager::metrics_util::UIDismissalReason reason) {
  // Clicking "Never" doesn't change the experiment state.
  if (reason == password_manager::metrics_util::CLICKED_NEVER)
    return;
  int nopes_count = GetCurrentNopesCount(prefs);
  if (reason == password_manager::metrics_util::CLICKED_NOPE)
    nopes_count++;
  else
    nopes_count = 0;
  prefs->SetInteger(prefs::kPasswordBubbleNopesCount, nopes_count);
}

}  // namespace password_bubble_experiment