WebHistoryItem.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 favicon for this item.
37    private Bitmap mFavicon;
38    // The pre-flattened data used for saving the state.
39    private byte[] mFlattenedData;
40
41    /**
42     * Basic constructor that assigns a unique id to the item. Called by JNI
43     * only.
44     */
45    private WebHistoryItem() {
46        synchronized (WebHistoryItem.class) {
47            mId = sNextId++;
48        }
49    }
50
51    /**
52     * Construct a new WebHistoryItem with initial flattened data.
53     * @param data The pre-flattened data coming from restoreState.
54     */
55    /*package*/ WebHistoryItem(byte[] data) {
56        mUrl = null; // This will be updated natively
57        mFlattenedData = data;
58        synchronized (WebHistoryItem.class) {
59            mId = sNextId++;
60        }
61    }
62
63    /**
64     * Construct a clone of a WebHistoryItem from the given item.
65     * @param item The history item to clone.
66     */
67    private WebHistoryItem(WebHistoryItem item) {
68        mUrl = item.mUrl;
69        mTitle = item.mTitle;
70        mFlattenedData = item.mFlattenedData;
71        mFavicon = item.mFavicon;
72        mId = item.mId;
73}
74
75    /**
76     * Return an identifier for this history item. If an item is a copy of
77     * another item, the identifiers will be the same even if they are not the
78     * same object.
79     * @return The id for this item.
80     */
81    public int getId() {
82        return mId;
83    }
84
85    /**
86     * Return the url of this history item. The url is the base url of this
87     * history item. See getTargetUrl() for the url that is the actual target of
88     * this history item.
89     * @return The base url of this history item.
90     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
91     * to synchronize this method.
92     */
93    public String getUrl() {
94        return mUrl;
95    }
96
97    /**
98     * Return the document title of this history item.
99     * @return The document title of this history item.
100     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
101     * to synchronize this method.
102     */
103    public String getTitle() {
104        return mTitle;
105    }
106
107    /**
108     * Return the favicon of this history item or null if no favicon was found.
109     * @return A Bitmap containing the favicon for this history item or null.
110     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
111     * to synchronize this method.
112     */
113    public Bitmap getFavicon() {
114        return mFavicon;
115    }
116
117    /**
118     * Set the favicon.
119     * @param icon A Bitmap containing the favicon for this history item.
120     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
121     * to synchronize this method.
122     */
123    /*package*/ void setFavicon(Bitmap icon) {
124        mFavicon = icon;
125    }
126
127    /**
128     * Get the pre-flattened data.
129     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
130     * to synchronize this method.
131     */
132    /*package*/ byte[] getFlattenedData() {
133        return mFlattenedData;
134    }
135
136    /**
137     * Inflate this item.
138     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
139     * to synchronize this method.
140     */
141    /*package*/ void inflate(int nativeFrame) {
142        inflate(nativeFrame, mFlattenedData);
143    }
144
145    /**
146     * Clone the history item for use by clients of WebView.
147     */
148    protected synchronized WebHistoryItem clone() {
149        return new WebHistoryItem(this);
150    }
151
152    /* Natively inflate this item, this method is called in the WebCore thread.
153     */
154    private native void inflate(int nativeFrame, byte[] data);
155
156    /* Called by jni when the item is updated */
157    private void update(String url, String title, Bitmap favicon, byte[] data) {
158        mUrl = url;
159        mTitle = title;
160        mFavicon = favicon;
161        mFlattenedData = data;
162    }
163}
164