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