diff -r 000000000000 -r 6474c204b198 mobile/android/base/sync/setup/SyncAuthenticatorService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/base/sync/setup/SyncAuthenticatorService.java Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,257 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.gecko.sync.setup; + +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; + +import org.mozilla.gecko.background.common.log.Logger; +import org.mozilla.gecko.sync.SyncConstants; +import org.mozilla.gecko.sync.Utils; +import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.AccountManager; +import android.accounts.NetworkErrorException; +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.IBinder; + +public class SyncAuthenticatorService extends Service { + private static final String LOG_TAG = "SyncAuthService"; + + private SyncAccountAuthenticator sAccountAuthenticator = null; + + @Override + public void onCreate() { + Logger.debug(LOG_TAG, "onCreate"); + sAccountAuthenticator = getAuthenticator(); + } + + @Override + public IBinder onBind(Intent intent) { + if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) { + return getAuthenticator().getIBinder(); + } + return null; + } + + private SyncAccountAuthenticator getAuthenticator() { + if (sAccountAuthenticator == null) { + sAccountAuthenticator = new SyncAccountAuthenticator(this); + } + return sAccountAuthenticator; + } + + /** + * Generate a "plain" auth token. + *
+ * Android caches only the value of the key
+ * AccountManager.KEY_AUTHTOKEN
, so if a caller needs the other
+ * keys in this bundle, it needs to invalidate the token (so that the bundle
+ * is re-generated).
+ *
+ * @param context
+ * Android context.
+ * @param account
+ * Android account.
+ * @return a Bundle
instance containing a subset of the following
+ * keys: (caller's must check for missing keys)
+ *
AccountManager.KEY_ACCOUNT_TYPE
: the Android
+ * Account's typeAccountManager.KEY_ACCOUNT_NAME
: the Android
+ * Account's nameAccountManager.KEY_AUTHTOKEN
: the Sync account's
+ * password Constants.OPTION_USERNAME
: the Sync account's
+ * hashed usernameConstants.OPTION_SERVER
: the Sync account's
+ * server Constants.OPTION_SYNCKEY
: the Sync account's
+ * sync key+ * This is not called when an Android Account is blown away due to + * the SD card being unmounted. + *
+ * Broadcasting a Firefox intent to version sharing this Android Account is + * a terrible hack, but it's better than the catching the generic + * "accounts changed" broadcast intent and trying to figure out whether our + * Account disappeared. + */ + @Override + public Bundle getAccountRemovalAllowed(final AccountAuthenticatorResponse response, Account account) + throws NetworkErrorException { + Bundle result = super.getAccountRemovalAllowed(response, account); + + if (result == null || + !result.containsKey(AccountManager.KEY_BOOLEAN_RESULT) || + result.containsKey(AccountManager.KEY_INTENT)) { + return result; + } + + final boolean removalAllowed = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT); + if (!removalAllowed) { + return result; + } + + // Bug 790931: Broadcast a message to all Firefox versions sharing this + // Android Account type telling that this Sync Account has been deleted. + // + // We would really prefer to receive Android's + // LOGIN_ACCOUNTS_CHANGED_ACTION broadcast, but that + // doesn't include enough information about which Accounts changed to + // correctly identify whether a Sync account has been removed (when some + // Firefox versions are installed on the SD card). + // + // Broadcast intents protected with permissions are secure, so it's okay + // to include password and sync key, etc. + final Intent intent = SyncAccounts.makeSyncAccountDeletedIntent(mContext, AccountManager.get(mContext), account); + Logger.info(LOG_TAG, "Account named " + account.name + " being removed; " + + "broadcasting secure intent " + intent.getAction() + "."); + mContext.sendBroadcast(intent, SyncConstants.PER_ACCOUNT_TYPE_PERMISSION); + + return result; + } + } +}