blob: ae6a2fd787d787af3fb8bbd4c289f5ef9a202ad9 [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.
25 * Cookies are manipulated according to RFC2109.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 */
Ignacio Solla451e3382014-11-10 10:35:54 +000027public abstract class CookieManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Jonathan Dixond3101b12012-04-12 20:51:51 +010029 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030 protected Object clone() throws CloneNotSupportedException {
31 throw new CloneNotSupportedException("doesn't implement Cloneable");
32 }
33
34 /**
Paul Miller9d67f362014-12-12 13:45:33 -080035 * Gets the singleton CookieManager instance.
Jonathan Dixond3101b12012-04-12 20:51:51 +010036 *
Steve Block4e584df2012-04-24 23:12:47 +010037 * @return the singleton CookieManager instance
Patrick Scott06312ca2010-03-19 15:40:05 -040038 */
Torne (Richard Coles)59375a02016-07-18 18:34:02 +010039 public static CookieManager getInstance() {
Jonathan Dixond3101b12012-04-12 20:51:51 +010040 return WebViewFactory.getProvider().getCookieManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 }
42
43 /**
Steve Block77db9992012-02-20 11:46:13 +000044 * Sets whether the application's {@link WebView} instances should send and
45 * accept cookies.
Nate Fischer0a6140d2017-09-05 12:37:49 -070046 * By default this is set to {@code true} and the WebView accepts cookies.
Hector Dearman9f595262014-07-21 11:00:30 +010047 * <p>
Nate Fischer0a6140d2017-09-05 12:37:49 -070048 * When this is {@code true}
Hector Dearman9f595262014-07-21 11:00:30 +010049 * {@link CookieManager#setAcceptThirdPartyCookies setAcceptThirdPartyCookies} and
50 * {@link CookieManager#setAcceptFileSchemeCookies setAcceptFileSchemeCookies}
51 * can be used to control the policy for those specific types of cookie.
Steve Block4e584df2012-04-24 23:12:47 +010052 *
53 * @param accept whether {@link WebView} instances should send and accept
Steve Block77db9992012-02-20 11:46:13 +000054 * cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
Ignacio Solla451e3382014-11-10 10:35:54 +000056 public abstract void setAcceptCookie(boolean accept);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58 /**
Steve Block77db9992012-02-20 11:46:13 +000059 * Gets whether the application's {@link WebView} instances send and accept
60 * cookies.
Steve Block4e584df2012-04-24 23:12:47 +010061 *
Nate Fischer0a6140d2017-09-05 12:37:49 -070062 * @return {@code true} if {@link WebView} instances send and accept cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
Ignacio Solla451e3382014-11-10 10:35:54 +000064 public abstract boolean acceptCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Hector Dearmana70d1d92014-05-22 13:50:15 +010066 /**
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010067 * Sets whether the {@link WebView} should allow third party cookies to be set.
68 * Allowing third party cookies is a per WebView policy and can be set
69 * differently on different WebView instances.
70 * <p>
71 * Apps that target {@link android.os.Build.VERSION_CODES#KITKAT} or below
72 * default to allowing third party cookies. Apps targeting
Dianne Hackborn955d8d62014-10-07 20:17:19 -070073 * {@link android.os.Build.VERSION_CODES#LOLLIPOP} or later default to disallowing
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010074 * third party cookies.
75 *
76 * @param webview the {@link WebView} instance to set the cookie policy on
77 * @param accept whether the {@link WebView} instance should accept
78 * third party cookies
79 */
Ignacio Solla451e3382014-11-10 10:35:54 +000080 public abstract void setAcceptThirdPartyCookies(WebView webview, boolean accept);
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010081
82 /**
83 * Gets whether the {@link WebView} should allow third party cookies to be set.
84 *
85 * @param webview the {@link WebView} instance to get the cookie policy for
Nate Fischer0a6140d2017-09-05 12:37:49 -070086 * @return {@code true} if the {@link WebView} accepts third party cookies
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010087 */
Ignacio Solla451e3382014-11-10 10:35:54 +000088 public abstract boolean acceptThirdPartyCookies(WebView webview);
Hector Dearmanfc9a27a2014-06-05 13:45:28 +010089
90 /**
Steve Block77db9992012-02-20 11:46:13 +000091 * Sets a cookie for the given URL. Any existing cookie with the same host,
92 * path and name will be replaced with the new cookie. The cookie being set
Hector Dearman0ac81cb2014-04-30 18:33:18 +010093 * will be ignored if it is expired.
Steve Block4e584df2012-04-24 23:12:47 +010094 *
Hector Dearman24a11d32014-05-21 12:49:12 +010095 * @param url the URL for which the cookie is to be set
Steve Block4e584df2012-04-24 23:12:47 +010096 * @param value the cookie as a string, using the format of the 'Set-Cookie'
Jonathan Dixond3101b12012-04-12 20:51:51 +010097 * HTTP response header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 */
Ignacio Solla451e3382014-11-10 10:35:54 +000099 public abstract void setCookie(String url, String value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100102 * Sets a cookie for the given URL. Any existing cookie with the same host,
103 * path and name will be replaced with the new cookie. The cookie being set
104 * will be ignored if it is expired.
105 * <p>
106 * This method is asynchronous.
107 * If a {@link ValueCallback} is provided,
108 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
109 * thread's {@link android.os.Looper} once the operation is complete.
110 * The value provided to the callback indicates whether the cookie was set successfully.
111 * You can pass {@code null} as the callback if you don't need to know when the operation
112 * completes or whether it succeeded, and in this case it is safe to call the method from a
113 * thread without a Looper.
114 *
115 * @param url the URL for which the cookie is to be set
116 * @param value the cookie as a string, using the format of the 'Set-Cookie'
117 * HTTP response header
118 * @param callback a callback to be executed when the cookie has been set
119 */
Nate Fischer3442c742017-09-08 17:02:00 -0700120 public abstract void setCookie(String url, String value, @Nullable ValueCallback<Boolean>
121 callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100122
123 /**
Steve Block77db9992012-02-20 11:46:13 +0000124 * Gets the cookies for the given URL.
Steve Block4e584df2012-04-24 23:12:47 +0100125 *
126 * @param url the URL for which the cookies are requested
127 * @return value the cookies as a string, using the format of the 'Cookie'
Steve Block77db9992012-02-20 11:46:13 +0000128 * HTTP request header
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000130 public abstract String getCookie(String url);
Kristian Monsen469aebb2011-03-01 20:14:59 +0000131
132 /**
Steve Block4e584df2012-04-24 23:12:47 +0100133 * See {@link #getCookie(String)}.
134 *
135 * @param url the URL for which the cookies are requested
136 * @param privateBrowsing whether to use the private browsing cookie jar
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
Ignacio Solla451e3382014-11-10 10:35:54 +0000139 * @hide Used by Browser and by WebViewProvider implementations.
Kristian Monsen469aebb2011-03-01 20:14:59 +0000140 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000141 @SystemApi
142 public abstract String getCookie(String url, boolean privateBrowsing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144 /**
Steve Block4e584df2012-04-24 23:12:47 +0100145 * 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 -0800146 * request header.
Steve Block4e584df2012-04-24 23:12:47 +0100147 *
148 * @param uri the WebAddress for which the cookies are requested
149 * @return value the cookies as a string, using the format of the 'Cookie'
Steve Block77db9992012-02-20 11:46:13 +0000150 * HTTP request header
Ignacio Solla451e3382014-11-10 10:35:54 +0000151 * @hide Used by RequestHandle and by WebViewProvider implementations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000153 @SystemApi
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 public synchronized String getCookie(WebAddress uri) {
Ignacio Solla451e3382014-11-10 10:35:54 +0000155 return getCookie(uri.toString());
Kristian Monsen85173052010-12-08 15:29:29 +0000156 }
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 /**
Steve Block77db9992012-02-20 11:46:13 +0000159 * Removes all session cookies, which are cookies without an expiration
160 * date.
Hector Dearman24a11d32014-05-21 12:49:12 +0100161 * @deprecated use {@link #removeSessionCookies(ValueCallback)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700163 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000164 public abstract void removeSessionCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100167 * Removes all session cookies, which are cookies without an expiration
168 * date.
169 * <p>
170 * This method is asynchronous.
171 * If a {@link ValueCallback} is provided,
172 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
173 * thread's {@link android.os.Looper} once the operation is complete.
174 * The value provided to the callback indicates whether any cookies were removed.
175 * You can pass {@code null} as the callback if you don't need to know when the operation
176 * completes or whether any cookie were removed, and in this case it is safe to call the
177 * method from a thread without a Looper.
178 * @param callback a callback which is executed when the session cookies have been removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 */
Nate Fischer3442c742017-09-08 17:02:00 -0700180 public abstract void removeSessionCookies(@Nullable ValueCallback<Boolean> callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100181
182 /**
183 * Removes all cookies.
184 * @deprecated Use {@link #removeAllCookies(ValueCallback)} instead.
185 */
186 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000187 public abstract void removeAllCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189 /**
Hector Dearman24a11d32014-05-21 12:49:12 +0100190 * Removes all cookies.
191 * <p>
192 * This method is asynchronous.
193 * If a {@link ValueCallback} is provided,
194 * {@link ValueCallback#onReceiveValue(T) onReceiveValue()} will be called on the current
195 * thread's {@link android.os.Looper} once the operation is complete.
196 * The value provided to the callback indicates whether any cookies were removed.
197 * You can pass {@code null} as the callback if you don't need to know when the operation
198 * completes or whether any cookies were removed, and in this case it is safe to call the
199 * method from a thread without a Looper.
200 * @param callback a callback which is executed when the cookies have been removed
201 */
Nate Fischer3442c742017-09-08 17:02:00 -0700202 public abstract void removeAllCookies(@Nullable ValueCallback<Boolean> callback);
Hector Dearman24a11d32014-05-21 12:49:12 +0100203
204 /**
Steve Block77db9992012-02-20 11:46:13 +0000205 * Gets whether there are stored cookies.
Steve Block4e584df2012-04-24 23:12:47 +0100206 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700207 * @return {@code true} if there are stored cookies
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000209 public abstract boolean hasCookies();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 /**
Jonathan Dixond3101b12012-04-12 20:51:51 +0100212 * See {@link #hasCookies()}.
Steve Block4e584df2012-04-24 23:12:47 +0100213 *
214 * @param privateBrowsing whether to use the private browsing cookie jar
Ignacio Solla451e3382014-11-10 10:35:54 +0000215 * @hide Used by Browser and WebViewProvider implementations.
Kristian Monsen469aebb2011-03-01 20:14:59 +0000216 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000217 @SystemApi
218 public abstract boolean hasCookies(boolean privateBrowsing);
Kristian Monsen469aebb2011-03-01 20:14:59 +0000219
220 /**
Steve Block77db9992012-02-20 11:46:13 +0000221 * Removes all expired cookies.
Hector Dearman24a11d32014-05-21 12:49:12 +0100222 * @deprecated The WebView handles removing expired cookies automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 */
Hector Dearman24a11d32014-05-21 12:49:12 +0100224 @Deprecated
Ignacio Solla451e3382014-11-10 10:35:54 +0000225 public abstract void removeExpiredCookie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 /**
Hector Dearmana70d1d92014-05-22 13:50:15 +0100228 * Ensures all cookies currently accessible through the getCookie API are
229 * written to persistent storage.
230 * This call will block the caller until it is done and may perform I/O.
231 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000232 public abstract void flush();
Iain Merrickc40fc2c2010-11-30 11:44:32 +0000233
234 /**
Steve Block77db9992012-02-20 11:46:13 +0000235 * Gets whether the application's {@link WebView} instances send and accept
236 * cookies for file scheme URLs.
Steve Block4e584df2012-04-24 23:12:47 +0100237 *
Nate Fischer0a6140d2017-09-05 12:37:49 -0700238 * @return {@code true} if {@link WebView} instances send and accept cookies for
Steve Block77db9992012-02-20 11:46:13 +0000239 * file scheme URLs
Kristian Monsen16d041c2010-12-06 13:43:51 +0000240 */
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100241 // Static for backward compatibility.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000242 public static boolean allowFileSchemeCookies() {
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100243 return getInstance().allowFileSchemeCookiesImpl();
244 }
245
246 /**
Steve Block4e584df2012-04-24 23:12:47 +0100247 * Implements {@link #allowFileSchemeCookies()}.
248 *
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100249 * @hide Only for use by WebViewProvider implementations
250 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000251 @SystemApi
252 protected abstract boolean allowFileSchemeCookiesImpl();
Kristian Monsen16d041c2010-12-06 13:43:51 +0000253
254 /**
Steve Block77db9992012-02-20 11:46:13 +0000255 * Sets whether the application's {@link WebView} instances should send and
256 * accept cookies for file scheme URLs.
Hector Dearman9f595262014-07-21 11:00:30 +0100257 * Use of cookies with file scheme URLs is potentially insecure and turned
258 * off by default.
259 * Do not use this feature unless you can be sure that no unintentional
260 * sharing of cookie data can take place.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000261 * <p>
Steve Block77db9992012-02-20 11:46:13 +0000262 * Note that calls to this method will have no effect if made after a
263 * {@link WebView} or CookieManager instance has been created.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000264 */
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100265 // Static for backward compatibility.
Kristian Monsen16d041c2010-12-06 13:43:51 +0000266 public static void setAcceptFileSchemeCookies(boolean accept) {
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100267 getInstance().setAcceptFileSchemeCookiesImpl(accept);
268 }
269
270 /**
Steve Block4e584df2012-04-24 23:12:47 +0100271 * Implements {@link #setAcceptFileSchemeCookies(boolean)}.
272 *
Jonathan Dixonbe58c402012-04-24 09:48:28 +0100273 * @hide Only for use by WebViewProvider implementations
274 */
Ignacio Solla451e3382014-11-10 10:35:54 +0000275 @SystemApi
276 protected abstract void setAcceptFileSchemeCookiesImpl(boolean accept);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}