1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core;
7 import org.chromium.base.JNINamespace;
10 * XWalkCookieManager manages cookies according to RFC2109 spec.
12 * Methods in this class are thread safe.
14 @JNINamespace("xwalk")
15 public final class XWalkCookieManager {
17 * Control whether cookie is enabled or disabled
18 * @param accept TRUE if accept cookie
20 public void setAcceptCookie(boolean accept) {
21 nativeSetAcceptCookie(accept);
25 * Return whether cookie is enabled
26 * @return TRUE if accept cookie
28 public boolean acceptCookie() {
29 return nativeAcceptCookie();
33 * Set cookie for a given url. The old cookie with same host/path/name will
34 * be removed. The new cookie will be added if it is not expired or it does
35 * not have expiration which implies it is session cookie.
36 * @param url The url which cookie is set for
37 * @param value The value for set-cookie: in http response header
39 public void setCookie(final String url, final String value) {
40 nativeSetCookie(url, value);
44 * Get cookie(s) for a given url so that it can be set to "cookie:" in http
46 * @param url The url needs cookie
47 * @return The cookies in the format of NAME=VALUE [; NAME=VALUE]
49 public String getCookie(final String url) {
50 String cookie = nativeGetCookie(url.toString());
51 // Return null if the string is empty to match legacy behavior
52 return cookie == null || cookie.trim().isEmpty() ? null : cookie;
56 * Remove all session cookies, which are cookies without expiration date
58 public void removeSessionCookie() {
59 nativeRemoveSessionCookie();
65 public void removeAllCookie() {
66 nativeRemoveAllCookie();
70 * Return true if there are stored cookies.
72 public boolean hasCookies() {
73 return nativeHasCookies();
77 * Remove all expired cookies
79 public void removeExpiredCookie() {
80 nativeRemoveExpiredCookie();
83 public void flushCookieStore() {
84 nativeFlushCookieStore();
88 * Whether cookies are accepted for file scheme URLs.
90 public boolean allowFileSchemeCookies() {
91 return nativeAllowFileSchemeCookies();
95 * Sets whether cookies are accepted for file scheme URLs.
97 * Use of cookies with file scheme URLs is potentially insecure. Do not
98 * use this feature unless you can be sure that no unintentional sharing
99 * of cookie data can take place.
101 * Note that calls to this method will have no effect if made after a
102 * WebView or CookieManager instance has been created.
104 public void setAcceptFileSchemeCookies(boolean accept) {
105 nativeSetAcceptFileSchemeCookies(accept);
108 private native void nativeSetAcceptCookie(boolean accept);
109 private native boolean nativeAcceptCookie();
111 private native void nativeSetCookie(String url, String value);
112 private native String nativeGetCookie(String url);
114 private native void nativeRemoveSessionCookie();
115 private native void nativeRemoveAllCookie();
116 private native void nativeRemoveExpiredCookie();
117 private native void nativeFlushCookieStore();
119 private native boolean nativeHasCookies();
121 private native boolean nativeAllowFileSchemeCookies();
122 private native void nativeSetAcceptFileSchemeCookies(boolean accept);