Home | History | Annotate | Download | only in webkit
      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 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  */
     27 public 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     // Custom client data that is not flattened or read by native code.
     45     private Object mCustomData;
     46 
     47     /**
     48      * Basic constructor that assigns a unique id to the item. Called by JNI
     49      * only.
     50      */
     51     private WebHistoryItem() {
     52         synchronized (WebHistoryItem.class) {
     53             mId = sNextId++;
     54         }
     55     }
     56 
     57     /**
     58      * Construct a new WebHistoryItem with initial flattened data.
     59      * @param data The pre-flattened data coming from restoreState.
     60      */
     61     /*package*/ WebHistoryItem(byte[] data) {
     62         mUrl = null; // This will be updated natively
     63         mFlattenedData = data;
     64         synchronized (WebHistoryItem.class) {
     65             mId = sNextId++;
     66         }
     67     }
     68 
     69     /**
     70      * Construct a clone of a WebHistoryItem from the given item.
     71      * @param item The history item to clone.
     72      */
     73     private WebHistoryItem(WebHistoryItem item) {
     74         mUrl = item.mUrl;
     75         mTitle = item.mTitle;
     76         mFlattenedData = item.mFlattenedData;
     77         mFavicon = item.mFavicon;
     78         mId = item.mId;
     79 }
     80 
     81     /**
     82      * Return an identifier for this history item. If an item is a copy of
     83      * another item, the identifiers will be the same even if they are not the
     84      * same object.
     85      * @return The id for this item.
     86      */
     87     public int getId() {
     88         return mId;
     89     }
     90 
     91     /**
     92      * Return the url of this history item. The url is the base url of this
     93      * history item. See getTargetUrl() for the url that is the actual target of
     94      * this history item.
     95      * @return The base url of this history item.
     96      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
     97      * to synchronize this method.
     98      */
     99     public String getUrl() {
    100         return mUrl;
    101     }
    102 
    103     /**
    104      * Return the original url of this history item. This was the requested
    105      * url, the final url may be different as there might have been
    106      * redirects while loading the site.
    107      * @return The original url of this history item.
    108      */
    109     public String getOriginalUrl() {
    110         return mOriginalUrl;
    111     }
    112 
    113     /**
    114      * Return the document title of this history item.
    115      * @return The document title of this history item.
    116      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    117      * to synchronize this method.
    118      */
    119     public String getTitle() {
    120         return mTitle;
    121     }
    122 
    123     /**
    124      * Return the favicon of this history item or null if no favicon was found.
    125      * @return A Bitmap containing the favicon for this history item or null.
    126      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    127      * to synchronize this method.
    128      */
    129     public Bitmap getFavicon() {
    130         return mFavicon;
    131     }
    132 
    133     /**
    134      * Return the touch icon url.
    135      * @hide
    136      */
    137     public String getTouchIconUrl() {
    138         return mTouchIconUrl;
    139     }
    140 
    141     /**
    142      * Return the custom data provided by the client.
    143      * @hide
    144      */
    145     public Object getCustomData() {
    146         return mCustomData;
    147     }
    148 
    149     /**
    150      * Set the custom data field.
    151      * @param data An Object containing any data the client wishes to associate
    152      *             with the item.
    153      * @hide
    154      */
    155     public void setCustomData(Object data) {
    156         // NOTE: WebHistoryItems are used in multiple threads. However, the
    157         // public facing apis are all getters with the exception of this one
    158         // api. Since this api is exclusive to clients, we don't make any
    159         // promises about thread safety.
    160         mCustomData = data;
    161     }
    162 
    163     /**
    164      * Set the favicon.
    165      * @param icon A Bitmap containing the favicon for this history item.
    166      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    167      * to synchronize this method.
    168      */
    169     /*package*/ void setFavicon(Bitmap icon) {
    170         mFavicon = icon;
    171     }
    172 
    173     /**
    174      * Set the touch icon url.
    175      * @hide
    176      */
    177     /*package*/ void setTouchIconUrl(String url) {
    178         mTouchIconUrl = url;
    179     }
    180 
    181     /**
    182      * Get the pre-flattened data.
    183      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    184      * to synchronize this method.
    185      */
    186     /*package*/ byte[] getFlattenedData() {
    187         return mFlattenedData;
    188     }
    189 
    190     /**
    191      * Inflate this item.
    192      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    193      * to synchronize this method.
    194      */
    195     /*package*/ void inflate(int nativeFrame) {
    196         inflate(nativeFrame, mFlattenedData);
    197     }
    198 
    199     /**
    200      * Clone the history item for use by clients of WebView.
    201      */
    202     protected synchronized WebHistoryItem clone() {
    203         return new WebHistoryItem(this);
    204     }
    205 
    206     /* Natively inflate this item, this method is called in the WebCore thread.
    207      */
    208     private native void inflate(int nativeFrame, byte[] data);
    209 
    210     /* Called by jni when the item is updated */
    211     private void update(String url, String originalUrl, String title,
    212             Bitmap favicon, byte[] data) {
    213         mUrl = url;
    214         mOriginalUrl = originalUrl;
    215         mTitle = title;
    216         mFavicon = favicon;
    217         mFlattenedData = data;
    218     }
    219 }
    220