OLD | NEW |
1 // Copyright 2015 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.browser.signin; | 5 package org.chromium.chrome.browser.signin; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.content.Context; | |
9 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
10 | 9 |
11 import org.chromium.base.Callback; | 10 import org.chromium.base.Callback; |
12 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
13 import org.chromium.base.ObserverList; | 12 import org.chromium.base.ObserverList; |
14 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
15 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
16 import org.chromium.base.annotations.JNINamespace; | 15 import org.chromium.base.annotations.JNINamespace; |
17 import org.chromium.components.signin.AccountManagerHelper; | 16 import org.chromium.components.signin.AccountManagerHelper; |
18 | 17 |
19 /** | 18 /** |
20 * Android wrapper of AccountTrackerService which provides access from the java l
ayer. | 19 * Android wrapper of AccountTrackerService which provides access from the java l
ayer. |
21 * It offers the capability of fetching and seeding system accounts into AccountT
rackerService in C++ | 20 * It offers the capability of fetching and seeding system accounts into AccountT
rackerService in C++ |
22 * layer, and notifies observers when it is complete. | 21 * layer, and notifies observers when it is complete. |
23 */ | 22 */ |
24 @JNINamespace("signin::android") | 23 @JNINamespace("signin::android") |
25 public class AccountTrackerService { | 24 public class AccountTrackerService { |
26 private static final String TAG = "AccountService"; | 25 private static final String TAG = "AccountService"; |
27 private static AccountTrackerService sAccountTrackerService; | 26 private static AccountTrackerService sAccountTrackerService; |
28 | 27 |
29 private SystemAccountsSeedingStatus mSystemAccountsSeedingStatus; | 28 private SystemAccountsSeedingStatus mSystemAccountsSeedingStatus; |
30 private boolean mSystemAccountsChanged; | 29 private boolean mSystemAccountsChanged; |
31 private boolean mSyncForceRefreshedForTest; | 30 private boolean mSyncForceRefreshedForTest; |
32 | 31 |
33 private final Context mContext; | |
34 | |
35 private enum SystemAccountsSeedingStatus { | 32 private enum SystemAccountsSeedingStatus { |
36 SEEDING_NOT_STARTED, | 33 SEEDING_NOT_STARTED, |
37 SEEDING_IN_PROGRESS, | 34 SEEDING_IN_PROGRESS, |
38 SEEDING_DONE, | 35 SEEDING_DONE, |
39 SEEDING_VALIDATING | 36 SEEDING_VALIDATING |
40 } | 37 } |
41 | 38 |
42 /** | 39 /** |
43 * Classes that want to listen for system accounts fetching and seeding shoul
d implement | 40 * Classes that want to listen for system accounts fetching and seeding shoul
d implement |
44 * this interface and register with {@link #addSystemAccountsSeededListener}. | 41 * this interface and register with {@link #addSystemAccountsSeededListener}. |
45 */ | 42 */ |
46 public interface OnSystemAccountsSeededListener { | 43 public interface OnSystemAccountsSeededListener { |
47 // Called at the end of seedSystemAccounts(). | 44 // Called at the end of seedSystemAccounts(). |
48 void onSystemAccountsSeedingComplete(); | 45 void onSystemAccountsSeedingComplete(); |
49 // Called in invalidateAccountSeedStatus() indicating that accounts have
changed. | 46 // Called in invalidateAccountSeedStatus() indicating that accounts have
changed. |
50 void onSystemAccountsChanged(); | 47 void onSystemAccountsChanged(); |
51 } | 48 } |
52 | 49 |
53 private final ObserverList<OnSystemAccountsSeededListener> mSystemAccountsSe
edingObservers = | 50 private final ObserverList<OnSystemAccountsSeededListener> mSystemAccountsSe
edingObservers = |
54 new ObserverList<>(); | 51 new ObserverList<>(); |
55 | 52 |
56 public static AccountTrackerService get(Context context) { | 53 public static AccountTrackerService get() { |
57 ThreadUtils.assertOnUiThread(); | 54 ThreadUtils.assertOnUiThread(); |
58 if (sAccountTrackerService == null) { | 55 if (sAccountTrackerService == null) { |
59 sAccountTrackerService = new AccountTrackerService(context); | 56 sAccountTrackerService = new AccountTrackerService(); |
60 } | 57 } |
61 return sAccountTrackerService; | 58 return sAccountTrackerService; |
62 } | 59 } |
63 | 60 |
64 private AccountTrackerService(Context context) { | 61 private AccountTrackerService() { |
65 mContext = context; | |
66 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_NOT_S
TARTED; | 62 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_NOT_S
TARTED; |
67 mSystemAccountsChanged = false; | 63 mSystemAccountsChanged = false; |
68 } | 64 } |
69 | 65 |
70 /** | 66 /** |
71 * Checks whether the account id <-> email mapping has been seeded into C++ l
ayer. | 67 * Checks whether the account id <-> email mapping has been seeded into C++ l
ayer. |
72 * If not, it automatically starts fetching the mapping and seeds it. | 68 * If not, it automatically starts fetching the mapping and seeds it. |
73 * @return Whether the accounts have been seeded already. | 69 * @return Whether the accounts have been seeded already. |
74 */ | 70 */ |
75 public boolean checkAndSeedSystemAccounts() { | 71 public boolean checkAndSeedSystemAccounts() { |
(...skipping 28 matching lines...) Expand all Loading... |
104 public void removeSystemAccountsSeededListener(OnSystemAccountsSeededListene
r observer) { | 100 public void removeSystemAccountsSeededListener(OnSystemAccountsSeededListene
r observer) { |
105 ThreadUtils.assertOnUiThread(); | 101 ThreadUtils.assertOnUiThread(); |
106 mSystemAccountsSeedingObservers.removeObserver(observer); | 102 mSystemAccountsSeedingObservers.removeObserver(observer); |
107 } | 103 } |
108 | 104 |
109 private void seedSystemAccounts() { | 105 private void seedSystemAccounts() { |
110 ThreadUtils.assertOnUiThread(); | 106 ThreadUtils.assertOnUiThread(); |
111 mSystemAccountsChanged = false; | 107 mSystemAccountsChanged = false; |
112 mSyncForceRefreshedForTest = false; | 108 mSyncForceRefreshedForTest = false; |
113 final AccountIdProvider accountIdProvider = AccountIdProvider.getInstanc
e(); | 109 final AccountIdProvider accountIdProvider = AccountIdProvider.getInstanc
e(); |
114 if (accountIdProvider.canBeUsed(mContext)) { | 110 if (accountIdProvider.canBeUsed()) { |
115 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_I
N_PROGRESS; | 111 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_I
N_PROGRESS; |
116 } else { | 112 } else { |
117 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_N
OT_STARTED; | 113 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_N
OT_STARTED; |
118 return; | 114 return; |
119 } | 115 } |
120 AccountManagerHelper.get(mContext).getGoogleAccounts(new Callback<Accoun
t[]>() { | 116 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() { |
121 @Override | 117 @Override |
122 public void onResult(final Account[] accounts) { | 118 public void onResult(final Account[] accounts) { |
123 new AsyncTask<Void, Void, String[][]>() { | 119 new AsyncTask<Void, Void, String[][]>() { |
124 @Override | 120 @Override |
125 public String[][] doInBackground(Void... params) { | 121 public String[][] doInBackground(Void... params) { |
126 Log.d(TAG, "Getting id/email mapping"); | 122 Log.d(TAG, "Getting id/email mapping"); |
127 String[][] accountIdNameMap = new String[2][accounts.len
gth]; | 123 String[][] accountIdNameMap = new String[2][accounts.len
gth]; |
128 for (int i = 0; i < accounts.length; ++i) { | 124 for (int i = 0; i < accounts.length; ++i) { |
129 accountIdNameMap[0][i] = | 125 accountIdNameMap[0][i] = |
130 accountIdProvider.getAccountId(mContext, acc
ounts[i].name); | 126 accountIdProvider.getAccountId(accounts[i].n
ame); |
131 accountIdNameMap[1][i] = accounts[i].name; | 127 accountIdNameMap[1][i] = accounts[i].name; |
132 } | 128 } |
133 return accountIdNameMap; | 129 return accountIdNameMap; |
134 } | 130 } |
135 @Override | 131 @Override |
136 public void onPostExecute(String[][] accountIdNameMap) { | 132 public void onPostExecute(String[][] accountIdNameMap) { |
137 if (mSyncForceRefreshedForTest) return; | 133 if (mSyncForceRefreshedForTest) return; |
138 if (mSystemAccountsChanged) { | 134 if (mSystemAccountsChanged) { |
139 seedSystemAccounts(); | 135 seedSystemAccounts(); |
140 return; | 136 return; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 * created because accounts changed notification from Android to Chrome has
latency. | 192 * created because accounts changed notification from Android to Chrome has
latency. |
197 */ | 193 */ |
198 public void validateSystemAccounts() { | 194 public void validateSystemAccounts() { |
199 ThreadUtils.assertOnUiThread(); | 195 ThreadUtils.assertOnUiThread(); |
200 if (!checkAndSeedSystemAccounts()) { | 196 if (!checkAndSeedSystemAccounts()) { |
201 // Do nothing if seeding is not done. | 197 // Do nothing if seeding is not done. |
202 return; | 198 return; |
203 } | 199 } |
204 | 200 |
205 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_VALID
ATING; | 201 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_VALID
ATING; |
206 AccountManagerHelper.get(mContext).getGoogleAccounts(new Callback<Accoun
t[]>() { | 202 AccountManagerHelper.get().getGoogleAccounts(new Callback<Account[]>() { |
207 @Override | 203 @Override |
208 public void onResult(final Account[] accounts) { | 204 public void onResult(final Account[] accounts) { |
209 if (mSystemAccountsChanged | 205 if (mSystemAccountsChanged |
210 || mSystemAccountsSeedingStatus | 206 || mSystemAccountsSeedingStatus |
211 != SystemAccountsSeedingStatus.SEEDING_VALIDATIN
G) { | 207 != SystemAccountsSeedingStatus.SEEDING_VALIDATIN
G) { |
212 return; | 208 return; |
213 } | 209 } |
214 | 210 |
215 String[] accountNames = new String[accounts.length]; | 211 String[] accountNames = new String[accounts.length]; |
216 for (int i = 0; i < accounts.length; ++i) { | 212 for (int i = 0; i < accounts.length; ++i) { |
217 accountNames[i] = accounts[i].name; | 213 accountNames[i] = accounts[i].name; |
218 } | 214 } |
219 if (nativeAreAccountsSeeded(accountNames)) { | 215 if (nativeAreAccountsSeeded(accountNames)) { |
220 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.S
EEDING_DONE; | 216 mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.S
EEDING_DONE; |
221 notifyObserversOnSeedingComplete(); | 217 notifyObserversOnSeedingComplete(); |
222 } | 218 } |
223 } | 219 } |
224 }); | 220 }); |
225 } | 221 } |
226 | 222 |
227 private void notifyObserversOnAccountsChange() { | 223 private void notifyObserversOnAccountsChange() { |
228 for (OnSystemAccountsSeededListener observer : mSystemAccountsSeedingObs
ervers) { | 224 for (OnSystemAccountsSeededListener observer : mSystemAccountsSeedingObs
ervers) { |
229 observer.onSystemAccountsChanged(); | 225 observer.onSystemAccountsChanged(); |
230 } | 226 } |
231 } | 227 } |
232 | 228 |
233 private static native void nativeSeedAccountsInfo(String[] gaiaIds, String[]
accountNames); | 229 private static native void nativeSeedAccountsInfo(String[] gaiaIds, String[]
accountNames); |
234 private static native boolean nativeAreAccountsSeeded(String[] accountNames)
; | 230 private static native boolean nativeAreAccountsSeeded(String[] accountNames)
; |
235 } | 231 } |
OLD | NEW |