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

Unified Diff: content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java

Issue 2813153002: getInstalledRelatedApps: Avoid blocking the UI thread doing computation. (Closed)
Patch Set: Respond to review. Created 3 years, 8 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 | « no previous file | content/public/android/junit/src/org/chromium/content/browser/installedapp/InstalledAppProviderTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java
diff --git a/content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java b/content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java
index d07abed0a3f938dd176d1d87d8d89a9ea214e5ea..6258aa3baeb8cfb5bb4d5209238e6069d9b29c28 100644
--- a/content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java
+++ b/content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java
@@ -9,6 +9,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
+import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
@@ -68,13 +69,46 @@ public class InstalledAppProviderImpl implements InstalledAppProvider {
@Override
public void filterInstalledApps(
- RelatedApplication[] relatedApps, FilterInstalledAppsResponse callback) {
+ final RelatedApplication[] relatedApps, final FilterInstalledAppsResponse callback) {
if (mFrameUrlDelegate.isIncognito()) {
callback.call(new RelatedApplication[0]);
return;
}
- URI frameUrl = mFrameUrlDelegate.getUrl();
+ final URI frameUrl = mFrameUrlDelegate.getUrl();
+
+ // Use an AsyncTask to execute the installed/related checks on a background thread (so as
+ // not to block the UI thread).
+ new AsyncTask<Void, Void, RelatedApplication[]>() {
+ @Override
+ protected RelatedApplication[] doInBackground(Void... unused) {
+ return filterInstalledAppsOnBackgroundThread(relatedApps, frameUrl);
+ }
+
+ @Override
+ protected void onPostExecute(RelatedApplication[] installedApps) {
+ callback.call(installedApps);
+ }
+ }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+ }
+
+ @Override
+ public void close() {}
+
+ @Override
+ public void onConnectionError(MojoException e) {}
+
+ /**
+ * Filters a list of apps, returning those that are both installed and match the origin.
+ *
+ * This method is expected to be called on a background thread (not the main UI thread).
+ *
+ * @param relatedApps A list of applications to be filtered.
+ * @param frameUrl The URL of the frame this operation was called from.
+ * @return A subsequence of applications that meet the criteria.
+ */
+ private RelatedApplication[] filterInstalledAppsOnBackgroundThread(
+ RelatedApplication[] relatedApps, URI frameUrl) {
ArrayList<RelatedApplication> installedApps = new ArrayList<RelatedApplication>();
PackageManager pm = mContext.getPackageManager();
for (RelatedApplication app : relatedApps) {
@@ -92,15 +126,9 @@ public class InstalledAppProviderImpl implements InstalledAppProvider {
RelatedApplication[] installedAppsArray = new RelatedApplication[installedApps.size()];
installedApps.toArray(installedAppsArray);
- callback.call(installedAppsArray);
+ return installedAppsArray;
}
- @Override
- public void close() {}
-
- @Override
- public void onConnectionError(MojoException e) {}
-
/**
* Determines whether a particular app is installed and matches the origin.
*
« no previous file with comments | « no previous file | content/public/android/junit/src/org/chromium/content/browser/installedapp/InstalledAppProviderTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698