Bug 993885 - Refactor SendTabActivity to avoid a race condition. r=mcomella, a=sledru
authorRichard Newman <rnewman@mozilla.com>
Wed, 27 Aug 2014 16:04:12 -0700
changeset 216652 764591e4e7f3
parent 216651 9eb4dff42df0
child 216653 7cf512b6b64c
push id3861
push userryanvm@gmail.com
push date2014-09-03 15:49 +0000
treeherdermozilla-beta@4815ff146c57 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella, sledru
bugs993885
milestone33.0
Bug 993885 - Refactor SendTabActivity to avoid a race condition. r=mcomella, a=sledru
mobile/android/base/sync/setup/activities/SendTabActivity.java
--- a/mobile/android/base/sync/setup/activities/SendTabActivity.java
+++ b/mobile/android/base/sync/setup/activities/SendTabActivity.java
@@ -141,18 +141,17 @@ public class SendTabActivity extends Loc
     TextView textView = (TextView) findViewById(R.id.title);
     textView.setText(sendTabData.title);
 
     textView = (TextView) findViewById(R.id.uri);
     textView.setText(sendTabData.uri);
 
     enableSend(false);
 
-    // will enableSend if appropriate.
-    updateClientList();
+    // Sending will be enabled in onResume, if appropriate.
   }
 
   protected static SendTabData getSendTabData(Intent intent) throws IllegalArgumentException {
     if (intent == null) {
       Logger.warn(LOG_TAG, "intent was null; aborting without sending tab.");
       throw new IllegalArgumentException();
     }
 
@@ -179,37 +178,37 @@ public class SendTabActivity extends Loc
 
     return sendTabData;
   }
 
   /**
    * Ensure that the view's list of clients is backed by a recently populated
    * array adapter.
    */
-  protected synchronized void updateClientList() {
+  protected synchronized void updateClientList(final TabSender sender, final ClientRecordArrayAdapter adapter) {
     // Fetching the client list hits the clients database, so we spin this onto
     // a background task.
     new AsyncTask<Void, Void, Collection<ClientRecord>>() {
 
       @Override
       protected Collection<ClientRecord> doInBackground(Void... params) {
-        return getOtherClients();
+        return getOtherClients(sender);
       }
 
       @Override
       protected void onPostExecute(final Collection<ClientRecord> clientArray) {
         // We're allowed to update the UI from here.
 
         Logger.debug(LOG_TAG, "Got " + clientArray.size() + " clients.");
-        arrayAdapter.setClientRecordList(clientArray);
+        adapter.setClientRecordList(clientArray);
         if (clientArray.size() == 1) {
-          arrayAdapter.checkItem(0, true);
+          adapter.checkItem(0, true);
         }
 
-        enableSend(arrayAdapter.getNumCheckedGUIDs() > 0);
+        enableSend(adapter.getNumCheckedGUIDs() > 0);
       }
     }.execute();
   }
 
   @Override
   public void onResume() {
     ActivityUtils.prepareLogging();
     Logger.info(LOG_TAG, "Called SendTabActivity.onResume.");
@@ -230,25 +229,31 @@ public class SendTabActivity extends Loc
         Logger.warn(LOG_TAG, "Firefox Account named like " + fxAccount.getObfuscatedEmail() +
             " needs action before it can send a tab; redirecting to status activity.");
         redirectToNewTask(FxAccountStatusActivity.class, false);
         return;
       }
 
       this.tabSender = new FxAccountTabSender(applicationContext, fxAccount);
 
+      // will enableSend if appropriate.
+      updateClientList(tabSender, this.arrayAdapter);
+
       Logger.info(LOG_TAG, "Allowing tab send for Firefox Account.");
       registerDisplayURICommand();
       return;
     }
 
     final Account[] syncAccounts = accountManager.getAccountsByType(SyncConstants.ACCOUNTTYPE_SYNC);
     if (syncAccounts.length > 0) {
       this.tabSender = new Sync11TabSender(applicationContext, syncAccounts[0], accountManager);
 
+      // will enableSend if appropriate.
+      updateClientList(tabSender, this.arrayAdapter);
+
       Logger.info(LOG_TAG, "Allowing tab send for Sync account.");
       registerDisplayURICommand();
       return;
     }
 
     // Offer to set up a Firefox Account, and finish this activity.
     redirectToNewTask(FxAccountGetStartedActivity.class, false);
   }
@@ -355,28 +360,28 @@ public class SendTabActivity extends Loc
     } finally {
       db.close();
     }
   }
 
   /**
    * @return a collection of client records, excluding our own.
    */
-  protected Collection<ClientRecord> getOtherClients() {
+  protected Collection<ClientRecord> getOtherClients(final TabSender sender) {
+    if (sender == null) {
+      Logger.warn(LOG_TAG, "No tab sender when fetching other client IDs.");
+      return new ArrayList<ClientRecord>(0);
+    }
+
     final Map<String, ClientRecord> all = getAllClients();
     if (all == null) {
       return new ArrayList<ClientRecord>(0);
     }
 
-    if (this.tabSender == null) {
-      Logger.warn(LOG_TAG, "No tab sender when fetching other client IDs.");
-      return new ArrayList<ClientRecord>(0);
-    }
-
-    final String ourGUID = this.tabSender.getAccountGUID();
+    final String ourGUID = sender.getAccountGUID();
     if (ourGUID == null) {
       return all.values();
     }
 
     final ArrayList<ClientRecord> out = new ArrayList<ClientRecord>(all.size());
     for (Entry<String, ClientRecord> entry : all.entrySet()) {
       if (ourGUID.equals(entry.getKey())) {
         continue;