diff options
author | nyquist@chromium.org <nyquist@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-15 12:21:29 +0000 |
---|---|---|
committer | nyquist@chromium.org <nyquist@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-15 12:21:29 +0000 |
commit | b6fd2de0da9846e36c45d49237b81066decbaf43 (patch) | |
tree | 00280749bf74a1a3cc663401d5927a569e6511b3 | |
parent | 0987a414cbb4fd039e6926861b78f7032ad631da (diff) | |
download | chromium_src-b6fd2de0da9846e36c45d49237b81066decbaf43.zip chromium_src-b6fd2de0da9846e36c45d49237b81066decbaf43.tar.gz chromium_src-b6fd2de0da9846e36c45d49237b81066decbaf43.tar.bz2 |
[sync] Split sync and signin helper class
Currently SyncStatusHelper handles both signin and sync-related tasks.
This splits it into two.
There will be a follow-up CL to remove the now deprecated methods, as
soon as the downstream code has been updated to use the new class.
BUG=159203
Review URL: https://chromiumcodereview.appspot.com/12873002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@188361 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 186 insertions, 48 deletions
diff --git a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java index 5786e6f..7ef58af 100644 --- a/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java +++ b/sync/android/java/src/org/chromium/sync/notifier/InvalidationService.java @@ -26,6 +26,7 @@ import org.chromium.sync.internal_api.pub.base.ModelType; import org.chromium.sync.notifier.InvalidationController.IntentProtocol; import org.chromium.sync.notifier.InvalidationPreferences.EditContext; import org.chromium.sync.signin.AccountManagerHelper; +import org.chromium.sync.signin.ChromeSigninController; import java.util.Collection; import java.util.Collections; @@ -189,7 +190,7 @@ public class InvalidationService extends AndroidListener { @Override public void requestAuthToken(final PendingIntent pendingIntent, @Nullable String invalidAuthToken) { - @Nullable Account account = SyncStatusHelper.get(this).getSignedInUser(); + @Nullable Account account = ChromeSigninController.get(this).getSignedInUser(); if (account == null) { // This should never happen, because this code should only be run if a user is // signed-in. @@ -387,7 +388,7 @@ public class InvalidationService extends AndroidListener { bundle.putLong("version", (version == null) ? 0 : version); bundle.putString("payload", (payload == null) ? "" : payload); } - Account account = SyncStatusHelper.get(this).getSignedInUser(); + Account account = ChromeSigninController.get(this).getSignedInUser(); String contractAuthority = InvalidationController.get(this).getContractAuthority(); requestSyncFromContentResolver(bundle, account, contractAuthority); } diff --git a/sync/android/java/src/org/chromium/sync/notifier/SyncStatusHelper.java b/sync/android/java/src/org/chromium/sync/notifier/SyncStatusHelper.java index 43c6fc7..cbc4fd4 100644 --- a/sync/android/java/src/org/chromium/sync/notifier/SyncStatusHelper.java +++ b/sync/android/java/src/org/chromium/sync/notifier/SyncStatusHelper.java @@ -8,17 +8,17 @@ package org.chromium.sync.notifier; import android.accounts.Account; import android.content.ContentResolver; import android.content.Context; -import android.content.SharedPreferences; import android.content.SyncStatusObserver; import android.os.StrictMode; -import android.preference.PreferenceManager; -import android.util.Log; -import org.chromium.base.ObserverList; import org.chromium.sync.signin.AccountManagerHelper; +import org.chromium.sync.signin.ChromeSigninController; import com.google.common.annotations.VisibleForTesting; +import java.util.HashMap; +import java.util.Map; + /** * A helper class to handle the current status of sync for Chrome in Android-land. * @@ -27,7 +27,10 @@ import com.google.common.annotations.VisibleForTesting; * To retrieve an instance of this class, call SyncStatusHelper.get(someContext). */ public class SyncStatusHelper { - + /** + * Deprecated. Use {@link ChromeSigninController.Listener}. + */ + @Deprecated public interface Listener { /** * Called when the user signs out of Chrome. @@ -38,20 +41,25 @@ public class SyncStatusHelper { // TODO(dsmyers): remove the downstream version of this constant. public static final String AUTH_TOKEN_TYPE_SYNC = "chromiumsync"; + /** + * Deprecated. Use {@link ChromeSigninController#SIGNED_IN_ACCOUNT_KEY}. + */ + @Deprecated @VisibleForTesting - public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username"; + public static final String SIGNED_IN_ACCOUNT_KEY = ChromeSigninController.SIGNED_IN_ACCOUNT_KEY; - public static final String TAG = "SyncStatusHelper"; + public static final String TAG = SyncStatusHelper.class.getSimpleName(); - private final Context mApplicationContext; + private static final Object LOCK = new Object(); - private final SyncContentResolverDelegate mSyncContentResolverWrapper; + private static SyncStatusHelper sSyncStatusHelper; - private static final Object lock = new Object(); + private final Context mApplicationContext; - private static SyncStatusHelper sSyncStatusHelper; + private final SyncContentResolverDelegate mSyncContentResolverWrapper; - private ObserverList<Listener> mListeners; + private final Map<Listener, SigninDelegateListenerDelegate> mListenerMap = + new HashMap<Listener, SigninDelegateListenerDelegate>(); /** * @param context the context @@ -61,7 +69,6 @@ public class SyncStatusHelper { SyncContentResolverDelegate syncContentResolverWrapper) { mApplicationContext = context.getApplicationContext(); mSyncContentResolverWrapper = syncContentResolverWrapper; - mListeners = new ObserverList<Listener>(); } /** @@ -75,10 +82,9 @@ public class SyncStatusHelper { * @return a singleton instance of the SyncStatusHelper */ public static SyncStatusHelper get(Context context) { - synchronized (lock) { + synchronized (LOCK) { if (sSyncStatusHelper == null) { - Context applicationContext = context.getApplicationContext(); - sSyncStatusHelper = new SyncStatusHelper(applicationContext, + sSyncStatusHelper = new SyncStatusHelper(context, new SystemSyncContentResolverDelegate()); } } @@ -95,7 +101,7 @@ public class SyncStatusHelper { @VisibleForTesting public static void overrideSyncStatusHelperForTests(Context context, SyncContentResolverDelegate syncContentResolverWrapper) { - synchronized (lock) { + synchronized (LOCK) { if (sSyncStatusHelper != null) { throw new IllegalStateException("SyncStatusHelper already exists"); } @@ -147,7 +153,7 @@ public class SyncStatusHelper { * @return true if sync is on, false otherwise */ public boolean isSyncEnabled() { - return isSyncEnabled(getSignedInUser()); + return isSyncEnabled(ChromeSigninController.get(mApplicationContext).getSignedInUser()); } /** @@ -212,36 +218,44 @@ public class SyncStatusHelper { StrictMode.setThreadPolicy(oldPolicy); } - // TODO(nyquist) Move all these methods about signed in user to GoogleServicesManager. + /** + * Deprecated. Use: {@link ChromeSigninController#getSignedInUser()}. + */ + @Deprecated public Account getSignedInUser() { - String syncAccountName = getSignedInAccountName(); - if (syncAccountName == null) { - return null; - } - return AccountManagerHelper.createAccountFromName(syncAccountName); + return ChromeSigninController.get(mApplicationContext).getSignedInUser(); } + /** + * Deprecated. Use: {@link ChromeSigninController#isSignedIn()}. + */ + @Deprecated public boolean isSignedIn() { - return getSignedInAccountName() != null; + return ChromeSigninController.get(mApplicationContext).isSignedIn(); } + /** + * Deprecated. Use: {@link ChromeSigninController#setSignedInAccountName(String)}. + */ + @Deprecated public void setSignedInAccountName(String accountName) { - getPreferences().edit() - .putString(SIGNED_IN_ACCOUNT_KEY, accountName) - .apply(); + ChromeSigninController.get(mApplicationContext).setSignedInAccountName(accountName); } + /** + * Deprecated. Use: {@link ChromeSigninController#clearSignedInUser()}. + */ + @Deprecated public void clearSignedInUser() { - Log.d(TAG, "Clearing user signed in to Chrome"); - setSignedInAccountName(null); - - for (Listener listener : mListeners) { - listener.onClearSignedInUser(); - } + ChromeSigninController.get(mApplicationContext).clearSignedInUser(); } - private String getSignedInAccountName() { - return getPreferences().getString(SIGNED_IN_ACCOUNT_KEY, null); + /** + * Deprecated. Use: {@link ChromeSigninController#getSignedInAccountName()}. + */ + @Deprecated + public String getSignedInAccountName() { + return ChromeSigninController.get(mApplicationContext).getSignedInAccountName(); } /** @@ -296,13 +310,6 @@ public class SyncStatusHelper { } /** - * Returns the default shared preferences. - */ - private SharedPreferences getPreferences() { - return PreferenceManager.getDefaultSharedPreferences(mApplicationContext); - } - - /** * Sets a new StrictMode.ThreadPolicy based on the current one, but allows disk reads * and disk writes. * @@ -323,17 +330,42 @@ public class SyncStatusHelper { /** * Adds a Listener. + * Deprecated. Use: {@link ChromeSigninController#addListener(ChromeSigninController.Listener)}. + * * @param listener Listener to add. */ public void addListener(Listener listener) { - mListeners.addObserver(listener); + SigninDelegateListenerDelegate signinListener = + new SigninDelegateListenerDelegate(listener); + mListenerMap.put(listener, signinListener); + ChromeSigninController.get(mApplicationContext).addListener(signinListener); } /** * Removes a Listener. + * Deprecated. Use: + * {@link ChromeSigninController#removeListener(ChromeSigninController.Listener)}. + * * @param listener Listener to remove from the list. */ + @Deprecated public void removeListener(Listener listener) { - mListeners.removeObserver(listener); + if (mListenerMap.containsKey(listener)) { + SigninDelegateListenerDelegate signinListener = mListenerMap.get(listener); + ChromeSigninController.get(mApplicationContext).removeListener(signinListener); + } + } + + private static class SigninDelegateListenerDelegate implements ChromeSigninController.Listener { + private final Listener mListener; + + private SigninDelegateListenerDelegate(Listener listener) { + mListener = listener; + } + + @Override + public void onClearSignedInUser() { + mListener.onClearSignedInUser(); + } } } diff --git a/sync/android/java/src/org/chromium/sync/signin/ChromeSigninController.java b/sync/android/java/src/org/chromium/sync/signin/ChromeSigninController.java new file mode 100644 index 0000000..555bb24 --- /dev/null +++ b/sync/android/java/src/org/chromium/sync/signin/ChromeSigninController.java @@ -0,0 +1,103 @@ +// Copyright 2013 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. + +package org.chromium.sync.signin; + +import android.accounts.Account; +import android.content.Context; +import android.preference.PreferenceManager; +import android.util.Log; + +import com.google.common.annotations.VisibleForTesting; + +import org.chromium.base.ObserverList; + +public class ChromeSigninController { + public interface Listener { + /** + * Called when the user signs out of Chrome. + */ + void onClearSignedInUser(); + } + + public static final String TAG = ChromeSigninController.class.getSimpleName(); + + @VisibleForTesting + public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username"; + + private static final Object LOCK = new Object(); + + private static ChromeSigninController sChromeSigninController; + + private final Context mApplicationContext; + + private final ObserverList<Listener> mListeners = new ObserverList<Listener>(); + + private ChromeSigninController(Context context) { + mApplicationContext = context.getApplicationContext(); + } + + /** + * A factory method for the ChromeSigninController. + * + * @param context the ApplicationContext is retrieved from the context used as an argument. + * @return a singleton instance of the ChromeSigninController + */ + public static ChromeSigninController get(Context context) { + synchronized (LOCK) { + if (sChromeSigninController == null) { + sChromeSigninController = new ChromeSigninController(context); + } + } + return sChromeSigninController; + } + + public Account getSignedInUser() { + String syncAccountName = getSignedInAccountName(); + if (syncAccountName == null) { + return null; + } + return AccountManagerHelper.createAccountFromName(syncAccountName); + } + + public boolean isSignedIn() { + return getSignedInAccountName() != null; + } + + public void setSignedInAccountName(String accountName) { + PreferenceManager.getDefaultSharedPreferences(mApplicationContext).edit() + .putString(SIGNED_IN_ACCOUNT_KEY, accountName) + .apply(); + } + + public void clearSignedInUser() { + Log.d(TAG, "Clearing user signed in to Chrome"); + setSignedInAccountName(null); + + for (Listener listener : mListeners) { + listener.onClearSignedInUser(); + } + } + + public String getSignedInAccountName() { + return PreferenceManager.getDefaultSharedPreferences(mApplicationContext) + .getString(SIGNED_IN_ACCOUNT_KEY, null); + } + + /** + * Adds a Listener. + * @param listener Listener to add. + */ + public void addListener(Listener listener) { + mListeners.addObserver(listener); + } + + /** + * Removes a Listener. + * @param listener Listener to remove from the list. + */ + public void removeListener(Listener listener) { + mListeners.removeObserver(listener); + } +} diff --git a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java index 6006ef3..bd5398a 100644 --- a/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java +++ b/sync/android/javatests/src/org/chromium/sync/notifier/InvalidationControllerTest.java @@ -22,6 +22,7 @@ import org.chromium.base.test.util.Feature; import org.chromium.sync.internal_api.pub.base.ModelType; import org.chromium.sync.notifier.InvalidationController.IntentProtocol; import org.chromium.sync.signin.AccountManagerHelper; +import org.chromium.sync.signin.ChromeSigninController; import org.chromium.sync.test.util.MockSyncContentResolverDelegate; import java.util.ArrayList; @@ -117,8 +118,9 @@ public class InvalidationControllerTest extends InstrumentationTestCase { // We don't want to use the system content resolver, so we override it. SyncStatusHelper.overrideSyncStatusHelperForTests(mContext, contentResolver); Account account = AccountManagerHelper.createAccountFromName("test@gmail.com"); + ChromeSigninController chromeSigninController = ChromeSigninController.get(mContext); + chromeSigninController.setSignedInAccountName(account.name); SyncStatusHelper syncStatusHelper = SyncStatusHelper.get(mContext); - syncStatusHelper.setSignedInAccountName(account.name); if (syncEnabled) { syncStatusHelper.enableAndroidSync(account); } else { |