CookieManager.java revision 0ac81cb785241f49abc39ba639abddc33b891971
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.net.WebAddress; 20 21/** 22 * Manages the cookies used by an application's {@link WebView} instances. 23 * Cookies are manipulated according to RFC2109. 24 */ 25public class CookieManager { 26 /** 27 * @hide Only for use by WebViewProvider implementations 28 */ 29 protected CookieManager() { 30 } 31 32 @Override 33 protected Object clone() throws CloneNotSupportedException { 34 throw new CloneNotSupportedException("doesn't implement Cloneable"); 35 } 36 37 /** 38 * Gets the singleton CookieManager instance. If this method is used 39 * before the application instantiates a {@link WebView} instance, 40 * {@link CookieSyncManager#createInstance CookieSyncManager.createInstance(Context)} 41 * must be called first. 42 * 43 * @return the singleton CookieManager instance 44 */ 45 public static synchronized CookieManager getInstance() { 46 return WebViewFactory.getProvider().getCookieManager(); 47 } 48 49 /** 50 * Sets whether the application's {@link WebView} instances should send and 51 * accept cookies. 52 * 53 * @param accept whether {@link WebView} instances should send and accept 54 * cookies 55 */ 56 public synchronized void setAcceptCookie(boolean accept) { 57 throw new MustOverrideException(); 58 } 59 60 /** 61 * Gets whether the application's {@link WebView} instances send and accept 62 * cookies. 63 * 64 * @return true if {@link WebView} instances send and accept cookies 65 */ 66 public synchronized boolean acceptCookie() { 67 throw new MustOverrideException(); 68 } 69 70 /** 71 * Sets a cookie for the given URL. Any existing cookie with the same host, 72 * path and name will be replaced with the new cookie. The cookie being set 73 * will be ignored if it is expired. 74 * 75 * @param url the URL for which the cookie is set 76 * @param value the cookie as a string, using the format of the 'Set-Cookie' 77 * HTTP response header 78 */ 79 public void setCookie(String url, String value) { 80 throw new MustOverrideException(); 81 } 82 83 /** 84 * Gets the cookies for the given URL. 85 * 86 * @param url the URL for which the cookies are requested 87 * @return value the cookies as a string, using the format of the 'Cookie' 88 * HTTP request header 89 */ 90 public String getCookie(String url) { 91 throw new MustOverrideException(); 92 } 93 94 /** 95 * See {@link #getCookie(String)}. 96 * 97 * @param url the URL for which the cookies are requested 98 * @param privateBrowsing whether to use the private browsing cookie jar 99 * @return value the cookies as a string, using the format of the 'Cookie' 100 * HTTP request header 101 * @hide Used by Browser, no intention to publish. 102 */ 103 public String getCookie(String url, boolean privateBrowsing) { 104 throw new MustOverrideException(); 105 } 106 107 /** 108 * Gets cookie(s) for a given uri so that it can be set to "cookie:" in http 109 * request header. 110 * 111 * @param uri the WebAddress for which the cookies are requested 112 * @return value the cookies as a string, using the format of the 'Cookie' 113 * HTTP request header 114 * @hide Used by RequestHandle, no intention to publish. 115 */ 116 public synchronized String getCookie(WebAddress uri) { 117 throw new MustOverrideException(); 118 } 119 120 /** 121 * Removes all session cookies, which are cookies without an expiration 122 * date. 123 */ 124 public void removeSessionCookie() { 125 throw new MustOverrideException(); 126 } 127 128 /** 129 * Removes all cookies. 130 */ 131 public void removeAllCookie() { 132 throw new MustOverrideException(); 133 } 134 135 /** 136 * Gets whether there are stored cookies. 137 * 138 * @return true if there are stored cookies 139 */ 140 public synchronized boolean hasCookies() { 141 throw new MustOverrideException(); 142 } 143 144 /** 145 * See {@link #hasCookies()}. 146 * 147 * @param privateBrowsing whether to use the private browsing cookie jar 148 * @hide Used by Browser, no intention to publish. 149 */ 150 public synchronized boolean hasCookies(boolean privateBrowsing) { 151 throw new MustOverrideException(); 152 } 153 154 /** 155 * Removes all expired cookies. 156 */ 157 public void removeExpiredCookie() { 158 throw new MustOverrideException(); 159 } 160 161 /** 162 * Flushes all cookies managed by the Chrome HTTP stack to flash. 163 * 164 * @hide Package level api, called from CookieSyncManager 165 */ 166 protected void flushCookieStore() { 167 throw new MustOverrideException(); 168 } 169 170 /** 171 * Gets whether the application's {@link WebView} instances send and accept 172 * cookies for file scheme URLs. 173 * 174 * @return true if {@link WebView} instances send and accept cookies for 175 * file scheme URLs 176 */ 177 // Static for backward compatibility. 178 public static boolean allowFileSchemeCookies() { 179 return getInstance().allowFileSchemeCookiesImpl(); 180 } 181 182 /** 183 * Implements {@link #allowFileSchemeCookies()}. 184 * 185 * @hide Only for use by WebViewProvider implementations 186 */ 187 protected boolean allowFileSchemeCookiesImpl() { 188 throw new MustOverrideException(); 189 } 190 191 /** 192 * Sets whether the application's {@link WebView} instances should send and 193 * accept cookies for file scheme URLs. 194 * Use of cookies with file scheme URLs is potentially insecure. Do not use 195 * this feature unless you can be sure that no unintentional sharing of 196 * cookie data can take place. 197 * <p> 198 * Note that calls to this method will have no effect if made after a 199 * {@link WebView} or CookieManager instance has been created. 200 */ 201 // Static for backward compatibility. 202 public static void setAcceptFileSchemeCookies(boolean accept) { 203 getInstance().setAcceptFileSchemeCookiesImpl(accept); 204 } 205 206 /** 207 * Implements {@link #setAcceptFileSchemeCookies(boolean)}. 208 * 209 * @hide Only for use by WebViewProvider implementations 210 */ 211 protected void setAcceptFileSchemeCookiesImpl(boolean accept) { 212 throw new MustOverrideException(); 213 } 214} 215