Chromium Code Reviews| Index: chrome/android/testshell/java/src/org/chromium/chrome/testshell/sync/SyncController.java |
| diff --git a/chrome/android/testshell/java/src/org/chromium/chrome/testshell/sync/SyncController.java b/chrome/android/testshell/java/src/org/chromium/chrome/testshell/sync/SyncController.java |
| index 9799ee33be161773d950834d1708731f7041dde6..ab2a9f183b46c547ab5dfddf44672d2a96e763b7 100644 |
| --- a/chrome/android/testshell/java/src/org/chromium/chrome/testshell/sync/SyncController.java |
| +++ b/chrome/android/testshell/java/src/org/chromium/chrome/testshell/sync/SyncController.java |
| @@ -8,24 +8,43 @@ import android.accounts.Account; |
| import android.app.Activity; |
| import android.app.FragmentManager; |
| import android.content.Context; |
| +import android.util.Log; |
| import org.chromium.base.ThreadUtils; |
| +import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory; |
| import org.chromium.chrome.browser.signin.SigninManager; |
| +import org.chromium.chrome.browser.identity.UuidBasedUniqueIdentificationGenerator; |
| import org.chromium.chrome.browser.sync.ProfileSyncService; |
| +import org.chromium.sync.notifier.InvalidationController; |
| import org.chromium.sync.notifier.SyncStatusHelper; |
| import org.chromium.sync.signin.AccountManagerHelper; |
| +import org.chromium.sync.signin.ChromeSigninController; |
| /** |
| * A helper class for signing in and out of Chromium. |
|
Yaron
2013/08/27 18:19:23
"for managing sync state for the ChromiumTestShell
nyquist
2013/08/30 00:55:43
Done.
|
| */ |
| -public class SyncController { |
| +public class SyncController implements ProfileSyncService.SyncStateChangedListener, |
| + SyncStatusHelper.SyncSettingsChangedObserver { |
| + private static final String TAG = "SyncController"; |
| + |
| + private static final String SESSIONS_UUID_PREF_KEY = "chromium.sync.sessions.id"; |
| private static SyncController sInstance; |
| private final Context mContext; |
| + private final ChromeSigninController mChromeSigninController; |
| + private final SyncStatusHelper mSyncStatusHelper; |
| + private final ProfileSyncService mProfileSyncService; |
| private SyncController(Context context) { |
| mContext = context; |
| + mChromeSigninController = ChromeSigninController.get(mContext); |
| + mSyncStatusHelper = SyncStatusHelper.get(context); |
| + mProfileSyncService = ProfileSyncService.get(mContext); |
| + mProfileSyncService.addSyncStateChangedListener(this); |
| + |
| + setupSessionSyncId(); |
| + mChromeSigninController.ensureGcmIsInitialized(); |
| } |
| /** |
| @@ -81,13 +100,88 @@ public class SyncController { |
| signinManager.startSignIn(activity, account, passive, new SigninManager.Observer() { |
| @Override |
| public void onSigninComplete() { |
| - ProfileSyncService.get(mContext).setSetupInProgress(false); |
| - // The SigninManager does not control the Android sync state. |
| - SyncStatusHelper.get(mContext).enableAndroidSync(account); |
| + SigninManager.get(mContext).logInSignedInUser(); |
| + mProfileSyncService.setSetupInProgress(false); |
| + mProfileSyncService.syncSignIn(); |
| + start(); |
| } |
| @Override |
| public void onSigninCancelled() { |
| + stop(); |
| + } |
| + }); |
| + } |
| + |
| + public void onResume() { |
| + refreshSyncState(); |
| + } |
| + |
| + private void setupSessionSyncId() { |
| + // Ensure that sync uses the correct UniqueIdentificationGenerator, but do not force the |
| + // registration, in case a test case has already overridden it. |
| + UuidBasedUniqueIdentificationGenerator generator = |
| + new UuidBasedUniqueIdentificationGenerator(mContext, SESSIONS_UUID_PREF_KEY); |
| + UniqueIdentificationGeneratorFactory.registerGenerator( |
| + UuidBasedUniqueIdentificationGenerator.GENERATOR_ID, generator, false); |
| + // Since we do not override the UniqueIdentificationGenerator, we get it from the factory, |
| + // instead of using the instance we just created. |
| + mProfileSyncService.setSessionsId(UniqueIdentificationGeneratorFactory |
| + .getInstance(UuidBasedUniqueIdentificationGenerator.GENERATOR_ID)); |
| + } |
| + |
| + private void refreshSyncState() { |
| + if (mSyncStatusHelper.isSyncEnabled()) |
| + start(); |
| + else |
| + stop(); |
| + } |
| + |
| + private void start() { |
| + ThreadUtils.assertOnUiThread(); |
| + if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) { |
| + Log.d(TAG, "Enabling sync"); |
| + Account account = mChromeSigninController.getSignedInUser(); |
| + InvalidationController.get(mContext).start(); |
| + mProfileSyncService.enableSync(); |
| + mSyncStatusHelper.enableAndroidSync(account); |
| + } |
| + } |
| + |
| + private void stop() { |
| + ThreadUtils.assertOnUiThread(); |
| + if (mChromeSigninController.isSignedIn()) { |
| + Log.d(TAG, "Disabling sync"); |
| + Account account = mChromeSigninController.getSignedInUser(); |
| + InvalidationController.get(mContext).stop(); |
| + mProfileSyncService.disableSync(); |
| + mSyncStatusHelper.disableAndroidSync(account); |
| + } |
| + } |
| + |
| + /** |
| + * From {@link ProfileSyncService.SyncStateChangedListener}. |
| + */ |
| + @Override |
| + public void syncStateChanged() { |
| + ThreadUtils.assertOnUiThread(); |
| + // If sync has been disabled from the dashboard, we must disable it. |
| + Account account = mChromeSigninController.getSignedInUser(); |
| + boolean isSyncSuppressStart = mProfileSyncService.isStartSuppressed(); |
| + boolean isSyncEnabled = mSyncStatusHelper.isSyncEnabled(account); |
| + if (account != null && isSyncSuppressStart && isSyncEnabled) |
| + stop(); |
| + } |
| + |
| + /** |
| + * From {@link SyncStatusHelper.SyncSettingsChangedObserver}. |
| + */ |
| + @Override |
| + public void syncSettingsChanged() { |
| + ThreadUtils.runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + refreshSyncState(); |
| } |
| }); |
| } |