Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.contextualsearch; | 5 package org.chromium.chrome.browser.contextualsearch; |
| 6 | 6 |
| 7 import org.chromium.base.Log; | 7 import org.chromium.base.Log; |
| 8 | 8 |
| 9 import java.net.URL; | |
| 10 | |
| 9 /** | 11 /** |
| 10 * Implements the UMA logging for Ranker that's used for Contextual Search Tap S uppression. | 12 * Implements the UMA logging for Ranker that's used for Contextual Search Tap S uppression. |
| 11 */ | 13 */ |
| 12 public class ContextualSearchRankerLoggerImpl implements ContextualSearchRankerL ogger { | 14 public class ContextualSearchRankerLoggerImpl implements ContextualSearchRankerL ogger { |
| 13 private static final String TAG = "ContextualSearch"; | 15 private static final String TAG = "ContextualSearch"; |
| 14 | 16 |
| 15 @Override | 17 // The current version of the Ranker model that we use for Contextual Search . |
| 16 public void log(Feature feature, Object value) { | 18 private static final int CONTEXTUAL_SEARCH_RANKER_MODEL_VERSION = 0; |
| 17 // TODO(donnd): log to an actual persistent proto ASAP! | 19 |
| 18 Log.v(TAG, "log %s with value %s", feature.toString(), value); | 20 // Pointer to the native instance of this class. |
| 21 private long mNativePointer; | |
| 22 | |
| 23 // The URL of the base page to log with Ranker. | |
| 24 private URL mBasePageUrl; | |
| 25 | |
| 26 public ContextualSearchRankerLoggerImpl() { | |
| 27 if (isEnabled()) { | |
| 28 mNativePointer = nativeInit(); | |
| 29 if (mBasePageUrl != null) { | |
|
Theresa
2017/05/12 01:02:25
Won't mBasePageUrl will always be null in the cons
Donn Denman
2017/05/12 23:08:55
You're right, we can remove this section. Done.
| |
| 30 nativeSetUkmServiceAndUrls( | |
| 31 mNativePointer, mBasePageUrl.toString(), getContextualSe archRankerUrl()); | |
| 32 } | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 /** | |
| 37 * This method should be called to clean up storage when an instance of this class is | |
| 38 * no longer in use. The nativeDestroy will call the destructor on the nati ve instance. | |
| 39 */ | |
| 40 void destroy() { | |
| 41 if (isEnabled()) { | |
| 42 assert mNativePointer != 0; | |
| 43 writeLogAndReset(); | |
| 44 nativeDestroy(mNativePointer); | |
| 45 mNativePointer = 0; | |
| 46 } | |
| 19 } | 47 } |
| 20 | 48 |
| 21 @Override | 49 @Override |
| 50 public void setBasePageUrl(URL basePageUrl) { | |
| 51 if (isEnabled()) { | |
| 52 mBasePageUrl = basePageUrl; | |
| 53 if (mBasePageUrl != null) { | |
|
Donn Denman
2017/05/12 23:08:55
I noticed that this is null for URIs that are not
| |
| 54 nativeSetUkmServiceAndUrls( | |
| 55 mNativePointer, mBasePageUrl.toString(), getContextualSe archRankerUrl()); | |
| 56 } | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 @Override | |
| 61 public void log(Feature feature, Object value) { | |
| 62 if (!isEnabled()) { | |
| 63 Log.v(TAG, "Ranker data %s: %s", feature, value); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 if (value instanceof String) { | |
| 68 nativeLogString(mNativePointer, feature.toString(), value.toString() ); | |
|
Roger McFarlane (Chromium)
2017/05/12 15:43:26
Do we have go ahead to do string logging like this
Donn Denman
2017/05/12 23:08:55
Good point!
Removed all string logging from this
| |
| 69 } else if (value instanceof Boolean) { | |
| 70 nativeLogLong(mNativePointer, feature.toString(), ((boolean) value ? 1 : 0)); | |
| 71 } else if (value instanceof Integer) { | |
| 72 Integer i = (int) value; | |
| 73 nativeLogLong(mNativePointer, feature.toString(), Long.valueOf(i)); | |
| 74 } else if (value instanceof Long) { | |
| 75 nativeLogLong(mNativePointer, feature.toString(), (long) value); | |
| 76 } else if (value instanceof Character) { | |
| 77 nativeLogLong( | |
| 78 mNativePointer, feature.toString(), Character.getNumericValu e((char) value)); | |
| 79 } else { | |
| 80 Log.w(TAG, | |
| 81 "Could not log feature to Ranker: " + feature.toString() + " of class " | |
| 82 + value.getClass()); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 @Override | |
| 22 public void logOutcome(Object value) { | 87 public void logOutcome(Object value) { |
| 23 log(Feature.OUTCOME_WAS_PANEL_OPENED, value); | 88 log(Feature.OUTCOME_WAS_PANEL_OPENED, value); |
| 24 } | 89 } |
| 25 | 90 |
| 26 @Override | 91 @Override |
| 27 public void writeLogAndReset() { | 92 public void writeLogAndReset() { |
| 28 Log.v(TAG, "Reset!\n"); | 93 if (isEnabled()) nativeWriteLogAndReset(mNativePointer); |
| 29 } | 94 } |
| 95 | |
| 96 // Whether actually writing data is enabled. If not, we may do nothing or p rint. | |
| 97 private boolean isEnabled() { | |
| 98 return ContextualSearchFieldTrial.isRankerLoggingEnabled(); | |
| 99 } | |
| 100 | |
| 101 // Gets the URL to use for the current Ranker model. | |
| 102 private String getContextualSearchRankerUrl() { | |
| 103 return "org.chromium.chrome.browser.contextualsearch.v" | |
| 104 + CONTEXTUAL_SEARCH_RANKER_MODEL_VERSION; | |
| 105 } | |
| 106 | |
| 107 // ========================================================================= =================== | |
| 108 // Native methods. | |
| 109 // ========================================================================= =================== | |
| 110 private native long nativeInit(); | |
| 111 private native void nativeDestroy(long nativeContextualSearchRankerLoggerImp l); | |
| 112 private native void nativeLogLong( | |
| 113 long nativeContextualSearchRankerLoggerImpl, String featureString, l ong value); | |
| 114 private native void nativeLogString( | |
| 115 long nativeContextualSearchRankerLoggerImpl, String featureString, S tring value); | |
| 116 private native void nativeSetUkmServiceAndUrls( | |
| 117 long nativeContextualSearchRankerLoggerImpl, String basePageUrl, Str ing modelUrl); | |
| 118 private native void nativeWriteLogAndReset(long nativeContextualSearchRanker LoggerImpl); | |
| 30 } | 119 } |
| OLD | NEW |