1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/fxa/receivers/FxAccountDeletedService.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.fxa.receivers; 1.9 + 1.10 +import org.mozilla.gecko.background.common.log.Logger; 1.11 +import org.mozilla.gecko.fxa.FxAccountConstants; 1.12 +import org.mozilla.gecko.sync.config.AccountPickler; 1.13 + 1.14 +import android.app.IntentService; 1.15 +import android.content.Context; 1.16 +import android.content.Intent; 1.17 + 1.18 +/** 1.19 + * A background service to clean up after a Firefox Account is deleted. 1.20 + * <p> 1.21 + * Note that we specifically handle deleting the pickle file using a Service and a 1.22 + * BroadcastReceiver, rather than a background thread, to allow channels sharing a Firefox account 1.23 + * to delete their respective pickle files (since, if one remains, the account will be restored 1.24 + * when that channel is used). 1.25 + */ 1.26 +public class FxAccountDeletedService extends IntentService { 1.27 + public static final String LOG_TAG = FxAccountDeletedService.class.getSimpleName(); 1.28 + 1.29 + public FxAccountDeletedService() { 1.30 + super(LOG_TAG); 1.31 + } 1.32 + 1.33 + @Override 1.34 + protected void onHandleIntent(final Intent intent) { 1.35 + // Intent can, in theory, be null. Bug 1025937. 1.36 + if (intent == null) { 1.37 + Logger.debug(LOG_TAG, "Short-circuiting on null intent."); 1.38 + return; 1.39 + } 1.40 + 1.41 + final Context context = this; 1.42 + 1.43 + long intentVersion = intent.getLongExtra( 1.44 + FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION_KEY, 0); 1.45 + long expectedVersion = FxAccountConstants.ACCOUNT_DELETED_INTENT_VERSION; 1.46 + if (intentVersion != expectedVersion) { 1.47 + Logger.warn(LOG_TAG, "Intent malformed: version " + intentVersion + " given but " + 1.48 + "version " + expectedVersion + "expected. Not cleaning up after deleted Account."); 1.49 + return; 1.50 + } 1.51 + 1.52 + // Android Account name, not Sync encoded account name. 1.53 + final String accountName = intent.getStringExtra( 1.54 + FxAccountConstants.ACCOUNT_DELETED_INTENT_ACCOUNT_KEY); 1.55 + if (accountName == null) { 1.56 + Logger.warn(LOG_TAG, "Intent malformed: no account name given. Not cleaning up after " + 1.57 + "deleted Account."); 1.58 + return; 1.59 + } 1.60 + 1.61 + Logger.info(LOG_TAG, "Firefox account named " + accountName + " being removed; " + 1.62 + "deleting saved pickle file '" + FxAccountConstants.ACCOUNT_PICKLE_FILENAME + "'."); 1.63 + deletePickle(context); 1.64 + } 1.65 + 1.66 + public static void deletePickle(final Context context) { 1.67 + try { 1.68 + AccountPickler.deletePickle(context, FxAccountConstants.ACCOUNT_PICKLE_FILENAME); 1.69 + } catch (Exception e) { 1.70 + // This should never happen, but we really don't want to die in a background thread. 1.71 + Logger.warn(LOG_TAG, "Got exception deleting saved pickle file; ignoring.", e); 1.72 + } 1.73 + } 1.74 +}