Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.signin; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.AsyncTask; | |
| 9 import android.util.Log; | |
| 10 | |
| 11 import org.chromium.base.ObserverList; | |
| 12 import org.chromium.base.ThreadUtils; | |
| 13 import org.chromium.sync.signin.AccountManagerHelper; | |
| 14 | |
| 15 /** | |
| 16 * Android wrapper of AccountTrackerService which provides access from the java l ayer. | |
| 17 * It offers the capability of fetching and seeding system accounts into AccountT rackerService in C++ | |
| 18 * layer, and notifies observers when it is complete. | |
| 19 */ | |
| 20 public class AccountTrackerService { | |
| 21 private static final String TAG = "AccountTrackerService"; | |
| 22 private static AccountTrackerService sAccountTrackerService; | |
| 23 private static SystemAccountsSeedingStatus sSystemAccountsSeedingStatus = | |
| 24 SystemAccountsSeedingStatus.SEEDING_NOT_STARTED; | |
| 25 private static boolean sForceRefreshed = false; | |
| 26 | |
| 27 private final Context mContext; | |
| 28 private final long mNativeAccountTrackerService; | |
| 29 | |
| 30 private enum SystemAccountsSeedingStatus { | |
| 31 SEEDING_NOT_STARTED, | |
| 32 SEEDING_IN_PROGRESS, | |
| 33 SEEDING_DONE | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * Classes that want to listen for system accounts fetching and seeding shoul d implement | |
| 38 * this interface and register with {@link #addSystemAccountsSeededListener}. | |
| 39 */ | |
| 40 public interface OnSystemAccountsSeededListener { | |
| 41 void onSystemAccountsSeedingComplete(); | |
| 42 void onSystemAccountsForceRefreshed(); | |
| 43 } | |
| 44 | |
| 45 private static final ObserverList<OnSystemAccountsSeededListener> | |
| 46 sSystemAccountsSeedingObservers = new ObserverList<OnSystemAccountsS eededListener>(); | |
| 47 | |
| 48 public static AccountTrackerService get(Context context) { | |
| 49 ThreadUtils.assertOnUiThread(); | |
| 50 if (sAccountTrackerService == null) { | |
| 51 sAccountTrackerService = new AccountTrackerService(context); | |
| 52 } | |
| 53 return sAccountTrackerService; | |
| 54 } | |
| 55 | |
| 56 private AccountTrackerService(Context context) { | |
| 57 mContext = context; | |
| 58 mNativeAccountTrackerService = nativeInit(); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Check whether systems accounts have been seeded into AccountTrackerService in C++ layer. | |
| 63 */ | |
| 64 public boolean isSystemAccountsSeeded() { | |
| 65 ThreadUtils.assertOnUiThread(); | |
| 66 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ DONE | |
| 67 && !sForceRefreshed) { | |
|
Roger Tawa OOO till Jul 10th
2015/08/21 20:29:15
I don't think we need the !sForceRefreshed check h
gogerald1
2015/08/21 20:47:08
Done.
| |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ NOT_STARTED) { | |
| 72 seedSystemAccounts(); | |
| 73 } | |
| 74 | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Register an |observer| to observe system accounts seeding status. | |
| 80 */ | |
| 81 public static void addSystemAccountsSeededListener(OnSystemAccountsSeededLis tener observer) { | |
| 82 ThreadUtils.assertOnUiThread(); | |
| 83 sSystemAccountsSeedingObservers.addObserver(observer); | |
| 84 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ DONE | |
| 85 && !sForceRefreshed) { | |
| 86 observer.onSystemAccountsSeedingComplete(); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 private void seedSystemAccounts() { | |
| 91 ThreadUtils.assertOnUiThread(); | |
| 92 sSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_IN_PR OGRESS; | |
| 93 sForceRefreshed = false; | |
| 94 AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(mCo ntext); | |
| 95 java.util.List<String> accountNamesList = accountManagerHelper.getGoogle AccountNames(); | |
| 96 final String[] accountNames = accountNamesList.toArray(new String[accoun tNamesList.size()]); | |
| 97 new AsyncTask<Void, Void, String[]>() { | |
| 98 @Override | |
| 99 public String[] doInBackground(Void... params) { | |
| 100 Log.d(TAG, "Getting id/email mapping"); | |
| 101 String[] accountIds = new String[accountNames.length]; | |
| 102 AccountIdProvider provider = AccountIdProvider.getInstance(); | |
| 103 for (int i = 0; i < accountIds.length; ++i) { | |
| 104 accountIds[i] = provider.getAccountId(mContext, accountNames [i]); | |
| 105 } | |
| 106 return accountIds; | |
| 107 } | |
| 108 @Override | |
| 109 public void onPostExecute(String[] accountIds) { | |
| 110 if (sForceRefreshed) { | |
| 111 seedSystemAccounts(); | |
| 112 } else { | |
| 113 nativeSeedAccountsInfo(mNativeAccountTrackerService, account Ids, accountNames); | |
| 114 sSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.S EEDING_DONE; | |
| 115 notifyObserversOnSeedingComplete(); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 }.execute(); | |
| 120 } | |
| 121 | |
| 122 private void notifyObserversOnSeedingComplete() { | |
| 123 for (OnSystemAccountsSeededListener observer : sSystemAccountsSeedingObs ervers) { | |
| 124 observer.onSystemAccountsSeedingComplete(); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 /** | |
| 129 * Force AccountTrackerService refresh/update systems accounts. | |
| 130 */ | |
| 131 public void forceRefresh() { | |
| 132 ThreadUtils.assertOnUiThread(); | |
| 133 sForceRefreshed = true; | |
|
Roger Tawa OOO till Jul 10th
2015/08/21 20:29:15
Nit: rename to sForceRefresh
gogerald1
2015/08/21 20:47:08
Done.
| |
| 134 notifyObserversOnForceRefreshed(); | |
| 135 if (sSystemAccountsSeedingStatus != SystemAccountsSeedingStatus.SEEDING_ IN_PROGRESS) { | |
| 136 seedSystemAccounts(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 private void notifyObserversOnForceRefreshed() { | |
| 141 for (OnSystemAccountsSeededListener observer : sSystemAccountsSeedingObs ervers) { | |
| 142 observer.onSystemAccountsForceRefreshed(); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Native methods. | |
| 147 private native long nativeInit(); | |
| 148 private native void nativeSeedAccountsInfo( | |
| 149 long nativeAccountTrackerServiceAndroid, String[] gaiaIds, String[] accountNames); | |
| 150 } | |
| OLD | NEW |