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
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.contextualsearch;
6 
7 import java.util.HashSet;
8 import java.util.Set;
9 
10 /**
11  * A set of {@link ContextualSearchHeuristic}s that support experimentation and logging for Tap
12  * suppression.
13  */
14 public class ContextualSearchHeuristics {
15     protected Set<ContextualSearchHeuristic> mHeuristics;
16     private QuickAnswersHeuristic mQuickAnswersHeuristic;
17 
18     /**
19      * Manages a set of heuristics.
20      */
ContextualSearchHeuristics()21     ContextualSearchHeuristics() {
22         mHeuristics = new HashSet<ContextualSearchHeuristic>();
23     }
24 
25     /**
26      * Logs the results seen for the heuristics and whether they would have activated if enabled.
27      * @param wasSearchContentViewSeen Whether the panel contents were seen.
28      * @param wasActivatedByTap Whether the panel was activated by a Tap or not.
29      */
logResultsSeen(boolean wasSearchContentViewSeen, boolean wasActivatedByTap)30     public void logResultsSeen(boolean wasSearchContentViewSeen, boolean wasActivatedByTap) {
31         for (ContextualSearchHeuristic heuristic : mHeuristics) {
32             heuristic.logResultsSeen(wasSearchContentViewSeen, wasActivatedByTap);
33         }
34     }
35 
36     /**
37      * Optionally logs data about the duration the panel was viewed and /or opened.
38      * Default is to not log anything.
39      * @param panelViewDurationMs The duration that the panel was viewed (Peek and opened) by the
40      *        user.  This should always be a positive number, since this method is only called when
41      *        the panel has been viewed (Peeked).
42      * @param panelOpenDurationMs The duration that the panel was opened, or 0 if it was never
43      *        opened.
44      */
logPanelViewedDurations(long panelViewDurationMs, long panelOpenDurationMs)45     public void logPanelViewedDurations(long panelViewDurationMs, long panelOpenDurationMs) {
46         for (ContextualSearchHeuristic heuristic : mHeuristics) {
47             heuristic.logPanelViewedDurations(panelViewDurationMs, panelOpenDurationMs);
48         }
49     }
50 
51     /**
52      * Logs the condition state for all the Tap suppression heuristics.
53      */
logContitionState()54     public void logContitionState() {
55         for (ContextualSearchHeuristic heuristic : mHeuristics) {
56             heuristic.logConditionState();
57         }
58     }
59 
60     /**
61      * Adds the given heuristic to the current set being managed.
62      * @param heuristicToAdd Another heuristic to manage.
63      */
add(ContextualSearchHeuristic heuristicToAdd)64     void add(ContextualSearchHeuristic heuristicToAdd) {
65         mHeuristics.add(heuristicToAdd);
66     }
67 
68     /**
69      * @return Whether any heuristic that should be considered for aggregate tap suppression logging
70      *         is satisfied regardless of whether the tap was actually suppressed.
71      */
isAnyConditionSatisfiedForAggregrateLogging()72     public boolean isAnyConditionSatisfiedForAggregrateLogging() {
73         for (ContextualSearchHeuristic heuristic : mHeuristics) {
74             if (heuristic.shouldAggregateLogForTapSuppression()
75                     && heuristic.isConditionSatisfiedForAggregateLogging()) {
76                 return true;
77             }
78         }
79         return false;
80     }
81 
82     /**
83      * Logs all the heuristics that want to provide a Ranker "feature" to the given recorder.
84      * @param recorder The recorder to log to.
85      */
logRankerTapSuppression(ContextualSearchInteractionRecorder recorder)86     public void logRankerTapSuppression(ContextualSearchInteractionRecorder recorder) {
87         for (ContextualSearchHeuristic heuristic : mHeuristics) {
88             heuristic.logRankerTapSuppression(recorder);
89         }
90     }
91 
92     /**
93      * Logs all the heuristics that want to provide outcomes to Ranker to the given recorder.
94      * @param recorder The logger to log to.
95      */
logRankerTapSuppressionOutcome(ContextualSearchInteractionRecorder recorder)96     public void logRankerTapSuppressionOutcome(ContextualSearchInteractionRecorder recorder) {
97         for (ContextualSearchHeuristic heuristic : mHeuristics) {
98             heuristic.logRankerTapSuppressionOutcome(recorder);
99         }
100     }
101 
102     /**
103      * Sets the {@link QuickAnswersHeuristic} so that it can be accessed externally by
104      * {@link #getQuickAnswersHeuristic}.
105      * @param quickAnswersHeuristic The active {@link QuickAnswersHeuristic}.
106      */
setQuickAnswersHeuristic(QuickAnswersHeuristic quickAnswersHeuristic)107     public void setQuickAnswersHeuristic(QuickAnswersHeuristic quickAnswersHeuristic) {
108         mQuickAnswersHeuristic = quickAnswersHeuristic;
109     }
110 
111     /**
112      * @return The active {@link QuickAnswersHeuristic}.
113      */
getQuickAnswersHeuristic()114     public QuickAnswersHeuristic getQuickAnswersHeuristic() {
115         return mQuickAnswersHeuristic;
116     }
117 }
118