Skip to content

Commit ebbf526

Browse files
committed
Clean up CrashlyticsWorkers, and fix some comments
1 parent 7005ce8 commit ebbf526

File tree

3 files changed

+16
-49
lines changed

3 files changed

+16
-49
lines changed

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/CrashlyticsRegistrar.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.firebase.crashlytics;
1616

17-
import static com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers.StrictLevel.ASSERT;
18-
1917
import com.google.firebase.FirebaseApp;
2018
import com.google.firebase.analytics.connector.AnalyticsConnector;
2119
import com.google.firebase.annotations.concurrent.Background;
@@ -69,10 +67,8 @@ public List<Component<?>> getComponents() {
6967
}
7068

7169
private FirebaseCrashlytics buildCrashlytics(ComponentContainer container) {
72-
// TODO(mrober): Make this a build time configuration. Do not release like this.
73-
CrashlyticsWorkers.setStrictLevel(ASSERT); // Kill the process on violation for debugging.
70+
CrashlyticsWorkers.setEnforcement(BuildConfig.DEBUG);
7471

75-
// CrashlyticsWorkers.checkMainThread();
7672
long startTime = System.currentTimeMillis();
7773

7874
FirebaseCrashlytics crashlytics =
@@ -86,8 +82,8 @@ private FirebaseCrashlytics buildCrashlytics(ComponentContainer container) {
8682
container.get(blockingExecutorService));
8783

8884
long duration = System.currentTimeMillis() - startTime;
89-
if (duration > 30) {
90-
Logger.getLogger().i("Initializing Crashlytics blocked main for " + duration + " ms");
85+
if (duration > 16) {
86+
Logger.getLogger().d("Initializing Crashlytics blocked main for " + duration + " ms");
9187
}
9288

9389
return crashlytics;

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/concurrency/CrashlyticsWorker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
/**
3434
* Worker for executing tasks sequentially on the given executor service.
3535
*
36-
* <p>Work on the queue may block, or it may return a Task, such that the underlying thread may be
37-
* re-used while the worker queue is still blocked.
36+
* <p>Work on the queue may suspend, or it may return a Task, such that the underlying thread may be
37+
* re-used while the worker queue is suspended.
3838
*
3939
* <p>Work enqueued on this worker will be run serially, regardless of the underlying executor.
4040
* Therefore, workers on the queue should not add new work to the queue and then block on it, as
@@ -104,7 +104,7 @@ public Task<Void> submit(Runnable runnable) {
104104
/**
105105
* Submits a <code>Callable Task</code> for asynchronous execution on the executor.
106106
*
107-
* <p>This is useful for making the worker block on an asynchronous operation, while letting the
107+
* <p>This is useful for making the worker suspend on an asynchronous operation, while letting the
108108
* underlying threads be re-used.
109109
*
110110
* <p>Returns a <code>Task</code> which will be resolved upon successful completion of the Task

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/concurrency/CrashlyticsWorkers.kt

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616

1717
package com.google.firebase.crashlytics.internal.concurrency
1818

19-
import android.os.Build
20-
import android.os.Looper
2119
import com.google.firebase.crashlytics.internal.Logger
22-
import com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers.StrictLevel.ASSERT
23-
import com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers.StrictLevel.NONE
24-
import com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers.StrictLevel.THROW
25-
import com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers.StrictLevel.WARN
2620
import java.util.concurrent.ExecutorService
2721

2822
/**
@@ -37,19 +31,20 @@ class CrashlyticsWorkers(
3731

3832
/**
3933
* The common worker is for common background tasks, like background init, user actions, and
40-
* processing uncaught exceptions. This is the main worker of the sdk.
34+
* processing uncaught exceptions. This is the main worker of the sdk. This worker will never
35+
* block on a disk write or network call.
4136
*/
4237
@JvmField val common = CrashlyticsWorker(backgroundExecutorService)
4338

4439
/**
45-
* The disk write worker is for background tasks that persisting data to local disk. Ideally, no
46-
* user action should require waiting on this (although still do).
40+
* The disk write worker is for background tasks that persisting data to local disk. No user
41+
* action should wait on this. Use for fire and forget, safe to ignore exceptions.
4742
*/
4843
@JvmField val diskWrite = CrashlyticsWorker(backgroundExecutorService)
4944

5045
/**
5146
* The data collect worker is for any background tasks that send data remotely, like fetching fid,
52-
* settings, or uploading crash reports. This worker is blocked until permission is granted.
47+
* settings, or uploading crash reports. This worker is suspended until permission is granted.
5348
*/
5449
@JvmField val dataCollect = CrashlyticsWorker(backgroundExecutorService)
5550

@@ -61,13 +56,8 @@ class CrashlyticsWorkers(
6156
private val threadName
6257
get() = Thread.currentThread().name
6358

64-
@JvmStatic var strictLevel: StrictLevel = NONE
65-
66-
@JvmStatic
67-
fun checkMainThread() =
68-
checkThread(::isMainThread) {
69-
"Must be called on the main thread, was called on $threadName."
70-
}
59+
/** When enabled, failed preconditions will cause assertion errors for debugging. */
60+
@JvmStatic var enforcement: Boolean = false
7161

7262
@JvmStatic
7363
fun checkBlockingThread() =
@@ -81,34 +71,15 @@ class CrashlyticsWorkers(
8171
"Must be called on a background thread, was called on $threadName."
8272
}
8373

84-
private fun isMainThread() =
85-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
86-
Looper.getMainLooper().isCurrentThread
87-
} else {
88-
Looper.getMainLooper() == Looper.myLooper()
89-
}
90-
9174
private fun isBlockingThread() = threadName.contains("Firebase Blocking Thread #")
9275

9376
private fun isBackgroundThread() = threadName.contains("Firebase Background Thread #")
9477

9578
private fun checkThread(isCorrectThread: () -> Boolean, failureMessage: () -> String) {
96-
if (strictLevel.level >= WARN.level && !isCorrectThread()) {
97-
Logger.getLogger().w(failureMessage())
98-
assert(strictLevel.level < ASSERT.level, failureMessage)
99-
check(strictLevel.level < THROW.level, failureMessage)
79+
if (!isCorrectThread()) {
80+
Logger.getLogger().d(failureMessage())
81+
assert(enforcement, failureMessage)
10082
}
10183
}
10284
}
103-
104-
enum class StrictLevel(val level: Int) : Comparable<StrictLevel> {
105-
/** Do not check for violations. */
106-
NONE(0),
107-
/** Log violations as warnings. */
108-
WARN(1),
109-
/** Throw an exception on violation. */
110-
THROW(2),
111-
/** Kill the process on violation. Useful for debugging. */
112-
ASSERT(3),
113-
}
11485
}

0 commit comments

Comments
 (0)