diff options
author | wnwen <wnwen@chromium.org> | 2015-04-20 13:28:05 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-20 20:28:11 +0000 |
commit | 0a03497c2772f3bac98026f18816f154eec32f21 (patch) | |
tree | 51746530bd7d03835c31eaa7434ef5aeccce7067 /base | |
parent | a741bfe7184b208561e8de4cf6f1c7d8158a8088 (diff) | |
download | chromium_src-0a03497c2772f3bac98026f18816f154eec32f21.zip chromium_src-0a03497c2772f3bac98026f18816f154eec32f21.tar.gz chromium_src-0a03497c2772f3bac98026f18816f154eec32f21.tar.bz2 |
Avoid PathUtils fetching directory paths on UI thread.
Start async task as soon as the data suffix is known and by the time the data
and other directories paths are requested the work would already be done in the
background.
BUG=473353
Review URL: https://codereview.chromium.org/1091843002
Cr-Commit-Position: refs/heads/master@{#325901}
Diffstat (limited to 'base')
-rw-r--r-- | base/android/java/src/org/chromium/base/PathUtils.java | 64 |
1 files changed, 62 insertions, 2 deletions
diff --git a/base/android/java/src/org/chromium/base/PathUtils.java b/base/android/java/src/org/chromium/base/PathUtils.java index d70c0cc..bcd850c 100644 --- a/base/android/java/src/org/chromium/base/PathUtils.java +++ b/base/android/java/src/org/chromium/base/PathUtils.java @@ -6,8 +6,11 @@ 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. */ @@ -15,29 +18,80 @@ 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. + * + * TODO(wnwen): Remove this after all clients have migrated and add asserts for not null. + * * @param suffix The private data directory suffix. * @see Context#getDir(String, int) + * @deprecated */ + @Deprecated public static void setPrivateDataDirectorySuffix(String suffix) { + sDirPathFetchTask = null; sDataDirectorySuffix = suffix; } /** + * 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, final Context appContext) { + sDataDirectorySuffix = null; + 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; + } + + /** * @return the private directory that is used to store application data. */ @CalledByNative public static String getDataDirectory(Context appContext) { - if (sDataDirectorySuffix == null) { + if (sDataDirectorySuffix == null && sDirPathFetchTask == null) { throw new IllegalStateException( "setDataDirectorySuffix must be called before getDataDirectory"); + } else if (sDirPathFetchTask != null) { + return getDirectoryPath(DATA_DIRECTORY); + } else { + // Temporarily allow UI thread directory fetching until all callers have been migrated. + return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath(); } - return appContext.getDir(sDataDirectorySuffix, Context.MODE_PRIVATE).getPath(); } /** @@ -45,6 +99,9 @@ public abstract class PathUtils { */ @CalledByNative public static String getDatabaseDirectory(Context appContext) { + if (sDirPathFetchTask != null) { + return getDirectoryPath(DATABASE_DIRECTORY); + } // Context.getDatabasePath() returns path for the provided filename. return appContext.getDatabasePath("foo").getParent(); } @@ -55,6 +112,9 @@ public abstract class PathUtils { @SuppressWarnings("unused") @CalledByNative public static String getCacheDirectory(Context appContext) { + if (sDirPathFetchTask != null) { + return getDirectoryPath(CACHE_DIRECTORY); + } return appContext.getCacheDir().getPath(); } |