WebHistoryItem.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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
43    /**
44     * Basic constructor that assigns a unique id to the item. Called by JNI
45     * only.
46     */
47    private WebHistoryItem() {
48        synchronized (WebHistoryItem.class) {
49            mId = sNextId++;
50        }
51    }
52
53    /**
54     * Construct a new WebHistoryItem with initial flattened data.
55     * @param data The pre-flattened data coming from restoreState.
56     */
57    /*package*/ WebHistoryItem(byte[] data) {
58        mUrl = null; // This will be updated natively
59        mFlattenedData = data;
60        synchronized (WebHistoryItem.class) {
61            mId = sNextId++;
62        }
63    }
64
65    /**
66     * Construct a clone of a WebHistoryItem from the given item.
67     * @param item The history item to clone.
68     */
69    private WebHistoryItem(WebHistoryItem item) {
70        mUrl = item.mUrl;
71        mTitle = item.mTitle;
72        mFlattenedData = item.mFlattenedData;
73        mFavicon = item.mFavicon;
74        mId = item.mId;
75}
76
77    /**
78     * Return an identifier for this history item. If an item is a copy of
79     * another item, the identifiers will be the same even if they are not the
80     * same object.
81     * @return The id for this item.
82     */
83    public int getId() {
84        return mId;
85    }
86
87    /**
88     * Return the url of this history item. The url is the base url of this
89     * history item. See getTargetUrl() for the url that is the actual target of
90     * this history item.
91     * @return The base url of this history item.
92     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
93     * to synchronize this method.
94     */
95    public String getUrl() {
96        return mUrl;
97    }
98
99    /**
100     * Return the original url of this history item. This was the requested
101     * url, the final url may be different as there might have been
102     * redirects while loading the site.
103     * @return The original url of this history item.
104     *
105     * @hide pending API Council approval
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     * Set the favicon.
133     * @param icon A Bitmap containing the favicon for this history item.
134     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
135     * to synchronize this method.
136     */
137    /*package*/ void setFavicon(Bitmap icon) {
138        mFavicon = icon;
139    }
140
141    /**
142     * Get the pre-flattened data.
143     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
144     * to synchronize this method.
145     */
146    /*package*/ byte[] getFlattenedData() {
147        return mFlattenedData;
148    }
149
150    /**
151     * Inflate this item.
152     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
153     * to synchronize this method.
154     */
155    /*package*/ void inflate(int nativeFrame) {
156        inflate(nativeFrame, mFlattenedData);
157    }
158
159    /**
160     * Clone the history item for use by clients of WebView.
161     */
162    protected synchronized WebHistoryItem clone() {
163        return new WebHistoryItem(this);
164    }
165
166    /* Natively inflate this item, this method is called in the WebCore thread.
167     */
168    private native void inflate(int nativeFrame, byte[] data);
169
170    /* Called by jni when the item is updated */
171    private void update(String url, String originalUrl, String title,
172            Bitmap favicon, byte[] data) {
173        mUrl = url;
174        mOriginalUrl = originalUrl;
175        mTitle = title;
176        mFavicon = favicon;
177        mFlattenedData = data;
178    }
179}
180