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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/metrics/LaunchMetrics.java

Issue 2435813002: Move Java CachedMetrics functionality to base/. (Closed)
Patch Set: Fix compile. Created 4 years, 2 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
OLDNEW
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.chrome.browser.metrics; 5 package org.chromium.chrome.browser.metrics;
6 6
7 import android.util.Pair; 7 import android.util.Pair;
8 8
9 import org.chromium.base.annotations.JNINamespace; 9 import org.chromium.base.annotations.JNINamespace;
10 import org.chromium.base.library_loader.LibraryLoader; 10 import org.chromium.base.metrics.CachedMetrics;
11 import org.chromium.base.metrics.RecordHistogram;
12 import org.chromium.base.metrics.RecordUserAction;
13 import org.chromium.content_public.browser.WebContents; 11 import org.chromium.content_public.browser.WebContents;
14 12
15 import java.util.ArrayList; 13 import java.util.ArrayList;
16 import java.util.List; 14 import java.util.List;
17 import java.util.concurrent.TimeUnit;
18 15
19 /** 16 /**
20 * Used for recording metrics about Chrome launches that need to be recorded bef ore the native 17 * Used for recording metrics about Chrome launches that need to be recorded bef ore the native
21 * library may have been loaded. Metrics are cached until the library is known to be loaded, then 18 * library may have been loaded. Metrics are cached until the library is known to be loaded, then
22 * committed to the MetricsService all at once. 19 * committed to the MetricsService all at once.
23 */ 20 */
24 @JNINamespace("metrics") 21 @JNINamespace("metrics")
25 public class LaunchMetrics { 22 public class LaunchMetrics {
26
27 /**
28 * Creating an instance of a subclass of this class automatically adds it to a list of objects
29 * that are committed when the native library is available.
30 */
31 private abstract static class CachedHistogram {
32 private static final List<CachedHistogram> sEvents = new ArrayList<Cache dHistogram>();
33
34 protected final String mHistogramName;
35
36 /**
37 * @param histogramName Name of the histogram to record.
38 */
39 protected CachedHistogram(String histogramName) {
40 mHistogramName = histogramName;
41 sEvents.add(this);
42 }
43
44 /** Commits the histogram. Expects the native library to be loaded. */
45 protected abstract void commitAndClear();
46 }
47
48 /**
49 * Caches an action that will be recorded after native side is loaded.
50 */
51 public static class ActionEvent extends CachedHistogram {
52 private int mCount;
53
54 public ActionEvent(String actionName) {
55 super(actionName);
56 }
57
58 public void record() {
59 if (LibraryLoader.isInitialized()) {
60 recordWithNative();
61 } else {
62 mCount++;
63 }
64 }
65
66 private void recordWithNative() {
67 RecordUserAction.record(mHistogramName);
68 }
69
70 @Override
71 protected void commitAndClear() {
72 while (mCount > 0) {
73 recordWithNative();
74 mCount--;
75 }
76 }
77 }
78
79 /** Caches a set of integer histogram samples. */
80 public static class SparseHistogramSample extends CachedHistogram {
81 private final List<Integer> mSamples = new ArrayList<Integer>();
82
83 public SparseHistogramSample(String histogramName) {
84 super(histogramName);
85 }
86
87 public void record(int sample) {
88 if (LibraryLoader.isInitialized()) {
89 recordWithNative(sample);
90 } else {
91 mSamples.add(sample);
92 }
93 }
94
95 private void recordWithNative(int sample) {
96 RecordHistogram.recordSparseSlowlyHistogram(mHistogramName, sample);
97 }
98
99 @Override
100 protected void commitAndClear() {
101 for (Integer sample : mSamples) {
102 recordWithNative(sample);
103 }
104 mSamples.clear();
105 }
106 }
107
108 /** Caches a set of enumerated histogram samples. */
109 public static class EnumeratedHistogramSample extends CachedHistogram {
110 private final List<Integer> mSamples = new ArrayList<Integer>();
111 private final int mMaxValue;
112
113 public EnumeratedHistogramSample(String histogramName, int maxValue) {
114 super(histogramName);
115 mMaxValue = maxValue;
116 }
117
118 public void record(int sample) {
119 if (LibraryLoader.isInitialized()) {
120 recordWithNative(sample);
121 } else {
122 mSamples.add(sample);
123 }
124 }
125
126 private void recordWithNative(int sample) {
127 RecordHistogram.recordEnumeratedHistogram(mHistogramName, sample, mM axValue);
128 }
129
130 @Override
131 protected void commitAndClear() {
132 for (Integer sample : mSamples) {
133 recordWithNative(sample);
134 }
135 mSamples.clear();
136 }
137 }
138
139 /** Caches a set of times histogram samples. */
140 public static class TimesHistogramSample extends CachedHistogram {
141 private final List<Long> mSamples = new ArrayList<Long>();
142 private final TimeUnit mTimeUnit;
143
144 public TimesHistogramSample(String histogramName, TimeUnit timeUnit) {
145 super(histogramName);
146 mTimeUnit = timeUnit;
147 }
148
149 public void record(long sample) {
150 if (LibraryLoader.isInitialized()) {
151 recordWithNative(sample);
152 } else {
153 mSamples.add(sample);
154 }
155 }
156
157 private void recordWithNative(long sample) {
158 RecordHistogram.recordTimesHistogram(mHistogramName, sample, mTimeUn it);
159 }
160
161 @Override
162 protected void commitAndClear() {
163 for (Long sample : mSamples) {
164 recordWithNative(sample);
165 }
166 mSamples.clear();
167 }
168 }
169
170 // Each list item is a pair of the url and where it was added from e.g. from the add to 23 // Each list item is a pair of the url and where it was added from e.g. from the add to
171 // homescreen menu item, an app banner, or unknown. The mapping of int sourc e values to 24 // homescreen menu item, an app banner, or unknown. The mapping of int sourc e values to
172 // their string names is found in the C++ ShortcutInfo struct. 25 // their string names is found in the C++ ShortcutInfo struct.
173 private static final List<Pair<String, Integer>> sActivityUrls = 26 private static final List<Pair<String, Integer>> sActivityUrls =
174 new ArrayList<Pair<String, Integer>>(); 27 new ArrayList<Pair<String, Integer>>();
175 private static final List<Pair<String, Integer>> sTabUrls = 28 private static final List<Pair<String, Integer>> sTabUrls =
176 new ArrayList<Pair<String, Integer>>(); 29 new ArrayList<Pair<String, Integer>>();
177 30
178 private static final List<Long> sWebappHistogramTimes = new ArrayList<Long>( ); 31 private static final List<Long> sWebappHistogramTimes = new ArrayList<Long>( );
179 32
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 nativeRecordLaunch(true, item.first, item.second, webContents); 68 nativeRecordLaunch(true, item.first, item.second, webContents);
216 } 69 }
217 sActivityUrls.clear(); 70 sActivityUrls.clear();
218 71
219 for (Pair<String, Integer> item : sTabUrls) { 72 for (Pair<String, Integer> item : sTabUrls) {
220 nativeRecordLaunch(false, item.first, item.second, webContents); 73 nativeRecordLaunch(false, item.first, item.second, webContents);
221 } 74 }
222 sTabUrls.clear(); 75 sTabUrls.clear();
223 76
224 // Record generic cached events. 77 // Record generic cached events.
225 for (CachedHistogram event : CachedHistogram.sEvents) event.commitAndCle ar(); 78 CachedMetrics.commitCachedMetrics();
226 } 79 }
227 80
228 /** 81 /**
229 * Records metrics about the state of the homepage on launch. 82 * Records metrics about the state of the homepage on launch.
230 * @param showHomeButton Whether the home button is shown. 83 * @param showHomeButton Whether the home button is shown.
231 * @param homepageIsNtp Whether the homepage is set to the NTP. 84 * @param homepageIsNtp Whether the homepage is set to the NTP.
232 * @param homepageUrl The value of the homepage URL. 85 * @param homepageUrl The value of the homepage URL.
233 */ 86 */
234 public static void recordHomePageLaunchMetrics( 87 public static void recordHomePageLaunchMetrics(
235 boolean showHomeButton, boolean homepageIsNtp, String homepageUrl) { 88 boolean showHomeButton, boolean homepageIsNtp, String homepageUrl) {
236 if (homepageUrl == null) { 89 if (homepageUrl == null) {
237 homepageUrl = ""; 90 homepageUrl = "";
238 assert !showHomeButton : "Homepage should be disabled for a null URL "; 91 assert !showHomeButton : "Homepage should be disabled for a null URL ";
239 } 92 }
240 nativeRecordHomePageLaunchMetrics(showHomeButton, homepageIsNtp, homepag eUrl); 93 nativeRecordHomePageLaunchMetrics(showHomeButton, homepageIsNtp, homepag eUrl);
241 } 94 }
242 95
243 private static native void nativeRecordLaunch( 96 private static native void nativeRecordLaunch(
244 boolean standalone, String url, int source, WebContents webContents) ; 97 boolean standalone, String url, int source, WebContents webContents) ;
245 private static native void nativeRecordHomePageLaunchMetrics( 98 private static native void nativeRecordHomePageLaunchMetrics(
246 boolean showHomeButton, boolean homepageIsNtp, String homepageUrl); 99 boolean showHomeButton, boolean homepageIsNtp, String homepageUrl);
247 } 100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698