1 | // Copyright (c) 2012 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.chromium.android_webview; |
6 | |
7 | import android.net.ParseException; |
8 | import android.util.Log; |
9 | |
10 | import org.chromium.base.JNINamespace; |
11 | import org.chromium.base.ThreadUtils; |
12 | |
13 | import java.util.concurrent.Callable; |
14 | |
15 | /** |
16 | * AwCookieManager manages cookies according to RFC2109 spec. |
17 | * |
18 | * Methods in this class are thread safe. |
19 | */ |
20 | @JNINamespace("android_webview") |
21 | public final class AwCookieManager { |
22 | /** |
23 | * Control whether cookie is enabled or disabled |
24 | * @param accept TRUE if accept cookie |
25 | */ |
26 | public void setAcceptCookie(boolean accept) { |
27 | nativeSetAcceptCookie(accept); |
28 | } |
29 | |
30 | /** |
31 | * Return whether cookie is enabled |
32 | * @return TRUE if accept cookie |
33 | */ |
34 | public boolean acceptCookie() { |
35 | return nativeAcceptCookie(); |
36 | } |
37 | |
38 | /** |
39 | * Set cookie for a given url. The old cookie with same host/path/name will |
40 | * be removed. The new cookie will be added if it is not expired or it does |
41 | * not have expiration which implies it is session cookie. |
42 | * @param url The url which cookie is set for |
43 | * @param value The value for set-cookie: in http response header |
44 | */ |
45 | public void setCookie(final String url, final String value) { |
46 | nativeSetCookie(url, value); |
47 | } |
48 | |
49 | /** |
50 | * Get cookie(s) for a given url so that it can be set to "cookie:" in http |
51 | * request header. |
52 | * @param url The url needs cookie |
53 | * @return The cookies in the format of NAME=VALUE [; NAME=VALUE] |
54 | */ |
55 | public String getCookie(final String url) { |
56 | String cookie = nativeGetCookie(url.toString()); |
57 | // Return null if the string is empty to match legacy behavior |
58 | return cookie == null || cookie.trim().isEmpty() ? null : cookie; |
59 | } |
60 | |
61 | /** |
62 | * Remove all session cookies, which are cookies without expiration date |
63 | */ |
64 | public void removeSessionCookie() { |
65 | nativeRemoveSessionCookie(); |
66 | } |
67 | |
68 | /** |
69 | * Remove all cookies |
70 | */ |
71 | public void removeAllCookie() { |
72 | nativeRemoveAllCookie(); |
73 | } |
74 | |
75 | /** |
76 | * Return true if there are stored cookies. |
77 | */ |
78 | public boolean hasCookies() { |
79 | return nativeHasCookies(); |
80 | } |
81 | |
82 | /** |
83 | * Remove all expired cookies |
84 | */ |
85 | public void removeExpiredCookie() { |
86 | nativeRemoveExpiredCookie(); |
87 | } |
88 | |
89 | public void flushCookieStore() { |
90 | nativeFlushCookieStore(); |
91 | } |
92 | |
93 | /** |
94 | * Whether cookies are accepted for file scheme URLs. |
95 | */ |
96 | public boolean allowFileSchemeCookies() { |
97 | return nativeAllowFileSchemeCookies(); |
98 | } |
99 | |
100 | /** |
101 | * Sets whether cookies are accepted for file scheme URLs. |
102 | * |
103 | * Use of cookies with file scheme URLs is potentially insecure. Do not use this feature unless |
104 | * you can be sure that no unintentional sharing of cookie data can take place. |
105 | * <p> |
106 | * Note that calls to this method will have no effect if made after a WebView or CookieManager |
107 | * instance has been created. |
108 | */ |
109 | public void setAcceptFileSchemeCookies(boolean accept) { |
110 | nativeSetAcceptFileSchemeCookies(accept); |
111 | } |
112 | |
113 | private native void nativeSetAcceptCookie(boolean accept); |
114 | private native boolean nativeAcceptCookie(); |
115 | |
116 | private native void nativeSetCookie(String url, String value); |
117 | private native String nativeGetCookie(String url); |
118 | |
119 | private native void nativeRemoveSessionCookie(); |
120 | private native void nativeRemoveAllCookie(); |
121 | private native void nativeRemoveExpiredCookie(); |
122 | private native void nativeFlushCookieStore(); |
123 | |
124 | private native boolean nativeHasCookies(); |
125 | |
126 | private native boolean nativeAllowFileSchemeCookies(); |
127 | private native void nativeSetAcceptFileSchemeCookies(boolean accept); |
128 | } |