Merge "Remove preloaded URLs from back stack when preloading fails."
diff --git a/src/com/android/browser/PreloadedTabControl.java b/src/com/android/browser/PreloadedTabControl.java
index d2482a4..824c00b 100644
--- a/src/com/android/browser/PreloadedTabControl.java
+++ b/src/com/android/browser/PreloadedTabControl.java
@@ -82,8 +82,9 @@
                 if (!called) {
                     if (LOGD_ENABLED) Log.d(LOGTAG, "Query not submitted; falling back");
                     loadUrl(fallbackUrl, fallbackHeaders);
+                    // make sure that the failed, preloaded URL is cleared from the back stack
+                    mTab.clearBackStackWhenItemAdded(fallbackUrl);
                 }
-                mTab.getWebView().clearHistory();
             }});
         return true;
     }
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 6aa8683..a251212 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -90,6 +90,7 @@
 
     // Log Tag
     private static final String LOGTAG = "Tab";
+    private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
     // Special case the logtag for messages for the Console to make it easier to
     // filter them and match the logtag used for these messages in older versions
     // of the browser.
@@ -166,6 +167,11 @@
     private Bitmap mCapture;
     private Handler mHandler;
 
+    /**
+     * See {@link #clearBackStackWhenItemAdded(String)}.
+     */
+    private String mClearHistoryMatchUrl;
+
     private static synchronized Bitmap getDefaultFavicon(Context context) {
         if (sDefaultFavicon == null) {
             sDefaultFavicon = BitmapFactory.decodeResource(
@@ -1398,6 +1404,18 @@
                 if (isInVoiceSearchMode()) {
                     item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
                 }
+                if (mClearHistoryMatchUrl != null) {
+                    if (LOGD_ENABLED) {
+                        Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
+                                + mClearHistoryMatchUrl);
+                    }
+                    if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
+                        if (mMainView != null) {
+                            mMainView.clearHistory();
+                        }
+                    }
+                    mClearHistoryMatchUrl = null;
+                }
             }
             @Override
             public void onIndexChanged(WebHistoryItem item, int index) {
@@ -1416,6 +1434,7 @@
         restoreState(state);
         setWebView(w);
         mHandler = new Handler() {
+            @Override
             public void handleMessage(Message m) {
                 switch (m.what) {
                 case MSG_CAPTURE:
@@ -2029,6 +2048,17 @@
         }
     }
 
+    /**
+     * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
+     * to be added to the stack.
+     *
+     * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
+     * not appear in the back stack.
+     */
+    public void clearBackStackWhenItemAdded(String urlToMatch) {
+        mClearHistoryMatchUrl = urlToMatch;
+    }
+
     protected void persistThumbnail() {
         BackgroundHandler.execute(mSaveThumbnail);
     }