Stop leaking temp directories in SkyShell

At startup, we make a list of all the temp directories that already exist and
then nuke them on a background thread after a five second delay. The five
second delay isn't particularly satisfying, but I think it's wortwhile to get
this work off the critical path of startup.

R=eseidel@chromium.org, eseidel@google.com

Review URL: https://codereview.chromium.org/1228083003 .
This commit is contained in:
Adam Barth
2015-07-09 09:57:41 -07:00
parent 90e211f9b1
commit b8a62ea0c5
3 changed files with 90 additions and 1 deletions

View File

@@ -100,6 +100,7 @@ if (is_android) {
"android/org/domokit/sky/shell/GestureProvider.java",
"android/org/domokit/sky/shell/PlatformServiceProvider.java",
"android/org/domokit/sky/shell/PlatformViewAndroid.java",
"android/org/domokit/sky/shell/ResourceCleaner.java",
"android/org/domokit/sky/shell/ResourceExtractor.java",
"android/org/domokit/sky/shell/ServiceFactory.java",
"android/org/domokit/sky/shell/ServiceRegistry.java",

View File

@@ -0,0 +1,86 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.domokit.sky.shell;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import java.io.File;
import java.io.FilenameFilter;
/**
* A class to clean up orphaned resource directories after unclean shutdowns.
**/
public class ResourceCleaner {
private static final String TAG = "ResourceCleaner";
private static final String TEMPORARY_RESOURCE_PREFIX = ".org.chromium.Chromium.";
private static final long DELAY_MS = 5000;
private class CleanTask extends AsyncTask<Void, Void, Void> {
private final File[] mFilesToDelete;
public CleanTask(File[] filesToDelete) {
mFilesToDelete = filesToDelete;
}
public boolean hasFilesToDelete() {
return mFilesToDelete.length > 0;
}
@Override
protected Void doInBackground(Void... unused) {
Log.i(TAG, "Cleaning " + mFilesToDelete.length + " resources.");
for (File file : mFilesToDelete) {
if (file.exists()) {
deleteRecursively(file);
}
}
return null;
}
private void deleteRecursively(File parent) {
if (parent.isDirectory()) {
for (File child : parent.listFiles()) {
deleteRecursively(child);
}
}
parent.delete();
}
}
private final Context mContext;
public ResourceCleaner(Context context) {
mContext = context;
}
public void start() {
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
return;
}
final CleanTask task = new CleanTask(cacheDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
boolean result = name.startsWith(TEMPORARY_RESOURCE_PREFIX);
return result;
}
}));
if (!task.hasFilesToDelete()) {
return;
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}, DELAY_MS);
}
}

View File

@@ -68,7 +68,9 @@ public class SkyApplication extends BaseChromiumApplication {
}
private void initResources() {
mResourceExtractor = new ResourceExtractor(getApplicationContext());
Context context = getApplicationContext();
new ResourceCleaner(context).start();
mResourceExtractor = new ResourceExtractor(context);
onBeforeResourceExtraction(mResourceExtractor);
mResourceExtractor.start();
}