blob: 3824c22a40a733f554a461fda298ee84402d9f1e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Nate Fischer3442c742017-09-08 17:02:00 -070019import android.annotation.Nullable;
Ignacio Solla451e3382014-11-10 10:35:54 +000020import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.net.WebAddress;
Doug Zongker45a9a142010-02-03 13:52:18 -080022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
Steve Block77db9992012-02-20 11:46:13 +000024 * Manages the cookies used by an application's {@link WebView} instances.
Torne (Richard Coles)22a92de2019-10-21 14:34:03 -040025 * <p>
26 * CookieManager represents cookies as strings in the same format as the
27 * HTTP {@code Cookie} and {@code Set-Cookie} header fields (defined in
28 * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03">RFC6265bis</a>).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 */
Ignacio Solla451e3382014-11-10 10:35:54 +000030public abstract class CookieManager {
Nate Fischer1e13fae2018-09-25 18:27:25 -070031 /**
32 * @deprecated This class should not be constructed by applications, use {@link #getInstance}
33 * instead to fetch the singleton instance.
34 */
35 // TODO(ntfschr): mark this as @SystemApi after a year.
36 @Deprecated
37 public CookieManager() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Jonathan Dixond3101b12012-04-12 20:51:51 +010039 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 protected Object clone() throws CloneNotSupportedException {
41 throw new CloneNotSupportedException("doesn't implement Cloneable");
42 }
43
44 /**
Paul Miller9d67f362014-12-12 13:45:33 -080045 * Gets the singleton CookieManager instance.
Jonathan Dixond3101b12012-04-12 20:51:51 +010046 *
Steve Block4e584df2012-04-24 23:12:47 +010047 * @return the singleton CookieManager instance
Patrick Scott06312ca2010-03-19 15:40:05 -040048 */
Torne (Richard Coles)59375a02016-07-18 18:34:02 +010049 public static CookieManager getInstance() {
Jonathan Dixond3101b12012-04-12 20:51:51 +010050 return WebViewFactory.getProvider().getCookieManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 }
52
53 /**
Steve Block77db9992012-02-20 11:46:13 +000054 * Sets whether the application's {@link WebView} instances should send and
55 * accept cookies.
Nate Fischer0a6140d2017-09-05 12:37:49 -070056 * By default this is set to {@code true} and the WebView accepts cookies.
Hector Dearman9f595262014-07-21 11:00:30 +010057 * <p>
Nate Fischer0a6140d2017-09-05 12:37:49 -070058 * When this is {@code true}
Hector Dearman9f595262014-07-21 11:00:30 +010059 * {@link CookieManager#setAcceptThirdPartyCookies setAcceptThirdPartyCookies} and
60 * {@link CookieManager#setAcceptFileSchemeCookies setAcceptFileSchemeCookies}
61 * can be used to control the policy for those specific types of cookie.
Steve Block4e584df2012-04-24 23:12:47 +010062 *
63 * @param accept whether {@link WebView} instances should send and accept
Steve Block77db9992012-02-20 11:46:13 +000064 * cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 */
Ignacio Solla451e3382014-11-10 10:35:54 +000066 public abstract void setAcceptCookie(boolean accept);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68 /**
Steve Block77db9992012-02-20 11:46:13 +000069 * Gets whether the application's {@link WebView} instances send and accept
70 * cookies.
Steve Block4e584df2012-04-24 23:12:47 +010071 *
Nate Fischer0a6140d2017-09-05 12:37:49 -070072 * @return {@code true} if {@link WebView} instances send and accept cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 */
Ignacio Solla451e3382014-11-10 10:35:54 +000074 public abstract boolean acceptCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
Hector Dearmana70d1d92014-05-22 13:50:15 +010076 /**
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010077 * Sets whether the {@link WebView} should allow third party cookies to be set.
78 * Allowing third party cookies is a per WebView policy and can be set
79 * differently on different WebView instances.
80 * <p>
81 * Apps that target {@link android.os.Build.VERSION_CODES#KITKAT} or below
82 * default to allowing third party cookies. Apps targeting
Dianne Hackborn955d8d62014-10-07 20:17:19 -070083 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} or later default to disallowing
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010084 * third party cookies.
85 *
86 * @param webview the {@link WebView} instance to set the cookie policy on
87 * @param accept whether the {@link WebView} instance should accept
88 * third party cookies
89 */
Ignacio Solla451e3382014-11-10 10:35:54 +000090 public abstract void setAcceptThirdPartyCookies(WebView webview, boolean accept);
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010091
92 /**
93 * Gets whether the {@link WebView} should allow third party cookies to be set.
94 *
95 * @param webview the {@link WebView} instance to get the cookie policy for
Nate Fischer0a6140d2017-09-05 12:37:49 -070096 * @return {@code true} if the {@link WebView} accepts third party cookies
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010097 */
Ignacio Solla451e3382014-11-10 10:35:54 +000098 public abstract boolean acceptThirdPartyCookies(WebView webview);
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010099
100 /**
Steve Block77db9992012-02-20 11:46:13 +0000101 * Sets a cookie for the given URL. Any existing cookie with the same host,
102 * path and name will be replaced with the new cookie. The cookie being set
Hector Dearman0ac81cb2014-04-30 18:33:18 +0100103 * will be ignored if it is expired.
Steve Block4e584df2012-04-24 23:12:47 +0100104 *
Hector Dearman24a11d32014-05-21 12:49:12 +0100105 * @param url the URL for which the cookie is to be set
Steve Block4e584df2012-04-24 23:12:47 +0100106 * @param value the cookie as a string, using the format of the 'Set-Cookie'
Jonathan Dixond3101b12012-04-12 20:51:51 +0100107 * HTTP response header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000109 public abstract void setCookie(String url, String value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100112 * Sets a cookie for the given URL. Any existing cookie with the same host,
113 * path and name will be replaced with the new cookie. The cookie being set
114 * will be ignored if it is expired.
115 * <p>
116 * This method is asynchronous.
117 * If a {@link ValueCallback} is provided,
118 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
119 * thread's {@link android.os.Looper} once the operation is complete.
120 * The value provided to the callback indicates whether the cookie was set successfully.
121 * You can pass {@code null} as the callback if you don't need to know when the operation
122 * completes or whether it succeeded, and in this case it is safe to call the method from a
123 * thread without a Looper.
124 *
125 * @param url the URL for which the cookie is to be set
126 * @param value the cookie as a string, using the format of the 'Set-Cookie'
127 * HTTP response header
128 * @param callback a callback to be executed when the cookie has been set
129 */
Nate Fischer3442c742017-09-08 17:02:00 -0700130 public abstract void setCookie(String url, String value, @Nullable ValueCallback<Boolean>
131 callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100132
133 /**
Steve Block77db9992012-02-20 11:46:13 +0000134 * Gets the cookies for the given URL.
Steve Block4e584df2012-04-24 23:12:47 +0100135 *
136 * @param url the URL for which the cookies are requested
137 * @return value the cookies as a string, using the format of the 'Cookie'
Steve Block77db9992012-02-20 11:46:13 +0000138 * HTTP request header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000140 public abstract String getCookie(String url);
Kristian Monsen469aebb2011-03-01 20:14:59 +0000141
142 /**
Steve Block4e584df2012-04-24 23:12:47 +0100143 * See {@link #getCookie(String)}.
144 *
145 * @param url the URL for which the cookies are requested
146 * @param privateBrowsing whether to use the private browsing cookie jar
147 * @return value the cookies as a string, using the format of the 'Cookie'
Steve Block77db9992012-02-20 11:46:13 +0000148 * HTTP request header
Ignacio Solla451e3382014-11-10 10:35:54 +0000149 * @hide Used by Browser and by WebViewProvider implementations.
Kristian Monsen469aebb2011-03-01 20:14:59 +0000150 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000151 @SystemApi
152 public abstract String getCookie(String url, boolean privateBrowsing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 /**
Steve Block4e584df2012-04-24 23:12:47 +0100155 * Gets cookie(s) for a given uri so that it can be set to "cookie:" in http
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * request header.
Steve Block4e584df2012-04-24 23:12:47 +0100157 *
158 * @param uri the WebAddress for which the cookies are requested
159 * @return value the cookies as a string, using the format of the 'Cookie'
Steve Block77db9992012-02-20 11:46:13 +0000160 * HTTP request header
Ignacio Solla451e3382014-11-10 10:35:54 +0000161 * @hide Used by RequestHandle and by WebViewProvider implementations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000163 @SystemApi
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 public synchronized String getCookie(WebAddress uri) {
Ignacio Solla451e3382014-11-10 10:35:54 +0000165 return getCookie(uri.toString());
Kristian Monsen85173052010-12-08 15:29:29 +0000166 }
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
Steve Block77db9992012-02-20 11:46:13 +0000169 * Removes all session cookies, which are cookies without an expiration
170 * date.
Hector Dearman24a11d32014-05-21 12:49:12 +0100171 * @deprecated use {@link #removeSessionCookies(ValueCallback)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700173 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000174 public abstract void removeSessionCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
176 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100177 * Removes all session cookies, which are cookies without an expiration
178 * date.
179 * <p>
180 * This method is asynchronous.
181 * If a {@link ValueCallback} is provided,
182 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
183 * thread's {@link android.os.Looper} once the operation is complete.
184 * The value provided to the callback indicates whether any cookies were removed.
185 * You can pass {@code null} as the callback if you don't need to know when the operation
186 * completes or whether any cookie were removed, and in this case it is safe to call the
187 * method from a thread without a Looper.
188 * @param callback a callback which is executed when the session cookies have been removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 */
Nate Fischer3442c742017-09-08 17:02:00 -0700190 public abstract void removeSessionCookies(@Nullable ValueCallback<Boolean> callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100191
192 /**
193 * Removes all cookies.
194 * @deprecated Use {@link #removeAllCookies(ValueCallback)} instead.
195 */
196 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000197 public abstract void removeAllCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100200 * Removes all cookies.
201 * <p>
202 * This method is asynchronous.
203 * If a {@link ValueCallback} is provided,
204 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
205 * thread's {@link android.os.Looper} once the operation is complete.
206 * The value provided to the callback indicates whether any cookies were removed.
207 * You can pass {@code null} as the callback if you don't need to know when the operation
208 * completes or whether any cookies were removed, and in this case it is safe to call the
209 * method from a thread without a Looper.
210 * @param callback a callback which is executed when the cookies have been removed
211 */
Nate Fischer3442c742017-09-08 17:02:00 -0700212 public abstract void removeAllCookies(@Nullable ValueCallback<Boolean> callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100213
214 /**
Steve Block77db9992012-02-20 11:46:13 +0000215 * Gets whether there are stored cookies.
Steve Block4e584df2012-04-24 23:12:47 +0100216 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700217 * @return {@code true} if there are stored cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000219 public abstract boolean hasCookies();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 /**
Jonathan Dixond3101b12012-04-12 20:51:51 +0100222 * See {@link #hasCookies()}.
Steve Block4e584df2012-04-24 23:12:47 +0100223 *
224 * @param privateBrowsing whether to use the private browsing cookie jar
Ignacio Solla451e3382014-11-10 10:35:54 +0000225 * @hide Used by Browser and WebViewProvider implementations.
Kristian Monsen469aebb2011-03-01 20:14:59 +0000226 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000227 @SystemApi
228 public abstract boolean hasCookies(boolean privateBrowsing);
Kristian Monsen469aebb2011-03-01 20:14:59 +0000229
230 /**
Steve Block77db9992012-02-20 11:46:13 +0000231 * Removes all expired cookies.
Hector Dearman24a11d32014-05-21 12:49:12 +0100232 * @deprecated The WebView handles removing expired cookies automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 */
Hector Dearman24a11d32014-05-21 12:49:12 +0100234 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000235 public abstract void removeExpiredCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 /**
Hector Dearmana70d1d92014-05-22 13:50:15 +0100238 * Ensures all cookies currently accessible through the getCookie API are
239 * written to persistent storage.
240 * This call will block the caller until it is done and may perform I/O.
241 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000242 public abstract void flush();
Iain Merrickc40fc2c2010-11-30 11:44:32 +0000243
244 /**
Steve Block77db9992012-02-20 11:46:13 +0000245 * Gets whether the application's {@link WebView} instances send and accept
246 * cookies for file scheme URLs.
Steve Block4e584df2012-04-24 23:12:47 +0100247 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700248 * @return {@code true} if {@link WebView} instances send and accept cookies for
Steve Block77db9992012-02-20 11:46:13 +0000249 * file scheme URLs
Kristian Monsen16d041c2010-12-06 13:43:51 +0000250 */
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100251 // Static for backward compatibility.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000252 public static boolean allowFileSchemeCookies() {
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100253 return getInstance().allowFileSchemeCookiesImpl();
254 }
255
256 /**
Steve Block4e584df2012-04-24 23:12:47 +0100257 * Implements {@link #allowFileSchemeCookies()}.
258 *
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100259 * @hide Only for use by WebViewProvider implementations
260 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000261 @SystemApi
262 protected abstract boolean allowFileSchemeCookiesImpl();
Kristian Monsen16d041c2010-12-06 13:43:51 +0000263
264 /**
Steve Block77db9992012-02-20 11:46:13 +0000265 * Sets whether the application's {@link WebView} instances should send and
266 * accept cookies for file scheme URLs.
Hector Dearman9f595262014-07-21 11:00:30 +0100267 * Use of cookies with file scheme URLs is potentially insecure and turned
268 * off by default.
269 * Do not use this feature unless you can be sure that no unintentional
270 * sharing of cookie data can take place.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000271 * <p>
Steve Block77db9992012-02-20 11:46:13 +0000272 * Note that calls to this method will have no effect if made after a
273 * {@link WebView} or CookieManager instance has been created.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000274 */
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100275 // Static for backward compatibility.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000276 public static void setAcceptFileSchemeCookies(boolean accept) {
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100277 getInstance().setAcceptFileSchemeCookiesImpl(accept);
278 }
279
280 /**
Steve Block4e584df2012-04-24 23:12:47 +0100281 * Implements {@link #setAcceptFileSchemeCookies(boolean)}.
282 *
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100283 * @hide Only for use by WebViewProvider implementations
284 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000285 @SystemApi
286 protected abstract void setAcceptFileSchemeCookiesImpl(boolean accept);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287}