Clear session cookies before attempting pre-login.

If ClientLogin issues session cookies, we do not want to clear them immediately
after getting them from login.  If we are not going to restore tabs, go ahead
and clear the cookies before attempting pre-login.  Keep track of the tab to
restore so that we don't need to figure it out again.  Requires a change in
frameworks/base that exposes the CookieManager api.

If we receive a 403 from IssueTokenAuth, inval the auth tokens and try again.

Bug: 3421214
Change-Id: I5dd4cc0eba365a20a731ac43dd2571ef6274eaa9
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 050ad94..af9928a 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -287,7 +287,42 @@
     }
 
     /**
+     * Check if the state can be restored.  If the state can be restored, the
+     * current tab index is returned.  This can be passed to restoreState below
+     * in order to restore the correct tab.  Otherwise, -1 is returned and the
+     * state cannot be restored.
+     */
+    int canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
+        final int numTabs = (inState == null)
+                ? - 1 : inState.getInt(Tab.NUMTABS, -1);
+        if (numTabs == -1) {
+            return -1;
+        }
+        final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1);
+
+        // Determine whether the saved current tab can be restored, and if not,
+        // which tab will take its place.
+        int currentTab = -1;
+        if (restoreIncognitoTabs ||
+                !inState.getBundle(Tab.WEBVIEW + oldCurrentTab)
+                .getBoolean(Tab.INCOGNITO)) {
+            currentTab = oldCurrentTab;
+        } else {
+            for (int i = 0; i < numTabs; i++) {
+                if (!inState.getBundle(Tab.WEBVIEW + i)
+                        .getBoolean(Tab.INCOGNITO)) {
+                    currentTab = i;
+                    break;
+                }
+            }
+        }
+
+        return currentTab;
+    }
+
+    /**
      * Restore the state of all the tabs.
+     * @param currentTab The tab index to restore.
      * @param inState The saved state of all the tabs.
      * @param restoreIncognitoTabs Restoring private browsing tabs
      * @param restoreAll All webviews get restored, not just the current tab
@@ -295,89 +330,69 @@
      * @return True if there were previous tabs that were restored. False if
      *         there was no saved state or restoring the state failed.
      */
-    boolean restoreState(Bundle inState, boolean restoreIncognitoTabs,
-            boolean restoreAll) {
-        final int numTabs = (inState == null)
-                ? -1 : inState.getInt(Tab.NUMTABS, -1);
-        if (numTabs == -1) {
-            return false;
-        } else {
-            final int oldCurrentTab = inState.getInt(Tab.CURRTAB, -1);
+    void restoreState(Bundle inState, int currentTab,
+            boolean restoreIncognitoTabs, boolean restoreAll) {
+        if (currentTab == -1) {
+            return;
+        }
 
-            // Determine whether the saved current tab can be restored, and
-            // if not, which tab will take its place.
-            int currentTab = -1;
-            if (restoreIncognitoTabs
-                    || !inState.getBundle(Tab.WEBVIEW + oldCurrentTab).getBoolean(Tab.INCOGNITO)) {
-                currentTab = oldCurrentTab;
+        // If currentTab is valid, numTabs must be present.
+        final int numTabs = inState.getInt(Tab.NUMTABS, -1);
+
+        // Map saved tab indices to new indices, in case any incognito tabs
+        // need to not be restored.
+        HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>();
+        originalTabIndices.put(-1, -1);
+        for (int i = 0; i < numTabs; i++) {
+            Bundle state = inState.getBundle(Tab.WEBVIEW + i);
+
+            if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
+                originalTabIndices.put(i, -1);
+            } else if (i == currentTab || restoreAll) {
+                Tab t = createNewTab();
+                // Me must set the current tab before restoring the state
+                // so that all the client classes are set.
+                if (i == currentTab) {
+                    setCurrentTab(t);
+                }
+                if (!t.restoreState(state)) {
+                    Log.w(LOGTAG, "Fail in restoreState, load home page.");
+                    t.getWebView().loadUrl(BrowserSettings.getInstance()
+                            .getHomePage());
+                }
+                originalTabIndices.put(i, getTabCount() - 1);
             } else {
-                for (int i = 0; i < numTabs; i++) {
-                    if (!inState.getBundle(Tab.WEBVIEW + i).getBoolean(Tab.INCOGNITO)) {
-                        currentTab = i;
-                        break;
-                    }
+                // Create a new tab and don't restore the state yet, add it
+                // to the tab list
+                Tab t = new Tab(mController, null, false, null, null);
+                if (state != null) {
+                    t.setSavedState(state);
+                    // Need to maintain the app id and original url so we
+                    // can possibly reuse this tab.
+                    t.setAppId(state.getString(Tab.APPID));
                 }
+                mTabs.add(t);
+                // added the tab to the front as they are not current
+                mTabQueue.add(0, t);
+                originalTabIndices.put(i, getTabCount() - 1);
             }
-            if (currentTab < 0) {
-                return false;
-            }
+        }
 
-            // Map saved tab indices to new indices, in case any incognito tabs
-            // need to not be restored.
-            HashMap<Integer, Integer> originalTabIndices = new HashMap<Integer, Integer>();
-            originalTabIndices.put(-1, -1);
-            for (int i = 0; i < numTabs; i++) {
-                Bundle state = inState.getBundle(Tab.WEBVIEW + i);
-
-                if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
-                    originalTabIndices.put(i, -1);
-                } else if (i == currentTab || restoreAll) {
-                    Tab t = createNewTab();
-                    // Me must set the current tab before restoring the state
-                    // so that all the client classes are set.
-                    if (i == currentTab) {
-                        setCurrentTab(t);
-                    }
-                    if (!t.restoreState(state)) {
-                        Log.w(LOGTAG, "Fail in restoreState, load home page.");
-                        t.getWebView().loadUrl(BrowserSettings.getInstance()
-                                .getHomePage());
-                    }
-                    originalTabIndices.put(i, getTabCount() - 1);
-                } else {
-                    // Create a new tab and don't restore the state yet, add it
-                    // to the tab list
-                    Tab t = new Tab(mController, null, false, null, null);
-                    if (state != null) {
-                        t.setSavedState(state);
-                        // Need to maintain the app id and original url so we
-                        // can possibly reuse this tab.
-                        t.setAppId(state.getString(Tab.APPID));
-                    }
-                    mTabs.add(t);
-                    // added the tab to the front as they are not current
-                    mTabQueue.add(0, t);
-                    originalTabIndices.put(i, getTabCount() - 1);
-                }
-            }
-
-            // Rebuild the tree of tabs. Do this after all tabs have been
-            // created/restored so that the parent tab exists.
-            for (int i = 0; i < numTabs; i++) {
-                final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
-                final Tab t = getTab(i);
-                if (b != null && t != null) {
-                    final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1));
-                    if (parentIndex != -1) {
-                        final Tab parent = getTab(parentIndex);
-                        if (parent != null) {
-                            parent.addChildTab(t);
-                        }
+        // Rebuild the tree of tabs. Do this after all tabs have been
+        // created/restored so that the parent tab exists.
+        for (int i = 0; i < numTabs; i++) {
+            final Bundle b = inState.getBundle(Tab.WEBVIEW + i);
+            final Tab t = getTab(i);
+            if (b != null && t != null) {
+                final Integer parentIndex = originalTabIndices.get(b.getInt(Tab.PARENTTAB, -1));
+                if (parentIndex != -1) {
+                    final Tab parent = getTab(parentIndex);
+                    if (parent != null) {
+                        parent.addChildTab(t);
                     }
                 }
             }
         }
-        return true;
     }
 
     /**