michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.fxa.receivers; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.fxa.FxAccountConstants; michael@0: import org.mozilla.gecko.sync.config.AccountPickler; michael@0: michael@0: import android.app.IntentService; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: michael@0: /** michael@0: * A background service to clean up after a Firefox Account is deleted. michael@0: *

michael@0: * Note that we specifically handle deleting the pickle file using a Service and a michael@0: * BroadcastReceiver, rather than a background thread, to allow channels sharing a Firefox account michael@0: * to delete their respective pickle files (since, if one remains, the account will be restored michael@0: * when that channel is used). michael@0: */ michael@0: public class FxAccountDeletedService extends IntentService { michael@0: public static final String LOG_TAG = FxAccountDeletedService.class.getSimpleName(); michael@0: michael@0: public FxAccountDeletedService() { michael@0: super(LOG_TAG); michael@0: } michael@0: michael@0: @Override michael@0: protected void onHandleIntent(final Intent intent) { michael@0: // Intent can, in theory, be null. Bug 1025937. michael@0: if (intent == null) { michael@0: Logger.debug(LOG_TAG, "Short-circuiting on null intent."); michael@0: return; michael@0: } michael@0: michael@0: final Context context = this; michael@0: michael@0: long intentVersion = intent.getLongExtra( michael@0: FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION_KEY, 0); michael@0: long expectedVersion = FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION; michael@0: if (intentVersion != expectedVersion) { michael@0: Logger.warn(LOG_TAG, "Intent malformed: version " + intentVersion + " given but " + michael@0: "version " + expectedVersion + "expected. Not cleaning up after deleted Account."); michael@0: return; michael@0: } michael@0: michael@0: // Android Account name, not Sync encoded account name. michael@0: final String accountName = intent.getStringExtra( michael@0: FxAccountConstants.ACCOUNT_DELETED_INTENT_ACCOUNT_KEY); michael@0: if (accountName == null) { michael@0: Logger.warn(LOG_TAG, "Intent malformed: no account name given. Not cleaning up after " + michael@0: "deleted Account."); michael@0: return; michael@0: } michael@0: michael@0: Logger.info(LOG_TAG, "Firefox account named " + accountName + " being removed; " + michael@0: "deleting saved pickle file '" + FxAccountConstants.ACCOUNT_PICKLE_FILENAME + "'."); michael@0: deletePickle(context); michael@0: } michael@0: michael@0: public static void deletePickle(final Context context) { michael@0: try { michael@0: AccountPickler.deletePickle(context, FxAccountConstants.ACCOUNT_PICKLE_FILENAME); michael@0: } catch (Exception e) { michael@0: // This should never happen, but we really don't want to die in a background thread. michael@0: Logger.warn(LOG_TAG, "Got exception deleting saved pickle file; ignoring.", e); michael@0: } michael@0: } michael@0: }