Skip to content

Commit

Permalink
Fix disk LRU cache key formatting error
Browse files Browse the repository at this point in the history
We hash the key using sha-256

Fix the issue related to:
Failed to get Bitmap from DiskLruCache:keys must match regex [a-z0-9_-]{1,64}:
"https://igalia.github.io/wolvic-3D-environments/spring/spring.png"

Now we can access the external environments bitmap offline after
they have been loaded once.

Signed-off-by: Songlin Jiang <[email protected]>
  • Loading branch information
HollowMan6 committed Dec 5, 2023
1 parent c86d8cf commit e51370e
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions app/src/common/shared/com/igalia/wolvic/utils/BitmapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

Expand All @@ -32,6 +36,28 @@ public class BitmapCache {
private Surface mCaptureSurface;
private boolean mCapturedAcquired;

private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}

private String hashKey(@NonNull String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return bytesToHex(digest.digest(input.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException e) {
Log.e(LOGTAG, Objects.requireNonNull(e.getMessage()));
return input;
}
}

public static BitmapCache getInstance(Context aContext) {
return ((VRBrowserApplication)aContext.getApplicationContext()).getBitmapCache();
}
Expand Down Expand Up @@ -75,11 +101,12 @@ void initDiskCache() {
}

public void addBitmap(@NonNull String aKey, @NonNull Bitmap aBitmap) {
mMemoryCache.put(aKey, aBitmap);
String finalKey = hashKey(aKey);
mMemoryCache.put(finalKey, aBitmap);
runIO(() -> {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit(aKey);
editor = mDiskCache.edit(finalKey);
if (editor != null) {
aBitmap.compress(Bitmap.CompressFormat.PNG, 80, editor.newOutputStream(0));
editor.commit();
Expand All @@ -100,21 +127,22 @@ public void addBitmap(@NonNull String aKey, @NonNull Bitmap aBitmap) {
}

public @NonNull CompletableFuture<Bitmap> getBitmap(@NonNull String aKey) {
Bitmap cached = mMemoryCache.get(aKey);
String finalKey = hashKey(aKey);
Bitmap cached = mMemoryCache.get(finalKey);
if (cached != null) {
return CompletableFuture.completedFuture(cached);
} else {
CompletableFuture<Bitmap> result = new CompletableFuture<>();
runIO(() -> {
try (DiskLruCache.Snapshot snapshot = mDiskCache.get(aKey)){
try (DiskLruCache.Snapshot snapshot = mDiskCache.get(finalKey)){
if (snapshot != null) {
Bitmap bitmap = BitmapFactory.decodeStream(snapshot.getInputStream(0));
if (bitmap != null) {
mMainThreadExecutor.execute(() -> {
if (mMemoryCache.get(aKey) == null) {
if (mMemoryCache.get(finalKey) == null) {
// Do not update cache if it already contains a value
// A tab could have saved a new image while we were loading the cached disk image.
mMemoryCache.put(aKey, bitmap);
mMemoryCache.put(finalKey, bitmap);
}
result.complete(bitmap);
});
Expand All @@ -135,18 +163,19 @@ public void addBitmap(@NonNull String aKey, @NonNull Bitmap aBitmap) {
}

public void removeBitmap(@NonNull String aKey) {
mMemoryCache.remove(aKey);
String finalKey = hashKey(aKey);
mMemoryCache.remove(finalKey);
runIO(() -> {
try {
mDiskCache.remove(aKey);
mDiskCache.remove(finalKey);
} catch (Exception ex) {
Log.e(LOGTAG, "Failed to remove Bitmap from DiskLruCache:" + ex.getMessage());
}
});
}

public boolean hasBitmap(@NonNull String aKey) {
return mMemoryCache.get(aKey) != null;
return mMemoryCache.get(hashKey(aKey)) != null;
}

private void runIO(Runnable aRunnable) {
Expand Down

0 comments on commit e51370e

Please sign in to comment.