Bug 1288816 - Do the RecentTabsPanel wait from a separate thread. r=jchen, a=gchang
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 23 Jul 2016 15:28:04 +0200
changeset 340168 07f644f906ca21016f8f6ebbb6e6463af5d88fea
parent 340167 04a6cd9d45f98ce74288587083bf8e281c4acdbe
child 340169 74d0f7c374dca39a6863ceb5083c2fb36c13f43c
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen, gchang
bugs1288816
milestone49.0a2
Bug 1288816 - Do the RecentTabsPanel wait from a separate thread. r=jchen, a=gchang When reading the previous session file, the RecentTabsPanel waits for GeckoApp (initialisation runs on the main thread) to have actually moved the previous session file to its correct location (happens from the background thread). Hence we can't do the wait from either of those two threads, because we risk a deadlock otherwise if the RecentTabsPanel happens to be initialised *before* GeckoApp. MozReview-Commit-ID: 54x89xHi9b9
mobile/android/base/java/org/mozilla/gecko/home/RecentTabsPanel.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsPanel.java
@@ -212,19 +212,43 @@ public class RecentTabsPanel extends Hom
 
             final TextView emptyText = (TextView) mEmptyView.findViewById(R.id.home_empty_text);
             emptyText.setText(R.string.home_last_tabs_empty);
 
             mList.setEmptyView(mEmptyView);
         }
     }
 
+    private void updateCursor(final boolean initialLoad) {
+        Thread updateCursorThread = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                // We need to ensure that the session restore code has updated sessionstore.bak as necessary.
+                GeckoProfile.get(getContext()).waitForOldSessionDataProcessing();
+
+                ThreadUtils.postToUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        // Reload the cursor to show recently closed tabs.
+                        if (initialLoad) {
+                            getLoaderManager().initLoader(LOADER_ID_RECENT_TABS, null, mCursorLoaderCallbacks);
+                        } else {
+                            getLoaderManager().restartLoader(LOADER_ID_RECENT_TABS, null, mCursorLoaderCallbacks);
+                        }
+                    }
+                });
+            }
+        }, "RecentTabsCursorThread");
+
+        updateCursorThread.start();
+    }
+
     @Override
     protected void load() {
-        getLoaderManager().initLoader(LOADER_ID_RECENT_TABS, null, mCursorLoaderCallbacks);
+        updateCursor(true);
     }
 
     @Override
     public void handleMessage(String event, NativeJSObject message, EventCallback callback) {
         final NativeJSObject[] tabs = message.getObjectArray("tabs");
         final int length = tabs.length;
 
         final ClosedTab[] closedTabs = new ClosedTab[length];
@@ -237,18 +261,17 @@ public class RecentTabsPanel extends Hom
         ThreadUtils.postToUiThread(new Runnable() {
             @Override
             public void run() {
                 mClosedTabs = closedTabs;
 
                 // The fragment might have been detached before this code
                 // runs in the UI thread.
                 if (getActivity() != null) {
-                    // Reload the cursor to show recently closed tabs.
-                    getLoaderManager().restartLoader(LOADER_ID_RECENT_TABS, null, mCursorLoaderCallbacks);
+                    updateCursor(false);
                 }
             }
         });
     }
 
     private void openTabsWithType(int type) {
         final Cursor c = mAdapter.getCursor();
         if (c == null || !c.moveToFirst()) {
@@ -324,19 +347,16 @@ public class RecentTabsPanel extends Hom
                 }
 
                 // Add an "Open all" button if more than 2 tabs were added to the list.
                 if (visibleClosedTabs > 1) {
                     addRow(c, null, null, RecentTabs.TYPE_OPEN_ALL_CLOSED, null);
                 }
             }
 
-            // We need to ensure that the session restore code has updated sessionstore.bak as necessary.
-            GeckoProfile.get(context).waitForOldSessionDataProcessing();
-
             final String jsonString = GeckoProfile.get(context).readSessionFile(true);
             if (jsonString == null) {
                 // No previous session data
                 return c;
             }
 
             final int count = c.getCount();