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 // Whether logging for the current URL is currently setup. |
| 24 private boolean mIsLoggingSetup; |
| 25 |
| 26 // Whether the service is ready to actually record log data. |
| 27 private boolean mCanServiceActuallyRecord; |
| 28 |
| 29 // Whether any data has been written to the log since calling setupLoggingFo
rPage(). |
| 30 private boolean mDidLog; |
| 31 |
| 32 /** |
| 33 * Constructs a Ranker Logger and associated native implementation to write
Contextual Search |
| 34 * ML data to Ranker. |
| 35 */ |
| 36 public ContextualSearchRankerLoggerImpl() { |
| 37 if (isEnabled()) mNativePointer = nativeInit(); |
| 38 } |
| 39 |
| 40 /** |
| 41 * This method should be called to clean up storage when an instance of this
class is |
| 42 * no longer in use. The nativeDestroy will call the destructor on the nati
ve instance. |
| 43 */ |
| 44 void destroy() { |
| 45 if (isEnabled()) { |
| 46 assert mNativePointer != 0; |
| 47 writeLogAndReset(); |
| 48 nativeDestroy(mNativePointer); |
| 49 mNativePointer = 0; |
| 50 mIsLoggingSetup = false; |
| 51 mCanServiceActuallyRecord = false; |
| 52 mDidLog = false; |
| 53 } |
19 } | 54 } |
20 | 55 |
21 @Override | 56 @Override |
22 public void logOutcome(Object value) { | 57 public void setupLoggingForPage(URL basePageUrl) { |
23 log(Feature.OUTCOME_WAS_PANEL_OPENED, value); | 58 if (isEnabled()) { |
| 59 mIsLoggingSetup = true; |
| 60 // The URL may be null for custom Chrome URIs like chrome://flags. |
| 61 if (basePageUrl != null) { |
| 62 nativeSetupLoggingAndRanker(mNativePointer, basePageUrl.toString
(), |
| 63 getContextualSearchRankerModelSelector()); |
| 64 mCanServiceActuallyRecord = true; |
| 65 } |
| 66 } |
| 67 } |
| 68 |
| 69 @Override |
| 70 public void log(Feature feature, Object value) { |
| 71 if (!isEnabled()) { |
| 72 Log.v(TAG, "Ranker data %s: %s", feature, value); |
| 73 return; |
| 74 } |
| 75 |
| 76 // TODO(donnd): Add some enforcement that log() calls are done before in
ference time. |
| 77 assert mIsLoggingSetup; |
| 78 if (value instanceof Boolean) { |
| 79 logToNative(feature.toString(), ((boolean) value ? 1 : 0)); |
| 80 } else if (value instanceof Integer) { |
| 81 Integer i = (int) value; |
| 82 logToNative(feature.toString(), Long.valueOf(i)); |
| 83 } else if (value instanceof Long) { |
| 84 logToNative(feature.toString(), (long) value); |
| 85 } else if (value instanceof Character) { |
| 86 logToNative(feature.toString(), Character.getNumericValue((char) val
ue)); |
| 87 } else { |
| 88 Log.w(TAG, |
| 89 "Could not log feature to Ranker: " + feature.toString() + "
of class " |
| 90 + value.getClass()); |
| 91 } |
| 92 } |
| 93 |
| 94 @Override |
| 95 public void logOutcome(Feature feature, Object value) { |
| 96 log(feature, value); |
24 } | 97 } |
25 | 98 |
26 @Override | 99 @Override |
27 public void writeLogAndReset() { | 100 public void writeLogAndReset() { |
28 Log.v(TAG, "Reset!\n"); | 101 if (isEnabled() && mDidLog) { |
| 102 nativeWriteLogAndReset(mNativePointer); |
| 103 mIsLoggingSetup = false; |
| 104 mCanServiceActuallyRecord = false; |
| 105 mDidLog = false; |
| 106 } |
29 } | 107 } |
| 108 |
| 109 /** Whether actually writing data is enabled. If not, we may do nothing or
print. */ |
| 110 private boolean isEnabled() { |
| 111 return ContextualSearchFieldTrial.isRankerLoggingEnabled(); |
| 112 } |
| 113 |
| 114 /** |
| 115 * Logs to the native instance. All native logging must go through this bot
tleneck. |
| 116 * @param feature The feature to log. |
| 117 * @param value The value to log. |
| 118 */ |
| 119 private void logToNative(String feature, long value) { |
| 120 if (mCanServiceActuallyRecord) { |
| 121 nativeLogLong(mNativePointer, feature, value); |
| 122 mDidLog = true; |
| 123 } |
| 124 } |
| 125 |
| 126 /** Gets the model selector that specifies which model to use for Ranker. */ |
| 127 private String getContextualSearchRankerModelSelector() { |
| 128 return "org.chromium.chrome.browser.contextualsearch.v" |
| 129 + CONTEXTUAL_SEARCH_RANKER_MODEL_VERSION; |
| 130 } |
| 131 |
| 132 // =========================================================================
=================== |
| 133 // Native methods. |
| 134 // =========================================================================
=================== |
| 135 private native long nativeInit(); |
| 136 private native void nativeDestroy(long nativeContextualSearchRankerLoggerImp
l); |
| 137 private native void nativeLogLong( |
| 138 long nativeContextualSearchRankerLoggerImpl, String featureString, l
ong value); |
| 139 private native void nativeSetupLoggingAndRanker(long nativeContextualSearchR
ankerLoggerImpl, |
| 140 String basePageUrl, String rankerModelSelector); |
| 141 private native void nativeWriteLogAndReset(long nativeContextualSearchRanker
LoggerImpl); |
30 } | 142 } |
OLD | NEW |