Bug 1246159 - Pre: implement SavedReaderViewHelper.getBytesUsed r=mcomella
authorAndrzej Hunt <ahunt@mozilla.com>
Tue, 24 May 2016 15:01:17 -0700
changeset 337686 6afaaa5cf43b2729a342bb7a7f51c5bbb7a4f41b
parent 337685 0d8baf8b4e768f4d403fab4c260997e173cde275
child 337687 199fac6509aa6aaa249a178d6bd39e87f3061a67
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs1246159
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1246159 - Pre: implement SavedReaderViewHelper.getBytesUsed r=mcomella MozReview-Commit-ID: EJ81q6f515q
mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
--- a/mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
+++ b/mobile/android/base/java/org/mozilla/gecko/reader/SavedReaderViewHelper.java
@@ -13,16 +13,17 @@ import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.GeckoProfile;
 import org.mozilla.gecko.annotation.RobocopTarget;
 import org.mozilla.gecko.db.UrlAnnotations;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Iterator;
 
 /**
  * Helper to keep track of items that are stored in the reader view cache. This is an in-memory list
  * of the reader view items that are cached on disk. It is intended to allow quickly determining whether
  * a given URL is in the cache, and also how many cached items there are.
  *
  * Currently we have 1:1 correspondence of reader view items (in the URL-annotations table)
  * to cached items. This is _not_ a true cache, we never purge/cleanup items here - we only remove
@@ -200,9 +201,46 @@ public class SavedReaderViewHelper {
         SavedReaderViewHelper rvh = getSavedReaderViewHelper(context);
 
         if (rvh.isURLCached(pageURL)) {
             return ReaderModeUtils.getAboutReaderForUrl(pageURL);
         } else {
             return pageURL;
         }
     }
+
+    /**
+     * Obtain the total disk space used for saved reader view items, in KB.
+     *
+     * @return Total disk space used (KB), or Integer.MAX_VALUE on overflow.
+     */
+    public synchronized int getDiskSpacedUsedKB() {
+        // JSONObject is not thread safe - we need to be synchronized to avoid issues (most likely to
+        // occur if items are removed during iteration).
+        final Iterator<String> keys = mItems.keys();
+        long bytes = 0;
+
+        while (keys.hasNext()) {
+            final String pageURL = keys.next();
+            try {
+                final JSONObject item = mItems.getJSONObject(pageURL);
+                bytes += item.getLong(SIZE);
+
+                // Overflow is highly unlikely (we will hit device storage limits before we hit integer limits),
+                // but we should still handle this for correctness.
+                // We definitely can't store our output in an int if we overflow the long here.
+                if (bytes < 0) {
+                    return Integer.MAX_VALUE;
+                }
+            } catch (JSONException e) {
+                // This shouldn't ever happen:
+                throw new IllegalStateException("Must be able to access items in saved reader view list", e);
+            }
+        }
+
+        long kb = bytes / 1024;
+        if (kb > Integer.MAX_VALUE) {
+            return Integer.MAX_VALUE;
+        } else {
+            return (int) kb;
+        }
+    }
 }