Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1925)

Unified Diff: android_webview/java/src/org/chromium/android_webview/AwCookieManager.java

Issue 273873003: Adds Asynchronous APIs to CookieManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/java/src/org/chromium/android_webview/AwCookieManager.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AwCookieManager.java b/android_webview/java/src/org/chromium/android_webview/AwCookieManager.java
index ab91e5d48bf9f55844b2fa350e8cdcb9a719cc30..08a7a209da894d319ed2c385f994edb987265e66 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwCookieManager.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwCookieManager.java
@@ -4,6 +4,11 @@
package org.chromium.android_webview;
+import android.os.Handler;
+import android.os.Looper;
+import android.webkit.ValueCallback;
+
+import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
/**
@@ -46,14 +51,42 @@ public final class AwCookieManager {
}
/**
+ * Synchronous version of setCookie.
+ */
+ public void setCookie(String url, String value) {
+ nativeSetCookieSync(url, value);
+ }
+
+ /**
+ * Deprecated synchronous version of removeSessionCookies.
+ */
+ public void removeSessionCookie() {
+ nativeRemoveSessionCookieSync();
+ }
+
+ /**
+ * Deprecated synchronous version of removeAllCookies.
+ */
+ public void removeAllCookie() {
+ nativeRemoveAllCookieSync();
+ }
+
+ /**
* Set cookie for a given url. The old cookie with same host/path/name will
* be removed. The new cookie will be added if it is not expired or it does
* not have expiration which implies it is session cookie.
- * @param url The url which cookie is set for
- * @param value The value for set-cookie: in http response header
+ * @param url The url which cookie is set for.
+ * @param value The value for set-cookie: in http response header.
+ * @param callback A callback called with the success status after the cookie is set.
*/
- public void setCookie(final String url, final String value) {
- nativeSetCookie(url, value);
+ public void setCookie(final String url, final String value,
+ final ValueCallback<Boolean> callback) {
+ try {
+ nativeSetCookie(url, value, CookieCallback.create(callback));
+ } catch (IllegalStateException e) {
+ throw new IllegalStateException(
+ "SetCookie must be called on a thread with a running Looper.");
+ }
}
/**
@@ -70,16 +103,28 @@ public final class AwCookieManager {
/**
* Remove all session cookies, which are cookies without expiration date
+ * @param callback A callback called after the cookies are removed with the number removed.
Torne 2014/05/12 14:27:35 Not sure the number removed is actually useful? Th
mkosiba (inactive) 2014/05/13 09:37:17 I guess you could check whether the value was 0. I
hjd_google 2014/05/15 10:52:07 This makes sense, I've moved the API to returning
*/
- public void removeSessionCookie() {
- nativeRemoveSessionCookie();
+ public void removeSessionCookies(ValueCallback<Integer> callback) {
+ try {
+ nativeRemoveSessionCookie(CookieCallback.create(callback));
+ } catch (IllegalStateException e) {
+ throw new IllegalStateException(
+ "removeSessionCookies must be called on a thread with a running Looper.");
+ }
}
/**
* Remove all cookies
+ * @param callback A callback called after the cookies are removed with the number removed.
*/
- public void removeAllCookie() {
- nativeRemoveAllCookie();
+ public void removeAllCookies(ValueCallback<Integer> callback) {
+ try {
+ nativeRemoveAllCookie(CookieCallback.create(callback));
+ } catch (IllegalStateException e) {
+ throw new IllegalStateException(
+ "removeAllCookies must be called on a thread with a running Looper.");
+ }
}
/**
@@ -120,17 +165,68 @@ public final class AwCookieManager {
nativeSetAcceptFileSchemeCookies(accept);
}
+ @CalledByNative
+ public static void invokeRemoveCookieCallback(CookieCallback<Integer> callback,
mkosiba (inactive) 2014/05/13 09:37:17 I'd call these by type (invokeIntegerCookieCallbac
hjd_google 2014/05/15 10:52:07 Done.
+ int numDeleted) {
+ callback.onReceiveValue(numDeleted);
+ }
+
+ @CalledByNative
+ public static void invokeSetCookieCallback(CookieCallback<Boolean> callback, boolean success) {
+ callback.onReceiveValue(success);
+ }
+
+ /**
+ * CookieCallback is a version of ValueCallback that native can call.
mkosiba (inactive) 2014/05/13 09:37:17 native can call ValueCallback too using the same m
hjd_google 2014/05/15 10:52:07 Done.
+ * We need to arrange for the users ValueCallback#onReceiveValue to be called on the
+ * thread that that called our API after the work is done. XXX
mkosiba (inactive) 2014/05/13 09:37:17 XXX?
hjd_google 2014/05/15 10:52:07 Done.
+ */
+ private static class CookieCallback<T> {
+ ValueCallback<T> mCallback;
+ Handler mHandler;
+
+ public CookieCallback(ValueCallback<T> callback, Handler handler) {
+ mCallback = callback;
+ mHandler = handler;
+ }
+
+ public static<T> CookieCallback<T> create(ValueCallback<T> callback) throws
mkosiba (inactive) 2014/05/13 09:37:17 hmmm.. maybe call this map or convert. I'd expect
hjd_google 2014/05/15 10:52:07 I went with convert.
+ IllegalStateException {
+ if (callback == null) {
+ return null;
+ }
+ if (Looper.myLooper() == null) {
+ throw new IllegalStateException(
+ "CookieCallback.create should be called on a thread with a running Looper.");
+ }
+ return new CookieCallback<T>(callback, new Handler());
+ }
+
+ public void onReceiveValue(final T t) {
+ mHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ mCallback.onReceiveValue(t);
+ }
+ });
+ }
+ }
+
private native void nativeSetAcceptCookie(boolean accept);
private native boolean nativeAcceptCookie();
private native void nativeSetAcceptThirdPartyCookie(boolean accept);
private native boolean nativeAcceptThirdPartyCookie();
- private native void nativeSetCookie(String url, String value);
+ private native void nativeSetCookie(String url, String value,
+ CookieCallback<Boolean> callback);
+ private native void nativeSetCookieSync(String url, String value);
private native String nativeGetCookie(String url);
- private native void nativeRemoveSessionCookie();
- private native void nativeRemoveAllCookie();
+ private native void nativeRemoveSessionCookie(CookieCallback<Integer> callback);
+ private native void nativeRemoveSessionCookieSync();
+ private native void nativeRemoveAllCookie(CookieCallback<Integer> callback);
+ private native void nativeRemoveAllCookieSync();
private native void nativeRemoveExpiredCookie();
private native void nativeFlushCookieStore();

Powered by Google App Engine
This is Rietveld 408576698