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