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.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.net.WebAddress; 22 23 /** 24 * Manages the cookies used by an application's {@link WebView} instances. 25 * Cookies are manipulated according to RFC2109. 26 */ 27 public abstract class CookieManager { 28 /** 29 * @deprecated This class should not be constructed by applications, use {@link #getInstance} 30 * instead to fetch the singleton instance. 31 */ 32 // TODO(ntfschr): mark this as @SystemApi after a year. 33 @Deprecated 34 public CookieManager() {} 35 36 @Override 37 protected Object clone() throws CloneNotSupportedException { 38 throw new CloneNotSupportedException("doesn't implement Cloneable"); 39 } 40 41 /** 42 * Gets the singleton CookieManager instance. 43 * 44 * @return the singleton CookieManager instance 45 */ 46 public static CookieManager getInstance() { 47 return WebViewFactory.getProvider().getCookieManager(); 48 } 49 50 /** 51 * Sets whether the application's {@link WebView} instances should send and 52 * accept cookies. 53 * By default this is set to {@code true} and the WebView accepts cookies. 54 * <p> 55 * When this is {@code true} 56 * {@link CookieManager#setAcceptThirdPartyCookies setAcceptThirdPartyCookies} and 57 * {@link CookieManager#setAcceptFileSchemeCookies setAcceptFileSchemeCookies} 58 * can be used to control the policy for those specific types of cookie. 59 * 60 * @param accept whether {@link WebView} instances should send and accept 61 * cookies 62 */ 63 public abstract void setAcceptCookie(boolean accept); 64 65 /** 66 * Gets whether the application's {@link WebView} instances send and accept 67 * cookies. 68 * 69 * @return {@code true} if {@link WebView} instances send and accept cookies 70 */ 71 public abstract boolean acceptCookie(); 72 73 /** 74 * Sets whether the {@link WebView} should allow third party cookies to be set. 75 * Allowing third party cookies is a per WebView policy and can be set 76 * differently on different WebView instances. 77 * <p> 78 * Apps that target {@link android.os.Build.VERSION_CODES#KITKAT} or below 79 * default to allowing third party cookies. Apps targeting 80 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} or later default to disallowing 81 * third party cookies. 82 * 83 * @param webview the {@link WebView} instance to set the cookie policy on 84 * @param accept whether the {@link WebView} instance should accept 85 * third party cookies 86 */ 87 public abstract void setAcceptThirdPartyCookies(WebView webview, boolean accept); 88 89 /** 90 * Gets whether the {@link WebView} should allow third party cookies to be set. 91 * 92 * @param webview the {@link WebView} instance to get the cookie policy for 93 * @return {@code true} if the {@link WebView} accepts third party cookies 94 */ 95 public abstract boolean acceptThirdPartyCookies(WebView webview); 96 97 /** 98 * Sets a cookie for the given URL. Any existing cookie with the same host, 99 * path and name will be replaced with the new cookie. The cookie being set 100 * will be ignored if it is expired. 101 * 102 * @param url the URL for which the cookie is to be set 103 * @param value the cookie as a string, using the format of the 'Set-Cookie' 104 * HTTP response header 105 */ 106 public abstract void setCookie(String url, String value); 107 108 /** 109 * Sets a cookie for the given URL. Any existing cookie with the same host, 110 * path and name will be replaced with the new cookie. The cookie being set 111 * will be ignored if it is expired. 112 * <p> 113 * This method is asynchronous. 114 * If a {@link ValueCallback} is provided, 115 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current 116 * thread's {@link android.os.Looper} once the operation is complete. 117 * The value provided to the callback indicates whether the cookie was set successfully. 118 * You can pass {@code null} as the callback if you don't need to know when the operation 119 * completes or whether it succeeded, and in this case it is safe to call the method from a 120 * thread without a Looper. 121 * 122 * @param url the URL for which the cookie is to be set 123 * @param value the cookie as a string, using the format of the 'Set-Cookie' 124 * HTTP response header 125 * @param callback a callback to be executed when the cookie has been set 126 */ 127 public abstract void setCookie(String url, String value, @Nullable ValueCallback<Boolean> 128 callback); 129 130 /** 131 * Gets the cookies for the given URL. 132 * 133 * @param url the URL for which the cookies are requested 134 * @return value the cookies as a string, using the format of the 'Cookie' 135 * HTTP request header 136 */ 137 public abstract String getCookie(String url); 138 139 /** 140 * See {@link #getCookie(String)}. 141 * 142 * @param url the URL for which the cookies are requested 143 * @param privateBrowsing whether to use the private browsing cookie jar 144 * @return value the cookies as a string, using the format of the 'Cookie' 145 * HTTP request header 146 * @hide Used by Browser and by WebViewProvider implementations. 147 */ 148 @SystemApi 149 public abstract String getCookie(String url, boolean privateBrowsing); 150 151 /** 152 * Gets cookie(s) for a given uri so that it can be set to "cookie:" in http 153 * request header. 154 * 155 * @param uri the WebAddress for which the cookies are requested 156 * @return value the cookies as a string, using the format of the 'Cookie' 157 * HTTP request header 158 * @hide Used by RequestHandle and by WebViewProvider implementations. 159 */ 160 @SystemApi 161 public synchronized String getCookie(WebAddress uri) { 162 return getCookie(uri.toString()); 163 } 164 165 /** 166 * Removes all session cookies, which are cookies without an expiration 167 * date. 168 * @deprecated use {@link #removeSessionCookies(ValueCallback)} instead. 169 */ 170 @Deprecated 171 public abstract void removeSessionCookie(); 172 173 /** 174 * Removes all session cookies, which are cookies without an expiration 175 * date. 176 * <p> 177 * This method is asynchronous. 178 * If a {@link ValueCallback} is provided, 179 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current 180 * thread's {@link android.os.Looper} once the operation is complete. 181 * The value provided to the callback indicates whether any cookies were removed. 182 * You can pass {@code null} as the callback if you don't need to know when the operation 183 * completes or whether any cookie were removed, and in this case it is safe to call the 184 * method from a thread without a Looper. 185 * @param callback a callback which is executed when the session cookies have been removed 186 */ 187 public abstract void removeSessionCookies(@Nullable ValueCallback<Boolean> callback); 188 189 /** 190 * Removes all cookies. 191 * @deprecated Use {@link #removeAllCookies(ValueCallback)} instead. 192 */ 193 @Deprecated 194 public abstract void removeAllCookie(); 195 196 /** 197 * Removes all cookies. 198 * <p> 199 * This method is asynchronous. 200 * If a {@link ValueCallback} is provided, 201 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current 202 * thread's {@link android.os.Looper} once the operation is complete. 203 * The value provided to the callback indicates whether any cookies were removed. 204 * You can pass {@code null} as the callback if you don't need to know when the operation 205 * completes or whether any cookies were removed, and in this case it is safe to call the 206 * method from a thread without a Looper. 207 * @param callback a callback which is executed when the cookies have been removed 208 */ 209 public abstract void removeAllCookies(@Nullable ValueCallback<Boolean> callback); 210 211 /** 212 * Gets whether there are stored cookies. 213 * 214 * @return {@code true} if there are stored cookies 215 */ 216 public abstract boolean hasCookies(); 217 218 /** 219 * See {@link #hasCookies()}. 220 * 221 * @param privateBrowsing whether to use the private browsing cookie jar 222 * @hide Used by Browser and WebViewProvider implementations. 223 */ 224 @SystemApi 225 public abstract boolean hasCookies(boolean privateBrowsing); 226 227 /** 228 * Removes all expired cookies. 229 * @deprecated The WebView handles removing expired cookies automatically. 230 */ 231 @Deprecated 232 public abstract void removeExpiredCookie(); 233 234 /** 235 * Ensures all cookies currently accessible through the getCookie API are 236 * written to persistent storage. 237 * This call will block the caller until it is done and may perform I/O. 238 */ 239 public abstract void flush(); 240 241 /** 242 * Gets whether the application's {@link WebView} instances send and accept 243 * cookies for file scheme URLs. 244 * 245 * @return {@code true} if {@link WebView} instances send and accept cookies for 246 * file scheme URLs 247 */ 248 // Static for backward compatibility. 249 public static boolean allowFileSchemeCookies() { 250 return getInstance().allowFileSchemeCookiesImpl(); 251 } 252 253 /** 254 * Implements {@link #allowFileSchemeCookies()}. 255 * 256 * @hide Only for use by WebViewProvider implementations 257 */ 258 @SystemApi 259 protected abstract boolean allowFileSchemeCookiesImpl(); 260 261 /** 262 * Sets whether the application's {@link WebView} instances should send and 263 * accept cookies for file scheme URLs. 264 * Use of cookies with file scheme URLs is potentially insecure and turned 265 * off by default. 266 * Do not use this feature unless you can be sure that no unintentional 267 * sharing of cookie data can take place. 268 * <p> 269 * Note that calls to this method will have no effect if made after a 270 * {@link WebView} or CookieManager instance has been created. 271 */ 272 // Static for backward compatibility. 273 public static void setAcceptFileSchemeCookies(boolean accept) { 274 getInstance().setAcceptFileSchemeCookiesImpl(accept); 275 } 276 277 /** 278 * Implements {@link #setAcceptFileSchemeCookies(boolean)}. 279 * 280 * @hide Only for use by WebViewProvider implementations 281 */ 282 @SystemApi 283 protected abstract void setAcceptFileSchemeCookiesImpl(boolean accept); 284 } 285