summaryrefslogtreecommitdiff
path: root/apex/blobstore/framework/java
diff options
context:
space:
mode:
authorSudheer Shanka <sudheersai@google.com>2020-03-20 00:12:14 -0700
committerSudheer Shanka <sudheersai@google.com>2020-03-20 12:06:53 -0700
commita80a997a3badadf629f06fd63ee0d4bae4e581a4 (patch)
tree4677843c4dfa01f83ab4c5323a5aa9dce96f09ba /apex/blobstore/framework/java
parentdb74198593842290281605191685084ddf58841f (diff)
Add BlobStoreManager.getRemainingLeaseQuotaBytes.
+ Add LimitExceededException which can be used to indicate that an app is doing something that will cause it to violate any limits enforced by the System. Bug: 150239703 Test: atest --test-mapping apex/blobstore Change-Id: I7584d1b8618be546080b0eddb2fe35de2611f9e7
Diffstat (limited to 'apex/blobstore/framework/java')
-rw-r--r--apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java55
-rw-r--r--apex/blobstore/framework/java/android/app/blob/IBlobStoreManager.aidl1
2 files changed, 48 insertions, 8 deletions
diff --git a/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java b/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java
index c339351759cd..cb87c6cefef5 100644
--- a/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java
+++ b/apex/blobstore/framework/java/android/app/blob/BlobStoreManager.java
@@ -25,6 +25,7 @@ import android.annotation.Nullable;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;
+import android.os.LimitExceededException;
import android.os.ParcelFileDescriptor;
import android.os.ParcelableException;
import android.os.RemoteCallback;
@@ -167,6 +168,12 @@ public class BlobStoreManager {
* finalized (either committed or abandoned) within a reasonable period of
* time, typically about a week.
*
+ * <p> If an app is planning to acquire a lease on this data (using
+ * {@link #acquireLease(BlobHandle, int)} or one of it's other variants) after committing
+ * this data (using {@link Session#commit(Executor, Consumer)}), it is recommended that
+ * the app checks the remaining quota for acquiring a lease first using
+ * {@link #getRemainingLeaseQuotaBytes()} and can skip contributing this data if needed.
+ *
* @param blobHandle the {@link BlobHandle} identifier for which a new session
* needs to be created.
* @return positive, non-zero unique id that represents the created session.
@@ -294,8 +301,11 @@ public class BlobStoreManager {
* @throws IllegalArgumentException when {@code blobHandle} is invalid or
* if the {@code leaseExpiryTimeMillis} is greater than the
* {@link BlobHandle#getExpiryTimeMillis()}.
- * @throws IllegalStateException when a lease could not be acquired, such as when the
- * caller is trying to acquire too many leases.
+ * @throws LimitExceededException when a lease could not be acquired, such as when the
+ * caller is trying to acquire leases on too much data. Apps
+ * can avoid this by checking the remaining quota using
+ * {@link #getRemainingLeaseQuotaBytes()} before trying to
+ * acquire a lease.
*
* @see {@link #acquireLease(BlobHandle, int)}
* @see {@link #acquireLease(BlobHandle, CharSequence)}
@@ -307,6 +317,7 @@ public class BlobStoreManager {
mContext.getOpPackageName());
} catch (ParcelableException e) {
e.maybeRethrow(IOException.class);
+ e.maybeRethrow(LimitExceededException.class);
throw new RuntimeException(e);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
@@ -350,8 +361,11 @@ public class BlobStoreManager {
* @throws IllegalArgumentException when {@code blobHandle} is invalid or
* if the {@code leaseExpiryTimeMillis} is greater than the
* {@link BlobHandle#getExpiryTimeMillis()}.
- * @throws IllegalStateException when a lease could not be acquired, such as when the
- * caller is trying to acquire too many leases.
+ * @throws LimitExceededException when a lease could not be acquired, such as when the
+ * caller is trying to acquire leases on too much data. Apps
+ * can avoid this by checking the remaining quota using
+ * {@link #getRemainingLeaseQuotaBytes()} before trying to
+ * acquire a lease.
*
* @see {@link #acquireLease(BlobHandle, int, long)}
* @see {@link #acquireLease(BlobHandle, CharSequence)}
@@ -363,6 +377,7 @@ public class BlobStoreManager {
mContext.getOpPackageName());
} catch (ParcelableException e) {
e.maybeRethrow(IOException.class);
+ e.maybeRethrow(LimitExceededException.class);
throw new RuntimeException(e);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
@@ -399,8 +414,11 @@ public class BlobStoreManager {
* @throws SecurityException when the blob represented by the {@code blobHandle} does not
* exist or the caller does not have access to it.
* @throws IllegalArgumentException when {@code blobHandle} is invalid.
- * @throws IllegalStateException when a lease could not be acquired, such as when the
- * caller is trying to acquire too many leases.
+ * @throws LimitExceededException when a lease could not be acquired, such as when the
+ * caller is trying to acquire leases on too much data. Apps
+ * can avoid this by checking the remaining quota using
+ * {@link #getRemainingLeaseQuotaBytes()} before trying to
+ * acquire a lease.
*
* @see {@link #acquireLease(BlobHandle, int, long)}
* @see {@link #acquireLease(BlobHandle, CharSequence, long)}
@@ -443,8 +461,11 @@ public class BlobStoreManager {
* @throws SecurityException when the blob represented by the {@code blobHandle} does not
* exist or the caller does not have access to it.
* @throws IllegalArgumentException when {@code blobHandle} is invalid.
- * @throws IllegalStateException when a lease could not be acquired, such as when the
- * caller is trying to acquire too many leases.
+ * @throws LimitExceededException when a lease could not be acquired, such as when the
+ * caller is trying to acquire leases on too much data. Apps
+ * can avoid this by checking the remaining quota using
+ * {@link #getRemainingLeaseQuotaBytes()} before trying to
+ * acquire a lease.
*
* @see {@link #acquireLease(BlobHandle, int)}
* @see {@link #acquireLease(BlobHandle, CharSequence, long)}
@@ -478,6 +499,24 @@ public class BlobStoreManager {
}
/**
+ * Return the remaining quota size for acquiring a lease (in bytes) which indicates the
+ * remaining amount of data that an app can acquire a lease on before the System starts
+ * rejecting lease requests.
+ *
+ * If an app wants to acquire a lease on a blob but the remaining quota size is not sufficient,
+ * then it can try releasing leases on any older blobs which are not needed anymore.
+ *
+ * @return the remaining quota size for acquiring a lease.
+ */
+ public @IntRange(from = 0) long getRemainingLeaseQuotaBytes() {
+ try {
+ return mService.getRemainingLeaseQuotaBytes(mContext.getOpPackageName());
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+ }
+
+ /**
* Wait until any pending tasks (like persisting data to disk) have finished.
*
* @hide
diff --git a/apex/blobstore/framework/java/android/app/blob/IBlobStoreManager.aidl b/apex/blobstore/framework/java/android/app/blob/IBlobStoreManager.aidl
index 20c15ab57496..39a9fb4bb1f4 100644
--- a/apex/blobstore/framework/java/android/app/blob/IBlobStoreManager.aidl
+++ b/apex/blobstore/framework/java/android/app/blob/IBlobStoreManager.aidl
@@ -31,6 +31,7 @@ interface IBlobStoreManager {
void acquireLease(in BlobHandle handle, int descriptionResId, in CharSequence description,
long leaseTimeoutMillis, in String packageName);
void releaseLease(in BlobHandle handle, in String packageName);
+ long getRemainingLeaseQuotaBytes(String packageName);
void waitForIdle(in RemoteCallback callback);