OSDN Git Service

DO NOT MERGE
authorGrace Kloba <klobag@google.com>
Mon, 1 Mar 2010 20:13:01 +0000 (12:13 -0800)
committerGrace Kloba <klobag@google.com>
Mon, 1 Mar 2010 20:13:01 +0000 (12:13 -0800)
Manual merge part of https://android-git.corp.google.com/g/#change,42462
from master to mr2 for Adobe.

Fix http://b/issue?id=2475242

core/java/android/webkit/CacheManager.java
core/java/android/webkit/WebViewDatabase.java

index 22dca3a..cc810e4 100644 (file)
@@ -24,10 +24,12 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
+import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 import org.bouncycastle.crypto.Digest;
@@ -541,6 +543,26 @@ public final class CacheManager {
                     Log.e(LOGTAG, f.getPath() + " delete failed.");
                 }
             }
+            // remove the unreferenced files in the cache directory
+            final List<String> fileList = mDataBase.getAllCacheFileNames();
+            if (fileList == null) return;
+            String[] toDelete = mBaseDir.list(new FilenameFilter() {
+                public boolean accept(File dir, String filename) {
+                    if (fileList.contains(filename)) {
+                        return false;
+                    } else {
+                        return true;
+                    }
+                }
+            });
+            if (toDelete == null) return;
+            size = toDelete.length;
+            for (int i = 0; i < size; i++) {
+                File f = new File(mBaseDir, toDelete[i]);
+                if (!f.delete()) {
+                    Log.e(LOGTAG, f.getPath() + " delete failed.");
+                }
+            }
         }
     }
 
index 110e4f8..4e48b3d 100644 (file)
@@ -19,6 +19,7 @@ package android.webkit;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 import java.util.Map.Entry;
 
@@ -727,6 +728,20 @@ public class WebViewDatabase {
         return pathList;
     }
 
+    List<String> getAllCacheFileNames() {
+        ArrayList<String> pathList = null;
+        Cursor cursor = mCacheDatabase.rawQuery("SELECT filepath FROM cache",
+                null);
+        if (cursor != null && cursor.moveToFirst()) {
+            pathList = new ArrayList<String>(cursor.getCount());
+            do {
+                pathList.add(cursor.getString(0));
+            } while (cursor.moveToNext());
+        }
+        cursor.close();
+        return pathList;
+    }
+
     //
     // password functions
     //