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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base; 5 package org.chromium.base;
6 6
7 import android.os.Handler; 7 import android.os.Handler;
8 import android.os.Looper; 8 import android.os.Looper;
9 import android.os.Process; 9 import android.os.Process;
10 10
11 import org.chromium.base.annotations.CalledByNative; 11 import org.chromium.base.annotations.CalledByNative;
12 12
13 import java.util.concurrent.Callable; 13 import java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException; 14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.FutureTask; 15 import java.util.concurrent.FutureTask;
16 16
17 /** 17 /**
18 * Helper methods to deal with threading related tasks. 18 * Helper methods to deal with threading related tasks.
19 */ 19 */
20 public class ThreadUtils { 20 public class ThreadUtils {
21 21
22 private static final Object sLock = new Object(); 22 private static final Object sLock = new Object();
23 23
24 private static boolean sWillOverride; 24 private static boolean sWillOverride;
25 25
26 private static Handler sUiThreadHandler; 26 private static Handler sUiThreadHandler;
27 27
28 private static boolean sThreadAssertsDisabled;
29
28 public static void setWillOverrideUiThread() { 30 public static void setWillOverrideUiThread() {
29 synchronized (sLock) { 31 synchronized (sLock) {
30 sWillOverride = true; 32 sWillOverride = true;
31 } 33 }
32 } 34 }
33 35
34 @VisibleForTesting 36 @VisibleForTesting
35 public static void setUiThread(Looper looper) { 37 public static void setUiThread(Looper looper) {
36 synchronized (sLock) { 38 synchronized (sLock) {
37 if (looper == null) { 39 if (looper == null) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 * @param task The Runnable to run 187 * @param task The Runnable to run
186 * @param delayMillis The delay in milliseconds until the Runnable will be r un 188 * @param delayMillis The delay in milliseconds until the Runnable will be r un
187 */ 189 */
188 @VisibleForTesting 190 @VisibleForTesting
189 public static void postOnUiThreadDelayed(Runnable task, long delayMillis) { 191 public static void postOnUiThreadDelayed(Runnable task, long delayMillis) {
190 getUiThreadHandler().postDelayed(task, delayMillis); 192 getUiThreadHandler().postDelayed(task, delayMillis);
191 } 193 }
192 194
193 /** 195 /**
194 * Throw an exception (when DCHECKs are enabled) if currently not running on the UI thread. 196 * Throw an exception (when DCHECKs are enabled) if currently not running on the UI thread.
197 *
198 * Can be disabled by setThreadAssertsDisabledForTesting(true).
195 */ 199 */
196 public static void assertOnUiThread() { 200 public static void assertOnUiThread() {
197 if (BuildConfig.DCHECK_IS_ON && !runningOnUiThread()) { 201 if (sThreadAssertsDisabled) return;
198 throw new IllegalStateException("Must be called on the Ui thread."); 202
199 } 203 assert runningOnUiThread() : "Must be called on the UI thread.";
200 } 204 }
201 205
202 /** 206 /**
203 * Throw an exception (regardless of build) if currently not running on the UI thread. 207 * Throw an exception (regardless of build) if currently not running on the UI thread.
204 * 208 *
209 * Can be disabled by setThreadAssertsEnabledForTesting(false).
210 *
205 * @see #assertOnUiThread() 211 * @see #assertOnUiThread()
206 */ 212 */
207 public static void checkUiThread() { 213 public static void checkUiThread() {
208 if (!runningOnUiThread()) { 214 if (!sThreadAssertsDisabled && !runningOnUiThread()) {
209 throw new IllegalStateException("Must be called on the Ui thread."); 215 throw new IllegalStateException("Must be called on the UI thread.");
210 } 216 }
211 } 217 }
212 218
213 /** 219 /**
220 * Throw an exception (when DCHECKs are enabled) if currently running on the UI thread.
221 *
222 * Can be disabled by setThreadAssertsDisabledForTesting(true).
223 */
224 public static void assertOnBackgroundThread() {
225 if (sThreadAssertsDisabled) return;
226
227 assert !runningOnUiThread() : "Must be called on a thread other than UI. ";
228 }
229
230 /**
231 * Disables thread asserts.
232 *
233 * Can be used by tests where code that normally runs multi-threaded is goin g to run
234 * single-threaded for the test (otherwise asserts that are valid in product ion would fail in
235 * those tests).
236 */
237 public static void setThreadAssertsDisabledForTesting(boolean disabled) {
238 sThreadAssertsDisabled = disabled;
239 }
240
241 /**
214 * @return true iff the current thread is the main (UI) thread. 242 * @return true iff the current thread is the main (UI) thread.
215 */ 243 */
216 public static boolean runningOnUiThread() { 244 public static boolean runningOnUiThread() {
217 return getUiThreadHandler().getLooper() == Looper.myLooper(); 245 return getUiThreadHandler().getLooper() == Looper.myLooper();
218 } 246 }
219 247
220 public static Looper getUiThreadLooper() { 248 public static Looper getUiThreadLooper() {
221 return getUiThreadHandler().getLooper(); 249 return getUiThreadHandler().getLooper();
222 } 250 }
223 251
224 /** 252 /**
225 * Set thread priority to audio. 253 * Set thread priority to audio.
226 */ 254 */
227 @CalledByNative 255 @CalledByNative
228 public static void setThreadPriorityAudio(int tid) { 256 public static void setThreadPriorityAudio(int tid) {
229 Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO); 257 Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO);
230 } 258 }
231 259
232 /** 260 /**
233 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not. 261 * Checks whether Thread priority is THREAD_PRIORITY_AUDIO or not.
234 * @param tid Thread id. 262 * @param tid Thread id.
235 * @return true for THREAD_PRIORITY_AUDIO and false otherwise. 263 * @return true for THREAD_PRIORITY_AUDIO and false otherwise.
236 */ 264 */
237 @CalledByNative 265 @CalledByNative
238 private static boolean isThreadPriorityAudio(int tid) { 266 private static boolean isThreadPriorityAudio(int tid) {
239 return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO; 267 return Process.getThreadPriority(tid) == Process.THREAD_PRIORITY_AUDIO;
240 } 268 }
241 } 269 }
OLDNEW
« 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