Bug 789923 - Drop saved thumbnails when device storage is low. r=mfinkle, lucasr
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 16 Oct 2012 10:07:06 -0400
changeset 110541 386b12107730c76308b3f9f444df69538c41d0b8
parent 110540 226cb9228a9e005da8769f28e655b3e05df44aac
child 110542 47ee1a38fe31ebfffd5aea68095b15ccc2446e83
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmfinkle, lucasr
bugs789923
milestone19.0a1
Bug 789923 - Drop saved thumbnails when device storage is low. r=mfinkle, lucasr
mobile/android/base/MemoryMonitor.java
mobile/android/base/db/BrowserDB.java
mobile/android/base/db/BrowserProvider.java.in
mobile/android/base/db/LocalBrowserDB.java
--- a/mobile/android/base/MemoryMonitor.java
+++ b/mobile/android/base/MemoryMonitor.java
@@ -1,15 +1,17 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import org.mozilla.gecko.db.BrowserDB;
+
 import android.content.BroadcastReceiver;
 import android.content.ComponentCallbacks2;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.Build;
 import android.os.Debug;
 import android.util.Log;
@@ -101,18 +103,17 @@ class MemoryMonitor extends BroadcastRec
         }
     }
 
     @Override
     public void onReceive(Context context, Intent intent) {
         if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(intent.getAction())) {
             Log.d(LOGTAG, "Device storage is low");
             mStoragePressure = true;
-            // TODO: drop or shrink disk caches
-            // TODO: drop stuff from browser.db
+            GeckoAppShell.getHandler().post(new StorageReducer());
         } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(intent.getAction())) {
             Log.d(LOGTAG, "Device storage is ok");
             mStoragePressure = false;
         } else if (ACTION_MEMORY_DUMP.equals(intent.getAction())) {
             String label = intent.getStringExtra("label");
             if (label == null) {
                 label = "default";
             }
@@ -196,9 +197,28 @@ class MemoryMonitor extends BroadcastRec
                 mPosted = false;
                 return;
             }
 
             // need to keep decrementing
             GeckoAppShell.getHandler().postDelayed(this, DECREMENT_DELAY);
         }
     }
+
+    class StorageReducer implements Runnable {
+        @Override
+        public void run() {
+            // this might get run right on startup, if so wait 10 seconds and try again
+            if (!GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning)) {
+                GeckoAppShell.getHandler().postDelayed(this, 10000);
+                return;
+            }
+
+            if (!mStoragePressure) {
+                // pressure is off, so we can abort
+                return;
+            }
+
+            BrowserDB.removeThumbnails(Tabs.getInstance().getContentResolver());
+            // TODO: drop or shrink disk caches
+        }
+    }
 }
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -70,16 +70,18 @@ public class BrowserDB {
         public BitmapDrawable getFaviconForUrl(ContentResolver cr, String uri);
 
         public void updateFaviconForUrl(ContentResolver cr, String uri, BitmapDrawable favicon);
 
         public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail);
 
         public byte[] getThumbnailForUrl(ContentResolver cr, String uri);
 
+        public void removeThumbnails(ContentResolver cr);
+
         public void registerBookmarkObserver(ContentResolver cr, ContentObserver observer);
 
         public void registerHistoryObserver(ContentResolver cr, ContentObserver observer);
     }
 
     static {
         // Forcing local DB no option to switch to Android DB for now
         sDb = null;
@@ -181,16 +183,20 @@ public class BrowserDB {
     public static void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail) {
         sDb.updateThumbnailForUrl(cr, uri, thumbnail);
     }
 
     public static byte[] getThumbnailForUrl(ContentResolver cr, String uri) {
         return sDb.getThumbnailForUrl(cr, uri);
     }
 
+    public static void removeThumbnails(ContentResolver cr) {
+        sDb.removeThumbnails(cr);
+    }
+
     public static void registerBookmarkObserver(ContentResolver cr, ContentObserver observer) {
         sDb.registerBookmarkObserver(cr, observer);
     }
 
     public static void registerHistoryObserver(ContentResolver cr, ContentObserver observer) {
         sDb.registerHistoryObserver(cr, observer);
     }
 
--- a/mobile/android/base/db/BrowserProvider.java.in
+++ b/mobile/android/base/db/BrowserProvider.java.in
@@ -1657,20 +1657,20 @@ public class BrowserProvider extends Con
                 break;
             }
 
             case IMAGES: {
                 debug("Update on IMAGES: " + uri);
 
                 String url = values.getAsString(Images.URL);
 
+                // if no URL is provided, update all of the entries
                 if (TextUtils.isEmpty(url))
-                    throw new IllegalArgumentException("Images.URL is required");
-
-                if (shouldUpdateOrInsert(uri))
+                    updated = updateExistingImage(uri, values, null, null);
+                else if (shouldUpdateOrInsert(uri))
                     updated = updateOrInsertImage(uri, values, Images.URL + " = ?",
                                                   new String[] { url });
                 else
                     updated = updateExistingImage(uri, values, Images.URL + " = ?",
                                                   new String[] { url });
 
                 break;
             }
--- a/mobile/android/base/db/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -675,16 +675,22 @@ public class LocalBrowserDB implements B
         int thumbnailIndex = c.getColumnIndexOrThrow(Images.THUMBNAIL);
 
         byte[] b = c.getBlob(thumbnailIndex);
         c.close();
 
         return b;
     }
 
+    public void removeThumbnails(ContentResolver cr) {
+        ContentValues values = new ContentValues();
+        values.putNull(Images.THUMBNAIL);
+        cr.update(mImagesUriWithProfile, values, null, null);
+    }
+
     // Utility function for updating existing history using batch operations
     public void updateHistoryInBatch(ContentResolver cr,
                                      Collection<ContentProviderOperation> operations,
                                      String url, String title,
                                      long date, int visits) {
         Cursor cursor = null;
 
         try {