Bug 830270 - Remove synced tabs on Sync account deletion. r=rnewman
authorVivek Balakrishnan <vivekb.balakrishnan@gmail.com>
Wed, 30 Jul 2014 08:19:34 -0700
changeset 196827 7614df67484e7190d444faf712ac7b55f4304708
parent 196826 cbe49d980c05bbed628650664231f539ec3c8cb7
child 196828 d4dc5916f039422513f267ead8ab21293ed50e34
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersrnewman
bugs830270
milestone34.0a1
Bug 830270 - Remove synced tabs on Sync account deletion. r=rnewman
mobile/android/base/sync/receivers/SyncAccountDeletedService.java
mobile/android/base/sync/repositories/android/FennecTabsRepository.java
--- a/mobile/android/base/sync/receivers/SyncAccountDeletedService.java
+++ b/mobile/android/base/sync/receivers/SyncAccountDeletedService.java
@@ -9,16 +9,17 @@ import org.mozilla.gecko.background.comm
 import org.mozilla.gecko.background.common.log.Logger;
 import org.mozilla.gecko.sync.Sync11Configuration;
 import org.mozilla.gecko.sync.SyncConstants;
 import org.mozilla.gecko.sync.SyncConfiguration;
 import org.mozilla.gecko.sync.Utils;
 import org.mozilla.gecko.sync.config.AccountPickler;
 import org.mozilla.gecko.sync.config.ClientRecordTerminator;
 import org.mozilla.gecko.sync.net.BasicAuthHeaderProvider;
+import org.mozilla.gecko.sync.repositories.android.FennecTabsRepository;
 import org.mozilla.gecko.sync.setup.Constants;
 import org.mozilla.gecko.sync.setup.SyncAccounts.SyncAccountParameters;
 
 import android.accounts.AccountManager;
 import android.app.IntentService;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
@@ -72,16 +73,20 @@ public class SyncAccountDeletedService e
       Logger.warn(LOG_TAG, "Got exception fetching account parameters from intent data; not deleting client record.");
       return;
     }
 
     // Bug 770785: delete the Account's client record.
     Logger.info(LOG_TAG, "Account named " + accountName + " being removed; " +
         "deleting client record from server.");
     deleteClientRecord(context, accountName, params.password, params.serverURL);
+
+    // Delete client database and non-local tabs.
+    Logger.info(LOG_TAG, "Deleting the entire clients database and non-local tabs");
+    FennecTabsRepository.deleteNonLocalClientsAndTabs(context);
   }
 
   public static void deletePickle(final Context context) {
     try {
       AccountPickler.deletePickle(context, Constants.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);
--- a/mobile/android/base/sync/repositories/android/FennecTabsRepository.java
+++ b/mobile/android/base/sync/repositories/android/FennecTabsRepository.java
@@ -28,16 +28,18 @@ import org.mozilla.gecko.sync.repositori
 import android.content.ContentProviderClient;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.RemoteException;
 
 public class FennecTabsRepository extends Repository {
+  private static final String LOG_TAG = "FennecTabsRepository";
+
   protected final ClientsDataDelegate clientsDataDelegate;
 
   public FennecTabsRepository(ClientsDataDelegate clientsDataDelegate) {
     this.clientsDataDelegate = clientsDataDelegate;
   }
 
   /**
    * Note that -- unlike most repositories -- this will only fetch Fennec's tabs,
@@ -346,9 +348,40 @@ public class FennecTabsRepository extend
         cursor.moveToNext();
       }
     } finally {
       cursor.moveToPosition(position);
     }
 
     return record;
   }
+
+  /**
+   * Deletes all non-local clients and remote tabs.
+   *
+   * This function doesn't delete non-local clients due to bug in TabsProvider. Refer Bug 1025128.
+   *
+   * Upon remote tabs deletion, the clients without tabs are not shown in UI.
+   */
+  public static void deleteNonLocalClientsAndTabs(Context context) {
+    final String nonLocalTabsSelection = BrowserContract.Tabs.CLIENT_GUID + " IS NOT NULL";
+
+    ContentProviderClient tabsProvider = context.getContentResolver()
+            .acquireContentProviderClient(BrowserContractHelpers.TABS_CONTENT_URI);
+    if (tabsProvider == null) {
+        Logger.warn(LOG_TAG, "Unable to create tabsProvider!");
+        return;
+    }
+
+    try {
+      Logger.info(LOG_TAG, "Clearing all non-local tabs for default profile.");
+      tabsProvider.delete(BrowserContractHelpers.TABS_CONTENT_URI, nonLocalTabsSelection, null);
+    } catch (RemoteException e) {
+      Logger.warn(LOG_TAG, "Error while deleting", e);
+    } finally {
+      try {
+        tabsProvider.release();
+      } catch (Exception e) {
+        Logger.warn(LOG_TAG, "Got exception releasing tabsProvider!", e);
+      }
+    }
+  }
 }