Commit 12dd026e authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Android: PathUtils.setPrivateDataDirectorySuffix using FutureTask

We don't need to use AsyncTask here since we don't use any onPreExecute
or onPostExecute. Webview cannot have a UI thread set during
CookieManager startup, so having AsyncTask post to the UI thread when it
runs onPostExecute is not going to work.

Bug: 542151
Change-Id: I289722205588c86bcc9fbde6206447223426e5d8
Reviewed-on: https://chromium-review.googlesource.com/1161043Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580292}
parent ba9f84b0
...@@ -20,6 +20,7 @@ import org.chromium.base.metrics.RecordHistogram; ...@@ -20,6 +20,7 @@ import org.chromium.base.metrics.RecordHistogram;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
...@@ -36,9 +37,9 @@ public abstract class PathUtils { ...@@ -36,9 +37,9 @@ public abstract class PathUtils {
private static final int CACHE_DIRECTORY = 2; private static final int CACHE_DIRECTORY = 2;
private static final int NUM_DIRECTORIES = 3; private static final int NUM_DIRECTORIES = 3;
private static final AtomicBoolean sInitializationStarted = new AtomicBoolean(); private static final AtomicBoolean sInitializationStarted = new AtomicBoolean();
private static AsyncTask<Void, Void, String[]> sDirPathFetchTask; private static FutureTask<String[]> sDirPathFetchTask;
// If the AsyncTask started in setPrivateDataDirectorySuffix() fails to complete by the time we // If the FutureTask started in setPrivateDataDirectorySuffix() fails to complete by the time we
// need the values, we will need the suffix so that we can restart the task synchronously on // need the values, we will need the suffix so that we can restart the task synchronously on
// the UI thread. // the UI thread.
private static String sDataDirectorySuffix; private static String sDataDirectorySuffix;
...@@ -102,8 +103,8 @@ public abstract class PathUtils { ...@@ -102,8 +103,8 @@ public abstract class PathUtils {
/** /**
* Fetch the path of the directory where private data is to be stored by the application. This * Fetch the path of the directory where private data is to be stored by the application. This
* is meant to be called in an AsyncTask in setPrivateDataDirectorySuffix(), but if we need the * is meant to be called in an FutureTask in setPrivateDataDirectorySuffix(), but if we need the
* result before the AsyncTask has had a chance to finish, then it's best to cancel the task * result before the FutureTask has had a chance to finish, then it's best to cancel the task
* and run it on the UI thread instead, inside getOrComputeDirectoryPaths(). * and run it on the UI thread instead, inside getOrComputeDirectoryPaths().
* *
* @see Context#getDir(String, int) * @see Context#getDir(String, int)
...@@ -148,12 +149,12 @@ public abstract class PathUtils { ...@@ -148,12 +149,12 @@ public abstract class PathUtils {
assert ContextUtils.getApplicationContext() != null; assert ContextUtils.getApplicationContext() != null;
sDataDirectorySuffix = suffix; sDataDirectorySuffix = suffix;
sCacheSubDirectory = cacheSubDir; sCacheSubDirectory = cacheSubDir;
sDirPathFetchTask = new AsyncTask<Void, Void, String[]>() {
@Override // We don't use an AsyncTask because this function is called in early Webview startup
protected String[] doInBackground(Void... unused) { // and it won't always have a UI thread available. Thus, we can't use
return PathUtils.setPrivateDataDirectorySuffixInternal(); // AsyncTask which inherently posts to the UI thread for onPostExecute().
} sDirPathFetchTask = new FutureTask<>(PathUtils::setPrivateDataDirectorySuffixInternal);
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); AsyncTask.THREAD_POOL_EXECUTOR.execute(sDirPathFetchTask);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment