Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Unified Diff: sky/shell/android/org/domokit/sky/shell/ResourceCleaner.java

Issue 1228083003: Stop leaking temp directories in SkyShell (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sky/shell/BUILD.gn ('k') | sky/shell/android/org/domokit/sky/shell/SkyApplication.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/shell/android/org/domokit/sky/shell/ResourceCleaner.java
diff --git a/sky/shell/android/org/domokit/sky/shell/ResourceCleaner.java b/sky/shell/android/org/domokit/sky/shell/ResourceCleaner.java
new file mode 100644
index 0000000000000000000000000000000000000000..8aa43a9628d195d05237dc8a972b3437cb7e2db1
--- /dev/null
+++ b/sky/shell/android/org/domokit/sky/shell/ResourceCleaner.java
@@ -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);
+ }
+}
« no previous file with comments | « sky/shell/BUILD.gn ('k') | sky/shell/android/org/domokit/sky/shell/SkyApplication.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698