| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.firstrun; | 5 package org.chromium.chrome.browser.firstrun; |
| 6 | 6 |
| 7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 | 9 |
| 10 import org.chromium.base.Callback; | 10 import org.chromium.base.Callback; |
| 11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
| 12 import org.chromium.chrome.browser.ChromeActivity; | 12 import org.chromium.chrome.browser.ChromeActivity; |
| 13 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | 13 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
| 14 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | 14 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
| 15 import org.chromium.chrome.browser.services.AndroidEduAndChildAccountHelper; | 15 import org.chromium.chrome.browser.services.AndroidEduAndChildAccountHelper; |
| 16 import org.chromium.chrome.browser.signin.AccountManagementFragment; | 16 import org.chromium.chrome.browser.signin.AccountManagementFragment; |
| 17 import org.chromium.chrome.browser.signin.SigninManager; | 17 import org.chromium.chrome.browser.signin.SigninManager; |
| 18 import org.chromium.chrome.browser.util.FeatureUtilities; | 18 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 19 import org.chromium.components.signin.AccountManagerHelper; | 19 import org.chromium.components.signin.AccountManagerHelper; |
| 20 import org.chromium.components.signin.ChromeSigninController; | 20 import org.chromium.components.signin.ChromeSigninController; |
| 21 | 21 |
| 22 import javax.annotation.Nullable; |
| 23 |
| 22 /** | 24 /** |
| 23 * A helper to perform all necessary steps for forced sign in. | 25 * A helper to perform all necessary steps for forced sign in. |
| 24 * The helper performs: | 26 * The helper performs: |
| 25 * - necessary Android EDU and child account checks; | 27 * - necessary Android EDU and child account checks; |
| 26 * - automatic non-interactive forced sign in for Android EDU and child accounts
; and | 28 * - automatic non-interactive forced sign in for Android EDU and child accounts
; and |
| 27 * The helper calls the observer's onSignInComplete() if | 29 * The helper calls the observer's onSignInComplete() if |
| 28 * - nothing needs to be done, or when | 30 * - nothing needs to be done, or when |
| 29 * - the sign in is complete. | 31 * - the sign in is complete. |
| 30 * | 32 * |
| 31 * Usage: | 33 * Usage: |
| 32 * ForcedSigninProcessor.start(appContext). | 34 * ForcedSigninProcessor.start(appContext). |
| 33 */ | 35 */ |
| 34 public final class ForcedSigninProcessor { | 36 public final class ForcedSigninProcessor { |
| 35 private static final String TAG = "ForcedSignin"; | 37 private static final String TAG = "ForcedSignin"; |
| 36 | 38 |
| 37 /* | 39 /* |
| 38 * Only for static usage. | 40 * Only for static usage. |
| 39 */ | 41 */ |
| 40 private ForcedSigninProcessor() {} | 42 private ForcedSigninProcessor() {} |
| 41 | 43 |
| 42 /** | 44 /** |
| 43 * Check whether a forced automatic signin is required and process it if it
is. | 45 * Check whether a forced automatic signin is required and process it if it
is. |
| 44 * This is triggered once per Chrome Application lifetime and everytime the
Account state | 46 * This is triggered once per Chrome Application lifetime and everytime the
Account state |
| 45 * changes with early exit if an account has already been signed in. | 47 * changes with early exit if an account has already been signed in. |
| 46 */ | 48 */ |
| 47 public static void start(final Context appContext) { | 49 public static void start(final Context appContext, @Nullable final Runnable
onComplete) { |
| 48 if (ChromeSigninController.get(appContext).isSignedIn()) return; | 50 if (ChromeSigninController.get(appContext).isSignedIn()) return; |
| 49 new AndroidEduAndChildAccountHelper() { | 51 new AndroidEduAndChildAccountHelper() { |
| 50 @Override | 52 @Override |
| 51 public void onParametersReady() { | 53 public void onParametersReady() { |
| 52 boolean isAndroidEduDevice = isAndroidEduDevice(); | 54 boolean isAndroidEduDevice = isAndroidEduDevice(); |
| 53 boolean hasChildAccount = hasChildAccount(); | 55 boolean hasChildAccount = hasChildAccount(); |
| 54 // If neither a child account or and EDU device, we return. | 56 // If neither a child account or and EDU device, we return. |
| 55 if (!isAndroidEduDevice && !hasChildAccount) return; | 57 if (!isAndroidEduDevice && !hasChildAccount) return; |
| 56 // Child account and EDU device at the same time is not supporte
d. | 58 // Child account and EDU device at the same time is not supporte
d. |
| 57 assert !(isAndroidEduDevice && hasChildAccount); | 59 assert !(isAndroidEduDevice && hasChildAccount); |
| 58 processForcedSignIn(appContext); | 60 processForcedSignIn(appContext, onComplete); |
| 59 } | 61 } |
| 60 }.start(appContext); | 62 }.start(appContext); |
| 61 } | 63 } |
| 62 | 64 |
| 63 /** | 65 /** |
| 64 * Processes the fully automatic non-FRE-related forced sign-in. | 66 * Processes the fully automatic non-FRE-related forced sign-in. |
| 65 * This is used to enforce the environment for Android EDU and child account
s. | 67 * This is used to enforce the environment for Android EDU and child account
s. |
| 66 */ | 68 */ |
| 67 private static void processForcedSignIn(final Context appContext) { | 69 private static void processForcedSignIn( |
| 70 final Context appContext, @Nullable final Runnable onComplete) { |
| 68 final SigninManager signinManager = SigninManager.get(appContext); | 71 final SigninManager signinManager = SigninManager.get(appContext); |
| 69 // By definition we have finished all the checks for first run. | 72 // By definition we have finished all the checks for first run. |
| 70 signinManager.onFirstRunCheckDone(); | 73 signinManager.onFirstRunCheckDone(); |
| 71 if (!FeatureUtilities.canAllowSync(appContext) || !signinManager.isSignI
nAllowed()) { | 74 if (!FeatureUtilities.canAllowSync(appContext) || !signinManager.isSignI
nAllowed()) { |
| 72 Log.d(TAG, "Sign in disallowed"); | 75 Log.d(TAG, "Sign in disallowed"); |
| 73 return; | 76 return; |
| 74 } | 77 } |
| 75 AccountManagerHelper.get(appContext).getGoogleAccounts(new Callback<Acco
unt[]>() { | 78 AccountManagerHelper.get(appContext).getGoogleAccounts(new Callback<Acco
unt[]>() { |
| 76 @Override | 79 @Override |
| 77 public void onResult(Account[] accounts) { | 80 public void onResult(Account[] accounts) { |
| 78 if (accounts.length != 1) { | 81 if (accounts.length != 1) { |
| 79 Log.d(TAG, "Incorrect number of accounts (%d)", accounts.len
gth); | 82 Log.d(TAG, "Incorrect number of accounts (%d)", accounts.len
gth); |
| 80 return; | 83 return; |
| 81 } | 84 } |
| 82 signinManager.signIn(accounts[0], null, new SigninManager.SignIn
Callback() { | 85 signinManager.signIn(accounts[0], null, new SigninManager.SignIn
Callback() { |
| 83 @Override | 86 @Override |
| 84 public void onSignInComplete() { | 87 public void onSignInComplete() { |
| 85 // Since this is a forced signin, signout is not allowed
. | 88 // Since this is a forced signin, signout is not allowed
. |
| 86 AccountManagementFragment.setSignOutAllowedPreferenceVal
ue( | 89 AccountManagementFragment.setSignOutAllowedPreferenceVal
ue( |
| 87 appContext, false); | 90 appContext, false); |
| 91 if (onComplete != null) { |
| 92 onComplete.run(); |
| 93 } |
| 88 } | 94 } |
| 89 | 95 |
| 90 @Override | 96 @Override |
| 91 public void onSignInAborted() {} | 97 public void onSignInAborted() { |
| 98 if (onComplete != null) { |
| 99 onComplete.run(); |
| 100 } |
| 101 } |
| 92 }); | 102 }); |
| 93 } | 103 } |
| 94 }); | 104 }); |
| 95 } | 105 } |
| 96 | 106 |
| 97 /** | 107 /** |
| 98 * If forced signin is required by policy, check that Google Play Services i
s available, and | 108 * If forced signin is required by policy, check that Google Play Services i
s available, and |
| 99 * show a non-cancelable dialog otherwise. | 109 * show a non-cancelable dialog otherwise. |
| 100 * @param activity The activity for which to show the dialog. | 110 * @param activity The activity for which to show the dialog. |
| 101 */ | 111 */ |
| 102 // TODO(bauerb): Once external dependencies reliably use policy to force sig
n-in, | 112 // TODO(bauerb): Once external dependencies reliably use policy to force sig
n-in, |
| 103 // consider removing the child account / EDU checks. | 113 // consider removing the child account / EDU checks. |
| 104 public static void checkCanSignIn(final ChromeActivity activity) { | 114 public static void checkCanSignIn(final ChromeActivity activity) { |
| 105 final Context appContext = activity.getApplicationContext(); | 115 final Context appContext = activity.getApplicationContext(); |
| 106 if (SigninManager.get(appContext).isForceSigninEnabled()) { | 116 if (SigninManager.get(appContext).isForceSigninEnabled()) { |
| 107 ExternalAuthUtils.getInstance().canUseGooglePlayServices(appContext, | 117 ExternalAuthUtils.getInstance().canUseGooglePlayServices(appContext, |
| 108 new UserRecoverableErrorHandler.ModalDialog(activity, false)
); | 118 new UserRecoverableErrorHandler.ModalDialog(activity, false)
); |
| 109 } | 119 } |
| 110 } | 120 } |
| 111 } | 121 } |
| OLD | NEW |