Add a client for WebBackForwardList operations.

Provide a custom data slot in WebHistoryItem to store application data in the
history item. A client of WebBackForwardList can insert their custom data into
the item when created. In onIndexChanged, the custom data can be read by the
client.
diff --git a/core/java/android/webkit/CallbackProxy.java b/core/java/android/webkit/CallbackProxy.java
index 61a2d2ef..a013a9b 100644
--- a/core/java/android/webkit/CallbackProxy.java
+++ b/core/java/android/webkit/CallbackProxy.java
@@ -69,6 +69,8 @@
     private volatile int mLatestProgress = 100;
     // Back/Forward list
     private final WebBackForwardList mBackForwardList;
+    // Back/Forward list client
+    private volatile WebBackForwardListClient mWebBackForwardListClient;
     // Used to call startActivity during url override.
     private final Context mContext;
 
@@ -107,6 +109,8 @@
     private static final int RECEIVED_TOUCH_ICON_URL             = 132;
     private static final int GET_VISITED_HISTORY                 = 133;
     private static final int OPEN_FILE_CHOOSER                   = 134;
+    private static final int ADD_HISTORY_ITEM                    = 135;
+    private static final int HISTORY_INDEX_CHANGED               = 136;
 
     // Message triggered by the client to resume execution
     private static final int NOTIFY                              = 200;
@@ -137,7 +141,7 @@
         // Used to start a default activity.
         mContext = context;
         mWebView = w;
-        mBackForwardList = new WebBackForwardList();
+        mBackForwardList = new WebBackForwardList(this);
     }
 
     /**
@@ -190,6 +194,14 @@
         return mBackForwardList;
     }
 
+    void setWebBackForwardListClient(WebBackForwardListClient client) {
+        mWebBackForwardListClient = client;
+    }
+
+    WebBackForwardListClient getWebBackForwardListClient() {
+        return mWebBackForwardListClient;
+    }
+
     /**
      * Called by the UI side.  Calling overrideUrlLoading from the WebCore
      * side will post a message to call this method.
@@ -700,6 +712,20 @@
                     mWebChromeClient.openFileChooser((UploadFile) msg.obj);
                 }
                 break;
+
+            case ADD_HISTORY_ITEM:
+                if (mWebBackForwardListClient != null) {
+                    mWebBackForwardListClient.onNewHistoryItem(
+                            (WebHistoryItem) msg.obj);
+                }
+                break;
+
+            case HISTORY_INDEX_CHANGED:
+                if (mWebBackForwardListClient != null) {
+                    mWebBackForwardListClient.onIndexChanged(
+                            (WebHistoryItem) msg.obj, msg.arg1);
+                }
+                break;
         }
     }
 
@@ -1400,4 +1426,20 @@
         }
         return uploadFile.getResult();
     }
+
+    void onNewHistoryItem(WebHistoryItem item) {
+        if (mWebBackForwardListClient == null) {
+            return;
+        }
+        Message msg = obtainMessage(ADD_HISTORY_ITEM, item);
+        sendMessage(msg);
+    }
+
+    void onIndexChanged(WebHistoryItem item, int index) {
+        if (mWebBackForwardListClient == null) {
+            return;
+        }
+        Message msg = obtainMessage(HISTORY_INDEX_CHANGED, index, 0, item);
+        sendMessage(msg);
+    }
 }
diff --git a/core/java/android/webkit/WebBackForwardList.java b/core/java/android/webkit/WebBackForwardList.java
index 62a5531..79e634e 100644
--- a/core/java/android/webkit/WebBackForwardList.java
+++ b/core/java/android/webkit/WebBackForwardList.java
@@ -31,13 +31,16 @@
     private ArrayList<WebHistoryItem> mArray;
     // Flag to indicate that the list is invalid
     private boolean mClearPending;
+    // CallbackProxy to issue client callbacks.
+    private final CallbackProxy mCallbackProxy;
 
     /**
      * Construct a back/forward list used by clients of WebView.
      */
