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.sync.receivers; michael@0: michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: import org.mozilla.gecko.background.common.GlobalConstants; michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.Sync11Configuration; michael@0: import org.mozilla.gecko.sync.SyncConstants; michael@0: import org.mozilla.gecko.sync.SyncConfiguration; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: import org.mozilla.gecko.sync.config.AccountPickler; michael@0: import org.mozilla.gecko.sync.config.ClientRecordTerminator; michael@0: import org.mozilla.gecko.sync.net.BasicAuthHeaderProvider; michael@0: import org.mozilla.gecko.sync.setup.Constants; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters; michael@0: michael@0: import android.accounts.AccountManager; michael@0: import android.app.IntentService; michael@0: import android.content.Context; michael@0: import android.content.Intent; michael@0: import android.content.SharedPreferences; michael@0: michael@0: public class SyncAccountDeletedService extends IntentService { michael@0: public static final String LOG_TAG = "SyncAccountDeletedService"; michael@0: michael@0: public SyncAccountDeletedService() { michael@0: super(LOG_TAG); michael@0: } michael@0: michael@0: @Override michael@0: protected void onHandleIntent(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(Constants.JSON_KEY_VERSION, 0); michael@0: long expectedVersion = SyncConstants.SYNC_ACCOUNT_DELETED_INTENT_VERSION; michael@0: if (intentVersion != expectedVersion) { michael@0: Logger.warn(LOG_TAG, "Intent malformed: version " + intentVersion + " given but version " + expectedVersion + "expected. " + michael@0: "Not cleaning up after deleted Account."); michael@0: return; michael@0: } michael@0: michael@0: String accountName = intent.getStringExtra(Constants.JSON_KEY_ACCOUNT); // Android Account name, not Sync encoded account name. michael@0: if (accountName == null) { michael@0: Logger.warn(LOG_TAG, "Intent malformed: no account name given. Not cleaning up after deleted Account."); michael@0: return; michael@0: } michael@0: michael@0: // Delete the Account pickle. michael@0: Logger.info(LOG_TAG, "Sync account named " + accountName + " being removed; " + michael@0: "deleting saved pickle file '" + Constants.ACCOUNT_PICKLE_FILENAME + "'."); michael@0: deletePickle(context); michael@0: michael@0: SyncAccountParameters params; michael@0: try { michael@0: String payload = intent.getStringExtra(Constants.JSON_KEY_PAYLOAD); michael@0: if (payload == null) { michael@0: Logger.warn(LOG_TAG, "Intent malformed: no payload given. Not deleting client record."); michael@0: return; michael@0: } michael@0: ExtendedJSONObject o = ExtendedJSONObject.parseJSONObject(payload); michael@0: params = new SyncAccountParameters(context, AccountManager.get(context), o); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception fetching account parameters from intent data; not deleting client record."); michael@0: return; michael@0: } michael@0: michael@0: // Bug 770785: delete the Account's client record. michael@0: Logger.info(LOG_TAG, "Account named " + accountName + " being removed; " + michael@0: "deleting client record from server."); michael@0: deleteClientRecord(context, accountName, params.password, params.serverURL); michael@0: } michael@0: michael@0: public static void deletePickle(final Context context) { michael@0: try { michael@0: AccountPickler.deletePickle(context, Constants.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: michael@0: public static void deleteClientRecord(final Context context, final String accountName, michael@0: final String password, final String serverURL) { michael@0: String encodedUsername; michael@0: try { michael@0: encodedUsername = Utils.usernameFromAccount(accountName); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Got exception deleting client record from server; ignoring.", e); michael@0: return; michael@0: } michael@0: michael@0: if (accountName == null || encodedUsername == null || password == null || serverURL == null) { michael@0: Logger.warn(LOG_TAG, "Account parameters were null; not deleting client record from server."); michael@0: return; michael@0: } michael@0: michael@0: // This is not exactly modular. We need to get some information about michael@0: // the account, namely the current clusterURL and client GUID, and we michael@0: // extract it by hand. We're not worried about the Account being michael@0: // deleted out from under us since the prefs remain even after Account michael@0: // deletion. michael@0: final String product = GlobalConstants.BROWSER_INTENT_PACKAGE; michael@0: final String profile = Constants.DEFAULT_PROFILE; michael@0: final long version = SyncConfiguration.CURRENT_PREFS_VERSION; michael@0: michael@0: SharedPreferences prefs; michael@0: try { michael@0: prefs = Utils.getSharedPreferences(context, product, encodedUsername, serverURL, profile, version); michael@0: } catch (Exception e) { michael@0: Logger.warn(LOG_TAG, "Caught exception fetching preferences; not deleting client record from server.", e); michael@0: return; michael@0: } michael@0: michael@0: try { michael@0: final String clientGUID = prefs.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null); michael@0: if (clientGUID == null) { michael@0: Logger.warn(LOG_TAG, "Client GUID was null; not deleting client record from server."); michael@0: return; michael@0: } michael@0: michael@0: BasicAuthHeaderProvider authHeaderProvider = new BasicAuthHeaderProvider(encodedUsername, password); michael@0: SyncConfiguration configuration = new Sync11Configuration(encodedUsername, authHeaderProvider, prefs); michael@0: if (configuration.getClusterURL() == null) { michael@0: Logger.warn(LOG_TAG, "Cluster URL was null; not deleting client record from server."); michael@0: return; michael@0: } michael@0: michael@0: try { michael@0: ClientRecordTerminator.deleteClientRecord(configuration, clientGUID); 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 client record from server; ignoring.", e); michael@0: } michael@0: } finally { michael@0: // Finally, a good place to do this. michael@0: prefs.edit().clear().commit(); michael@0: } michael@0: } michael@0: }