mobile/android/base/sync/receivers/SyncAccountDeletedService.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.sync.receivers;
michael@0 6
michael@0 7 import org.mozilla.gecko.sync.ExtendedJSONObject;
michael@0 8 import org.mozilla.gecko.background.common.GlobalConstants;
michael@0 9 import org.mozilla.gecko.background.common.log.Logger;
michael@0 10 import org.mozilla.gecko.sync.Sync11Configuration;
michael@0 11 import org.mozilla.gecko.sync.SyncConstants;
michael@0 12 import org.mozilla.gecko.sync.SyncConfiguration;
michael@0 13 import org.mozilla.gecko.sync.Utils;
michael@0 14 import org.mozilla.gecko.sync.config.AccountPickler;
michael@0 15 import org.mozilla.gecko.sync.config.ClientRecordTerminator;
michael@0 16 import org.mozilla.gecko.sync.net.BasicAuthHeaderProvider;
michael@0 17 import org.mozilla.gecko.sync.setup.Constants;
michael@0 18 import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters;
michael@0 19
michael@0 20 import android.accounts.AccountManager;
michael@0 21 import android.app.IntentService;
michael@0 22 import android.content.Context;
michael@0 23 import android.content.Intent;
michael@0 24 import android.content.SharedPreferences;
michael@0 25
michael@0 26 public class SyncAccountDeletedService extends IntentService {
michael@0 27 public static final String LOG_TAG = "SyncAccountDeletedService";
michael@0 28
michael@0 29 public SyncAccountDeletedService() {
michael@0 30 super(LOG_TAG);
michael@0 31 }
michael@0 32
michael@0 33 @Override
michael@0 34 protected void onHandleIntent(Intent intent) {
michael@0 35 // Intent can, in theory, be null. Bug 1025937.
michael@0 36 if (intent == null) {
michael@0 37 Logger.debug(LOG_TAG, "Short-circuiting on null intent.");
michael@0 38 return;
michael@0 39 }
michael@0 40
michael@0 41 final Context context = this;
michael@0 42
michael@0 43 long intentVersion = intent.getLongExtra(Constants.JSON_KEY_VERSION, 0);
michael@0 44 long expectedVersion = SyncConstants.SYNC_ACCOUNT_DELETED_INTENT_VERSION;
michael@0 45 if (intentVersion != expectedVersion) {
michael@0 46 Logger.warn(LOG_TAG, "Intent malformed: version " + intentVersion + " given but version " + expectedVersion + "expected. " +
michael@0 47 "Not cleaning up after deleted Account.");
michael@0 48 return;
michael@0 49 }
michael@0 50
michael@0 51 String accountName = intent.getStringExtra(Constants.JSON_KEY_ACCOUNT); // Android Account name, not Sync encoded account name.
michael@0 52 if (accountName == null) {
michael@0 53 Logger.warn(LOG_TAG, "Intent malformed: no account name given. Not cleaning up after deleted Account.");
michael@0 54 return;
michael@0 55 }
michael@0 56
michael@0 57 // Delete the Account pickle.
michael@0 58 Logger.info(LOG_TAG, "Sync account named " + accountName + " being removed; " +
michael@0 59 "deleting saved pickle file '" + Constants.ACCOUNT_PICKLE_FILENAME + "'.");
michael@0 60 deletePickle(context);
michael@0 61
michael@0 62 SyncAccountParameters params;
michael@0 63 try {
michael@0 64 String payload = intent.getStringExtra(Constants.JSON_KEY_PAYLOAD);
michael@0 65 if (payload == null) {
michael@0 66 Logger.warn(LOG_TAG, "Intent malformed: no payload given. Not deleting client record.");
michael@0 67 return;
michael@0 68 }
michael@0 69 ExtendedJSONObject o = ExtendedJSONObject.parseJSONObject(payload);
michael@0 70 params = new SyncAccountParameters(context, AccountManager.get(context), o);
michael@0 71 } catch (Exception e) {
michael@0 72 Logger.warn(LOG_TAG, "Got exception fetching account parameters from intent data; not deleting client record.");
michael@0 73 return;
michael@0 74 }
michael@0 75
michael@0 76 // Bug 770785: delete the Account's client record.
michael@0 77 Logger.info(LOG_TAG, "Account named " + accountName + " being removed; " +
michael@0 78 "deleting client record from server.");
michael@0 79 deleteClientRecord(context, accountName, params.password, params.serverURL);
michael@0 80 }
michael@0 81
michael@0 82 public static void deletePickle(final Context context) {
michael@0 83 try {
michael@0 84 AccountPickler.deletePickle(context, Constants.ACCOUNT_PICKLE_FILENAME);
michael@0 85 } catch (Exception e) {
michael@0 86 // This should never happen, but we really don't want to die in a background thread.
michael@0 87 Logger.warn(LOG_TAG, "Got exception deleting saved pickle file; ignoring.", e);
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 public static void deleteClientRecord(final Context context, final String accountName,
michael@0 92 final String password, final String serverURL) {
michael@0 93 String encodedUsername;
michael@0 94 try {
michael@0 95 encodedUsername = Utils.usernameFromAccount(accountName);
michael@0 96 } catch (Exception e) {
michael@0 97 Logger.warn(LOG_TAG, "Got exception deleting client record from server; ignoring.", e);
michael@0 98 return;
michael@0 99 }
michael@0 100
michael@0 101 if (accountName == null || encodedUsername == null || password == null || serverURL == null) {
michael@0 102 Logger.warn(LOG_TAG, "Account parameters were null; not deleting client record from server.");
michael@0 103 return;
michael@0 104 }
michael@0 105
michael@0 106 // This is not exactly modular. We need to get some information about
michael@0 107 // the account, namely the current clusterURL and client GUID, and we
michael@0 108 // extract it by hand. We're not worried about the Account being
michael@0 109 // deleted out from under us since the prefs remain even after Account
michael@0 110 // deletion.
michael@0 111 final String product = GlobalConstants.BROWSER_INTENT_PACKAGE;
michael@0 112 final String profile = Constants.DEFAULT_PROFILE;
michael@0 113 final long version = SyncConfiguration.CURRENT_PREFS_VERSION;
michael@0 114
michael@0 115 SharedPreferences prefs;
michael@0 116 try {
michael@0 117 prefs = Utils.getSharedPreferences(context, product, encodedUsername, serverURL, profile, version);
michael@0 118 } catch (Exception e) {
michael@0 119 Logger.warn(LOG_TAG, "Caught exception fetching preferences; not deleting client record from server.", e);
michael@0 120 return;
michael@0 121 }
michael@0 122
michael@0 123 try {
michael@0 124 final String clientGUID = prefs.getString(SyncConfiguration.PREF_ACCOUNT_GUID, null);
michael@0 125 if (clientGUID == null) {
michael@0 126 Logger.warn(LOG_TAG, "Client GUID was null; not deleting client record from server.");
michael@0 127 return;
michael@0 128 }
michael@0 129
michael@0 130 BasicAuthHeaderProvider authHeaderProvider = new BasicAuthHeaderProvider(encodedUsername, password);
michael@0 131 SyncConfiguration configuration = new Sync11Configuration(encodedUsername, authHeaderProvider, prefs);
michael@0 132 if (configuration.getClusterURL() == null) {
michael@0 133 Logger.warn(LOG_TAG, "Cluster URL was null; not deleting client record from server.");
michael@0 134 return;
michael@0 135 }
michael@0 136
michael@0 137 try {
michael@0 138 ClientRecordTerminator.deleteClientRecord(configuration, clientGUID);
michael@0 139 } catch (Exception e) {
michael@0 140 // This should never happen, but we really don't want to die in a background thread.
michael@0 141 Logger.warn(LOG_TAG, "Got exception deleting client record from server; ignoring.", e);
michael@0 142 }
michael@0 143 } finally {
michael@0 144 // Finally, a good place to do this.
michael@0 145 prefs.edit().clear().commit();
michael@0 146 }
michael@0 147 }
michael@0 148 }

mercurial