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..5db791e9e2d4e41bf6fe0f0a5ea46ba7ee3765ed 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,46 @@ 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. |
+ * A helper class for managing sync state for the ChromiumTestShell. |
+ * |
+ * Builds on top of the ProfileSyncService (which manages Chrome's sync engine's state) and mimics |
+ * the minimum additional functionality needed to fully enable sync for Chrome on Android. |
*/ |
-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 +103,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(); |
} |
}); |
} |