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 import java.util.concurrent.Semaphore; | |
| 16 | |
| 17 /** | |
| 18 * Android wrapper of AccountTrackerService which provides access from the java l ayer. | |
| 19 * It offers the capability of fetching and seeding system accounts into AccountT rackerService in C++ | |
| 20 * layer, and notifies observers when it is complete. | |
| 21 */ | |
| 22 public class AccountTrackerService { | |
| 23 private static final String TAG = "AccountTrackerService"; | |
| 24 private static AccountTrackerService sAccountTrackerService; | |
| 25 private static SystemAccountsSeedingStatus sSystemAccountsSeedingStatus = | |
| 26 SystemAccountsSeedingStatus.SEEDING_NOT_STARTED; | |
| 27 | |
| 28 private final Context mContext; | |
| 29 private final long mNativeAccountTrackerService; | |
| 30 private final Semaphore mSemaphore = new Semaphore(1, true); | |
| 31 | |
| 32 private enum SystemAccountsSeedingStatus { | |
| 33 SEEDING_NOT_STARTED, | |
| 34 SEEDING_IN_PROGRESS, | |
| 35 SEEDING_DONE | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Classes that want to listen for system accounts fetching and seeding shoul d implement | |
| 40 * this interface and register with {@link #addSystemAccountsSeededListener}. | |
| 41 */ | |
| 42 public interface OnSystemAccountsSeededListener { | |
| 43 void onSystemAccountsSeedingComplete(); | |
| 44 void onSystemAccountsForceRefreshed(); | |
| 45 } | |
| 46 | |
| 47 private static final ObserverList<OnSystemAccountsSeededListener> | |
| 48 sSystemAccountsSeedingObservers = new ObserverList<OnSystemAccountsS eededListener>(); | |
| 49 | |
| 50 public static AccountTrackerService get(Context context) { | |
| 51 ThreadUtils.assertOnUiThread(); | |
| 52 if (sAccountTrackerService == null) { | |
| 53 sAccountTrackerService = new AccountTrackerService(context); | |
| 54 } | |
| 55 return sAccountTrackerService; | |
| 56 } | |
| 57 | |
| 58 private AccountTrackerService(Context context) { | |
| 59 mContext = context; | |
| 60 mNativeAccountTrackerService = nativeInit(); | |
| 61 } | |
| 62 | |
| 63 /** | |
| 64 * Check whether systems accounts have been seeded into AccountTrackerService in C++ layer. | |
| 65 */ | |
| 66 public boolean isSystemAccountsSeeded() { | |
| 67 ThreadUtils.assertOnUiThread(); | |
| 68 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ DONE) { | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ NOT_STARTED) { | |
| 73 seedSystemAccounts(); | |
| 74 } | |
| 75 | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Register an |observer| to observe system accounts seeding status. | |
| 81 */ | |
| 82 public static void addSystemAccountsSeededListener(OnSystemAccountsSeededLis tener observer) { | |
| 83 ThreadUtils.assertOnUiThread(); | |
| 84 sSystemAccountsSeedingObservers.addObserver(observer); | |
| 85 if (sSystemAccountsSeedingStatus == SystemAccountsSeedingStatus.SEEDING_ DONE) { | |
| 86 observer.onSystemAccountsSeedingComplete(); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 private void seedSystemAccounts() { | |
| 91 AccountManagerHelper accountManagerHelper = AccountManagerHelper.get(mCo ntext); | |
| 92 java.util.List<String> accountNamesList = accountManagerHelper.getGoogle AccountNames(); | |
| 93 final String[] accountNames = accountNamesList.toArray(new String[accoun tNamesList.size()]); | |
| 94 new AsyncTask<Void, Void, String[]>() { | |
| 95 @Override | |
| 96 public String[] doInBackground(Void... params) { | |
| 97 try { | |
| 98 mSemaphore.acquire(); | |
|
Roger Tawa OOO till Jul 10th
2015/08/19 14:37:26
seedSystemAccounts() can only be called from UI th
gogerald1
2015/08/20 15:48:49
Done. Add one more flag to get rid of semaphore.
| |
| 99 } catch (InterruptedException e) { | |
| 100 Log.w(TAG, "Got InterruptedException while waiting for semap hore"); | |
| 101 return new String[0]; | |
| 102 } | |
| 103 if (sSystemAccountsSeedingStatus | |
| 104 == SystemAccountsSeedingStatus.SEEDING_NOT_STARTED) { | |
| 105 Log.d(TAG, "Getting id/email mapping"); | |
| 106 sSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.S EEDING_IN_PROGRESS; | |
| 107 String[] accountIds = new String[accountNames.length]; | |
| 108 AccountIdProvider provider = AccountIdProvider.getInstance() ; | |
| 109 for (int i = 0; i < accountIds.length; ++i) { | |
| 110 accountIds[i] = provider.getAccountId(mContext, accountN ames[i]); | |
| 111 } | |
| 112 return accountIds; | |
| 113 } | |
| 114 return new String[0]; | |
| 115 } | |
| 116 @Override | |
| 117 public void onPostExecute(String[] accountIds) { | |
| 118 if (sSystemAccountsSeedingStatus | |
| 119 == SystemAccountsSeedingStatus.SEEDING_IN_PROGRESS) { | |
| 120 nativeSeedAccountsInfo(mNativeAccountTrackerService, account Ids, accountNames); | |
| 121 sSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.S EEDING_DONE; | |
| 122 notifyObserversOnSeedingComplete(); | |
| 123 } | |
| 124 mSemaphore.release(); | |
| 125 } | |
| 126 | |
| 127 }.execute(); | |
| 128 } | |
| 129 | |
| 130 private void notifyObserversOnSeedingComplete() { | |
| 131 for (OnSystemAccountsSeededListener observer : sSystemAccountsSeedingObs ervers) { | |
| 132 observer.onSystemAccountsSeedingComplete(); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * Force AccountTrackerService refresh/update systems accounts. | |
| 138 */ | |
| 139 public void forceRefresh() { | |
| 140 ThreadUtils.assertOnUiThread(); | |
| 141 sSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_NOT_S TARTED; | |
| 142 notifyObserversOnForceRefreshed(); | |
| 143 seedSystemAccounts(); | |
| 144 } | |
| 145 | |
| 146 private void notifyObserversOnForceRefreshed() { | |
| 147 for (OnSystemAccountsSeededListener observer : sSystemAccountsSeedingObs ervers) { | |
| 148 observer.onSystemAccountsForceRefreshed(); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 // Native methods. | |
| 153 private native long nativeInit(); | |
| 154 private native void nativeSeedAccountsInfo( | |
| 155 long nativeAccountTrackerServiceAndroid, String[] gaiaIds, String[] accountNames); | |
| 156 } | |
| OLD | NEW |