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 import java.net.MalformedURLException;
     22 import java.net.URL;
     23 
     24 /**
     25  * A convenience class for accessing fields in an entry in the back/forward list
     26  * of a WebView. Each WebHistoryItem is a snapshot of the requested history
     27  * item. Each history item may be updated during the load of a page.
     28  * @see WebBackForwardList
     29  */
     30 public class WebHistoryItem implements Cloneable {
     31     // Global identifier count.
     32     private static int sNextId = 0;
     33     // Unique identifier.
     34     private final int mId;
     35     // The title of this item's document.
     36     private String mTitle;
     37     // The base url of this item.
     38     private String mUrl;
     39     // The original requested url of this item.
     40     private String mOriginalUrl;
     41     // The favicon for this item.
     42     private Bitmap mFavicon;
     43     // The pre-flattened data used for saving the state.
     44     private byte[] mFlattenedData;
     45     // The apple-touch-icon url for use when adding the site to the home screen,
     46     // as obtained from a <link> element in the page.
     47     private String mTouchIconUrlFromLink;
     48     // If no <link> is specified, this holds the default location of the
     49     // apple-touch-icon.
     50     private String mTouchIconUrlServerDefault;
     51     // Custom client data that is not flattened or read by native code.
     52     private Object mCustomData;
     53 
     54     /**
     55      * Basic constructor that assigns a unique id to the item. Called by JNI
     56      * only.
     57      */
     58     private WebHistoryItem() {
     59         synchronized (WebHistoryItem.class) {
     60             mId = sNextId++;
     61         }
     62     }
     63 
     64     /**
     65      * Construct a new WebHistoryItem with initial flattened data.
     66      * @param data The pre-flattened data coming from restoreState.
     67      */
     68     /*package*/ WebHistoryItem(byte[] data) {
     69         mUrl = null; // This will be updated natively
     70         mFlattenedData = data;
     71         synchronized (WebHistoryItem.class) {
     72             mId = sNextId++;
     73         }
     74     }
     75 
     76     /**
     77      * Construct a clone of a WebHistoryItem from the given item.
     78      * @param item The history item to clone.
     79      */
     80     private WebHistoryItem(WebHistoryItem item) {
     81         mUrl = item.mUrl;
     82         mTitle = item.mTitle;
     83         mFlattenedData = item.mFlattenedData;
     84         mFavicon = item.mFavicon;
     85         mId = item.mId;
     86 }
     87 
     88     /**
     89      * Return an identifier for this history item. If an item is a copy of
     90      * another item, the identifiers will be the same even if they are not the
     91      * same object.
     92      * @return The id for this item.
     93      * @deprecated This method is now obsolete.
     94      */
     95     @Deprecated
     96     public int getId() {
     97         return mId;
     98     }
     99 
    100     /**
    101      * Return the url of this history item. The url is the base url of this
    102      * history item. See getTargetUrl() for the url that is the actual target of
    103      * this history item.
    104      * @return The base url of this history item.
    105      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    106      * to synchronize this method.
    107      */
    108     public String getUrl() {
    109         return mUrl;
    110     }
    111 
    112     /**
    113      * Return the original url of this history item. This was the requested
    114      * url, the final url may be different as there might have been
    115      * redirects while loading the site.
    116      * @return The original url of this history item.
    117      */
    118     public String getOriginalUrl() {
    119         return mOriginalUrl;
    120     }
    121 
    122     /**
    123      * Return the document title of this history item.
    124      * @return The document title of this history item.
    125      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    126      * to synchronize this method.
    127      */
    128     public String getTitle() {
    129         return mTitle;
    130     }
    131 
    132     /**
    133      * Return the favicon of this history item or null if no favicon was found.
    134      * @return A Bitmap containing the favicon for this history item or null.
    135      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    136      * to synchronize this method.
    137      */
    138     public Bitmap getFavicon() {
    139         return mFavicon;
    140     }
    141 
    142     /**
    143      * Return the touch icon url.
    144      * If no touch icon <link> tag was specified, returns
    145      * <host>/apple-touch-icon.png. The DownloadTouchIcon class that
    146      * attempts to retrieve the touch icon will handle the case where
    147      * that file does not exist. An icon set by a <link> tag is always
    148      * used in preference to an icon saved on the server.
    149      * @hide
    150      */
    151     public String getTouchIconUrl() {
    152         if (mTouchIconUrlFromLink != null) {
    153             return mTouchIconUrlFromLink;
    154         } else if (mTouchIconUrlServerDefault != null) {
    155             return mTouchIconUrlServerDefault;
    156         }
    157 
    158         try {
    159             URL url = new URL(mOriginalUrl);
    160             mTouchIconUrlServerDefault = new URL(url.getProtocol(), url.getHost(), url.getPort(),
    161                     "/apple-touch-icon.png").toString();
    162         } catch (MalformedURLException e) {
    163             return null;
    164         }
    165         return mTouchIconUrlServerDefault;
    166     }
    167 
    168     /**
    169      * Return the custom data provided by the client.
    170      * @hide
    171      */
    172     public Object getCustomData() {
    173         return mCustomData;
    174     }
    175 
    176     /**
    177      * Set the custom data field.
    178      * @param data An Object containing any data the client wishes to associate
    179      *             with the item.
    180      * @hide
    181      */
    182     public void setCustomData(Object data) {
    183         // NOTE: WebHistoryItems are used in multiple threads. However, the
    184         // public facing apis are all getters with the exception of this one
    185         // api. Since this api is exclusive to clients, we don't make any
    186         // promises about thread safety.
    187         mCustomData = data;
    188     }
    189 
    190     /**
    191      * Set the favicon.
    192      * @param icon A Bitmap containing the favicon for this history item.
    193      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    194      * to synchronize this method.
    195      */
    196     /*package*/ void setFavicon(Bitmap icon) {
    197         mFavicon = icon;
    198     }
    199 
    200     /**
    201      * Set the touch icon url. Will not overwrite an icon that has been
    202      * set already from a <link> tag, unless the new icon is precomposed.
    203      * @hide
    204      */
    205     /*package*/ void setTouchIconUrl(String url, boolean precomposed) {
    206         if (precomposed || mTouchIconUrlFromLink == null) {
    207             mTouchIconUrlFromLink = url;
    208         }
    209     }
    210 
    211     /**
    212      * Get the pre-flattened data.
    213      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    214      * to synchronize this method.
    215      */
    216     /*package*/ byte[] getFlattenedData() {
    217         return mFlattenedData;
    218     }
    219 
    220     /**
    221      * Inflate this item.
    222      * Note: The VM ensures 32-bit atomic read/write operations so we don't have
    223      * to synchronize this method.
    224      */
    225     /*package*/ void inflate(int nativeFrame) {
    226         inflate(nativeFrame, mFlattenedData);
    227     }
    228 
    229     /**
    230      * Clone the history item for use by clients of WebView.
    231      */
    232     protected synchronized WebHistoryItem clone() {
    233         return new WebHistoryItem(this);
    234     }
    235 
    236     /* Natively inflate this item, this method is called in the WebCore thread.
    237      */
    238     private native void inflate(int nativeFrame, byte[] data);
    239 
    240     /* Called by jni when the item is updated */
    241     private void update(String url, String originalUrl, String title,
    242             Bitmap favicon, byte[] data) {
    243         mUrl = url;
    244         mOriginalUrl = originalUrl;
    245         mTitle = title;
    246         mFavicon = favicon;
    247         mFlattenedData = data;
    248     }
    249 }
    250