diff --git a/core/java/android/webkit/CallbackProxy.java b/core/java/android/webkit/CallbackProxy.java index 61a2d2ef046d8..a013a9b2d03a4 100644 --- a/core/java/android/webkit/CallbackProxy.java +++ b/core/java/android/webkit/CallbackProxy.java @@ -69,6 +69,8 @@ class CallbackProxy extends Handler { 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 @@ class CallbackProxy extends Handler { 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 @@ class CallbackProxy extends Handler { // Used to start a default activity. mContext = context; mWebView = w; - mBackForwardList = new WebBackForwardList(); + mBackForwardList = new WebBackForwardList(this); } /** @@ -190,6 +194,14 @@ class CallbackProxy extends Handler { 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 @@ class CallbackProxy extends Handler { 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 @@ class CallbackProxy extends Handler { } 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 62a55318d3065..79e634e5faa42 100644 --- a/core/java/android/webkit/WebBackForwardList.java +++ b/core/java/android/webkit/WebBackForwardList.java @@ -31,13 +31,16 @@ public class WebBackForwardList implements Cloneable, Serializable { private ArrayList 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(); + mCallbackProxy = proxy; } /** @@ -116,6 +119,9 @@ public class WebBackForwardList implements Cloneable, Serializable { } // Add the item to the list. mArray.add(item); + if (mCallbackProxy != null) { + mCallbackProxy.onNewHistoryItem(item); + } } /** @@ -152,7 +158,7 @@ public class WebBackForwardList implements Cloneable, Serializable { * 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 @@ public class WebBackForwardList implements Cloneable, Serializable { */ /*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 0000000000000..7fe92816c2355 --- /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 item has been added to the + * WebBackForwardList. + * @param item The newly created WebHistoryItem + */ + public void onNewHistoryItem(WebHistoryItem item) { } + + /** + * Notify the client that the item at index 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 abd82377d68c6..428a59ce90f48 100644 --- a/core/java/android/webkit/WebHistoryItem.java +++ b/core/java/android/webkit/WebHistoryItem.java @@ -41,6 +41,8 @@ public class WebHistoryItem implements Cloneable { 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 @@ -136,6 +138,28 @@ public class WebHistoryItem implements Cloneable { return mTouchIconUrl; } + /** + * 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. diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 577249767de57..72791fb830e7d 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -2859,6 +2859,25 @@ public class WebView extends AbsoluteLayout return mCallbackProxy.getWebChromeClient(); } + /** + * 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.