• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.metrics;
6 
7 /** Holds the {@link CachingUmaRecorder} used by {@link RecordHistogram}. */
8 public class UmaRecorderHolder {
UmaRecorderHolder()9     private UmaRecorderHolder() {}
10 
11     /** The instance held by this class. */
12     private static CachingUmaRecorder sRecorder = new CachingUmaRecorder();
13 
14     /** Set up native UMA Recorder */
15     private static boolean sSetUpNativeUmaRecorder = true;
16 
17     /** Whether onLibraryLoaded() was called. */
18     private static boolean sNativeInitialized;
19 
20     /** Returns the held {@link UmaRecorder}. */
get()21     public static UmaRecorder get() {
22         return sRecorder;
23     }
24 
25     /**
26      * Set a new {@link UmaRecorder} delegate for the {@link CachingUmaRecorder}.
27      * Returns after the cache has been flushed to the new delegate.
28      * <p>
29      * It should be used in processes that don't or haven't loaded native yet. This should never
30      * be called after calling {@link #onLibraryLoaded()}.
31      *
32      * @param recorder the new UmaRecorder that metrics will be forwarded to.
33      */
setNonNativeDelegate(UmaRecorder recorder)34     public static void setNonNativeDelegate(UmaRecorder recorder) {
35         UmaRecorder previous = sRecorder.setDelegate(recorder);
36         assert !(previous instanceof NativeUmaRecorder)
37                 : "A NativeUmaRecorder has already been set";
38     }
39 
40     /**
41      * Whether a native UMA Recorder should be set up.
42      * @param setUpNativeUmaRecorder indicates whether a native UMA recorder should be set up.
43      * Defaults to true if unset.
44      */
setUpNativeUmaRecorder(boolean setUpNativeUmaRecorder)45     public static void setUpNativeUmaRecorder(boolean setUpNativeUmaRecorder) {
46         sSetUpNativeUmaRecorder = setUpNativeUmaRecorder;
47     }
48 
49     /** Starts forwarding metrics to the native code. Returns after the cache has been flushed. */
onLibraryLoaded()50     public static void onLibraryLoaded() {
51         if (!sSetUpNativeUmaRecorder) return;
52 
53         assert !sNativeInitialized;
54         sNativeInitialized = true;
55         sRecorder.setDelegate(new NativeUmaRecorder());
56     }
57 
58     /** Reset globals for tests. */
resetForTesting()59     public static void resetForTesting() {
60         // Prevent hitting cache size limits from tests running without ever switching to the native
61         // recorder. Also guards against tests that use setNonNativeDelegate() to inject a mock from
62         // forgetting to reset it.
63         if (!sNativeInitialized) {
64             sRecorder = new CachingUmaRecorder();
65         }
66     }
67 }
68