1 // Copyright 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.
5 package org.chromium.android_webview;
7 import android.util.SparseArray;
8 import android.webkit.ValueCallback;
10 import org.chromium.base.CalledByNative;
11 import org.chromium.base.JNINamespace;
12 import org.chromium.base.ThreadUtils;
15 * Bridge between android.webview.WebStorage and native QuotaManager. This object is owned by Java
16 * AwBrowserContext and the native side is owned by the native AwBrowserContext.
18 * TODO(boliu): Actually make this true after Java AwBrowserContext is added.
20 @JNINamespace("android_webview")
21 public class AwQuotaManagerBridge {
22 // TODO(boliu): This should be obtained from Java AwBrowserContext that owns this.
23 private static native long nativeGetDefaultNativeAwQuotaManagerBridge();
25 // TODO(boliu): This should be owned by Java AwBrowserContext, not a singleton.
26 private static AwQuotaManagerBridge sInstance;
27 public static AwQuotaManagerBridge getInstance() {
28 ThreadUtils.assertOnUiThread();
29 if (sInstance == null) {
30 sInstance = new AwQuotaManagerBridge(nativeGetDefaultNativeAwQuotaManagerBridge());
36 * This class represent the callback value of android.webview.WebStorage.getOrigins. The values
37 * are optimized for JNI convenience and need to be converted.
39 public static class Origins {
40 // Origin, usage, and quota data in parallel arrays of same length.
41 public final String[] mOrigins;
42 public final long[] mUsages;
43 public final long[] mQuotas;
45 Origins(String[] origins, long[] usages, long[] quotas) {
52 // This is not owning. The native object is owned by the native AwBrowserContext.
53 private long mNativeAwQuotaManagerBridgeImpl;
55 // The Java callbacks are saved here. An incrementing callback id is generated for each saved
56 // callback and is passed to the native side to identify callback.
58 private SparseArray<ValueCallback<Origins>> mPendingGetOriginCallbacks;
59 private SparseArray<ValueCallback<Long>> mPendingGetQuotaForOriginCallbacks;
60 private SparseArray<ValueCallback<Long>> mPendingGetUsageForOriginCallbacks;
62 private AwQuotaManagerBridge(long nativeAwQuotaManagerBridgeImpl) {
63 mNativeAwQuotaManagerBridgeImpl = nativeAwQuotaManagerBridgeImpl;
64 mPendingGetOriginCallbacks =
65 new SparseArray<ValueCallback<Origins>>();
66 mPendingGetQuotaForOriginCallbacks = new SparseArray<ValueCallback<Long>>();
67 mPendingGetUsageForOriginCallbacks = new SparseArray<ValueCallback<Long>>();
68 nativeInit(mNativeAwQuotaManagerBridgeImpl);
71 private int getNextId() {
72 ThreadUtils.assertOnUiThread();
77 * There are five HTML5 offline storage APIs.
78 * 1) Web Storage (ie the localStorage and sessionStorage variables)
80 * 3) Application cache
86 * Implements WebStorage.deleteAllData(). Clear the storage of all five offline APIs.
88 * TODO(boliu): Actually clear Web Storage.
90 public void deleteAllData() {
91 nativeDeleteAllData(mNativeAwQuotaManagerBridgeImpl);
95 * Implements WebStorage.deleteOrigin(). Clear the storage of APIs 2-5 for the given origin.
97 public void deleteOrigin(String origin) {
98 nativeDeleteOrigin(mNativeAwQuotaManagerBridgeImpl, origin);
102 * Implements WebStorage.getOrigins. Get the per origin usage and quota of APIs 2-5 in
105 public void getOrigins(ValueCallback<Origins> callback) {
106 int callbackId = getNextId();
107 assert mPendingGetOriginCallbacks.get(callbackId) == null;
108 mPendingGetOriginCallbacks.put(callbackId, callback);
109 nativeGetOrigins(mNativeAwQuotaManagerBridgeImpl, callbackId);
113 * Implements WebStorage.getQuotaForOrigin. Get the quota of APIs 2-5 in aggregate for given
116 public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) {
117 int callbackId = getNextId();
118 assert mPendingGetQuotaForOriginCallbacks.get(callbackId) == null;
119 mPendingGetQuotaForOriginCallbacks.put(callbackId, callback);
120 nativeGetUsageAndQuotaForOrigin(mNativeAwQuotaManagerBridgeImpl, origin, callbackId, true);
124 * Implements WebStorage.getUsageForOrigin. Get the usage of APIs 2-5 in aggregate for given
127 public void getUsageForOrigin(String origin, ValueCallback<Long> callback) {
128 int callbackId = getNextId();
129 assert mPendingGetUsageForOriginCallbacks.get(callbackId) == null;
130 mPendingGetUsageForOriginCallbacks.put(callbackId, callback);
131 nativeGetUsageAndQuotaForOrigin(mNativeAwQuotaManagerBridgeImpl, origin, callbackId, false);
135 private void onGetOriginsCallback(int callbackId, String[] origin, long[] usages,
137 assert mPendingGetOriginCallbacks.get(callbackId) != null;
138 mPendingGetOriginCallbacks.get(callbackId).onReceiveValue(
139 new Origins(origin, usages, quotas));
140 mPendingGetOriginCallbacks.remove(callbackId);
144 private void onGetUsageAndQuotaForOriginCallback(
145 int callbackId, boolean isQuota, long usage, long quota) {
147 assert mPendingGetQuotaForOriginCallbacks.get(callbackId) != null;
148 mPendingGetQuotaForOriginCallbacks.get(callbackId).onReceiveValue(quota);
149 mPendingGetQuotaForOriginCallbacks.remove(callbackId);
151 assert mPendingGetUsageForOriginCallbacks.get(callbackId) != null;
152 mPendingGetUsageForOriginCallbacks.get(callbackId).onReceiveValue(usage);
153 mPendingGetUsageForOriginCallbacks.remove(callbackId);
157 private native void nativeInit(long nativeAwQuotaManagerBridgeImpl);
158 private native void nativeDeleteAllData(long nativeAwQuotaManagerBridgeImpl);
159 private native void nativeDeleteOrigin(long nativeAwQuotaManagerBridgeImpl, String origin);
160 private native void nativeGetOrigins(long nativeAwQuotaManagerBridgeImpl, int callbackId);
161 private native void nativeGetUsageAndQuotaForOrigin(long nativeAwQuotaManagerBridgeImpl,
162 String origin, int callbackId, boolean isQuota);