CookieManager manages cookies according to RFC2109 spec.
private static final char PERIOD = '.';
private static final char COMMA = ',';
private static final char EQUAL = '=';
This contains a list of 2nd-level domains that aren't allowed to have
wildcards when combined with country-codes. For example: [.co.uk].
{ "ac", "co", "com", "ed", "edu", "go", "gouv", "gov", "info", "lg", "ne", "net", "or", "org" };
Package level class to be accessed by cookie sync manager
boolean valuesMatch = !((value == null) ^ (in.value == null));
int urlLen = urlHost.length();
Log.w(LOGTAG, "Empty cookie path");
int urlLen = urlPath.length();
if (diff != 0) return diff;
if (diff != 0) return diff;
if (cookie2.value == null) { if (cookie1.value != null) { } else if (cookie1.value == null) { Get a singleton CookieManager. If this is called before any
WebView is created or outside of
WebView context, the
caller needs to call
CookieSyncManager#createInstance(Context )
first.
Control whether cookie is enabled or disabled
- Parameters:
accept TRUE if accept cookie
Return whether cookie is enabled
- Returns:
- TRUE if accept cookie
Set cookie for a given url. The old cookie with same host/path/name will
be removed. The new cookie will be added if it is not expired or it does
not have expiration which implies it is session cookie.
- Parameters:
url The url which cookie is set forvalue The value for set-cookie: in http response header
Log.e(LOGTAG, "Bad address: " + url);
Set cookie for a given uri. The old cookie with same host/path/name will
be removed. The new cookie will be added if it is not expired or it does
not have expiration which implies it is session cookie.
- Parameters:
uri The uri which cookie is set forvalue The value for set-cookie: in http response header- Hide:
- - hide this because it takes in a parameter of type WebAddress,
a system private class.
Log.v(LOGTAG, "setCookie: uri: " + uri + " value: " + value);
if (hostAndPath == null) { if (hostAndPath[1].length() > 1) { index > 0 ? index : index + 1);
cookies = parseCookie(hostAndPath[0], hostAndPath[1], value);
Log.e(LOGTAG, "parse cookie failed for: " + value);
if (cookies == null || cookies.size() == 0) { if (cookieList == null) { int size = cookies.size();
for (int i = 0; i < size; i++) { if (cookie.expires < 0 || cookie.expires > now) { if (!cookieEntry.secure || HTTPS.equals(uri.mScheme)) { cookieEntry.value = cookie.value;
cookieEntry.expires = cookie.expires;
cookieEntry.secure = cookie.secure;
cookieEntry.lastAcessTime = now;
cookieEntry.lastUpdateTime = now;
cookieEntry.lastUpdateTime = now;
if (!done && (cookie.expires < 0 || cookie.expires > now)) { cookie.lastAcessTime = now;
cookie.lastUpdateTime = now;
toDelete.lastAcessTime = now;
if ((cookieEntry2.lastAcessTime < toDelete.lastAcessTime)
Get cookie(s) for a given url so that it can be set to "cookie:" in http
request header.
- Parameters:
url The url needs cookie- Returns:
- The cookies in the format of NAME=VALUE [; NAME=VALUE]
Log.e(LOGTAG, "Bad address: " + url);
Get cookie(s) for a given uri so that it can be set to "cookie:" in http
request header.
- Parameters:
uri The uri needs cookie- Returns:
- The cookies in the format of NAME=VALUE [; NAME=VALUE]
- Hide:
- - hide this because it has a parameter of type WebAddress, which
is a system private class.
if (hostAndPath == null) { if (cookieList == null) { && (cookie.expires < 0 || cookie.expires > now)
&& (!cookie.secure || secure)
cookie.lastAcessTime = now;
if (cookie.value != null) { Log.v(LOGTAG, "getCookie: uri: " + uri + " value: " + ret);
Log.v(LOGTAG, "getCookie: uri: " + uri
+ " But can't find cookie.");
Remove all session cookies, which are cookies without expiration date
if (cookie.expires == -1) { Return true if there are stored cookies.
Remove all expired cookies
if (cookie.expires > 0 && cookie.expires < now) { Package level api, called from CookieSyncManager
Get a list of cookies which are updated since a given time.
- Parameters:
last The given time in millisec- Returns:
- A list of cookies
if (cookie.lastUpdateTime > last) { Package level api, called from CookieSyncManager
Delete a Cookie in the RAM
- Parameters:
cookie Cookie to be deleted
if (cookieList != null) { Package level api, called from CookieSyncManager
Called after a cookie is synced to FLASH
- Parameters:
cookie Cookie to be synced
Package level api, called from CookieSyncManager
Delete the least recent used domains if the total cookie count in RAM
exceeds the limit
- Returns:
- A list of cookies which are removed from RAM
byteCount += cookie.domain.length()
Log.v(LOGTAG, count + " cookies used " + byteCount
+ " bytes with " + mapSize + " domains");
int toGo = mapSize / 10 + 1;
Log.v(LOGTAG, "delete domain: " + domain
Extract the host and path out of a uri
- Parameters:
uri The given WebAddress- Returns:
- The host and path in the format of String[], String[0] is host
which has at least two periods, String[1] is path which always
ended with "/"
if (uri.mHost != null && uri.mPath != null) { Get the base domain for a give host. E.g. mail.google.com will return
google.com
- Parameters:
host The give host- Returns:
- the base domain
while (nextIndex < lastIndex) { startIndex = nextIndex + 1;
parseCookie() parses the cookieString which is a comma-separated list of
one or more cookies in the format of "NAME=VALUE; expires=DATE;
path=PATH; domain=DOMAIN_NAME; secure httponly" to a list of Cookies.
Here is a sample: IGDND=1, IGPC=ET=UB8TSNwtDmQ:AF=0; expires=Sun,
17-Jan-2038 19:14:07 GMT; path=/ig; domain=.google.com, =,
PREF=ID=408909b1b304593d:TM=1156459854:LM=1156459854:S=V-vCAU6Sh-gobCfO;
expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com which
contains 3 cookies IGDND, IGPC, PREF and an empty cookie
- Parameters:
host The default hostpath The default pathcookieString The string coming from "Set-Cookie:"- Returns:
- A list of Cookies
int length = cookieString.length();
if (index < 0 || index >= length) { cookie = new Cookie(host, path);
if ((semicolonIndex != -1 && (semicolonIndex < equalIndex)) ||
if (semicolonIndex == -1) { cookie.name = cookieString.substring(index, semicolonIndex);
cookie.name = cookieString.substring(index, equalIndex);
if ((equalIndex < length - 1) &&
if (semicolonIndex == -1) { cookie.value = cookieString.substring(equalIndex + 1,
} else if (equalIndex + 1 == semicolonIndex
|| semicolonIndex < equalIndex) { cookie.value = cookieString.substring(equalIndex + 1,
if (index < 0 || index >= length) { if (index == length) break;
if (index == length) break;
(comaIndex - equalIndex <= 10)) { if (semicolonIndex == -1 && commaIndex == -1) { } else if (semicolonIndex == -1) { } else if (commaIndex == -1) { index = Math.min(semicolonIndex, commaIndex);
cookieString.substring(equalIndex + 1, index);
cookie.expires = AndroidHttpClient.parseDate(value);
"illegal format for expires: " + value);
"illegal format for max-age: " + value);
if ((len == lastPeriod + 3)
&& (len >= 6 && len <= 8)) { if (cookie != null && cookie.domain != null) {