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 // TODO(yongsheng): remove public modifier.
16 public final class XWalkCookieManager {
18 * Control whether cookie is enabled or disabled
19 * @param accept TRUE if accept cookie
21 public void setAcceptCookie(boolean accept) {
22 nativeSetAcceptCookie(accept);
26 * Return whether cookie is enabled
27 * @return TRUE if accept cookie
29 public boolean acceptCookie() {
30 return nativeAcceptCookie();
34 * Set cookie for a given url. The old cookie with same host/path/name will
35 * be removed. The new cookie will be added if it is not expired or it does
36 * not have expiration which implies it is session cookie.
37 * @param url The url which cookie is set for
38 * @param value The value for set-cookie: in http response header
40 public void setCookie(final String url, final String value) {
41 nativeSetCookie(url, value);
45 * Get cookie(s) for a given url so that it can be set to "cookie:" in http
47 * @param url The url needs cookie
48 * @return The cookies in the format of NAME=VALUE [; NAME=VALUE]
50 public String getCookie(final String url) {
51 String cookie = nativeGetCookie(url.toString());
52 // Return null if the string is empty to match legacy behavior
53 return cookie == null || cookie.trim().isEmpty() ? null : cookie;
57 * Remove all session cookies, which are cookies without expiration date
59 public void removeSessionCookie() {
60 nativeRemoveSessionCookie();
66 public void removeAllCookie() {
67 nativeRemoveAllCookie();
71 * Return true if there are stored cookies.
73 public boolean hasCookies() {
74 return nativeHasCookies();
78 * Remove all expired cookies
80 public void removeExpiredCookie() {
81 nativeRemoveExpiredCookie();
84 public void flushCookieStore() {
85 nativeFlushCookieStore();
89 * Whether cookies are accepted for file scheme URLs.
91 public boolean allowFileSchemeCookies() {
92 return nativeAllowFileSchemeCookies();
96 * Sets whether cookies are accepted for file scheme URLs.
98 * Use of cookies with file scheme URLs is potentially insecure. Do not
99 * use this feature unless you can be sure that no unintentional sharing
100 * of cookie data can take place.
102 * Note that calls to this method will have no effect if made after a
103 * WebView or CookieManager instance has been created.
105 public void setAcceptFileSchemeCookies(boolean accept) {
106 nativeSetAcceptFileSchemeCookies(accept);
109 private native void nativeSetAcceptCookie(boolean accept);
110 private native boolean nativeAcceptCookie();
112 private native void nativeSetCookie(String url, String value);
113 private native String nativeGetCookie(String url);
115 private native void nativeRemoveSessionCookie();
116 private native void nativeRemoveAllCookie();
117 private native void nativeRemoveExpiredCookie();
118 private native void nativeFlushCookieStore();
120 private native boolean nativeHasCookies();
122 private native boolean nativeAllowFileSchemeCookies();
123 private native void nativeSetAcceptFileSchemeCookies(boolean accept);