The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.net.WebAddress; |
Doug Zongker | 45a9a14 | 2010-02-03 13:52:18 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 22 | * Manages the cookies used by an application's {@link WebView} instances. |
| 23 | * Cookies are manipulated according to RFC2109. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | */ |
Jonathan Dixon | 939e504 | 2012-04-12 21:21:07 +0100 | [diff] [blame] | 25 | public class CookieManager { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 26 | /** |
| 27 | * @hide Only for use by WebViewProvider implementations |
| 28 | */ |
| 29 | protected CookieManager() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 32 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | protected Object clone() throws CloneNotSupportedException { |
| 34 | throw new CloneNotSupportedException("doesn't implement Cloneable"); |
| 35 | } |
| 36 | |
| 37 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 38 | * Gets the singleton CookieManager instance. If this method is used |
| 39 | * before the application instantiates a {@link WebView} instance, |
Steve Block | c723e35 | 2012-08-14 14:48:05 +0100 | [diff] [blame] | 40 | * {@link CookieSyncManager#createInstance CookieSyncManager.createInstance(Context)} |
| 41 | * must be called first. |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 42 | * |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 43 | * @return the singleton CookieManager instance |
Patrick Scott | 06312ca | 2010-03-19 15:40:05 -0400 | [diff] [blame] | 44 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | public static synchronized CookieManager getInstance() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 46 | return WebViewFactory.getProvider().getCookieManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 50 | * Sets whether the application's {@link WebView} instances should send and |
| 51 | * accept cookies. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 52 | * |
| 53 | * @param accept whether {@link WebView} instances should send and accept |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 54 | * cookies |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | */ |
| 56 | public synchronized void setAcceptCookie(boolean accept) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 57 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 61 | * Gets whether the application's {@link WebView} instances send and accept |
| 62 | * cookies. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 63 | * |
| 64 | * @return true if {@link WebView} instances send and accept cookies |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | */ |
| 66 | public synchronized boolean acceptCookie() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 67 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 70 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 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 |
Hector Dearman | 0ac81cb | 2014-04-30 18:33:18 +0100 | [diff] [blame] | 73 | * will be ignored if it is expired. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 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' |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 77 | * HTTP response header |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | */ |
| 79 | public void setCookie(String url, String value) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 80 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 84 | * Gets the cookies for the given URL. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 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' |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 88 | * HTTP request header |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | */ |
| 90 | public String getCookie(String url) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 91 | throw new MustOverrideException(); |
Kristian Monsen | 469aebb | 2011-03-01 20:14:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 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' |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 100 | * HTTP request header |
| 101 | * @hide Used by Browser, no intention to publish. |
Kristian Monsen | 469aebb | 2011-03-01 20:14:59 +0000 | [diff] [blame] | 102 | */ |
| 103 | public String getCookie(String url, boolean privateBrowsing) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 104 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 108 | * Gets cookie(s) for a given uri so that it can be set to "cookie:" in http |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | * request header. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 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' |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 113 | * HTTP request header |
| 114 | * @hide Used by RequestHandle, no intention to publish. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | */ |
| 116 | public synchronized String getCookie(WebAddress uri) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 117 | throw new MustOverrideException(); |
Kristian Monsen | 8517305 | 2010-12-08 15:29:29 +0000 | [diff] [blame] | 118 | } |
| 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 121 | * Removes all session cookies, which are cookies without an expiration |
| 122 | * date. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | */ |
| 124 | public void removeSessionCookie() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 125 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 129 | * Removes all cookies. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | */ |
| 131 | public void removeAllCookie() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 132 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 136 | * Gets whether there are stored cookies. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 137 | * |
| 138 | * @return true if there are stored cookies |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | */ |
| 140 | public synchronized boolean hasCookies() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 141 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 145 | * See {@link #hasCookies()}. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 146 | * |
| 147 | * @param privateBrowsing whether to use the private browsing cookie jar |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 148 | * @hide Used by Browser, no intention to publish. |
Kristian Monsen | 469aebb | 2011-03-01 20:14:59 +0000 | [diff] [blame] | 149 | */ |
| 150 | public synchronized boolean hasCookies(boolean privateBrowsing) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 151 | throw new MustOverrideException(); |
Kristian Monsen | 469aebb | 2011-03-01 20:14:59 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 155 | * Removes all expired cookies. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | */ |
| 157 | public void removeExpiredCookie() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 158 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 162 | * Flushes all cookies managed by the Chrome HTTP stack to flash. |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 163 | * |
| 164 | * @hide Package level api, called from CookieSyncManager |
Iain Merrick | c40fc2c | 2010-11-30 11:44:32 +0000 | [diff] [blame] | 165 | */ |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 166 | protected void flushCookieStore() { |
| 167 | throw new MustOverrideException(); |
Iain Merrick | c40fc2c | 2010-11-30 11:44:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 171 | * Gets whether the application's {@link WebView} instances send and accept |
| 172 | * cookies for file scheme URLs. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 173 | * |
| 174 | * @return true if {@link WebView} instances send and accept cookies for |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 175 | * file scheme URLs |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 176 | */ |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 177 | // Static for backward compatibility. |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 178 | public static boolean allowFileSchemeCookies() { |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 179 | return getInstance().allowFileSchemeCookiesImpl(); |
| 180 | } |
| 181 | |
| 182 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 183 | * Implements {@link #allowFileSchemeCookies()}. |
| 184 | * |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 185 | * @hide Only for use by WebViewProvider implementations |
| 186 | */ |
| 187 | protected boolean allowFileSchemeCookiesImpl() { |
| 188 | throw new MustOverrideException(); |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 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. |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 197 | * <p> |
Steve Block | 77db999 | 2012-02-20 11:46:13 +0000 | [diff] [blame] | 198 | * Note that calls to this method will have no effect if made after a |
| 199 | * {@link WebView} or CookieManager instance has been created. |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 200 | */ |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 201 | // Static for backward compatibility. |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 202 | public static void setAcceptFileSchemeCookies(boolean accept) { |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 203 | getInstance().setAcceptFileSchemeCookiesImpl(accept); |
| 204 | } |
| 205 | |
| 206 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 207 | * Implements {@link #setAcceptFileSchemeCookies(boolean)}. |
| 208 | * |
Jonathan Dixon | be58c40 | 2012-04-24 09:48:28 +0100 | [diff] [blame] | 209 | * @hide Only for use by WebViewProvider implementations |
| 210 | */ |
| 211 | protected void setAcceptFileSchemeCookiesImpl(boolean accept) { |
| 212 | throw new MustOverrideException(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | } |