summaryrefslogtreecommitdiff
path: root/chromium/components/signin/core/browser/profile_management_switches.cc
blob: a26592c75da20daf487cc1c2205bba567be9268b (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
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
// 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 "components/signin/core/browser/profile_management_switches.h"

#include <string>

#include "base/callback.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "build/build_config.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/signin/core/browser/signin_buildflags.h"
#include "components/signin/core/browser/signin_switches.h"

#if defined(OS_CHROMEOS)
#include "components/signin/core/browser/signin_pref_names.h"
#endif

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
#include "components/prefs/pref_service.h"
#endif

namespace signin {

namespace {

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
const char kDiceMigrationCompletePref[] = "signin.DiceMigrationComplete";

// Returns whether Dice is enabled for the user, based on the account
// consistency mode and the dice pref value.
bool IsDiceEnabledForPrefValue(bool dice_pref_value) {
  switch (GetAccountConsistencyMethod()) {
    case AccountConsistencyMethod::kDisabled:
    case AccountConsistencyMethod::kMirror:
    case AccountConsistencyMethod::kDiceFixAuthErrors:
    case AccountConsistencyMethod::kDicePrepareMigration:
      return false;
    case AccountConsistencyMethod::kDice:
      return true;
    case AccountConsistencyMethod::kDiceMigration:
      return dice_pref_value;
  }
  NOTREACHED();
  return false;
}
#endif  // BUILDFLAG(ENABLE_DICE_SUPPORT)

// Returns a callback telling if site isolation is enabled for Gaia origins.
base::RepeatingCallback<bool()>* GetIsGaiaIsolatedCallback() {
  static base::RepeatingCallback<bool()> g_is_gaia_isolated_callback;
  return &g_is_gaia_isolated_callback;
}

bool AccountConsistencyMethodGreaterOrEqual(AccountConsistencyMethod a,
                                            AccountConsistencyMethod b) {
  return static_cast<int>(a) >= static_cast<int>(b);
}

}  // namespace

// base::Feature definitions.
const base::Feature kAccountConsistencyFeature{
    "AccountConsistency", base::FEATURE_DISABLED_BY_DEFAULT};
const char kAccountConsistencyFeatureMethodParameter[] = "method";
const char kAccountConsistencyFeatureMethodMirror[] = "mirror";
const char kAccountConsistencyFeatureMethodDiceFixAuthErrors[] =
    "dice_fix_auth_errors";
const char kAccountConsistencyFeatureMethodDicePrepareMigration[] =
    "dice_prepare_migration_new_endpoint";
const char kAccountConsistencyFeatureMethodDiceMigration[] = "dice_migration";
const char kAccountConsistencyFeatureMethodDice[] = "dice";

const base::Feature kUnifiedConsent{"UnifiedConsent",
                                    base::FEATURE_DISABLED_BY_DEFAULT};

bool DiceMethodGreaterOrEqual(AccountConsistencyMethod a,
                              AccountConsistencyMethod b) {
  DCHECK_NE(AccountConsistencyMethod::kMirror, a);
  DCHECK_NE(AccountConsistencyMethod::kMirror, b);
  return AccountConsistencyMethodGreaterOrEqual(a, b);
}

void RegisterAccountConsistencyProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
#if defined(OS_CHROMEOS)
  registry->RegisterBooleanPref(prefs::kAccountConsistencyMirrorRequired,
                                false);
#endif
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  registry->RegisterBooleanPref(kDiceMigrationCompletePref, false);
#endif
}

AccountConsistencyMethod GetAccountConsistencyMethod() {
#if BUILDFLAG(ENABLE_MIRROR)
  // Mirror is always enabled on Android and iOS.
  return AccountConsistencyMethod::kMirror;
#endif

#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  DCHECK(!GetIsGaiaIsolatedCallback()->is_null());
  const AccountConsistencyMethod kDefaultMethod =
      AccountConsistencyMethod::kDiceFixAuthErrors;

  if (!GetIsGaiaIsolatedCallback()->Run())
    return kDefaultMethod;
#else
  const AccountConsistencyMethod kDefaultMethod =
      AccountConsistencyMethod::kDisabled;
#endif

  if (!base::FeatureList::IsEnabled(kAccountConsistencyFeature))
    return kDefaultMethod;

  std::string method_value = base::GetFieldTrialParamValueByFeature(
      kAccountConsistencyFeature, kAccountConsistencyFeatureMethodParameter);

  if (method_value == kAccountConsistencyFeatureMethodMirror)
    return AccountConsistencyMethod::kMirror;
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  else if (method_value == kAccountConsistencyFeatureMethodDiceFixAuthErrors)
    return AccountConsistencyMethod::kDiceFixAuthErrors;
  else if (method_value == kAccountConsistencyFeatureMethodDicePrepareMigration)
    return AccountConsistencyMethod::kDicePrepareMigration;
  else if (method_value == kAccountConsistencyFeatureMethodDiceMigration)
    return AccountConsistencyMethod::kDiceMigration;
  else if (method_value == kAccountConsistencyFeatureMethodDice)
    return AccountConsistencyMethod::kDice;
#endif

  return kDefaultMethod;
}

bool IsAccountConsistencyMirrorEnabled() {
  return GetAccountConsistencyMethod() == AccountConsistencyMethod::kMirror;
}

bool IsDicePrepareMigrationEnabled() {
  return AccountConsistencyMethodGreaterOrEqual(
      GetAccountConsistencyMethod(),
      AccountConsistencyMethod::kDicePrepareMigration);
}

bool IsDiceMigrationEnabled() {
  return AccountConsistencyMethodGreaterOrEqual(
      GetAccountConsistencyMethod(), AccountConsistencyMethod::kDiceMigration);
}

bool IsDiceEnabledForProfile(const PrefService* user_prefs) {
  DCHECK(user_prefs);
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  return IsDiceEnabledForPrefValue(
      user_prefs->GetBoolean(kDiceMigrationCompletePref));
#else
  return false;
#endif
}

bool IsDiceEnabled(const BooleanPrefMember* dice_pref_member) {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  DCHECK(dice_pref_member);
  DCHECK_EQ(kDiceMigrationCompletePref, dice_pref_member->GetPrefName());
  return IsDiceEnabledForPrefValue(dice_pref_member->GetValue());
#else
  return false;
#endif
}

std::unique_ptr<BooleanPrefMember> CreateDicePrefMember(
    PrefService* user_prefs) {
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  std::unique_ptr<BooleanPrefMember> pref_member =
      std::make_unique<BooleanPrefMember>();
  pref_member->Init(kDiceMigrationCompletePref, user_prefs);
  return pref_member;
#else
  return std::unique_ptr<BooleanPrefMember>();
#endif
}

void MigrateProfileToDice(PrefService* user_prefs) {
  DCHECK(IsDiceMigrationEnabled());
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
  user_prefs->SetBoolean(kDiceMigrationCompletePref, true);
#endif
}

bool IsDiceFixAuthErrorsEnabled() {
  return AccountConsistencyMethodGreaterOrEqual(
      GetAccountConsistencyMethod(),
      AccountConsistencyMethod::kDiceFixAuthErrors);
}

bool IsExtensionsMultiAccount() {
#if defined(OS_ANDROID) || defined(OS_IOS)
  NOTREACHED() << "Extensions are not enabled on Android or iOS";
  // Account consistency is enabled on Android and iOS.
  return false;
#endif

  return base::CommandLine::ForCurrentProcess()->HasSwitch(
             switches::kExtensionsMultiAccount) ||
         GetAccountConsistencyMethod() == AccountConsistencyMethod::kMirror;
}

void SetGaiaOriginIsolatedCallback(
    const base::RepeatingCallback<bool()>& is_gaia_isolated) {
  *GetIsGaiaIsolatedCallback() = is_gaia_isolated;
}

}  // namespace signin