1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_RANKER_LOGGER_IMPL_H_ 6 #define CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_RANKER_LOGGER_IMPL_H_ 7 8 #include "base/android/jni_android.h" 9 #include "base/memory/weak_ptr.h" 10 #include "services/metrics/public/cpp/ukm_source_id.h" 11 12 namespace content { 13 class BrowserContext; 14 class WebContents; 15 } // namespace content 16 17 namespace assist_ranker { 18 class BinaryClassifierPredictor; 19 class RankerExample; 20 } // namespace assist_ranker 21 22 // A Java counterpart will be generated for this enum. 23 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.contextualsearch 24 enum AssistRankerPrediction { 25 ASSIST_RANKER_PREDICTION_UNDETERMINED, 26 ASSIST_RANKER_PREDICTION_UNAVAILABLE, 27 ASSIST_RANKER_PREDICTION_SUPPRESS, 28 ASSIST_RANKER_PREDICTION_SHOW, 29 }; 30 31 // Provides the native portion of the Java class by the same name. 32 // Runs Ranker inference and logging through UKM for Ranker model development. 33 // This is used to prediction whether a tap gesture will be useful to the user 34 // or not and possible suppression. 35 class ContextualSearchRankerLoggerImpl { 36 public: 37 ContextualSearchRankerLoggerImpl(JNIEnv* env, jobject obj); 38 ~ContextualSearchRankerLoggerImpl(); 39 40 // Calls the destructor. Should be called when this native object is no 41 // longer needed. 42 void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj); 43 44 // Sets up the logging and Ranker for Contextual Search features using the 45 // given details. 46 // |java_web_contents| is the |WebContents| of the base-page (where the user 47 // tapped). 48 void SetupLoggingAndRanker( 49 JNIEnv* env, 50 jobject obj, 51 const base::android::JavaParamRef<jobject>& java_web_contents); 52 53 // Logs an int32 value with the given feature name. 54 void LogInt32(JNIEnv* env, 55 jobject obj, 56 const base::android::JavaParamRef<jstring>& j_feature, 57 jint j_int); 58 59 // Runs the model and returns the inference result as an 60 // AssistRankerPrediction enum. 61 AssistRankerPrediction RunInference(JNIEnv* env, jobject obj); 62 63 // Writes the currently logged data and resets the current builder to be 64 // ready to start logging the next set of data. 65 void WriteLogAndReset(JNIEnv* env, jobject obj); 66 67 // Returns whether or not AssistRanker query is enabled. 68 bool IsQueryEnabled(JNIEnv* env, jobject obj); 69 70 private: 71 // Returns whether or not AssistRanker query is enabled. 72 bool IsQueryEnabledInternal(); 73 74 // Adds feature to the RankerExample. 75 void LogFeature(const std::string& feature_name, int value); 76 77 // Sets up the Ranker Predictor for the given |web_contents|. 78 void SetupRankerPredictor(content::WebContents& web_contents); 79 80 // Logs to UMA when an important feature or outcome is present in the example. 81 void logImportantFeaturePresent(const std::string& feature, 82 bool is_outcome) const; 83 84 // The source_id for UKMs for the current page. 85 ukm::SourceId source_id_ = ukm::kInvalidSourceId; 86 87 // The Ranker Predictor for whether a tap gesture should be suppressed or not. 88 base::WeakPtr<assist_ranker::BinaryClassifierPredictor> predictor_; 89 90 // The |BrowserContext| currently associated with the above predictor. 91 // The object not owned by ContextualSearchRankerLoggerImpl. 92 content::BrowserContext* browser_context_ = nullptr; 93 94 // The current RankerExample or null. 95 // Set of features from one example of a Tap to predict a suppression 96 // decision. 97 std::unique_ptr<assist_ranker::RankerExample> ranker_example_; 98 99 // Whether Ranker has predicted the decision yet. 100 bool has_predicted_decision_ = false; 101 102 // The linked Java object. 103 base::android::ScopedJavaGlobalRef<jobject> java_object_; 104 105 DISALLOW_COPY_AND_ASSIGN(ContextualSearchRankerLoggerImpl); 106 }; 107 108 #endif // CHROME_BROWSER_ANDROID_CONTEXTUALSEARCH_CONTEXTUAL_SEARCH_RANKER_LOGGER_IMPL_H_ 109