Index: android_webview/java/src/org/chromium/android_webview/AwQuotaManagerBridge.java |
diff --git a/android_webview/java/src/org/chromium/android_webview/AwQuotaManagerBridge.java b/android_webview/java/src/org/chromium/android_webview/AwQuotaManagerBridge.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..256795864b5d84de1a0d78c55ea85d056503a9f8 |
--- /dev/null |
+++ b/android_webview/java/src/org/chromium/android_webview/AwQuotaManagerBridge.java |
@@ -0,0 +1,126 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.android_webview; |
+ |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.JNINamespace; |
+ |
+import android.os.Looper; |
+import android.webkit.ValueCallback; |
+ |
+import java.util.Map; |
+import java.util.HashMap; |
+ |
+/** |
+ * Bridge between android.webview.WebStorage and native QuotaManager. This object is owned by Java |
+ * AwBrowserContext and the native side is owned by the native AwBrowserContext. |
+ * |
+ * TODO(boliu): Actually make this true after Java AwBrowserContext is added. |
+ */ |
+@JNINamespace("android_webview") |
+public class AwQuotaManagerBridge { |
+ // TODO(boliu): This should be obtained from Java AwBrowserContext that owns this. |
+ private static native int nativeGetDefaultNativeAwQuotaManagerBridge(); |
+ |
+ // TODO(boliu): This should be owned by Java AwBrowserContext, not a singleton. |
+ private static AwQuotaManagerBridge sInstance; |
+ public static AwQuotaManagerBridge getInstance() { |
+ assert Looper.myLooper() == Looper.getMainLooper(); |
+ if (sInstance == null) { |
+ sInstance = new AwQuotaManagerBridge(nativeGetDefaultNativeAwQuotaManagerBridge()); |
+ } |
+ return sInstance; |
+ } |
+ |
+ public static class Origins { |
+ public final byte[][] mOrigins; |
+ public final long[] mUsage; |
+ public final long[] mQuota; |
+ |
+ Origins(byte[][] origins, long[] usage, long[] quota) { |
+ mOrigins = origins; |
+ mUsage = usage; |
+ mQuota = quota; |
+ |
+ } |
+ } |
+ |
+ // This is not owning. The native object is owned by the native AwBrowserContext. |
+ // TODO(boliu): Native should clear this when destroyed. Check if it ever happens. |
+ private int mNativeAwQuotaManagerBridgeImpl; |
+ |
+ private int mNextId; |
+ private Map<Integer, ValueCallback<Origins>> mPendingGetOriginCallbacks; |
+ private Map<Integer, ValueCallback<Long>> mPendingGetQuotaForOriginCallbacks; |
+ private Map<Integer, ValueCallback<Long>> mPendingGetUsageForOriginCallbacks; |
+ |
+ private AwQuotaManagerBridge(int nativeAwQuotaManagerBridgeImpl) { |
+ mNativeAwQuotaManagerBridgeImpl = nativeAwQuotaManagerBridgeImpl; |
+ mPendingGetOriginCallbacks = |
+ new HashMap<Integer, ValueCallback<Origins>>(); |
+ mPendingGetQuotaForOriginCallbacks = new HashMap<Integer, ValueCallback<Long>>(); |
+ mPendingGetUsageForOriginCallbacks = new HashMap<Integer, ValueCallback<Long>>(); |
+ nativeInit(mNativeAwQuotaManagerBridgeImpl); |
+ } |
+ |
+ private int getNextId() { |
+ return ++mNextId; |
+ } |
+ |
+ public void deleteAllData() { |
+ nativeDeleteAllData(mNativeAwQuotaManagerBridgeImpl); |
+ } |
+ |
+ public void deleteOrigin(String origin) { |
+ nativeDeleteOrigin(mNativeAwQuotaManagerBridgeImpl, origin); |
+ } |
+ |
+ public void getOrigins(ValueCallback<Origins> callback) { |
+ int callbackId = getNextId(); |
+ assert !mPendingGetOriginCallbacks.containsKey(callbackId); |
+ mPendingGetOriginCallbacks.put(callbackId, callback); |
+ nativeGetOrigins(mNativeAwQuotaManagerBridgeImpl, callbackId); |
+ } |
+ |
+ public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) { |
+ int callbackId = getNextId(); |
+ assert !mPendingGetQuotaForOriginCallbacks.containsKey(callbackId); |
+ mPendingGetQuotaForOriginCallbacks.put(callbackId, callback); |
+ nativeGetUsageAndQuotaForOrigin(mNativeAwQuotaManagerBridgeImpl, origin, callbackId, true); |
+ } |
+ |
+ public void getUsageForOrigin(String origin, ValueCallback<Long> callback) { |
+ int callbackId = getNextId(); |
+ assert !mPendingGetUsageForOriginCallbacks.containsKey(callbackId); |
+ mPendingGetUsageForOriginCallbacks.put(callbackId, callback); |
+ nativeGetUsageAndQuotaForOrigin(mNativeAwQuotaManagerBridgeImpl, origin, callbackId, false); |
+ } |
+ |
+ @CalledByNative |
+ private void onGetOriginsCallback(int callbackId, byte[][] origin, long[] usage, long[] quota) { |
+ assert mPendingGetOriginCallbacks.containsKey(callbackId); |
+ mPendingGetOriginCallbacks.remove(callbackId).onReceiveValue( |
+ new Origins(origin, usage, quota)); |
+ } |
+ |
+ @CalledByNative |
+ private void onGetUsageAndQuotaForOriginCallback( |
+ int callbackId, boolean isQuota, long usage, long quota) { |
boliu
2013/02/20 00:53:51
I've tried to follow usage first, quota second thr
|
+ if (isQuota) { |
+ assert mPendingGetQuotaForOriginCallbacks.containsKey(callbackId); |
+ mPendingGetQuotaForOriginCallbacks.remove(callbackId).onReceiveValue(quota); |
+ } else { |
+ assert mPendingGetUsageForOriginCallbacks.containsKey(callbackId); |
+ mPendingGetUsageForOriginCallbacks.remove(callbackId).onReceiveValue(usage); |
+ } |
+ } |
+ |
+ private native void nativeInit(int nativeAwQuotaManagerBridgeImpl); |
+ private native void nativeDeleteAllData(int nativeAwQuotaManagerBridgeImpl); |
+ private native void nativeDeleteOrigin(int nativeAwQuotaManagerBridgeImpl, String origin); |
+ private native void nativeGetOrigins(int nativeAwQuotaManagerBridgeImpl, int callbackId); |
+ private native void nativeGetUsageAndQuotaForOrigin(int nativeAwQuotaManagerBridgeImpl, |
+ String origin, int callbackId, boolean isQuota); |
+} |