10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/signin/core/common/profile_management_switches.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/command_line.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/metrics/field_trial.h"
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "build/build_config.h"
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/signin/core/common/signin_switches.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char kNewProfileManagementFieldTrialName[] = "NewProfileManagement";
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Different state of new profile management/identity consistency.  The code
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// below assumes the order of the values in this enum.  That is, new profile
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// management is included in consistent identity.
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)enum State {
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  STATE_OLD_AVATAR_MENU,
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  STATE_NEW_AVATAR_MENU,
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  STATE_NEW_PROFILE_MANAGEMENT,
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  STATE_ACCOUNT_CONSISTENCY,
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)State GetProcessState() {
27e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  // Disables the new avatar menu if the web-based signin is turned on, because
28e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  // the new avatar menu always uses the inline signin, which may break some
29e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  // SAML users.
30e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch  if (switches::IsEnableWebBasedSignin())
31e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch    return STATE_OLD_AVATAR_MENU;
32e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Find the state of both command line args.
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool is_new_avatar_menu =
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          switches::kEnableNewAvatarMenu);
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool is_new_profile_management =
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          switches::kEnableNewProfileManagement);
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool is_consistent_identity =
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          switches::kEnableAccountConsistency);
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool not_new_avatar_menu =
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          switches::kDisableNewAvatarMenu);
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bool not_new_profile_management =
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          switches::kDisableNewProfileManagement);
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bool not_consistent_identity =
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      CommandLine::ForCurrentProcess()->HasSwitch(
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          switches::kDisableAccountConsistency);
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  int count_args = (is_new_avatar_menu ? 1 : 0) +
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (is_new_profile_management ? 1 : 0) +
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (is_consistent_identity ? 1 : 0) +
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      (not_new_avatar_menu ? 1 : 0) +
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (not_new_profile_management ? 1 : 0) +
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (not_consistent_identity ? 1 : 0);
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bool invalid_commandline = count_args > 1;
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // At most only one of the command line args should be specified, otherwise
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // the finch group assignment is undefined.  If this is the case, disable
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // the field trial so that data is not collected in the wrong group.
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string trial_type;
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (invalid_commandline) {
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::FieldTrial* field_trial =
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::FieldTrialList::Find(kNewProfileManagementFieldTrialName);
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (field_trial)
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      field_trial->Disable();
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    trial_type.clear();
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  } else {
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Since the experiment is not being disabled, get the full name of the
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // field trial which will initialize the underlying mechanism.
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    trial_type =
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::FieldTrialList::FindFullName(kNewProfileManagementFieldTrialName);
76f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Forcing the old avatar menu takes precedent over other args.
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Enable command line args take precedent over disable command line args.
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Consistent identity args take precedent over new profile management args.
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (not_new_avatar_menu) {
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return STATE_OLD_AVATAR_MENU;
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  } else if (is_consistent_identity) {
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return STATE_ACCOUNT_CONSISTENCY;
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else if (is_new_profile_management) {
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return STATE_NEW_PROFILE_MANAGEMENT;
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  } else if (is_new_avatar_menu) {
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return STATE_NEW_AVATAR_MENU;
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  } else if (not_new_profile_management) {
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return STATE_OLD_AVATAR_MENU;
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  } else if (not_consistent_identity) {
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return STATE_OLD_AVATAR_MENU;
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Set the default state
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_ANDROID)
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  State state = STATE_ACCOUNT_CONSISTENCY;
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  State state = STATE_OLD_AVATAR_MENU;
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!trial_type.empty()) {
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (trial_type == "Enabled") {
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      state = STATE_NEW_PROFILE_MANAGEMENT;
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else if (trial_type == "AccountConsistency") {
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      state = STATE_ACCOUNT_CONSISTENCY;
1076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    } else if (trial_type == "NewAvatarMenu") {
1086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      state = STATE_NEW_AVATAR_MENU;
109f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    } else {
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      state = STATE_OLD_AVATAR_MENU;
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return state;
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool CheckFlag(std::string command_switch, State min_state) {
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Individiual flag settings take precedence.
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(command_switch))
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return true;
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return GetProcessState() >= min_state;
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace switches {
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool IsEnableAccountConsistency() {
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return GetProcessState() >= STATE_ACCOUNT_CONSISTENCY;
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool IsEnableWebBasedSignin() {
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return CommandLine::ForCurrentProcess()->HasSwitch(
135e4256316f8b5e8d1ec0df1f7762771622a53fa63Ben Murdoch      switches::kEnableWebBasedSignin);
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool IsExtensionsMultiAccount() {
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return CheckFlag(switches::kExtensionsMultiAccount,
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   STATE_NEW_PROFILE_MANAGEMENT);
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool IsFastUserSwitching() {
1440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return CommandLine::ForCurrentProcess()->HasSwitch(
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      switches::kFastUserSwitching);
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool IsGoogleProfileInfo() {
1496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return CheckFlag(switches::kGoogleProfileInfo, STATE_NEW_AVATAR_MENU);
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
152a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochbool IsNewAvatarMenu() {
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // NewAvatarMenu is only available on desktop.
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#else
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return GetProcessState() >= STATE_NEW_AVATAR_MENU;
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
159a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
160a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool IsNewProfileManagement() {
162cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return GetProcessState() >= STATE_NEW_PROFILE_MANAGEMENT;
1630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
165a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochbool IsNewProfileManagementPreviewEnabled() {
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // No promotion to Enable Account Consistency.
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return false;
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void EnableNewAvatarMenuForTesting(base::CommandLine* command_line) {
1715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  command_line->AppendSwitch(switches::kEnableNewAvatarMenu);
1725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!command_line->HasSwitch(switches::kDisableNewAvatarMenu));
173a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}
174a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void DisableNewAvatarMenuForTesting(base::CommandLine* command_line) {
1766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  command_line->AppendSwitch(switches::kDisableNewAvatarMenu);
1776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(!command_line->HasSwitch(switches::kEnableNewAvatarMenu));
1786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
1796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
180f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void EnableNewProfileManagementForTesting(base::CommandLine* command_line) {
181f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  command_line->AppendSwitch(switches::kEnableNewProfileManagement);
182f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(!command_line->HasSwitch(switches::kDisableNewProfileManagement));
183f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
184f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
185f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void EnableAccountConsistencyForTesting(base::CommandLine* command_line) {
186f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  command_line->AppendSwitch(switches::kEnableAccountConsistency);
187f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DCHECK(!command_line->HasSwitch(switches::kDisableAccountConsistency));
188f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
189f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace switches
191