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.internal;
7 import org.chromium.base.JNINamespace;
10 * XWalkCookieManager manages cookies according to RFC2109 spec.
12 * Methods in this class are thread safe.
16 @JNINamespace("xwalk")
17 public final class XWalkCookieManager {
19 * Control whether cookie is enabled or disabled
20 * @param accept TRUE if accept cookie
22 public void setAcceptCookie(boolean accept) {
23 nativeSetAcceptCookie(accept);
27 * Return whether cookie is enabled
28 * @return TRUE if accept cookie
30 public boolean acceptCookie() {
31 return nativeAcceptCookie();
35 * Set cookie for a given url. The old cookie with same host/path/name will
36 * be removed. The new cookie will be added if it is not expired or it does
37 * not have expiration which implies it is session cookie.
38 * @param url The url which cookie is set for
39 * @param value The value for set-cookie: in http response header
41 public void setCookie(final String url, final String value) {
42 nativeSetCookie(url, value);
46 * Get cookie(s) for a given url so that it can be set to "cookie:" in http
48 * @param url The url needs cookie
49 * @return The cookies in the format of NAME=VALUE [; NAME=VALUE]
51 public String getCookie(final String url) {
52 String cookie = nativeGetCookie(url.toString());
53 // Return null if the string is empty to match legacy behavior
54 return cookie == null || cookie.trim().isEmpty() ? null : cookie;
58 * Remove all session cookies, which are cookies without expiration date
60 public void removeSessionCookie() {
61 nativeRemoveSessionCookie();
67 public void removeAllCookie() {
68 nativeRemoveAllCookie();
72 * Return true if there are stored cookies.
74 public boolean hasCookies() {
75 return nativeHasCookies();
79 * Remove all expired cookies
81 public void removeExpiredCookie() {
82 nativeRemoveExpiredCookie();
85 public void flushCookieStore() {
86 nativeFlushCookieStore();
90 * Whether cookies are accepted for file scheme URLs.
92 public boolean allowFileSchemeCookies() {
93 return nativeAllowFileSchemeCookies();
97 * Sets whether cookies are accepted for file scheme URLs.
99 * Use of cookies with file scheme URLs is potentially insecure. Do not
100 * use this feature unless you can be sure that no unintentional sharing
101 * of cookie data can take place.
103 * Note that calls to this method will have no effect if made after a
104 * WebView or CookieManager instance has been created.
106 public void setAcceptFileSchemeCookies(boolean accept) {
107 nativeSetAcceptFileSchemeCookies(accept);
110 private native void nativeSetAcceptCookie(boolean accept);
111 private native boolean nativeAcceptCookie();
113 private native void nativeSetCookie(String url, String value);
114 private native String nativeGetCookie(String url);
116 private native void nativeRemoveSessionCookie();
117 private native void nativeRemoveAllCookie();
118 private native void nativeRemoveExpiredCookie();
119 private native void nativeFlushCookieStore();
121 private native boolean nativeHasCookies();
123 private native boolean nativeAllowFileSchemeCookies();
124 private native void nativeSetAcceptFileSchemeCookies(boolean accept);