profile_management_switches.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 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
5#include "components/signin/core/common/profile_management_switches.h"
6
7#include "base/command_line.h"
8#include "base/metrics/field_trial.h"
9#include "build/build_config.h"
10#include "components/signin/core/common/signin_switches.h"
11
12namespace {
13
14const char kNewProfileManagementFieldTrialName[] = "NewProfileManagement";
15
16// Different state of new profile management/identity consistency.  The code
17// below assumes the order of the values in this enum.  That is, new profile
18// management is included in consistent identity.
19enum State {
20  STATE_OLD_AVATAR_MENU,
21  STATE_NEW_AVATAR_MENU,
22  STATE_NEW_PROFILE_MANAGEMENT,
23  STATE_ACCOUNT_CONSISTENCY,
24};
25
26State GetProcessState() {
27  // Find the state of both command line args.
28  bool is_new_avatar_menu =
29      CommandLine::ForCurrentProcess()->HasSwitch(
30          switches::kEnableNewAvatarMenu);
31  bool is_new_profile_management =
32      CommandLine::ForCurrentProcess()->HasSwitch(
33          switches::kEnableNewProfileManagement);
34  bool is_consistent_identity =
35      CommandLine::ForCurrentProcess()->HasSwitch(
36          switches::kEnableAccountConsistency);
37  bool not_new_avatar_menu =
38      CommandLine::ForCurrentProcess()->HasSwitch(
39          switches::kDisableNewAvatarMenu);
40  bool not_new_profile_management =
41      CommandLine::ForCurrentProcess()->HasSwitch(
42          switches::kDisableNewProfileManagement);
43  bool not_consistent_identity =
44      CommandLine::ForCurrentProcess()->HasSwitch(
45          switches::kDisableAccountConsistency);
46  int count_args = (is_new_avatar_menu ? 1 : 0) +
47      (is_new_profile_management ? 1 : 0) +
48      (is_consistent_identity ? 1 : 0) +
49      (not_new_avatar_menu ? 1 : 0) +
50      (not_new_profile_management ? 1 : 0) +
51      (not_consistent_identity ? 1 : 0);
52  bool invalid_commandline = count_args > 1;
53
54  // At most only one of the command line args should be specified, otherwise
55  // the finch group assignment is undefined.  If this is the case, disable
56  // the field trial so that data is not collected in the wrong group.
57  std::string trial_type;
58  if (invalid_commandline) {
59    base::FieldTrial* field_trial =
60        base::FieldTrialList::Find(kNewProfileManagementFieldTrialName);
61    if (field_trial)
62      field_trial->Disable();
63
64    trial_type.clear();
65  } else {
66    // Since the experiment is not being disabled, get the full name of the
67    // field trial which will initialize the underlying mechanism.
68    trial_type =
69        base::FieldTrialList::FindFullName(kNewProfileManagementFieldTrialName);
70  }
71
72  // Forcing the old avatar menu takes precedent over other args.
73  // Enable command line args take precedent over disable command line args.
74  // Consistent identity args take precedent over new profile management args.
75  if (not_new_avatar_menu) {
76    return STATE_OLD_AVATAR_MENU;
77  } else if (is_consistent_identity) {
78    return STATE_ACCOUNT_CONSISTENCY;
79  } else if (is_new_profile_management) {
80    return STATE_NEW_PROFILE_MANAGEMENT;
81  } else if (is_new_avatar_menu) {
82    return STATE_NEW_AVATAR_MENU;
83  } else if (not_new_profile_management) {
84    return STATE_OLD_AVATAR_MENU;
85  } else if (not_consistent_identity) {
86    return STATE_OLD_AVATAR_MENU;
87  }
88
89  // Set the default state
90#if defined(OS_ANDROID)
91  State state = STATE_ACCOUNT_CONSISTENCY;
92#else
93  State state = STATE_OLD_AVATAR_MENU;
94#endif
95
96  if (!trial_type.empty()) {
97    if (trial_type == "Enabled") {
98      state = STATE_NEW_PROFILE_MANAGEMENT;
99    } else if (trial_type == "AccountConsistency") {
100      state = STATE_ACCOUNT_CONSISTENCY;
101    } else if (trial_type == "NewAvatarMenu") {
102      state = STATE_NEW_AVATAR_MENU;
103    } else {
104      state = STATE_OLD_AVATAR_MENU;
105    }
106  }
107
108  return state;
109}
110
111bool CheckFlag(std::string command_switch, State min_state) {
112  // Individiual flag settings take precedence.
113  if (CommandLine::ForCurrentProcess()->HasSwitch(command_switch))
114    return true;
115
116  return GetProcessState() >= min_state;
117}
118
119}  // namespace
120
121namespace switches {
122
123bool IsEnableAccountConsistency() {
124  return GetProcessState() >= STATE_ACCOUNT_CONSISTENCY;
125}
126
127bool IsEnableWebBasedSignin() {
128  return CommandLine::ForCurrentProcess()->HasSwitch(
129      switches::kEnableWebBasedSignin) && !IsNewProfileManagement();
130}
131
132bool IsExtensionsMultiAccount() {
133  return CheckFlag(switches::kExtensionsMultiAccount,
134                   STATE_NEW_PROFILE_MANAGEMENT);
135}
136
137bool IsFastUserSwitching() {
138  return CommandLine::ForCurrentProcess()->HasSwitch(
139      switches::kFastUserSwitching);
140}
141
142bool IsGoogleProfileInfo() {
143  return CheckFlag(switches::kGoogleProfileInfo, STATE_NEW_AVATAR_MENU);
144}
145
146bool IsNewAvatarMenu() {
147  // NewAvatarMenu is only available on desktop.
148#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
149  return false;
150#else
151  return GetProcessState() >= STATE_NEW_AVATAR_MENU;
152#endif
153}
154
155bool IsNewProfileManagement() {
156  return GetProcessState() >= STATE_NEW_PROFILE_MANAGEMENT;
157}
158
159bool IsNewProfileManagementPreviewEnabled() {
160  // No promotion to Enable Account Consistency.
161  return false;
162}
163
164void EnableNewAvatarMenuForTesting(base::CommandLine* command_line) {
165  command_line->AppendSwitch(switches::kEnableNewAvatarMenu);
166  DCHECK(!command_line->HasSwitch(switches::kDisableNewAvatarMenu));
167}
168
169void DisableNewAvatarMenuForTesting(base::CommandLine* command_line) {
170  command_line->AppendSwitch(switches::kDisableNewAvatarMenu);
171  DCHECK(!command_line->HasSwitch(switches::kEnableNewAvatarMenu));
172}
173
174void EnableNewProfileManagementForTesting(base::CommandLine* command_line) {
175  command_line->AppendSwitch(switches::kEnableNewProfileManagement);
176  DCHECK(!command_line->HasSwitch(switches::kDisableNewProfileManagement));
177}
178
179void EnableAccountConsistencyForTesting(base::CommandLine* command_line) {
180  command_line->AppendSwitch(switches::kEnableAccountConsistency);
181  DCHECK(!command_line->HasSwitch(switches::kDisableAccountConsistency));
182}
183
184}  // namespace switches
185