741095f4dc53d18cafb30cef28bfc143801fd794
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkCookieManager.java
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.
4
5 package org.xwalk.core;
6
7 import org.chromium.base.JNINamespace;
8
9 /**
10  * XWalkCookieManager manages cookies according to RFC2109 spec.
11  *
12  * Methods in this class are thread safe.
13  */
14 @JNINamespace("xwalk")
15 // TODO(yongsheng): remove public modifier.
16 public final class XWalkCookieManager {
17     /**
18      * Control whether cookie is enabled or disabled
19      * @param accept TRUE if accept cookie
20      */
21     public void setAcceptCookie(boolean accept) {
22         nativeSetAcceptCookie(accept);
23     }
24
25     /**
26      * Return whether cookie is enabled
27      * @return TRUE if accept cookie
28      */
29     public boolean acceptCookie() {
30         return nativeAcceptCookie();
31     }
32
33     /**
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
39      */
40     public void setCookie(final String url, final String value) {
41         nativeSetCookie(url, value);
42     }
43
44     /**
45      * Get cookie(s) for a given url so that it can be set to "cookie:" in http
46      * request header.
47      * @param url The url needs cookie
48      * @return The cookies in the format of NAME=VALUE [; NAME=VALUE]
49      */
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;
54     }
55
56     /**
57      * Remove all session cookies, which are cookies without expiration date
58      */
59     public void removeSessionCookie() {
60         nativeRemoveSessionCookie();
61     }
62
63     /**
64      * Remove all cookies
65      */
66     public void removeAllCookie() {
67         nativeRemoveAllCookie();
68     }
69
70     /**
71      *  Return true if there are stored cookies.
72      */
73     public boolean hasCookies() {
74         return nativeHasCookies();
75     }
76
77     /**
78      * Remove all expired cookies
79      */
80     public void removeExpiredCookie() {
81         nativeRemoveExpiredCookie();
82     }
83
84     public void flushCookieStore() {
85         nativeFlushCookieStore();
86     }
87
88     /**
89      * Whether cookies are accepted for file scheme URLs.
90      */
91     public boolean allowFileSchemeCookies() {
92         return nativeAllowFileSchemeCookies();
93     }
94
95     /**
96      * Sets whether cookies are accepted for file scheme URLs.
97      *
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.
101      * <p>
102      * Note that calls to this method will have no effect if made after a
103      * WebView or CookieManager instance has been created.
104      */
105     public void setAcceptFileSchemeCookies(boolean accept) {
106         nativeSetAcceptFileSchemeCookies(accept);
107     }
108
109     private native void nativeSetAcceptCookie(boolean accept);
110     private native boolean nativeAcceptCookie();
111
112     private native void nativeSetCookie(String url, String value);
113     private native String nativeGetCookie(String url);
114
115     private native void nativeRemoveSessionCookie();
116     private native void nativeRemoveAllCookie();
117     private native void nativeRemoveExpiredCookie();
118     private native void nativeFlushCookieStore();
119
120     private native boolean nativeHasCookies();
121
122     private native boolean nativeAllowFileSchemeCookies();
123     private native void nativeSetAcceptFileSchemeCookies(boolean accept);
124 }