Index: base/android/java/src/org/chromium/base/ThreadUtils.java |
diff --git a/base/android/java/src/org/chromium/base/ThreadUtils.java b/base/android/java/src/org/chromium/base/ThreadUtils.java |
index dbad59d093fa695f43c060bc1e93873a95295761..4979c5f7fcb496a7b44e882dcd366c209e1d705f 100644 |
--- a/base/android/java/src/org/chromium/base/ThreadUtils.java |
+++ b/base/android/java/src/org/chromium/base/ThreadUtils.java |
@@ -25,6 +25,8 @@ public class ThreadUtils { |
private static Handler sUiThreadHandler; |
+ private static boolean sThreadAssertsEnabled = true; |
agrieve
2017/05/05 15:00:48
nit: Generally better to have things default-initi
Matt Giuca
2017/05/08 01:18:10
Done.
|
+ |
public static void setWillOverrideUiThread() { |
synchronized (sLock) { |
sWillOverride = true; |
@@ -191,12 +193,41 @@ public class ThreadUtils { |
} |
/** |
- * Asserts that the current thread is running on the main thread. |
+ * Asserts that the current thread is the main (UI) thread. |
+ * |
+ * Can be disabled by setThreadAssertsEnabled(false). |
*/ |
public static void assertOnUiThread() { |
- if (BuildConfig.DCHECK_IS_ON && !runningOnUiThread()) { |
- throw new IllegalStateException("Must be called on the Ui thread."); |
+ if (!sThreadAssertsEnabled) { |
+ return; |
+ } |
+ |
+ assert runningOnUiThread() : "Must be called on the UI thread."; |
+ } |
+ |
+ /** |
+ * Asserts that the current thread is NOT the main (UI) thread. |
+ * |
+ * Can be disabled by setThreadAssertsEnabled(false). |
+ */ |
+ public static void assertOnBackgroundThread() { |
+ if (!sThreadAssertsEnabled) { |
+ return; |
} |
+ |
+ assert !runningOnUiThread() : "Must be called on a thread other than UI."; |
+ } |
+ |
+ /** |
+ * Disables thread asserts. |
+ * |
+ * Can be used by tests where code that normally runs multi-threaded is going to run |
+ * single-threaded for the test (otherwise asserts that are valid in production would fail in |
+ * those tests). |
+ */ |
+ @VisibleForTesting |
+ public static void setThreadAssertsEnabled(boolean enabled) { |
Ted C
2017/05/05 14:09:39
I would add "ForTesting" to the end of the name to
agrieve
2017/05/05 15:00:49
Along the same lines, probably would drop "Visible
Matt Giuca
2017/05/08 01:18:10
I'm confused about both of these suggestions... yo
agrieve
2017/05/08 14:18:15
Consistency is certainly not our strong suite. To
|
+ sThreadAssertsEnabled = enabled; |
} |
/** |