Index: chrome/android/java_staging/src/org/chromium/chrome/browser/util/NonThreadSafe.java |
diff --git a/chrome/android/java_staging/src/org/chromium/chrome/browser/util/NonThreadSafe.java b/chrome/android/java_staging/src/org/chromium/chrome/browser/util/NonThreadSafe.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9bfc32cede62bfd107833018628a35f492ea0425 |
--- /dev/null |
+++ b/chrome/android/java_staging/src/org/chromium/chrome/browser/util/NonThreadSafe.java |
@@ -0,0 +1,45 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.util; |
+ |
+import org.chromium.base.VisibleForTesting; |
+import org.chromium.base.annotations.SuppressFBWarnings; |
+ |
+/** |
+ * NonThreadSafe is a helper class used to help verify that methods of a |
+ * class are called from the same thread. |
+ */ |
+public class NonThreadSafe { |
+ private Long mThreadId = null; |
+ |
+ public NonThreadSafe() { |
+ ensureThreadIdAssigned(); |
+ } |
+ |
+ /** |
+ * Changes the thread that is checked for in CalledOnValidThread. This may |
+ * be useful when an object may be created on one thread and then used |
+ * exclusively on another thread. |
+ */ |
+ @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
+ @VisibleForTesting |
+ public synchronized void detachFromThread() { |
+ mThreadId = null; |
+ } |
+ |
+ /** |
+ * Checks if the method is called on the valid thread. |
+ * Assigns the current thread if no thread was assigned. |
+ */ |
+ @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
+ public synchronized boolean calledOnValidThread() { |
+ ensureThreadIdAssigned(); |
+ return mThreadId.equals(Thread.currentThread().getId()); |
+ } |
+ |
+ private void ensureThreadIdAssigned() { |
+ if (mThreadId == null) mThreadId = Thread.currentThread().getId(); |
+ } |
+} |