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