1 // Copyright 2014 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.
5 #ifndef COMPONENTS_OMNIBOX_AUTOCOMPLETE_PROVIDER_H_
6 #define COMPONENTS_OMNIBOX_AUTOCOMPLETE_PROVIDER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/string16.h"
11 #include "components/metrics/proto/omnibox_event.pb.h"
12 #include "components/omnibox/autocomplete_match.h"
14 class AutocompleteInput
;
16 typedef std::vector
<metrics::OmniboxEventProto_ProviderInfo
> ProvidersInfo
;
18 // The AutocompleteProviders each return different kinds of matches,
19 // such as history or search matches. These matches are given
20 // "relevance" scores. Higher scores are better matches than lower
21 // scores. The relevance scores and classes providing the respective
22 // matches are as listed below.
24 // IMPORTANT CAVEAT: The tables below are NOT COMPLETE. Developers
25 // often forget to keep these tables in sync with the code when they
26 // change scoring algorithms or add new providers. For example,
27 // neither the HistoryQuickProvider (which is a provider that appears
28 // often) nor the ShortcutsProvider are listed here. For the best
29 // idea of how scoring works and what providers are affecting which
30 // queries, play with chrome://omnibox/ for a while. While the tables
31 // below may have some utility, nothing compares with first-hand
32 // investigation and experience.
34 // UNKNOWN input type:
35 // --------------------------------------------------------------------|-----
36 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
37 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
38 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
39 // Search Primary Provider (past query in history within 2 days) | 1399**
40 // Search Primary Provider (what you typed) | 1300
41 // HistoryURL (what you typed, some inexact matches) | 1200++
42 // Keyword (substituting, exact match) | 1100
43 // Search Primary Provider (past query in history older than 2 days) | 1050--
44 // HistoryURL (some inexact matches) | 900++
45 // BookmarkProvider (prefix match in bookmark title) | 900+-
47 // Search Primary Provider (navigational suggestion) | 800++
48 // Search Primary Provider (suggestion) | 600++
49 // Keyword (inexact match) | 450
50 // Search Secondary Provider (what you typed) | 250
51 // Search Secondary Provider (past query in history) | 200--
52 // Search Secondary Provider (navigational suggestion) | 150++
53 // Search Secondary Provider (suggestion) | 100++
56 // --------------------------------------------------------------------|-----
57 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
58 // HistoryURL (good exact or inline autocomplete matches, some inexact)| 1410++
59 // HistoryURL (intranet url never visited match, some inexact matches) | 1400++
60 // HistoryURL (what you typed, some inexact matches) | 1200++
61 // Keyword (substituting, exact match) | 1100
62 // HistoryURL (some inexact matches) | 900++
64 // Search Primary Provider (what you typed) | 850
65 // Search Primary Provider (navigational suggestion) | 800++
66 // Search Primary Provider (past query in history) | 750--
67 // Keyword (inexact match) | 700
68 // Search Primary Provider (suggestion) | 300++
69 // Search Secondary Provider (what you typed) | 250
70 // Search Secondary Provider (past query in history) | 200--
71 // Search Secondary Provider (navigational suggestion) | 150++
72 // Search Secondary Provider (suggestion) | 100++
75 // --------------------------------------------------------------------|-----
76 // Search Primary or Secondary (past query in history within 2 days) | 1599**
77 // Keyword (non-substituting or in keyword UI mode, exact match) | 1500
78 // Keyword (substituting, exact match) | 1450
79 // Search Primary Provider (past query in history within 2 days) | 1399**
80 // Search Primary Provider (what you typed) | 1300
81 // Search Primary Provider (past query in history older than 2 days) | 1050--
82 // HistoryURL (inexact match) | 900++
83 // BookmarkProvider (prefix match in bookmark title) | 900+-
84 // Search Primary Provider (navigational suggestion) | 800++
85 // Search Primary Provider (suggestion) | 600++
86 // Keyword (inexact match) | 450
87 // Search Secondary Provider (what you typed) | 250
88 // Search Secondary Provider (past query in history) | 200--
89 // Search Secondary Provider (navigational suggestion) | 150++
90 // Search Secondary Provider (suggestion) | 100++
92 // FORCED_QUERY input type:
93 // --------------------------------------------------------------------|-----
94 // Search Primary Provider (past query in history within 2 days) | 1399**
95 // Search Primary Provider (what you typed) | 1300
96 // Search Primary Provider (past query in history older than 2 days) | 1050--
97 // Search Primary Provider (navigational suggestion) | 800++
98 // Search Primary Provider (suggestion) | 600++
100 // (A search keyword is a keyword with a replacement string; a bookmark keyword
101 // is a keyword with no replacement string, that is, a shortcut for a URL.)
103 // There are two possible providers for search suggestions. If the user has
104 // typed a keyword, then the primary provider is the keyword provider and the
105 // secondary provider is the default provider. If the user has not typed a
106 // keyword, then the primary provider corresponds to the default provider.
108 // Search providers may supply relevance values along with their results to be
109 // used in place of client-side calculated values.
111 // The value column gives the ranking returned from the various providers.
112 // ++: a series of matches with relevance from n up to (n + max_matches).
113 // --: relevance score falls off over time (discounted 50 points @ 15 minutes,
114 // 450 points @ two weeks)
115 // **: relevance score falls off over two days (discounted 99 points after two
117 // +-: A base score that the provider will adjust upward or downward based on
118 // provider-specific metrics.
120 // A single result provider for the autocomplete system. Given user input, the
121 // provider decides what (if any) matches to return, their relevance, and their
123 class AutocompleteProvider
124 : public base::RefCountedThreadSafe
<AutocompleteProvider
> {
126 // Different AutocompleteProvider implementations.
128 TYPE_BOOKMARK
= 1 << 0,
129 TYPE_BUILTIN
= 1 << 1,
130 TYPE_HISTORY_QUICK
= 1 << 2,
131 TYPE_HISTORY_URL
= 1 << 3,
132 TYPE_KEYWORD
= 1 << 4,
133 TYPE_SEARCH
= 1 << 5,
134 TYPE_SHORTCUTS
= 1 << 6,
135 TYPE_ZERO_SUGGEST
= 1 << 7,
138 explicit AutocompleteProvider(Type type
);
140 // Returns a string describing a particular AutocompleteProvider type.
141 static const char* TypeToString(Type type
);
143 // Called to start an autocomplete query. The provider is responsible for
144 // tracking its matches for this query and whether it is done processing the
145 // query. When new matches are available or the provider finishes, it
146 // calls the controller's OnProviderUpdate() method. The controller can then
147 // get the new matches using the provider's accessors.
148 // Exception: Matches available immediately after starting the query (that
149 // is, synchronously) do not cause any notifications to be sent. The
150 // controller is expected to check for these without prompting (since
151 // otherwise, starting each provider running would result in a flurry of
154 // Once Stop() has been called, usually no more notifications should be sent.
155 // (See comments on Stop() below.)
157 // |minimal_changes| is an optimization that lets the provider do less work
158 // when the |input|'s text hasn't changed. See the body of
159 // OmniboxPopupModel::StartAutocomplete().
161 // |called_due_to_focus| is true when Start() is being called in response to
162 // the omnibox being focused, instead of due to e.g. user input changes. Most
163 // providers should not provide matches in this case. Providers which want to
164 // display matches on focus can use this flag to know when they can do so.
165 virtual void Start(const AutocompleteInput
& input
,
166 bool minimal_changes
,
167 bool called_due_to_focus
) = 0;
169 // Advises the provider to stop processing. This may be called even if the
170 // provider is already done. If the provider caches any results, it should
171 // clear the cache based on the value of |clear_cached_results|. Normally,
172 // once this is called, the provider should not send more notifications to
175 // If |user_inactivity_timer| is true, Stop() is being called because it's
176 // been a long time since the user started the current query, and returning
177 // further asynchronous results would normally just be disruptive. Most
178 // providers should still stop processing in this case, but continuing is
179 // legal if there's a good reason the user is likely to want even long-
180 // delayed asynchronous results, e.g. the user has explicitly invoked a
181 // keyword extension and the extension is still processing the request.
182 virtual void Stop(bool clear_cached_results
,
183 bool due_to_user_inactivity
);
185 // Returns the enum equivalent to the name of this provider.
186 // TODO(derat): Make metrics use AutocompleteProvider::Type directly, or at
187 // least move this method to the metrics directory.
188 metrics::OmniboxEventProto_ProviderType
AsOmniboxEventProviderType() const;
190 // Called to delete a match and the backing data that produced it. This
191 // match should not appear again in this or future queries. This can only be
192 // called for matches the provider marks as deletable. This should only be
193 // called when no query is running.
194 // NOTE: Do NOT call OnProviderUpdate() in this method, it is the
195 // responsibility of the caller to do so after calling us.
196 virtual void DeleteMatch(const AutocompleteMatch
& match
);
198 // Called when an omnibox event log entry is generated. This gives
199 // a provider the opportunity to add diagnostic information to the
200 // logs. A provider is expected to append a single entry of whatever
201 // information it wants to |provider_info|.
202 virtual void AddProviderInfo(ProvidersInfo
* provider_info
) const;
204 // Called when a new omnibox session starts or the current session ends.
205 // This gives the opportunity to reset the internal state, if any, associated
206 // with the previous session.
207 virtual void ResetSession();
209 // Returns the set of matches for the current query.
210 const ACMatches
& matches() const { return matches_
; }
212 // Returns whether the provider is done processing the query.
213 bool done() const { return done_
; }
215 // Returns this provider's type.
216 Type
type() const { return type_
; }
218 // Returns a string describing this provider's type.
219 const char* GetName() const;
221 // A suggested upper bound for how many matches a provider should return.
222 // TODO(pkasting): http://b/1111299 , http://b/933133 This should go away once
223 // we have good relevance heuristics; the controller should handle all
225 static const size_t kMaxMatches
;
228 friend class base::RefCountedThreadSafe
<AutocompleteProvider
>;
229 FRIEND_TEST_ALL_PREFIXES(BookmarkProviderTest
, InlineAutocompletion
);
231 typedef std::pair
<bool, base::string16
> FixupReturn
;
233 virtual ~AutocompleteProvider();
235 // Fixes up user URL input to make it more possible to match against. Among
236 // many other things, this takes care of the following:
237 // * Prepending file:// to file URLs
238 // * Converting drive letters in file URLs to uppercase
239 // * Converting case-insensitive parts of URLs (like the scheme and domain)
241 // * Convert spaces to %20s
242 // Note that we don't do this in AutocompleteInput's constructor, because if
243 // e.g. we convert a Unicode hostname to punycode, other providers will show
244 // output that surprises the user ("Search Google for xn--6ca.com").
245 // Returns a bool indicating whether fixup succeeded, as well as the fixed-up
246 // input text. The returned string will be the same as the input string if
247 // fixup failed; this lets callers who don't care about failure simply use the
248 // string unconditionally.
249 static FixupReturn
FixupUserInput(const AutocompleteInput
& input
);
251 // Trims "http:" and up to two subsequent slashes from |url|. Returns the
252 // number of characters that were trimmed.
253 // NOTE: For a view-source: URL, this will trim from after "view-source:" and
255 static size_t TrimHttpPrefix(base::string16
* url
);
263 DISALLOW_COPY_AND_ASSIGN(AutocompleteProvider
);
266 #endif // COMPONENTS_OMNIBOX_AUTOCOMPLETE_PROVIDER_H_