OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.metrics; | 5 package org.chromium.base.metrics; |
6 | 6 |
7 import org.chromium.base.ThreadUtils; | 7 import org.chromium.base.ThreadUtils; |
| 8 import org.chromium.base.VisibleForTesting; |
8 import org.chromium.base.annotations.JNINamespace; | 9 import org.chromium.base.annotations.JNINamespace; |
9 | 10 |
10 /** | 11 /** |
11 * Java API for recording UMA actions. | 12 * Java API for recording UMA actions. |
12 * | 13 * |
13 * WARNINGS: | 14 * WARNINGS: |
14 * JNI calls are relatively costly - avoid using in performance-critical code. | 15 * JNI calls are relatively costly - avoid using in performance-critical code. |
15 * | 16 * |
16 * We use a script (extract_actions.py) to scan the source code and extract acti
ons. A string | 17 * We use a script (extract_actions.py) to scan the source code and extract acti
ons. A string |
17 * literal (not a variable) must be passed to record(). | 18 * literal (not a variable) must be passed to record(). |
18 */ | 19 */ |
19 @JNINamespace("base::android") | 20 @JNINamespace("base::android") |
20 public class RecordUserAction { | 21 public class RecordUserAction { |
| 22 private static Throwable sDisabledBy; |
| 23 |
| 24 /** |
| 25 * Tests may not have native initialized, so they may need to disable metric
s. The value should |
| 26 * be reset after the test done, to avoid carrying over state to unrelated t
ests. |
| 27 */ |
| 28 @VisibleForTesting |
| 29 public static void setDisabledForTests(boolean disabled) { |
| 30 if (disabled && sDisabledBy != null) { |
| 31 throw new IllegalStateException("UserActions are already disabled.",
sDisabledBy); |
| 32 } |
| 33 sDisabledBy = disabled ? new Throwable() : null; |
| 34 } |
| 35 |
21 public static void record(final String action) { | 36 public static void record(final String action) { |
| 37 if (sDisabledBy != null) return; |
| 38 |
22 if (ThreadUtils.runningOnUiThread()) { | 39 if (ThreadUtils.runningOnUiThread()) { |
23 nativeRecordUserAction(action); | 40 nativeRecordUserAction(action); |
24 return; | 41 return; |
25 } | 42 } |
26 | 43 |
27 ThreadUtils.runOnUiThread(new Runnable() { | 44 ThreadUtils.runOnUiThread(new Runnable() { |
28 @Override | 45 @Override |
29 public void run() { | 46 public void run() { |
30 nativeRecordUserAction(action); | 47 nativeRecordUserAction(action); |
31 } | 48 } |
32 }); | 49 }); |
33 } | 50 } |
34 | 51 |
35 private static native void nativeRecordUserAction(String action); | 52 private static native void nativeRecordUserAction(String action); |
36 } | 53 } |
OLD | NEW |