Commit 0642df2c authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Fix non-UI thread ContextUtils.getProcessName() crash pre JB mr2

Before MR2, a ThreadLocal was used to store the currentActivityThread.
I don't think there are any existing calls to getProcessName() off of
the main thread, so this isn't fixing an existing crash.

Change-Id: If4cf17dbfe4f1cead2cc5a4ec043405ec665e922
Reviewed-on: https://chromium-review.googlesource.com/962866
Commit-Queue: agrieve <agrieve@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543787}
parent 6af9df3e
......@@ -25,6 +25,8 @@ import org.chromium.base.annotations.MainDex;
public class ContextUtils {
private static final String TAG = "ContextUtils";
private static Context sApplicationContext;
// TODO(agrieve): Remove sProcessName caching when we stop supporting JB.
private static String sProcessName;
/**
* Initialization-on-demand holder. This exists for thread-safe lazy initialization.
......@@ -132,6 +134,9 @@ public class ContextUtils {
throw new RuntimeException("Global application context cannot be set to null.");
}
sApplicationContext = appContext;
// TODO(agrieve): Remove when we stop supporting JB.
getProcessName(); // Prime the cache for getProcessName().
}
/**
......@@ -166,13 +171,20 @@ public class ContextUtils {
/** @return The name of the current process. E.g. "org.chromium.chrome:privileged_process0". */
public static String getProcessName() {
if (sProcessName != null) {
return sProcessName;
}
try {
// An even more convenient ActivityThread.currentProcessName() exists, but was not added
// until JB MR2.
Class<?> activityThreadClazz = Class.forName("android.app.ActivityThread");
Object activityThread =
activityThreadClazz.getMethod("currentActivityThread").invoke(null);
return (String) activityThreadClazz.getMethod("getProcessName").invoke(activityThread);
// Before JB MR2, currentActivityThread() returns null when called on a non-UI thread.
// Cache the name to allow other threads to access it.
sProcessName =
(String) activityThreadClazz.getMethod("getProcessName").invoke(activityThread);
return sProcessName;
} catch (Exception e) { // No multi-catch below API level 19 for reflection exceptions.
// If fallback logic is ever needed, refer to:
// https://chromium-review.googlesource.com/c/chromium/src/+/905563/1
......
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