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.net.WebAddress; 20 21 /** 22 * Manages the cookies used by an application's {@link WebView} instances. 23 * Cookies are manipulated according to RFC2109. 24 */ 25 public 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 * must not have expired and must not be a session cookie, otherwise it 74 * will be ignored. 75 * 76 * @param url the URL for which the cookie is set 77 * @param value the cookie as a string, using the format of the 'Set-Cookie' 78 * HTTP response header 79 */ 80 public void setCookie(String url, String value) { 81 throw new MustOverrideException(); 82 } 83 84 /** 85 * Gets the cookies for the given URL. 86 * 87 * @param url the URL for which the cookies are requested 88 * @return value the cookies as a string, using the format of the 'Cookie' 89 * HTTP request header 90 */ 91 public String getCookie(String url) { 92 throw new MustOverrideException(); 93 } 94 95 /** 96 * See {@link #getCookie(String)}. 97 * 98 * @param url the URL for which the cookies are requested 99 * @param privateBrowsing whether to use the private browsing cookie jar 100 * @return value the cookies as a string, using the format of the 'Cookie' 101 * HTTP request header 102 * @hide Used by Browser, no intention to publish. 103 */ 104 public String getCookie(String url, boolean privateBrowsing) { 105 throw new MustOverrideException(); 106 } 107 108 /** 109 * Gets cookie(s) for a given uri so that it can be set to "cookie:" in http 110 * request header. 111 * 112 * @param uri the WebAddress for which the cookies are requested 113 * @return value the cookies as a string, using the format of the 'Cookie' 114 * HTTP request header 115 * @hide Used by RequestHandle, no intention to publish. 116 */ 117 public synchronized String getCookie(WebAddress uri) { 118 throw new MustOverrideException(); 119 } 120 121 /** 122 * Removes all session cookies, which are cookies without an expiration 123 * date. 124 */ 125 public void removeSessionCookie() { 126 throw new MustOverrideException(); 127 } 128 129 /** 130 * Removes all cookies. 131 */ 132 public void removeAllCookie() { 133 throw new MustOverrideException(); 134 } 135 136 /** 137 * Gets whether there are stored cookies. 138 * 139 * @return true if there are stored cookies 140 */ 141 public synchronized boolean hasCookies() { 142 throw new MustOverrideException(); 143 } 144 145 /** 146 * See {@link #hasCookies()}. 147 * 148 * @param privateBrowsing whether to use the private browsing cookie jar 149 * @hide Used by Browser, no intention to publish. 150 */ 151 public synchronized boolean hasCookies(boolean privateBrowsing) { 152 throw new MustOverrideException(); 153 } 154 155 /** 156 * Removes all expired cookies. 157 */ 158 public void removeExpiredCookie() { 159 throw new MustOverrideException(); 160 } 161 162 /** 163 * Flushes all cookies managed by the Chrome HTTP stack to flash. 164 * 165 * @hide Package level api, called from CookieSyncManager 166 */ 167 protected void flushCookieStore() { 168 throw new MustOverrideException(); 169 } 170 171 /** 172 * Gets whether the application's {@link WebView} instances send and accept 173 * cookies for file scheme URLs. 174 * 175 * @return true if {@link WebView} instances send and accept cookies for 176 * file scheme URLs 177 */ 178 // Static for backward compatibility. 179 public static boolean allowFileSchemeCookies() { 180 return getInstance().allowFileSchemeCookiesImpl(); 181 } 182 183 /** 184 * Implements {@link #allowFileSchemeCookies()}. 185 * 186 * @hide Only for use by WebViewProvider implementations 187 */ 188 protected boolean allowFileSchemeCookiesImpl() { 189 throw new MustOverrideException(); 190 } 191 192 /** 193 * Sets whether the application's {@link WebView} instances should send and 194 * accept cookies for file scheme URLs. 195 * Use of cookies with file scheme URLs is potentially insecure. Do not use 196 * this feature unless you can be sure that no unintentional sharing of 197 * cookie data can take place. 198 * <p> 199 * Note that calls to this method will have no effect if made after a 200 * {@link WebView} or CookieManager instance has been created. 201 */ 202 // Static for backward compatibility. 203 public static void setAcceptFileSchemeCookies(boolean accept) { 204 getInstance().setAcceptFileSchemeCookiesImpl(accept); 205 } 206 207 /** 208 * Implements {@link #setAcceptFileSchemeCookies(boolean)}. 209 * 210 * @hide Only for use by WebViewProvider implementations 211 */ 212 protected void setAcceptFileSchemeCookiesImpl(boolean accept) { 213 throw new MustOverrideException(); 214 } 215 } 216