-    /*package*/ WebBackForwardList() {
+    /*package*/ WebBackForwardList(CallbackProxy proxy) {
         mCurrentIndex = -1;
         mArray = new ArrayList<WebHistoryItem>();
+        mCallbackProxy = proxy;
     }
 
     /**
@@ -116,6 +119,9 @@
         }
         // Add the item to the list.
         mArray.add(item);
+        if (mCallbackProxy != null) {
+            mCallbackProxy.onNewHistoryItem(item);
+        }
     }
 
     /**
@@ -152,7 +158,7 @@
      * webkit package classes.
      */
     protected synchronized WebBackForwardList clone() {
-        WebBackForwardList l = new WebBackForwardList();
+        WebBackForwardList l = new WebBackForwardList(null);
         if (mClearPending) {
             // If a clear is pending, return a copy with only the current item.
             l.addHistoryItem(getCurrentItem());
@@ -174,6 +180,9 @@
      */
     /*package*/ synchronized void setCurrentIndex(int newIndex) {
         mCurrentIndex = newIndex;
+        if (mCallbackProxy != null) {
+            mCallbackProxy.onIndexChanged(getItemAtIndex(newIndex), newIndex);
+        }
     }
 
     /**
diff --git a/core/java/android/webkit/WebBackForwardListClient.java b/core/java/android/webkit/WebBackForwardListClient.java
new file mode 100644
index 0000000..7fe9281
--- /dev/null
+++ b/core/java/android/webkit/WebBackForwardListClient.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.webkit;
+
+/**
+ * Interface to receive notifications when items are added to the
+ * {@link WebBackForwardList}.
+ * {@hide}
+ */
+public abstract class WebBackForwardListClient {
+
+    /**
+     * Notify the client that <var>item</var> has been added to the
+     * WebBackForwardList.
+     * @param item The newly created WebHistoryItem
+     */
+    public void onNewHistoryItem(WebHistoryItem item) { }
+
+    /**
+     * Notify the client that the <var>item</var> at <var>index</var> is now
+     * the current history item.
+     * @param item A WebHistoryItem
+     * @param index The new history index
+     */
+    public void onIndexChanged(WebHistoryItem item, int index) { }
+}
diff --git a/core/java/android/webkit/WebHistoryItem.java b/core/java/android/webkit/WebHistoryItem.java
index abd8237..428a59c 100644
--- a/core/java/android/webkit/WebHistoryItem.java
+++ b/core/java/android/webkit/WebHistoryItem.java
@@ -41,6 +41,8 @@
     private byte[] mFlattenedData;
     // The apple-touch-icon url for use when adding the site to the home screen
     private String mTouchIconUrl;
+    // Custom client data that is not flattened or read by native code.
+    private Object mCustomData;
 
     /**
      * Basic constructor that assigns a unique id to the item. Called by JNI
@@ -137,6 +139,28 @@
     }
 
     /**
+     * Return the custom data provided by the client.
+     * @hide
+     */
+    public Object getCustomData() {
+        return mCustomData;
+    }
+
+    /**
+     * Set the custom data field.
+     * @param data An Object containing any data the client wishes to associate
+     *             with the item.
+     * @hide
+     */
+    public void setCustomData(Object data) {
+        // NOTE: WebHistoryItems are used in multiple threads. However, the
+        // public facing apis are all getters with the exception of this one
+        // api. Since this api is exclusive to clients, we don't make any
+        // promises about thread safety.
+        mCustomData = data;
+    }
+
+    /**
      * Set the favicon.
      * @param icon A Bitmap containing the favicon for this history item.
      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 5772497..72791fb 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -2860,6 +2860,25 @@
     }
 
     /**
+     * Set the back/forward list client. This is an implementation of
+     * WebBackForwardListClient for handling new items and changes in the
+     * history index.
+     * @param client An implementation of WebBackForwardListClient.
+     * {@hide}
+     */
+    public void setWebBackForwardListClient(WebBackForwardListClient client) {
+        mCallbackProxy.setWebBackForwardListClient(client);
+    }
+
+    /**
+     * Gets the WebBackForwardListClient.
+     * {@hide}
+     */
+    public WebBackForwardListClient getWebBackForwardListClient() {
+        return mCallbackProxy.getWebBackForwardListClient();
+    }
+
+    /**
      * Set the Picture listener. This is an interface used to receive
      * notifications of a new Picture.
      * @param listener An implementation of WebView.PictureListener.