Skip to content

Commit

Permalink
[sync] Split sync and signin helper class
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nyquist@chromium.org committed Mar 15, 2013
1 parent 0987a41 commit b6fd2de
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -189,7 +190,7 @@ public void reissueRegistrations(byte[] clientId) {
@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.
Expand Down Expand Up @@ -387,7 +388,7 @@ private void requestSync(@Nullable ObjectId objectId, @Nullable Long version,
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);
}
Expand Down
122 changes: 77 additions & 45 deletions sync/android/java/src/org/chromium/sync/notifier/SyncStatusHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
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.
*
Expand All @@ -27,7 +27,10 @@
* 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.
Expand All @@ -38,20 +41,25 @@ public interface Listener {
// 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
Expand All @@ -61,7 +69,6 @@ private SyncStatusHelper(Context context,
SyncContentResolverDelegate syncContentResolverWrapper) {
mApplicationContext = context.getApplicationContext();
mSyncContentResolverWrapper = syncContentResolverWrapper;
mListeners = new ObserverList<Listener>();
}

/**
Expand All @@ -75,10 +82,9 @@ private SyncStatusHelper(Context context,
* @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());
}
}
Expand All @@ -95,7 +101,7 @@ public static SyncStatusHelper get(Context context) {
@VisibleForTesting
public static void overrideSyncStatusHelperForTests(Context context,
SyncContentResolverDelegate syncContentResolverWrapper) {
synchronized (lock) {
synchronized (LOCK) {
if (sSyncStatusHelper != null) {
throw new IllegalStateException("SyncStatusHelper already exists");
}
Expand Down Expand Up @@ -147,7 +153,7 @@ public boolean isSyncEnabled(Account account) {
* @return true if sync is on, false otherwise
*/
public boolean isSyncEnabled() {
return isSyncEnabled(getSignedInUser());
return isSyncEnabled(ChromeSigninController.get(mApplicationContext).getSignedInUser());
}

/**
Expand Down Expand Up @@ -212,36 +218,44 @@ public void disableAndroidSync(Account account) {
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();
}

/**
Expand Down Expand Up @@ -295,13 +309,6 @@ public void onStatusChanged(int which) {
protected abstract void syncSettingsChanged();
}

/**
* 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.
Expand All @@ -323,17 +330,42 @@ private static StrictMode.ThreadPolicy temporarilyAllowDiskWritesAndDiskReads()

/**
* 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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -117,8 +118,9 @@ private void setupSync(boolean syncEnabled) {
// 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 {
Expand Down

0 comments on commit b6fd2de

Please sign in to comment.