WebHistoryItem.java revision 2ba1262b420ef5b9abebcd818c46774b8517f244
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
17package android.webkit;
18
19import android.graphics.Bitmap;
20
21/**
22 * A convenience class for accessing fields in an entry in the back/forward list
23 * of a WebView. Each WebHistoryItem is a snapshot of the requested history
24 * item. Each history item may be updated during the load of a page.
25 * @see WebBackForwardList
26 */
27public class WebHistoryItem implements Cloneable {
28    // Global identifier count.
29    private static int sNextId = 0;
30    // Unique identifier.
31    private final int mId;
32    // The title of this item's document.
33    private String mTitle;
34    // The base url of this item.
35    private String mUrl;
36    // The original requested url of this item.
37    private String mOriginalUrl;
38    // The favicon for this item.
39    private Bitmap mFavicon;
40    // The pre-flattened data used for saving the state.
41    private byte[] mFlattenedData;
42    // The apple-touch-icon url for use when adding the site to the home screen
43    private String mTouchIconUrl;
44
45    /**
46     * Basic constructor that assigns a unique id to the item. Called by JNI
47     * only.
48     */
49    private WebHistoryItem() {
50        synchronized (WebHistoryItem.class) {
51            mId = sNextId++;
52        }
53    }
54
55    /**
56     * Construct a new WebHistoryItem with initial flattened data.
57     * @param data The pre-flattened data coming from restoreState.
58     */
59    /*package*/ WebHistoryItem(byte[] data) {
60        mUrl = null; // This will be updated natively
61        mFlattenedData = data;
62        synchronized (WebHistoryItem.class) {
63            mId = sNextId++;
64        }
65    }
66
67    /**
68     * Construct a clone of a WebHistoryItem from the given item.
69     * @param item The history item to clone.
70     */
71    private WebHistoryItem(WebHistoryItem item) {
72        mUrl = item.mUrl;
73        mTitle = item.mTitle;
74        mFlattenedData = item.mFlattenedData;
75        mFavicon = item.mFavicon;
76        mId = item.mId;
77}
78
79    /**
80     * Return an identifier for this history item. If an item is a copy of
81     * another item, the identifiers will be the same even if they are not the
82     * same object.
83     * @return The id for this item.
84     */
85    public int getId() {
86        return mId;
87    }
88
89    /**
90     * Return the url of this history item. The url is the base url of this
91     * history item. See getTargetUrl() for the url that is the actual target of
92     * this history item.
93     * @return The base url of this history item.
94     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
95     * to synchronize this method.
96     */
97    public String getUrl() {
98        return mUrl;
99    }
100
101    /**
102     * Return the original url of this history item. This was the requested
103     * url, the final url may be different as there might have been
104     * redirects while loading the site.
105     * @return The original url of this history item.
106     */
107    public String getOriginalUrl() {
108        return mOriginalUrl;
109    }
110
111    /**
112     * Return the document title of this history item.
113     * @return The document title of this history item.
114     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
115     * to synchronize this method.
116     */
117    public String getTitle() {
118        return mTitle;
119    }
120
121    /**
122     * Return the favicon of this history item or null if no favicon was found.
123     * @return A Bitmap containing the favicon for this history item or null.
124     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
125     * to synchronize this method.
126     */
127    public Bitmap getFavicon() {
128        return mFavicon;
129    }
130
131    /**
132     * Return the touch icon url.
133     * @hide
134     */
135    public String getTouchIconUrl() {
136        return mTouchIconUrl;
137    }
138
139    /**
140     * Set the favicon.
141     * @param icon A Bitmap containing the favicon for this history item.
142     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
143     * to synchronize this method.
144     */
145    /*package*/ void setFavicon(Bitmap icon) {
146        mFavicon = icon;
147    }
148
149    /**
150     * Set the touch icon url.
151     * @hide
152     */
153    /*package*/ void setTouchIconUrl(String url) {
154        mTouchIconUrl = url;
155    }
156
157    /**
158     * Get the pre-flattened data.
159     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
160     * to synchronize this method.
161     */
162    /*package*/ byte[] getFlattenedData() {
163        return mFlattenedData;
164    }
165
166    /**
167     * Inflate this item.
168     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
169     * to synchronize this method.
170     */
171    /*package*/ void inflate(int nativeFrame) {
172        inflate(nativeFrame, mFlattenedData);
173    }
174
175    /**
176     * Clone the history item for use by clients of WebView.
177     */
178    protected synchronized WebHistoryItem clone() {
179        return new WebHistoryItem(this);
180    }
181
182    /* Natively inflate this item, this method is called in the WebCore thread.
183     */
184    private native void inflate(int nativeFrame, byte[] data);
185
186    /* Called by jni when the item is updated */
187    private void update(String url, String originalUrl, String title,
188            Bitmap favicon, byte[] data) {
189        mUrl = url;
190        mOriginalUrl = originalUrl;
191        mTitle = title;
192        mFavicon = favicon;
193        mFlattenedData = data;
194    }
195}
196