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

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: Fix spelling of UI. 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 771fab3ac45fd152fcadaac7d7a865ab0d9d35db..c396c509adcc93199fc92cd04c49012bf3219074 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 sThreadAssertsDisabled;
+
public static void setWillOverrideUiThread() {
synchronized (sLock) {
sWillOverride = true;
@@ -192,24 +194,50 @@ public class ThreadUtils {
/**
* Throw an exception (when DCHECKs are enabled) if currently not running on the UI thread.
+ *
+ * Can be disabled by setThreadAssertsDisabledForTesting(true).
*/
public static void assertOnUiThread() {
- if (BuildConfig.DCHECK_IS_ON && !runningOnUiThread()) {
- throw new IllegalStateException("Must be called on the Ui thread.");
- }
+ if (sThreadAssertsDisabled) return;
+
+ assert runningOnUiThread() : "Must be called on the UI thread.";
}
/**
* Throw an exception (regardless of build) if currently not running on the UI thread.
*
+ * Can be disabled by setThreadAssertsEnabledForTesting(false).
+ *
* @see #assertOnUiThread()
*/
public static void checkUiThread() {
- if (!runningOnUiThread()) {
- throw new IllegalStateException("Must be called on the Ui thread.");
+ if (!sThreadAssertsDisabled && !runningOnUiThread()) {
+ throw new IllegalStateException("Must be called on the UI thread.");
}
}
+ /**
+ * Throw an exception (when DCHECKs are enabled) if currently running on the UI thread.
+ *
+ * Can be disabled by setThreadAssertsDisabledForTesting(true).
+ */
+ public static void assertOnBackgroundThread() {
+ if (sThreadAssertsDisabled) 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).
+ */
+ public static void setThreadAssertsDisabledForTesting(boolean disabled) {
+ sThreadAssertsDisabled = disabled;
+ }
+
/**
* @return true iff the current thread is the main (UI) thread.
*/
« 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