diff -r 000000000000 -r 6474c204b198 mobile/android/base/fxa/receivers/FxAccountDeletedService.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mobile/android/base/fxa/receivers/FxAccountDeletedService.java Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,71 @@ +/* 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.fxa.receivers; + +import org.mozilla.gecko.background.common.log.Logger; +import org.mozilla.gecko.fxa.FxAccountConstants; +import org.mozilla.gecko.sync.config.AccountPickler; + +import android.app.IntentService; +import android.content.Context; +import android.content.Intent; + +/** + * A background service to clean up after a Firefox Account is deleted. + *
+ * Note that we specifically handle deleting the pickle file using a Service and a + * BroadcastReceiver, rather than a background thread, to allow channels sharing a Firefox account + * to delete their respective pickle files (since, if one remains, the account will be restored + * when that channel is used). + */ +public class FxAccountDeletedService extends IntentService { + public static final String LOG_TAG = FxAccountDeletedService.class.getSimpleName(); + + public FxAccountDeletedService() { + super(LOG_TAG); + } + + @Override + protected void onHandleIntent(final Intent intent) { + // Intent can, in theory, be null. Bug 1025937. + if (intent == null) { + Logger.debug(LOG_TAG, "Short-circuiting on null intent."); + return; + } + + final Context context = this; + + long intentVersion = intent.getLongExtra( + FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION_KEY, 0); + long expectedVersion = FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION; + if (intentVersion != expectedVersion) { + Logger.warn(LOG_TAG, "Intent malformed: version " + intentVersion + " given but " + + "version " + expectedVersion + "expected. Not cleaning up after deleted Account."); + return; + } + + // Android Account name, not Sync encoded account name. + final String accountName = intent.getStringExtra( + FxAccountConstants.ACCOUNT_DELETED_INTENT_ACCOUNT_KEY); + if (accountName == null) { + Logger.warn(LOG_TAG, "Intent malformed: no account name given. Not cleaning up after " + + "deleted Account."); + return; + } + + Logger.info(LOG_TAG, "Firefox account named " + accountName + " being removed; " + + "deleting saved pickle file '" + FxAccountConstants.ACCOUNT_PICKLE_FILENAME + "'."); + deletePickle(context); + } + + public static void deletePickle(final Context context) { + try { + AccountPickler.deletePickle(context, FxAccountConstants.ACCOUNT_PICKLE_FILENAME); + } catch (Exception e) { + // This should never happen, but we really don't want to die in a background thread. + Logger.warn(LOG_TAG, "Got exception deleting saved pickle file; ignoring.", e); + } + } +}