| Index: chrome/android/java/src/org/chromium/chrome/browser/signin/SigninHelper.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/signin/SigninHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/signin/SigninHelper.java
|
| index a4669b829867fd019790de919ca92cf302a3143d..b01572db9b24a0b3c97fa2ec8fb419dce2917dc6 100644
|
| --- a/chrome/android/java/src/org/chromium/chrome/browser/signin/SigninHelper.java
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/signin/SigninHelper.java
|
| @@ -100,7 +100,7 @@
|
|
|
|
|
| @VisibleForTesting
|
| - protected final Context mAppContext;
|
| + protected final Context mContext;
|
|
|
| private final ChromeSigninController mChromeSigninController;
|
|
|
| @@ -111,8 +111,6 @@
|
| private final AccountTrackerService mAccountTrackerService;
|
|
|
| private final OAuth2TokenService mOAuth2TokenService;
|
| -
|
| - private SharedPreferences mSharedPreferences;
|
|
|
|
|
| public static SigninHelper get(Context context) {
|
| @@ -124,39 +122,13 @@
|
| return sInstance;
|
| }
|
|
|
| - /**
|
| - * Create an instance of SigninHelper without native dependencies for tests.
|
| - */
|
| - @VisibleForTesting
|
| - public static void initializeForTests(Context context) {
|
| - synchronized (LOCK) {
|
| - Context appContext = context.getApplicationContext();
|
| - sInstance = new SigninHelper(appContext, null, null, null, null, null,
|
| - PreferenceManager.getDefaultSharedPreferences(appContext));
|
| - }
|
| - }
|
| -
|
| - private SigninHelper(Context appContext, ProfileSyncService profileSyncService,
|
| - SigninManager signinManager, AccountTrackerService accountTrackerService,
|
| - OAuth2TokenService oAuth2TokenService, ChromeSigninController chromeSigninController,
|
| - SharedPreferences sharedPreferences) {
|
| - mAppContext = appContext;
|
| - mProfileSyncService = profileSyncService;
|
| - mSigninManager = signinManager;
|
| - mAccountTrackerService = accountTrackerService;
|
| - mOAuth2TokenService = oAuth2TokenService;
|
| - mChromeSigninController = chromeSigninController;
|
| - mSharedPreferences = sharedPreferences;
|
| - }
|
| -
|
| - private SigninHelper(Context appContext) {
|
| - this(appContext,
|
| - ProfileSyncService.get(),
|
| - SigninManager.get(appContext),
|
| - AccountTrackerService.get(appContext),
|
| - OAuth2TokenService.getForProfile(Profile.getLastUsedProfile()),
|
| - ChromeSigninController.get(appContext),
|
| - PreferenceManager.getDefaultSharedPreferences(appContext));
|
| + private SigninHelper(Context context) {
|
| + mContext = context;
|
| + mProfileSyncService = ProfileSyncService.get();
|
| + mSigninManager = SigninManager.get(mContext);
|
| + mAccountTrackerService = AccountTrackerService.get(mContext);
|
| + mOAuth2TokenService = OAuth2TokenService.getForProfile(Profile.getLastUsedProfile());
|
| + mChromeSigninController = ChromeSigninController.get(mContext);
|
| }
|
|
|
| public void validateAccountSettings(boolean accountsChanged) {
|
| @@ -172,17 +144,18 @@
|
| PrefServiceBridge.getInstance().getSyncLastAccountName();
|
| if (lastSyncAccountName != null && !lastSyncAccountName.isEmpty()) return;
|
|
|
| - boolean hasKnownAccountKeys = mSharedPreferences.contains(ANDROID_ACCOUNTS_PREFS_KEY);
|
| + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
| + boolean hasKnownAccountKeys = sharedPrefs.contains(ANDROID_ACCOUNTS_PREFS_KEY);
|
| // Nothing to do if Android accounts are not changed and already known to Chrome.
|
| if (hasKnownAccountKeys && !accountsChanged) return;
|
|
|
| List<String> currentAccountNames =
|
| - AccountManagerHelper.get(mAppContext).getGoogleAccountNames();
|
| + AccountManagerHelper.get(mContext).getGoogleAccountNames();
|
| if (hasKnownAccountKeys) {
|
| ChromePreferenceManager chromePreferenceManager =
|
| - ChromePreferenceManager.getInstance(mAppContext);
|
| + ChromePreferenceManager.getInstance(mContext);
|
| if (!chromePreferenceManager.getSigninPromoShown()) {
|
| - Set<String> lastKnownAccountNames = mSharedPreferences.getStringSet(
|
| + Set<String> lastKnownAccountNames = sharedPrefs.getStringSet(
|
| ANDROID_ACCOUNTS_PREFS_KEY, new HashSet<String>());
|
| Set<String> newAccountNames = new HashSet<String>(currentAccountNames);
|
| newAccountNames.removeAll(lastKnownAccountNames);
|
| @@ -192,33 +165,33 @@
|
| }
|
| }
|
|
|
| - mSharedPreferences.edit().putStringSet(
|
| + sharedPrefs.edit().putStringSet(
|
| ANDROID_ACCOUNTS_PREFS_KEY, new HashSet<String>(currentAccountNames)).apply();
|
| return;
|
| }
|
|
|
| - String renamedAccount = getNewSignedInAccountName();
|
| + String renamedAccount = getNewSignedInAccountName(mContext);
|
| if (accountsChanged && renamedAccount != null) {
|
| - handleAccountRename(ChromeSigninController.get(mAppContext).getSignedInAccountName(),
|
| + handleAccountRename(ChromeSigninController.get(mContext).getSignedInAccountName(),
|
| renamedAccount);
|
| return;
|
| }
|
|
|
| // Always check for account deleted.
|
| - if (!accountExists(syncAccount)) {
|
| + if (!accountExists(mContext, syncAccount)) {
|
| // It is possible that Chrome got to this point without account
|
| // rename notification. Let us signout before doing a rename.
|
| // updateAccountRenameData(mContext, new SystemAccountChangeEventChecker());
|
| AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
|
| @Override
|
| protected Void doInBackground(Void... params) {
|
| - updateAccountRenameData(new SystemAccountChangeEventChecker());
|
| + updateAccountRenameData(mContext, new SystemAccountChangeEventChecker());
|
| return null;
|
| }
|
|
|
| @Override
|
| protected void onPostExecute(Void result) {
|
| - String renamedAccount = getNewSignedInAccountName();
|
| + String renamedAccount = getNewSignedInAccountName(mContext);
|
| if (renamedAccount == null) {
|
| mSigninManager.signOut();
|
| } else {
|
| @@ -233,10 +206,10 @@
|
| if (accountsChanged) {
|
| // Account details have changed so inform the token service that credentials
|
| // should now be available.
|
| - mOAuth2TokenService.validateAccounts(mAppContext, false);
|
| - }
|
| -
|
| - if (mProfileSyncService != null && AndroidSyncSettings.isSyncEnabled(mAppContext)) {
|
| + mOAuth2TokenService.validateAccounts(mContext, false);
|
| + }
|
| +
|
| + if (mProfileSyncService != null && AndroidSyncSettings.isSyncEnabled(mContext)) {
|
| if (mProfileSyncService.isFirstSetupComplete()) {
|
| if (accountsChanged) {
|
| // Nudge the syncer to ensure it does a full sync.
|
| @@ -271,7 +244,7 @@
|
| // If Chrome dies, we can try it again on next run.
|
| // Otherwise, if re-sign-in fails, we'll just leave chrome
|
| // signed-out.
|
| - clearNewSignedInAccountName();
|
| + clearNewSignedInAccountName(mContext);
|
| performResignin(newName);
|
| }
|
| });
|
| @@ -295,8 +268,8 @@
|
| });
|
| }
|
|
|
| - private boolean accountExists(Account account) {
|
| - Account[] accounts = AccountManagerHelper.get(mAppContext).getGoogleAccounts();
|
| + private static boolean accountExists(Context context, Account account) {
|
| + Account[] accounts = AccountManagerHelper.get(context).getGoogleAccounts();
|
| for (Account a : accounts) {
|
| if (a.equals(account)) {
|
| return true;
|
| @@ -308,46 +281,48 @@
|
| /**
|
| * Sets the ACCOUNTS_CHANGED_PREFS_KEY to true.
|
| */
|
| - public void markAccountsChangedPref() {
|
| + public static void markAccountsChangedPref(Context context) {
|
| // The process may go away as soon as we return from onReceive but Android makes sure
|
| // that in-flight disk writes from apply() complete before changing component states.
|
| - mSharedPreferences.edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, true).apply();
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, true).apply();
|
| }
|
|
|
| /**
|
| * @return The new account name of the current user. Null if it wasn't renamed.
|
| */
|
| - @VisibleForTesting
|
| - public String getNewSignedInAccountName() {
|
| - return (mSharedPreferences.getString(ACCOUNT_RENAMED_PREFS_KEY, null));
|
| - }
|
| -
|
| - private void clearNewSignedInAccountName() {
|
| - mSharedPreferences.edit().putString(ACCOUNT_RENAMED_PREFS_KEY, null).apply();
|
| - }
|
| -
|
| - private String getLastKnownAccountName() {
|
| + public static String getNewSignedInAccountName(Context context) {
|
| + return (PreferenceManager.getDefaultSharedPreferences(context)
|
| + .getString(ACCOUNT_RENAMED_PREFS_KEY, null));
|
| + }
|
| +
|
| + private static void clearNewSignedInAccountName(Context context) {
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit()
|
| + .putString(ACCOUNT_RENAMED_PREFS_KEY, null)
|
| + .apply();
|
| + }
|
| +
|
| + private static String getLastKnownAccountName(Context context) {
|
| // This is the last known name of the currently signed in user.
|
| // It can be:
|
| // 1. The signed in account name known to the ChromeSigninController.
|
| // 2. A pending newly choosen name that is differed from the one known to
|
| // ChromeSigninController but is stored in ACCOUNT_RENAMED_PREFS_KEY.
|
| - String name = mSharedPreferences.getString(ACCOUNT_RENAMED_PREFS_KEY, null);
|
| + String name = PreferenceManager.getDefaultSharedPreferences(context).getString(
|
| + ACCOUNT_RENAMED_PREFS_KEY, null);
|
|
|
| // If there is no pending rename, take the name known to ChromeSigninController.
|
| - if (name == null) {
|
| - return ChromeSigninController.get(mAppContext).getSignedInAccountName();
|
| - }
|
| - return name;
|
| - }
|
| -
|
| - public void updateAccountRenameData() {
|
| - updateAccountRenameData(new SystemAccountChangeEventChecker());
|
| + return name == null ? ChromeSigninController.get(context).getSignedInAccountName() : name;
|
| + }
|
| +
|
| + public static void updateAccountRenameData(Context context) {
|
| + updateAccountRenameData(context, new SystemAccountChangeEventChecker());
|
| }
|
|
|
| @VisibleForTesting
|
| - public void updateAccountRenameData(AccountChangeEventChecker checker) {
|
| - String curName = getLastKnownAccountName();
|
| + public static void updateAccountRenameData(Context context, AccountChangeEventChecker checker) {
|
| + String curName = getLastKnownAccountName(context);
|
|
|
| // Skip the search if there is no signed in account.
|
| if (curName == null) return;
|
| @@ -355,7 +330,7 @@
|
| String newName = curName;
|
|
|
| // This is the last read index of all the account change event.
|
| - int eventIndex = mSharedPreferences.getInt(
|
| + int eventIndex = PreferenceManager.getDefaultSharedPreferences(context).getInt(
|
| ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY, 0);
|
|
|
| int newIndex = eventIndex;
|
| @@ -363,7 +338,7 @@
|
| try {
|
| outerLoop:
|
| while (true) {
|
| - List<String> nameChanges = checker.getAccountChangeEvents(mAppContext,
|
| + List<String> nameChanges = checker.getAccountChangeEvents(context,
|
| newIndex, newName);
|
|
|
| for (String name : nameChanges) {
|
| @@ -371,7 +346,8 @@
|
| // We have found a rename event of the current account.
|
| // We need to check if that account is further renamed.
|
| newName = name;
|
| - if (!accountExists(AccountManagerHelper.createAccountFromName(newName))) {
|
| + if (!accountExists(context, AccountManagerHelper.get(context)
|
| + .createAccountFromName(newName))) {
|
| newIndex = 0; // Start from the beginning of the new account.
|
| continue outerLoop;
|
| }
|
| @@ -389,26 +365,31 @@
|
| }
|
|
|
| if (!curName.equals(newName)) {
|
| - mSharedPreferences.edit().putString(ACCOUNT_RENAMED_PREFS_KEY, newName).apply();
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit().putString(ACCOUNT_RENAMED_PREFS_KEY, newName).apply();
|
| }
|
|
|
| if (newIndex != eventIndex) {
|
| - mSharedPreferences.edit().putInt(
|
| - ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY, newIndex).apply();
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit().putInt(ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY, newIndex).apply();
|
| }
|
| }
|
|
|
| @VisibleForTesting
|
| - public void resetAccountRenameEventIndex() {
|
| - mSharedPreferences.edit().putInt(ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY, 0).apply();
|
| - }
|
| -
|
| - public boolean checkAndClearAccountsChangedPref() {
|
| - if (mSharedPreferences.getBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false)) {
|
| + public static void resetAccountRenameEventIndex(Context context) {
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit().putInt(ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY, 0).apply();
|
| + }
|
| +
|
| + public static boolean checkAndClearAccountsChangedPref(Context context) {
|
| + if (PreferenceManager.getDefaultSharedPreferences(context)
|
| + .getBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false)) {
|
| // Clear the value in prefs.
|
| - mSharedPreferences.edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false).apply();
|
| + PreferenceManager.getDefaultSharedPreferences(context)
|
| + .edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false).apply();
|
| return true;
|
| - }
|
| - return false;
|
| + } else {
|
| + return false;
|
| + }
|
| }
|
| }
|
|
|