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

Unified Diff: base/android/java/src/org/chromium/base/ThreadUtils.java

Issue 2810373002: InstalledAppProviderImpl: Assert that code is run off the UI thread. (Closed)
Patch Set: Use real assert instead of exception (apparently this works now). Created 3 years, 7 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/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
}
/**
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/installedapp/InstalledAppProviderImpl.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698