OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.shell.sync; | 5 package org.chromium.chrome.browser.sync; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.Activity; | 8 import android.app.Activity; |
9 import android.app.FragmentManager; | |
10 import android.content.Context; | 9 import android.content.Context; |
11 import android.util.Log; | 10 import android.util.Log; |
12 | 11 |
13 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
14 import org.chromium.chrome.browser.identity.UniqueIdentificationGeneratorFactory ; | |
15 import org.chromium.chrome.browser.identity.UuidBasedUniqueIdentificationGenerat or; | |
16 import org.chromium.chrome.browser.invalidation.InvalidationController; | 13 import org.chromium.chrome.browser.invalidation.InvalidationController; |
17 import org.chromium.chrome.browser.signin.SigninManager; | 14 import org.chromium.chrome.browser.signin.SigninManager; |
18 import org.chromium.chrome.browser.signin.SigninManager.SignInFlowObserver; | 15 import org.chromium.chrome.browser.signin.SigninManager.SignInFlowObserver; |
19 import org.chromium.chrome.browser.sync.ProfileSyncService; | |
20 import org.chromium.sync.notifier.SyncStatusHelper; | 16 import org.chromium.sync.notifier.SyncStatusHelper; |
21 import org.chromium.sync.signin.AccountManagerHelper; | 17 import org.chromium.sync.signin.AccountManagerHelper; |
22 import org.chromium.sync.signin.ChromeSigninController; | 18 import org.chromium.sync.signin.ChromeSigninController; |
23 | 19 |
24 /** | 20 /** |
25 * A helper class for managing sync state for the ChromeShell. | 21 * A helper class for managing sync state. |
26 * | 22 * |
27 * Builds on top of the ProfileSyncService (which manages Chrome's sync engine's state) and mimics | 23 * Builds on top of the ProfileSyncService (which manages Chrome's sync engine's state) and mimics |
28 * the minimum additional functionality needed to fully enable sync for Chrome o n Android. | 24 * the minimum additional functionality needed to fully enable sync for Chrome o n Android. |
29 */ | 25 */ |
30 public class SyncController implements ProfileSyncService.SyncStateChangedListen er, | 26 public class SyncController implements ProfileSyncService.SyncStateChangedListen er, |
31 SyncStatusHelper.SyncSettingsChangedObserver { | 27 SyncStatusHelper.SyncSettingsChangedObserver { |
32 private static final String TAG = "SyncController"; | 28 private static final String TAG = "SyncController"; |
33 | 29 |
34 private static final String SESSIONS_UUID_PREF_KEY = "chromium.sync.sessions .id"; | |
35 | |
36 private static SyncController sInstance; | 30 private static SyncController sInstance; |
37 | 31 |
38 private final Context mContext; | 32 private final Context mContext; |
39 private final ChromeSigninController mChromeSigninController; | 33 private final ChromeSigninController mChromeSigninController; |
40 private final SyncStatusHelper mSyncStatusHelper; | 34 private final SyncStatusHelper mSyncStatusHelper; |
41 private final ProfileSyncService mProfileSyncService; | 35 private final ProfileSyncService mProfileSyncService; |
42 | 36 |
43 private SyncController(Context context) { | 37 private SyncController(Context context) { |
44 mContext = context; | 38 mContext = context; |
45 mChromeSigninController = ChromeSigninController.get(mContext); | 39 mChromeSigninController = ChromeSigninController.get(mContext); |
46 mSyncStatusHelper = SyncStatusHelper.get(context); | 40 mSyncStatusHelper = SyncStatusHelper.get(context); |
47 mSyncStatusHelper.registerSyncSettingsChangedObserver(this); | 41 mSyncStatusHelper.registerSyncSettingsChangedObserver(this); |
48 mProfileSyncService = ProfileSyncService.get(mContext); | 42 mProfileSyncService = ProfileSyncService.get(mContext); |
49 mProfileSyncService.addSyncStateChangedListener(this); | 43 mProfileSyncService.addSyncStateChangedListener(this); |
50 | |
51 setupSessionSyncId(); | |
52 mChromeSigninController.ensureGcmIsInitialized(); | 44 mChromeSigninController.ensureGcmIsInitialized(); |
nyquist
2015/01/21 22:53:04
Could we move this to SigninManager and have it li
maxbogue
2015/01/21 23:37:56
Leaving as is per offline discussion.
| |
53 } | 45 } |
54 | 46 |
55 /** | 47 /** |
56 * Retrieve the singleton instance of this class. | 48 * Retrieve the singleton instance of this class. |
57 * | 49 * |
58 * @param context the current context. | 50 * @param context the current context. |
59 * @return the singleton instance. | 51 * @return the singleton instance. |
60 */ | 52 */ |
61 public static SyncController get(Context context) { | 53 public static SyncController get(Context context) { |
62 ThreadUtils.assertOnUiThread(); | 54 ThreadUtils.assertOnUiThread(); |
63 if (sInstance == null) { | 55 if (sInstance == null) { |
64 sInstance = new SyncController(context.getApplicationContext()); | 56 sInstance = new SyncController(context.getApplicationContext()); |
65 } | 57 } |
66 return sInstance; | 58 return sInstance; |
67 } | 59 } |
68 | 60 |
69 /** | 61 /** |
70 * Open a dialog that gives the user the option to sign in from a list of av ailable accounts. | |
71 * | |
72 * @param fragmentManager the FragmentManager. | |
73 */ | |
74 public static void openSigninDialog(FragmentManager fragmentManager) { | |
75 AccountChooserFragment chooserFragment = new AccountChooserFragment(); | |
76 chooserFragment.show(fragmentManager, null); | |
77 } | |
78 | |
79 /** | |
80 * Open a dialog that gives the user the option to sign out. | |
81 * | |
82 * @param fragmentManager the FragmentManager. | |
83 */ | |
84 public static void openSignOutDialog(FragmentManager fragmentManager) { | |
85 SignoutFragment signoutFragment = new SignoutFragment(); | |
86 signoutFragment.show(fragmentManager, null); | |
87 } | |
88 | |
89 /** | |
90 * Trigger Chromium sign in of the given account. | 62 * Trigger Chromium sign in of the given account. |
91 * | 63 * |
92 * This also ensure that sync setup is not in progress anymore, so sync will start after | 64 * This also ensure that sync setup is not in progress anymore, so sync will start after |
93 * sync initialization has happened. | 65 * sync initialization has happened. |
94 * | 66 * |
95 * @param activity the current activity. | 67 * @param activity the current activity. |
96 * @param accountName the full account name. | 68 * @param accountName the full account name. |
97 */ | 69 */ |
98 public void signIn(Activity activity, String accountName) { | 70 public void signIn(Activity activity, String accountName) { |
99 final Account account = AccountManagerHelper.createAccountFromName(accou ntName); | 71 final Account account = AccountManagerHelper.createAccountFromName(accou ntName); |
(...skipping 12 matching lines...) Expand all Loading... | |
112 start(); | 84 start(); |
113 } | 85 } |
114 | 86 |
115 @Override | 87 @Override |
116 public void onSigninCancelled() { | 88 public void onSigninCancelled() { |
117 stop(); | 89 stop(); |
118 } | 90 } |
119 }); | 91 }); |
120 } | 92 } |
121 | 93 |
122 public void onStart() { | 94 /** |
123 refreshSyncState(); | 95 * Updates sync to reflect the state of the Android sync settings. |
96 */ | |
97 public void updateSyncStateFromAndroid() { | |
98 if (mSyncStatusHelper.isSyncEnabled()) { | |
99 start(); | |
100 } else { | |
101 stop(); | |
102 } | |
124 } | 103 } |
125 | 104 |
126 private void setupSessionSyncId() { | 105 /** |
127 // Ensure that sync uses the correct UniqueIdentificationGenerator, but do not force the | 106 * Starts sync if the master sync flag is enabled. |
128 // registration, in case a test case has already overridden it. | 107 * |
129 UuidBasedUniqueIdentificationGenerator generator = | 108 * Affects native sync, the invalidation controller, and the Android sync se ttings. |
130 new UuidBasedUniqueIdentificationGenerator(mContext, SESSIONS_UU ID_PREF_KEY); | 109 */ |
131 UniqueIdentificationGeneratorFactory.registerGenerator( | 110 public void start() { |
132 UuidBasedUniqueIdentificationGenerator.GENERATOR_ID, generator, false); | |
133 // Since we do not override the UniqueIdentificationGenerator, we get it from the factory, | |
134 // instead of using the instance we just created. | |
135 mProfileSyncService.setSessionsId(UniqueIdentificationGeneratorFactory | |
136 .getInstance(UuidBasedUniqueIdentificationGenerator.GENERATOR_ID )); | |
137 } | |
138 | |
139 private void refreshSyncState() { | |
140 if (mSyncStatusHelper.isSyncEnabled()) | |
141 start(); | |
142 else | |
143 stop(); | |
144 } | |
145 | |
146 private void start() { | |
147 ThreadUtils.assertOnUiThread(); | 111 ThreadUtils.assertOnUiThread(); |
148 if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) { | 112 if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) { |
149 Log.d(TAG, "Enabling sync"); | 113 Log.d(TAG, "Enabling sync"); |
150 Account account = mChromeSigninController.getSignedInUser(); | 114 Account account = mChromeSigninController.getSignedInUser(); |
151 InvalidationController.get(mContext).start(); | 115 InvalidationController.get(mContext).start(); |
152 mProfileSyncService.enableSync(); | 116 mProfileSyncService.enableSync(); |
153 mSyncStatusHelper.enableAndroidSync(account); | 117 mSyncStatusHelper.enableAndroidSync(account); |
154 } | 118 } |
155 } | 119 } |
156 | 120 |
157 /** | 121 /** |
158 * Stops Sync if a user is currently signed in. | 122 * Stops Sync if a user is currently signed in. |
123 * | |
124 * Affects native sync, the invalidation controller, and the Android sync se ttings. | |
159 */ | 125 */ |
160 public void stop() { | 126 public void stop() { |
161 ThreadUtils.assertOnUiThread(); | 127 ThreadUtils.assertOnUiThread(); |
162 if (mChromeSigninController.isSignedIn()) { | 128 if (mChromeSigninController.isSignedIn()) { |
163 Log.d(TAG, "Disabling sync"); | 129 Log.d(TAG, "Disabling sync"); |
164 Account account = mChromeSigninController.getSignedInUser(); | 130 Account account = mChromeSigninController.getSignedInUser(); |
165 InvalidationController.get(mContext).stop(); | 131 InvalidationController.get(mContext).stop(); |
166 mProfileSyncService.disableSync(); | 132 mProfileSyncService.disableSync(); |
167 mSyncStatusHelper.disableAndroidSync(account); | 133 if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) { |
134 // Only disable Android's Chrome sync setting if we weren't disa bled | |
135 // by the master sync setting. This way, when master sync is ena bled | |
136 // they will both be on and sync will start again. | |
137 mSyncStatusHelper.disableAndroidSync(account); | |
138 } | |
168 } | 139 } |
169 } | 140 } |
170 | 141 |
171 /** | 142 /** |
172 * From {@link ProfileSyncService.SyncStateChangedListener}. | 143 * From {@link ProfileSyncService.SyncStateChangedListener}. |
nyquist
2015/01/21 22:53:04
Could you add a comment about what this affects li
maxbogue
2015/01/21 23:37:56
Done.
| |
173 */ | 144 */ |
174 @Override | 145 @Override |
175 public void syncStateChanged() { | 146 public void syncStateChanged() { |
176 ThreadUtils.assertOnUiThread(); | 147 ThreadUtils.assertOnUiThread(); |
177 // If sync has been disabled from the dashboard, we must disable it. | |
178 Account account = mChromeSigninController.getSignedInUser(); | 148 Account account = mChromeSigninController.getSignedInUser(); |
179 boolean isSyncSuppressStart = mProfileSyncService.isStartSuppressed(); | 149 // Don't do anything if there isn't an account. |
180 boolean isSyncEnabled = mSyncStatusHelper.isSyncEnabled(account); | 150 if (account == null) return; |
181 if (account != null && isSyncSuppressStart && isSyncEnabled) | 151 boolean isSyncActive = !mProfileSyncService.isStartSuppressed(); |
182 stop(); | 152 // Make the Java state match the native state. |
153 if (isSyncActive) { | |
154 InvalidationController.get(mContext).start(); | |
155 mSyncStatusHelper.enableAndroidSync(account); | |
156 } else { | |
157 InvalidationController.get(mContext).stop(); | |
158 if (mSyncStatusHelper.isMasterSyncAutomaticallyEnabled()) { | |
159 // See comment in stop(). | |
160 mSyncStatusHelper.disableAndroidSync(account); | |
161 } | |
162 } | |
183 } | 163 } |
184 | 164 |
185 /** | 165 /** |
186 * From {@link SyncStatusHelper.SyncSettingsChangedObserver}. | 166 * From {@link SyncStatusHelper.SyncSettingsChangedObserver}. |
187 */ | 167 */ |
188 @Override | 168 @Override |
189 public void syncSettingsChanged() { | 169 public void syncSettingsChanged() { |
190 ThreadUtils.runOnUiThread(new Runnable() { | 170 ThreadUtils.runOnUiThread(new Runnable() { |
191 @Override | 171 @Override |
192 public void run() { | 172 public void run() { |
193 refreshSyncState(); | 173 updateSyncStateFromAndroid(); |
194 } | 174 } |
195 }); | 175 }); |
196 } | 176 } |
197 } | 177 } |
OLD | NEW |