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

Unified Diff: base/android/java/src/org/chromium/base/PathUtils.java

Issue 1124763003: Update from https://crrev.com/327068 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: update nacl, buildtools, fix display_change_notifier_unittest Created 5 years, 7 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
Index: base/android/java/src/org/chromium/base/PathUtils.java
diff --git a/base/android/java/src/org/chromium/base/PathUtils.java b/base/android/java/src/org/chromium/base/PathUtils.java
index d70c0cc95ead4185f7a68fa1da2310db2b09a43d..5da080e834d6e684b53e272c7b4cc38eaf2cfbe3 100644
--- a/base/android/java/src/org/chromium/base/PathUtils.java
+++ b/base/android/java/src/org/chromium/base/PathUtils.java
@@ -6,26 +6,58 @@ package org.chromium.base;
import android.content.Context;
import android.content.pm.ApplicationInfo;
+import android.os.AsyncTask;
import android.os.Environment;
+import java.util.concurrent.ExecutionException;
+
/**
* This class provides the path related methods for the native library.
*/
public abstract class PathUtils {
- private static String sDataDirectorySuffix;
+ private static final int DATA_DIRECTORY = 0;
+ private static final int DATABASE_DIRECTORY = 1;
+ private static final int CACHE_DIRECTORY = 2;
+ private static final int NUM_DIRECTORIES = 3;
+ private static AsyncTask<String, Void, String[]> sDirPathFetchTask;
// Prevent instantiation.
private PathUtils() {}
/**
- * Sets the suffix that should be used for the directory where private data is to be stored
- * by the application.
+ * Starts an asynchronous task to fetch the path of the directory where private data is to be
+ * stored by the application.
+ *
* @param suffix The private data directory suffix.
* @see Context#getDir(String, int)
*/
- public static void setPrivateDataDirectorySuffix(String suffix) {
- sDataDirectorySuffix = suffix;
+ public static void setPrivateDataDirectorySuffix(String suffix, Context context) {
+ final Context appContext = context.getApplicationContext();
+ sDirPathFetchTask = new AsyncTask<String, Void, String[]>() {
+ @Override
+ protected String[] doInBackground(String... dataDirectorySuffix) {
+ String[] paths = new String[NUM_DIRECTORIES];
+ paths[DATA_DIRECTORY] =
+ appContext.getDir(dataDirectorySuffix[0], Context.MODE_PRIVATE).getPath();
+ paths[DATABASE_DIRECTORY] = appContext.getDatabasePath("foo").getParent();
+ paths[CACHE_DIRECTORY] = appContext.getCacheDir().getPath();
+ return paths;
+ }
+ }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, suffix);
+ }
+
+ /**
+ * @param index The index of the cached directory path.
+ * @return The directory path requested, or null if not available.
+ */
+ private static String getDirectoryPath(int index) {
+ try {
+ return sDirPathFetchTask.get()[index];
+ } catch (InterruptedException e) {
+ } catch (ExecutionException e) {
+ }
+ return null;
}
/**
@@ -33,11 +65,8 @@ public abstract class PathUtils {
*/
@CalledByNative
public static String getDataDirectory(Context appContext) {
- if (sDataDirectorySuffix == null) {
- throw new IllegalStateException(
- "setDataDirectorySuffix must be called before getDataDirectory");
- }
- return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath();
+ assert sDirPathFetchTask != null : "setDataDirectorySuffix must be called first.";
+ return getDirectoryPath(DATA_DIRECTORY);
}
/**
@@ -45,8 +74,8 @@ public abstract class PathUtils {
*/
@CalledByNative
public static String getDatabaseDirectory(Context appContext) {
- // Context.getDatabasePath() returns path for the provided filename.
- return appContext.getDatabasePath("foo").getParent();
+ assert sDirPathFetchTask != null : "setDataDirectorySuffix must be called first.";
+ return getDirectoryPath(DATABASE_DIRECTORY);
}
/**
@@ -55,7 +84,8 @@ public abstract class PathUtils {
@SuppressWarnings("unused")
@CalledByNative
public static String getCacheDirectory(Context appContext) {
- return appContext.getCacheDir().getPath();
+ assert sDirPathFetchTask != null : "setDataDirectorySuffix must be called first.";
+ return getDirectoryPath(CACHE_DIRECTORY);
}
/**

Powered by Google App Engine
This is Rietveld 408576698