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