InstalledAppProviderImpl: Assert that code is run off the UI thread.

Adds methods to ThreadUtils to support this: a new
assertOnBackgroundThread to complement assertOnUiThread, as well as a
way to disable these checks for testing (since the tests are
single-threaded).

BUG=710745

Review-Url: https://codereview.chromium.org/2810373002
Cr-Commit-Position: refs/heads/master@{#470786}


CrOS-Libchrome-Original-Commit: 25a9390dce3a08a787c852a3d381322767dace3c
diff --git a/base/android/java/src/org/chromium/base/ThreadUtils.java b/base/android/java/src/org/chromium/base/ThreadUtils.java
index 771fab3..c396c50 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 @@
 
     private static Handler sUiThreadHandler;
 
+    private static boolean sThreadAssertsDisabled;
+
     public static void setWillOverrideUiThread() {
         synchronized (sLock) {
             sWillOverride = true;
@@ -192,25 +194,51 @@
 
     /**
      * 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.
      */
     public static boolean runningOnUiThread() {