• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.webkit;
18 
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 
22 /**
23  * This class contains the back/forward list for a WebView.
24  * WebView.copyBackForwardList() will return a copy of this class used to
25  * inspect the entries in the list.
26  */
27 public class WebBackForwardList implements Cloneable, Serializable {
28     // Current position in the list.
29     private int mCurrentIndex;
30     // ArrayList of WebHistoryItems for maintaining our copy.
31     private ArrayList<WebHistoryItem> mArray;
32     // Flag to indicate that the list is invalid
33     private boolean mClearPending;
34 
35     /**
36      * Construct a back/forward list used by clients of WebView.
37      */
WebBackForwardList()38     /*package*/ WebBackForwardList() {
39         mCurrentIndex = -1;
40         mArray = new ArrayList<WebHistoryItem>();
41     }
42 
43     /**
44      * Return the current history item. This method returns null if the list is
45      * empty.
46      * @return The current history item.
47      */
getCurrentItem()48     public synchronized WebHistoryItem getCurrentItem() {
49         return getItemAtIndex(mCurrentIndex);
50     }
51 
52     /**
53      * Get the index of the current history item. This index can be used to
54      * directly index into the array list.
55      * @return The current index from 0...n or -1 if the list is empty.
56      */
getCurrentIndex()57     public synchronized int getCurrentIndex() {
58         return mCurrentIndex;
59     }
60 
61     /**
62      * Get the history item at the given index. The index range is from 0...n
63      * where 0 is the first item and n is the last item.
64      * @param index The index to retrieve.
65      */
getItemAtIndex(int index)66     public synchronized WebHistoryItem getItemAtIndex(int index) {
67         if (index < 0 || index >= getSize()) {
68             return null;
69         }
70         return mArray.get(index);
71     }
72 
73     /**
74      * Get the total size of the back/forward list.
75      * @return The size of the list.
76      */
getSize()77     public synchronized int getSize() {
78         return mArray.size();
79     }
80 
81     /**
82      * Mark the back/forward list as having a pending clear. This is used on the
83      * UI side to mark the list as being invalid during the clearHistory method.
84      */
setClearPending()85     /*package*/ synchronized void setClearPending() {
86         mClearPending = true;
87     }
88 
89     /**
90      * Return the status of the clear flag. This is used on the UI side to
91      * determine if the list is valid for checking things like canGoBack.
92      */
getClearPending()93     /*package*/ synchronized boolean getClearPending() {
94         return mClearPending;
95     }
96 
97     /**
98      * Add a new history item to the list. This will remove all items after the
99      * current item and append the new item to the end of the list. Called from
100      * the WebCore thread only. Synchronized because the UI thread may be
101      * reading the array or the current index.
102      * @param item A new history item.
103      */
addHistoryItem(WebHistoryItem item)104     /*package*/ synchronized void addHistoryItem(WebHistoryItem item) {
105         // Update the current position because we are going to add the new item
106         // in that slot.
107         ++mCurrentIndex;
108         // If the current position is not at the end, remove all history items
109         // after the current item.
110         final int size = mArray.size();
111         final int newPos = mCurrentIndex;
112         if (newPos != size) {
113             for (int i = size - 1; i >= newPos; i--) {
114                 final WebHistoryItem h = mArray.remove(i);
115             }
116         }
117         // Add the item to the list.
118         mArray.add(item);
119     }
120 
121     /**
122      * Clear the back/forward list. Called from the WebCore thread.
123      */
close(int nativeFrame)124     /*package*/ synchronized void close(int nativeFrame) {
125         // Clear the array first because nativeClose will call addHistoryItem
126         // with the current item.
127         mArray.clear();
128         mCurrentIndex = -1;
129         nativeClose(nativeFrame);
130         // Reset the clear flag
131         mClearPending = false;
132     }
133 
134     /* Remove the item at the given index. Called by JNI only. */
removeHistoryItem(int index)135     private synchronized void removeHistoryItem(int index) {
136         // XXX: This is a special case. Since the callback is only triggered
137         // when removing the first item, we can assert that the index is 0.
138         // This lets us change the current index without having to query the
139         // native BackForwardList.
140         if (DebugFlags.WEB_BACK_FORWARD_LIST && (index != 0)) {
141             throw new AssertionError();
142         }
143         final WebHistoryItem h = mArray.remove(index);
144         // XXX: If we ever add another callback for removing history items at
145         // any index, this will no longer be valid.
146         mCurrentIndex--;
147     }
148 
149     /**
150      * Clone the entire object to be used in the UI thread by clients of
151      * WebView. This creates a copy that should never be modified by any of the
152      * webkit package classes.
153      */
clone()154     protected synchronized WebBackForwardList clone() {
155         WebBackForwardList l = new WebBackForwardList();
156         if (mClearPending) {
157             // If a clear is pending, return a copy with only the current item.
158             l.addHistoryItem(getCurrentItem());
159             return l;
160         }
161         l.mCurrentIndex = mCurrentIndex;
162         int size = getSize();
163         l.mArray = new ArrayList<WebHistoryItem>(size);
164         for (int i = 0; i < size; i++) {
165             // Add a copy of each WebHistoryItem
166             l.mArray.add(mArray.get(i).clone());
167         }
168         return l;
169     }
170 
171     /**
172      * Set the new history index.
173      * @param newIndex The new history index.
174      */
setCurrentIndex(int newIndex)175     /*package*/ synchronized void setCurrentIndex(int newIndex) {
176         mCurrentIndex = newIndex;
177     }
178 
179     /**
180      * Restore the history index.
181      */
restoreIndex(int nativeFrame, int index)182     /*package*/ static native synchronized void restoreIndex(int nativeFrame,
183             int index);
184 
185     /* Close the native list. */
nativeClose(int nativeFrame)186     private static native void nativeClose(int nativeFrame);
187 }
188